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

import cn.com.ctop.kuaishou.modules.report.mapper.KuaiShouYiCheReportMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaiShouYiCheReportService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class KuaiShouYiCheReportServiceImpl implements IKuaiShouYiCheReportService {
    @Resource
    private KuaiShouYiCheReportMapper kuaiShouYiCheReportMapper;

    @Override
    public JSONObject getHourReport(JSONObject requestJson) {
        log.info("易车时报数据统计开始：requestJson:{}", requestJson);
        Long projectId = requestJson.getLong("projectId");
        BigDecimal disCount = requestJson.getBigDecimal("disCount");
        String statDate = requestJson.getString("statDate");

        JSONObject returnJson = new JSONObject();
        Calendar calendar = Calendar.getInstance();
        int maxHour = calendar.get(Calendar.HOUR_OF_DAY);

        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("projectId", projectId);
        requestMap.put("disCount", disCount);
        requestMap.put("statDate", statDate);
        requestMap.put("statHour", maxHour);

        String yesterday = DateUtils.getAnotherDay("yyyy-MM-dd", statDate, -1);
        String weekAgoDay = DateUtils.getAnotherDay("yyyy-MM-dd", statDate, -6);

        // 安卓
        requestMap.put("systemType", "android");
        // 账号
        List<JSONObject> androidReportArr = kuaiShouYiCheReportMapper.selectReportByMap(requestMap);

        // 今日
        requestMap.put("statDate", statDate);
        requestMap.put("dateDesc", statDate + "整体数据");
        JSONObject nowJson = kuaiShouYiCheReportMapper.selectSumReportByMap(requestMap);
        androidReportArr.add(nowJson);

        // 昨日
        requestMap.put("statDate", yesterday);
        requestMap.put("dateDesc", "昨日整体数据");
        JSONObject yesterdayJson = kuaiShouYiCheReportMapper.selectSumReportByMap(requestMap);
        androidReportArr.add(yesterdayJson);
        // 七日
        requestMap.put("statDate", weekAgoDay);
        requestMap.put("dateDesc", "上周同期整体数据");

        JSONObject weekAgoJson = kuaiShouYiCheReportMapper.selectSumReportByMap(requestMap);
        androidReportArr.add(weekAgoJson);


        // ios
        requestMap.put("systemType", "ios");
        requestMap.put("statDate", statDate);
        // 账号
        List<JSONObject> iosReportArr = kuaiShouYiCheReportMapper.selectReportByMap(requestMap);

        // 今日
        requestMap.put("dateDesc", statDate + "整体数据");
        JSONObject iosNowJson = kuaiShouYiCheReportMapper.selectSumReportByMap(requestMap);
        iosReportArr.add(iosNowJson);

        // 昨日
        requestMap.put("statDate", yesterday);
        requestMap.put("dateDesc", "昨日整体数据");
        JSONObject iosYesterdayJson = kuaiShouYiCheReportMapper.selectSumReportByMap(requestMap);
        iosReportArr.add(iosYesterdayJson);
        // 七日
        requestMap.put("statDate", weekAgoDay);
        requestMap.put("dateDesc", "上周同期整体数据");
        JSONObject iosWeekAgoJson = kuaiShouYiCheReportMapper.selectSumReportByMap(requestMap);
        iosReportArr.add(iosWeekAgoJson);

        returnJson.put("android", androidReportArr);
        returnJson.put("ios", iosReportArr);
        return returnJson;
    }

    @Override
    public JSONObject getDailyReport(JSONObject requestJson) {

        JSONObject returnJson = new JSONObject();
        try {
            String statDate = requestJson.getString("statDate");
            BigDecimal disCount = requestJson.getBigDecimal("disCount");
            Long projectId = requestJson.getLong("projectId");
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put("disCount", disCount);
            requestMap.put("projectId", projectId);
            requestMap.put("statDate", statDate);
            requestMap.put("systemType", "ios");
            JSONObject iosReport = kuaiShouYiCheReportMapper.getDailyReportByMap(requestMap);
            requestMap.put("systemType", "android");
            JSONObject androidReport = kuaiShouYiCheReportMapper.getDailyReportByMap(requestMap);
            returnJson.put("ios", iosReport);
            returnJson.put("android", androidReport);
            returnJson.remove("systemType");
            JSONObject directApplicationReport = kuaiShouYiCheReportMapper.getDirectApplicationReportByMap(requestMap);
            // 应用直达
            returnJson.put("directApplication", directApplicationReport);
            List<JSONObject> channelSubcontractingReport = kuaiShouYiCheReportMapper.getChannelSubcontractingReportByMap(requestMap);
            // 渠道包分析
            returnJson.put("channelSubcontracting", channelSubcontractingReport);

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

        }
        return returnJson;
    }
}
