package com.gkhy.safePlatform.riskCtrl.service.impl; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService; import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.exception.BusinessException; import com.gkhy.safePlatform.commons.utils.idService.IdGenerateUtil; import com.gkhy.safePlatform.commons.vo.ResultVO; import com.gkhy.safePlatform.commons.vo.SearchResultVO; import com.gkhy.safePlatform.riskCtrl.entity.EmergencyPracticeReportItem; import com.gkhy.safePlatform.riskCtrl.entity.IncidentMonthReportItem; import com.gkhy.safePlatform.riskCtrl.entity.PreventMonthReportDetail; import com.gkhy.safePlatform.riskCtrl.entity.RiskMonthReport; import com.gkhy.safePlatform.riskCtrl.enums.ReportBizEnum; import com.gkhy.safePlatform.riskCtrl.model.bo.EmergencyDataBO; import com.gkhy.safePlatform.riskCtrl.model.bo.IncidentDataBO; import com.gkhy.safePlatform.riskCtrl.model.bo.RiskDataBO; import com.gkhy.safePlatform.riskCtrl.model.dto.resp.EmergencyPracticeReportItemRespDTO; import com.gkhy.safePlatform.riskCtrl.model.dto.resp.IncidentMonthReportItemRespDTO; import com.gkhy.safePlatform.riskCtrl.model.dto.resp.PreventMonthReportDetailRespDTO; import com.gkhy.safePlatform.riskCtrl.model.dto.resp.RiskMonthReportRespDTO; import com.gkhy.safePlatform.riskCtrl.service.RiskReportService; import com.gkhy.safePlatform.riskCtrl.service.baseService.EmergencyPracticeReportItemService; import com.gkhy.safePlatform.riskCtrl.service.baseService.IncidentMonthReportItemService; import com.gkhy.safePlatform.riskCtrl.service.baseService.PreventMonthReportDetailService; import com.gkhy.safePlatform.riskCtrl.service.baseService.RiskMonthReportService; import com.gkhy.safePlatform.riskCtrl.utils.RiskReportAnalysisHandler; import org.apache.dubbo.config.annotation.DubboReference; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit; @Service public class RiskReportServiceImpl implements RiskReportService { @DubboReference(check = false) private AccountDepartmentService departmentService; @Autowired private CollectReportDataService collectReportDataService; @Autowired private RiskMonthReportService riskMonthReportService; @Autowired private PreventMonthReportDetailService preventMonthReportDetailService; @Autowired private EmergencyPracticeReportItemService emergencyPracticeReportItemService; @Autowired private IncidentMonthReportItemService incidentMonthReportItemService; @Autowired private RedissonClient redissonClient; /** * 生成企业月度风险报表 * @param depId * @param year * @param month * @return */ @Transactional @Override public ResultVO generateDepartmentMonthRiskReport(Long depId, Integer year, Integer month) { ResultVO resultVO = new ResultVO<>(); //step1、检查该月报表是否已经生成 RiskMonthReport prevReport = riskMonthReportService.findByMonth(ReportBizEnum.PREVENT_RISK.getType(),depId,year, month); RiskMonthReport incidentReport = riskMonthReportService.findByMonth(ReportBizEnum.INCIDENT_COUNT.getType(),depId, year, month); RiskMonthReport emgcReport = riskMonthReportService.findByMonth(ReportBizEnum.EMERGENCY_PRACTICE.getType(),depId, year, month); if(prevReport != null || incidentReport != null || emgcReport != null){ resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("月度报表已存在"); return resultVO; } //step2、获取各系统数据 if(year == null || year < 0 || year > 9999){ resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg(ResultCodes.CLIENT_PARAM_ERROR.getDesc()); return resultVO; } if(month != null && (month < 0 || month > 12)){ resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg(ResultCodes.CLIENT_PARAM_ERROR.getDesc()); return resultVO; } ResultVO depInfoRpcResult = departmentService.getDepInfoByDepId(depId); if(!depInfoRpcResult.getCode().equals(ResultCodes.OK.getCode())){ throw new RuntimeException("RPC调用出错"); } DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO) depInfoRpcResult.getData(); if(depInfo == null){ throw new RuntimeException("部门不存在"); } RiskDataBO riskDataBO = collectReportDataService.getPreventRiskData(depInfo,year,month); IncidentDataBO incidentDataBO = collectReportDataService.getIncidentCountData(depInfo,year,month); EmergencyDataBO emergencyDataBO = collectReportDataService.getEmergencyCountData(depInfo,year,month); //step3、处理数据 //3.1、报表POJO RiskMonthReport riskMonthReport = new RiskMonthReport(); IdGenerateUtil idGenerateUtil = new IdGenerateUtil(); riskMonthReport.setId(idGenerateUtil.nextId()); riskMonthReport.setDepId(depId); riskMonthReport.setYear(year); riskMonthReport.setMonth(month); riskMonthReport.setDepLevel(depInfo.getDepLevel()); LocalDateTime beginTime = LocalDateTime.of(year,month,1,0,0,0); DateTime dt = DateUtil.beginOfMonth(Date.from(beginTime.atZone(ZoneId.systemDefault()).toInstant())); riskMonthReport.setBeginDate(dt.toLocalDateTime().toLocalDate()); dt = DateUtil.endOfMonth(Date.from(beginTime.atZone(ZoneId.systemDefault()).toInstant())); riskMonthReport.setEndDate(dt.toLocalDateTime().toLocalDate()); riskMonthReport.setGmtCreate(LocalDateTime.now()); RiskReportAnalysisHandler riskReportAnalysisHandler = new RiskReportAnalysisHandler(); //3.2、双重预防报表POJO PreventMonthReportDetail preventMonthReportDetail = riskReportAnalysisHandler.analysisPrevReportData(riskDataBO); preventMonthReportDetail.setReportId(riskMonthReport.getId()); //3.3、应急管理报表POJO List emergencyReportItemList = riskReportAnalysisHandler.analysisEmgcReportData(emergencyDataBO); //3.4、事故管理报表POJO List incidentMonthReportItemList = riskReportAnalysisHandler.analysisIncidentReportDate(incidentDataBO); //step4、保存入库 if(riskMonthReportService.save(riskMonthReport) == false){ throw new BusinessException(ResultCodes.BUSINESS_ERROR); } if(preventMonthReportDetail != null){ if(preventMonthReportDetailService.save(preventMonthReportDetail) == false){ throw new BusinessException(ResultCodes.BUSINESS_ERROR); } } if(emergencyReportItemList != null && !emergencyReportItemList.isEmpty()){ for(EmergencyPracticeReportItem item : emergencyReportItemList){ item.setId(idGenerateUtil.nextId()); item.setReportId(riskMonthReport.getId()); } if(emergencyPracticeReportItemService.saveBatch(emergencyReportItemList) == false){ throw new BusinessException(ResultCodes.BUSINESS_ERROR); } } if(incidentMonthReportItemList != null && !incidentMonthReportItemList.isEmpty()){ for(IncidentMonthReportItem i : incidentMonthReportItemList){ i.setId(idGenerateUtil.nextId()); i.setReportId(riskMonthReport.getId()); } if(incidentMonthReportItemService.saveBatch(incidentMonthReportItemList) == false){ throw new BusinessException(ResultCodes.BUSINESS_ERROR); } } resultVO.setCode(ResultCodes.OK.getCode()); return resultVO; } @Transactional @Override public ResultVO generateDepartmentMonthRiskReport(Byte biz, Long depId, Integer year, Integer month) { ResultVO resultVO = new ResultVO<>(); //1、参数校验 if(biz == null || ReportBizEnum.parse(biz) == null){ resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("业务类型不支持"); return resultVO; } if(depId == null || depId < 1 || year == null || year < 2000 || year > 9999 || month == null || month < 1 || month > 12){ resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("参数错误"); return resultVO; } //加分布式锁 String lockName = "LOCK_RISK_MONTH_REPORT_GENERATE_"+depId+"_"+biz; RLock lock = redissonClient.getLock(lockName); lock.lock(30L, TimeUnit.SECONDS); //2、检查报表是否已经存在 RiskMonthReport report = riskMonthReportService.findByMonth(biz,depId,year,month); if(report != null && report.getId() > 0){ lock.unlock(); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("报表已经生成"); return resultVO; } //3、校验部门是否存在 ResultVO depInfoRpcResult = departmentService.getDepInfoByDepId(depId); if(!depInfoRpcResult.getCode().equals(ResultCodes.OK.getCode())){ lock.unlock(); throw new RuntimeException("RPC调用出错"); } DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO) depInfoRpcResult.getData(); if(depInfo == null){ lock.unlock(); throw new RuntimeException("部门不存在"); } //4、生成报表项 RiskMonthReport riskMonthReport = new RiskMonthReport(); IdGenerateUtil idGenerateUtil = new IdGenerateUtil(); riskMonthReport.setId(idGenerateUtil.nextId()); riskMonthReport.setDepId(depId); riskMonthReport.setBiz(biz); riskMonthReport.setYear(year); riskMonthReport.setMonth(month); riskMonthReport.setDepLevel(depInfo.getDepLevel()); LocalDateTime beginTime = LocalDateTime.of(year,month,1,0,0,0); DateTime dt = DateUtil.beginOfMonth(Date.from(beginTime.atZone(ZoneId.systemDefault()).toInstant())); riskMonthReport.setBeginDate(dt.toLocalDateTime().toLocalDate()); dt = DateUtil.endOfMonth(Date.from(beginTime.atZone(ZoneId.systemDefault()).toInstant())); riskMonthReport.setEndDate(dt.toLocalDateTime().toLocalDate()); riskMonthReport.setGmtCreate(LocalDateTime.now()); if(riskMonthReportService.save(riskMonthReport) == false){ lock.unlock(); throw new BusinessException(ResultCodes.BUSINESS_ERROR); } RiskReportAnalysisHandler riskReportAnalysisHandler = new RiskReportAnalysisHandler(); //5、获取对应业务系统的数据,生成业务报表 if(biz.equals(ReportBizEnum.PREVENT_RISK.getType())){ //双重预防 RiskDataBO riskDataBO = collectReportDataService.getPreventRiskData(depInfo,year,month); PreventMonthReportDetail preventMonthReportDetail = riskReportAnalysisHandler.analysisPrevReportData(riskDataBO); preventMonthReportDetail.setReportId(riskMonthReport.getId()); if(preventMonthReportDetail != null){ if(preventMonthReportDetailService.save(preventMonthReportDetail) == false){ lock.unlock(); throw new BusinessException(ResultCodes.BUSINESS_ERROR); } } }else if(biz.equals(ReportBizEnum.EMERGENCY_PRACTICE.getType())){ //应急管理 EmergencyDataBO emergencyDataBO = collectReportDataService.getEmergencyCountData(depInfo,year,month); List emergencyReportItemList = riskReportAnalysisHandler.analysisEmgcReportData(emergencyDataBO); if(emergencyReportItemList != null && !emergencyReportItemList.isEmpty()){ for(EmergencyPracticeReportItem item : emergencyReportItemList){ item.setId(idGenerateUtil.nextId()); item.setReportId(riskMonthReport.getId()); } if(emergencyPracticeReportItemService.saveBatch(emergencyReportItemList) == false){ lock.unlock(); throw new BusinessException(ResultCodes.BUSINESS_ERROR); } } }else if(biz.equals(ReportBizEnum.INCIDENT_COUNT.getType())){ //事故管理 IncidentDataBO incidentDataBO = collectReportDataService.getIncidentCountData(depInfo,year,month); List incidentMonthReportItemList = riskReportAnalysisHandler.analysisIncidentReportDate(incidentDataBO); if(incidentMonthReportItemList != null && !incidentMonthReportItemList.isEmpty()){ for(IncidentMonthReportItem i : incidentMonthReportItemList){ i.setId(idGenerateUtil.nextId()); i.setReportId(riskMonthReport.getId()); } if(incidentMonthReportItemService.saveBatch(incidentMonthReportItemList) == false){ lock.unlock(); throw new BusinessException(ResultCodes.BUSINESS_ERROR); } } } lock.unlock(); resultVO.setCode(ResultCodes.OK.getCode()); return resultVO; } /** * 获取指定部门指定月份指定业务的月报 * @param depId * @param biz * @param year * @param month * @return */ @Override public SearchResultVO getReportByMonth(Long depId, Byte biz, Integer year, Integer month) { SearchResultVO resultVO = new SearchResultVO<>(); resultVO.setUsePage(false); //1、参数校验 if(depId == null || depId < 1 || biz == null || year == null || month == null || year < 2000 || year > 9999 || month < 1|| month > 12){ resultVO.setCount(0); resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("参数错误"); return resultVO; } if(ReportBizEnum.parse(biz) == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("业务不支持"); return resultVO; } //2、部门校验 ResultVO depSearchResult = departmentService.getDepInfoByDepId(depId); if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("系统调用错误"); return resultVO; } if(depSearchResult.getData() == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("部门不存在"); return resultVO; } //3、判断查询实时当月数据,还是往期数据 LocalDate checkDate = LocalDate.of(year,month,1); LocalDate nowDate = LocalDate.now(); LocalDate offsetDate = LocalDate.of(nowDate.getYear(),nowDate.getMonthValue(),1); if(checkDate.isAfter(offsetDate)){ //查询将来的数据 resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("超出查询范围"); return resultVO; }else if(checkDate.isBefore(offsetDate)){ //查询固化报表 RiskMonthReport riskMonthReport = riskMonthReportService.findByMonth(biz,depId,year,month); if(riskMonthReport == null){ ResultVO genResult = generateDepartmentMonthRiskReport(biz,depId,year,month); if(genResult == null || !genResult.getCode().equals(ResultCodes.OK.getCode())){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg(genResult.getMsg()); return resultVO; }else { //重新获取固化报表 riskMonthReport = riskMonthReportService.findByMonth(biz,depId,year,month); } } if(riskMonthReport == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("查询失败"); return resultVO; } //封装后返回 RiskMonthReportRespDTO dto = packageRiskMonthReportData(riskMonthReport); resultVO.setCode(ResultCodes.OK.getCode()); resultVO.setCount(1); resultVO.setData(dto); return resultVO; }else { //查询当月数据 RiskMonthReportRespDTO reportRespDTO = getNowMonthRiskReport((DepInfoRPCRespDTO) depSearchResult.getData(),biz); if(reportRespDTO == null || reportRespDTO.getDetail() == null){ resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("未获取到当前月份数据"); return resultVO; } resultVO.setCode(ResultCodes.OK.getCode()); resultVO.setCount(1); resultVO.setData(reportRespDTO); return resultVO; } } /** * 获取指定部门的指定年度报表 * @param depId * @param biz * @param year * @return */ @Override public SearchResultVO> getDepartmentReportByYear(Long depId, Byte biz, Integer year) { SearchResultVO resultVO = new SearchResultVO<>(); if(depId == null || biz == null || year == null){ resultVO.setCode(ResultCodes.SERVER_PARAM_NULL.getCode()); resultVO.setMsg(ResultCodes.SERVER_PARAM_NULL.getDesc()); } if(ReportBizEnum.parse(biz) == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("业务不支持"); return resultVO; } //部门校验 ResultVO depSearchResult = departmentService.getDepInfoByDepId(depId); if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("系统调用错误"); return resultVO; } if(depSearchResult.getData() == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("部门不存在"); return resultVO; } //判断是否为当前年度 Integer maxMonth = 12; if(year == LocalDate.now().getYear()){ maxMonth = LocalDate.now().getMonthValue(); } List reportRespDTOS = new ArrayList<>(); for(int month = 1;month<= maxMonth;month++){ SearchResultVO searchResultVO = getReportByMonth(depId,biz,year,month); if(searchResultVO != null && searchResultVO.getCode().equals(ResultCodes.OK.getCode()) && searchResultVO.getData() != null){ reportRespDTOS.add((RiskMonthReportRespDTO) searchResultVO.getData()); } } resultVO.setCode(ResultCodes.OK.getCode()); if(!reportRespDTOS.isEmpty()){ resultVO.setData(reportRespDTOS); resultVO.setCount(reportRespDTOS.size()); } return resultVO; } /** * 获取指定部门连续多个月的报表 * @param depId * @param biz * @param beginYear * @param beginMonth * @param endYear * @param endMonth * @return */ @Override public SearchResultVO> getDepartmentReportByMultipleMonth(Long depId, Byte biz, Integer beginYear, Integer beginMonth, Integer endYear, Integer endMonth) { SearchResultVO resultVO = new SearchResultVO<>(); if(depId == null || biz == null || beginYear == null || beginMonth == null || endYear == null || endMonth == null){ resultVO.setCode(ResultCodes.SERVER_PARAM_NULL.getCode()); resultVO.setMsg(ResultCodes.SERVER_PARAM_NULL.getDesc()); } if(ReportBizEnum.parse(biz) == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("业务不支持"); return resultVO; } LocalDate beginDate = LocalDate.of(beginYear,beginMonth,1); LocalDate endDate = LocalDate.of(endYear,endMonth,1); if(beginDate.isAfter(endDate)){ resultVO.setCount(0); resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("查询日期错误"); return resultVO; } //部门校验 ResultVO depSearchResult = departmentService.getDepInfoByDepId(depId); if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("系统调用错误"); return resultVO; } if(depSearchResult.getData() == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("部门不存在"); return resultVO; } List reportRespDTOS = new ArrayList<>(); while (!beginDate.isAfter(endDate)){ SearchResultVO searchResultVO = getReportByMonth(depId,biz,beginDate.getYear(), beginDate.getMonthValue()); if(searchResultVO != null && searchResultVO.getCode().equals(ResultCodes.OK.getCode()) && searchResultVO.getData() != null){ reportRespDTOS.add((RiskMonthReportRespDTO) searchResultVO.getData()); } beginDate = beginDate.plusMonths(1); } resultVO.setCode(ResultCodes.OK.getCode()); if(!reportRespDTOS.isEmpty()){ resultVO.setData(reportRespDTOS); resultVO.setCount(reportRespDTOS.size()); } return resultVO; } /** * 获取指定部门的子部门连续多个月的报表 * @param depId * @param biz * @param beginYear * @param beginMonth * @param endYear * @param endMonth * @return */ @Override public SearchResultVO> getSubDepartmentReportByMultipleMonth(Long depId, Byte biz, Integer beginYear, Integer beginMonth, Integer endYear, Integer endMonth) { SearchResultVO resultVO = new SearchResultVO<>(); if(depId == null || biz == null || beginYear == null || beginMonth == null || endYear == null || endMonth == null){ resultVO.setCode(ResultCodes.SERVER_PARAM_NULL.getCode()); resultVO.setMsg(ResultCodes.SERVER_PARAM_NULL.getDesc()); } if(ReportBizEnum.parse(biz) == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("业务不支持"); return resultVO; } LocalDate beginDate = LocalDate.of(beginYear,beginMonth,1); LocalDate endDate = LocalDate.of(endYear,endMonth,1); if(beginDate.isAfter(endDate)){ resultVO.setCount(0); resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("查询日期错误"); return resultVO; } //部门校验 ResultVO depSearchResult = departmentService.getDepInfoByDepId(depId); if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("系统调用错误"); return resultVO; } if(depSearchResult.getData() == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("部门不存在"); return resultVO; } //获取子部门列表 ResultVO> subDepListResult = departmentService.listSubDepsByDepId(depId); if(subDepListResult == null || !subDepListResult.getCode().equals(ResultCodes.OK.getCode())){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("获取子部门出错"); return resultVO; } List subDepList = (List) subDepListResult.getData(); if(subDepList == null || subDepList.isEmpty()){ resultVO.setCount(0); resultVO.setCode(ResultCodes.OK.getCode()); resultVO.setMsg("子部门数据未找到"); return resultVO; } //获取子部门报表 List reportRespDTOS = new ArrayList<>(); while (!beginDate.isAfter(endDate)){ for(DepInfoRPCRespDTO subDep : subDepList){ if(subDep == null) continue; SearchResultVO searchResultVO = getReportByMonth(subDep.getDepId(),biz, beginDate.getYear(), beginDate.getMonthValue()); if(searchResultVO != null && searchResultVO.getCode().equals(ResultCodes.OK.getCode()) && searchResultVO.getData() != null){ reportRespDTOS.add((RiskMonthReportRespDTO) searchResultVO.getData()); } } beginDate = beginDate.plusMonths(1); } resultVO.setCode(ResultCodes.OK.getCode()); if(!reportRespDTOS.isEmpty()){ resultVO.setData(reportRespDTOS); resultVO.setCount(reportRespDTOS.size()); } return resultVO; } /** * 获取公司指定年份的报表 * @param year * @return */ @Override public SearchResultVO> getCompanyReportByYear(Byte biz,Integer year) { SearchResultVO resultVO = new SearchResultVO<>(); //公司所在部门ID //todo:需从数据库读取 Long companyDepId = 1L; if(ReportBizEnum.parse(biz) == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("业务不支持"); return resultVO; } //部门校验 ResultVO depSearchResult = departmentService.getDepInfoByDepId(companyDepId); if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("系统调用错误"); return resultVO; } if(depSearchResult.getData() == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("未获取到公司信息"); return resultVO; } //判断是否为当前年度 Integer maxMonth = 12; if(year == LocalDate.now().getYear()){ maxMonth = LocalDate.now().getMonthValue(); } List reportRespDTOS = new ArrayList<>(); for(int month = 1;month<= maxMonth;month++){ SearchResultVO searchResultVO = getReportByMonth(companyDepId,biz,year,month); if(searchResultVO != null && searchResultVO.getCode().equals(ResultCodes.OK.getCode()) && searchResultVO.getData() != null){ reportRespDTOS.add((RiskMonthReportRespDTO) searchResultVO.getData()); } } resultVO.setCode(ResultCodes.OK.getCode()); if(!reportRespDTOS.isEmpty()){ resultVO.setData(reportRespDTOS); resultVO.setCount(reportRespDTOS.size()); } return resultVO; } /** * 获取公司在指定年月的报表 * @param year * @return */ @Override public SearchResultVO getCompanyReportByMonth(Byte biz,Integer year,Integer month) { SearchResultVO resultVO = new SearchResultVO<>(); resultVO.setUsePage(false); //公司所在部门ID //todo:需从数据库读取 Long companyDepId = 1L; //1、参数校验 if(biz == null || year == null || month == null || year < 2000 || year > 9999 || month < 1|| month > 12){ resultVO.setCount(0); resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("参数错误"); return resultVO; } if(ReportBizEnum.parse(biz) == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode()); resultVO.setMsg("业务不支持"); return resultVO; } //2、部门校验 ResultVO depSearchResult = departmentService.getDepInfoByDepId(companyDepId); if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("系统调用错误"); return resultVO; } if(depSearchResult.getData() == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("未获取到公司信息"); return resultVO; } //3、判断查询实时当月数据,还是往期数据 LocalDate checkDate = LocalDate.of(year,month,1); LocalDate nowDate = LocalDate.now(); LocalDate offsetDate = LocalDate.of(nowDate.getYear(),nowDate.getMonthValue(),1); if(checkDate.isAfter(offsetDate)){ //查询将来的数据 resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("超出查询范围"); return resultVO; }else if(checkDate.isBefore(offsetDate)){ //查询固化报表 RiskMonthReport riskMonthReport = riskMonthReportService.findByMonth(biz,companyDepId,year,month); if(riskMonthReport == null){ ResultVO genResult = generateDepartmentMonthRiskReport(biz,companyDepId,year,month); if(genResult == null || !genResult.getCode().equals(ResultCodes.OK.getCode())){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg(genResult.getMsg()); return resultVO; }else { //重新获取固化报表 riskMonthReport = riskMonthReportService.findByMonth(biz,companyDepId,year,month); } } if(riskMonthReport == null){ resultVO.setCount(0); resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("查询失败"); return resultVO; } //封装后返回 RiskMonthReportRespDTO dto = packageRiskMonthReportData(riskMonthReport); resultVO.setCode(ResultCodes.OK.getCode()); resultVO.setCount(1); resultVO.setData(dto); return resultVO; }else { //查询当月数据 RiskMonthReportRespDTO reportRespDTO = getNowMonthRiskReport((DepInfoRPCRespDTO) depSearchResult.getData(),biz); if(reportRespDTO == null || reportRespDTO.getDetail() == null){ resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode()); resultVO.setMsg("未获取到当前月份数据"); return resultVO; } resultVO.setCode(ResultCodes.OK.getCode()); resultVO.setCount(1); resultVO.setData(reportRespDTO); return resultVO; } } /** * 包装报表数据,获取报表详情信息 * @param report * @return */ private RiskMonthReportRespDTO packageRiskMonthReportData(RiskMonthReport report){ if(report == null || report.getId() == null) return null; RiskMonthReportRespDTO respDTO = new RiskMonthReportRespDTO(); BeanUtils.copyProperties(report,respDTO); if(report.getBiz().equals(ReportBizEnum.PREVENT_RISK.getType())){ PreventMonthReportDetailRespDTO detail = getPreventReportDetail(report.getId()); respDTO.setDetail(detail); }else if(report.getBiz().equals(ReportBizEnum.INCIDENT_COUNT.getType())){ List details = getIncidentReportDetail(report.getId()); respDTO.setDetail(details); }else if(report.getBiz().equals(ReportBizEnum.EMERGENCY_PRACTICE.getType())){ List details = getEmergencyReportDeatil(report.getId()); respDTO.setDetail(details); } return respDTO; } /** * 获取隐患报表详情数据 * @param reportId * @return */ private PreventMonthReportDetailRespDTO getPreventReportDetail(Long reportId){ if(reportId == null || reportId < 0) return null; PreventMonthReportDetail detail = preventMonthReportDetailService.getById(reportId); if(detail == null) return null; PreventMonthReportDetailRespDTO dto = new PreventMonthReportDetailRespDTO(); BeanUtils.copyProperties(detail,dto); return dto; } /** * 获取应急演练报表详情数据 * @param reportId * @return */ private List getEmergencyReportDeatil(Long reportId){ if(reportId == null || reportId < 0) return null; List itemList = emergencyPracticeReportItemService.listByReportId(reportId); if(itemList == null || itemList.isEmpty()) return null; List dtoList = new ArrayList<>(); for(EmergencyPracticeReportItem item : itemList){ if(item == null) continue; EmergencyPracticeReportItemRespDTO dto = new EmergencyPracticeReportItemRespDTO(); BeanUtils.copyProperties(item,dto); dtoList.add(dto); } return dtoList; } /** * 获取事故管理报表详情数据 * @param reportId * @return */ private List getIncidentReportDetail(Long reportId){ if(reportId == null || reportId < 0) return null; List itemList = incidentMonthReportItemService.listByReportId(reportId); if(itemList == null || itemList.isEmpty()) return null; List dtoList = new ArrayList<>(); for(IncidentMonthReportItem item : itemList){ if(item == null) continue; IncidentMonthReportItemRespDTO dto = new IncidentMonthReportItemRespDTO(); BeanUtils.copyProperties(item,dto); dtoList.add(dto); } return dtoList; } /** * 获取当月的风险报告数据 * @param depInfo * @param biz * @return */ private RiskMonthReportRespDTO getNowMonthRiskReport(DepInfoRPCRespDTO depInfo,Byte biz){ if(depInfo == null || biz == null || depInfo.getDepId() == null || ReportBizEnum.parse(biz) == null){ return null; } LocalDate nowDate = LocalDate.now(); //生成报表对象 RiskMonthReportRespDTO reportRespDTO = new RiskMonthReportRespDTO(); reportRespDTO.setBiz(biz); reportRespDTO.setDepId(depInfo.getDepId()); reportRespDTO.setDepLevel(depInfo.getDepLevel()); reportRespDTO.setYear(nowDate.getYear()); reportRespDTO.setMonth(nowDate.getMonthValue()); reportRespDTO.setBeginDate(DateUtil.beginOfMonth(Date.from(nowDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())).toLocalDateTime().toLocalDate()); reportRespDTO.setEndDate(DateUtil.endOfMonth(Date.from(nowDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())).toLocalDateTime().toLocalDate()); RiskReportAnalysisHandler riskReportAnalysisHandler = new RiskReportAnalysisHandler(); //获取报表详细数据,并打包数据 if(biz.equals(ReportBizEnum.PREVENT_RISK.getType())){ //双重预防隐患统计 RiskDataBO riskDataBO = collectReportDataService.getPreventRiskData(depInfo,nowDate.getYear(), nowDate.getMonthValue()); PreventMonthReportDetail reportDetail = riskReportAnalysisHandler.analysisPrevReportData(riskDataBO); PreventMonthReportDetailRespDTO detailRespDTO = packagePreventReportDetailDto(reportDetail); reportRespDTO.setDetail(detailRespDTO); }else if(biz.equals(ReportBizEnum.EMERGENCY_PRACTICE.getType())){ //应急管理统计 EmergencyDataBO emergencyDataBO = collectReportDataService.getEmergencyCountData(depInfo, nowDate.getYear(), nowDate.getMonthValue()); List reportItemList = riskReportAnalysisHandler.analysisEmgcReportData(emergencyDataBO); List itemRespDTOList = packageEmergencyReportDetailDto(reportItemList); reportRespDTO.setDetail(itemRespDTOList); }else if(biz.equals(ReportBizEnum.INCIDENT_COUNT.getType())){ //事故管理统计 IncidentDataBO incidentDataBO = collectReportDataService.getIncidentCountData(depInfo,nowDate.getYear(), nowDate.getMonthValue()); List reportItemList = riskReportAnalysisHandler.analysisIncidentReportDate(incidentDataBO); List reportItemRespDTOS = packageIncidentReportDetailDto(reportItemList); reportRespDTO.setDetail(reportItemRespDTOS); } return reportRespDTO; } /** * 打包应急管理统计数据 * @param reportItemList * @return */ private List packageEmergencyReportDetailDto(List reportItemList){ if(reportItemList == null || reportItemList.isEmpty()) return null; List itemRespDTOList = new ArrayList<>(); for(EmergencyPracticeReportItem item : reportItemList){ if(item == null) continue; EmergencyPracticeReportItemRespDTO itemRespDTO = new EmergencyPracticeReportItemRespDTO(); BeanUtils.copyProperties(item,itemRespDTO); itemRespDTOList.add(itemRespDTO); } return itemRespDTOList; } /** * 打包事故管理统计数据 * @param reportItemList * @return */ private List packageIncidentReportDetailDto(List reportItemList){ if(reportItemList == null || reportItemList.isEmpty()) return null; List itemRespDTOList = new ArrayList<>(); for(IncidentMonthReportItem item : reportItemList){ if(item == null) continue; IncidentMonthReportItemRespDTO dto = new IncidentMonthReportItemRespDTO(); BeanUtils.copyProperties(item,dto); itemRespDTOList.add(dto); } return itemRespDTOList; } /** * 打包双重预防隐患统计数据 * @param reportDetail * @return */ private PreventMonthReportDetailRespDTO packagePreventReportDetailDto(PreventMonthReportDetail reportDetail){ if(reportDetail == null) return null; PreventMonthReportDetailRespDTO dto = new PreventMonthReportDetailRespDTO(); BeanUtils.copyProperties(reportDetail,dto); return dto; } }