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

import cn.com.ctop.common.module.mapper.UserAllocationMapper;
import cn.com.ctop.common.module.service.IMaterialAscriptionService;
import cn.com.ctop.toutiao.modules.report.entity.BytedanceVideoEtlInfo;
import cn.com.ctop.toutiao.modules.report.mapper.BytedanceVideoEtlInfoMapper;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceReportMaterialDailyService;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceVideoEtlInfoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 头条视频数据清洗信息
 * @author jeecg-boot
 * @date   2020-12-21
 * @version V1.0
 */
@Slf4j
@Service
public class BytedanceVideoEtlInfoServiceImpl extends ServiceImpl<BytedanceVideoEtlInfoMapper, BytedanceVideoEtlInfo> implements IBytedanceVideoEtlInfoService {
    @Autowired
    private IBytedanceReportMaterialDailyService materialDailyService;
    @Resource
    private UserAllocationMapper userAllocationMapper;
    static ExecutorService executorService = Executors.newFixedThreadPool(100);
    @Autowired
    private IMaterialAscriptionService materialAscriptionService;
    @Override
    public void etlBytedanceVideoInfo(Date getDate) {
        String date = DateUtils.formatDate(getDate);
        //1:查询单当日视频信息
        List<BytedanceVideoEtlInfo> materials = materialDailyService.getSignatureByDate(date);
        if(null!=materials&&!materials.isEmpty()){
            CountDownLatch countDownLatch= new CountDownLatch(materials.size());
            materials.forEach(material->{
                executorService.submit(()->{
                    try {
                        if(null == material.getVideoCode()|| "".equals(material.getVideoCode().trim())){
                            return;
                        }
                        String signature = material.getVideoCode();
                        BytedanceVideoEtlInfo etlInfo = this.getBySignature(signature);
                        BytedanceVideoEtlInfo nowData = materialDailyService.getEtlDataByParams(signature,date);
                        String minDate = nowData.getStateDate();
                        Integer days = DateUtils.getdaysOfTwoDate(minDate,date);
                        if(null == etlInfo){
                            //第一次查询的数据
                            etlInfo = nowData;
                            BigDecimal totalCost = etlInfo.getTotalCost();
                            JSONObject ascription =  materialAscriptionService.getDetailByCode(signature);
                            String planId = null;
                            if(null!=ascription){
                                planId = ascription.getString("planId");
                                if(null!=planId&&!"".equals(planId.trim())){
                                    etlInfo.setPlanId(planId);
                                }
                                String planName =  ascription.getString("planName");
                                if(null!=planName&&!"".equals(planName.trim())){
                                    etlInfo.setPlanName(planName);
                                }
                                String clipId = ascription.getString("clipId");
                                if(null!=clipId&&!"".equals(clipId.trim())){
                                    etlInfo.setClipId(clipId);
                                }
                                String clipName = ascription.getString("clipName");
                                if(null!=clipName&&!"".equals(clipName.trim())){
                                    etlInfo.setClipName(clipName);
                                }
                                String shotId = ascription.getString("shotId");
                                if(null!=shotId&&!"".equals(shotId.trim())){
                                    etlInfo.setShotId(shotId);
                                }
                                String shotName = ascription.getString("shotName");
                                if(null!=shotName&&!"".equals(shotName.trim())){
                                    etlInfo.setShotName(shotName);
                                }
                            }
                            if(null!=planId){
                                String companyName = userAllocationMapper.getCompanyNameByUserId(planId);
                                if (null!=companyName&&!"".equals(companyName.trim())){
                                    etlInfo.setCompanyName(companyName);
                                }
                            }
                            //计算投放总天数
                            if(days <= 7){
                                etlInfo.setSingleWeekCost(nowData.getTotalCost());
                            }
                            if(days <= 14){
                                etlInfo.setTwoWeekCost(nowData.getTotalCost());
                            }
                            if(days <= 28){
                                etlInfo.setFourWeekCost(nowData.getTotalCost());
                            }
                            //计算是否有效，爆款
                            if(totalCost.compareTo(new BigDecimal("5000"))>0){
                                //此刻成为有效
                                etlInfo.setEffectDate(date);
                                etlInfo.setEffectDayNum(days);
                            }
                            if(totalCost.compareTo(new BigDecimal("50000"))>0){
                                //此刻成为爆款
                                etlInfo.setFaddishDate(date);
                                etlInfo.setFaddishDayNum(days);
                            }
                            etlInfo.setCreateTime(new Date());
                            etlInfo.setUpdateTime(new Date());
                            this.save(etlInfo);
                        }else{
                            JSONObject ascription = materialAscriptionService.getDetailByCode(signature);
                            String planId = null;
                            if(null!=ascription){
                                planId = ascription.getString("planId");
                                if(null!=planId&&!"".equals(planId.trim())){
                                    etlInfo.setPlanId(planId);
                                }
                                String planName =  ascription.getString("planName");
                                if(null!=planName&&!"".equals(planName.trim())){
                                    etlInfo.setPlanName(planName);
                                }
                                String clipId = ascription.getString("clipId");
                                if(null!=clipId&&!"".equals(clipId.trim())){
                                    etlInfo.setClipId(clipId);
                                }
                                String clipName = ascription.getString("clipName");
                                if(null!=clipName&&!"".equals(clipName.trim())){
                                    etlInfo.setClipName(clipName);
                                }
                                String shotId = ascription.getString("shotId");
                                if(null!=shotId&&!"".equals(shotId.trim())){
                                    etlInfo.setShotId(shotId);
                                }
                                String shotName = ascription.getString("shotName");
                                if(null!=shotName&&!"".equals(shotName.trim())){
                                    etlInfo.setShotName(shotName);
                                }
                            }
                            if(null!=planId){
                                String companyName = userAllocationMapper.getCompanyNameByUserId(planId);
                                if (null!=companyName&&!"".equals(companyName.trim())){
                                    etlInfo.setCompanyName(companyName);
                                }
                            }
                            etlInfo.setTotalCost(nowData.getTotalCost());
                            BigDecimal totalCost = etlInfo.getTotalCost();
                            etlInfo.setClickNum(nowData.getClickNum());
                            etlInfo.setShowNum(nowData.getShowNum());
                            etlInfo.setActiveNum(nowData.getActiveNum());
                            etlInfo.setConvertNum(nowData.getConvertNum());
                            etlInfo.setUpdateTime(new Date());
                            //计算投放总天数
                            if(days <= 7){
                                etlInfo.setSingleWeekCost(nowData.getTotalCost());
                            }
                            if(days <= 14){
                                etlInfo.setTwoWeekCost(nowData.getTotalCost());
                            }
                            if(days <= 28){
                                etlInfo.setFourWeekCost(nowData.getTotalCost());
                            }
                            if(null == etlInfo.getEffectDayNum()){
                                if(totalCost.compareTo(new BigDecimal("5000"))>0){
                                    //此刻成为有效
                                    etlInfo.setEffectDate(date);
                                    etlInfo.setEffectDayNum(days);
                                }
                            }
                            if(null == etlInfo.getFaddishDayNum()){
                                if(totalCost.compareTo(new BigDecimal("50000"))>0){
                                    //此刻成为爆款
                                    etlInfo.setFaddishDate(date);
                                    etlInfo.setFaddishDayNum(days);
                                }
                            }
                            this.updateById(etlInfo);
                        }
                    }catch (Exception e){

                    }finally {
                        countDownLatch.countDown();
                    }
                });
            });

            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("{}数据获取完成",date);
        }
    }

    @Override
    public BytedanceVideoEtlInfo getBySignature(String signature) {
        QueryWrapper<BytedanceVideoEtlInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("video_code",signature).last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public void etlBytedanceVideoInfoByProjectId(Date getDate) {
        String date = DateUtils.formatDate(getDate);
        //1:查询单当日视频信息
        List<BytedanceVideoEtlInfo> materials = materialDailyService.getSignatureByDate(date);
        if(null!=materials&&!materials.isEmpty()){
            CountDownLatch countDownLatch= new CountDownLatch(materials.size());
            materials.forEach(material->{
                executorService.submit(()->{
                    try {
                        if(null == material.getVideoCode()|| "".equals(material.getVideoCode().trim())){
                            return;
                        }
                        String signature = material.getVideoCode();
                        BytedanceVideoEtlInfo etlInfo = this.getBySignature(signature);
                        BytedanceVideoEtlInfo nowData = materialDailyService.getEtlDataByParams(signature,date);
                        String minDate = nowData.getStateDate();
                        Integer days = DateUtils.getdaysOfTwoDate(minDate,date);
                        if(null == etlInfo){
                            //第一次查询的数据
                            etlInfo = nowData;
                            BigDecimal totalCost = etlInfo.getTotalCost();
                            JSONObject ascription =  materialAscriptionService.getDetailByCode(signature);
                            String planId = null;
                            if(null!=ascription){
                                planId = ascription.getString("planId");
                                if(null!=planId&&!"".equals(planId.trim())){
                                    etlInfo.setPlanId(planId);
                                }
                                String planName =  ascription.getString("planName");
                                if(null!=planName&&!"".equals(planName.trim())){
                                    etlInfo.setPlanName(planName);
                                }
                                String clipId = ascription.getString("clipId");
                                if(null!=clipId&&!"".equals(clipId.trim())){
                                    etlInfo.setClipId(clipId);
                                }
                                String clipName = ascription.getString("clipName");
                                if(null!=clipName&&!"".equals(clipName.trim())){
                                    etlInfo.setClipName(clipName);
                                }
                                String shotId = ascription.getString("shotId");
                                if(null!=shotId&&!"".equals(shotId.trim())){
                                    etlInfo.setShotId(shotId);
                                }
                                String shotName = ascription.getString("shotName");
                                if(null!=shotName&&!"".equals(shotName.trim())){
                                    etlInfo.setShotName(shotName);
                                }
                            }
                            if(null!=planId){
                                String companyName = userAllocationMapper.getCompanyNameByUserId(planId);
                                if (null!=companyName&&!"".equals(companyName.trim())){
                                    etlInfo.setCompanyName(companyName);
                                }
                            }
                            //计算投放总天数
                            if(days <= 7){
                                etlInfo.setSingleWeekCost(nowData.getTotalCost());
                            }
                            if(days <= 14){
                                etlInfo.setTwoWeekCost(nowData.getTotalCost());
                            }
                            if(days <= 28){
                                etlInfo.setFourWeekCost(nowData.getTotalCost());
                            }
                            //计算是否有效，爆款
                            if(totalCost.compareTo(new BigDecimal("5000"))>0){
                                //此刻成为有效
                                etlInfo.setEffectDate(date);
                                etlInfo.setEffectDayNum(days);
                            }
                            if(totalCost.compareTo(new BigDecimal("100000"))>0){
                                //此刻成为爆款
                                etlInfo.setFaddishDate(date);
                                etlInfo.setFaddishDayNum(days);
                            }
                            etlInfo.setCreateTime(new Date());
                            etlInfo.setUpdateTime(new Date());
                            this.save(etlInfo);
                        }else{
                            JSONObject ascription =  materialAscriptionService.getDetailByCode(signature);
                            String planId = null;
                            if(null!=ascription){
                                planId = ascription.getString("planId");
                                if(null!=planId&&!"".equals(planId.trim())){
                                    etlInfo.setPlanId(planId);
                                }
                                String planName =  ascription.getString("planName");
                                if(null!=planName&&!"".equals(planName.trim())){
                                    etlInfo.setPlanName(planName);
                                }
                                String clipId = ascription.getString("clipId");
                                if(null!=clipId&&!"".equals(clipId.trim())){
                                    etlInfo.setClipId(clipId);
                                }
                                String clipName = ascription.getString("clipName");
                                if(null!=clipName&&!"".equals(clipName.trim())){
                                    etlInfo.setClipName(clipName);
                                }
                                String shotId = ascription.getString("shotId");
                                if(null!=shotId&&!"".equals(shotId.trim())){
                                    etlInfo.setShotId(shotId);
                                }
                                String shotName = ascription.getString("shotName");
                                if(null!=shotName&&!"".equals(shotName.trim())){
                                    etlInfo.setShotName(shotName);
                                }
                            }
                            if(null!=planId){
                                String companyName = userAllocationMapper.getCompanyNameByUserId(planId);
                                if (null!=companyName&&!"".equals(companyName.trim())){
                                    etlInfo.setCompanyName(companyName);
                                }
                            }
                            //只需要更新汇总数据
                            etlInfo.setTotalCost(nowData.getTotalCost());
                            BigDecimal totalCost = etlInfo.getTotalCost();
                            etlInfo.setClickNum(nowData.getClickNum());
                            etlInfo.setShowNum(nowData.getShowNum());
                            etlInfo.setActiveNum(nowData.getActiveNum());
                            etlInfo.setConvertNum(nowData.getConvertNum());
                            etlInfo.setUpdateTime(new Date());
                            //计算投放总天数
                            if(days <= 7){
                                etlInfo.setSingleWeekCost(nowData.getTotalCost());
                            }
                            if(days <= 14){
                                etlInfo.setTwoWeekCost(nowData.getTotalCost());
                            }
                            if(days <= 28){
                                etlInfo.setFourWeekCost(nowData.getTotalCost());
                            }
                            if(null == etlInfo.getEffectDayNum()){
                                if(totalCost.compareTo(new BigDecimal("5000"))>0){
                                    //此刻成为有效
                                    etlInfo.setEffectDate(date);
                                    etlInfo.setEffectDayNum(days);
                                }
                            }
                            if(null == etlInfo.getFaddishDayNum()){
                                if(totalCost.compareTo(new BigDecimal("100000"))>0){
                                    //此刻成为爆款
                                    etlInfo.setFaddishDate(date);
                                    etlInfo.setFaddishDayNum(days);
                                }
                            }
                            this.updateById(etlInfo);
                        }
                    }catch (Exception e){

                    }finally {
                        countDownLatch.countDown();
                    }
                });
            });

            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("{}数据获取完成",date);
        }
    }
}
