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

import cn.com.ctop.bytedance.entity.BytedanceAdvertiserDailyReport;
import cn.com.ctop.bytedance.mapper.BytedanceAdvertiserDailyReportMapper;
import cn.com.ctop.bytedance.mapper.BytedanceReportMapper;
import cn.com.ctop.bytedance.service.IBytedanceReportService;
import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.mapper.UserAllocationMapper;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAccount;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyAccountMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BytedanceReportServiceImpl implements IBytedanceReportService {
    @Autowired
    private UserAllocationMapper userAllocationMapper;

    @Autowired
    private BytedanceAdvertiserDailyReportMapper bytedanceAdvertiserDailyReportMapper;

    @Autowired
    private BytedanceReportMapper reportMapper;

    @Autowired
    private KuaishouReportDailyAccountMapper kuaishouReportDailyAccountMapper;

    /**
     * 媒体昨日总消耗
     *
     * @param loginId
     * @return
     */
    @Override
    public JSONObject getAccountReport(String loginId) {
        JSONObject returnJson = new JSONObject();
        QueryWrapper<UserAllocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginId);
        queryWrapper.isNotNull("account_id");
        queryWrapper.groupBy("account_id");
        List<UserAllocation> userAllocations = userAllocationMapper.selectList(queryWrapper);
        if (!Check.isNull(userAllocations)) {
            // 头条相关
            JSONObject bytedanceJson = new JSONObject();
            BigDecimal byteDanceCost = new BigDecimal(0);
            Long byteDanceClick = 0L;
            Long byteDanceShow = 0L;
            Long byteDanceConvert = 0L;
            Long byteDanceForm = 0L;
            Long byteDanceNextDayOpen = 0L;
            Long byteDanceActive = 0L;
            JSONArray bytedanceDetail = new JSONArray();

            // 快手相关
            JSONArray kuaishouDetail = new JSONArray();
            JSONObject kuaishouJson = new JSONObject();
            BigDecimal kuaishouCost = new BigDecimal(0);

            Long kuaishouClick = 0L;
            Long kuaishouShow = 0L;
            Long kuaishouConvert = 0L;
            Long kuaishouForm = 0L;
            Long kuaishouNextDayOpen = 0L;
            Long kuaishouActive = 0L;


            for (UserAllocation userAllocation : userAllocations) {
                if ("1".equals(userAllocation.getMediaId())) {
                    QueryWrapper<BytedanceAdvertiserDailyReport> bytedanceAdvertiserDailyReportQueryWrapper = new QueryWrapper<>();
                    bytedanceAdvertiserDailyReportQueryWrapper.eq("advertiser_id", userAllocation.getAccountId());
                    bytedanceAdvertiserDailyReportQueryWrapper.between("stat_datetime", "2019-07-01 00:00:00", "2019-07-01 23:59:59");
                    bytedanceAdvertiserDailyReportQueryWrapper.orderByDesc("create_time");
                    bytedanceAdvertiserDailyReportQueryWrapper.last("limit 1");
                    BytedanceAdvertiserDailyReport bytedanceAdvertiserDailyReport = bytedanceAdvertiserDailyReportMapper.selectOne(bytedanceAdvertiserDailyReportQueryWrapper);
                    if (!Check.isNull(bytedanceAdvertiserDailyReport)) {
                        JSONObject bytedanceDetailJson = new JSONObject();
                        BigDecimal cost = bytedanceAdvertiserDailyReport.getCost();
                        // 账号总消耗
                        bytedanceDetailJson.put("cost", cost);
                        byteDanceCost = byteDanceCost.add(cost);
                        Long click = bytedanceAdvertiserDailyReport.getClick();
                        // 账号总点击
                        bytedanceDetailJson.put("click", click);
                        byteDanceClick += click;
                        Long showNum = bytedanceAdvertiserDailyReport.getShowNum();
                        // 账号总展示
                        bytedanceDetailJson.put("show", showNum);
                        byteDanceShow += showNum;
                        Long convertNum = bytedanceAdvertiserDailyReport.getConvertNum();
                        // 账号总转化
                        bytedanceDetailJson.put("convert", convertNum);
                        byteDanceConvert += convertNum;
                        Long form = bytedanceAdvertiserDailyReport.getForm();
                        // 账号总表单提交
                        bytedanceDetailJson.put("form", form);
                        byteDanceForm += form;
                        Long nextDayOpen = bytedanceAdvertiserDailyReport.getNextDayOpen();
                        // 账号总次日留存
                        bytedanceDetailJson.put("nextDayOpen", nextDayOpen);
                        byteDanceNextDayOpen += nextDayOpen;
                        Long active = bytedanceAdvertiserDailyReport.getActive();
                        // 账号总激活
                        bytedanceDetailJson.put("active", active);
                        byteDanceActive += active;
                        // 账号
                        bytedanceDetailJson.put("accountId", userAllocation.getAccountId());
                        // 账号 名称
                        bytedanceDetailJson.put("accountName", userAllocation.getAuthName());
                        bytedanceDetail.add(bytedanceDetailJson);
                    }

                } else if ("2".equals(userAllocation.getMediaId())) {
                    // 查询快手相关报表数据
                    QueryWrapper<KuaishouReportDailyAccount> kuaishouReportDailyAccountQueryWrapper = new QueryWrapper<>();
                    kuaishouReportDailyAccountQueryWrapper.eq("account_id", userAllocation.getAccountId());
                    kuaishouReportDailyAccountQueryWrapper.between("stat_date", "2019-10-08 00:00:00", "2019-10-08 23:59:59");
                    kuaishouReportDailyAccountQueryWrapper.orderByDesc("create_time");
                    kuaishouReportDailyAccountQueryWrapper.last("limit 1");
                    KuaishouReportDailyAccount reportDailyAccount = kuaishouReportDailyAccountMapper.selectOne(kuaishouReportDailyAccountQueryWrapper);
                    if (!Check.isNull(reportDailyAccount)) {
                        JSONObject kuaishouDetailJson = new JSONObject();
                        kuaishouDetailJson.put("accountId", userAllocation.getAccountId());
                        kuaishouDetailJson.put("accountName", userAllocation.getAuthName());

                        // 花费
                        BigDecimal cost = reportDailyAccount.getCharge();
                        kuaishouDetailJson.put("cost", cost);
                        kuaishouCost = kuaishouCost.add(cost);

                        Long click = reportDailyAccount.getAclick();
                        kuaishouDetailJson.put("click", click);
                        kuaishouClick += click;

                        Long show = reportDailyAccount.getPhotoShow();
                        kuaishouDetailJson.put("show", show);
                        kuaishouShow += show;

                        Long convert = reportDailyAccount.getBclick();
                        kuaishouDetailJson.put("convert", convert);
                        kuaishouConvert += convert;

                        Long form = reportDailyAccount.getSubmit();
                        kuaishouDetailJson.put("form", form);
                        kuaishouForm += form;


                        Long active = reportDailyAccount.getActivation();
                        kuaishouDetailJson.put("active", active);
                        kuaishouActive += active;

                        kuaishouDetail.add(kuaishouDetailJson);

                    }

                }
            }
            // 总消耗
            bytedanceJson.put("cost", byteDanceCost);
            // 总点击
            bytedanceJson.put("click", byteDanceClick);
            // 总展示
            bytedanceJson.put("show", byteDanceShow);
            // 总转化
            bytedanceJson.put("convert", byteDanceConvert);
            // 总表单提交
            bytedanceJson.put("form", byteDanceForm);
            // 总次留数
            bytedanceJson.put("nextDayOpen", byteDanceNextDayOpen);
            // 总激活
            bytedanceJson.put("active", byteDanceActive);

            returnJson.put("bytedance", bytedanceJson);
            returnJson.put("bytedanceDetail", bytedanceDetail);


            kuaishouJson.put("cost", kuaishouCost);
            kuaishouJson.put("click", kuaishouClick);
            kuaishouJson.put("show", kuaishouShow);
            kuaishouJson.put("convert", kuaishouConvert);
            kuaishouJson.put("form", kuaishouForm);
            kuaishouJson.put("nextDayOpen", 0);
            kuaishouJson.put("active", kuaishouActive);
            returnJson.put("kuaishou", kuaishouJson);
            returnJson.put("kuaishouDetail", kuaishouDetail);
            System.err.println(returnJson);

        }


        return returnJson;
    }

    /**
     * 快手-大盘报表
     *
     * @param requestJson
     * @return
     */
    @Override
    public JSONObject getKuaiShouAccountReport(JSONObject requestJson) {
        Integer type = requestJson.getInteger("type");
        JSONObject accountJson = new JSONObject();
        try {
            String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
            String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
            if (type == 1) {
                // 今日总消耗
                JSONObject nowJson = reportMapper.selectDayReport(nowDate);
                if (Check.isNull(nowJson)) {
                    return accountJson;
                }
                int maxHour = nowJson.getInteger("maxHour");
                if (maxHour == 0 || Check.isNull(maxHour)) {
                    return accountJson;
                }
                String maxHourStr = String.valueOf(maxHour - 1);
                accountJson = reportMapper.selectSumDayHourlyByHourAndDate(nowDate, maxHourStr);
                accountJson.put("maxHour", maxHour);
                if (!Check.isNull(accountJson)) {
                    JSONObject yesterdayJson = reportMapper.selectSumDayHourlyByHourAndDate(anotherDay, maxHourStr);
                    if (!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);
                        }


                        // 今日封面展示
                        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);
                        }
                    }
                }
            } else if (type == 2) {
                accountJson = reportMapper.selectDayReport(anotherDay);
            } else if (type == 3) {
                String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", anotherDay, -6);
                accountJson = reportMapper.selectByDate(anotherDay, endDate);
            } else if (type == 4) {
                String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", anotherDay, -14);
                accountJson = reportMapper.selectByDate(anotherDay, endDate);
            } else if (type == 5) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -1);
                accountJson = reportMapper.selectByDate(anotherDay, endDate);
            } else if (type == 6) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -3);
                accountJson = reportMapper.selectByDate(anotherDay, endDate);
            } else if (type == 7) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -6);
                accountJson = reportMapper.selectByDate(anotherDay, endDate);
            } else if (type == 8) {
                accountJson = reportMapper.selectAllAccount();
            } else if (type == 9) {
                String startDate = requestJson.getString("startDate");
                String endDate = requestJson.getString("endDate");
                accountJson = reportMapper.selectByDate(startDate, endDate);
            } else if (type == 10) {
                String sign = requestJson.getString("sign");
                String bigDate = requestJson.getString("bigDate");
                String smallDate = requestJson.getString("smallDate");
                JSONObject beComparedJson = new JSONObject();
                JSONObject comparedJson = new JSONObject();

                if ("month".equals(sign)) {
                    Map<String, Object> dateMap = DateUtils.compareDate("yyyy-MM", bigDate, smallDate);
                    beComparedJson = reportMapper.selectMonthReportByDate((String) dateMap.get("smallDate"));
                    comparedJson = reportMapper.selectMonthReportByDate((String) dateMap.get("bigDate"));
                } else if ("day".equals(sign)) {
                    Map<String, Object> dateMap = DateUtils.compareDate("yyyy-MM-dd", bigDate, smallDate);
                    beComparedJson = reportMapper.selectDayReportByDate((String) dateMap.get("smallDate"));
                    comparedJson = reportMapper.selectDayReportByDate((String) dateMap.get("bigDate"));
                }

                if (!Check.isNull(comparedJson)) {
                    BigDecimal cost = comparedJson.getBigDecimal("cost");
                    BigDecimal comparedCost = beComparedJson.getBigDecimal("cost");
                    if (comparedCost.compareTo(new BigDecimal(0)) != 0) {
                        BigDecimal costProportion = (cost.subtract(comparedCost)).divide(comparedCost, 2, RoundingMode.HALF_UP);
                        comparedJson.put("costProportion", costProportion);
                    } else {
                        comparedJson.put("costProportion", 0);
                    }

                    Long photoClick = comparedJson.getLong("photoClick");
                    Long comparedPhotoClick = beComparedJson.getLong("photoClick");
                    if (comparedPhotoClick != 0L) {
                        double photoClickProportion = (photoClick.doubleValue() - comparedPhotoClick.doubleValue()) / comparedPhotoClick.doubleValue();
                        comparedJson.put("photoClickProportion", photoClickProportion);
                    } else {
                        comparedJson.put("photoClickProportion", 0);
                    }

                    Long aClick = comparedJson.getLong("aclick");
                    Long comparedAClick = beComparedJson.getLong("aclick");
                    if (comparedAClick != 0L) {
                        double aClickProportion = (aClick.doubleValue() - comparedAClick.doubleValue()) / comparedAClick.doubleValue();
                        comparedJson.put("comparedAclick", aClickProportion);
                    } else {
                        comparedJson.put("comparedAclick", 0);
                    }
                    Long comparedPhotoShow = beComparedJson.getLong("photoShow");
                    Long photoShow = comparedJson.getLong("photoShow");
                    if (comparedPhotoShow != 0L) {
                        double showProportion = (photoShow.doubleValue() - comparedPhotoShow.doubleValue()) / comparedPhotoShow.doubleValue();
                        comparedJson.put("showProportion", showProportion);
                    } else {
                        comparedJson.put("showProportion", 0);
                    }
                }
                accountJson.put("beCompared", beComparedJson);
                accountJson.put("compared", comparedJson);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return accountJson;
    }

    @Override
    public List<JSONObject> getKuaiShouAccounDetailtReport(JSONObject requestJson) {
        Integer type = requestJson.getInteger("type");
        List<JSONObject> detail = new ArrayList<>();
        try {
            String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
            String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
            if (type == 1) {
                //  今日明细
                List<JSONObject> yesterdayDetail = reportMapper.selectReportDetail(anotherDay);
                for (int i = 0; i < yesterdayDetail.size(); i++) {

                    JSONObject yesterdayJson = yesterdayDetail.get(i);
                    if (!Check.isNull(yesterdayJson)) {
                        String yesterdayStatHour = yesterdayJson.getString("statHour");
                        JSONObject nowJson = reportMapper.selectDayHourlyByHourAndDate(nowDate, yesterdayStatHour);
                        if (!Check.isNull(nowJson)) {
                            nowJson.put("statDate", nowDate);
                            // 今日消耗
                            BigDecimal cost = nowJson.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);
                                nowJson.put("costProportion", costProportion);
                            } else {
                                nowJson.put("costProportion", 0);
                            }

                            // 今日封面展示
                            Long photoShow = nowJson.getLong("photoShow");
                            // 昨日封面展示
                            Long yesterdayPhotoShow = yesterdayJson.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 = yesterdayJson.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 = yesterdayJson.getLong("aclick");
                            // 较昨日增加比例
                            if (yesterdayAClick != 0L) {
                                double aClickProportion = (aClick.doubleValue() - yesterdayAClick.doubleValue()) / yesterdayAClick.doubleValue();
                                nowJson.put("aClickProportion", aClickProportion);
                            } else {
                                nowJson.put("aClickProportion", 0);
                            }
                            yesterdayJson.put("now", nowJson);
                        }
                    }
                    detail.add(yesterdayJson);
                }
            } else if (type == 2) {
                detail = reportMapper.selectReportDetail(anotherDay);
            } else if (type == 3) {
                String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", anotherDay, -6);
                detail = reportMapper.selectDayDetailByDate(anotherDay, endDate);
            } else if (type == 4) {
                String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", anotherDay, -14);
                detail = reportMapper.selectDayDetailByDate(anotherDay, endDate);
            } else if (type == 5) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -1);
                detail = reportMapper.selectDayDetailByDate(anotherDay, endDate);
            } else if (type == 6) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -3);
                detail = reportMapper.selectDayDetailByDate(anotherDay, endDate);
            } else if (type == 7) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -6);
                detail = reportMapper.selectDayDetailByMonth(anotherDay, endDate);
            } else if (type == 8) {
                detail = reportMapper.selectAllDayDetailByMonth();
            } else if (type == 9) {
                String startDate = requestJson.getString("startDate");
                String endDate = requestJson.getString("endDate");
                detail = reportMapper.selectDayDetailByDate(startDate, endDate);

            } else if (type == 10) {
                String sign = requestJson.getString("sign");
                String bigDate = requestJson.getString("bigDate");
                String smallDate = requestJson.getString("smallDate");
                List<JSONObject> comparedArr = new ArrayList<>();
                String comparedDate = "";
                if ("month".equals(sign)) {
                    Map<String, Object> dateMap = DateUtils.compareDate("yyyy-MM", bigDate, smallDate);
                    comparedDate = (String) dateMap.get("smallDate");
                    comparedArr = reportMapper.selectDayDetailsByMonth((String) dateMap.get("bigDate"));
                } else if ("day".equals(sign)) {
                    Map<String, Object> dateMap = DateUtils.compareDate("yyyy-MM-dd", bigDate, smallDate);
                    comparedDate = (String) dateMap.get("smallDate");
                    comparedArr = reportMapper.selectHourlyDetailsByDate((String) dateMap.get("bigDate"));
                }

                for (int i = 0; i < comparedArr.size(); i++) {
                    JSONObject comparedJson = comparedArr.get(i);
                    if (!Check.isNull(comparedJson)) {
                        JSONObject beComparedJson = new JSONObject();
                        String statDate = comparedJson.getString("statDate");
                        String beComparedDate = "";
                        String statHour = "";
                        if ("month".equals(sign)) {
                            String day = DateUtils.getDay("yyyy-MM-dd", statDate);
                            beComparedDate = comparedDate + "-" + day;
                            beComparedJson = reportMapper.selectDayDailyByDate(beComparedDate);

                        } else if ("day".equals(sign)) {
                            statHour = comparedJson.getString("statHour");
                            beComparedJson = reportMapper.selectDayHourlyByHourAndDate(comparedDate, statHour);
                            beComparedDate = comparedDate;
                        }

                        if (!Check.isNull(beComparedJson)) {
                            JSONObject json = new JSONObject();
                            BigDecimal cost = comparedJson.getBigDecimal("cost");
                            Long photoShow = comparedJson.getLong("photoShow");
                            Long photoClick = comparedJson.getLong("photoClick");
                            Long aClick = comparedJson.getLong("aclick");
                            json.put("cost", cost);
                            json.put("photoShow", photoShow);
                            json.put("photoClick", photoClick);
                            json.put("aClick", aClick);
                            json.put("statDate", statDate);
                            if ("day".equals(sign)) {
                                json.put("statHour", statHour);
                            }

                            BigDecimal comparedCost = beComparedJson.getBigDecimal("cost");
                            Long comparedPhotoShow = beComparedJson.getLong("photoShow");
                            Long comparedPhotoClick = beComparedJson.getLong("photoClick");
                            Long comparedAClick = beComparedJson.getLong("aclick");
                            json.put("beComparedCost", comparedCost);
                            json.put("beComparedPhotoShow", comparedPhotoShow);
                            json.put("beComparedPhotoClick", comparedPhotoClick);
                            json.put("beComparedAClick", comparedAClick);
                            json.put("beComparedDate", beComparedDate);

                            if (comparedCost.compareTo(new BigDecimal(0)) != 0) {
                                BigDecimal costProportion = (cost.subtract(comparedCost)).divide(comparedCost, 2, RoundingMode.HALF_UP);
                                json.put("costProportion", costProportion);
                            } else {
                                json.put("costProportion", 0);
                            }
                            if (comparedPhotoClick != 0L) {
                                double photoClickProportion = (photoClick.doubleValue() - comparedPhotoClick.doubleValue()) / comparedPhotoClick.doubleValue();
                                json.put("photoClickProportion", photoClickProportion);
                            } else {
                                json.put("photoClickProportion", 0);
                            }
                            if (comparedPhotoShow != 0L) {
                                double showProportion = (photoShow.doubleValue() - comparedPhotoShow.doubleValue()) / comparedPhotoShow.doubleValue();
                                json.put("showProportion", showProportion);
                            } else {
                                json.put("showProportion", 0);
                            }
                            if (comparedAClick != 0L) {
                                double aClickProportion = (aClick.doubleValue() - comparedAClick.doubleValue()) / comparedAClick.doubleValue();
                                json.put("comparedAclick", aClickProportion);
                            } else {
                                json.put("comparedAclick", 0);
                            }
                            detail.add(json);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return detail;
    }


    /**
     * 明细列表
     *
     * @param requestJson
     * @return
     */
    @Override
    public List<JSONObject> getReportList(JSONObject requestJson) {
        List<JSONObject> reportList = new ArrayList<>();
        try {
            Integer type = requestJson.getInteger("type");
            String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
            String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
            if (type == 1) {
                List<JSONObject> nowReportList = reportMapper.selectReportDetail(nowDate);
                List<JSONObject> yesterdayReportList = reportMapper.selectReportDetail(anotherDay);
                JSONObject json = new JSONObject();
                json.put("now", nowReportList);
                json.put("yesterday", yesterdayReportList);
                reportList.add(json);

            } else if (type == 2) {
                reportList = reportMapper.selectReportDetail(anotherDay);
            } else if (type == 3) {
                String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", anotherDay, -6);
                reportList = reportMapper.selectDayDetailByDate(anotherDay, endDate);
            } else if (type == 4) {
                String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", anotherDay, -14);
                reportList = reportMapper.selectDayDetailByDate(anotherDay, endDate);
            } else if (type == 5) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -1);
                reportList = reportMapper.selectDayDetailByDate(anotherDay, endDate);
            } else if (type == 6) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -3);
                reportList = reportMapper.selectDayDetailByDate(anotherDay, endDate);
            } else if (type == 7) {
                String endDate = DateUtils.getMonthBefore("yyyy-MM-dd", anotherDay, -6);
                reportList = reportMapper.selectDayDetailByMonth(anotherDay, endDate);
            } else if (type == 8) {
                reportList = reportMapper.selectAllDayDetailByMonth();
            } else if (type == 9) {
                String startDate = requestJson.getString("startDate");
                String endDate = requestJson.getString("endDate");
                reportList = reportMapper.selectDayDetailByDate(startDate, endDate);
            } else if (type == 10) {
                String sign = requestJson.getString("sign");
                String bigDate = requestJson.getString("bigDate");
                String smallDate = requestJson.getString("smallDate");
                Map<String, Object> dateMap = new HashMap<>();
                if ("month".equals(sign)) {
                    dateMap = DateUtils.compareDate("yyyy-MM", bigDate, smallDate);
                } else if ("day".equals(sign)) {
                    dateMap = DateUtils.compareDate("yyyy-MM-dd", bigDate, smallDate);
                }
                String comparedDate = (String) dateMap.get("bigDate");
                String beComparedDate = (String) dateMap.get("smallDate");
                JSONObject detailJson = new JSONObject();
                if ("month".equals(sign)) {
                    List<JSONObject> comparedArr = reportMapper.selectDayDetailsByMonth(beComparedDate);
                    detailJson.put("beStatDate", beComparedDate);
                    detailJson.put("beComparedDetail", comparedArr);
                    List<JSONObject> beComparedArr = reportMapper.selectDayDetailsByMonth(comparedDate);
                    detailJson.put("statDate", comparedDate);
                    detailJson.put("comparedDetail", beComparedArr);
                } else if ("day".equals(sign)) {
                    List<JSONObject> comparedArr = reportMapper.selectHourlyDetailsByDate(comparedDate);
                    detailJson.put("statDate", comparedDate);
                    detailJson.put("comparedDetail", comparedArr);
                    List<JSONObject> beComparedArr = reportMapper.selectHourlyDetailsByDate(beComparedDate);
                    detailJson.put("beStatDate", beComparedDate);
                    detailJson.put("beComparedDetail", beComparedArr);
                }
                reportList.add(detailJson);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reportList;
    }
}
