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.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import cn.com.ctop.common.module.utils.CtopAdConstant;
import cn.com.ctop.common.module.utils.HttpUtils;
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.kuaishou.modules.graphql.service.IKuaishouWebInterfaceService;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceAdvertiserDataService;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceCampaignService;
import cn.com.ctop.toutiao.modules.report.service.IReportService;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceCreativeService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.ctop.entity.KuaishouEffectVideoInfo;
import org.jeecg.modules.ctop.service.IKuaishouEffectVideoInfoService;
import org.jeecg.modules.demo.mock.MockController;
import org.jeecg.modules.demo.test.entity.JeecgDemo;
import org.jeecg.modules.demo.test.mapper.JeecgDemoMapper;
import org.jeecg.modules.demo.test.service.IJeecgDemoService;
import org.jeecg.modules.system.service.ISysDataLogService;
import org.junit.Assert;
import org.jeecg.modules.ctop.service.IRuleEngineService;
import org.jeecg.modules.ctop.vo.QueryParam;
import org.jeecg.modules.ctop.vo.RuleResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SampleTest {
    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private IByteDanceAdvertiserDataService advertiserDataService;
    @Autowired
    private IReportService reportService;
    @Autowired
    private IKuaishouInterfaceService interfaceService;
	@Resource
	private JeecgDemoMapper jeecgDemoMapper;
	@Resource
	private IJeecgDemoService jeecgDemoService;
	@Resource
	private ISysDataLogService sysDataLogService;
	@Resource
	private MockController mock;
    private IByteDanceCreativeService creativeService;

    @Test
    public void loadBytedanceCreativeData() {
        /**
         *
         */
        CtopOauthToken token = oauthTokenService.getTokenByAccountId(1649600126891015L);
        creativeService.getAdvertiserCreative(token,null,null);
//        advertiserDataService.getMaterialList(token);
    }

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

	@Test
	public void testSelect() {
		System.out.println(("----- selectAll method test ------"));
		List<JeecgDemo> userList = jeecgDemoMapper.selectList(null);
		Assert.assertEquals(5, userList.size());
		userList.forEach(System.out::println);
	}

	@Test
	public void testXmlSql() {
		System.out.println(("----- selectAll method test ------"));
		List<JeecgDemo> userList = jeecgDemoMapper.getDemoByName("Sandy12");
		userList.forEach(System.out::println);
	}

	/**
	 * 测试事务
	 */
	@Test
	public void testTran() {
		jeecgDemoService.testTran();
	}

	//author:lvdandan-----date：20190315---for:添加数据日志测试----
	/**
	 * 测试数据日志添加
	 */
	@Test
	public void testDataLogSave() {
		System.out.println(("----- datalog test ------"));
		String tableName = "jeecg_demo";
		String dataId = "4028ef81550c1a7901550c1cd6e70001";
		String dataContent = mock.sysDataLogJson();
		sysDataLogService.addDataLog(tableName, dataId, dataContent);
	}
	//author:lvdandan-----date：20190315---for:添加数据日志测试----


    @Autowired
    private IByteDanceCampaignService campaignService;
    @Test
    public void testGetCampaign(){
        CtopOauthToken token = oauthTokenService.getOauthTokenByAccountId("1649600126891015");
        campaignService.getAdvertiserCampaign(token,"1672261893872756",null);
    }

    @Autowired
    private IKuaishouEffectVideoInfoService effectVideoInfoService;
    @Test
    public void loadKuaishouEffectVideoInfo(){
        List<KuaishouEffectVideoInfo>videoInfos = effectVideoInfoService.list();
        for (KuaishouEffectVideoInfo video:videoInfos) {
            if(null!=video.getSignature()&&!"".equals(video.getSignature())){
                effectVideoInfoService.updateFinalCostDateBySignature(video.getSignature());
                effectVideoInfoService.updateInitialCostDateBySignature(video.getSignature());
                effectVideoInfoService.updateTotalCostBySignature(video.getSignature());
//                effectVideoInfoService.updateTowWeekCostBySignature(video.getSignature());
            }
        }
    }

    @Test
    public void getIndustryInfo(){
        String access_token = "cbdf6502c04978ae401cb92a64530fb73a8c7dbe";
        // 请求地址
        String url = "https://ad.oceanengine.com/open_api/2/tools/industry/get/";
        JSONObject jsonObject = HttpUtils.bytedanceGetRequest(access_token, url, new JSONObject());
        System.out.println(jsonObject.toJSONString());
    }

    @Test
    public void loadImageInfo()throws Exception{
        List<CtopOauthToken>tokens = oauthTokenService.selectToutiaoToken();
        for (CtopOauthToken token:tokens) {
            advertiserDataService.getImageByPage(token,null,1);
            Thread.sleep(1000L);
        }
    }

    @Resource
    private KieSession kieSession;
    @Autowired
    private IRuleEngineService ruleEngineService ;

    @Test
    public void testEngine(){
        QueryParam queryParam1 = new QueryParam() ;
        queryParam1.setParamId("1");
        queryParam1.setParamSign("+");
        QueryParam queryParam2 = new QueryParam() ;
        queryParam2.setParamId("2");
        queryParam2.setParamSign("-");
        // 入参
        kieSession.insert(queryParam1);
        kieSession.insert(queryParam2);
        kieSession.insert(this.ruleEngineService);
        // 返参
        RuleResult resultParam = new RuleResult();
        kieSession.insert(resultParam);
        kieSession.fireAllRules();
        System.out.println(resultParam.isPostCodeResult());
    }
}
