gaoluyang
2026-10-16 7081c41bc3beb0ab49a22974ea4b010ae6854afe
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
package cn.iocoder.yudao.module.mes.service.dv.checkplan;
 
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.checkplan.vo.MesDvCheckPlanPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.checkplan.vo.MesDvCheckPlanSaveReqVO;
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.module.mes.enums.dv.MesDvCheckPlanStatusEnum;
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.Collection;
import java.util.Collections;
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.checkplan.vo.MesDvCheckPlanImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.checkplan.vo.MesDvCheckPlanImportRespVO;
import cn.iocoder.yudao.module.mes.enums.dv.MesDvCheckPlanTypeEnum;
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 MesDvCheckPlanServiceImpl implements MesDvCheckPlanService {
 
    @Resource
    private MesDvCheckPlanMapper checkPlanMapper;
 
    @Resource
    @Lazy
    private MesDvCheckPlanMachineryService checkPlanMachineryService;
    @Resource
    @Lazy
    private MesDvCheckPlanSubjectService checkPlanSubjectService;
 
    @Override
    public Long createCheckPlan(MesDvCheckPlanSaveReqVO createReqVO) {
        // 1. 校验编码唯一
        validateCheckPlanCodeUnique(null, createReqVO.getCode());
 
        // 2. 插入方案
        MesDvCheckPlanDO checkPlan = BeanUtils.toBean(createReqVO, MesDvCheckPlanDO.class);
        checkPlan.setStatus(MesDvCheckPlanStatusEnum.PREPARE.getStatus()); // 默认草稿
        checkPlanMapper.insert(checkPlan);
        return checkPlan.getId();
    }
 
    @Override
    public void updateCheckPlan(MesDvCheckPlanSaveReqVO updateReqVO) {
        // 1.1 校验存在
        MesDvCheckPlanDO existPlan = doValidateCheckPlanExists(updateReqVO.getId());
        // 1.2 校验草稿状态
        if (ObjUtil.notEqual(MesDvCheckPlanStatusEnum.PREPARE.getStatus(), existPlan.getStatus())) {
            throw exception(DV_CHECK_PLAN_NOT_PREPARE);
        }
        // 1.3 校验编码唯一
        validateCheckPlanCodeUnique(updateReqVO.getId(), updateReqVO.getCode());
 
        // 2. 更新
        updateReqVO.setStatus(null); // 不允许通过 update 修改状态
        MesDvCheckPlanDO updateObj = BeanUtils.toBean(updateReqVO, MesDvCheckPlanDO.class);
        checkPlanMapper.updateById(updateObj);
    }
 
    @Override
    public void enableCheckPlan(Long id) {
        // 1.1 校验存在 + 校验草稿状态
        validateCheckPlanPrepare(id);
        // 1.2 校验至少关联一台设备
        Long machineryCount = checkPlanMachineryService.getCheckPlanMachineryCountByPlanId(id);
        if (machineryCount <= 0) {
            throw exception(DV_CHECK_PLAN_NO_MACHINERY);
        }
        // 1.3 校验至少关联一个项目
        Long subjectCount = checkPlanSubjectService.getCheckPlanSubjectCountByPlanId(id);
        if (subjectCount <= 0) {
            throw exception(DV_CHECK_PLAN_NO_SUBJECT);
        }
 
        // 2. 更新状态为已启用
        checkPlanMapper.updateById(new MesDvCheckPlanDO().setId(id)
                .setStatus(MesDvCheckPlanStatusEnum.ENABLED.getStatus()));
    }
 
    @Override
    public void disableCheckPlan(Long id) {
        // 1. 校验存在 + 校验已启用状态
        MesDvCheckPlanDO plan = doValidateCheckPlanExists(id);
        if (ObjUtil.notEqual(MesDvCheckPlanStatusEnum.ENABLED.getStatus(), plan.getStatus())) {
            throw exception(DV_CHECK_PLAN_NOT_ENABLED);
        }
 
        // 2. 更新状态为草稿
        checkPlanMapper.updateById(new MesDvCheckPlanDO().setId(id)
                .setStatus(MesDvCheckPlanStatusEnum.PREPARE.getStatus()));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCheckPlan(Long id) {
        // 1. 校验存在 + 校验草稿状态
        validateCheckPlanPrepare(id);
 
        // 2.1 级联删除设备关联 + 项目关联
        checkPlanMachineryService.deleteByPlanId(id);
        checkPlanSubjectService.deleteByPlanId(id);
        // 2.2 删除方案
        checkPlanMapper.deleteById(id);
    }
 
    @Override
    public MesDvCheckPlanDO validateCheckPlanPrepare(Long planId) {
        MesDvCheckPlanDO plan = doValidateCheckPlanExists(planId);
        if (ObjUtil.notEqual(MesDvCheckPlanStatusEnum.PREPARE.getStatus(), plan.getStatus())) {
            throw exception(DV_CHECK_PLAN_NOT_PREPARE);
        }
        return plan;
    }
 
    @Override
    public MesDvCheckPlanDO getCheckPlan(Long id) {
        return checkPlanMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesDvCheckPlanDO> getCheckPlanPage(MesDvCheckPlanPageReqVO pageReqVO) {
        return checkPlanMapper.selectPage(pageReqVO);
    }
 
    @Override
    public void validateCheckPlanExists(Long id) {
        if (checkPlanMapper.selectById(id) == null) {
            throw exception(DV_CHECK_PLAN_NOT_EXISTS);
        }
    }
 
    private MesDvCheckPlanDO doValidateCheckPlanExists(Long id) {
        MesDvCheckPlanDO plan = checkPlanMapper.selectById(id);
        if (plan == null) {
            throw exception(DV_CHECK_PLAN_NOT_EXISTS);
        }
        return plan;
    }
 
    @Override
    public MesDvCheckPlanDO validateCheckPlanExistsAndType(Long id, Integer type) {
        MesDvCheckPlanDO plan = doValidateCheckPlanExists(id);
        // 校验类型匹配
        if (ObjUtil.notEqual(plan.getType(), type)) {
            throw exception(DV_CHECK_PLAN_TYPE_MISMATCH);
        }
        // 校验方案已启用
        if (ObjUtil.notEqual(MesDvCheckPlanStatusEnum.ENABLED.getStatus(), plan.getStatus())) {
            throw exception(DV_CHECK_PLAN_NOT_ENABLED_FOR_RECORD);
        }
        return plan;
    }
 
    private void validateCheckPlanCodeUnique(Long id, String code) {
        MesDvCheckPlanDO plan = checkPlanMapper.selectByCode(code);
        if (plan == null) {
            return;
        }
        if (ObjUtil.notEqual(plan.getId(), id)) {
            throw exception(DV_CHECK_PLAN_CODE_DUPLICATE);
        }
    }
 
    @Override
    public List<MesDvCheckPlanDO> getCheckPlanList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return checkPlanMapper.selectByIds(ids);
    }
 
 
    @Override
    public MesDvCheckPlanImportRespVO importCheckPlanList(List<MesDvCheckPlanImportExcelVO> importList, boolean updateSupport) {
        if (CollUtil.isEmpty(importList)) {
            throw exception(DV_CHECK_PLAN_IMPORT_LIST_IS_EMPTY);
        }
        MesDvCheckPlanImportRespVO respVO = MesDvCheckPlanImportRespVO.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 = StrUtil.blankToDefault(importItem.getCode(), "第 " + currentIndex + " 行");
            if (StrUtil.isBlank(importItem.getCode())) {
                respVO.getFailureCodes().put(key, "方案编码不能为空");
                return;
            }
            try {
                MesDvCheckPlanSaveReqVO saveReqVO = BeanUtils.toBean(importItem, MesDvCheckPlanSaveReqVO.class);
                saveReqVO.setType(ObjUtil.defaultIfNull(importItem.getType(), MesDvCheckPlanTypeEnum.CHECK.getType()));
                saveReqVO.setCycleType(ObjUtil.defaultIfNull(importItem.getCycleType(), 1));
                saveReqVO.setCycleCount(ObjUtil.defaultIfNull(importItem.getCycleCount(), 1));
                MesDvCheckPlanDO exist = checkPlanMapper.selectByCode(importItem.getCode());
                if (exist == null) {
                    createCheckPlan(saveReqVO);
                    respVO.getCreateCodes().add(saveReqVO.getCode());
                } else if (updateSupport) {
                    if (ObjUtil.notEqual(MesDvCheckPlanStatusEnum.PREPARE.getStatus(), exist.getStatus())) {
                        respVO.getFailureCodes().put(key, "方案已启用,不能更新:" + importItem.getCode());
                        return;
                    }
                    saveReqVO.setId(exist.getId());
                    updateCheckPlan(saveReqVO);
                    respVO.getUpdateCodes().add(saveReqVO.getCode());
                } else {
                    respVO.getFailureCodes().put(key, "方案编码已存在:" + importItem.getCode());
                }
            } catch (ServiceException e) {
                respVO.getFailureCodes().put(key, e.getMessage());
            }
        });
        return respVO;
    }
 
}