package cn.com.ctop.crawler.modules.pangolin.service.impl;

import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.crawler.modules.pangolin.entity.PangolinReportAppHour;
import cn.com.ctop.crawler.modules.pangolin.mapper.PangolinReportAppHourMapper;
import cn.com.ctop.crawler.modules.pangolin.service.PangolinReportAppHourService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class PangolinReportAppHourServiceImpl extends ServiceImpl<PangolinReportAppHourMapper, PangolinReportAppHour> implements PangolinReportAppHourService {
    @Autowired
    private PangolinReportAppHourMapper pangolinReportAppHourMapper;

    @Override
    public Map<String, Object> getRealTimeData(String userId, JSONObject data) {

        Map<String, Object> resultMap = new HashMap<>();
        String today = DateUtils.formatDate();
        String yesterday = DateUtils.addDay(today,-1);
        String sevenDaysAgo = DateUtils.addDay(today,-7);
        String type = data.getString("type");
        String compareTime1 = data.getString("compareTime1");
        String compareTime2 = data.getString("compareTime2");
        Integer appId = data.getInteger("appId");
        if(null == appId||appId == 0){
            appId = null;
        }
        List<JSONObject> dataList = new ArrayList<>();
        for(int i=0;i<24;i++){
            JSONObject getData = this.getComparedDate(appId,i,userId,type,today,yesterday,sevenDaysAgo,compareTime1,compareTime2);
            dataList.add(getData);
        }
        resultMap.put("data",dataList);
        ResultMapUtils.setResultMap(resultMap, StatusCode.COMMON_SUCCESS);
        return resultMap;
    }

    private JSONObject getComparedDate(Integer appId,Integer hour,String userId,String type,String today, String yesterday, String sevenDaysAgo, String compareTime1, String compareTime2) {
        return pangolinReportAppHourMapper.getComparedDate(appId,hour,userId,type,today, yesterday,sevenDaysAgo,compareTime1,compareTime2);
    }
}
