gongchunyi
7 小时以前 cfbff67bdc169c65569bac125fdccfd372bbfd60
src/main/java/com/ruoyi/safe/service/impl/SafeHiddenServiceImpl.java
@@ -4,21 +4,30 @@
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.enums.RecordTypeEnum;
import com.ruoyi.basic.mapper.StorageAttachmentMapper;
import com.ruoyi.basic.mapper.StorageBlobMapper;
import com.ruoyi.basic.pojo.StorageAttachment;
import com.ruoyi.project.system.domain.SysNotice;
import com.ruoyi.project.system.service.ISysDictDataService;
import com.ruoyi.project.system.service.ISysNoticeService;
import com.ruoyi.safe.dto.SafeHiddenDto;
import com.ruoyi.safe.mapper.SafeHiddenFileMapper;
import com.ruoyi.safe.mapper.SafeHiddenMapper;
import com.ruoyi.safe.pojo.SafeHidden;
import com.ruoyi.safe.pojo.SafeHiddenFile;
import com.ruoyi.safe.service.SafeHiddenService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
 * <p>
@@ -35,9 +44,10 @@
    private final SafeHiddenMapper safeHiddenMapper;
    private final ISysNoticeService sysNoticeService;
    private final ISysDictDataService sysDictDataService;
    private final StorageAttachmentMapper storageAttachmentMapper;
    private final StorageBlobMapper storageBlobMapper;
    private final SafeHiddenFileMapper safeHiddenFileMapper;
    @Override
    public IPage<SafeHiddenDto> pageSafeHidden(Page page, SafeHiddenDto safeHiddenDto) {
@@ -51,15 +61,14 @@
        String no = "YH" + String.format("%s%03d", datePrefix, safeHidden.getId());
        safeHidden.setHiddenCode(no);
        safeHiddenMapper.updateById(safeHidden);
        //获取隐患类型的字典
        String type = sysDictDataService.selectDictLabel("hidden_danger_type", safeHidden.getType());
        //消息通知
        sysNoticeService.simpleNoticeByUser("隐患排查上报整改",
                "隐患类型:"+type+"\t"+
                        "隐患风险等级:"+safeHidden.getRiskLevel()+"\t"+
                        "隐患具体位置:"+safeHidden.getLocation()+"\t"+
                        "隐患描述:"+safeHidden.getHiddenDesc()+"\t"+
                        "整改完成期限(日期):"+safeHidden.getRectifyTime(),
                "隐患类型:" + type + "\t" +
                        "隐患风险等级:" + safeHidden.getRiskLevel() + "\t" +
                        "隐患具体位置:" + safeHidden.getLocation() + "\t" +
                        "隐患描述:" + safeHidden.getHiddenDesc() + "\t" +
                        "整改完成期限(日期):" + safeHidden.getRectifyTime(),
                Arrays.asList(Long.valueOf(safeHidden.getRectifyUserId())),
                "/safeProduction/dangerInvestigation?id=" + safeHidden.getId());
        return 0;
@@ -67,15 +76,68 @@
    @Override
    public int delSafeHidden(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return 0;
        }
        List<SafeHidden> safeHiddens = safeHiddenMapper.selectBatchIds(ids);
        List<String> rectifiedHiddenCodes = safeHiddens.stream()
                .filter(this::isRectified)
                .map(item -> StringUtils.hasText(item.getHiddenCode()) ? item.getHiddenCode() : String.valueOf(item.getId()))
                .collect(Collectors.toList());
        if (!rectifiedHiddenCodes.isEmpty()) {
            throw new RuntimeException("已整改的隐患不能删除:" + String.join("、", rectifiedHiddenCodes));
        }
        for (SafeHidden safeHidden : safeHiddens) {
            //  删除对应的消息通知
            sysNoticeService.remove(new LambdaQueryWrapper<SysNotice>()
                    .eq(SysNotice::getNoticeTitle, "隐患排查上报整改")
                    .eq(SysNotice::getSenderId, safeHidden.getCreateUser())
                    .apply("CAST(notice_content AS CHAR) LIKE CONCAT('%', {0}, '%')", safeHidden.getId()));
        }
        deleteAttachments(ids);
        safeHiddenMapper.deleteBatchIds(ids);
        return 0;
    }
    private boolean isRectified(SafeHidden safeHidden) {
        return safeHidden.getRectifyActualTime() != null
                || StringUtils.hasText(safeHidden.getRectifyMeasures())
                || safeHidden.getVerifyTime() != null
                || StringUtils.hasText(safeHidden.getVerifyResult())
                || StringUtils.hasText(safeHidden.getVerifyRemark());
    }
    private void deleteAttachments(List<Integer> ids) {
        List<Long> recordIds = ids.stream()
                .filter(Objects::nonNull)
                .map(Integer::longValue)
                .collect(Collectors.toList());
        if (recordIds.isEmpty()) {
            return;
        }
        List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>()
                .eq(StorageAttachment::getRecordType, RecordTypeEnum.SAFE_HIDDEN.getType())
                .in(StorageAttachment::getRecordId, recordIds));
        if (!storageAttachments.isEmpty()) {
            List<Long> storageAttachmentIds = storageAttachments.stream()
                    .map(StorageAttachment::getId)
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
            List<Long> storageBlobIds = storageAttachments.stream()
                    .map(StorageAttachment::getStorageBlobId)
                    .filter(Objects::nonNull)
                    .distinct()
                    .collect(Collectors.toList());
            if (!storageAttachmentIds.isEmpty()) {
                storageAttachmentMapper.deleteByIds(storageAttachmentIds);
            }
            if (!storageBlobIds.isEmpty()) {
                storageBlobMapper.deleteByIds(storageBlobIds);
            }
        }
        safeHiddenFileMapper.delete(new LambdaQueryWrapper<SafeHiddenFile>().in(SafeHiddenFile::getSafeHiddenId, ids));
    }
}