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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDaily;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDailyGender;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyGenderMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouAudienceReportDailyGenderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 人群分析报表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-12-03
 */
@Slf4j
@Service
public class KuaishouAudienceReportDailyGenderServiceImpl extends ServiceImpl<KuaishouAudienceReportDailyGenderMapper, KuaishouAudienceReportDailyGender> implements IKuaishouAudienceReportDailyGenderService {
    @Resource
    KuaishouAudienceReportDailyGenderMapper genderMapper;

    @Override
    public void mergeAudienceReportDailyDate(String statDate){
        List<KuaishouAudienceReportDailyGender> genderList = genderMapper.selectByStatDate(statDate);
        Map<String,KuaishouAudienceReportDailyGender> genderResultMap = new HashMap<>();
        List<KuaishouAudienceReportDailyGender> result = new ArrayList<>();
        if(!Check.isNull(genderList)){
            genderList.forEach(gender ->{
                String key = gender.getProjectId()+gender.getGender()+gender.getSignature()+gender.getStatDate();
                if(genderResultMap.containsKey(key)){
                    KuaishouAudienceReportDailyGender mergeGender = genderResultMap.get(key);
                    merge(mergeGender, gender);
                }else {
                    genderResultMap.put(key,gender);
                }
            });
        }

        Set<String> keys = genderResultMap.keySet();
        keys.forEach(key -> result.add(genderResultMap.get(key)));

        if(!Check.isNull(result)){
            saveAudienceReportDailyGenderToDb(result);
        }
    }

    public void merge(KuaishouAudienceReportDaily o1, KuaishouAudienceReportDaily o2){
        o1.setAclick(o1.getAclick()+o2.getAclick());
        o1.setBclick(o1.getBclick()+o2.getBclick());
        o1.setPhotoClickCost(o1.getPhotoClickCost()+o2.getPhotoClickCost());
        o1.setDownloadStarted(o1.getDownloadStarted()+o2.getDownloadStarted());
        o1.setDownloadCompleted(o1.getDownloadCompleted()+o2.getDownloadCompleted());
        o1.setActivation(o1.getActivation()+o2.getActivation());
        o1.setSubmit(o1.getSubmit()+o2.getSubmit());
        o1.setPhotoShow(o1.getPhotoShow()+o2.getPhotoShow());
        o1.setPhotoClickRatio(o1.getPhotoClickRatio()+o2.getPhotoClickRatio());
        o1.setPhotoClick(o1.getPhotoClick()+o2.getPhotoClick());
        o1.setCharge(o1.getCharge()+o2.getCharge());
        o1.setActionRatio(o1.getActionRatio()+o2.getActionRatio());
//        if(!Check.isNull(o1.getImpression_1kCost()) && !Check.isNull(o2.getImpression_1kCost())){
//        }else{
//            o1.setImpression_1kCost(!Check.isNull(o1.getImpression_1kCost()) ? o1.getImpression_1kCost() : !Check.isNull(o2.getImpression_1kCost()) ? o2.getImpression_1kCost() : null);
//        }

        o1.setImpression_1kCost(add(o1.getImpression_1kCost(),o2.getImpression_1kCost()));
        o1.setActionCost(add(o1.getActionCost(),o2.getActionCost()));
        o1.setEventPayFirstDay(o1.getEventPayFirstDay()+o2.getEventPayFirstDay());
        o1.setEventPayPurchaseAmountFirstDay(add(o1.getEventPayPurchaseAmountFirstDay(),o2.getEventPayPurchaseAmountFirstDay()));

        o1.setEventPayFirstDayRoi(o1.getEventPayFirstDayRoi()+o2.getEventPayFirstDayRoi());
        o1.setEventPay(o1.getEventPay()+o2.getEventPay());
        o1.setEventPayPurchaseAmount(add(o1.getEventPayPurchaseAmount(),o2.getEventPayPurchaseAmount()));

        o1.setEventPayRoi(o1.getEventPayRoi()+o2.getEventPayRoi());
        o1.setEventRegister(o1.getEventRegister()+o2.getEventRegister());
        o1.setEventRegisterCost(add(o1.getEventRegisterCost(),o2.getEventRegisterCost()));

        o1.setEventRegisterRatio(o1.getEventRegisterRatio()+o2.getEventRegisterRatio());
        o1.setEventJinJianApp(o1.getEventJinJianApp()+o2.getEventJinJianApp());
        o1.setEventJinJianAppCost(add(o1.getEventJinJianAppCost(),o2.getEventJinJianAppCost()));
        o1.setEventCreditGrantApp(o1.getEventCreditGrantApp()+o2.getEventCreditGrantApp());
        o1.setEventCreditGrantAppCost(add(o1.getEventCreditGrantAppCost(),o2.getEventCreditGrantAppCost()));
        o1.setEventOrderPaid(o1.getEventOrderPaid()+o2.getEventOrderPaid());
        o1.setEventOrderPaidPurchaseAmount(add(o1.getEventOrderPaidPurchaseAmount(),o2.getEventOrderPaidPurchaseAmount()));
        o1.setEventNextDayStayCost(add(o1.getEventNextDayStayCost(),o2.getEventNextDayStayCost()));
        o1.setEventNextDayStayRatio(add(o1.getEventNextDayStayRatio(),o2.getEventNextDayStayRatio()));
        o1.setEventOrderPaidCost(add(o1.getEventOrderPaidCost(),o2.getEventOrderPaidCost()));

        o1.setEventNextDayStay(o1.getEventNextDayStay()+o2.getEventNextDayStay());
        o1.setEventAppInvoked(o1.getEventAppInvoked()+o2.getEventAppInvoked());
        o1.setEventCreditGrantAppRatio(add(o1.getEventCreditGrantAppRatio(),o2.getEventCreditGrantAppRatio()));
    }

    public BigDecimal add(BigDecimal b1, BigDecimal b2){
        if(!Check.isNull(b1) && !Check.isNull(b2)){
            return b1.add(b2);
        }else{
            return !Check.isNull(b1) ? b1 : !Check.isNull(b2) ? b2 : new BigDecimal(0);
        }
    }

    private void saveAudienceReportDailyGenderToDb(List<KuaishouAudienceReportDailyGender> mergeGenderList) {
        if(mergeGenderList == null || mergeGenderList.isEmpty()){
            return;
        }

        List<KuaishouAudienceReportDailyGender> batchList = new ArrayList<>();
        for(KuaishouAudienceReportDailyGender mergeGender : mergeGenderList){
            batchList.add(mergeGender);

            if(batchList.size() >= 1000){
                genderMapper.replaceBatchNewMerge(batchList);
                batchList.clear();
            }
        }

        if(batchList.isEmpty() == false){
            genderMapper.replaceBatchNewMerge(batchList);
            batchList.clear();
        }
    }
}
