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
248
249
250
251
252
253
254
255
256
257
258
package com.ruoyi.safe.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.project.system.domain.SysUser;
import com.ruoyi.safe.dto.SafeLineInspectionExecuteDto;
import com.ruoyi.safe.pojo.SafeLineInspection;
import com.ruoyi.safe.mapper.SafeLineInspectionMapper;
import com.ruoyi.safe.pojo.SafeLineInspectionHazard;
import com.ruoyi.safe.pojo.SafeLineInspectionRecord;
import com.ruoyi.safe.pojo.SafeHidden;
import com.ruoyi.safe.service.SafeHiddenService;
import com.ruoyi.safe.service.SafeLineInspectionHazardService;
import com.ruoyi.safe.service.SafeLineInspectionRecordService;
import com.ruoyi.safe.service.SafeLineInspectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 安全生产--线路巡检任务 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2026-06-29
 */
@Service
public class SafeLineInspectionServiceImpl extends ServiceImpl<SafeLineInspectionMapper, SafeLineInspection> implements SafeLineInspectionService {
 
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SafeLineInspectionRecordService safeLineInspectionRecordService;
    @Autowired
    private SafeLineInspectionHazardService safeLineInspectionHazardService;
    @Autowired
    private SafeHiddenService safeHiddenService;
    @Autowired
    private FileUtil fileUtil;
 
    @Override
    public IPage<SafeLineInspection> pageSafeLineInspection(Page page, SafeLineInspection safeLineInspection) {
        IPage<SafeLineInspection> result = this.lambdaQuery()
                .eq(safeLineInspection.getScheduleTaskId() != null, SafeLineInspection::getScheduleTaskId, safeLineInspection.getScheduleTaskId())
                .like(StringUtils.isNotBlank(safeLineInspection.getInspectionCode()), SafeLineInspection::getInspectionCode, safeLineInspection.getInspectionCode())
                .like(StringUtils.isNotBlank(safeLineInspection.getInspectionName()), SafeLineInspection::getInspectionName, safeLineInspection.getInspectionName())
                .like(StringUtils.isNotBlank(safeLineInspection.getLineName()), SafeLineInspection::getLineName, safeLineInspection.getLineName())
                .like(StringUtils.isNotBlank(safeLineInspection.getInspectionProject()), SafeLineInspection::getInspectionProject, safeLineInspection.getInspectionProject())
                .eq(StringUtils.isNotBlank(safeLineInspection.getStatus()), SafeLineInspection::getStatus, safeLineInspection.getStatus())
                .orderByDesc(SafeLineInspection::getCreateTime)
                .page(page);
 
        // 关联查询巡检人名称
        fillInspectorName(result.getRecords());
 
        return result;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean executeInspection(SafeLineInspectionExecuteDto executeDto) {
        if (executeDto == null || executeDto.getInspectionId() == null) {
            throw new IllegalArgumentException("巡检记录不能为空");
        }
        SafeLineInspection inspection = this.getById(executeDto.getInspectionId());
        if (inspection == null) {
            throw new IllegalArgumentException("巡检记录不存在");
        }
        if ("已完成".equals(inspection.getStatus())) {
            throw new IllegalArgumentException("巡检记录已完成,请勿重复执行");
        }
        if (!"正常".equals(executeDto.getCheckResult()) && !"异常".equals(executeDto.getCheckResult())) {
            throw new IllegalArgumentException("请选择巡检结果");
        }
 
        LocalDateTime checkTime = executeDto.getCheckTime() == null ? LocalDateTime.now() : executeDto.getCheckTime();
        SafeLineInspectionRecord record = buildInspectionRecord(inspection, executeDto, checkTime);
        safeLineInspectionRecordService.save(record);
 
        inspection.setInspectionResult(executeDto.getCheckResult());
        inspection.setActualInspectionTime(checkTime);
        inspection.setUpdateTime(LocalDateTime.now());
        inspection.setUpdateUser(SecurityUtils.getUserId().intValue());
        appendCurrentInspector(inspection);
 
        if ("异常".equals(executeDto.getCheckResult())) {
            validateHazard(executeDto);
            SafeHidden hidden = createSafeHidden(executeDto);
            safeHiddenService.add(hidden);
            fileUtil.saveStorageAttachment(
                    ApplicationTypeEnum.IMAGE,
                    RecordTypeEnum.SAFE_HIDDEN,
                    hidden.getId().longValue(),
                    executeDto.getStorageBlobDTOs()
            );
 
            SafeLineInspectionHazard hazard = buildLineHazard(inspection, record, hidden, executeDto);
            safeLineInspectionHazardService.save(hazard);
 
            inspection.setHiddenId(hidden.getId());
            inspection.setStatus("待整改");
        } else {
            inspection.setHiddenId(null);
            inspection.setStatus("已完成");
        }
 
        return this.updateById(inspection);
    }
 
    private SafeLineInspectionRecord buildInspectionRecord(SafeLineInspection inspection,
                                                           SafeLineInspectionExecuteDto executeDto,
                                                           LocalDateTime checkTime) {
        SafeLineInspectionRecord record = new SafeLineInspectionRecord();
        record.setInspectionId(inspection.getId());
        record.setCheckPoint(inspection.getLineName());
        record.setCheckContent(inspection.getInspectionProject());
        record.setCheckResult(executeDto.getCheckResult());
        record.setCheckDesc("异常".equals(executeDto.getCheckResult()) ? null : executeDto.getCheckDesc());
        record.setCheckTime(checkTime);
        record.setStorageBlobDTOs(executeDto.getStorageBlobDTOs());
        return record;
    }
 
    private void validateHazard(SafeLineInspectionExecuteDto executeDto) {
        if (executeDto.getStorageBlobDTOs() == null || executeDto.getStorageBlobDTOs().isEmpty()) {
            throw new IllegalArgumentException("巡检异常时请上传异常照片");
        }
        if (StringUtils.isBlank(executeDto.getHiddenType())) {
            throw new IllegalArgumentException("请选择隐患类型");
        }
        if (StringUtils.isBlank(executeDto.getRiskLevel())) {
            throw new IllegalArgumentException("请选择隐患风险等级");
        }
        if (StringUtils.isBlank(executeDto.getHazardLocation())) {
            throw new IllegalArgumentException("请输入隐患位置");
        }
        if (StringUtils.isBlank(executeDto.getHazardDesc())) {
            throw new IllegalArgumentException("请输入异常描述/整改要求");
        }
        if (executeDto.getRectifyUserId() == null) {
            throw new IllegalArgumentException("请选择整改责任人");
        }
        if (executeDto.getRectifyTime() == null) {
            throw new IllegalArgumentException("请选择整改完成期限");
        }
    }
 
    private SafeHidden createSafeHidden(SafeLineInspectionExecuteDto executeDto) {
        SafeHidden hidden = new SafeHidden();
        hidden.setType(executeDto.getHiddenType());
        hidden.setRiskLevel(executeDto.getRiskLevel());
        hidden.setLocation(executeDto.getHazardLocation());
        hidden.setHiddenDesc(executeDto.getHazardDesc());
        hidden.setRectifyUserId(executeDto.getRectifyUserId());
        hidden.setRectifyUserMobile(executeDto.getRectifyUserMobile());
        hidden.setRectifyTime(executeDto.getRectifyTime());
        return hidden;
    }
 
    private SafeLineInspectionHazard buildLineHazard(SafeLineInspection inspection,
                                                     SafeLineInspectionRecord record,
                                                     SafeHidden hidden,
                                                     SafeLineInspectionExecuteDto executeDto) {
        SafeLineInspectionHazard hazard = new SafeLineInspectionHazard();
        hazard.setInspectionId(inspection.getId());
        hazard.setRecordId(record.getId());
        hazard.setSafeHiddenId(hidden.getId());
        hazard.setHazardCode(hidden.getHiddenCode());
        hazard.setHazardDesc(executeDto.getHazardDesc());
        hazard.setHazardLevel(executeDto.getRiskLevel());
        hazard.setHazardLocation(executeDto.getHazardLocation());
        hazard.setStatus("待整改");
        hazard.setRectifyUserId(executeDto.getRectifyUserId());
        hazard.setRectifyDeadline(executeDto.getRectifyTime());
        return hazard;
    }
 
    private void appendCurrentInspector(SafeLineInspection inspection) {
        Long currentUserId = SecurityUtils.getUserId();
        if (currentUserId == null) {
            return;
        }
        String currentUserIdStr = String.valueOf(currentUserId);
        String inspectorId = inspection.getInspectorId();
        if (StringUtils.isBlank(inspectorId)) {
            inspection.setInspectorId(currentUserIdStr);
            return;
        }
        boolean exists = Arrays.stream(inspectorId.split(","))
                .map(String::trim)
                .anyMatch(currentUserIdStr::equals);
        if (!exists) {
            inspection.setInspectorId(inspectorId + "," + currentUserIdStr);
        }
    }
 
    /**
     * 填充巡检人名称(支持多巡检人,逗号分隔)
     */
    private void fillInspectorName(List<SafeLineInspection> records) {
        if (records == null || records.isEmpty()) {
            return;
        }
 
        // 收集所有巡检人ID(处理逗号分隔的多个ID)
        List<Long> allInspectorIds = records.stream()
                .map(SafeLineInspection::getInspectorId)
                .filter(ids -> ids != null && !ids.isEmpty())
                .flatMap(ids -> Arrays.stream(ids.split(",")))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .map(Long::parseLong)
                .distinct()
                .collect(Collectors.toList());
 
        if (allInspectorIds.isEmpty()) {
            return;
        }
 
        // 批量查询用户信息
        List<SysUser> users = sysUserMapper.selectUserByIds(allInspectorIds);
        Map<Long, String> userMap = users.stream()
                .collect(Collectors.toMap(SysUser::getUserId, SysUser::getNickName, (v1, v2) -> v1));
 
        // 设置巡检人名称
        for (SafeLineInspection record : records) {
            if (record.getInspectorId() != null && !record.getInspectorId().isEmpty()) {
                String inspectorNames = Arrays.stream(record.getInspectorId().split(","))
                        .map(String::trim)
                        .filter(s -> !s.isEmpty())
                        .map(idStr -> {
                            try {
                                Long id = Long.parseLong(idStr);
                                return userMap.getOrDefault(id, "未知用户");
                            } catch (NumberFormatException e) {
                                return "未知用户";
                            }
                        })
                        .collect(Collectors.joining(","));
                record.setInspectorName(inspectorNames);
            }
        }
    }
}