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
package cn.iocoder.yudao.module.mes.service.safety;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.inspection.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.SafetyInspectionDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.SafetyInspectionPointDO;
import cn.iocoder.yudao.module.mes.dal.mysql.safety.SafetyInspectionMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.safety.SafetyInspectionPointMapper;
import cn.iocoder.yudao.module.mes.enums.safety.SafetyInspectCycleEnum;
import cn.iocoder.yudao.module.mes.enums.safety.SafetyInspectionPointTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDateTime;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.SafetyErrorCodeConstants.*;
 
/**
 * 安全生产 - 巡检点位与巡检记录 Service
 *
 * <p>覆盖高压配电室、机组区等日常安全巡检。巡检发现异常时可直接转隐患上报。</p>
 *
 * @author 超级管理员
 */
@Service
@Validated
public class SafetyInspectionService {
 
    @Resource
    private SafetyInspectionPointMapper pointMapper;
    @Resource
    private SafetyInspectionMapper inspectionMapper;
    @Resource
    private SafetyRefResolver refResolver;
 
    // ==================== 巡检点位 ====================
 
    public Long createPoint(SafetyInspectionPointSaveReqVO reqVO) {
        reqVO.setId(null);
        String code = StrUtil.trim(reqVO.getCode());
        if (pointMapper.selectByCode(code) != null) {
            throw exception(SAFETY_POINT_CODE_DUPLICATE);
        }
        SafetyInspectionPointDO point = BeanUtils.toBean(reqVO, SafetyInspectionPointDO.class);
        point.setCode(code);
        point.setName(StrUtil.trim(reqVO.getName()));
        point.setInspectCycle(StrUtil.blankToDefault(reqVO.getInspectCycle(), SafetyInspectCycleEnum.DAILY.getCycle()));
        applyPointRefs(point, reqVO.getWorkshopId(), reqVO.getOwnerId());
        pointMapper.insert(point);
        return point.getId();
    }
 
    public void updatePoint(SafetyInspectionPointSaveReqVO reqVO) {
        SafetyInspectionPointDO old = validatePointExists(reqVO.getId());
        String code = StrUtil.trim(reqVO.getCode());
        SafetyInspectionPointDO byCode = pointMapper.selectByCode(code);
        if (byCode != null && !byCode.getId().equals(old.getId())) {
            throw exception(SAFETY_POINT_CODE_DUPLICATE);
        }
        SafetyInspectionPointDO point = BeanUtils.toBean(reqVO, SafetyInspectionPointDO.class);
        point.setCode(code);
        point.setName(StrUtil.trim(reqVO.getName()));
        applyPointRefs(point, reqVO.getWorkshopId(), reqVO.getOwnerId());
        pointMapper.updateById(point);
    }
 
    public void deletePoint(Long id) {
        validatePointExists(id);
        pointMapper.deleteById(id);
    }
 
    public SafetyInspectionPointDO getPoint(Long id) {
        return validatePointExists(id);
    }
 
    public PageResult<SafetyInspectionPointDO> getPointPage(SafetyInspectionPointPageReqVO reqVO) {
        return pointMapper.selectPage(reqVO);
    }
 
    /**
     * 启用中的巡检点位列表(移动端巡检选择用)
     */
    public List<SafetyInspectionPointDO> getEnabledPointList() {
        return pointMapper.selectListByStatus(0);
    }
 
    /**
     * 点位的主数据引用:车间 + 责任人,从主数据带出名称落快照
     */
    private void applyPointRefs(SafetyInspectionPointDO point, Long workshopId, Long ownerId) {
        point.setWorkshopId(workshopId);
        if (workshopId == null) {
            point.setWorkshopName("");
        } else {
            point.setWorkshopName(refResolver.requireWorkshop(workshopId).getName());
        }
        point.setOwnerId(ownerId);
        if (ownerId == null) {
            point.setOwnerName("");
        } else {
            point.setOwnerName(refResolver.requireEmployee(ownerId, SAFETY_EMPLOYEE_REQUIRED).getName());
        }
    }
 
    /**
     * 批量回填点位的车间名与责任人姓名(用主数据最新值覆盖快照)
     */
    public void fillPointRefs(List<SafetyInspectionPointDO> points) {
        if (CollUtil.isEmpty(points)) {
            return;
        }
        var workshopMap = refResolver.workshopMap(points.stream().map(SafetyInspectionPointDO::getWorkshopId).toList());
        var employeeMap = refResolver.employeeMap(points.stream().map(SafetyInspectionPointDO::getOwnerId).toList());
        points.forEach(point -> {
            var workshop = workshopMap.get(point.getWorkshopId());
            if (workshop != null) {
                point.setWorkshopName(workshop.getName());
            }
            var employee = employeeMap.get(point.getOwnerId());
            if (employee != null) {
                point.setOwnerName(employee.getName());
            }
        });
    }
 
    public static void fillPointDisplay(SafetyInspectionPointDO point, SafetyInspectionPointRespVO respVO) {
        respVO.setPointTypeName(SafetyInspectionPointTypeEnum.getNameByType(point.getPointType()));
        respVO.setInspectCycleName(SafetyInspectCycleEnum.getNameByCycle(point.getInspectCycle()));
    }
 
    // ==================== 巡检记录 ====================
 
    public Long createInspection(SafetyInspectionSaveReqVO reqVO) {
        reqVO.setId(null);
        SafetyInspectionPointDO point = validatePointExists(reqVO.getPointId());
        if (Integer.valueOf(1).equals(point.getStatus())) {
            throw exception(SAFETY_POINT_DISABLED);
        }
        SafetyInspectionDO inspection = buildInspection(reqVO, point);
        inspectionMapper.insert(inspection);
        return inspection.getId();
    }
 
    public void updateInspection(SafetyInspectionSaveReqVO reqVO) {
        SafetyInspectionDO old = validateInspectionExists(reqVO.getId());
        SafetyInspectionPointDO point = validatePointExists(reqVO.getPointId());
        SafetyInspectionDO inspection = buildInspection(reqVO, point);
        inspection.setId(old.getId());
        inspectionMapper.updateById(inspection);
    }
 
    public void deleteInspection(Long id) {
        validateInspectionExists(id);
        inspectionMapper.deleteById(id);
    }
 
    public SafetyInspectionDO getInspection(Long id) {
        return validateInspectionExists(id);
    }
 
    public PageResult<SafetyInspectionDO> getInspectionPage(SafetyInspectionPageReqVO reqVO) {
        return inspectionMapper.selectPage(reqVO);
    }
 
    private SafetyInspectionDO buildInspection(SafetyInspectionSaveReqVO reqVO, SafetyInspectionPointDO point) {
        SafetyInspectionDO inspection = BeanUtils.toBean(reqVO, SafetyInspectionDO.class);
        inspection.setPointId(point.getId());
        inspection.setPointName(point.getName());
        inspection.setPointType(point.getPointType());
        inspection.setInspectorId(reqVO.getInspectorId());
        inspection.setInspector(refResolver.requireEmployee(reqVO.getInspectorId(), SAFETY_EMPLOYEE_REQUIRED).getName());
        inspection.setResult(StrUtil.blankToDefault(reqVO.getResult(), "NORMAL"));
        inspection.setInspectTime(reqVO.getInspectTime() == null ? LocalDateTime.now() : reqVO.getInspectTime());
        inspection.setHazardCount(reqVO.getHazardCount() == null ? 0 : reqVO.getHazardCount());
        inspection.setRemark(reqVO.getRemark() == null ? "" : reqVO.getRemark().trim());
        return inspection;
    }
 
    /**
     * 批量回填巡检人最新姓名
     */
    public void fillInspectorNames(List<SafetyInspectionDO> records) {
        if (CollUtil.isEmpty(records)) {
            return;
        }
        var employeeMap = refResolver.employeeMap(records.stream().map(SafetyInspectionDO::getInspectorId).toList());
        records.forEach(record -> {
            var employee = employeeMap.get(record.getInspectorId());
            if (employee != null) {
                record.setInspector(employee.getName());
            }
        });
    }
 
    /** 最近一次巡检时间(用于点位列表展示) */
    public LocalDateTime getLastInspectTime(Long pointId) {
        SafetyInspectionDO latest = inspectionMapper.selectLatestByPointId(pointId);
        return latest == null ? null : latest.getInspectTime();
    }
 
    private SafetyInspectionPointDO validatePointExists(Long id) {
        SafetyInspectionPointDO point = id == null ? null : pointMapper.selectById(id);
        if (point == null) {
            throw exception(SAFETY_POINT_NOT_EXISTS);
        }
        return point;
    }
 
    private SafetyInspectionDO validateInspectionExists(Long id) {
        SafetyInspectionDO inspection = id == null ? null : inspectionMapper.selectById(id);
        if (inspection == null) {
            throw exception(SAFETY_INSPECTION_NOT_EXISTS);
        }
        return inspection;
    }
 
}