package cn.com.ctop.huananvideoreport.service.impl;

import cn.com.ctop.common.module.entity.MaterialInfo;
import cn.com.ctop.huananvideoreport.dto.HuananVideoReportDTO;
import cn.com.ctop.huananvideoreport.entity.HuananVideoReport;
import cn.com.ctop.huananvideoreport.mapper.HuananVideoReportMapper;
import cn.com.ctop.huananvideoreport.service.IHuananVideoReportService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.assertj.core.util.DateUtil;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

/**
 * 华南视频报表类
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-04-28
 */
@Service
public class HuananVideoReportServiceImpl extends ServiceImpl<HuananVideoReportMapper, HuananVideoReport> implements IHuananVideoReportService {

    @Autowired
    private HuananVideoReportMapper huananVideoReportMapper;

    public void getHuananMaterialInfo(String startDate, String endDate) throws ParseException {

        List<HuananVideoReport> mList = huananVideoReportMapper.getHuananMaterialInfo(startDate, endDate);

        for (HuananVideoReport m : mList) {
            huananVideoReportMapper.insert(m);
        }


        QueryWrapper<HuananVideoReport> queryWrapper = new QueryWrapper<>();
        List<HuananVideoReport> mList2 = huananVideoReportMapper.selectList(queryWrapper);

        for (HuananVideoReport m : mList2) {
            String statDate = DateUtils.addDay(m.getStatDatetime(), -1);
            HuananVideoReport mb = huananVideoReportMapper.selectByDateAndId(m.getMaterialId(), statDate);

            BigDecimal huanbi = BigDecimal.ZERO;
            if (mb != null && mb.getCost().compareTo(BigDecimal.ZERO) > 0) {
                huanbi = m.getCost().subtract(mb.getCost()).divide(mb.getCost(), 7, BigDecimal.ROUND_HALF_UP);
            }
            HuananVideoReportDTO accountConcatInfo = huananVideoReportMapper.getAccountConcatInfo(m.getMaterialId());

            m.setAccountId(accountConcatInfo.getAccountId());
            m.setAuthName(accountConcatInfo.getAuthName());
            m.setHuanbi(huanbi);
            huananVideoReportMapper.updateById(m);
        }


    }


}
