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

import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils2;
import cn.com.ctop.kuaishou.modules.batch.service.IMaterialUploadTaskService;
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.stereotype.Component;

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

@Slf4j
@Component
public class SyncMaterialUploadJob {
    @Autowired
    private IMaterialUploadTaskService taskService;
    @Autowired
    private IMaterialInfoService materialInfoService;
    private static String uploadUrl = "http://upload.bjhcstszkj.cn/material/materialUploadTask/uploadMaterial";
    static ExecutorService kuaishouShouService = Executors.newFixedThreadPool(10);
    static ExecutorService bytedanceervice = Executors.newFixedThreadPool(10);


    /**
     * 素材同步
     */
    @XxlJob("syncMaterialUpload")
    public void syncMaterialUpload() {
        List<String> md5List = taskService.getMd5List();
        if (Check.isNull(md5List)) {
            log.error("没有待上传的素材");
            return;
        }
        for (int i = 0; i < md5List.size(); i++) {
            String signature = md5List.get(i);
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put("signature", signature);
            try {
                taskService.updateBySignature(signature);
                String s = HttpUtils2.httpGet(uploadUrl, requestMap, null);
                if (!Check.isNull(s)) {
                    JSONObject result = JSONObject.parseObject(s);
                    Integer code = result.getInteger("code");
                    if (code == 0) {
                        log.info("素材同步任务提交成功");
                    }
                }
            } catch (Exception e) {
                log.error("请求同步素材服务失败，signature：{}", signature);
                e.printStackTrace();
            }
        }
    }

    /**
     * 更新快手同步状态
     */
    @XxlJob("updateKuaiShouMaterialStatus")
    public void updateKuaiShouMaterialStatus() {
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -30);
        anotherDay += " 00:00:01";
        log.info("查询快手未同步素材列表，开始时间：{}", anotherDay);
        List<String> codeList = materialInfoService.getCodeList(anotherDay);
        log.info("查询条数：{}", codeList.size());
        if (Check.isNull(codeList)) {
            return;
        }
        for (int i = 0; i < codeList.size(); i++) {
            String code = codeList.get(i);
            if (Check.isNull(code)) {
                continue;
            }
            kuaishouShouService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        Integer num = materialInfoService.getCountByCode(code);
                        if (num > 0) {
                            materialInfoService.updateByCode(code);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    }


    /**
     * 更新头条同步状态
     */
    @XxlJob(value = "updateBytedanceMaterialStatus")
    public void updateBytedanceMaterialStatus() {
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -30);
        anotherDay += " 00:00:01";
        log.info("查询头条未同步素材列表，开始时间：{}", anotherDay);
        List<String> codeList = materialInfoService.getBytedanceCodeList(anotherDay);
        log.info("查询条数：{}", codeList.size());
        if (Check.isNull(codeList)) {
            return;
        }
        for (int i = 0; i < codeList.size(); i++) {
            String code = codeList.get(i);
            if (Check.isNull(code)) {
                continue;
            }
            bytedanceervice.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        Integer num = materialInfoService.getBytedanceCountByCode(code);
                        if (num > 0) {
                            materialInfoService.updateByteDanceByCode(code);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    }

}
