package org.jeecg;

import cn.com.ctop.common.module.entity.BindAccountLogin;
import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.IBindAccountLoginService;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.CtopAdConstant;
import cn.com.ctop.crawler.modules.pangolin.entity.PangolinApp;
import cn.com.ctop.crawler.modules.pangolin.service.PangolinAppService;
import cn.com.ctop.crawler.modules.pangolin.service.PangolinCrawlerService;
import cn.com.ctop.crawler.modules.pangolin.service.PangolinLoginService;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceFundDailyService;
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.junit4.SpringRunner;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class SampleTest {
    @Autowired
    private IBindAccountLoginService bindAccountLoginService;
    @Autowired
    private PangolinLoginService pangolinLoginService;
    @Autowired
    private PangolinCrawlerService pangolinCrawlerService;
    @Autowired
    private PangolinAppService pangolinAppService;
    @Test
    public void testPangolinLogin(){
        QueryWrapper<BindAccountLogin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_type","pangolin");
        queryWrapper.eq("status",1);
        List<BindAccountLogin> list = bindAccountLoginService.list(queryWrapper);
        if(list!=null&&!list.isEmpty()){
            for (BindAccountLogin login:list) {
                if(null==login.getCookie()||"".equals(login.getCookie().trim())){
                    pangolinLoginService.pangolinLogin(login.getAccountName(), login.getPassword());
                }
            }
        }
    }

    @Test
    public void testPangolinData(){
        QueryWrapper<BindAccountLogin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_type", "pangolin");
        queryWrapper.eq("status", 1);
        queryWrapper.isNotNull("cookie");
        List<BindAccountLogin> list = bindAccountLoginService.list(queryWrapper);
        if (list != null && !list.isEmpty()) {
            for (BindAccountLogin bindAccountLogin : list) {
                pangolinCrawlerService.getChannelList(bindAccountLogin);
                pangolinCrawlerService.getActivationList(bindAccountLogin, DateUtils.getNowDate("yyyy-MM-dd"), 1);
                pangolinCrawlerService.getCheckList(bindAccountLogin);
                QueryWrapper<PangolinApp> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("account_name", bindAccountLogin.getAccountName());
                List<PangolinApp> appList = pangolinAppService.list(queryWrapper1);
                if (appList != null && !appList.isEmpty()) {
                    appList.forEach(pangolinApp -> pangolinCrawlerService.getRealTimeList(bindAccountLogin, pangolinApp.getId()));
                }
            }
        }
    }

    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IBytedanceFundDailyService bytedanceFundDailyService;
    static ExecutorService executorService = null;


    @Test
    public void testflowFund(){
        List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE);
        if (null == tokens || tokens.size() <= 0) {
            log.error("头条账户流水数据异常：未获取到可用的token");
            return ;
        }
        CountDownLatch countDownLatch   = new CountDownLatch(tokens.size());
        executorService = Executors.newFixedThreadPool(5);
        tokens.forEach(token -> {
            executorService.submit(() -> {
                try {
                    for(int i=0;i<60;i++){
                        String findDate = DateUtils.addDay(DateUtils.formatDate(),-i);
                        bytedanceFundDailyService.loadFundDataByPage(token, findDate, findDate,1);
                    }
                }catch (Exception e){
                }finally {
                    countDownLatch.countDown();
                }
            });
        });
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
















