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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.report.mapper.AccountReportMapper;
import cn.com.ctop.kuaishou.modules.report.service.IAccountReportService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.DateUtils;
import org.springframework.stereotype.Service;

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

/**
 * @author yumeng
 * 2019年11月12日10:04:00
 */
@Service
public class AccountReportServiceImpl implements IAccountReportService {
    @Resource
    private AccountReportMapper accountReportMapper;

    /**
     * 获取 汇总
     *
     * @param json
     * @return
     */
    @Override
    public JSONObject getSummaryReport(JSONObject json) {
        JSONObject returnJson = new JSONObject();
        if (Check.isNull(json)) {
            return null;
        }
        JSONArray accountIds = json.getJSONArray("accountIds");
        if (Check.isNull(accountIds)) {
            return null;
        }
        try {
            JSONObject accountJson = new JSONObject();
            List<JSONObject> detail = new ArrayList<>();

            Integer type = json.getInteger("type");
            BigDecimal discount = json.getBigDecimal("discount");
            String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
            String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
            if (type == 1) {
                // 今日汇总
                Calendar calendar = Calendar.getInstance();
                int maxHour = calendar.get(Calendar.HOUR_OF_DAY);
                if(maxHour == 0){
                    return null;
                }
                String maxHourStr = String.valueOf(maxHour - 1);
                // 今日 小时汇总数据
                accountJson = accountReportMapper.selectSumDayHourlyByHourAndDate(nowDate, maxHourStr, accountIds);
                // 昨日 小时汇总数据
                JSONObject yesterdayJson = accountReportMapper.selectSumDayHourlyByHourAndDate(anotherDay, maxHourStr, accountIds);
                accountJson.put("maxHour", maxHour);
                if (!Check.isNull(accountJson) && !Check.isNull(yesterdayJson)) {
                    // 今日消耗
                    BigDecimal cost = accountJson.getBigDecimal("cost");
                    // 昨日消耗
                    BigDecimal yesterdayCost = yesterdayJson.getBigDecimal("cost");
                    BigDecimal compareBigDecimal = new BigDecimal(0);
                    if (yesterdayCost.compareTo(compareBigDecimal) != 0) {
                        BigDecimal costProportion = (cost.subtract(yesterdayCost)).divide(yesterdayCost, 2, RoundingMode.HALF_UP);
                        accountJson.put("costProportion", costProportion);
                    } else {
                        accountJson.put("costProportion", 0);
                    }

                    BigDecimal discountCost = cost.divide(discount, 2, RoundingMode.HALF_UP);
                    // 折后消耗
                    accountJson.put("discountCost", discountCost);
                    BigDecimal yesterdayDiscountCost = yesterdayCost.divide(discount, 2, RoundingMode.HALF_UP);
                    if (yesterdayDiscountCost.compareTo(compareBigDecimal) != 0) {
                        BigDecimal discountCostProportion = (discountCost.subtract(yesterdayDiscountCost)).divide(yesterdayDiscountCost, 2, RoundingMode.HALF_UP);
                        accountJson.put("discountCostProportion", discountCostProportion);
                    } else {
                        accountJson.put("discountCostProportion", 0);
                    }

                    // 今日封面展示
                    Long photoShow = accountJson.getLong("photoShow");
                    // 昨日封面展示
                    Long yesterdayPhotoShow = yesterdayJson.getLong("photoShow");
                    if (yesterdayPhotoShow != 0L) {
                        double showProportion = (photoShow.doubleValue() - yesterdayPhotoShow.doubleValue()) / yesterdayPhotoShow.doubleValue();
                        accountJson.put("showProportion", showProportion);
                    } else {
                        accountJson.put("showProportion", 0);
                    }

                    // 今日封面展示
                    Long photoClick = accountJson.getLong("photoClick");
                    // 昨日封面展示
                    Long yesterdayPhotoClick = yesterdayJson.getLong("photoClick");
                    // 较昨日增加比例
                    if (yesterdayPhotoClick != 0L) {
                        double photoClickProportion = (photoClick.doubleValue() - yesterdayPhotoClick.doubleValue()) / yesterdayPhotoClick.doubleValue();
                        accountJson.put("photoClickProportion", photoClickProportion);
                    } else {
                        accountJson.put("photoClickProportion", 0);
                    }

                    Long aClick = accountJson.getLong("aclick");
                    Long yesterdayAClick = yesterdayJson.getLong("aclick");
                    if (yesterdayAClick != 0L) {
                        double aClickProportion = (aClick.doubleValue() - yesterdayAClick.doubleValue()) / yesterdayAClick.doubleValue();
                        accountJson.put("aClickProportion", aClickProportion);
                    } else {
                        accountJson.put("aClickProportion", 0);
                    }
                }


                // 折线图
                List<JSONObject> yesterdayDetailArr = accountReportMapper.selectReportDetail(anotherDay, accountIds);
                for (int i = 0; i < yesterdayDetailArr.size(); i++) {

                    JSONObject yesterdayDetail = yesterdayDetailArr.get(i);
                    if (!Check.isNull(yesterdayDetail)) {
                        String yesterdayStatHour = yesterdayDetail.getString("statHour");
                        JSONObject nowJson = accountReportMapper.selectDayHourlyByHourAndDate(nowDate, yesterdayStatHour, accountIds);
                        if (!Check.isNull(nowJson)) {
                            nowJson.put("statDate", nowDate);
                            // 今日消耗
                            BigDecimal cost = nowJson.getBigDecimal("cost");
                            // 昨日消耗
                            BigDecimal yesterdayCost = yesterdayDetail.getBigDecimal("cost");
                            BigDecimal compareBigDecimal = new BigDecimal(0);
                            if (yesterdayCost.compareTo(compareBigDecimal) != 0) {
                                BigDecimal costProportion = ((cost.subtract(yesterdayCost))).divide(yesterdayCost, 2, RoundingMode.HALF_UP);
                                nowJson.put("costProportion", costProportion);
                            } else {
                                nowJson.put("costProportion", 0);
                            }

                            // 今日封面展示
                            Long photoShow = nowJson.getLong("photoShow");
                            // 昨日封面展示
                            Long yesterdayPhotoShow = yesterdayDetail.getLong("photoShow");
                            if (yesterdayPhotoShow != 0L) {
                                double showProportion = (photoShow.doubleValue() - yesterdayPhotoShow.doubleValue()) / yesterdayPhotoShow.doubleValue();
                                // 较昨日增加比例
                                nowJson.put("showProportion", showProportion);
                            } else {
                                // 较昨日增加比例
                                nowJson.put("showProportion", 0);
                            }

                            // 今日封面展示
                            Long photoClick = nowJson.getLong("photoClick");
                            // 昨日封面展示
                            Long yesterdayPhotoClick = yesterdayDetail.getLong("photoClick");
                            if (yesterdayPhotoClick != 0L) {
                                double photoClickProportion = (photoClick.doubleValue() - yesterdayPhotoClick.doubleValue()) / yesterdayPhotoClick.doubleValue();
                                nowJson.put("photoClickProportion", photoClickProportion);
                            } else {
                                nowJson.put("photoClickProportion", 0);
                            }

                            // 今日封面展示
                            Long aClick = nowJson.getLong("aclick");
                            // 昨日封面展示
                            Long yesterdayAClick = yesterdayDetail.getLong("aclick");
                            if (yesterdayAClick != 0L) {
                                double aClickProportion = (aClick.doubleValue() - yesterdayAClick.doubleValue()) / yesterdayAClick.doubleValue();
                                // 较昨日增加比例
                                nowJson.put("aClickProportion", aClickProportion);
                            } else {
                                // 较昨日增加比例
                                nowJson.put("aClickProportion", 0);
                            }

                            BigDecimal discountCost = cost.divide(discount, 2, RoundingMode.HALF_UP);
                            // 折后消耗
                            nowJson.put("discountCost", discountCost);
                            BigDecimal yesterdayDiscountCost = yesterdayCost.divide(discount, 2, RoundingMode.HALF_UP);
                            if (yesterdayDiscountCost.compareTo(compareBigDecimal) != 0) {
                                BigDecimal discountCostProportion = (discountCost.subtract(yesterdayDiscountCost)).divide(yesterdayDiscountCost, 2, RoundingMode.HALF_UP);
                                nowJson.put("discountCostProportion", discountCostProportion);
                            } else {
                                nowJson.put("discountCostProportion", 0);
                            }


                            yesterdayDetail.put("now", nowJson);
                        }
                    }

                    detail.add(yesterdayDetail);
                }
            } else if (type == 2) {
                accountJson = accountReportMapper.selectDayReport(anotherDay, accountIds);
                detail = accountReportMapper.selectReportDetail(anotherDay, accountIds);
            } else if (type == 3) {
                String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", anotherDay, -6);
                accountJson = accountReportMapper.selectByDate(anotherDay, endDate, accountIds);
                detail = accountReportMapper.selectDayDetailByDate(anotherDay, endDate, accountIds);
            } else if (type == 4) {
                String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", anotherDay, -14);
                accountJson = accountReportMapper.selectByDate(anotherDay, endDate, accountIds);
                detail = accountReportMapper.selectDayDetailByDate(anotherDay, endDate, accountIds);
            } else if (type == 5) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -1);
                accountJson = accountReportMapper.selectByDate(anotherDay, endDate, accountIds);
                detail = accountReportMapper.selectDayDetailByDate(anotherDay, endDate, accountIds);
            } else if (type == 6) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -3);
                accountJson = accountReportMapper.selectByDate(anotherDay, endDate, accountIds);
                detail = accountReportMapper.selectDayDetailByDate(anotherDay, endDate, accountIds);
            } else if (type == 7) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -6);
                accountJson = accountReportMapper.selectByDate(anotherDay, endDate, accountIds);
                detail = accountReportMapper.selectDayDetailByMonth(anotherDay, endDate, accountIds);
            } else if (type == 8) {
                accountJson = accountReportMapper.selectAllAccount(accountIds);
                detail = accountReportMapper.selectAllByAccounts(accountIds);
            } else if (type == 9) {
                String startDate = json.getString("startDate");
                String endDate = json.getString("endDate");
                accountJson = accountReportMapper.selectByDate(startDate, endDate, accountIds);
                detail = accountReportMapper.selectDayDetailByDate(startDate, endDate, accountIds);
            }

            if (type != 1) {
                if (!Check.isNull(accountJson)) {
                    BigDecimal cost = accountJson.getBigDecimal("cost");
                    BigDecimal discountCost = cost.divide(discount, 2, RoundingMode.HALF_UP);
                    // 折后消耗
                    accountJson.put("discountCost", discountCost);
                }
            }
            returnJson.put("summary", accountJson);
            returnJson.put("trend", detail);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnJson;
    }

    @Override
    public JSONObject getStatistics(JSONObject json) {
        JSONObject returnJson = new JSONObject();
        try {
            Integer type = json.getInteger("type");
            String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
            String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
            JSONArray accountIds = json.getJSONArray("accountIds");
            BigDecimal discount = json.getBigDecimal("discount");
            if (Check.isNull(accountIds)) {
                return null;
            }

            Map<String, Object> queryMap = new HashMap<>();
            queryMap.put("discount", discount);
            String statDate = "";
            List<JSONObject> accountDetail = new ArrayList<>();
            Integer statHour = json.getInteger("statHour");
            Integer hour;
            if (type == 1) {
                // 今日汇总
                if (Check.isNull(statHour)) {
                    Calendar calendar = Calendar.getInstance();
                    int maxHour = calendar.get(Calendar.HOUR_OF_DAY);
                    hour = maxHour - 1;
                } else {
                    hour = statHour;
                }
                JSONObject chainRatioJson = new JSONObject();
                accountDetail = accountReportMapper.selectReportDetailGroupAccountId(nowDate, accountIds, hour);
                statDate = nowDate;
                JSONObject nowAccountSummary = accountReportMapper.selectSummaryByAccountId(nowDate, accountIds, hour, discount);
                if (!Check.isNull(nowAccountSummary)) {
                    returnJson.put("now", nowAccountSummary);
                    JSONObject yesterdayAccountSummary = accountReportMapper.selectSummaryByAccountId(anotherDay, accountIds, hour, discount);
                    if (!Check.isNull(yesterdayAccountSummary)) {
                        returnJson.put("yesterday", yesterdayAccountSummary);
                        BigDecimal nowCost = nowAccountSummary.getBigDecimal("cost");
                        BigDecimal yesterdayCost = yesterdayAccountSummary.getBigDecimal("cost");
                        // 消耗环比
                        if (!Check.isNull(nowCost) && !Check.isNull(yesterdayCost)) {
                            if (yesterdayCost.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal costProportion = (nowCost.subtract(yesterdayCost)).divide(yesterdayCost, 2, RoundingMode.HALF_UP);
                                chainRatioJson.put("costProportion", costProportion);
                            } else {
                                chainRatioJson.put("costProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("costProportion", 0);
                        }

                        // 今日封面展示
                        Long photoShow = nowAccountSummary.getLong("photoShow");
                        // 昨日封面展示
                        Long yesterdayPhotoShow = yesterdayAccountSummary.getLong("photoShow");
                        // 较昨日增加比例
                        if (!Check.isNull(photoShow) && !Check.isNull(yesterdayPhotoShow)) {
                            if (yesterdayPhotoShow != 0L) {
                                double showProportion = (photoShow.doubleValue() - yesterdayPhotoShow.doubleValue()) / yesterdayPhotoShow.doubleValue();
                                chainRatioJson.put("showProportion", showProportion);
                            } else {
                                chainRatioJson.put("showProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("showProportion", 0);
                        }

                        // 封面点击
                        Long photoClick = nowAccountSummary.getLong("photoClick");
                        Long yesterdayPhotoClick = yesterdayAccountSummary.getLong("photoClick");
                        // 点击环比
                        if (!Check.isNull(photoClick) && !Check.isNull(yesterdayPhotoClick)) {
                            if (yesterdayPhotoClick != 0L) {
                                double photoClickProportion = (photoClick.doubleValue() - yesterdayPhotoClick.doubleValue()) / yesterdayPhotoClick.doubleValue();
                                chainRatioJson.put("photoClickProportion", photoClickProportion);
                            } else {
                                chainRatioJson.put("photoClickProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("photoClickProportion", 0);
                        }

                        // 爆光
                        Long nowAClick = nowAccountSummary.getLong("aclick");
                        Long yesterdayAClick = yesterdayAccountSummary.getLong("aclick");
                        //爆光环比
                        if (!Check.isNull(nowAClick) && !Check.isNull(yesterdayAClick)) {
                            if (yesterdayAClick != 0L) {
                                double aClickProportion = (nowAClick.doubleValue() - yesterdayAClick.doubleValue()) / yesterdayAClick.doubleValue();
                                chainRatioJson.put("aClickProportion", aClickProportion);
                            } else {
                                chainRatioJson.put("aClickProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("aClickProportion", 0);
                        }

                        // 封面点击率
                        BigDecimal nowPhotoClickRatio = nowAccountSummary.getBigDecimal("photoClickRatio");
                        BigDecimal yesterdayPhotoClickRatio = yesterdayAccountSummary.getBigDecimal("photoClickRatio");
                        // 封面点击率环比
                        if (!Check.isNull(nowPhotoClickRatio) && !Check.isNull(yesterdayPhotoClickRatio)) {
                            if (yesterdayPhotoClickRatio.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal photoClickRatioProportion = (nowPhotoClickRatio.subtract(yesterdayPhotoClickRatio)).divide(yesterdayPhotoClickRatio, 2, RoundingMode.HALF_UP);
                                chainRatioJson.put("photoClickRatioProportion", photoClickRatioProportion);
                            } else {
                                chainRatioJson.put("photoClickRatioProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("photoClickRatioProportion", 0);
                        }

                        // cvr
                        BigDecimal nowCvr = nowAccountSummary.getBigDecimal("cvr");
                        BigDecimal yesterdayCvr = yesterdayAccountSummary.getBigDecimal("cvr");
                        // cvr环比
                        if (!Check.isNull(nowCvr) && !Check.isNull(yesterdayCvr)) {
                            if (yesterdayCvr.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal cvrProportion = (nowCvr.subtract(yesterdayCvr)).divide(yesterdayCvr, 2, RoundingMode.HALF_UP);
                                chainRatioJson.put("cvrProportion", cvrProportion);
                            } else {
                                chainRatioJson.put("cvrProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("cvrProportion", 0);
                        }

                        // cpm
                        BigDecimal nowCpm = nowAccountSummary.getBigDecimal("cpm");
                        BigDecimal yesterdayCpm = yesterdayAccountSummary.getBigDecimal("cpm");
                        // cpm环比
                        if (!Check.isNull(nowCpm) && !Check.isNull(yesterdayCpm)) {
                            if (yesterdayCpm.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal cpmProportion = (nowCpm.subtract(yesterdayCpm)).divide(yesterdayCpm, 2, RoundingMode.HALF_UP);
                                chainRatioJson.put("cpmProportion", cpmProportion);
                            } else {
                                chainRatioJson.put("cpmProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("cpmProportion", 0);
                        }

                        // 行为数
                        Long nowBClick = nowAccountSummary.getLong("bClick");
                        Long yesterdayBClick = yesterdayAccountSummary.getLong("bClick");
                        //行为数环比
                        if (!Check.isNull(nowBClick) && !Check.isNull(yesterdayBClick)) {
                            if (yesterdayAClick != 0L) {
                                double bClickProportion = (nowBClick.doubleValue() - yesterdayBClick.doubleValue()) / yesterdayBClick.doubleValue();
                                chainRatioJson.put("bClickProportion", bClickProportion);
                            } else {
                                chainRatioJson.put("bClickProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("bClickProportion", 0);
                        }

                        // 转化数
                        Long nowConversionNum = nowAccountSummary.getLong("conversionNum");
                        Long yesterdayConversionNum = yesterdayAccountSummary.getLong("conversionNum");
                        //表单提交数环比
                        if (!Check.isNull(nowConversionNum) && !Check.isNull(yesterdayConversionNum)) {
                            if (yesterdayAClick != 0L) {
                                double nowConversionNumProportion = (nowConversionNum.doubleValue() - yesterdayConversionNum.doubleValue()) / yesterdayConversionNum.doubleValue();
                                chainRatioJson.put("nowConversionNumProportion", nowConversionNumProportion);
                            } else {
                                chainRatioJson.put("nowConversionNumProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("nowConversionNumProportion", 0);
                        }


                        // 折后转化单价
                        BigDecimal nowAfterConversionPrice = nowAccountSummary.getBigDecimal("afterConversionPrice");
                        BigDecimal yesterdayAfterConversionPrice = yesterdayAccountSummary.getBigDecimal("afterConversionPrice");
                        // 折前消耗单价
                        if (!Check.isNull(nowAfterConversionPrice) && !Check.isNull(yesterdayAfterConversionPrice)) {
                            if (yesterdayAfterConversionPrice.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal afterConversionPriceProportion = (nowAfterConversionPrice.subtract(yesterdayAfterConversionPrice)).divide(yesterdayAfterConversionPrice, 2, RoundingMode.HALF_UP);
                                chainRatioJson.put("afterConversionPriceProportion", afterConversionPriceProportion);
                            } else {
                                chainRatioJson.put("afterConversionPriceProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("afterConversionPriceProportion", 0);
                        }

                        // 折前转化单价
                        BigDecimal nowConversionPrice = nowAccountSummary.getBigDecimal("conversionPrice");
                        BigDecimal yesterdayConversionPrice = yesterdayAccountSummary.getBigDecimal("conversionPrice");
                        // 折前消耗单价
                        if (!Check.isNull(nowConversionPrice) && !Check.isNull(yesterdayConversionPrice)) {
                            if (yesterdayConversionPrice.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal beforeConversionPriceProportion = (nowConversionPrice.subtract(yesterdayConversionPrice)).divide(yesterdayConversionPrice, 2, RoundingMode.HALF_UP);
                                chainRatioJson.put("beforeConversionPriceProportion", beforeConversionPriceProportion);
                            } else {
                                chainRatioJson.put("beforeConversionPriceProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("beforeConversionPriceProportion", 0);
                        }


                        // 表单提交数
                        Long nowFormCount = nowAccountSummary.getLong("formCount");
                        Long yesterdayFormCount = yesterdayAccountSummary.getLong("formCount");
                        //表单提交数环比
                        if (!Check.isNull(nowFormCount) && !Check.isNull(yesterdayFormCount)) {
                            if (yesterdayAClick != 0L) {
                                double formCountProportion = (nowFormCount.doubleValue() - yesterdayFormCount.doubleValue()) / yesterdayFormCount.doubleValue();
                                chainRatioJson.put("formCountProportion", formCountProportion);
                            } else {
                                chainRatioJson.put("formCountProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("formCountProportion", 0);
                        }


                        // 激活数
                        Long nowActivationCount = nowAccountSummary.getLong("activationCount");
                        Long yesterdayActivationCount = yesterdayAccountSummary.getLong("activationCount");
                        //激活数环比
                        if (!Check.isNull(nowActivationCount) && !Check.isNull(yesterdayActivationCount)) {
                            if (yesterdayAClick != 0L) {
                                double activationCountProportion = (nowActivationCount.doubleValue() - yesterdayActivationCount.doubleValue()) / yesterdayActivationCount.doubleValue();
                                chainRatioJson.put("activationCountProportion", activationCountProportion);
                            } else {
                                chainRatioJson.put("activationCountProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("activationCountProportion", 0);
                        }

                        // 表单提交单价
                        BigDecimal nowFormPrice = nowAccountSummary.getBigDecimal("formPrice");
                        BigDecimal yesterdayFormPrice = yesterdayAccountSummary.getBigDecimal("formPrice");
                        // 折前消耗单价
                        if (!Check.isNull(nowFormPrice) && !Check.isNull(yesterdayFormPrice)) {
                            if (yesterdayFormPrice.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal formPriceProportion = (nowFormPrice.subtract(yesterdayFormPrice)).divide(yesterdayFormPrice, 2, RoundingMode.HALF_UP);
                                chainRatioJson.put("beforeFormPriceProportion", formPriceProportion);
                            } else {
                                chainRatioJson.put("beforeFormPriceProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("beforeFormPriceProportion", 0);
                        }


                        // 表单提交单价
                        BigDecimal nowAfterFormPrice = nowAccountSummary.getBigDecimal("afterFormPrice");
                        BigDecimal yesterdayAfterFormPrice = yesterdayAccountSummary.getBigDecimal("afterFormPrice");
                        // 折后提交单价
                        if (!Check.isNull(nowAfterFormPrice) && !Check.isNull(yesterdayAfterFormPrice)) {
                            if (yesterdayAfterFormPrice.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal afterFormPriceProportion = (nowAfterFormPrice.subtract(yesterdayAfterFormPrice)).divide(yesterdayAfterFormPrice, 2, RoundingMode.HALF_UP);
                                chainRatioJson.put("afterFormPriceProportion", afterFormPriceProportion);
                            } else {
                                chainRatioJson.put("afterFormPriceProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("afterFormPriceProportion", 0);
                        }


                        // 转化提交单价
                        BigDecimal nowActivationPrice = nowAccountSummary.getBigDecimal("activationPrice");
                        BigDecimal yesterdayActivationPrice = yesterdayAccountSummary.getBigDecimal("activationPrice");
                        // 折前激活单价
                        if (!Check.isNull(nowActivationPrice) && !Check.isNull(yesterdayActivationPrice)) {
                            if (yesterdayActivationPrice.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal activationPriceProportion = (nowActivationPrice.subtract(yesterdayActivationPrice)).divide(yesterdayActivationPrice, 2, RoundingMode.HALF_UP);
                                chainRatioJson.put("beforeActivationPriceProportion", activationPriceProportion);
                            } else {
                                chainRatioJson.put("beforeActivationPriceProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("beforeActivationPriceProportion", 0);
                        }

                        // 转化提交单价
                        BigDecimal nowAfterActivationPrice = nowAccountSummary.getBigDecimal("afterActivationPrice");
                        BigDecimal yesterdayAfterActivationPrice = yesterdayAccountSummary.getBigDecimal("afterActivationPrice");
                        // 折后激活单价
                        if (!Check.isNull(nowAfterActivationPrice) && !Check.isNull(yesterdayAfterActivationPrice)) {
                            if (yesterdayAfterActivationPrice.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal activationPriceProportion = (nowAfterActivationPrice.subtract(yesterdayAfterActivationPrice)).divide(yesterdayAfterActivationPrice, 2, RoundingMode.HALF_UP);
                                chainRatioJson.put("afterActivationPriceProportion", activationPriceProportion);
                            } else {
                                chainRatioJson.put("afterActivationPriceProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("afterActivationPriceProportion", 0);
                        }


                        BigDecimal nowEventRegisterCost = nowAccountSummary.getBigDecimal("eventRegisterCost");  // 今日注册成本
                        BigDecimal yesterdayEventRegisterCost = yesterdayAccountSummary.getBigDecimal("eventRegisterCost"); // 昨日注册成本
                        if (!Check.isNull(nowEventRegisterCost) && !Check.isNull(yesterdayEventRegisterCost)) {
                            if (yesterdayEventRegisterCost.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal eventRegisterCostProportion = (nowEventRegisterCost.subtract(yesterdayEventRegisterCost)).divide(yesterdayEventRegisterCost, 4, RoundingMode.HALF_UP);
                                chainRatioJson.put("eventRegisterCostProportion", eventRegisterCostProportion);
                            } else {
                                chainRatioJson.put("eventRegisterCostProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("eventRegisterCostProportion", 0);
                        }


                        BigDecimal nowEventRegister = nowAccountSummary.getBigDecimal("eventRegister");  // 今日注册数
                        BigDecimal yesterdayEventRegister = yesterdayAccountSummary.getBigDecimal("eventRegister"); // 昨日注册数
                        if (!Check.isNull(nowEventRegister) && !Check.isNull(yesterdayEventRegister)) {
                            if (yesterdayEventRegister.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal eventRegisterProportion = (nowEventRegister.subtract(yesterdayEventRegister)).divide(yesterdayEventRegister, 4, RoundingMode.HALF_UP);
                                chainRatioJson.put("eventRegisterProportion", eventRegisterProportion);
                            } else {
                                chainRatioJson.put("eventRegisterProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("eventRegisterProportion", 0);
                        }

                        //次留数
                        Long nowEventNextDayStay = nowAccountSummary.getLong("activationCount");
                        Long yesterdayEventNextDayStay = yesterdayAccountSummary.getLong("activationCount");
                        //表单提交数环比
                        if (!Check.isNull(nowEventNextDayStay) && !Check.isNull(yesterdayEventNextDayStay)) {
                            if (yesterdayAClick != 0L) {
                                double eventNextDayStayProportion = (nowEventNextDayStay.doubleValue() - yesterdayEventNextDayStay.doubleValue()) / yesterdayEventNextDayStay.doubleValue();
                                chainRatioJson.put("eventNextDayStayProportion", eventNextDayStayProportion);
                            } else {
                                chainRatioJson.put("eventNextDayStayProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("eventNextDayStayProportion", 0);
                        }


                        BigDecimal nowEventNextDayStayRatio = nowAccountSummary.getBigDecimal("eventRegister");  // 今日注册数
                        BigDecimal yesterdayEventNextDayStayRatio = yesterdayAccountSummary.getBigDecimal("eventRegister"); // 昨日注册数
                        if (!Check.isNull(nowEventNextDayStayRatio) && !Check.isNull(yesterdayEventNextDayStayRatio)) {
                            if (yesterdayEventNextDayStayRatio.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal eventNextDayStayRatioProportion = (nowEventNextDayStayRatio.subtract(yesterdayEventNextDayStayRatio)).divide(yesterdayEventNextDayStayRatio, 4, RoundingMode.HALF_UP);
                                chainRatioJson.put("eventNextDayStayRatioProportion", eventNextDayStayRatioProportion);
                            } else {
                                chainRatioJson.put("eventNextDayStayRatioProportion", 0);
                            }
                        } else {
                            chainRatioJson.put("eventNextDayStayRatioProportion", 0);
                        }

                        returnJson.put("chainRatio", chainRatioJson);
                    }

                }
            } else if (type == 2) {
                queryMap.put("statDate", anotherDay);
                queryMap.put("accountIds", accountIds);
                JSONObject ratioJson;
                if (Check.isNull(statHour)) {
                    accountDetail = accountReportMapper.selectReportDetailGroupAccountIdByDate(anotherDay, accountIds);
                    statDate = anotherDay;
                    ratioJson = accountReportMapper.selectSummaryByMap(queryMap);

                } else {
                    accountDetail = accountReportMapper.selectReportDetailGroupAccountId(anotherDay, accountIds, statHour);
                    queryMap.put("statHour", statHour);
                    statDate = anotherDay;
                    ratioJson = accountReportMapper.selectHourSummaryByMap(queryMap);
                }

                returnJson.put("ratioJson", ratioJson);
            } else if (type == 3) {
                String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", anotherDay, -6);
                accountDetail = accountReportMapper.selectAccountReportByDate(anotherDay, endDate, accountIds, discount);
                statDate = endDate + " ~ " + anotherDay;


                queryMap.put("startDate", endDate);
                queryMap.put("endDate", anotherDay);
                queryMap.put("accountIds", accountIds);


            } else if (type == 4) {
                String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", anotherDay, -14);
                accountDetail = accountReportMapper.selectAccountReportByDate(anotherDay, endDate, accountIds, discount);
                statDate = endDate + " ~ " + anotherDay;
                queryMap.put("startDate", endDate);
                queryMap.put("endDate", anotherDay);
                queryMap.put("accountIds", accountIds);

            } else if (type == 5) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -1);
                accountDetail = accountReportMapper.selectAccountReportByDate(anotherDay, endDate, accountIds, discount);
                statDate = endDate + " ~ " + anotherDay;
                queryMap.put("startDate", endDate);
                queryMap.put("endDate", anotherDay);
                queryMap.put("accountIds", accountIds);
            } else if (type == 6) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -3);
                accountDetail = accountReportMapper.selectAccountReportByDate(anotherDay, endDate, accountIds, discount);
                statDate = endDate + " ~ " + anotherDay;
                queryMap.put("startDate", endDate);
                queryMap.put("endDate", anotherDay);
                queryMap.put("accountIds", accountIds);
            } else if (type == 7) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -6);
                accountDetail = accountReportMapper.selectAccountReportByDate(anotherDay, endDate, accountIds, discount);
                statDate = endDate + " ~ " + anotherDay;
                queryMap.put("startDate", endDate);
                queryMap.put("endDate", anotherDay);
                queryMap.put("accountIds", accountIds);
            } else if (type == 8) {
                accountDetail = accountReportMapper.selectAccountReport(accountIds, discount);
                statDate = "合计";
                queryMap.put("accountIds", accountIds);
            } else if (type == 9) {
                String startDate = json.getString("startDate");
                String endDate = json.getString("endDate");
                accountDetail = accountReportMapper.selectAccountReportByDate(startDate, endDate, accountIds, discount);
                statDate = endDate + " ~ " + startDate;
                queryMap.put("startDate", endDate);
                queryMap.put("endDate", startDate);
                queryMap.put("accountIds", accountIds);
            }

            if (type != 1 && type != 2) {
                JSONObject ratioJson = accountReportMapper.selectSummaryByMap(queryMap);
                returnJson.put("ratioJson", ratioJson);
            }

            returnJson.put("statDate", statDate);
            returnJson.put("accountDetail", accountDetail);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnJson;
    }
}
