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

import cn.com.ctop.common.module.utils.Check;
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.RocketSupplyChainService;
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.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Component
public class RocketOrderLIstGetJob {

    @Autowired
    private RocketSupplyChainService rocketSupplyChainService;

    @Autowired
    private IKuaiShouActivityItemListService activityItemListService;

    static ExecutorService fxService = Executors.newFixedThreadPool(3);

    /**
     * 昨日快分销数据获取
     **/
    @XxlJob("rocketYesterdayOrderGet")
    public void rocketYesterdayOrderGet() throws Exception {
        List<JSONObject> cookies = rocketSupplyChainService.getCookie();
        if (Check.isNull(cookies)) {
            log.error("cookie数据为空");
            return;
        }
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String date = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
        JSONArray hourList = KuaishouHourEnum.getHourList();
        JSONArray minSecList = KuaishouMinSecEnum.getMinSecList();
        for (int i = 0; i < hourList.size(); i++) {
            String hour = hourList.getString(i);
            for (int j = 0; j < minSecList.size(); j++) {
                JSONObject minSecJson = minSecList.getJSONObject(j);
                String startTime = minSecJson.getString("startTime");
                String endTime = minSecJson.getString("endTime");
                String start = date + hour + startTime;
                String end = date + hour + endTime;
                System.err.println("开始时间：" + start);
                System.err.println("结束时间：" + end);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Long orderCreateTimeStart = sdf.parse(start).getTime();
                Long orderCreateTimeEnd = sdf.parse(end).getTime();
                for (int p = 0; p < cookies.size(); p++) {
                    JSONObject cookieJson = cookies.get(p);
                    String cookie = cookieJson.getString("cookie");
                    Long regimentalId = cookieJson.getLong("id");
                    rocketSupplyChainService.getOrderList(cookie, 0, orderCreateTimeStart, orderCreateTimeEnd, regimentalId);
                }
            }
        }
    }


    /**
     * 当前小时数据获取
     **/
    @XxlJob("rocketNowHourOrderGet")
    public void rocketNowHourOrderGet() throws Exception {
        List<JSONObject> cookies = rocketSupplyChainService.getCookie();
        if (Check.isNull(cookies)) {
            log.error("cookie数据为空");
            return;
        }
        String date = DateUtils.getNowDate("yyyy-MM-dd");
        Integer nowHour = DateUtils.getNowHour();
        JSONArray hours = new JSONArray();
        hours.add(nowHour);
        if (nowHour >= 1) {
            hours.add(nowHour - 1);
        }


        JSONArray minSecList = KuaishouMinSecEnum.getMinSecList();
        for (int i = 0; i < hours.size(); i++) {
            Integer hour = hours.getInteger(i);
            for (int j = 0; j < minSecList.size(); j++) {
                JSONObject minSecJson = minSecList.getJSONObject(j);
                String startTime = minSecJson.getString("startTime");
                String endTime = minSecJson.getString("endTime");
                String start = date + " " + hour + ":" + startTime;
                String end = date + " " + hour + ":" + endTime;
                System.err.println("开始时间：" + start);
                System.err.println("结束时间：" + end);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Long orderCreateTimeStart = sdf.parse(start).getTime();
                Long orderCreateTimeEnd = sdf.parse(end).getTime();
                for (int p = 0; p < cookies.size(); p++) {
                    JSONObject cookieJson = cookies.get(p);
                    String cookie = cookieJson.getString("cookie");
                    Long regimentalId = cookieJson.getLong("id");
                    rocketSupplyChainService.getOrderList(cookie, 0, orderCreateTimeStart, orderCreateTimeEnd, regimentalId);
                }
            }
        }
    }

    /**
     * 当天所有数据获取
     **/
    /**
     * 当天数据获取
     **/
    @XxlJob("rocketFrontHourOrderGet")
    public void rocketFrontHourOrderGet() throws Exception {
        List<JSONObject> cookies = rocketSupplyChainService.getCookie();
        if (Check.isNull(cookies)) {
            log.error("cookie数据为空");
            return;
        }
        String date = DateUtils.getNowDate("yyyy-MM-dd");
        JSONArray hourList = KuaishouHourEnum.getHourList();
        JSONArray minSecList = KuaishouMinSecEnum.getMinSecList();
        Integer nowHour = DateUtils.getNowHour();
        for (int i = 0; i < hourList.size(); i++) {
            String hour = hourList.getString(i);
            Integer integer = Integer.valueOf(hour.replace(":", "").replace(" ", ""));
            if (integer <= nowHour) {
                for (int j = 0; j < minSecList.size(); j++) {
                    JSONObject minSecJson = minSecList.getJSONObject(j);
                    String startTime = minSecJson.getString("startTime");
                    String endTime = minSecJson.getString("endTime");
                    String start = date + hour + startTime;
                    String end = date + hour + endTime;
                    System.err.println("开始时间：" + start);
                    System.err.println("结束时间：" + end);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Long orderCreateTimeStart = sdf.parse(start).getTime();
                    Long orderCreateTimeEnd = sdf.parse(end).getTime();
                    for (int p = 0; p < cookies.size(); p++) {
                        JSONObject cookieJson = cookies.get(p);
                        String cookie = cookieJson.getString("cookie");
                        Long regimentalId = cookieJson.getLong("id");
                        rocketSupplyChainService.getOrderList(cookie, 0, orderCreateTimeStart, orderCreateTimeEnd, regimentalId);
                    }
                }
            }
        }
    }


}
