gongchunyi
5 天以前 d2f07163fc4074f335986dd51eedbdc7be9e19ea
src/main/java/com/ruoyi/safe/service/impl/SafeLineInspectionHazardServiceImpl.java
@@ -2,16 +2,27 @@
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 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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;
@@ -29,17 +40,37 @@
    @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(safeLineInspectionHazard.getHazardCode() != null, SafeLineInspectionHazard::getHazardCode, safeLineInspectionHazard.getHazardCode())
                .eq(safeLineInspectionHazard.getStatus() != null, SafeLineInspectionHazard::getStatus, safeLineInspectionHazard.getStatus())
                .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
@@ -49,6 +80,8 @@
                .orderByDesc(SafeLineInspectionHazard::getCreateTime)
                .list();
        fillRectifyUserName(records);
        fillInspectionInfo(records);
        fillRectifyAttachments(records);
        return records;
    }
@@ -62,12 +95,65 @@
    @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())) {
            Long currentUserId = SecurityUtils.getUserId();
            entity.setRectifyUserId(currentUserId.intValue());
            validateRectify(entity, oldRecord);
            entity.setRectifyUserId(oldRecord.getRectifyUserId());
            entity.setRectifyTime(LocalDateTime.now());
        }
        return super.updateById(entity);
        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() {
@@ -96,4 +182,66 @@
            }
        });
    }
    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);
    }
}