gongchunyi
5 天以前 64de92ae028b582b7fbe8d3c84eb568edf0d400f
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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);
    }
}