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

import cn.com.ctop.common.module.utils.StringUtils;
import cn.com.ctop.toutiao.modules.report.entity.BytedancePlanDailyReport;
import cn.com.ctop.toutiao.modules.report.mapper.BytedancePlanDailyReportMapper;
import cn.com.ctop.toutiao.modules.report.service.IBytedancePlanDailyReportService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 广告计划日报表信息
 * @Author: jeecg-boot
 * @Date: 2019-09-11
 * @Version: V1.0
 */
@Service
@Primary

public class BytedancePlanDailyReportServiceImpl extends ServiceImpl<BytedancePlanDailyReportMapper, BytedancePlanDailyReport> implements IBytedancePlanDailyReportService {
    @Autowired
    private BytedancePlanDailyReportMapper dailyReportMapper;
    @Override
    public void loadData(String data, String date) {
        String getData = StringUtils.replaceBlank(data);
        String[]orderDatas = getData.split("|");
        if(null!=orderDatas&&orderDatas.length>0){
            for (int i=0;i<orderDatas.length;i++) {
                String orderData = orderDatas[i];
                String[] orderObjects = orderData.split(",");
                String planCode = orderObjects[0].replace("yk_xxl_jrtt_","");
                String registNum =orderObjects[1];
                String orderNum =orderObjects[2];
                QueryWrapper<BytedancePlanDailyReport> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("plan_code",planCode).eq("stat_datetime",date+" 00:00:00");
                BytedancePlanDailyReport report = this.getOne(queryWrapper);
                if(null!=report){
                    report.setOrderNum(Integer.parseInt(orderNum));
                    report.setRegistNum(Integer.parseInt(registNum));
                    this.updateById(report);
                }
            }
        }
    }

    @Override
    public void replaceBatch(List<BytedancePlanDailyReport> dailyReports) {
        dailyReportMapper.replaceBatch(dailyReports);
    }

    @Override
    public void cleanYzData(Integer projectId) {
        dailyReportMapper.cleanYzData(projectId);
        dailyReportMapper.cleanYzDataFuck(projectId);
    }

    @Override
    public void cleanChannelCodeData(Integer projectId, Date getDate) {
        String dateString = DateUtils.formatDate(getDate) + " 00:00:00";
        dailyReportMapper.cleanChannelCodeData(projectId, dateString);
    }

    @Override
    public void cleanYddHourlyChannelCode(Integer projectId, Date getDate) {
        String dateString = DateUtils.formatDate(getDate);
        dailyReportMapper.cleanYddHourlyChannelCode(projectId, dateString);
    }
}
