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

import cn.com.ctop.toutiao.modules.report.mapper.BytedanceHomepageMapper;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceHomepageReportService;
import cn.com.ctop.toutiao.modules.report.utils.LinkUtils;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.constant.SystemDateConstant;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Created by JQ.bi on 2020.05.19
 */
@Service
public class BytedanceHomepageReportServiceImpl implements IBytedanceHomepageReportService {

    @Autowired
    BytedanceHomepageMapper bytedanceHomepageMapper;

    @Override
    public Map<String, Object> getWeekReport(JSONObject requestBody) {
        Map<String, Object> result = new HashMap<>();
        //当天日期
        String nowDate = DateUtils.getNowDate(SystemDateConstant.yyyy_MM_dd);
        //本周汇总数据
        JSONObject weekSumData = bytedanceHomepageMapper.querySumByStartEndDate(nowDate,
                DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, nowDate, -7));
        //上周汇总数据
        JSONObject weekBeforeSumData = bytedanceHomepageMapper.querySumByStartEndDate(DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, nowDate, -7),
                DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, nowDate, -14));
        //一周内每日数据汇总
        List<JSONObject> weekDetailData = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, nowDate, -7));
        //一周内cost top数据
        List<JSONObject> costTop = bytedanceHomepageMapper.queryTopCost(nowDate, DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, nowDate, -7), 7);
        JSONObject top = new JSONObject();
        top.put("top7", costTop);
        //周明细环比数据拼接
        List<Map<String,Object>> costWeekData = new ArrayList<>();
        JSONObject costWeekLink = new JSONObject();
        List<Map<String,Object>> clickWeekData = new ArrayList<>();
        JSONObject clickWeekLink = new JSONObject();
        List<Map<String,Object>> showNumWeekData = new ArrayList<>();
        JSONObject showNumWeekLink = new JSONObject();
        weekDetailData.forEach(day -> {
            Map<String,Object> cost= new HashMap<>();
            Map<String,Object> click= new HashMap<>();
            Map<String,Object> showNum= new HashMap<>();
            cost.put("statDate",day.get("statDate"));
            cost.put("cost",day.get("cost"));
            click.put("statDate",day.get("statDate"));
            click.put("click",day.get("click"));
            showNum.put("statDate",day.get("statDate"));
            showNum.put("showNum",day.get("showNum"));
            costWeekData.add(cost);
            clickWeekData.add(click);
            showNumWeekData.add(showNum);
        });
        costWeekLink.put("weekCostSum", weekSumData.get("cost"));
        costWeekLink.put("weekCostSumLink", LinkUtils.countLink(weekSumData.getBigDecimal("cost"), weekBeforeSumData.getBigDecimal("cost")));
        clickWeekLink.put("weekClickSum", weekSumData.get("click"));
        clickWeekLink.put("weekClickSumLink", LinkUtils.countLink(BigDecimal.valueOf(weekSumData.getLongValue("click")), BigDecimal.valueOf(weekBeforeSumData.getLong("click"))));
        showNumWeekLink.put("weekShowNumSum", weekSumData.get("showNum"));
        showNumWeekLink.put("weekShowNumSumLink", LinkUtils.countLink(BigDecimal.valueOf(weekSumData.getLongValue("showNum")), BigDecimal.valueOf(weekBeforeSumData.getLong("showNum"))));

        result.put("costWeek", costWeekData);
        result.put("costWeekLink", costWeekLink);
        result.put("clickWeek", clickWeekData);
        result.put("clickWeekLink", clickWeekLink);
        result.put("showNumWeek", showNumWeekData);
        result.put("showNumWeekLink", showNumWeekLink);
        result.put("top", top);

        return result;
    }

    /**
     * timeType :1,2,3,4,5,6,7,8,9  前端约束入参类型
     */
    @Override
    public Map<String,List<JSONObject>> getChartByDate(JSONObject requestBody) {
        int timeType = requestBody.getInteger("type");
        Map<String,List<JSONObject>> resultBody=new HashMap<>();
        if (requestBody.isEmpty()) {
            return resultBody;
        }
        List<JSONObject> resultChart = new ArrayList<>();
        List<JSONObject> resultList = new ArrayList<>();
        //当天日期
        String nowDate = DateUtils.getNowDate(SystemDateConstant.yyyy_MM_dd);
        //startTime 为取什么时间段数据的开始时间，默认昨天
        String anotherDay = DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, nowDate, -1);
        switch (timeType) {
            //今天
            case 1:
                resultChart = getCompareGroupByHour(nowDate, anotherDay);
                resultList = getFormDetailByDate(nowDate,anotherDay,"hour");
                break;
            //昨天
            case 2:
                resultChart = getCompareGroupByHour(anotherDay, DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -1));
                resultList = getFormDetailByDate(anotherDay,DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -1),"hour");
                break;
            //近一周
            case 3:
                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                        DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -6));
                resultList = getFormDetailByDate(nowDate, DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -6),"day");
                break;
            //近半个月（15天）
            case 4:
                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                        DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -14));
                resultList = getFormDetailByDate(nowDate, DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -14),"day");
                break;
            //近一个月
            case 5:
                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                        DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -1));
                resultList = getFormDetailByDate(nowDate, DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -1),"day");
                break;
            //近三个月
            case 6:
                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                        DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -3));
                resultList = getFormDetailByDate(nowDate, DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -3),"day");
                break;
            //近半年
            case 7:
                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                        DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -6));
                resultList = getFormDetailByDate(nowDate, DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -6),"day");
                break;
            //近一年
            case 8:
                resultChart = bytedanceHomepageMapper.queryDetailGroupMonthByDate( nowDate, SystemDateConstant.BEGIN_TIME);
                resultList = getFormDetailByDate(nowDate, SystemDateConstant.BEGIN_TIME,"day");
                break;
            //自定义起始时间
            case 9:
                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(requestBody.getString("endDate"), requestBody.getString("startDate"));
                resultList = getFormDetailByDate(requestBody.getString("endDate"), requestBody.getString("startDate"),"day");
                break;
            default:
        }
        resultBody.put("chart",resultChart);
        resultBody.put("list",resultList);
        return resultBody;
    }

    @Override
    public Map<String, JSONObject> getCompareByDate(JSONObject requestBody) {
        Map<String,JSONObject> resultBody=new HashMap<>();
        if (requestBody.isEmpty()) {
            return resultBody;
        }
        String sign= requestBody.getString("sign");
        String smallDate= requestBody.getString("smallDate");
        String bigDate= requestBody.getString("bigDate");
        resultBody.put("chart",this.getCompareChart(sign,smallDate,bigDate));
        resultBody.put("list",this.getCompareList(sign,smallDate,bigDate));

        return resultBody;
    }


    //表格数据查询
    private List<JSONObject> getFormDetailByDate(String endDate, String startDate, String group){
        List<JSONObject> result ;
        if("day".equals(group)){
            result= getListLink(bytedanceHomepageMapper.queryDetailByStartEndDate(endDate,startDate));
        }else {
            List<JSONObject> after=bytedanceHomepageMapper.queryTodayDetailReportBy(endDate);
            List<JSONObject> before=bytedanceHomepageMapper.queryTodayDetailReportBy(startDate);
            result=LinkUtils.getCompareDate(after,before,"statHour");
        }
        return result;
    }

    //自定义日期对比（日对比，月对比）
    private JSONObject getCompareChart(String sign, String smallDate, String bigDate) {
        JSONObject result = new JSONObject();
        if (!sign.isEmpty() && "month".equals(sign)) {
            result.put("compare",this.getCompareGroupByDay(smallDate, bigDate));
        } else if (!sign.isEmpty() && "day".equals(sign)) {
            result.put("compare",this.getCompareGroupByHour(bigDate, smallDate));
        }
        return result;
    }
    private JSONObject getCompareList(String sign, String smallDate, String bigDate){
        JSONObject result = new JSONObject();
        if (!sign.isEmpty() && "month".equals(sign)) {
            result.put("smallDate",getListLink(bytedanceHomepageMapper.queryDetailGroupDayByMonth(smallDate)));
            result.put("bigDate",getListLink(bytedanceHomepageMapper.queryDetailGroupDayByMonth(bigDate)));
        } else if (!sign.isEmpty() && "day".equals(sign)) {
            result.put("smallDate",getListLink(bytedanceHomepageMapper.queryTodayDetailReportBy(smallDate)));
            result.put("bigDate",getListLink(bytedanceHomepageMapper.queryTodayDetailReportBy(bigDate)));
        }
        return result;
    }
    //List环比计算
    private List<JSONObject> getListLink(List<JSONObject> result){
        if(result.size()>1){
            for(int i=1;i<result.size();i++){
                if(i==1){result.get(i).put("costProportion",BigDecimal.ZERO);}
                result.get(i).put("costProportion",LinkUtils.countLink((BigDecimal) result.get(i).get("cost"),(BigDecimal) result.get(i-1).get("cost")));
            }
        }
        return result;
    }

    //两个月份的数据对比
    private List<JSONObject> getCompareGroupByDay(String smallDate, String bigDate) {
        //大月份数据
        List<JSONObject> afterDetail = bytedanceHomepageMapper.queryDetailGroupDayByMonth(bigDate);
        //小月份数据
        List<JSONObject> beforeDetail = bytedanceHomepageMapper.queryDetailGroupDayByMonth(smallDate);

        return LinkUtils.getCompareDate(beforeDetail,afterDetail,"day");
    }

    //今天昨天数据对比
    private List<JSONObject> getCompareGroupByHour(String nowDate, String anotherDay) {
        List<JSONObject> afterDetail = bytedanceHomepageMapper.queryTodayDetailReportBy(nowDate);
        List<JSONObject> beforeDetail = bytedanceHomepageMapper.queryTodayDetailReportBy(anotherDay);
        return LinkUtils.getCompareDate(beforeDetail, afterDetail, "statHour");
    }

}
