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.KuaishouProjectStrategy;
import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouProjectStrategyService;
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 KuaishouAiProjectCreativeJob {

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

    @Autowired
    private IKuaishouProjectStrategyService kuaishouProjectStrategyService;

    /**
     * 项目自定义上新
     */
    @XxlJob("projectCustomCreativeOnNew")
    public void projectCustomCreativeOnNew() {
        List<KuaishouProjectStrategy> strategies = kuaishouProjectStrategyService.getAllEffectStrategy(4, "1");
        if (null == strategies || strategies.isEmpty()) {
            return;
        }
        String url = jobUrl + "/jeecg-boot/ai/projectCreate/projectCreative";
        JSONObject jsonObject = null;
        try {
            for (KuaishouProjectStrategy strategy : strategies) {
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("id", strategy.getId());
                String result = HttpUtils2.httpGet(url, requestMap, null);
                jsonObject = JSONObject.parseObject(result);
                if (!Check.isNull(jsonObject)) {
                    Integer code = jsonObject.getInteger("code");
                    if (!Check.isNull(code) && code == 0) {
                        log.info("快手自动上新异步执行中，accountId：{}", strategy.getProjectId());
                    } else {
                        log.error("快手自动上新异步执行失败，accountId：{},结果：{}", strategy.getProjectId(), jsonObject);
                    }
                } else {
                    log.info("快手自动上新返回结果为空，accountId：{},结果：{}", strategy.getProjectId(), jsonObject);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("自定义上新异常，返回结果：{}", jsonObject);
        }
    }

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

    /**
     * 项目程序化上新创意
     */
    @XxlJob("projectProgramCreativeAuto")
    public void kuaishouProgramCreativeAuto() {
        List<KuaishouProjectStrategy> strategies = kuaishouProjectStrategyService.getAllEffectStrategy(7, "1");
        if (null == strategies || strategies.isEmpty()) {
            return;
        }
        JSONObject jsonObject = null;
        try {
            String url = jobUrl + "/jeecg-boot/ai/projectCreate/projectProgramAutomaticCreates";
            for (KuaishouProjectStrategy strategy : strategies) {
                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.parseObject(result);
                if (!Check.isNull(jsonObject)) {
                    Integer code = jsonObject.getInteger("code");
                    if (!Check.isNull(code) && code == 0) {
                        log.info("程序化创意上新异步执行中，accountId：{}", strategy.getAccountId());
                    } else {
                        log.error("程序化创意上新异步执行失败，accountId：{},结果：{}", strategy.getAccountId(), jsonObject);
                    }
                } else {
                    log.error("程序化创意上新返回结果为空，accountId：{},结果：{}", strategy.getAccountId(), jsonObject);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("程序化创意上新异常，返回结果：{}", jsonObject);
        }
    }

    /**
     * 项目程序化高质量创意
     */
    @XxlJob("projectProgramCreativeTop")
    public void kuaishouProgramCreativeTop() {
        List<KuaishouProjectStrategy> strategies = kuaishouProjectStrategyService.getAllEffectStrategy(7, "2");
        if (null == strategies || strategies.isEmpty()) {
            return;
        }
        JSONObject jsonObject = null;

        try {
            String url = jobUrl + "/jeecg-boot/ai/projectCreate/projectProgramCreativeTop";
            for (KuaishouProjectStrategy strategy : strategies) {
                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.parseObject(result);
                if (!Check.isNull(jsonObject)) {
                    Integer code = jsonObject.getInteger("code");
                    if (!Check.isNull(code) && code == 0) {
                        log.info("程序化高质量创建异步执行中，accountId：{}", strategy.getAccountId());
                    } else {
                        log.error("程序化高质量创建异步执行失败，accountId：{},结果：{}", strategy.getAccountId(), jsonObject);
                    }
                } else {
                    log.error("程序化高质量创建返回结果为空，accountId：{},结果：{}", strategy.getAccountId(), jsonObject);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("程序化高质量创建异常，返回结果：{}", jsonObject);
        }

    }
}
