package cn.com.ctop.kuaishou.modules.batch.controller;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yumeng
 * 2019年11月12日10:04:00
 */
@RestController
@RequestMapping("/kuaishou")
public class KuaiShouController {

    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;

    @RequestMapping("/campaignCreate")
    public void campaignCreate() {
        String accessToken = "f60f51a8f7d8938d6147d38357fed70c";
        Long advertiserId = 23212L;
        JSONObject requestJson = new JSONObject();
        requestJson.put("campaign_name", "Test Api");
        requestJson.put("type", 2);
        requestJson.put("day_budget", 0L);
        Map<String, Object> stringObjectMap = kuaishouInterfaceService.campaignCreate(accessToken, advertiserId, requestJson);
        System.err.println(JSON.toJSONString(stringObjectMap));
    }

    /**
     * 创建组
     */
    @RequestMapping("/adUnitCreate")
    public void adUnitCcreate() {
        String accessToken = "cbe9caeffcc2c5ccd97a05bf909896da";
        Long advertiserId = 23212L;
        JSONObject requestJson = new JSONObject();
        requestJson.put("campaign_id", 4178072L);
        requestJson.put("unit_name", "测试广告组名称");
        requestJson.put("bid_type", 2);
        requestJson.put("bid", 100000);
        requestJson.put("deep_conversion_type", 3);
        requestJson.put("page", 2);
        requestJson.put("begin_time", "2019-07-24");
        requestJson.put("end_time", "2019-07-25");
        requestJson.put("day_budget", 10000000);
        requestJson.put("app_id", 156982);
        requestJson.put("show_mode", 1);
        requestJson.put("speed", 1);
        JSONObject targetJson = new JSONObject();
        JSONObject ageJson = new JSONObject();
        ageJson.put("min", 15);
        ageJson.put("max", 50);
        targetJson.put("age", ageJson);

        targetJson.put("gender", 0);
        targetJson.put("platform_os", 0);
        targetJson.put("android_osv", 3);
        targetJson.put("network", 0);
        Map<String, Object> stringObjectMap = kuaishouInterfaceService.adUnitCreate(accessToken, advertiserId, requestJson);
        System.err.println(JSON.toJSONString(stringObjectMap));
    }


    @RequestMapping("/getCampaignList")
    public void getCampaignList() {
        String accessToken = "18cf38d50da314dcbd9cb6db9c43bcbf";
        Long advertiserId = 1595407L;
        kuaishouInterfaceService.getCampaignList(accessToken, advertiserId, DateUtils.addDay(new Date(),-1), new Date(), 1);

    }

    @RequestMapping("/getGroupList")
    public void getGroupList() {
        String accessToken = "604d2f2ffe5573c3500ffee358ba9b3c";
        Long advertiserId = 1595407L;
        kuaishouInterfaceService.getGroupList(accessToken, advertiserId, DateUtils.addDay(new Date(),-1), new Date(), 1);

    }

    @RequestMapping("/getCreativeList")
    public void getCreativeList() {
        String accessToken = "604d2f2ffe5573c3500ffee358ba9b3c";
        Long advertiserId = 1595407L;
        kuaishouInterfaceService.getCreativeList(accessToken, advertiserId, DateUtils.addDay(new Date(),-1), new Date(), 1);
    }

    @RequestMapping("/getDeepConversionInfos")
    public void getDeepConversionInfos() {
        String accessToken = "cbe9caeffcc2c5ccd97a05bf909896da";
        Long advertiserId = 23212L;
        kuaishouInterfaceService.getDeepConversionInfos(advertiserId, accessToken);
    }







    @RequestMapping("/image/get")
    public void imageGet() {
        String accessToken = "360d43b49553684a011d6607322582b1";
        Long advertiserId = 23212L;
        String imageToken = "market825e095b83eb4456a31ce95704d03238.jpg";
        kuaishouInterfaceService.imageGet(advertiserId, accessToken, imageToken);
    }


    @RequestMapping("/area/list")
    public void areaList() {
        String accessToken = "360d43b49553684a011d6607322582b1";
        Long advertiserId = 23212L;
        String imageToken = "market825e095b83eb4456a31ce95704d03238.jpg";
        kuaishouInterfaceService.areaList(accessToken);
    }


    @RequestMapping("/getAppList")
    public void getAppList() throws IOException {
        String accessToken = "360d43b49553684a011d6607322582b1";
        Long advertiserId = 23212L;
        kuaishouInterfaceService.getAppList(advertiserId, accessToken);

    }

    @RequestMapping(value = "/app/update", consumes = "multipart/form-data;charset=utf-8")
    public void appUpdate(@RequestParam("multipartFile") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        String accessToken = "360d43b49553684a011d6607322582b1";
        Long advertiserId = 23212L;
        JSONObject requestJson = new JSONObject();
        kuaishouInterfaceService.appUpdate(advertiserId, accessToken, requestJson, multipartFile, request);
    }


    @Autowired
    private ICtopOauthTokenService tokenService;

    @RequestMapping("/test")
    public void test() throws IOException {
        tokenService.getKuaiShouRefreshToken(23212L, "b51ce96a098299b04fa6cb70617e653d");

    }

    @RequestMapping("report")
    public Map<String, Object> getReport(String accountId, String startDate, String endDate, Integer type, String temporalGranularity) {
        Map<String, Object> result = new HashMap<>();
        CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId);
        if (null != type) {
            if (type == 1) {
                //广告主
                if (null != temporalGranularity && "hourly".equals(temporalGranularity)) {
                    result.put("data", kuaishouInterfaceService.getAccountHourlyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                } else {
                    result.put("data", kuaishouInterfaceService.getAccountDailyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                }
            } else if (type == 2) {
                //广告组
                if (null != temporalGranularity && "hourly".equals(temporalGranularity)) {
                    result.put("data", kuaishouInterfaceService.getGroupHourlyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                } else {
                    result.put("data", kuaishouInterfaceService.getGroupDailyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                }
            } else if (type == 3) {
                //广告计划
                if (null != temporalGranularity && "hourly".equals(temporalGranularity)) {
                    result.put("data", kuaishouInterfaceService.getCampaignHourlyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                } else {
                    result.put("data", kuaishouInterfaceService.getCampaignDailyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                }
            } else {
                //广告创意
                if (null != temporalGranularity && "hourly".equals(temporalGranularity)) {
                    result.put("data", kuaishouInterfaceService.getCreativeHourlyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                } else {
                    result.put("data", kuaishouInterfaceService.getCreativeDailyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                }
            }
        }
        return result;
    }

    @RequestMapping("advertiserInfo")
    public void advertiserInfo() {
        kuaishouInterfaceService.advertiserInfo(23212L, "10b6ad966b7e958d1510a78a5315ec5c");
    }

    @RequestMapping("getImageList")
    public void getImageList() {
        String accessToken = "604d2f2ffe5573c3500ffee358ba9b3c";
        Long advertiserId = 1595407L;
        kuaishouInterfaceService.getImageList(accessToken, advertiserId, new Date(), new Date(), 1);
    }

    @RequestMapping("getVideoList")
    public void getVideoList() {
        String accessToken = "e8959cf08e59e78b91b22aa4ed593c00";
        Long advertiserId = 1022694L;
        kuaishouInterfaceService.getVideoList(accessToken, advertiserId, DateUtils.addDay(new Date(), -1), new Date(), 1);
    }

}
