package com.gkhy.safePlatform.equipment.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService; import com.gkhy.safePlatform.equipment.entity.*; import com.gkhy.safePlatform.equipment.excepiton.EquipmentException; import com.gkhy.safePlatform.equipment.model.dto.req.*; import com.gkhy.safePlatform.equipment.model.dto.resp.KeypointEquipmentInfoExcel; import com.gkhy.safePlatform.equipment.repository.EquipmentTypeMngRepository; import com.gkhy.safePlatform.equipment.repository.KeypointEquipmentInfoRepository; import com.gkhy.safePlatform.equipment.service.*; import com.gkhy.safePlatform.equipment.service.baseService.*; import com.gkhy.safePlatform.equipment.utils.DateUtils; import com.gkhy.safePlatform.equipment.utils.poihelper.ExcelLogs; import com.gkhy.safePlatform.equipment.utils.poihelper.ExcelUtil; import org.apache.commons.collections.CollectionUtils; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.query.PageQuery; import com.gkhy.safePlatform.commons.vo.ResultVO; import com.gkhy.safePlatform.commons.vo.SearchResultVO; import com.gkhy.safePlatform.equipment.model.dto.resp.KeypointEquipmentInfoDto; import com.gkhy.safePlatform.equipment.utils.QueryHelpPlus; import com.gkhy.safePlatform.commons.utils.BeanCopyUtils; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.Serializable; import java.net.URLEncoder; import java.util.*; import java.util.stream.Collectors; /** * 重点监管装置/设备 详细信息(KeypointEquipmentInfo)表服务实现类 * * @author xurui * @since 2022-07-19 13:36:27 */ @Service("keypointEquipmentInfoService") public class KeypointEquipmentInfoServiceImpl implements KeypointEquipmentInfoService { @Autowired private KeypointEquipmentInfoBaseService keypointEquipmentInfoBaseService; @Autowired private EquipmentTakecareDetailBaseService equipmentTakecareDetailBaseService; @Autowired private EquipmentTestDetailBaseService equipmentTestDetailBaseService; @Autowired private EquipmentRepairDetailBaseService equipmentRepairDetailBaseService; @Autowired private EquipmentTakecareStardardDetailBaseService equipmentTakecareStardardDetailBaseService; @Autowired private EquipmentCheckStandardDetailBaseService equipmentCheckStandardDetailBaseService; @DubboReference(check = false) private AccountDepartmentService accountDepartmentService; @Autowired private EquipmentInfoService equipmentInfoService; @Autowired private EquipmentTypeMngRepository equipmentTypeMngRepository; @Autowired public HttpServletResponse response; @Override public ResultVO queryAll(PageQuery pageQuery) { Long pageIndex = pageQuery.getPageIndex(); Long pageSize = pageQuery.getPageSize(); IPage page = new Page<>(pageIndex, pageSize); page = keypointEquipmentInfoBaseService.selectPage(page, QueryHelpPlus.getPredicate(KeypointEquipmentInfo.class, pageQuery.getSearchParams())); List respList = BeanCopyUtils.copyBeanList(page.getRecords(), KeypointEquipmentInfoDto.class); Set collectTypeIdSet = new HashSet(); //获取额外信息 respList.forEach(f->{ //设备保养 EquipmentTakecareDetailQueryCriteria criteria = new EquipmentTakecareDetailQueryCriteria(); criteria.setEquipmentId(f.getId()); criteria.setInfoTpe(3); List takecareDetailList = equipmentTakecareDetailBaseService.queryAll(criteria); f.setTakecareDetailList(takecareDetailList); //设备检测 EquipmentTestDetailQueryCriteria criteria1 = new EquipmentTestDetailQueryCriteria(); criteria1.setEquipmentId(f.getId()); criteria1.setInfoTpe(3); List checkDetailList = equipmentTestDetailBaseService.queryAll(criteria1); f.setCheckDetailList(checkDetailList); //设备检查 TODO //设备维修 EquipmentRepairDetailQueryCriteria criteria2 = new EquipmentRepairDetailQueryCriteria(); criteria2.setEquipmentId(f.getId()); criteria2.setInfoTpe(3); List repairDetailList = equipmentRepairDetailBaseService.queryAll(criteria2); f.setRepaireDetailList(repairDetailList); // --------------------------- 获取设备设施类型管理ID----------------------- collectTypeIdSet.add(f.getEquipmentTypeId()); // --------------------------- 获取部门信息----------------------- //收集所用到的部门ID Set collectDepIdSet = new HashSet(); collectDepIdSet.add(f.getDepartmentId()); takecareDetailList.forEach(info->{collectDepIdSet.add(info.getLeadingPersonDepartmentId());}); checkDetailList.forEach(info->{collectDepIdSet.add(info.getTestPersonDepartmentId());}); repairDetailList.forEach(info->{collectDepIdSet.add(info.getRepairPersonDepartmentId());}); //获取部门名集合 Map depNameMap = equipmentInfoService.getDepName(collectDepIdSet); //set f.setDepartmentName(depNameMap.get(f.getDepartmentId())); takecareDetailList.forEach(info->{info.setLeadingPersonDepartmentName(depNameMap.get(info.getLeadingPersonDepartmentId()));}); checkDetailList.forEach(info->{info.setTestPersonDepartmentName(depNameMap.get(info.getTestPersonDepartmentId()));}); repairDetailList.forEach(info->{info.setRepairPersonDepartmentName(depNameMap.get(info.getRepairPersonDepartmentId()));}); }); //获取设备设施类型名称 if(!collectTypeIdSet.isEmpty()){ List typeMngList = equipmentTypeMngRepository.selectList(new QueryWrapper().in("id",collectTypeIdSet)); Map typePool = new HashMap<>(); typeMngList.forEach(f->{ typePool.put(f.getId(),f.getTypeName()); }); respList.forEach(f->{ f.setEquipmentTypeName(typePool.get(f.getEquipmentTypeId())); }); } return new SearchResultVO<>( true, pageIndex, pageSize,page.getPages(), page.getTotal(), respList, ResultCodes.OK ); } @Override public List queryAll(KeypointEquipmentInfoQueryCriteria criteria) { return keypointEquipmentInfoBaseService.queryAll( criteria); } @Override public KeypointEquipmentInfoDto selectOne(Serializable id) { KeypointEquipmentInfo keypointEquipmentInfo = keypointEquipmentInfoBaseService.getById(id); if(keypointEquipmentInfo == null){ return null; } KeypointEquipmentInfoDto infoDto = BeanCopyUtils.copyBean(keypointEquipmentInfo, KeypointEquipmentInfoDto.class); //设备保养 EquipmentTakecareDetailQueryCriteria criteria = new EquipmentTakecareDetailQueryCriteria(); criteria.setEquipmentId(infoDto.getId()); criteria.setInfoTpe(3); List takecareDetailList = equipmentTakecareDetailBaseService.queryAll(criteria); infoDto.setTakecareDetailList(takecareDetailList); //设备检测 EquipmentTestDetailQueryCriteria criteria1 = new EquipmentTestDetailQueryCriteria(); criteria1.setEquipmentId(infoDto.getId()); criteria1.setInfoTpe(3); List checkDetailList = equipmentTestDetailBaseService.queryAll(criteria1); infoDto.setCheckDetailList(checkDetailList); //设备检查 TODO //设备维修 EquipmentRepairDetailQueryCriteria criteria2 = new EquipmentRepairDetailQueryCriteria(); criteria2.setEquipmentId(infoDto.getId()); criteria2.setInfoTpe(3); List repairDetailList = equipmentRepairDetailBaseService.queryAll(criteria2); infoDto.setRepaireDetailList(repairDetailList); //检查标准 EquipmentCheckStandardDetailQueryCriteria criteria3 = new EquipmentCheckStandardDetailQueryCriteria(); criteria3.setEquipmentId(infoDto.getId()); criteria3.setInfoTpe(3); List checkStandardDetailList = equipmentCheckStandardDetailBaseService.queryAll(criteria3); infoDto.setCheckStandardeDetailList(checkStandardDetailList); //保养标准 EquipmentTakecareStardardDetailQueryCriteria criteria4 = new EquipmentTakecareStardardDetailQueryCriteria(); criteria4.setEquipmentId(infoDto.getId()); criteria4.setInfoTpe(3); List takecareStardardDetailList = equipmentTakecareStardardDetailBaseService.queryAll(criteria4); infoDto.setTakecareStardardeDetailList(takecareStardardDetailList); // --------------------------- 获取部门信息----------------------- //收集所用到的部门ID Set collectDepIdSet = new HashSet(); collectDepIdSet.add(infoDto.getDepartmentId()); takecareDetailList.forEach(info->{collectDepIdSet.add(info.getLeadingPersonDepartmentId());}); checkDetailList.forEach(info->{collectDepIdSet.add(info.getTestPersonDepartmentId());}); repairDetailList.forEach(info->{collectDepIdSet.add(info.getRepairPersonDepartmentId());}); //获取部门名集合 Map depNameMap = equipmentInfoService.getDepName(collectDepIdSet); //set infoDto.setDepartmentName(depNameMap.get(infoDto.getDepartmentId())); takecareDetailList.forEach(info->{info.setLeadingPersonDepartmentName(depNameMap.get(info.getLeadingPersonDepartmentId()));}); checkDetailList.forEach(info->{info.setTestPersonDepartmentName(depNameMap.get(info.getTestPersonDepartmentId()));}); repairDetailList.forEach(info->{info.setRepairPersonDepartmentName(depNameMap.get(info.getRepairPersonDepartmentId()));}); EquipmentTypeMng typeMng = equipmentTypeMngRepository.selectOne(new QueryWrapper().eq("id",infoDto.getEquipmentTypeId())); if(typeMng != null){ infoDto.setEquipmentTypeName(typeMng.getTypeName()); } // TODO 调用应急预案RPC infoDto.setEmergencePlanName(""); return infoDto; } @Override public void addOrUpdate(KeypointEquipmentInfoDto infoDto) { KeypointEquipmentInfo equipmentInfo = BeanCopyUtils.copyBean(infoDto, KeypointEquipmentInfo.class); //保存主数据 if (infoDto.getId() == null) { //新增 keypointEquipmentInfoBaseService.save(equipmentInfo); } else { keypointEquipmentInfoBaseService.update(equipmentInfo,new UpdateWrapper().eq("id",infoDto.getId())); } //保存设备保养 List takecareDetailList = infoDto.getTakecareDetailList(); takecareDetailList.forEach(f->{ f.setEquipmentId(equipmentInfo.getId()); f.setInfoTpe(3); }); equipmentTakecareDetailBaseService.saveOrUpdateBatch(takecareDetailList); if(StringUtils.hasText(infoDto.getDelTakecareDetails())){ List idList = Arrays.stream(infoDto.getDelTakecareDetails().split(",")).map(s-> Long.parseLong(s.trim())) .collect(Collectors.toList()); List delList = new ArrayList<>(); idList.forEach(f->{ EquipmentTakecareDetail info = new EquipmentTakecareDetail(); info.setDelFlag(1); info.setId(f); delList.add(info); }); equipmentTakecareDetailBaseService.updateBatchById(delList); } //保存设备检测 List testDetailList = infoDto.getTestDetailList(); testDetailList.forEach(f->{ f.setEquipmentId(equipmentInfo.getId()); f.setInfoTpe(3); }); equipmentTestDetailBaseService.saveOrUpdateBatch(testDetailList); if(StringUtils.hasText(infoDto.getDelTestDetails())){ List idList = Arrays.stream(infoDto.getDelTestDetails().split(",")).map(s-> Long.parseLong(s.trim())) .collect(Collectors.toList()); List delList = new ArrayList<>(); idList.forEach(f->{ EquipmentTestDetail info = new EquipmentTestDetail(); info.setDelFlag(1); info.setId(f); delList.add(info); }); equipmentTestDetailBaseService.updateBatchById(delList); } //设备检查 TODO //保存设备维修 List repaireDetailList = infoDto.getRepaireDetailList(); repaireDetailList.forEach(f->{ f.setEquipmentId(equipmentInfo.getId()); f.setInfoTpe(3); }); equipmentRepairDetailBaseService.saveOrUpdateBatch(repaireDetailList); if(StringUtils.hasText(infoDto.getDelRepaireDetails())){ List idList = Arrays.stream(infoDto.getDelRepaireDetails().split(",")).map(s-> Long.parseLong(s.trim())) .collect(Collectors.toList()); List delList = new ArrayList<>(); idList.forEach(f->{ EquipmentRepairDetail info = new EquipmentRepairDetail(); info.setDelFlag(1); info.setId(f); delList.add(info); }); equipmentRepairDetailBaseService.updateBatchById(delList); } //保存检查标准 List checkStandardeDetailList = infoDto.getCheckStandardeDetailList(); checkStandardeDetailList.forEach(f->{ f.setEquipmentId(equipmentInfo.getId()); f.setInfoTpe(3); }); equipmentCheckStandardDetailBaseService.saveOrUpdateBatch(checkStandardeDetailList); if(StringUtils.hasText(infoDto.getDelCheckStandardeDetails())){ List idList = Arrays.stream(infoDto.getDelCheckStandardeDetails().split(",")).map(s-> Long.parseLong(s.trim())) .collect(Collectors.toList()); List delList = new ArrayList<>(); idList.forEach(f->{ EquipmentCheckStandardDetail info = new EquipmentCheckStandardDetail(); info.setDelFlag(1); info.setId(f); delList.add(info); }); equipmentCheckStandardDetailBaseService.updateBatchById(delList); } //保存保养标准 List takecareStardardeDetailList = infoDto.getTakecareStardardeDetailList(); takecareStardardeDetailList.forEach(f->{ f.setEquipmentId(equipmentInfo.getId()); f.setInfoTpe(3); }); equipmentTakecareStardardDetailBaseService.saveOrUpdateBatch(takecareStardardeDetailList); if(StringUtils.hasText(infoDto.getDelTakecareStardardeDetails())){ List idList = Arrays.stream(infoDto.getDelTakecareStardardeDetails().split(",")).map(s-> Long.parseLong(s.trim())) .collect(Collectors.toList()); List delList = new ArrayList<>(); idList.forEach(f->{ EquipmentTakecareStardardDetail info = new EquipmentTakecareStardardDetail(); info.setDelFlag(1); info.setId(f); delList.add(info); }); equipmentTakecareStardardDetailBaseService.updateBatchById(delList); } } @Override public void importData(MultipartFile file) throws IOException { String contentType = file.getContentType(); if(!"application/vnd.ms-excel".equals(contentType) && !"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(contentType)) { throw new EquipmentException("上传的excel格式错误"); } Collection importExcel = ExcelUtil.importExcel(KeypointEquipmentInfoImportExcel.class, file.getInputStream(), "yyyy-MM-dd HH:mm:ss", new ExcelLogs() , 0); if (CollectionUtils.isEmpty(importExcel)) { return ; } List respList = BeanCopyUtils.copyBeanList((List)importExcel, KeypointEquipmentInfo.class); keypointEquipmentInfoBaseService.saveBatch(respList); } @Override public void delete(Long[] ids) { if(ids == null){ throw new EquipmentException(ResultCodes.CLIENT_PARAM_ILLEGAL); } List idList = Arrays.asList(ids); List delList = new ArrayList<>(); idList.forEach(f->{ KeypointEquipmentInfo info = new KeypointEquipmentInfo(); info.setDelFlag(1); info.setId(f); delList.add(info); }); keypointEquipmentInfoBaseService.updateBatchById(delList); } @Override public void exportTemplate() throws IOException { Map map = new LinkedHashMap<>(); map.put("1","类型/类别外键 "); map.put("2","装置/部位名称"); map.put("3","所属单位部门/外键"); map.put("4","具体位置"); map.put("5","负责人姓名"); map.put("6","联系人/外键"); map.put("7","录入人/外键"); map.put("8","责任人/外键"); map.put("9","装置部位分类 1:关键装置 2:重点部位"); map.put("10","检查周期"); map.put("11","应急预案/外键"); map.put("12","主要危险有害因素"); map.put("13","易导致风险"); map.put("14","应急处置措施"); map.put("15","现场图片地址路径"); String fileName = URLEncoder.encode("重点监管装置设备数据导入模板.xls", "UTF-8"); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); ExcelUtil.exportExcel(map,new ArrayList<>() , response.getOutputStream()); response.getOutputStream().close(); } @Override public void exportData(KeypointEquipmentInfoQueryCriteria queryCriteria) throws IOException { Map map = new LinkedHashMap<>(); map.put("0","ID"); map.put("1","类型/类别外键"); map.put("2","装置/部位名称"); map.put("3","所属单位部门/外键"); map.put("4","具体位置"); map.put("5","负责人姓名"); map.put("6","联系人/外键"); map.put("7","录入人/外键"); map.put("8","责任人/外键"); map.put("9","装置部位分类 1:关键装置 2:重点部位"); map.put("10","检查周期"); map.put("11","应急预案/外键"); map.put("12","主要危险有害因素"); map.put("13","易导致风险"); map.put("14","应急处置措施"); map.put("15","现场图片地址路径"); String key = DateUtils.date2String(new Date(), DateUtils.PATTERN_ALLTIME_NOSIGN) ; String fileName = URLEncoder.encode("重点监管装置设备"+key+".xls", "UTF-8"); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); List respList = BeanCopyUtils.copyBeanList(this.queryAll(queryCriteria), KeypointEquipmentInfoExcel.class); ExcelUtil.exportExcel(map,respList , response.getOutputStream(),DateUtils.PATTERN_STANDARD); response.getOutputStream().close(); } }