package com.ruoyi.safe.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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;
/**
*
* 安全生产--隐患排查上报 服务实现类
*
*
* @author 芯导软件(江苏)有限公司
* @since 2026-01-28 11:10:54
*/
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class SafeHiddenServiceImpl extends ServiceImpl implements SafeHiddenService {
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 pageSafeHidden(Page page, SafeHiddenDto safeHiddenDto) {
return safeHiddenMapper.pageSafeHidden(page, safeHiddenDto);
}
@Override
public int add(SafeHidden safeHidden) {
safeHiddenMapper.insert(safeHidden);
String datePrefix = LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd"));
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(),
Arrays.asList(Long.valueOf(safeHidden.getRectifyUserId())),
"/safeProduction/dangerInvestigation?id=" + safeHidden.getId());
return 0;
}
@Override
public int delSafeHidden(List ids) {
if (ids == null || ids.isEmpty()) {
return 0;
}
List safeHiddens = safeHiddenMapper.selectBatchIds(ids);
List 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()
.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 ids) {
List recordIds = ids.stream()
.filter(Objects::nonNull)
.map(Integer::longValue)
.collect(Collectors.toList());
if (recordIds.isEmpty()) {
return;
}
List storageAttachments = storageAttachmentMapper.selectList(new LambdaQueryWrapper()
.eq(StorageAttachment::getRecordType, RecordTypeEnum.SAFE_HIDDEN.getType())
.in(StorageAttachment::getRecordId, recordIds));
if (!storageAttachments.isEmpty()) {
List storageAttachmentIds = storageAttachments.stream()
.map(StorageAttachment::getId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
List 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().in(SafeHiddenFile::getSafeHiddenId, ids));
}
}