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.JSONArray;
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");
        List<Long>projectIds = null;
        List<String>accountNames = null;
        JSONArray accountNameArray = data.getJSONArray("accountName");
        JSONArray projectIdArray = data.getJSONArray("projectId");
        if(null!=accountNameArray&&!accountNameArray.isEmpty()){
            accountNames = new ArrayList<>();
            for(int i=0;i<accountNameArray.size();i++){
                accountNames.add(accountNameArray.getString(i));
            }
        }
        if(null!=projectIdArray&&!projectIdArray.isEmpty()){
            projectIds = new ArrayList<>();
            for(int i=0;i<projectIdArray.size();i++){
                projectIds.add(projectIdArray.getLong(i));
            }
        }
        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,projectIds,accountNames);
            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,List<Long>projectIds,List<String>  accountNames) {
        JSONObject data = pangolinReportAppHourMapper.getComparedDate(appId,hour,userId,type,today, yesterday,sevenDaysAgo,compareTime1,compareTime2,projectIds,accountNames);
        if(null==data|| "null".equals(data.toJSONString())){
            data = new JSONObject();
            data.put("today",0);
            data.put("yesterday",0);
            data.put("sevendaysAgo",0);
            data.put("hour",hour);
            if(null!=compareTime1&&!"".equals(compareTime1.trim())){
                data.put(compareTime1,0);
            }
            if(null!=compareTime2&&!"".equals(compareTime2.trim())){
                data.put(compareTime2,0);
            }
        }
        return data;
    }
}
