package com.ruoyi.safe.service.impl;
|
|
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.ruoyi.basic.dto.StorageBlobVO;
|
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.common.utils.StringUtils;
|
import com.ruoyi.project.system.domain.SysUser;
|
import com.ruoyi.project.system.mapper.SysUserMapper;
|
import com.ruoyi.safe.mapper.SafeLineInspectionHazardMapper;
|
import com.ruoyi.safe.mapper.SafeLineInspectionMapper;
|
import com.ruoyi.safe.pojo.SafeHidden;
|
import com.ruoyi.safe.pojo.SafeLineInspection;
|
import com.ruoyi.safe.pojo.SafeLineInspectionHazard;
|
import com.ruoyi.safe.service.SafeHiddenService;
|
import com.ruoyi.safe.service.SafeLineInspectionHazardService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.io.Serializable;
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 安全生产--线路巡检隐患 服务实现类
|
* </p>
|
*
|
* @author 芯导软件(江苏)有限公司
|
* @since 2026-06-29
|
*/
|
@Service
|
public class SafeLineInspectionHazardServiceImpl extends ServiceImpl<SafeLineInspectionHazardMapper, SafeLineInspectionHazard> implements SafeLineInspectionHazardService {
|
|
@Autowired
|
private SysUserMapper sysUserMapper;
|
@Autowired
|
private SafeHiddenService safeHiddenService;
|
@Autowired
|
private SafeLineInspectionMapper safeLineInspectionMapper;
|
@Autowired
|
private FileUtil fileUtil;
|
|
@Override
|
public IPage<SafeLineInspectionHazard> pageSafeLineInspectionHazard(Page page, SafeLineInspectionHazard safeLineInspectionHazard) {
|
IPage<SafeLineInspectionHazard> result = this.lambdaQuery()
|
.eq(safeLineInspectionHazard.getInspectionId() != null, SafeLineInspectionHazard::getInspectionId, safeLineInspectionHazard.getInspectionId())
|
.like(StringUtils.isNotBlank(safeLineInspectionHazard.getHazardCode()), SafeLineInspectionHazard::getHazardCode, safeLineInspectionHazard.getHazardCode())
|
.like(StringUtils.isNotBlank(safeLineInspectionHazard.getHazardDesc()), SafeLineInspectionHazard::getHazardDesc, safeLineInspectionHazard.getHazardDesc())
|
.eq(StringUtils.isNotBlank(safeLineInspectionHazard.getStatus()), SafeLineInspectionHazard::getStatus, safeLineInspectionHazard.getStatus())
|
.orderByDesc(SafeLineInspectionHazard::getCreateTime)
|
.page(page);
|
fillRectifyUserName(result.getRecords());
|
fillInspectionInfo(result.getRecords());
|
fillRectifyAttachments(result.getRecords());
|
return result;
|
}
|
|
@Override
|
public SafeLineInspectionHazard getById(Serializable id) {
|
SafeLineInspectionHazard record = super.getById(id);
|
if (record != null) {
|
fillRectifyUserName(Collections.singletonList(record));
|
fillInspectionInfo(Collections.singletonList(record));
|
fillRectifyAttachments(Collections.singletonList(record));
|
}
|
return record;
|
}
|
|
@Override
|
public List<SafeLineInspectionHazard> listByInspectionId(Integer inspectionId) {
|
List<SafeLineInspectionHazard> records = this.lambdaQuery()
|
.eq(SafeLineInspectionHazard::getInspectionId, inspectionId)
|
.orderByDesc(SafeLineInspectionHazard::getCreateTime)
|
.list();
|
fillRectifyUserName(records);
|
fillInspectionInfo(records);
|
fillRectifyAttachments(records);
|
return records;
|
}
|
|
@Override
|
public boolean save(SafeLineInspectionHazard entity) {
|
if (entity.getHazardCode() == null || entity.getHazardCode().isEmpty()) {
|
entity.setHazardCode(generateHazardCode());
|
}
|
return super.save(entity);
|
}
|
|
@Override
|
public boolean updateById(SafeLineInspectionHazard entity) {
|
SafeLineInspectionHazard oldRecord = entity.getId() == null ? null : this.getById(entity.getId());
|
if (oldRecord == null) {
|
throw new IllegalArgumentException("整改记录不存在");
|
}
|
Integer safeHiddenId = entity.getSafeHiddenId() != null
|
? entity.getSafeHiddenId()
|
: oldRecord.getSafeHiddenId();
|
if ("已整改".equals(entity.getStatus())) {
|
validateRectify(entity, oldRecord);
|
entity.setRectifyUserId(oldRecord.getRectifyUserId());
|
entity.setRectifyTime(LocalDateTime.now());
|
}
|
boolean updated = super.updateById(entity);
|
if (updated && "已整改".equals(entity.getStatus())) {
|
syncRectifyAttachments(entity);
|
}
|
if (updated && "已整改".equals(entity.getStatus()) && safeHiddenId != null) {
|
SafeHidden hidden = new SafeHidden();
|
hidden.setId(safeHiddenId);
|
hidden.setRectifyMeasures(entity.getRectifyDesc());
|
hidden.setRectifyActualTime(LocalDate.now());
|
safeHiddenService.updateById(hidden);
|
}
|
if (updated && "已整改".equals(entity.getStatus())) {
|
Integer inspectionId = entity.getInspectionId() != null
|
? entity.getInspectionId()
|
: oldRecord.getInspectionId();
|
completeInspectionIfAllRectified(inspectionId);
|
}
|
return updated;
|
}
|
|
private void validateRectify(SafeLineInspectionHazard entity, SafeLineInspectionHazard oldRecord) {
|
if ("已整改".equals(oldRecord.getStatus())) {
|
throw new IllegalArgumentException("该隐患已整改,请勿重复操作");
|
}
|
Long currentUserId = SecurityUtils.getUserId();
|
if (oldRecord.getRectifyUserId() == null || currentUserId == null
|
|| !oldRecord.getRectifyUserId().equals(currentUserId.intValue())) {
|
throw new IllegalArgumentException("只有整改责任人可以进行整改");
|
}
|
if (StringUtils.isBlank(entity.getRectifyDesc())) {
|
throw new IllegalArgumentException("请输入整改说明");
|
}
|
if (entity.getStorageBlobDTOs() == null || entity.getStorageBlobDTOs().isEmpty()) {
|
throw new IllegalArgumentException("请上传整改后照片");
|
}
|
}
|
|
private void syncRectifyAttachments(SafeLineInspectionHazard entity) {
|
if (entity.getId() == null || entity.getStorageBlobDTOs() == null) {
|
return;
|
}
|
fileUtil.saveStorageAttachment(
|
ApplicationTypeEnum.IMAGE,
|
RecordTypeEnum.SAFE_LINE_INSPECTION_HAZARD,
|
entity.getId().longValue(),
|
entity.getStorageBlobDTOs()
|
);
|
}
|
|
private String generateHazardCode() {
|
long count = this.count() + 1;
|
return "YH" + String.format("%05d", count);
|
}
|
|
private void fillRectifyUserName(List<SafeLineInspectionHazard> records) {
|
if (records == null || records.isEmpty()) {
|
return;
|
}
|
List<Long> userIds = records.stream()
|
.map(SafeLineInspectionHazard::getRectifyUserId)
|
.filter(Objects::nonNull)
|
.map(Integer::longValue)
|
.distinct()
|
.collect(Collectors.toList());
|
if (userIds.isEmpty()) {
|
return;
|
}
|
Map<Long, String> userMap = sysUserMapper.selectUserByIds(userIds).stream()
|
.collect(Collectors.toMap(SysUser::getUserId, SysUser::getNickName, (v1, v2) -> v1));
|
records.forEach(record -> {
|
if (record.getRectifyUserId() != null) {
|
record.setRectifyUserName(userMap.getOrDefault(record.getRectifyUserId().longValue(), "未知用户"));
|
}
|
});
|
}
|
|
private void fillInspectionInfo(List<SafeLineInspectionHazard> records) {
|
if (records == null || records.isEmpty()) {
|
return;
|
}
|
List<Integer> inspectionIds = records.stream()
|
.map(SafeLineInspectionHazard::getInspectionId)
|
.filter(Objects::nonNull)
|
.distinct()
|
.collect(Collectors.toList());
|
if (inspectionIds.isEmpty()) {
|
return;
|
}
|
Map<Integer, SafeLineInspection> inspectionMap = safeLineInspectionMapper.selectBatchIds(inspectionIds).stream()
|
.collect(Collectors.toMap(SafeLineInspection::getId, item -> item, (v1, v2) -> v1));
|
records.forEach(record -> {
|
SafeLineInspection inspection = inspectionMap.get(record.getInspectionId());
|
if (inspection != null) {
|
record.setInspectionCode(inspection.getInspectionCode());
|
record.setInspectionName(inspection.getInspectionName());
|
record.setLineName(inspection.getLineName());
|
}
|
});
|
}
|
|
private void fillRectifyAttachments(List<SafeLineInspectionHazard> records) {
|
if (records == null || records.isEmpty()) {
|
return;
|
}
|
records.forEach(record -> {
|
if (record.getId() == null) {
|
return;
|
}
|
List<StorageBlobVO> attachments = fileUtil.getStorageBlobVOsByRecordTypeAndRecordId(
|
RecordTypeEnum.SAFE_LINE_INSPECTION_HAZARD,
|
record.getId().longValue()
|
);
|
record.setStorageBlobVOs(attachments == null ? Collections.emptyList() : attachments);
|
});
|
}
|
|
private void completeInspectionIfAllRectified(Integer inspectionId) {
|
if (inspectionId == null) {
|
return;
|
}
|
long unrectifiedCount = this.lambdaQuery()
|
.eq(SafeLineInspectionHazard::getInspectionId, inspectionId)
|
.ne(SafeLineInspectionHazard::getStatus, "已整改")
|
.count();
|
if (unrectifiedCount > 0) {
|
return;
|
}
|
SafeLineInspection inspection = safeLineInspectionMapper.selectById(inspectionId);
|
if (inspection == null || "已完成".equals(inspection.getStatus())) {
|
return;
|
}
|
Long currentUserId = SecurityUtils.getUserId();
|
inspection.setStatus("已完成");
|
inspection.setUpdateTime(LocalDateTime.now());
|
inspection.setUpdateUser(currentUserId == null ? null : currentUserId.intValue());
|
safeLineInspectionMapper.updateById(inspection);
|
}
|
}
|