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

import cn.com.ctop.toutiao.modules.link.entity.BaseReportBytedanceVideoDaily;
import cn.com.ctop.toutiao.modules.link.entity.ETLReportBytedanceVideo;
import cn.com.ctop.toutiao.modules.link.mapper.ETLReportBytedanceVideoMapper;
import cn.com.ctop.toutiao.modules.link.service.IBaseReportBytedanceVideoDailyService;
import cn.com.ctop.toutiao.modules.link.service.IETLReportBytedanceVideoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 头条视频报表ETL
 */
@Slf4j
@Service
public class ETLReportBytedanceVideoServiceImpl extends ServiceImpl<ETLReportBytedanceVideoMapper, ETLReportBytedanceVideo> implements IETLReportBytedanceVideoService {

    @Autowired
    IBaseReportBytedanceVideoDailyService baseReportBytedanceVideoDailyService;

    @Autowired
    ETLReportBytedanceVideoMapper etlReportBytedanceVideoMapper;

    @Override
    public List<BaseReportBytedanceVideoDaily> listByStatDate(String statDate) {
        QueryWrapper<BaseReportBytedanceVideoDaily>  queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("stat_datetime",statDate);
        return baseReportBytedanceVideoDailyService.list(queryWrapper);
    }

/*    @Override
    public void cleanETL(String statDate) {
        List<BaseReportBytedanceVideoDaily> source = listByStatDate(statDate);
        if(source.isEmpty()){
            XxlJobHelper.log("头条数据报表清洗ETL异常，请检查数据源");
        }
        List<ETLReportBytedanceVideo> batch = new ArrayList<>();
        source.forEach(it->{
            JSONObject videoInfo= etlReportBytedanceVideoMapper.queryVideoInfoByMaterialId(it.getMaterialId());
            if(null==videoInfo||videoInfo.isEmpty()){
                XxlJobHelper.log("头条数据报表清洗ETL异常，未关联到video_info,materialId:{}",it.getMaterialId());
            }else {
                //TODO 暂时从老数据库查询，新素材库怕同步数据有偏差
                //JSONObject materialVideo= etlReportBytedanceVideoMapper.queryMaterialVideoByMd5(videoInfo.getString("md5"));
                JSONObject materialVideo= etlReportBytedanceVideoMapper.queryMaterialVideoByMd5Old(videoInfo.getString("md5"));
                //JSONObject relatePerson=etlReportBytedanceVideoMapper.queryRelatePersonByMd5(videoInfo.getString("md5"));
                JSONObject relatePerson=etlReportBytedanceVideoMapper.queryRelatePersonByMd5Old(videoInfo.getString("md5"));

                JSONObject accountDetail=etlReportBytedanceVideoMapper.queryAccountDetailByAccountId(it.getAccountId());
                ETLReportBytedanceVideo temp = JSONObject.toJavaObject(JSONObject.parseObject(JSON.toJSON(it).toString()),ETLReportBytedanceVideo.class);
                temp.setMd5(videoInfo.getString("md5"));
                temp.setToutiaoUrl(videoInfo.getString("url"));
                if(null!=materialVideo&&!materialVideo.isEmpty()) {
                    temp.setUrl(materialVideo.getString("url"));
                    temp.setCoverUrl(materialVideo.getString("coverUrl"));
                    temp.setMaterialName(materialVideo.getString("materialName"));
                    temp.setMaterialCreateTime(materialVideo.getDate("createTime").toString());
                }
                if(null!=accountDetail&&!accountDetail.isEmpty()){
                    temp.setAccountName(accountDetail.getString("accountName"));
                    temp.setProjectId(accountDetail.getLong("projectId"));
                    temp.setProjectName(accountDetail.getString("projectName"));
                    temp.setProductId(accountDetail.getLong("productId"));
                    temp.setProductName(accountDetail.getString("productName"));
                    temp.setAdvertiserId(accountDetail.getString("advertiserId"));
                    temp.setAdvertiserName(accountDetail.getString("advertiserName"));
                }
                if(null!=relatePerson&&!relatePerson.isEmpty()){
                    temp.setClipId(relatePerson.getString("clipId"));
                    temp.setClipName(relatePerson.getString("clipName"));
                    temp.setShotId(relatePerson.getString("shotId"));
                    temp.setShotName(relatePerson.getString("shotName"));
                    temp.setPlanId(relatePerson.getString("planId"));
                    temp.setPlanName(relatePerson.getString("planName"));
                }
                batch.add(temp);
            }
            this.saveBatch(batch);
        });
    }*/

    @Override
    public void cleanETL(String statDate) {
        List<ETLReportBytedanceVideo> etlReportBytedanceVideos = etlReportBytedanceVideoMapper.queryETLData(statDate);
        if(etlReportBytedanceVideos.isEmpty()){
            log.warn("{},头条etl报表清洗异常，base数据源为空",statDate);
            return;
        }
        boolean result = this.saveBatch(etlReportBytedanceVideos);
        if(result)
            log.info("{},头条etl报表清洗完成，数量：{}",statDate,etlReportBytedanceVideos.size());
        else
            log.error("{},头条etl报表清洗失败,请重试",statDate);
    }

}
