package cn.com.ctop.kuaishou.modules.report.service.impl;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.service.IProjectService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.CtopAdConstant;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouReportDailyMaterialService;
import cn.com.ctop.kuaishou.modules.report.entity.EtlKuaishouAccountMaterialReportDaily;
import cn.com.ctop.kuaishou.modules.report.mapper.EtlKuaishouAccountMaterialReportDailyMapper;
import cn.com.ctop.kuaishou.modules.report.service.IEtlKuaishouAccountMaterialReportDailyService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 快手账户素材报表清洗表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2021-02-25
 */
@Service
@Slf4j
public class EtlKuaishouAccountMaterialReportDailyServiceImpl extends ServiceImpl<EtlKuaishouAccountMaterialReportDailyMapper, EtlKuaishouAccountMaterialReportDaily> implements IEtlKuaishouAccountMaterialReportDailyService {
    @Resource
    private EtlKuaishouAccountMaterialReportDailyMapper etlKuaishouAccountMaterialReportDailyMapper;
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaiShouReportDailyMaterialService dailyMaterialService;
    static ExecutorService etlExecutorService = Executors.newFixedThreadPool(10);
    static ExecutorService accountEtlExecutorService = Executors.newFixedThreadPool(10);

    @Override
    public void etlKuaishouAccountMaterialReportInfo(String formatDate) {
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        CountDownLatch countDownLatch = new CountDownLatch(tokens.size());
        tokens.forEach(token -> etlExecutorService.submit(() -> {
            try {
                List<EtlKuaishouAccountMaterialReportDaily> getList = etlKuaishouAccountMaterialReportDailyMapper.etlKuaishouAccountMaterialReportDailyList(formatDate, token.getAccountId());
                if (null != getList && !getList.isEmpty()) {
                    getList.forEach(report -> etlKuaishouAccountMaterialReportDailyMapper.replace(report));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
        }));
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("{}数据获取完成", formatDate);
    }

    @Override
    public void etlKuaishouReportCompanyInfo() {
        etlKuaishouAccountMaterialReportDailyMapper.etlKuaishouVideoCompanyInfo();
    }

    @Override
    public void etlKuaishouVideoReportStateDate() {
        etlKuaishouAccountMaterialReportDailyMapper.etlKuaishouVideoReportStateDate();
    }

    @Override
    public void etlKuaishouVideoType() {
        etlKuaishouAccountMaterialReportDailyMapper.etlKuaishouVideoTypeZr(CtopAdConstant.VIDEO_TYPR_ZR);
        etlKuaishouAccountMaterialReportDailyMapper.etlKuaishouVideoTypeZz(CtopAdConstant.VIDEO_TYPR_ZZ);
        etlKuaishouAccountMaterialReportDailyMapper.etlBytedanceVideoTypeZr(CtopAdConstant.VIDEO_TYPR_ZR);
        etlKuaishouAccountMaterialReportDailyMapper.etlBytedanceVideoTypeZz(CtopAdConstant.VIDEO_TYPR_ZZ);
    }

    @Autowired
    private IProjectService projectService;

    @Override
    public void cleanVideoInfo(Long accountId, String statDate) {
        accountEtlExecutorService.submit(() -> {
            JSONObject projectJson = projectService.getProjectByAccountId(accountId);
            Long projectId = projectJson.getLong("projectId");
            String projectName = projectJson.getString("projectName");
            List<EtlKuaishouAccountMaterialReportDaily> reportList = dailyMaterialService.getReportList(accountId, statDate, projectId, projectName);
            if (Check.isNull(reportList)) {
                return;
            }
            List<List<EtlKuaishouAccountMaterialReportDaily>> partition = Lists.partition(reportList, 500);
            for (int i = 0; i < partition.size(); i++) {
                List<EtlKuaishouAccountMaterialReportDaily> addList = partition.get(i);
                if (!Check.isNull(addList)) {
                    etlKuaishouAccountMaterialReportDailyMapper.addBatch(addList);
                }
            }
        });
    }

    @Override
    public List<String> getDateList() {
        return etlKuaishouAccountMaterialReportDailyMapper.getDateList();
    }

    @Override
    public List<Long> getAccountListByDate(String date) {
        return etlKuaishouAccountMaterialReportDailyMapper.getAccountList(date);
    }
}
