package cn.com.ctop.job.kuaishou.handler;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils2;
import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAdvertiserStrategy;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouAdvertiserStrategyService;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author syh
 * 自动投放定时任务
 */
@Component
@Slf4j
public class KuaishouAiCreativeJob {

    @Value("${xxl-job.requestUrl}")
    private String jobUrl;

    @Autowired
    private IAiKuaishouAdvertiserStrategyService strategyService;

    /**
     * 自定义上新创意
     */
    @XxlJob("kuaishouCustomCreativeAuto")
    public void kuaishouCustomCreativeAuto() {
        List<AiKuaishouAdvertiserStrategy> strategies = strategyService.getAllEffectStrategy();
        if (null == strategies || strategies.isEmpty()) {
            return;
        }
        String url = jobUrl + "/jeecg-boot/ai/create/customCreativeLimit";
        for (AiKuaishouAdvertiserStrategy strategy : strategies) {
            if (null != strategy.getOpenProgramCreate() && strategy.getOpenProgramCreate() != 1) {
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("id", strategy.getId());
                String result = HttpUtils2.httpGet(url, requestMap, null);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (!Check.isNull(jsonObject)) {
                    Integer code = jsonObject.getInteger("code");
                    if (code == 0) {
                        log.info("快手自动上新异步执行中，accountId：{}", strategy.getAccountId());
                    } else {
                        log.error("快手自动上新异执行失败，accountId：{}", strategy.getAccountId());
                    }
                } else {
                    log.error("快手自动上新返回结果为空，accountId：{}", strategy.getAccountId());
                }
            }
        }


    }

    /**
     * 补充创意
     */
    @XxlJob("kuaishouCustomCreativeSupplement")
    public void kuaishouCustomCreativeSupplement() {
        List<AiKuaishouAdvertiserStrategy> strategies = strategyService.getAllEffectStrategy();
        if (null == strategies || strategies.isEmpty()) {
            return;
        }
        try {
            String url = jobUrl + "/jeecg-boot/ai/create/kuaishouCustomCreativeSupplement";
            int hour = DateUtils.getNowHour();
            for (AiKuaishouAdvertiserStrategy strategy : strategies) {
                if (null != strategy.getOpenProgramCreate() && strategy.getOpenProgramCreate() != 1) {
                    Map<String, Object> requestMap = new HashMap<>();
                    requestMap.put("id", strategy.getId());
                    requestMap.put("hour", hour);
                    String result = HttpUtils2.httpGet(url, requestMap, null);
                    JSONObject jsonObject = JSONObject.parseObject(result);
                    if (!Check.isNull(jsonObject)) {
                        Integer code = jsonObject.getInteger("code");
                        if (code == 0) {
                            log.info("快手创意补充异步执行中，accountId：{}", strategy.getAccountId());
                        } else {
                            log.error("快手创意补充异步执行失败，accountId：{}", strategy.getAccountId());
                        }
                    } else {
                        log.error("快手创意补充返回结果为空，accountId：{}", strategy.getAccountId());
                    }
                }
            }


        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * 程序化上新创意
     */
    @XxlJob("kuaishouProgramCreativeAuto")
    public void kuaishouProgramCreativeAuto() {
        List<AiKuaishouAdvertiserStrategy> strategies = strategyService.getAllEffectStrategy();
        if (null == strategies || strategies.isEmpty()) {
            return;
        }
        String url = jobUrl + "/jeecg-boot/ai/create/kuaishouProgramCreativeAuto";
        for (AiKuaishouAdvertiserStrategy strategy : strategies) {
            if (null != strategy.getOpenProgramCreate() && strategy.getOpenProgramCreate() != 0) {
                long videoCnt = strategy.getProgramUnitCnt() * 5 / 2;
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("id", strategy.getId());
                requestMap.put("videoCnt", videoCnt);
                String result = HttpUtils2.httpGet(url, requestMap, null);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (!Check.isNull(jsonObject)) {
                    Integer code = jsonObject.getInteger("code");
                    if (code == 0) {
                        log.info("快手上新程序化创意补充异步执行中，accountId：{}", strategy.getAccountId());
                    } else {
                        log.error("快手上新程序化异步执行失败，accountId：{}", strategy.getAccountId());
                    }
                } else {
                    log.error("快手上新程序化充返回结果为空，accountId：{}", strategy.getAccountId());
                }


            }
        }


    }

    /**
     * 程序化高质量创意
     */
    @XxlJob("kuaishouProgramCreativeTop")
    public void kuaishouProgramCreativeTop() {
        List<AiKuaishouAdvertiserStrategy> strategies = strategyService.getAllEffectStrategy();
        if (null == strategies || strategies.isEmpty()) {
            return;
        }
        String url = jobUrl + "/jeecg-boot/ai/create/autoCreateProgramHistoryTopCreative";
        for (AiKuaishouAdvertiserStrategy strategy : strategies) {
            if (null != strategy.getOpenProgramCreate() && strategy.getOpenProgramCreate() != 0) {
                long videoCnt = strategy.getProgramUnitCnt() * 5 / 2;
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("id", strategy.getId());
                requestMap.put("videoCnt", videoCnt);
                String result = HttpUtils2.httpGet(url, requestMap, null);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (!Check.isNull(jsonObject)) {
                    Integer code = jsonObject.getInteger("code");
                    if (code == 0) {
                        log.info("快手历史高质量程序化创意补充异步执行中，accountId：{}", strategy.getAccountId());
                    } else {
                        log.error("快手历史高质量程序化异步执行失败，accountId：{}", strategy.getAccountId());
                    }
                } else {
                    log.error("快手历史高质量程序化充返回结果为空，accountId：{}", strategy.getAccountId());
                }
            }
        }

    }
}
