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.IKuaiShouActivityInfoService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouActivityItemListService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouSupplyChainService;
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.ParseException;
import java.text.SimpleDateFormat;

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

@Slf4j
@Component
public class KuaishouOrderLIstGetJob {

    @Autowired
    private IKuaishouSupplyChainService ikuaishouSupplyChainService;
    @Autowired
    private IKuaiShouActivityInfoService activityInfoService;
    @Autowired
    private IKuaiShouActivityItemListService activityItemListService;
    static ExecutorService fxService = Executors.newFixedThreadPool(3);
    static ExecutorService fxNowService = Executors.newFixedThreadPool(3);


    /**
     * 快分销-招商活动商品数据同步
     **/
    @XxlJob("getActivityItemList")
    public void getActivityItemList() throws Exception {
        log.info("-------------------------快分销招商活动商品数据开始--------------------------");
        List<JSONObject> cookies = ikuaishouSupplyChainService.getCookie();
        if (Check.isNull(cookies)) {
            log.error("cookie数据为空");
            return;
        }
        Map<Long, Integer> itemMap = activityInfoService.getItemMap();
        for (int j = 0; j < cookies.size(); j++) {
            JSONObject cookieJson = cookies.get(j);
            Long id = cookieJson.getLong("id");
            String cookie = cookieJson.getString("cookie");
            List<JSONObject> activityIdList = activityInfoService.getActivityIdList(id);
            if (!Check.isNull(activityIdList)) {
                for (int i = 0; i < activityIdList.size(); i++) {
                    JSONObject jsonObject = activityIdList.get(i);
                    Long activityId = jsonObject.getLong("activityId");
                    Integer activityStatus = jsonObject.getInteger("activityStatus");
                    activityItemListService.getActivityItemList(cookie, 0, activityId, activityStatus, itemMap);
                    Thread.sleep(1000);
                }
            }
        }
    }


    /**
     * 快分销-招商活动数据同步
     **/
    @XxlJob("getActivityInfo")
    public void getActivityInfo() throws Exception {
        log.info("-------------------------快分销招商活动数据开始--------------------------");
        List<JSONObject> cookies = ikuaishouSupplyChainService.getCookie();
        if (Check.isNull(cookies)) {
            log.error("cookie数据为空");
            return;
        }
        for (int i = 0; i < cookies.size(); i++) {
            JSONObject cookieJson = cookies.get(i);
            Long id = cookieJson.getLong("id");
            String cookie = cookieJson.getString("cookie");
            activityInfoService.getActivityInfo(id, cookie, 0);
        }
    }


    public static void main(String[] args) {
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String start = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -93);
        String end = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -2);
        List<String> days = DateUtils.getDays(start, end);
        System.err.println(days);
    }






    /**
     * 昨日快分销数据获取
     **/
    @XxlJob("yesterdayOrderGet")
    public void yesterdayOrderGet() throws Exception {
        log.info("-------------------------获取快分销订单昨日数据开始数据开始--------------------------");
        List<JSONObject> cookies = ikuaishouSupplyChainService.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");
                    ikuaishouSupplyChainService.getOrderList(cookie, 0, orderCreateTimeStart, orderCreateTimeEnd,regimentalId);
                }
            }
        }
    }


    /**
     * 昨日快分销数据获取
     **/
    @XxlJob("beforeYesterdayOrderGet")
    public void beforeYesterdayOrderGet() throws Exception {
        log.info("-------------------------获取快分销订单前日数据开始数据开始--------------------------");
        List<JSONObject> cookies = ikuaishouSupplyChainService.getCookie();
        if (Check.isNull(cookies)) {
            log.error("cookie数据为空");
            return;
        }
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String date = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -2);
        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");
                    ikuaishouSupplyChainService.getOrderList(cookie, 0, orderCreateTimeStart, orderCreateTimeEnd,regimentalId);
                }

            }
        }
    }


    /**
     * 当前小时数据获取
     **/
    @XxlJob("nowHourOrderGet")
    public void nowHourOrderGet() throws Exception {
        log.info("-------------------------获取快分销订单当天当小时数据开始--------------------------");
        List<JSONObject> cookies = ikuaishouSupplyChainService.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");
                    ikuaishouSupplyChainService.getOrderList(cookie, 0, orderCreateTimeStart, orderCreateTimeEnd,regimentalId);
                }
            }
        }
    }


    /**
     * 当天前前小时数据获取
     **/
    /**
     * 当天数据获取
     **/
    @XxlJob("frontHourOrderGet")
    public void frontHourOrderGet() throws Exception {
        log.info("-------------------------获取快分销订单当天数据开始--------------------------");
        List<JSONObject> cookies = ikuaishouSupplyChainService.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");
                        ikuaishouSupplyChainService.getOrderList(cookie, 0, orderCreateTimeStart, orderCreateTimeEnd, regimentalId);
                    }
                }
            }
        }


    }


}
