gongchunyi
2 小时以前 cfbff67bdc169c65569bac125fdccfd372bbfd60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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;
 
/**
 * <p>
 * 安全生产--隐患排查上报 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2026-01-28 11:10:54
 */
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class SafeHiddenServiceImpl extends ServiceImpl<SafeHiddenMapper, SafeHidden> 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<SafeHiddenDto> 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<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));
    }
}