package org.jeecg;

import cn.com.ctop.alarm.modules.service.IAlarmEventSendService;
import cn.com.ctop.common.module.entity.BindAccountLogin;
import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IBindAccountLoginService;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.common.module.utils.CtopAdConstant;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouDailyReportTaskService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouHistoryReportTaskService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import cn.com.ctop.kuaishou.modules.graphql.service.IKuaishouWebInterfaceService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouCostGroupService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouReportDailyAgentService;
import cn.com.ctop.kuaishou.modules.report.service.IRuleKuaiShouPlanService;
import cn.com.ctop.oa.modules.service.IWechatCheckinDataService;
import cn.com.ctop.oa.modules.service.IWechatDepartmentService;
import cn.com.ctop.oa.modules.service.IWechatNoListService;
import cn.com.ctop.oa.modules.service.IWechatUserListService;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceAdvertiserDataService;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceFundDailyService;
import cn.com.ctop.toutiao.modules.report.service.IReportService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.jeecg.common.util.DateUtils.getAnotherDay;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("wps")
@Slf4j
public class SampleTest {
    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private IReportService reportService;
    @Autowired
    private IByteDanceAdvertiserDataService advertiserDataService;
    @Autowired
    private IKuaiShouHistoryReportTaskService kuaiShouHistoryReportTaskService;
    @Autowired
    private ICtopOauthTokenService tokenService;

    @Test
    public void loadBytedanceCreativeData() {
        QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("media_id", 2);
        List<CtopOauthToken> list = oauthTokenService.list(queryWrapper);
        for (CtopOauthToken token : list) {
            kuaiShouHistoryReportTaskService.createTask(token.getAccountId(), token.getAccessToken(), "2020-09-20", "2020-09-20", "daily");
        }
    }

    @Autowired
    private IBindAccountLoginService bindAccountLoginService;
    @Autowired
    private IKuaishouWebInterfaceService kuaishouWebInterfaceService;

    @Test
    public void loadKuaishouCookie() {
        List<BindAccountLogin> list = bindAccountLoginService.getListByParams(CtopAdConstant.PLATFORM_TYPE_KUAISHOU_PY, 1);
        if (list != null && !list.isEmpty()) {
            int i = 0;
            for (BindAccountLogin login : list) {
                if (null == login.getCookie() || "".equals(login.getCookie().trim())) {
                    kuaishouWebInterfaceService.getkuaishouWebLoginCookie(login);
                    i++;
                }
            }
        }
    }

    static ExecutorService executorService = null;
    static CountDownLatch countDownLatch = null;

    @Test
    public void deleteKuaishouComment() {
        Long start = System.currentTimeMillis();
        List<BindAccountLogin> loginList = bindAccountLoginService.getListByParams(CtopAdConstant.PLATFORM_TYPE_KUAISHOU_PY, 1);
        if (loginList != null && !loginList.isEmpty()) {
            executorService = Executors.newFixedThreadPool(5);
            countDownLatch = new CountDownLatch(loginList.size());
            loginList.forEach(login -> executorService.submit(() -> {
                try {
                    kuaishouWebInterfaceService.deleteAllComment(new HashMap<>(), login);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                } finally {
                    countDownLatch.countDown();
                }
            }));
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Long end = System.currentTimeMillis();
            log.info("快手删评论所用时长：{}毫秒", end - start);
        }
    }

    @Autowired
    private IUserAllocationService allocationService;

    @Test
    public void testLoadBytedanceData() {
        List<UserAllocation> allocations = allocationService.getByParams(633L, null, 0);
        if (null != allocations && !allocations.isEmpty()) {
            for (UserAllocation allocation : allocations) {
                CtopOauthToken token = tokenService.getTokenByAccountId(allocation.getAccountId());
                String startDate = "2020-11-10";
                String endDate = "2020-11-10";
                reportService.getAdvertiserReport(token, DateUtils.parseDate(startDate, "yyyy-MM-dd"), DateUtils.parseDate(endDate, "yyyy-MM-dd"), CtopAdConstant.BYTEDANCE_REPORT_TYPE_HOURLY);
            }
        }
        System.out.println("任务结束");
    }

    @Test
    public void loadKuaishouPlanData() {
        Date getDate = DateUtils.addDay(new Date(), -1);
        //1:查询当日数据
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        tokens.forEach(token -> kuaishouInterfaceService.getAdvertiserCampaignReportDaily(token, getDate, getDate));
    }

    @Autowired
    private IBytedanceFundDailyService bytedanceFundDailyService;

    @Test
    public void loadFoudData() {
        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        for (CtopOauthToken token : tokens) {
            System.out.println(token.getAccountId() + "############################");
            for (int i = 20; i < 130; i++) {
                Date date = DateUtils.addDay(new Date(), -i);
                bytedanceFundDailyService.loadFundDataByPage(token, DateUtils.formatDate(date), DateUtils.formatDate(date), 1);
                System.out.println(i + "############################");
            }
        }
    }

    @Autowired
    private IKuaiShouHistoryReportTaskService reportTaskService;

    /**
     * 测试获取快手图片消耗数据任务
     */
    @Test
    public void loadKuaishouImageTask() {
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        for (int i = 1; i < 130; i++) {
            Date getDate = DateUtils.addDay(new Date(), -i);
            String getDateStr = DateUtils.formatDate(getDate);
            tokens.forEach(token -> reportTaskService.createTask(token.getAccountId(), token.getAccessToken(), getDateStr, getDateStr, CtopAdConstant.KUAISHOU_LOAD_JOB_TYPE_DAILY));
        }
    }

    @Autowired
    private IKuaiShouDailyReportTaskService dailyReportTaskService;

    /**
     * 测试获取快手图片消耗数据入库
     */
    @Test
    public void loadKuaishouImageTaskFile() {
        String nowDate = DateUtils.getDate("yyyy-MM-dd");
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        for (int i = 1; i < 15; i++) {
            String statDate = getAnotherDay("yyyy-MM-dd", nowDate, -i);
            tokens.forEach(token -> dailyReportTaskService.getTaskList(token.getAccountId(), token.getAccessToken(), statDate));
        }
        //  查询快手token
    }

    @Test
    public void testLoadBytedanceImageData() {
        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        countDownLatch = new CountDownLatch(tokens.size());
        executorService = Executors.newFixedThreadPool(8);
        tokens.forEach(token -> executorService.submit(() -> {
            try {
                advertiserDataService.getMaterialList(token);
            } catch (Exception e) {
            } finally {
                countDownLatch.countDown();
            }
        }));
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("素材数据获取完成");

    }

    @Autowired
    private IKuaishouReportDailyAgentService kuaishouReportDailyAgentService;

    @Test
    public void testData() {
        Date date = new Date();
        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        executorService = Executors.newFixedThreadPool(10);
        countDownLatch = new CountDownLatch(tokens.size());
        tokens.forEach(token -> executorService.submit(() -> {
            try {
                reportService.getAdvertiserPlanRuleData(token, date, date, CtopAdConstant.BYTEDANCE_REPORT_TYPE_DAILY);
            } catch (Exception e) {

            } finally {
                countDownLatch.countDown();
            }
        }));
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void loadKuaishouAgentData() {
        kuaishouReportDailyAgentService.loginAgent();
        try {
            for (int i = 1; i < 30; i++) {
                kuaishouReportDailyAgentService.getAccount(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void loadKuaishouHourlyReportData(){
        Date getDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
        String hour = simpleDateFormat.format(getDate);
        if ("00".equals(hour)) {
            getDate = DateUtils.addDay(getDate, -1);
        }
        //1:查询当日数据
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        Date finalGetDate = getDate;
        tokens.forEach(token -> kuaishouInterfaceService.getAdvertiserReportHourly(token, finalGetDate, finalGetDate));
    }
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;

    @Test
    public void loadKuaishouGroupData() throws ParseException {
        Date endDate = new Date();
        String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", DateUtils.formatDate(endDate), -1);
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sim.parse(anotherDay);
        CtopOauthToken token = tokenService.getTokenByAccountId(7087890L);
        kuaishouInterfaceService.getGroupList(token, startDate, endDate);
    }


    @Test
    public void loadAccountData() {
//        List<UserAllocation> allocations = allocationService.getByParams(776L,null,0);
//        for (UserAllocation allocation:allocations) {
        CtopOauthToken token = tokenService.getTokenByAccountId(93238965516L);
        for (int i = 92; i < 100; i++) {
            Date getDate = DateUtils.addDay(new Date(), -i);
            reportService.getAdvertiserReport(token, getDate, getDate, CtopAdConstant.BYTEDANCE_REPORT_TYPE_DAILY);
        }
//        }
    }


    @Autowired
    private IKuaishouCostGroupService kuaishouCostGroupService;
    @Autowired
    private IAlarmEventSendService alarmEventSendService;

    @Test
    public void addTest() {
        kuaishouCostGroupService.saveInfo();
    }

    @Test
    public void sendTest() {
        //检查频率 HOUR - 每小时；HALF_HOUR-每半小时
        alarmEventSendService.matchAlarmRules("HOUR");
    }


    @Autowired
    private IWechatCheckinDataService wechatCheckinDataService;
    @Autowired
    private IWechatDepartmentService wechatDepartmentService;
    @Autowired
    private IWechatUserListService wechatUserInfoService;
    @Autowired
    private IWechatNoListService wechatNoListService;

    @Test
    public void getData() {
        //汇创 人员
//        wechatUserInfoService.getUserList();
//        //考勤记录
//        wechatCheckinDataService.getCheckinData("2020-11-01 00:00:00", "2020-12-01 23:59:59");
        //异常记录
//        wechatNoListService.getNoDateByNo("2020-11-01 00:00:00", "2020-12-01 23:59:59", "0");
        //优容
        wechatUserInfoService.getYRUserList();
        wechatCheckinDataService.getYRCheckinData("2020-11-01 00:00:00", "2020-11-30 23:59:59");
        wechatNoListService.getNoDateByNo("2020-11-01 00:00:00", "2020-12-01 23:59:59", "1");
    }

    @Autowired
    IRuleKuaiShouPlanService kuaiShouPlanService;
    @Test
    public void kuaishouRuleData(){
        //kuaiShouPlanService.cleanRuleDataPlan(3429620L,"2020-12-07","2020-12-07",1);
        kuaiShouPlanService.cleanRuleDataUnit(3429620L,"2020-12-07","2020-12-07",1);
        kuaiShouPlanService.cleanRuleDataTarget(3429620L,"2020-12-07","2020-12-07",1);

    }


}
