package ctop.bytedance.ad.report.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import ctop.bytedance.ad.common.utils.Check;
import ctop.bytedance.ad.report.entity.BytedanceReportMaterialDaily;
import ctop.bytedance.ad.report.mapper.BytedanceReportMaterialDailyMapper;
import ctop.bytedance.ad.report.service.IBytedanceReportMaterialDailyService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 素材报表
 * @author jeecg-boot
 * @date   2021-05-18
 * @version V1.0
 */
@Service
@Primary
public class BytedanceReportMaterialDailyServiceImpl extends ServiceImpl<BytedanceReportMaterialDailyMapper, BytedanceReportMaterialDaily> implements IBytedanceReportMaterialDailyService {

    @Resource
    private BytedanceReportMaterialDailyMapper bytedanceReportMaterialDailyMapper;

    @Override
    public Result getMaterialDailyReportByAccountId(String accountId, String startTime, String endTime, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> dailyReportList = bytedanceReportMaterialDailyMapper.selectMaterialDailyReportByAccountId(accountId, startTime, endTime);

        Date maxUpdateTime = null;

        for (Map<String, Object> dailyReportMap: dailyReportList) {
            if(dailyReportMap == null || dailyReportMap.isEmpty()){
                return Result.error("头条视频日报数据为空。");
            }



            if(Check.isNull(maxUpdateTime) || maxUpdateTime.before(DateUtils.str2Date(dailyReportMap.get("updateTime").toString(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")))){
                maxUpdateTime = DateUtils.str2Date(dailyReportMap.get("updateTime").toString(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            }

            if(dailyReportMap.get("count") == null){
                dailyReportMap.put("count",0);
            }

            BigDecimal cost = new BigDecimal(Check.isNull(dailyReportMap.get("cost")) ? "0" : dailyReportMap.get("cost").toString());
            BigDecimal click = new BigDecimal(Check.isNull(dailyReportMap.get("click")) ? "0" : dailyReportMap.get("click").toString());
            BigDecimal showMaterial = new BigDecimal(Check.isNull(dailyReportMap.get("showMaterial")) ? "0" : dailyReportMap.get("showMaterial").toString());
            BigDecimal convertMaterial = new BigDecimal(Check.isNull(dailyReportMap.get("convertMaterial")) ? "0" : dailyReportMap.get("convertMaterial").toString());


            DecimalFormat decimalFormat = new DecimalFormat("0.00#");
            dailyReportMap.put("cost", decimalFormat.format(cost));
            dailyReportMap.put("click", click);
            dailyReportMap.put("showMaterial", showMaterial);
            dailyReportMap.put("convertMaterial", convertMaterial);

            //被除数不能为0
            if (StringUtils.equals("0",showMaterial.toString())){
                dailyReportMap.put("costPerThousandShow", 0);
                dailyReportMap.put("clickRate", 0);
            }else {
                BigDecimal costPerThousandShow = cost.multiply(new BigDecimal(1000)).divide(showMaterial, 2, BigDecimal.ROUND_HALF_UP);
                dailyReportMap.put("costPerThousandShow", decimalFormat.format(costPerThousandShow));
                BigDecimal clickRate = click.multiply(new BigDecimal(100)).divide(showMaterial, 2, BigDecimal.ROUND_HALF_UP);
                dailyReportMap.put("clickRate", decimalFormat.format(clickRate));
            }
            if (StringUtils.equals("0", convertMaterial.toString())) {
                dailyReportMap.put("costConvert", 0);
            }else {
                BigDecimal costConvert = cost.divide(convertMaterial, 2, BigDecimal.ROUND_HALF_UP);
                dailyReportMap.put("costConvert", decimalFormat.format(costConvert));
            }
            if (StringUtils.equals("0", click.toString())) {
                dailyReportMap.put("costPerClick", 0);
                dailyReportMap.put("convertRate", 0);
            }else {
                BigDecimal costPerClick = cost.divide(click, 2, BigDecimal.ROUND_HALF_UP);
                dailyReportMap.put("costPerClick", decimalFormat.format(costPerClick));
                BigDecimal convertRate = convertMaterial.multiply(new BigDecimal(100)).divide(click,2,BigDecimal.ROUND_HALF_UP);
                dailyReportMap.put("convertRate", decimalFormat.format(convertRate));
            }
        }

        for (Map<String, Object> dailyReportMap: dailyReportList) {
            if(!Check.isNull(maxUpdateTime)){
                dailyReportMap.put("updateTime",maxUpdateTime);
            }

        }

        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(dailyReportList);

        return Result.successMsg("数据加载成功。",pageInfo);
    }
}
