package com.pqs9000.serviceimpl.monitorMap;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.mapper.configuration.DicDataMapper;
import com.njcn.mapper.user.DeptsMapper;
import com.njcn.pojo.configuration.DicData;
import com.njcn.pojo.user.Depts;
import com.njcn.shiro.token.TokenManager;
import com.njcn.sso.pojo.user.User;
import com.njcn.utils.UserUtil;
import com.njcn.utils.Utils;
import com.pqs9000.mapper.monitorMap.MonitorScopeMapper;
import com.pqs9000.pojo.monitorMap.AreaMonitorBaseInfo;
import com.pqs9000.pojo.monitorMap.AreaSubLineVO;
import com.pqs9000.service.monitorMap.MonitorScopeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * system
 *
 * @author cdf
 * @date 2023/7/11
 */
@Service
public class MonitorScopeServiceImpl implements MonitorScopeService {

    @Resource
    private UserUtil userUtil;
    @Resource
    private MonitorScopeMapper monitorScopeMapper;
    @Resource
    private DicDataMapper dicDataMapper;

    @Resource
    private DeptsMapper deptsMapper;

    @Override
    public List<AreaSubLineVO> getMonitorScope(Date startTime, Date endTime) {
        List<AreaSubLineVO> resultList= new ArrayList<>();
        List<Integer> lineIds = userUtil.listLine();
        List<List<Integer>> lineTmpList;
        if(lineIds.size()>1000){
            lineTmpList = Utils.splitList(lineIds,1000);
            assert lineTmpList != null;
            for(List<Integer> item:lineTmpList){
                List<AreaSubLineVO>  result = monitorScopeMapper.getMonitorScope(item,startTime,endTime);
                if(CollectionUtil.isNotEmpty(result)){
                    resultList.addAll(result);
                }
            }
        }else {
            resultList = monitorScopeMapper.getMonitorScope(lineIds,startTime,endTime);
        }
        return resultList;
    }

    @Override
    public AreaMonitorBaseInfo getAreaLineEventCount(Date startTime, Date endTime) {
        List<Integer> lineIds = userUtil.listLineRun();
        int allCount = 0,allEventCount = 0;
        List<List<Integer>> lineTmpList;
        if(lineIds.size()>1000){
            lineTmpList = Utils.splitList(lineIds,1000);
            assert lineTmpList != null;
            for(List<Integer> item:lineTmpList){
                int temCount = monitorScopeMapper.getAreaLineEventCount(item);
                allCount+=temCount;

                int temEventCount = monitorScopeMapper.getEventCount(item,startTime,endTime);
                allEventCount+=temEventCount;
            }
        }else {
            allCount = monitorScopeMapper.getAreaLineEventCount(lineIds);
            allEventCount = monitorScopeMapper.getEventCount(lineIds,startTime,endTime);
        }
        AreaMonitorBaseInfo areaMonitorBaseInfo = new AreaMonitorBaseInfo();
        areaMonitorBaseInfo.setMonitorAllCount(lineIds.size());
        areaMonitorBaseInfo.setEventCount(allEventCount);
        areaMonitorBaseInfo.setOnlineMonitorCount(allCount);

        User user =new User();
        user.setUserIndex(TokenManager.getUserId());
        Depts depts = userUtil.getDeptsByUser(user);
        if(Objects.nonNull(depts)){
           DicData dicData = dicDataMapper.selectId(depts.getArea());
           if(Objects.nonNull(dicData)) {
               areaMonitorBaseInfo.setDeptName(dicData.getDicName());
           }
        }
        return areaMonitorBaseInfo;
    }
}
