xiaoyi
6 天以前 9daeb6eac6e46710055e625bbbd56331b8890e21
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
259
260
261
262
263
264
265
package cn.iocoder.yudao.module.mes.service.dv.checkrecord;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
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.dv.checkrecord.vo.MesDvCheckRecordPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.checkrecord.vo.MesDvCheckRecordSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.checkplan.MesDvCheckPlanSubjectDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.checkrecord.MesDvCheckRecordDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.checkrecord.MesDvCheckRecordLineDO;
import cn.iocoder.yudao.module.mes.enums.dv.MesDvCheckRecordStatusEnum;
import cn.iocoder.yudao.module.mes.enums.dv.MesDvCheckPlanTypeEnum;
import cn.iocoder.yudao.module.mes.enums.dv.MesDvCheckResultEnum;
import cn.iocoder.yudao.module.mes.dal.mysql.dv.checkrecord.MesDvCheckRecordMapper;
import cn.iocoder.yudao.module.mes.service.dv.checkplan.MesDvCheckPlanService;
import cn.iocoder.yudao.module.mes.service.dv.checkplan.MesDvCheckPlanSubjectService;
import cn.iocoder.yudao.module.mes.service.dv.machinery.MesDvMachineryService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.ArrayList;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
import cn.iocoder.yudao.module.mes.controller.admin.dv.checkrecord.vo.MesDvCheckRecordImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.checkrecord.vo.MesDvCheckRecordImportRespVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.machinery.MesDvMachineryDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dv.machinery.MesDvMachineryMapper;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.checkplan.MesDvCheckPlanDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dv.checkplan.MesDvCheckPlanMapper;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.hutool.core.util.StrUtil;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * MES 设备点检记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MesDvCheckRecordServiceImpl implements MesDvCheckRecordService {
 
    @Resource
    private MesDvCheckPlanMapper checkPlanMapper;
 
    @Resource
    private MesDvMachineryMapper machineryMapper;
 
 
    @Resource
    private MesDvCheckRecordMapper checkRecordMapper;
 
    @Resource
    @Lazy
    private MesDvCheckRecordLineService checkRecordLineService;
    @Resource
    private MesDvMachineryService machineryService;
    @Resource
    private MesDvCheckPlanService checkPlanService;
    @Resource
    private MesDvCheckPlanSubjectService checkPlanSubjectService;
 
    @Resource
    private AdminUserApi adminUserApi;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCheckRecord(MesDvCheckRecordSaveReqVO createReqVO) {
        // 1. 校验关联数据
        validateCheckRecordSave(createReqVO);
 
        // 2. 插入主记录,状态默认为草稿
        MesDvCheckRecordDO checkRecord = BeanUtils.toBean(createReqVO, MesDvCheckRecordDO.class)
                .setStatus(MesDvCheckRecordStatusEnum.DRAFT.getStatus());
        checkRecordMapper.insert(checkRecord);
 
        // 3. 如果指定了点检计划,自动生成明细行
        if (createReqVO.getPlanId() != null) {
            generateCheckRecordLines(checkRecord.getId(), createReqVO.getPlanId());
        }
        return checkRecord.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCheckRecord(MesDvCheckRecordSaveReqVO updateReqVO) {
        // 1.1 校验存在 + 状态为草稿
        validateCheckRecordDraft(updateReqVO.getId());
        // 1.2 校验关联数据
        validateCheckRecordSave(updateReqVO);
 
        // 2. 如果计划变更,删除旧明细并重新生成
        MesDvCheckRecordDO existRecord = checkRecordMapper.selectById(updateReqVO.getId());
        Long oldPlanId = existRecord.getPlanId();
        Long newPlanId = updateReqVO.getPlanId();
        boolean planChanged = ObjUtil.notEqual(oldPlanId, newPlanId);
        if (planChanged) {
            checkRecordLineService.deleteByRecordId(updateReqVO.getId());
            if (newPlanId != null) {
                generateCheckRecordLines(updateReqVO.getId(), newPlanId);
            }
        }
 
        // 3. 更新主记录
        MesDvCheckRecordDO updateObj = BeanUtils.toBean(updateReqVO, MesDvCheckRecordDO.class);
        checkRecordMapper.updateById(updateObj);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitCheckRecord(Long id) {
        // 1.1 校验状态为草稿
        validateCheckRecordDraft(id);
        // 1.2 校验至少有一条明细
        List<MesDvCheckRecordLineDO> lines = checkRecordLineService.getCheckRecordLineListByRecordId(id);
        if (CollUtil.isEmpty(lines)) {
            throw exception(DV_CHECK_RECORD_NO_LINE);
        }
 
        // 2. 状态改为已完成
        MesDvCheckRecordDO record = checkRecordMapper.selectById(id);
        MesDvCheckRecordDO updateObj = new MesDvCheckRecordDO()
                .setId(id).setStatus(MesDvCheckRecordStatusEnum.FINISHED.getStatus());
        checkRecordMapper.updateById(updateObj);
 
        // 3. 回写设备台账的【最近点检时间】
        if (record.getCheckTime() != null) {
            machineryService.updateMachineryLastCheckTime(record.getMachineryId(), record.getCheckTime());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCheckRecord(Long id) {
        // 1. 校验状态为草稿(已完成不可删除)
        validateCheckRecordDraft(id);
 
        // 2.1 删除主记录
        checkRecordMapper.deleteById(id);
        // 2.2 级联删除明细
        checkRecordLineService.deleteByRecordId(id);
    }
 
    @Override
    public void validateCheckRecordExists(Long id) {
        if (checkRecordMapper.selectById(id) == null) {
            throw exception(DV_CHECK_RECORD_NOT_EXISTS);
        }
    }
 
    @Override
    public Long getCheckRecordCountByMachineryId(Long machineryId) {
        return checkRecordMapper.selectCountByMachineryId(machineryId);
    }
 
    @Override
    public MesDvCheckRecordDO getCheckRecord(Long id) {
        return checkRecordMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesDvCheckRecordDO> getCheckRecordPage(MesDvCheckRecordPageReqVO pageReqVO) {
        return checkRecordMapper.selectPage(pageReqVO);
    }
 
    // ==================== 校验方法 ====================
 
    @Override
    public void validateCheckRecordDraft(Long id) {
        MesDvCheckRecordDO record = checkRecordMapper.selectById(id);
        if (record == null) {
            throw exception(DV_CHECK_RECORD_NOT_EXISTS);
        }
        if (ObjUtil.notEqual(MesDvCheckRecordStatusEnum.DRAFT.getStatus(), record.getStatus())) {
            throw exception(DV_CHECK_RECORD_NOT_DRAFT);
        }
    }
 
    private void validateCheckRecordSave(MesDvCheckRecordSaveReqVO reqVO) {
        // 校验设备是否存在
        machineryService.validateMachineryExists(reqVO.getMachineryId());
        // 校验点检计划是否存在
        if (reqVO.getPlanId() != null) {
            checkPlanService.validateCheckPlanExistsAndType(reqVO.getPlanId(), MesDvCheckPlanTypeEnum.CHECK.getType());
        }
        // 校验点检人是否存在
        if (reqVO.getUserId() != null) {
            adminUserApi.validateUser(reqVO.getUserId());
        }
    }
 
    /**
     * 根据计划自动生成点检项目明细行
     */
    private void generateCheckRecordLines(Long recordId, Long planId) {
        List<MesDvCheckPlanSubjectDO> planSubjects = checkPlanSubjectService.getCheckPlanSubjectListByPlanId(planId);
        if (CollUtil.isEmpty(planSubjects)) {
            return;
        }
        List<MesDvCheckRecordLineDO> lines = new ArrayList<>(planSubjects.size());
        for (MesDvCheckPlanSubjectDO planSubject : planSubjects) {
            lines.add(new MesDvCheckRecordLineDO()
                    .setRecordId(recordId).setSubjectId(planSubject.getSubjectId())
                    .setCheckStatus(MesDvCheckResultEnum.NORMAL.getResult()));
        }
        checkRecordLineService.createCheckRecordLineList(lines);
    }
 
 
    @Override
    public MesDvCheckRecordImportRespVO importCheckRecordList(List<MesDvCheckRecordImportExcelVO> importList, boolean updateSupport) {
        if (CollUtil.isEmpty(importList)) {
            throw exception(DV_CHECK_RECORD_IMPORT_LIST_IS_EMPTY);
        }
        MesDvCheckRecordImportRespVO respVO = MesDvCheckRecordImportRespVO.builder()
                .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
                .failureCodes(new LinkedHashMap<>()).build();
        AtomicInteger index = new AtomicInteger(1);
        importList.forEach(importItem -> {
            int currentIndex = index.getAndIncrement();
            String key = "第 " + currentIndex + " 行";
            if (StrUtil.isBlank(importItem.getMachineryCode())) {
                respVO.getFailureCodes().put(key, "设备编码不能为空");
                return;
            }
            if (importItem.getCheckTime() == null) {
                respVO.getFailureCodes().put(key, "点检时间不能为空");
                return;
            }
            MesDvMachineryDO machinery = machineryMapper.selectByCode(importItem.getMachineryCode());
            if (machinery == null) {
                respVO.getFailureCodes().put(key, "设备不存在:" + importItem.getMachineryCode());
                return;
            }
            Long planId = null;
            if (StrUtil.isNotBlank(importItem.getPlanCode())) {
                MesDvCheckPlanDO plan = checkPlanMapper.selectByCode(importItem.getPlanCode());
                if (plan == null) {
                    respVO.getFailureCodes().put(key, "点检保养方案不存在:" + importItem.getPlanCode());
                    return;
                }
                planId = plan.getId();
            }
            try {
                MesDvCheckRecordSaveReqVO saveReqVO = BeanUtils.toBean(importItem, MesDvCheckRecordSaveReqVO.class);
                saveReqVO.setMachineryId(machinery.getId());
                saveReqVO.setPlanId(planId);
                Long id = createCheckRecord(saveReqVO);
                respVO.getCreateCodes().add(String.valueOf(id));
            } catch (ServiceException e) {
                respVO.getFailureCodes().put(key, e.getMessage());
            }
        });
        return respVO;
    }
 
}