package com.ruoyi.safe.service.impl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.basic.enums.ApplicationTypeEnum; import com.ruoyi.basic.enums.RecordTypeEnum; import com.ruoyi.basic.utils.FileUtil; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.project.system.domain.SysUser; import com.ruoyi.safe.dto.SafeLineInspectionExecuteDto; import com.ruoyi.safe.pojo.SafeLineInspection; import com.ruoyi.safe.mapper.SafeLineInspectionMapper; import com.ruoyi.safe.pojo.SafeLineInspectionHazard; import com.ruoyi.safe.pojo.SafeLineInspectionRecord; import com.ruoyi.safe.pojo.SafeHidden; import com.ruoyi.safe.service.SafeHiddenService; import com.ruoyi.safe.service.SafeLineInspectionHazardService; import com.ruoyi.safe.service.SafeLineInspectionRecordService; import com.ruoyi.safe.service.SafeLineInspectionService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.project.system.mapper.SysUserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** *

* 安全生产--线路巡检任务 服务实现类 *

* * @author 芯导软件(江苏)有限公司 * @since 2026-06-29 */ @Service public class SafeLineInspectionServiceImpl extends ServiceImpl implements SafeLineInspectionService { @Autowired private SysUserMapper sysUserMapper; @Autowired private SafeLineInspectionRecordService safeLineInspectionRecordService; @Autowired private SafeLineInspectionHazardService safeLineInspectionHazardService; @Autowired private SafeHiddenService safeHiddenService; @Autowired private FileUtil fileUtil; @Override public IPage pageSafeLineInspection(Page page, SafeLineInspection safeLineInspection) { IPage result = this.lambdaQuery() .eq(safeLineInspection.getScheduleTaskId() != null, SafeLineInspection::getScheduleTaskId, safeLineInspection.getScheduleTaskId()) .like(StringUtils.isNotBlank(safeLineInspection.getInspectionCode()), SafeLineInspection::getInspectionCode, safeLineInspection.getInspectionCode()) .like(StringUtils.isNotBlank(safeLineInspection.getInspectionName()), SafeLineInspection::getInspectionName, safeLineInspection.getInspectionName()) .like(StringUtils.isNotBlank(safeLineInspection.getLineName()), SafeLineInspection::getLineName, safeLineInspection.getLineName()) .like(StringUtils.isNotBlank(safeLineInspection.getInspectionProject()), SafeLineInspection::getInspectionProject, safeLineInspection.getInspectionProject()) .eq(StringUtils.isNotBlank(safeLineInspection.getStatus()), SafeLineInspection::getStatus, safeLineInspection.getStatus()) .orderByDesc(SafeLineInspection::getCreateTime) .page(page); // 关联查询巡检人名称 fillInspectorName(result.getRecords()); return result; } @Override @Transactional(rollbackFor = Exception.class) public boolean executeInspection(SafeLineInspectionExecuteDto executeDto) { if (executeDto == null || executeDto.getInspectionId() == null) { throw new IllegalArgumentException("巡检记录不能为空"); } SafeLineInspection inspection = this.getById(executeDto.getInspectionId()); if (inspection == null) { throw new IllegalArgumentException("巡检记录不存在"); } if ("已完成".equals(inspection.getStatus())) { throw new IllegalArgumentException("巡检记录已完成,请勿重复执行"); } if (!"正常".equals(executeDto.getCheckResult()) && !"异常".equals(executeDto.getCheckResult())) { throw new IllegalArgumentException("请选择巡检结果"); } LocalDateTime checkTime = executeDto.getCheckTime() == null ? LocalDateTime.now() : executeDto.getCheckTime(); SafeLineInspectionRecord record = buildInspectionRecord(inspection, executeDto, checkTime); safeLineInspectionRecordService.save(record); inspection.setInspectionResult(executeDto.getCheckResult()); inspection.setActualInspectionTime(checkTime); inspection.setUpdateTime(LocalDateTime.now()); inspection.setUpdateUser(SecurityUtils.getUserId().intValue()); appendCurrentInspector(inspection); if ("异常".equals(executeDto.getCheckResult())) { validateHazard(executeDto); SafeHidden hidden = createSafeHidden(executeDto); safeHiddenService.add(hidden); fileUtil.saveStorageAttachment( ApplicationTypeEnum.IMAGE, RecordTypeEnum.SAFE_HIDDEN, hidden.getId().longValue(), executeDto.getStorageBlobDTOs() ); SafeLineInspectionHazard hazard = buildLineHazard(inspection, record, hidden, executeDto); safeLineInspectionHazardService.save(hazard); inspection.setHiddenId(hidden.getId()); inspection.setStatus("待整改"); } else { inspection.setHiddenId(null); inspection.setStatus("已完成"); } return this.updateById(inspection); } private SafeLineInspectionRecord buildInspectionRecord(SafeLineInspection inspection, SafeLineInspectionExecuteDto executeDto, LocalDateTime checkTime) { SafeLineInspectionRecord record = new SafeLineInspectionRecord(); record.setInspectionId(inspection.getId()); record.setCheckPoint(inspection.getLineName()); record.setCheckContent(inspection.getInspectionProject()); record.setCheckResult(executeDto.getCheckResult()); record.setCheckDesc("异常".equals(executeDto.getCheckResult()) ? null : executeDto.getCheckDesc()); record.setCheckTime(checkTime); record.setStorageBlobDTOs(executeDto.getStorageBlobDTOs()); return record; } private void validateHazard(SafeLineInspectionExecuteDto executeDto) { if (executeDto.getStorageBlobDTOs() == null || executeDto.getStorageBlobDTOs().isEmpty()) { throw new IllegalArgumentException("巡检异常时请上传异常照片"); } if (StringUtils.isBlank(executeDto.getHiddenType())) { throw new IllegalArgumentException("请选择隐患类型"); } if (StringUtils.isBlank(executeDto.getRiskLevel())) { throw new IllegalArgumentException("请选择隐患风险等级"); } if (StringUtils.isBlank(executeDto.getHazardLocation())) { throw new IllegalArgumentException("请输入隐患位置"); } if (StringUtils.isBlank(executeDto.getHazardDesc())) { throw new IllegalArgumentException("请输入异常描述/整改要求"); } if (executeDto.getRectifyUserId() == null) { throw new IllegalArgumentException("请选择整改责任人"); } if (executeDto.getRectifyTime() == null) { throw new IllegalArgumentException("请选择整改完成期限"); } } private SafeHidden createSafeHidden(SafeLineInspectionExecuteDto executeDto) { SafeHidden hidden = new SafeHidden(); hidden.setType(executeDto.getHiddenType()); hidden.setRiskLevel(executeDto.getRiskLevel()); hidden.setLocation(executeDto.getHazardLocation()); hidden.setHiddenDesc(executeDto.getHazardDesc()); hidden.setRectifyUserId(executeDto.getRectifyUserId()); hidden.setRectifyUserMobile(executeDto.getRectifyUserMobile()); hidden.setRectifyTime(executeDto.getRectifyTime()); return hidden; } private SafeLineInspectionHazard buildLineHazard(SafeLineInspection inspection, SafeLineInspectionRecord record, SafeHidden hidden, SafeLineInspectionExecuteDto executeDto) { SafeLineInspectionHazard hazard = new SafeLineInspectionHazard(); hazard.setInspectionId(inspection.getId()); hazard.setRecordId(record.getId()); hazard.setSafeHiddenId(hidden.getId()); hazard.setHazardCode(hidden.getHiddenCode()); hazard.setHazardDesc(executeDto.getHazardDesc()); hazard.setHazardLevel(executeDto.getRiskLevel()); hazard.setHazardLocation(executeDto.getHazardLocation()); hazard.setStatus("待整改"); hazard.setRectifyUserId(executeDto.getRectifyUserId()); hazard.setRectifyDeadline(executeDto.getRectifyTime()); return hazard; } private void appendCurrentInspector(SafeLineInspection inspection) { Long currentUserId = SecurityUtils.getUserId(); if (currentUserId == null) { return; } String currentUserIdStr = String.valueOf(currentUserId); String inspectorId = inspection.getInspectorId(); if (StringUtils.isBlank(inspectorId)) { inspection.setInspectorId(currentUserIdStr); return; } boolean exists = Arrays.stream(inspectorId.split(",")) .map(String::trim) .anyMatch(currentUserIdStr::equals); if (!exists) { inspection.setInspectorId(inspectorId + "," + currentUserIdStr); } } /** * 填充巡检人名称(支持多巡检人,逗号分隔) */ private void fillInspectorName(List records) { if (records == null || records.isEmpty()) { return; } // 收集所有巡检人ID(处理逗号分隔的多个ID) List allInspectorIds = records.stream() .map(SafeLineInspection::getInspectorId) .filter(ids -> ids != null && !ids.isEmpty()) .flatMap(ids -> Arrays.stream(ids.split(","))) .map(String::trim) .filter(s -> !s.isEmpty()) .map(Long::parseLong) .distinct() .collect(Collectors.toList()); if (allInspectorIds.isEmpty()) { return; } // 批量查询用户信息 List users = sysUserMapper.selectUserByIds(allInspectorIds); Map userMap = users.stream() .collect(Collectors.toMap(SysUser::getUserId, SysUser::getNickName, (v1, v2) -> v1)); // 设置巡检人名称 for (SafeLineInspection record : records) { if (record.getInspectorId() != null && !record.getInspectorId().isEmpty()) { String inspectorNames = Arrays.stream(record.getInspectorId().split(",")) .map(String::trim) .filter(s -> !s.isEmpty()) .map(idStr -> { try { Long id = Long.parseLong(idStr); return userMap.getOrDefault(id, "未知用户"); } catch (NumberFormatException e) { return "未知用户"; } }) .collect(Collectors.joining(",")); record.setInspectorName(inspectorNames); } } } }