package com.xxl.job.executor.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxl.job.executor.entity.vo.AccountVideoDTO;
import com.xxl.job.executor.report.entity.ByteDanceVideoReportDaily;
import com.xxl.job.executor.report.mapper.ByteDanceVideoReportDailyMapper;
import com.xxl.job.executor.report.service.IByteDanceVideoReportDailyService;
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;

/**
 * 视频按时间段统计
 * @author jeecg-boot
 * @date   2020-05-13
 * @version V1.0
 */
@Service
@Slf4j
public class ByteDanceVideoReportDailyServiceImpl extends ServiceImpl<ByteDanceVideoReportDailyMapper, ByteDanceVideoReportDaily> implements IByteDanceVideoReportDailyService {

    @Autowired
    private ByteDanceVideoReportDailyMapper byteDanceVideoReportDailyMapper;

    @Override
    public void videoInfoList(String startDate, String endDate){
        //华南
        videoInfoListByCompanyId(startDate, endDate, "6608ed13c0dd42de93c790dbdf124234");
        //华北
        videoInfoListByCompanyId(startDate, endDate, "d57fecdcf7a94d009736d9c850731582");
        //华东
        videoInfoListByCompanyId(startDate, endDate, "4b10089775c040119e139087517aed88");
    }

    private void videoInfoListByCompanyId(String startDate, String endDate, String companyId){
        QueryWrapper<ByteDanceVideoReportDaily> wrapper = new QueryWrapper<>();
        wrapper.ge("stat_datetime",startDate);
        wrapper.le("stat_datetime",endDate);
        wrapper.eq("company_id", companyId);
        byteDanceVideoReportDailyMapper.delete(wrapper);

        List<ByteDanceVideoReportDaily> mList = byteDanceVideoReportDailyMapper.videoInfoList(startDate, endDate, companyId, null);
        if(null!=mList&&!mList.isEmpty()){
            List<ByteDanceVideoReportDaily> reports = new ArrayList<>();
            for(ByteDanceVideoReportDaily report :mList){
                if(null == report.getMaterialName()|| "".equals(report.getMaterialName().trim())){
                    report.setMaterialName("-");
                }
                if(null == report.getProjectName()|| "".equals(report.getProjectName().trim())){
                    report.setProjectName("-");
                }
                if(null == report.getAdvertiserName()|| "".equals(report.getAdvertiserName().trim())){
                    report.setAdvertiserName("-");
                }
                reports.add(report);
            }
            byteDanceVideoReportDailyMapper.insertBatch(reports);
        }
    }

    /**
     * 根据accountId和时间清洗头条素材为头条视频报表数据
     */
    @Override
    public void videoInfoListByAccountId(String startDate, String endDate, Long accountId){
        QueryWrapper<ByteDanceVideoReportDaily> wrapper = new QueryWrapper<>();
        wrapper.ge("stat_datetime",startDate);
        wrapper.le("stat_datetime",endDate);
        wrapper.eq("account_id", accountId);
        byteDanceVideoReportDailyMapper.delete(wrapper);

        String companyId = byteDanceVideoReportDailyMapper.getCompanyIdByAccountId(accountId);

        List<ByteDanceVideoReportDaily> mList = byteDanceVideoReportDailyMapper.videoInfoList(startDate, endDate, companyId, accountId);
        for (ByteDanceVideoReportDaily m:mList){
            byteDanceVideoReportDailyMapper.insert(m);
        }
        log.info("根据accountId和时间清洗头条素材为头条视频报表数据结束，开始时间：{},结束时间:{},accountId:{}",startDate,endDate,accountId );
    }
}
