zhangfeng
2022-10-10 1305d0aa469fe7330fd2f8e9fbb10d95042571be
emergency/emergency-service/src/main/java/com/gkhy/safePlatform/emergency/service/impl/EmergencyCountServiceImpl.java
@@ -11,9 +11,11 @@
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.emergency.entity.*;
import com.gkhy.safePlatform.emergency.enums.DepartmentLevelEnum;
import com.gkhy.safePlatform.emergency.enums.EmergencyResultCodes;
import com.gkhy.safePlatform.emergency.excepiton.EmergencyException;
import com.gkhy.safePlatform.emergency.model.dto.resp.*;
import com.gkhy.safePlatform.emergency.query.EmergencyDrillCountQuery;
import com.gkhy.safePlatform.emergency.query.EmergencyDrillExecuteCountQuery;
import com.gkhy.safePlatform.emergency.query.EmergencySuppliesCountQuery;
import com.gkhy.safePlatform.emergency.rpc.api.model.dto.req.EmergencyExecuteNumRPCReq;
@@ -32,6 +34,7 @@
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@Service("emergencyCountService")
public class EmergencyCountServiceImpl implements EmergencyCountService {
@@ -170,6 +173,8 @@
        }
        return searchResultVO;
    }
    private void getSingleNumData(EmergencyExecuteNumRPCResp emergencyExecuteNumRPCResp,EmergencyExecuteNumRPCReq query){
        List<Long> deptIds = new ArrayList<>();
@@ -337,5 +342,277 @@
                ResultCodes.OK
        );
    }
    /**********************************重构****************************************************/
    /**
     * 查询部门最后一次演练时间
     * @param deptId
     * @return
     */
    @Override
    public EmergencyExecuteTimeRespDTO getLastTimeByDeptId(Long deptId) {
        if(null == deptId){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL);
        }
        //通过RPC获取部门相关数据
        DepInfoRPCRespDTO depInfoRPCRespDTO = getDepInfoByDepId(deptId);
        // 查询该部门最后一次演练时间
        String lastTime = emergencyDrillExecuteInfoService.getLastTimeByDeptId(deptId);
        //填充数据
        EmergencyExecuteTimeRespDTO respDTO = new EmergencyExecuteTimeRespDTO();
        respDTO.setDepId(depInfoRPCRespDTO.getDepId());
        respDTO.setDepLevel(depInfoRPCRespDTO.getDepLevel());
        respDTO.setLastPricticeTime(StringUtils.isBlank(lastTime) == true ? "" : lastTime);
        return respDTO;
    }
    /**
     * 查询部门及其子级部门最后一次演练时间
     * 后面按部门等级划分
     */
    public EmergencyExecuteTimeRespDTO  getLastTimeByDeptIds(Long deptId){
        if(null == deptId){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL);
        }
        //通过RPC获取部门相关数据
        List<DepInfoRPCRespDTO> depInfoList = getDepListInfoByDepId(deptId);
        //过滤获取部门id
        List<Long> deptIdList = depInfoList
                .stream()
                .map(dep -> dep.getDepId())
                .collect(Collectors.toList());
        //过滤出传入部门的信息
        List<DepInfoRPCRespDTO> selectDepList = depInfoList
                .stream()
                .filter(dep -> dep.getDepId().equals(deptId))
                .collect(Collectors.toList());
        // 查询该部门ids最后一次演练时间
        String lastTime = emergencyDrillExecuteInfoService.getLastTimeByDeptIds(deptIdList);
        //填充数据
        EmergencyExecuteTimeRespDTO respDTO = new EmergencyExecuteTimeRespDTO();
        respDTO.setDepId(deptId);
        respDTO.setLastPricticeTime(StringUtils.isBlank(lastTime) == true ? "" : lastTime);
        if(selectDepList.size()>0){
            respDTO.setDepLevel(selectDepList.get(0).getDepLevel());
        }
        return respDTO;
    }
    /**
     * 根据部门id和指定年份 统计演练数据
     *
     */
    public StatisticsDepLevelYearExecuteRespDTO getCountByDeptIdAndYear(EmergencyDrillCountQuery query){
        if(null == query.getDeptId()){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL,"部门id不可为空");
        }
        if(null == query.getYear()){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL,"年份不可为空");
        }
        //通过RPC获取部门相关数据
        DepInfoRPCRespDTO depInfoRPCRespDTO = getDepInfoByDepId(query.getDeptId());
        //填充数据
        StatisticsDepLevelYearExecuteRespDTO respDTO = new StatisticsDepLevelYearExecuteRespDTO();
        respDTO.setDepLevel(depInfoRPCRespDTO.getDepLevel());
        respDTO.setYear(query.getYear());
        //获取指定部门指定时间下的应急数据
        List<EmergencyExecuteCountDO> executeCountDOList  = emergencyDrillExecuteInfoService.getCountByDeptIdAndYear(query.getYear(),query.getDeptId());
        //声明月份
        int allMonth = 12;
        //判断是否是当前年分
//        if(query.getYear().equals(TimeUtils.getCurrentYear())){
//            allMonth = TimeUtils.getCurrentMonth();
//        }
        //总数量
        int totalCount = 0;
        List<StatisticsMonthExecuteCountRespDTO> executeCountRespDTOList = new ArrayList<>();
        //循环月份
        for(int i = 1;i <= allMonth; i++){
            StatisticsMonthExecuteCountRespDTO executeCountRespDTO = new StatisticsMonthExecuteCountRespDTO();
            executeCountRespDTO.setYear(query.getYear());
            executeCountRespDTO.setMonth(i);
            //获取当月数据
            int month = i;
            List<EmergencyExecuteCountDO> selectMonthList = executeCountDOList
                    .stream()
                    .filter(ac -> ac.getMonth().equals(month))
                    .collect(Collectors.toList());
            if(selectMonthList.size()>0){
                executeCountRespDTO.setCount(selectMonthList.get(0).getCount());
                totalCount += selectMonthList.get(0).getCount();
            }else{
                executeCountRespDTO.setCount(0);
            }
            executeCountRespDTOList.add(executeCountRespDTO);
        }
        respDTO.setYearTotalCount(totalCount);
        respDTO.setMonthExecuteCountList(executeCountRespDTOList);
        return respDTO;
    }
    /**
     * 根据指定部门、月份 统计演练数据
     * @param query
     * @return
     */
    @Override
    public StatisticsDepLevelMonthEexcuteRespDTO getCountByDeptIdAndMonth(EmergencyDrillCountQuery query) {
        if(null == query.getDeptId()){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL,"部门id不可为空");
        }
        if(null == query.getYear()){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL,"年份不可为空");
        }
        if(null == query.getMonth()){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL,"月份不可为空");
        }
        //通过RPC获取部门相关数据
        DepInfoRPCRespDTO depInfoRPCRespDTO = getDepInfoByDepId(query.getDeptId());
        //获取指定月份的演练数据
        EmergencyExecuteCountDO executeCountDO  = emergencyDrillExecuteInfoService.getCountByDeptIdAndMonth(query.getYear(),query.getMonth(), query.getDeptId());
        //填充数据
        StatisticsDepLevelMonthEexcuteRespDTO respDTO = new StatisticsDepLevelMonthEexcuteRespDTO();
        respDTO.setDepLevel(depInfoRPCRespDTO.getDepLevel());
        respDTO.setMonth(query.getMonth());
        respDTO.setExecuteCount(executeCountDO.getCount());
        return respDTO;
    }
    /**
     * 根据部门id及其子级部门和指定年份 统计演练数据
     *
     */
    public List<StatisticsDepLevelYearExecuteRespDTO> getCountByDeptIdsAndYear(EmergencyDrillCountQuery query){
        if(null == query.getDeptId()){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL,"部门id不可为空");
        }
        if(null == query.getYear()){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL,"年份不可为空");
        }
        //通过RPC获取部门相关数据
        List<DepInfoRPCRespDTO> depInfoList = getDepListInfoByDepId(query.getDeptId());
        //过滤获取部门id
        List<Long> deptIds = depInfoList
                .stream()
                .map(dep -> dep.getDepId())
                .collect(Collectors.toList());
        //开始时间结束时间
        String startTime = TimeUtils.getYearFirst(query.getYear());
        String endTime = TimeUtils.getYearLast(query.getYear());
        //获取指定部门指定时间下的应急数据
        List<EmergencyExecuteCountDO> executeCountDOList  = emergencyDrillExecuteInfoService.getCountByDeptIdsAndTime(startTime,endTime,deptIds);
        List<StatisticsDepLevelYearExecuteRespDTO> respDTOList = new ArrayList<>();
        //循环部门等级
        for(DepartmentLevelEnum departmentLevelEnum : DepartmentLevelEnum.values()){
            //填充部门级别数据
            StatisticsDepLevelYearExecuteRespDTO respDTO = new StatisticsDepLevelYearExecuteRespDTO();
            respDTO.setDepLevel(departmentLevelEnum.getCode());
            respDTO.setYear(query.getYear());
            //获取该部门级别下面的相关部门
            List<DepInfoRPCRespDTO> selectDepList = depInfoList
                    .stream()
                    .filter(dep -> dep.getDepLevel().equals(departmentLevelEnum.getCode()))
                    .collect(Collectors.toList());
            //获取该部门级别下相关部门演练数据
            List<EmergencyExecuteCountDO> selectExeList = executeCountDOList
                    .stream()
                    .filter(exe -> selectDepList.stream().map(dep -> dep.getDepId()).collect(Collectors.toList()).contains(exe.getDeptId()))
                    .collect(Collectors.toList());
            List<StatisticsMonthExecuteCountRespDTO> monthExecuteCountList = new ArrayList<>();
            //声明月份
            int allMonth = 12;
            //声明一年总数量
            int yearTotalCount =0;
            //判断是否是当前年分
//            if(query.getYear().equals(TimeUtils.getCurrentYear())){
//                allMonth = TimeUtils.getCurrentMonth();
//            }
            //12个月份
            for(int i = 1;i <= allMonth;i++){
                //数量
                int count  = 0;
                //循环
                for(EmergencyExecuteCountDO executeCountDO : selectExeList){
                    if(executeCountDO.getMonth() == i){
                        count += executeCountDO.getCount();
                        yearTotalCount += executeCountDO.getCount();
                    }
                }
                StatisticsMonthExecuteCountRespDTO executeCountRespDTO = new StatisticsMonthExecuteCountRespDTO();
                executeCountRespDTO.setYear(query.getYear());
                executeCountRespDTO.setMonth(i);
                executeCountRespDTO.setCount(count);
                monthExecuteCountList.add(executeCountRespDTO);
            }
            respDTO.setMonthExecuteCountList(monthExecuteCountList);
            respDTO.setYearTotalCount(yearTotalCount);
            respDTOList.add(respDTO);
        }
        return respDTOList;
    }
    /**
     * 根据部门id及其子级部门和指定者月份 统计演练数据
     *
     */
    @Override
    public List<StatisticsDepLevelMonthEexcuteRespDTO> getCountByDeptIdsAndMonth(EmergencyDrillCountQuery query) {
        if(null == query.getDeptId()){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL,"部门id不可为空");
        }
        if(null == query.getYear()){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL,"年份不可为空");
        }
        if(null == query.getMonth()){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PARAM_NULL,"月份不可为空");
        }
        //通过RPC获取部门相关数据
        List<DepInfoRPCRespDTO> depInfoList = getDepListInfoByDepId(query.getDeptId());
        //过滤获取部门id
        List<Long> deptIds = depInfoList
                .stream()
                .map(dep -> dep.getDepId())
                .collect(Collectors.toList());
        //开始时间结束时间
        String startTime = TimeUtils.getMonthFirst(query.getYear(),query.getMonth());
        String endTime = TimeUtils.getMonthLast(query.getYear(),query.getMonth());
        //获取演练数据
        List<EmergencyExecuteCountDO> executeCountDOList  = emergencyDrillExecuteInfoService.getCountByDeptIdsAndTime(startTime,endTime,deptIds);
        List<StatisticsDepLevelMonthEexcuteRespDTO> respDTOList = new ArrayList<>();
        //循环部门等级
        for(DepartmentLevelEnum departmentLevelEnum : DepartmentLevelEnum.values()) {
            //填充部门级别数据
            StatisticsDepLevelMonthEexcuteRespDTO respDTO = new StatisticsDepLevelMonthEexcuteRespDTO();
            respDTO.setDepLevel(departmentLevelEnum.getCode());
            respDTO.setMonth(query.getMonth());
            //获取该部门级别下面的相关部门
            List<DepInfoRPCRespDTO> selectDepList = depInfoList
                    .stream()
                    .filter(dep -> dep.getDepLevel().equals(departmentLevelEnum.getCode()))
                    .collect(Collectors.toList());
            //获取该部门级别下相关部门演练数据
            List<EmergencyExecuteCountDO> selectExeList = executeCountDOList
                    .stream()
                    .filter(exe -> selectDepList.stream().map(dep -> dep.getDepId()).collect(Collectors.toList()).contains(exe.getDeptId()))
                    .collect(Collectors.toList());
            //数量
            int count  = 0;
            //循环
            for(EmergencyExecuteCountDO executeCountDO : selectExeList){
                count += executeCountDO.getCount();
            }
            respDTO.setExecuteCount(count);
            respDTOList.add(respDTO);
        }
        return respDTOList;
    }
}