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.PangolinReportChannelDaily;
import cn.com.ctop.crawler.modules.pangolin.mapper.PangolinReportChannelDailyMapper;
import cn.com.ctop.crawler.modules.pangolin.service.PangolinReportChannelDailyService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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 PangolinReportChannelDailyServiceImpl extends ServiceImpl<PangolinReportChannelDailyMapper, PangolinReportChannelDaily> implements PangolinReportChannelDailyService {
    @Autowired
    private PangolinReportChannelDailyMapper pangolinReportChannelDailyMapper;

    @Override
    public Map<String, Object>  getActivationList(String userId, JSONObject data) {
        Map<String, Object> result = new HashMap<>();
        Integer pageNo = data.getInteger("pageNo");
        if(null == pageNo||pageNo == 0){
            pageNo = 1;
        }
        Integer pageSize = data.getInteger("pageSize");
        if(null == pageSize||pageSize == 0){
            pageSize = 10;
        }
        String operateSystem = data.getString("operateSystem");
        if(null!=operateSystem&& "all".equals(operateSystem.trim())){
            operateSystem = null;
        }
        Integer appId = data.getInteger("appId");
        if(null == appId||appId == 0){
            appId = null;
        }
        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));
            }
        }
        String startDate = data.getString("startDate");
        String endDate = data.getString("endDate");
        PageHelper.startPage(pageNo,pageSize);
        List<PangolinReportChannelDaily> dailyList = pangolinReportChannelDailyMapper.getActivationList(appId,startDate, endDate, userId, operateSystem,projectIds,accountNames);
        PageInfo<PangolinReportChannelDaily> pageInfo = new PageInfo<>(dailyList);
        result.put("data", pageInfo);
        Integer totalActive = pangolinReportChannelDailyMapper.sumAppList(appId,startDate,endDate,userId,operateSystem,projectIds,accountNames);
        Long totalDays = 1L;
        if(!startDate.equals(endDate)){
            totalDays = DateUtils.getDiscrepantDays(startDate,endDate)+1;
        }
        if(null == totalActive){
            result.put("totalActive","-");
            result.put("avgActive","-");
        }else if(0 == totalActive){
            result.put("totalActive","0");
            result.put("avgActive","0");
        }else{
            result.put("totalActive",totalActive+"");
            result.put("avgActive",totalActive/totalDays+"");
        }
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }
}
