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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.ai.service.IKuaiShouCreateService;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCampaign;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCampaignService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Api(tags = "快手-批量工具")
@RestController
@RequestMapping("/kuaishou/create")
public class KuaiShouCreateController {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService iKuaishouInterfaceService;
    @Autowired
    private IKuaiShouCampaignService campaignService;
    @Autowired
    private IKuaiShouCreateService kuaiShouCreateService;
    static ExecutorService executorService = Executors.newFixedThreadPool(10);

    /**
     * 创建计划
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/campaignCreate")
    public JSONObject campaignCreate(@RequestBody JSONObject requestJson) {
        JSONObject result = new JSONObject();
        try {
            Long accountId = requestJson.getLong("account_id");
            CtopOauthToken oauthToken = tokenService.getTokenByAccountId(accountId);
            if (Check.isNull(oauthToken)) {
                throw new Exception("未获取到账户信息");
            }
            JSONObject campaignJson = new JSONObject();
            campaignJson.put("campaign_name", requestJson.getString("campaign_name"));
            campaignJson.put("type", requestJson.getInteger("type"));
            if (!Check.isNull(requestJson.getLong("day_budget"))) {
                campaignJson.put("day_budget", requestJson.getLong("day_budget"));
            }
            if (!Check.isNull(requestJson.getJSONArray("day_budget_schedule"))) {
                campaignJson.put("day_budget_schedule", requestJson.getJSONArray("day_budget_schedule"));
            }
            Map<String, Object> campaignMap = iKuaishouInterfaceService.campaignCreate(oauthToken.getAccessToken(), accountId, campaignJson);
            Integer code = (Integer) campaignMap.get("code");
            if (code != 0) {
                throw new Exception((String) campaignMap.get("message"));
            }
            Long campaignId = (Long) campaignMap.get("campaignId");
            result.put("code", 0);
            result.put("message", "SUCCESS");
            JSONObject data = new JSONObject();
            data.put("campaign_id", campaignId);
            data.put("account_id", accountId);
            KuaiShouCampaign campaign = campaignService.getCampaignByAccountAndCampaign(accountId, campaignId);
            if (!Check.isNull(campaign)) {
                data.put("campaign_create_time", campaign.getPutCreateTime());
            }
            result.put("data", data);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", -1);
            result.put("message", e.getMessage());
            JSONObject data = new JSONObject();
            data.put("account_id", requestJson.getLong("account_id"));
            result.put("data", data);
        }
        return result;
    }


    /**
     * 创建组 创意
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/createUnitAndCreative")
    public JSONObject createUnitAndCreative(@RequestBody JSONObject requestJson) {
        JSONObject result = new JSONObject();
        try {
            Long accountId = requestJson.getLong("account_id");
            if (Check.isNull(accountId)) {
                throw new Exception("请填写账户id进行创建");
            }
            CtopOauthToken oauthToken = tokenService.getTokenByAccountId(accountId);
            if (Check.isNull(oauthToken)) {
                throw new Exception("未获取到账户信息");
            }
            Long campaign_id = requestJson.getLong("campaign_id");
            if (Check.isNull(campaign_id)) {
                throw new Exception("请填写需要创建的计划id");
            }
            JSONArray group_list = requestJson.getJSONArray("group_list");
            if (Check.isNull(group_list)) {
                throw new Exception("请填写需要创建的组信息");
            }
            executorService.submit(() -> kuaiShouCreateService.createUnitAndCreative(oauthToken, campaign_id, group_list));
            result.put("code", 0);
            result.put("message", "异步执行中，等待回调...");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", -1);
            result.put("message", e.getMessage());
            JSONObject data = new JSONObject();
            data.put("account_id", requestJson.getLong("account_id"));
            result.put("data", data);
        }
        return result;
    }
}
