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

import cn.com.ctop.toutiao.modules.link.entity.ETLReportBytedanceVideo;
import cn.com.ctop.toutiao.modules.report.entity.EtlBytedanceReportVideoDaily;
import cn.com.ctop.toutiao.modules.report.mapper.EtlBytedanceReportVideoDailyMapper;
import cn.com.ctop.toutiao.modules.report.service.IEtlBytedanceReportVideoDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.BiFunction;

/**
 * 头条视频分日报表信息
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2021-02-20
 */
@Service
@Slf4j
public class EtlBytedanceReportVideoDailyServiceImpl extends ServiceImpl<EtlBytedanceReportVideoDailyMapper, EtlBytedanceReportVideoDaily> implements IEtlBytedanceReportVideoDailyService {


    @Resource
    private EtlBytedanceReportVideoDailyMapper etlBytedanceReportVideoDailyMapper;

    @Override
    public Result updBytedanceVideo(Long accountId, Long projectId, String timeStr) throws Exception {
        CompletableFuture future = CompletableFuture.supplyAsync(() -> {
            return etlBytedanceReportVideoDailyMapper.getBytedanceVideoInfo(accountId, projectId, timeStr);
        }).handle(new BiFunction<List<Map<String, Object>>, Throwable, Object>() {
            @Override
            public Object apply(List<Map<String, Object>> list, Throwable throwable) {
                if (throwable != null) {
                    return Result.error(throwable.getMessage());
                }
                list.forEach(map -> {
                    String coverUrl = map.get("posterUrl") + "";
                    String url = map.get("videoUrl") + "";
                    String md5 = map.get("md5") + "";
                    etlBytedanceReportVideoDailyMapper.updBytedanceVideoByMd5(md5, coverUrl, url);
                });
                return Result.ok("修改成功。");
            }
        });
        return Result.ok(future.get());
    }

    static ExecutorService executorService = Executors.newFixedThreadPool(20);
    static CountDownLatch countDownLatch = null;

    @Override
    public void etlBytedanceVideo(Long accountId, String date) {
        List<ETLReportBytedanceVideo> list = etlBytedanceReportVideoDailyMapper.etlBytedanceVideoInfo(date, accountId);
        if (null != list && !list.isEmpty()) {
            countDownLatch = new CountDownLatch(list.size());
            list.forEach(videoInfo -> executorService.submit(() -> {
                try {
                    etlBytedanceReportVideoDailyMapper.replaceEtlVideoInfo(videoInfo);
                } catch (Exception e) {
                } finally {
                    countDownLatch.countDown();
                }
            }));

            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("数据清洗完成，accountId:{},date:{}", accountId, date);
        }
    }


}
