xiaoyi
2026-10-29 2af6d5d996786d9b6b859b748af2ab9b71ad23e1
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
package cn.iocoder.yudao.module.mes.service.safety;
 
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.train.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.SafetyAccidentDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.SafetyDrillDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.SafetyTrainingDO;
import cn.iocoder.yudao.module.mes.dal.mysql.safety.SafetyAccidentMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.safety.SafetyDrillMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.safety.SafetyTrainingMapper;
import cn.iocoder.yudao.module.mes.enums.safety.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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 SafetyTrainingService {
 
    private static final DateTimeFormatter CODE_DATE = DateTimeFormatter.ofPattern("yyyyMMdd");
 
    @Resource
    private SafetyTrainingMapper trainingMapper;
    @Resource
    private SafetyDrillMapper drillMapper;
    @Resource
    private SafetyAccidentMapper accidentMapper;
 
    // ==================== 培训 ====================
 
    public Long createTraining(SafetyTrainingSaveReqVO reqVO) {
        reqVO.setId(null);
        SafetyTrainingDO training = BeanUtils.toBean(reqVO, SafetyTrainingDO.class);
        training.setTitle(StrUtil.trim(reqVO.getTitle()));
        training.setTrainingType(StrUtil.blankToDefault(reqVO.getTrainingType(), SafetyTrainingTypeEnum.SAFETY.getType()));
        training.setParticipantCount(resolveCount(reqVO.getParticipantCount(), reqVO.getParticipants()));
        training.setRemark(reqVO.getRemark() == null ? "" : reqVO.getRemark().trim());
        trainingMapper.insert(training);
        return training.getId();
    }
 
    public void updateTraining(SafetyTrainingSaveReqVO reqVO) {
        validateTrainingExists(reqVO.getId());
        SafetyTrainingDO training = BeanUtils.toBean(reqVO, SafetyTrainingDO.class);
        training.setTitle(StrUtil.trim(reqVO.getTitle()));
        training.setParticipantCount(resolveCount(reqVO.getParticipantCount(), reqVO.getParticipants()));
        trainingMapper.updateById(training);
    }
 
    public void deleteTraining(Long id) {
        validateTrainingExists(id);
        trainingMapper.deleteById(id);
    }
 
    public SafetyTrainingDO getTraining(Long id) {
        return validateTrainingExists(id);
    }
 
    public PageResult<SafetyTrainingDO> getTrainingPage(SafetyTrainingPageReqVO reqVO) {
        return trainingMapper.selectPage(reqVO);
    }
 
    public static void fillTrainingDisplay(SafetyTrainingDO training, SafetyTrainingRespVO respVO) {
        respVO.setTrainingTypeName(SafetyTrainingTypeEnum.getNameByType(training.getTrainingType()));
    }
 
    // ==================== 演练 ====================
 
    public Long createDrill(SafetyDrillSaveReqVO reqVO) {
        reqVO.setId(null);
        SafetyDrillDO drill = BeanUtils.toBean(reqVO, SafetyDrillDO.class);
        drill.setTitle(StrUtil.trim(reqVO.getTitle()));
        drill.setDrillType(StrUtil.blankToDefault(reqVO.getDrillType(), SafetyDrillTypeEnum.FIRE.getType()));
        drill.setParticipantCount(resolveCount(reqVO.getParticipantCount(), reqVO.getParticipants()));
        drill.setRemark(reqVO.getRemark() == null ? "" : reqVO.getRemark().trim());
        drillMapper.insert(drill);
        return drill.getId();
    }
 
    public void updateDrill(SafetyDrillSaveReqVO reqVO) {
        validateDrillExists(reqVO.getId());
        SafetyDrillDO drill = BeanUtils.toBean(reqVO, SafetyDrillDO.class);
        drill.setTitle(StrUtil.trim(reqVO.getTitle()));
        drill.setParticipantCount(resolveCount(reqVO.getParticipantCount(), reqVO.getParticipants()));
        drillMapper.updateById(drill);
    }
 
    public void deleteDrill(Long id) {
        validateDrillExists(id);
        drillMapper.deleteById(id);
    }
 
    public SafetyDrillDO getDrill(Long id) {
        return validateDrillExists(id);
    }
 
    public PageResult<SafetyDrillDO> getDrillPage(SafetyDrillPageReqVO reqVO) {
        return drillMapper.selectPage(reqVO);
    }
 
    public static void fillDrillDisplay(SafetyDrillDO drill, SafetyDrillRespVO respVO) {
        respVO.setDrillTypeName(SafetyDrillTypeEnum.getNameByType(drill.getDrillType()));
    }
 
    // ==================== 事故 ====================
 
    public Long createAccident(SafetyAccidentSaveReqVO reqVO) {
        reqVO.setId(null);
        SafetyAccidentDO accident = BeanUtils.toBean(reqVO, SafetyAccidentDO.class);
        accident.setCode(accidentMapper.selectNextCode("AC" + LocalDate.now().format(CODE_DATE)));
        accident.setTitle(StrUtil.trim(reqVO.getTitle()));
        accident.setLevel(StrUtil.blankToDefault(reqVO.getLevel(), SafetyAccidentLevelEnum.GENERAL.getLevel()));
        accident.setStatus(StrUtil.blankToDefault(reqVO.getStatus(), SafetyAccidentStatusEnum.REPORTED.getStatus()));
        accident.setReportTime(reqVO.getReportTime() == null ? LocalDateTime.now() : reqVO.getReportTime());
        accident.setRemark(reqVO.getRemark() == null ? "" : reqVO.getRemark().trim());
        accidentMapper.insert(accident);
        return accident.getId();
    }
 
    public void updateAccident(SafetyAccidentSaveReqVO reqVO) {
        validateAccidentExists(reqVO.getId());
        SafetyAccidentDO accident = BeanUtils.toBean(reqVO, SafetyAccidentDO.class);
        accident.setCode(null);
        accidentMapper.updateById(accident);
    }
 
    public void deleteAccident(Long id) {
        validateAccidentExists(id);
        accidentMapper.deleteById(id);
    }
 
    public SafetyAccidentDO getAccident(Long id) {
        return validateAccidentExists(id);
    }
 
    public PageResult<SafetyAccidentDO> getAccidentPage(SafetyAccidentPageReqVO reqVO) {
        return accidentMapper.selectPage(reqVO);
    }
 
    public static void fillAccidentDisplay(SafetyAccidentDO accident, SafetyAccidentRespVO respVO) {
        respVO.setAccidentTypeName(SafetyAccidentTypeEnum.getNameByType(accident.getAccidentType()));
        respVO.setLevelName(SafetyAccidentLevelEnum.getNameByLevel(accident.getLevel()));
        respVO.setStatusName(SafetyAccidentStatusEnum.getNameByStatus(accident.getStatus()));
    }
 
    // ==================== 台账导出用查询 ====================
 
    public List<SafetyTrainingDO> listTrainingBetween(LocalDate begin, LocalDate end) {
        return trainingMapper.selectListBetween(begin, end);
    }
 
    public List<SafetyDrillDO> listDrillBetween(LocalDate begin, LocalDate end) {
        return drillMapper.selectListBetween(begin, end);
    }
 
    public List<SafetyAccidentDO> listAccidentBetween(LocalDate begin, LocalDate end) {
        return accidentMapper.selectListBetween(
                begin == null ? null : begin.atStartOfDay(),
                end == null ? null : end.atTime(23, 59, 59));
    }
 
    // ==================== 私有 ====================
 
    /** 参加人数未填时,按姓名逗号分隔数量推断 */
    private static int resolveCount(Integer count, String participants) {
        if (count != null && count > 0) {
            return count;
        }
        if (StrUtil.isBlank(participants)) {
            return 0;
        }
        return (int) java.util.Arrays.stream(participants.split("[,,、]"))
                .map(String::trim).filter(StrUtil::isNotBlank).count();
    }
 
    private SafetyTrainingDO validateTrainingExists(Long id) {
        SafetyTrainingDO training = id == null ? null : trainingMapper.selectById(id);
        if (training == null) {
            throw exception(SAFETY_TRAINING_NOT_EXISTS);
        }
        return training;
    }
 
    private SafetyDrillDO validateDrillExists(Long id) {
        SafetyDrillDO drill = id == null ? null : drillMapper.selectById(id);
        if (drill == null) {
            throw exception(SAFETY_DRILL_NOT_EXISTS);
        }
        return drill;
    }
 
    private SafetyAccidentDO validateAccidentExists(Long id) {
        SafetyAccidentDO accident = id == null ? null : accidentMapper.selectById(id);
        if (accident == null) {
            throw exception(SAFETY_ACCIDENT_NOT_EXISTS);
        }
        return accident;
    }
 
}