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.IJiaoYangFenXiaoService;
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.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class JiaoYangFenXiaoJob {
    @Autowired
    private IJiaoYangFenXiaoService jiaoYangFenXiaoService;


    /**
     * 获取二创信息
     *
     * @throws Exception
     */
    @XxlJob("jyTopItem")
    public void jyTopItem() throws Exception {
        String cookie = jiaoYangFenXiaoService.getCookieById(341063986L);
        if (Check.isNull(cookie)) {
            log.error("cookie数据为空");
            return;
        }
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        Long statDate = Long.valueOf(nowDate.replace("-", ""));
        List<JSONObject> accountList = jiaoYangFenXiaoService.getAccountList();
        for (int i = 0; i < accountList.size(); i++) {
            JSONObject jsonObject = accountList.get(i);
            Long id = jsonObject.getLong("id");
            String name = jsonObject.getString("name");
            jiaoYangFenXiaoService.getItemTop(cookie, statDate, id, name, 0);
        }
    }


    /**
     * 获取二创信息
     *
     * @throws Exception
     */
    @XxlJob("jyClipCooperation")
    public void jyClipCooperation() throws Exception {

        List<JSONObject> cookies = jiaoYangFenXiaoService.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);
        Long statDate = Long.valueOf(date.replace("-", ""));
        List<Integer> statusList = new ArrayList();

        statusList.add(1);

        for (int p = 0; p < cookies.size(); p++) {
            JSONObject cookieJson = cookies.get(p);
            String cookie = cookieJson.getString("cookie");
            for (int i = 0; i < statusList.size(); i++) {
                Integer status = statusList.get(i);
                jiaoYangFenXiaoService.getClipCooperation(cookie, 0, statDate, status);
            }

        }


    }


    /**
     * 获取二创信息
     *
     * @throws Exception
     */
    @XxlJob("jyClipCooperationV2")
    public void jyClipCooperationV2() throws Exception {

        List<JSONObject> cookies = jiaoYangFenXiaoService.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);
        Long statDate = Long.valueOf(date.replace("-", ""));
        List<Integer> statusList = new ArrayList();

        statusList.add(1);

        for (int p = 0; p < cookies.size(); p++) {
            JSONObject cookieJson = cookies.get(p);
            String cookie = cookieJson.getString("cookie");
            for (int i = 0; i < statusList.size(); i++) {
                Integer status = statusList.get(i);
                jiaoYangFenXiaoService.getClipCooperationV2(cookie, 0, statDate, status);
            }

        }


    }


    @XxlJob("jyLastHourOrderGet")
    public void jyLastHourOrderGet() throws Exception {
        log.info("-------------------------获取快分销订单当天当小时数据开始--------------------------");
        List<JSONObject> cookies = jiaoYangFenXiaoService.getCookie();
        if (Check.isNull(cookies)) {
            log.error("cookie数据为空");
            return;
        }
        String date = DateUtils.getNowDate("yyyy-MM-dd");
        Integer nowHour = DateUtils.getNowHour();
        JSONArray hours = new JSONArray();

        if (nowHour >= 1) {
            hours.add(nowHour - 1);
        } else {
            return;
        }
        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");
                    jiaoYangFenXiaoService.getOrderList(cookie, 1, 0, orderCreateTimeStart, orderCreateTimeEnd);
                }
            }
        }
    }


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


    /**
     * 当天前前小时数据获取
     **/
    /**
     * 当天数据获取
     **/
    @XxlJob("jyFrontHourOrderGet")
    public void frontHourOrderGet() throws Exception {

        List<JSONObject> cookies = jiaoYangFenXiaoService.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");
                        jiaoYangFenXiaoService.getOrderList(cookie, 1, 0, orderCreateTimeStart, orderCreateTimeEnd);
                    }
                }
            }
        }


    }


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


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

            }
        }
    }


    /**
     * 快分销-当前月所有数据
     **/
    @XxlJob("jyNowMonthyOrderGet")
    public void jyNowMonthyOrderGet() throws Exception {
        List<JSONObject> cookies = jiaoYangFenXiaoService.getCookie();
        if (Check.isNull(cookies)) {
            log.error("cookie数据为空");
            return;
        }
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -3);
        String monthFirstDay = DateUtils.getMonthFirstDay();
        List<String> days = DateUtils.getDays(monthFirstDay, endDate);
        JSONArray hourList = KuaishouHourEnum.getHourList();
        JSONArray minSecList = KuaishouMinSecEnum.getMinSecList();
        for (int z = 0; z < days.size(); z++) {
            String date = days.get(z);
            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");
                        jiaoYangFenXiaoService.getOrderList(cookie, 1, 0, orderCreateTimeStart, orderCreateTimeEnd);
                    }
                }
            }
        }
    }


    /**
     * 上个月数据获取
     *
     * @throws Exception
     */
    @XxlJob("jyAllOrderGet")
    public void jyAllOrderGet() throws Exception {
        //   List<String> dateList = new ArrayList<>();
        Map<String, String> monthStartAndEnd = DateUtils.getMonthStartAndEnd();
        String firstDay = monthStartAndEnd.get("firstDay");
        String lastDay = monthStartAndEnd.get("lastDay");
        List<String> days = DateUtils.getDays(firstDay, lastDay);
        List<JSONObject> cookies = jiaoYangFenXiaoService.getCookie();
        if (Check.isNull(cookies)) {
            log.error("cookie数据为空");
            return;
        }


        JSONArray hourList = KuaishouHourEnum.getHourList();
        JSONArray minSecList = KuaishouMinSecEnum.getMinSecList();
        for (int z = 0; z < days.size(); z++) {
            String date = days.get(z);
            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");
                        jiaoYangFenXiaoService.getOrderList(cookie, 1, 0, orderCreateTimeStart, orderCreateTimeEnd);
                    }
                }
            }

        }
    }


    // 前7 14 21 30 天数据获取
    @XxlJob("jyAppointYesterdayOrderGet")
    public void jyAppointYesterdayOrderGet() throws Exception {
        log.info("-------------------------获取快分销订单前日数据开始数据开始--------------------------");
        List<JSONObject> cookies = jiaoYangFenXiaoService.getCookie();
        if (Check.isNull(cookies)) {
            log.error("cookie数据为空");
            return;
        }
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String date7 = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -7);
        String date14 = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -14);
        String date21 = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -21);
        String date30 = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -30);
        List<String> days = new ArrayList<>();
        days.add(date7);
        days.add(date14);
        days.add(date21);
        days.add(date30);
        JSONArray hourList = KuaishouHourEnum.getHourList();
        JSONArray minSecList = KuaishouMinSecEnum.getMinSecList();
        for (int z = 0; z < days.size(); z++) {
            String date = days.get(z);
            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");
                        jiaoYangFenXiaoService.getOrderList(cookie, 1, 0, orderCreateTimeStart, orderCreateTimeEnd);
                    }
                }
            }

        }
    }


}
