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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAccountAutoStrategy;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouAccountAutoDoService;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouAccountAutoStrategyService;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@Api(tags = "快手-自动投放——重构")
@RestController
@RequestMapping("/auto/create")
public class AiKuaishouAccountAutoDoController {
    @Autowired
    private IAiKuaishouAccountAutoDoService autoDoService;
    @Autowired
    private IAiKuaishouAccountAutoStrategyService strategyService;
    static ExecutorService customService = Executors.newFixedThreadPool(8);
    static ExecutorService programService = Executors.newFixedThreadPool(8);

    /**
     * 创建自定义上新广告创意 、频率 20分钟/次
     */
    @GetMapping(value = "/createCustomUpNewCreative")
    public JSONObject createCustomUpNewCreative(String id) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(id)) {
                throw new Exception("主键id不能为空");
            }
            AiKuaishouAccountAutoStrategy strategy = strategyService.getById(id);
            if (Check.isNull(strategy)) {
                throw new Exception("根据id获取详细信息为空");
            }
            Boolean unitOverrun = autoDoService.getUnitOverrun(strategy.getAccountId());
            if (unitOverrun) {
                log.error("****** 广告组创建超限,accountId:{}", strategy.getAccountId());
                throw new Exception("今日广告组创建已超限");
            }
            customService.submit(() -> autoDoService.createCustomUpNewCreative(strategy));
            returnJson.put("code", 0);
            returnJson.put("message", "异步创建中");
        } catch (Exception e) {
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;
    }

    /**
     * 每日补充创建创意 、频率 每天22点05分钟执行1次
     */
    @GetMapping(value = "/supplementTodayCreatives")
    public JSONObject supplementTodayCreatives(String id) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(id)) {
                throw new Exception("主键id不能为空");
            }
            AiKuaishouAccountAutoStrategy strategy = strategyService.getById(id);
            if (Check.isNull(strategy)) {
                throw new Exception("根据id获取详细信息为空");
            }
            Boolean unitOverrun = autoDoService.getUnitOverrun(strategy.getAccountId());
            if (unitOverrun) {
                log.error("****** 广告组创建超限,accountId:{}", strategy.getAccountId());
                throw new Exception("今日广告组创建已超限");
            }
            customService.submit(() -> autoDoService.supplementTodayCreatives(strategy));
            returnJson.put("code", 0);
            returnJson.put("message", "异步创建中");
        } catch (Exception e) {
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;
    }

    /**
     * 创建自定义非上新广告创意 、频率 60分钟/次
     */
    @GetMapping(value = "/createCustomCreative")
    public JSONObject createCustomCreative(String id) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(id)) {
                throw new Exception("主键id不能为空");
            }
            AiKuaishouAccountAutoStrategy strategy = strategyService.getById(id);
            if (Check.isNull(strategy)) {
                throw new Exception("根据id获取详细信息为空");
            }
            Boolean unitOverrun = autoDoService.getUnitOverrun(strategy.getAccountId());
            if (unitOverrun) {
                log.error("****** 广告组创建超限,accountId:{}", strategy.getAccountId());
                throw new Exception("今日广告组创建已超限");
            }
            customService.submit(() -> autoDoService.createCustomCreative(strategy));
            returnJson.put("code", 0);
            returnJson.put("message", "异步创建中");
        } catch (Exception e) {
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;

    }

    /**
     * 创建程序化广告创意 频率 60分钟/次
     */
    @GetMapping(value = "/createProgramCreative")
    public JSONObject createProgramCreative(String id) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(id)) {
                throw new Exception("主键id不能为空");
            }
            AiKuaishouAccountAutoStrategy strategy = strategyService.getById(id);
            if (Check.isNull(strategy)) {
                throw new Exception("根据id获取详细信息为空");
            }
            Boolean unitOverrun = autoDoService.getUnitOverrun(strategy.getAccountId());
            if (unitOverrun) {
                log.error("组创建超限,accountId:{}", strategy.getAccountId());
                throw new Exception("今日组创建已超限");
            }
            programService.submit(() -> autoDoService.createProgramCreative(strategy));
            returnJson.put("code", 0);
            returnJson.put("message", "异步创建中");
        } catch (Exception e) {
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;

    }
}
