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.dimension.entity.KuaiShouDimensionStrategy;
import cn.com.ctop.kuaishou.modules.dimension.service.IKuaiShouDimensionCreativeService;
import cn.com.ctop.kuaishou.modules.dimension.service.IKuaiShouDimensionStrategyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;

/**
 * 账户维度-盯盘每小时数据
 *
 * @author syh
 */
@Slf4j
@Component
public class KuaishouDimensionCreateJob {
    @Autowired
    private IKuaiShouDimensionStrategyService strategyService;
    @Autowired
    private IKuaiShouDimensionCreativeService dimensionCreativeService;

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

    @XxlJob("dimensionCreateUnit")
    public void dimensionCreateUnit() throws Exception {
        QueryWrapper<KuaiShouDimensionStrategy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("strategy_status", 0);
        List<KuaiShouDimensionStrategy> list = strategyService.list(queryWrapper);
        if (Check.isNull(list)) {
            return;
        }
        String url = jobUrl + "/jeecg-boot/dimension/kuaiShouDimensionAccount/createUnit";
        for (KuaiShouDimensionStrategy strategy : list) {
            try {
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("strategyId", strategy.getId());
                HttpUtils2.httpGet(url, requestMap, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @XxlJob("againCreateCreative")
    public void againCreateCreative() throws Exception {
        List<Long> accountIds = dimensionCreativeService.getAccountList();
        if (Check.isNull(accountIds)) {
            log.info("暂无待提交创意账户");
            return;
        }
        String url = jobUrl + "/jeecg-boot/dimension/kuaiShouDimensionCreative/againCreateCreative";
        for (int i = 0; i < accountIds.size(); i++) {
            Long accountId = accountIds.get(i);
            try {
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("accountId", accountId);
                HttpUtils2.httpGet(url, requestMap, null);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}
