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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.kuaishou.modules.batch.Enums.KuaishouHourEnum;
import cn.com.ctop.kuaishou.modules.batch.Enums.KuaishouMinSecEnum;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouActivityItemListService;
import cn.com.ctop.kuaishou.modules.batch.service.IKwaixiaodianInfoService;
import cn.com.ctop.kuaishou.modules.batch.service.YufuSupplyChainService;
import com.alibaba.fastjson.JSONArray;
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.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Component
public class KwaixiaodianInfoJob {

    @Autowired
    private IKwaixiaodianInfoService kwaixiaodianInfoService;

    private static String historyDateDetailUrl = "http://ruixuan.api.bjhcstszkj.cn/kwaixiaodian/jst/costDetail";
    private static String cleanDataUrl = "http://ruixuan.api.bjhcstszkj.cn/kwaixiaodian/jst/cleanCost";


    // 获取近两个月数据
    @XxlJob("historyDateDetail")
    public void historyDateDetail() throws Exception {
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        Long startDate = Long.valueOf(DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -60).replace("-", ""));
        Long endDate = Long.valueOf(nowDate.replace("-", ""));
        kwaixiaodianInfoService.deleteByDate();

        List<JSONObject> dataList = kwaixiaodianInfoService.getSkuList(startDate, endDate);
        if (!Check.isNull(dataList)) {
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject jsonObject = dataList.get(i);
                String skuId = jsonObject.getString("skuId");
                Integer dataType = jsonObject.getInteger("dataType");
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("skuId", skuId);
                requestMap.put("dataType", dataType);
                requestMap.put("date", endDate);
                HttpUtils.httpPostRequest(historyDateDetailUrl, requestMap, null);

            }
        }

    }

    @XxlJob("cleanHistoryData")
    public void cleanHistoryData() throws Exception {
        Long nowDate = Long.valueOf(DateUtils.getNowDate("yyyy-MM-dd").replace("-", ""));
        List<JSONObject> dataList = kwaixiaodianInfoService.getCleanDate(nowDate);
        if (!Check.isNull(dataList)) {
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject jsonObject = dataList.get(i);
                String skuId = jsonObject.getString("skuId");
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("skuId", skuId);
                HttpUtils.httpPostRequest(cleanDataUrl, requestMap, null);
            }
        }

    }


    // 获取当天数据
    @XxlJob("nowDateDetail")
    public void nowDateDetail() throws Exception {
        Long nowDate = Long.valueOf(DateUtils.getNowDate("yyyy-MM-dd").replace("-", ""));
        kwaixiaodianInfoService.deleteByDate();
        List<JSONObject> dataList = kwaixiaodianInfoService.getSkuList(nowDate, nowDate);
        if (!Check.isNull(dataList)) {
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject jsonObject = dataList.get(i);
                String skuId = jsonObject.getString("skuId");
                Integer dataType = jsonObject.getInteger("dataType");
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("skuId", skuId);
                requestMap.put("dataType", dataType);
                requestMap.put("date", nowDate);
                HttpUtils.httpPostRequest(historyDateDetailUrl, requestMap, null);
            }
        }

    }

    @XxlJob("cleanNowData")
    public void cleanNowData() throws Exception {
        Long nowDate = Long.valueOf(DateUtils.getNowDate("yyyy-MM-dd").replace("-", ""));
        List<JSONObject> dataList = kwaixiaodianInfoService.getCleanDate(nowDate);
        if (!Check.isNull(dataList)) {
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject jsonObject = dataList.get(i);
                String skuId = jsonObject.getString("skuId");
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("skuId", skuId);
                HttpUtils.httpPostRequest(cleanDataUrl, requestMap, null);
            }
        }

    }


}
