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.batch.service.IMaterialUploadTaskService;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
public class HostingCreateJob {
    @Value("${xxl-job.requestUrl}")
    private String jobUrl;
    @Autowired
    private IMaterialUploadTaskService taskService;

    /**
     * 智能托管
     */
    @XxlJob("createHosting")
    public void createHosting() {
        String url = jobUrl + "/jeecg-boot/task/kuaishouHostingTask/createHosting";
        List<Long> idList = taskService.getHostingIdList();
        if (Check.isNull(idList)) {
            log.error("没有待创建的智能托管定时任务");
            return;
        }
        for (int i = 0; i < idList.size(); i++) {
            Long tableId = idList.get(i);
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put("id", tableId);
            String s = HttpUtils2.httpGet(url, requestMap, null);
            JSONObject result = JSONObject.parseObject(s);
            if (!Check.isNull(result)) {
                if (result.getInteger("code") != 0) {
                    log.error("任务提交失败，id：{}", tableId);
                }
            } else {
                log.error("任务提交返回结果为空，id：{}", tableId);
            }
        }
    }
}
