云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
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
package cn.iocoder.yudao.module.mes.service.dv.maintenrecord;
 
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.maintenrecord.vo.MesDvMaintenRecordPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.maintenrecord.vo.MesDvMaintenRecordSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.maintenrecord.MesDvMaintenRecordDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.maintenrecord.MesDvMaintenRecordLineDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dv.maintenrecord.MesDvMaintenRecordMapper;
import cn.iocoder.yudao.module.mes.enums.dv.MesDvMaintenRecordStatusEnum;
import cn.iocoder.yudao.module.mes.enums.dv.MesDvCheckPlanTypeEnum;
import cn.iocoder.yudao.module.mes.service.dv.checkplan.MesDvCheckPlanService;
import cn.iocoder.yudao.module.mes.service.dv.machinery.MesDvMachineryService;
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.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.maintenrecord.vo.MesDvMaintenRecordImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.maintenrecord.vo.MesDvMaintenRecordImportRespVO;
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.ArrayList;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * MES 设备保养记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MesDvMaintenRecordServiceImpl implements MesDvMaintenRecordService {
 
    @Resource
    private MesDvCheckPlanMapper checkPlanMapper;
 
    @Resource
    private MesDvMachineryMapper machineryMapper;
 
    @Resource
    private MesDvMaintenRecordMapper maintenRecordMapper;
 
    @Resource
    @Lazy
    private MesDvMaintenRecordLineService maintenRecordLineService;
    @Resource
    private MesDvMachineryService machineryService;
    @Resource
    private MesDvCheckPlanService checkPlanService;
 
    @Override
    public Long createMaintenRecord(MesDvMaintenRecordSaveReqVO createReqVO) {
        // 1. 校验关联数据
        validateMaintenRecordRelation(createReqVO);
 
        // 2. 插入
        MesDvMaintenRecordDO maintenRecord = BeanUtils.toBean(createReqVO, MesDvMaintenRecordDO.class);
        maintenRecord.setStatus(MesDvMaintenRecordStatusEnum.PREPARE.getStatus());
        maintenRecordMapper.insert(maintenRecord);
        return maintenRecord.getId();
    }
 
    @Override
    public void updateMaintenRecord(MesDvMaintenRecordSaveReqVO updateReqVO) {
        // 1.1 校验存在,且状态为草稿
        validateMaintenRecordDraft(updateReqVO.getId());
        // 1.2 校验关联数据
        validateMaintenRecordRelation(updateReqVO);
 
        // 2. 更新
        MesDvMaintenRecordDO updateObj = BeanUtils.toBean(updateReqVO, MesDvMaintenRecordDO.class);
        maintenRecordMapper.updateById(updateObj);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitMaintenRecord(Long id) {
        // 1.1 校验状态为草稿
        validateMaintenRecordDraft(id);
        // 1.2 校验至少有一条明细
        List<MesDvMaintenRecordLineDO> lines = maintenRecordLineService.getMaintenRecordLineListByRecordId(id);
        if (CollUtil.isEmpty(lines)) {
            throw exception(MAINTEN_RECORD_NO_LINE);
        }
 
        // 2. 状态改为已提交
        MesDvMaintenRecordDO record = maintenRecordMapper.selectById(id);
        MesDvMaintenRecordDO updateObj = new MesDvMaintenRecordDO();
        updateObj.setId(id);
        updateObj.setStatus(MesDvMaintenRecordStatusEnum.SUBMITTED.getStatus());
        maintenRecordMapper.updateById(updateObj);
 
        // 3. 回写设备台账的【最近保养时间】
        if (record.getMaintenTime() != null) {
            machineryService.updateMachineryLastMaintenTime(record.getMachineryId(), record.getMaintenTime());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMaintenRecord(Long id) {
        // 校验存在,且状态为草稿
        validateMaintenRecordDraft(id);
 
        // 删除
        maintenRecordMapper.deleteById(id);
        // 级联删除子表
        maintenRecordLineService.deleteMaintenRecordLineByRecordId(id);
    }
 
    private void validateMaintenRecordRelation(MesDvMaintenRecordSaveReqVO reqVO) {
        // 校验设备是否存在
        machineryService.validateMachineryExists(reqVO.getMachineryId());
        // 校验保养计划是否存在
        if (reqVO.getPlanId() != null) {
            checkPlanService.validateCheckPlanExistsAndType(reqVO.getPlanId(), MesDvCheckPlanTypeEnum.MAINTENANCE.getType());
        }
    }
 
    @Override
    public void validateMaintenRecordExists(Long id) {
        if (maintenRecordMapper.selectById(id) == null) {
            throw exception(MAINTEN_RECORD_NOT_EXISTS);
        }
    }
 
    @Override
    public Long getMaintenRecordCountByMachineryId(Long machineryId) {
        return maintenRecordMapper.selectCountByMachineryId(machineryId);
    }
 
    @Override
    public MesDvMaintenRecordDO validateMaintenRecordDraft(Long id) {
        MesDvMaintenRecordDO record = maintenRecordMapper.selectById(id);
        if (record == null) {
            throw exception(MAINTEN_RECORD_NOT_EXISTS);
        }
        if (ObjUtil.notEqual(MesDvMaintenRecordStatusEnum.PREPARE.getStatus(), record.getStatus())) {
            throw exception(MAINTEN_RECORD_NOT_DRAFT);
        }
        return record;
    }
 
    @Override
    public MesDvMaintenRecordDO getMaintenRecord(Long id) {
        return maintenRecordMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesDvMaintenRecordDO> getMaintenRecordPage(MesDvMaintenRecordPageReqVO pageReqVO) {
        return maintenRecordMapper.selectPage(pageReqVO);
    }
 
 
    @Override
    public MesDvMaintenRecordImportRespVO importMaintenRecordList(List<MesDvMaintenRecordImportExcelVO> importList, boolean updateSupport) {
        if (CollUtil.isEmpty(importList)) {
            throw exception(DV_MAINTEN_RECORD_IMPORT_LIST_IS_EMPTY);
        }
        MesDvMaintenRecordImportRespVO respVO = MesDvMaintenRecordImportRespVO.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.getMaintenTime() == 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 {
                MesDvMaintenRecordSaveReqVO saveReqVO = BeanUtils.toBean(importItem, MesDvMaintenRecordSaveReqVO.class);
                saveReqVO.setMachineryId(machinery.getId());
                saveReqVO.setPlanId(planId);
                Long id = createMaintenRecord(saveReqVO);
                respVO.getCreateCodes().add(String.valueOf(id));
            } catch (ServiceException e) {
                respVO.getFailureCodes().put(key, e.getMessage());
            }
        });
        return respVO;
    }
 
}