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.service.IKuaishouSupplyChainService;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.jeecg.common.util.DateUtils;
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;

@Component
public class settlementJob {


    @Autowired
    private IKuaishouSupplyChainService ikuaishouSupplyChainService;


    public static void main(String[] args) {
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String startDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -31);
        System.err.println(startDate);
    }

    @XxlJob("refreshCourierNumber")
    public void refreshCourierNumber() throws Exception {
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String startDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -31);
        List<JSONObject> list = ikuaishouSupplyChainService.getSamplesData(startDate);
        if (Check.isNull(list)) {
            return;
        }
        for (JSONObject jsonObject : list) {
            Long id = jsonObject.getLong("id");
            String courierNumber = jsonObject.getString("courierNumber");
            if (Check.isNull(courierNumber)) {
                continue;
            }
            String companyCode = jsonObject.getString("companyCode");
            if (Check.isNull(companyCode)) {
                continue;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("id", id);
            map.put("courierNumber", courierNumber);
            map.put("companyCode", companyCode);
            HttpUtils.httpGet("http://ruixuan.api.bjhcstszkj.cn/itemCollectSamples/refreshCourierNumber", map, null);
            Thread.sleep(1000L);
        }


    }


    /**
     * 自定义创建监测链接预警
     */
    @XxlJob("cleanSettlementData")
    public void cleanSettlementData() {
        String url = "http://ruixuan.api.bjhcstszkj.cn/settlement/data";
        String date = DateUtils.getLastDay(DateUtils.date2Str(), 1);
        Long statDate = Long.valueOf(date.replace("-", ""));
        List<JSONObject> list = ikuaishouSupplyChainService.getDataList(statDate);
        if (Check.isNull(list)) {
            return;
        }
        List<List<JSONObject>> partition = Lists.partition(list, 1000);
        for (int i = 0; i < partition.size(); i++) {
            List<JSONObject> jsonObjects = partition.get(i);
            Map<String, Object> param = new HashMap<>();
            param.put("statDate", statDate);
            param.put("data", jsonObjects);
            HttpUtils.httpPostRequest(url, param, null);
        }

    }


    @XxlJob("mgsCleanSettlementData")
    public void mgsCleanSettlementData() {
        String url = "http://mgs.api.bjhcstszkj.cn/settlement/data";
        String date = DateUtils.getLastDay(DateUtils.date2Str(), 1);
        Long statDate = Long.valueOf(date.replace("-", ""));
        List<JSONObject> list = ikuaishouSupplyChainService.getMgsDataList(statDate);
        if (Check.isNull(list)) {
            return;
        }
        List<List<JSONObject>> partition = Lists.partition(list, 1000);
        for (int i = 0; i < partition.size(); i++) {
            List<JSONObject> jsonObjects = partition.get(i);
            Map<String, Object> param = new HashMap<>();
            param.put("statDate", statDate);
            param.put("data", jsonObjects);
            HttpUtils.httpPostRequest(url, param, null);
        }

    }


    @XxlJob("rocketCleanSettlementData")
    public void rocketCleanSettlementData() {
        String url = "http://rocket.api.bjhcstszkj.cn/settlement/data";
        String date = DateUtils.getLastDay(DateUtils.date2Str(), 1);
        Long statDate = Long.valueOf(date.replace("-", ""));
        List<JSONObject> list = ikuaishouSupplyChainService.getRocketDataList(statDate);
        if (Check.isNull(list)) {
            return;
        }
        List<List<JSONObject>> partition = Lists.partition(list, 1000);
        for (int i = 0; i < partition.size(); i++) {
            List<JSONObject> jsonObjects = partition.get(i);
            Map<String, Object> param = new HashMap<>();
            param.put("statDate", statDate);
            param.put("data", jsonObjects);
            HttpUtils.httpPostRequest(url, param, null);
        }

    }

    @XxlJob("yufuCleanSettlementData")
    public void yufuCleanSettlementData() {
        String url = "http://yufu.api.bjhcstszkj.cn/settlement/data";
        String date = DateUtils.getLastDay(DateUtils.date2Str(), 1);
        Long statDate = Long.valueOf(date.replace("-", ""));
        List<JSONObject> list = ikuaishouSupplyChainService.getYufuDataList(statDate);
        if (Check.isNull(list)) {
            return;
        }
        List<List<JSONObject>> partition = Lists.partition(list, 1000);
        for (int i = 0; i < partition.size(); i++) {
            List<JSONObject> jsonObjects = partition.get(i);
            Map<String, Object> param = new HashMap<>();
            param.put("statDate", statDate);
            param.put("data", jsonObjects);
            HttpUtils.httpPostRequest(url, param, null);
        }

    }



    /*@XxlJob("cleanPromoterSettlementData")
    public void cleanPromoterSettlementData() {
        String url = "http://ruixuan.api.bjhcstszkj.cn/settlement/promoterData";
        String date = DateUtils.getLastDay(DateUtils.date2Str(), 1);
        Long statDate = Long.valueOf(date.replace("-", ""));
        List<JSONObject> list = ikuaishouSupplyChainService.getPromoterDataList(statDate);
        if (Check.isNull(list)) {
            return;
        }
        List<List<JSONObject>> partition = Lists.partition(list, 1000);
        for (int i = 0; i < partition.size(); i++) {
            List<JSONObject> jsonObjects = partition.get(i);
            Map<String, Object> param = new HashMap<>();
            param.put("statDate", statDate);
            param.put("data", jsonObjects);
            HttpUtils.httpPostRequest(url, param, null);
        }

    }*/


    @XxlJob("cleanPromoterBindChannel")
    public void cleanPromoterBindChannel() {
        Long date = Long.valueOf(DateUtils.getNowDate("yyyyMMdd"));
        ikuaishouSupplyChainService.deletePromoterBindChannelByDate(date);
        ikuaishouSupplyChainService.cleanKuaiShouPromoterBindChannel(date);
        ikuaishouSupplyChainService.cleanBytedancePromoterBindChannel(date);


        ikuaishouSupplyChainService.deleteMgsPromoterBindChannelByDate(date);
        ikuaishouSupplyChainService.cleanMgsKuaiShouPromoterBindChannel(date);


        ikuaishouSupplyChainService.deleteRocketPromoterBindChannelByDate(date);
        ikuaishouSupplyChainService.cleanRocketKuaiShouPromoterBindChannel(date);


        ikuaishouSupplyChainService.deleteYufuPromoterBindChannelByDate(date);
        ikuaishouSupplyChainService.cleanYufuKuaiShouPromoterBindChannel(date);

        ikuaishouSupplyChainService.deleteYuXiuPromoterBindChannelByDate(date);
        ikuaishouSupplyChainService.cleanYuXiuKuaiShouPromoterBindChannel(date);


        ikuaishouSupplyChainService.deleteZhiShuiPromoterBindChannelByDate(date);
        ikuaishouSupplyChainService.cleanZhiShuiKuaiShouPromoterBindChannel(date);


        // 招商

        ikuaishouSupplyChainService.deleteItemBindCourtshipByDate(date);
        ikuaishouSupplyChainService.cleanKuaiShouItemBindCourtship(date);
        ikuaishouSupplyChainService.cleanBytedanceItemBindCourtship(date);


        ikuaishouSupplyChainService.deleteMgsItemBindCourtshipByDate(date);
        ikuaishouSupplyChainService.cleanMgsItemBindCourtship(date);


        ikuaishouSupplyChainService.deleteRocketItemBindCourtshipByDate(date);
        ikuaishouSupplyChainService.cleanRocketItemBindCourtship(date);


        ikuaishouSupplyChainService.deleteYufuItemBindCourtshipByDate(date);
        ikuaishouSupplyChainService.cleanYufuItemBindCourtship(date);

        ikuaishouSupplyChainService.deleteYuXiuItemBindCourtshipByDate(date);
        ikuaishouSupplyChainService.cleanYuXiuItemBindCourtship(date);


        ikuaishouSupplyChainService.deleteZhiShuiItemBindCourtshipByDate(date);
        ikuaishouSupplyChainService.cleanZhiShuiItemBindCourtship(date);


    }


    @XxlJob("getCcrInfo")
    public void getCcrInfo() throws InterruptedException {
        List<JSONObject> cookies = ikuaishouSupplyChainService.getCookie();
        if (Check.isNull(cookies)) {
            return;
        }


        String date = DateUtils.getNowDate("yyyy-MM-dd");
        String yesDate = DateUtils.getAnotherDay("yyyyMMdd", date, -1);


        for (int i = 0; i < cookies.size(); i++) {
            JSONObject jsonObject = cookies.get(i);
            String cookie = jsonObject.getString("cookie");
            List<String> activityKeyWords = ikuaishouSupplyChainService.getActivityKeyWord(yesDate, "ruixuan");
            if (!Check.isNull(activityKeyWords)) {
                for (int j = 0; j < activityKeyWords.size(); j++) {
                    String activityKeyWord = activityKeyWords.get(i);
                    ikuaishouSupplyChainService.getCcrInfo(cookie, 0, "ruixuan", activityKeyWord);
                }
            }
        }

        Thread.sleep(5 * 1000);

        List<JSONObject> mgsCookies = ikuaishouSupplyChainService.getMgsCookie();
        for (int i = 0; i < mgsCookies.size(); i++) {
            JSONObject jsonObject = mgsCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            List<String> activityKeyWords = ikuaishouSupplyChainService.getActivityKeyWord(yesDate, "miaogousi");
            if (!Check.isNull(activityKeyWords)) {
                for (int j = 0; j < activityKeyWords.size(); j++) {
                    String activityKeyWord = activityKeyWords.get(i);
                    ikuaishouSupplyChainService.getCcrInfo(cookie, 0, "miaogousi", activityKeyWord);
                }
            }
        }
        Thread.sleep(5 * 1000);
        List<JSONObject> rocketCookies = ikuaishouSupplyChainService.getRocketCookie();
        for (int i = 0; i < rocketCookies.size(); i++) {
            JSONObject jsonObject = rocketCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            List<String> activityKeyWords = ikuaishouSupplyChainService.getActivityKeyWord(yesDate, "rocket");
            if (!Check.isNull(activityKeyWords)) {
                for (int j = 0; j < activityKeyWords.size(); j++) {
                    String activityKeyWord = activityKeyWords.get(i);
                    ikuaishouSupplyChainService.getCcrInfo(cookie, 0, "rocket", activityKeyWord);
                }
            }
        }
        Thread.sleep(5 * 1000);
        List<JSONObject> yufuCookies = ikuaishouSupplyChainService.getYuFuCiikie();
        for (int i = 0; i < yufuCookies.size(); i++) {
            JSONObject jsonObject = yufuCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            List<String> activityKeyWords = ikuaishouSupplyChainService.getActivityKeyWord(yesDate, "yufu");
            if (!Check.isNull(activityKeyWords)) {
                for (int j = 0; j < activityKeyWords.size(); j++) {
                    String activityKeyWord = activityKeyWords.get(i);
                    ikuaishouSupplyChainService.getCcrInfo(cookie, 0, "yufu", activityKeyWord);
                }
            }
        }
    }


    @XxlJob("getSellerInfoNowDate")
    public void getSellerInfoNowDate() throws InterruptedException {
        List<JSONObject> cookies = ikuaishouSupplyChainService.getCookie();
        if (Check.isNull(cookies)) {
            return;
        }

        String date = DateUtils.getNowDate("yyyy-MM-dd");
        for (int i = 0; i < cookies.size(); i++) {
            JSONObject jsonObject = cookies.get(i);
            String cookie = jsonObject.getString("cookie");
            ikuaishouSupplyChainService.getSellerInfo(cookie, 0, "ruixuan", date);
        }

        Thread.sleep(5 * 1000);
        List<JSONObject> mgsCookies = ikuaishouSupplyChainService.getMgsCookie();
        for (int i = 0; i < mgsCookies.size(); i++) {
            JSONObject jsonObject = mgsCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            ikuaishouSupplyChainService.getSellerInfo(cookie, 0, "miaogousi", date);
        }

        Thread.sleep(5 * 1000);
        List<JSONObject> rocketCookies = ikuaishouSupplyChainService.getRocketCookie();
        for (int i = 0; i < rocketCookies.size(); i++) {
            JSONObject jsonObject = rocketCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            ikuaishouSupplyChainService.getSellerInfo(cookie, 0, "rocket", date);
        }

        Thread.sleep(5 * 1000);
        List<JSONObject> yufuCookies = ikuaishouSupplyChainService.getYuFuCiikie();
        for (int i = 0; i < yufuCookies.size(); i++) {
            JSONObject jsonObject = yufuCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            ikuaishouSupplyChainService.getSellerInfo(cookie, 0, "yufu", date);
        }



        Thread.sleep(5 * 1000);
        List<JSONObject> yuxiuCookies = ikuaishouSupplyChainService.getyuxiuCiikie();
        for (int i = 0; i < yuxiuCookies.size(); i++) {
            JSONObject jsonObject = yuxiuCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            ikuaishouSupplyChainService.getSellerInfo(cookie, 0, "yuxiu", date);
        }


        Thread.sleep(5 * 1000);
        List<JSONObject> zhishuiCookies = ikuaishouSupplyChainService.getZhiShuiCiikie();
        for (int i = 0; i < zhishuiCookies.size(); i++) {
            JSONObject jsonObject = zhishuiCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            ikuaishouSupplyChainService.getSellerInfo(cookie, 0, "zhishui", date);
        }



    }


    @XxlJob("getSellerInfoYesDate")
    public void getSellerInfoYesDate() throws InterruptedException {
        List<JSONObject> cookies = ikuaishouSupplyChainService.getCookie();
        if (Check.isNull(cookies)) {
            return;
        }

        String date = DateUtils.getNowDate("yyyy-MM-dd");
        String yesDate = DateUtils.getAnotherDay("yyyy-MM-dd", date, -1);
        for (int i = 0; i < cookies.size(); i++) {
            JSONObject jsonObject = cookies.get(i);
            String cookie = jsonObject.getString("cookie");
            ikuaishouSupplyChainService.getSellerInfo(cookie, 0, "ruixuan", yesDate);
        }
        Thread.sleep(5 * 1000);
        List<JSONObject> mgsCookies = ikuaishouSupplyChainService.getMgsCookie();
        for (int i = 0; i < mgsCookies.size(); i++) {
            JSONObject jsonObject = mgsCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            ikuaishouSupplyChainService.getSellerInfo(cookie, 0, "miaogousi", yesDate);
        }
        Thread.sleep(5 * 1000);
        List<JSONObject> rocketCookies = ikuaishouSupplyChainService.getRocketCookie();
        for (int i = 0; i < rocketCookies.size(); i++) {
            JSONObject jsonObject = rocketCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            ikuaishouSupplyChainService.getSellerInfo(cookie, 0, "rocket", yesDate);
        }
        Thread.sleep(5 * 1000);
        List<JSONObject> yufuCookies = ikuaishouSupplyChainService.getYuFuCiikie();
        for (int i = 0; i < yufuCookies.size(); i++) {
            JSONObject jsonObject = yufuCookies.get(i);
            String cookie = jsonObject.getString("cookie");
            ikuaishouSupplyChainService.getSellerInfo(cookie, 0, "yufu", yesDate);
        }
    }


}

