4 天以前 5140e16bd1cb2bdf974d47a4342f9e27ef804655
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package cn.iocoder.yudao.module.mes.service.dv.machinery;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
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.machinery.vo.MesDvMachineryImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.MesDvMachineryImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.MesDvMachineryPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.MesDvMachinerySaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.machinery.MesDvMachineryDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.machinery.MesDvMachineryTypeDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkshopDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dv.machinery.MesDvMachineryMapper;
import cn.iocoder.yudao.module.mes.enums.wm.BarcodeBizTypeEnum;
import cn.iocoder.yudao.module.mes.service.dv.checkplan.MesDvCheckPlanMachineryService;
import cn.iocoder.yudao.module.mes.service.dv.checkrecord.MesDvCheckRecordService;
import cn.iocoder.yudao.module.mes.service.dv.maintenrecord.MesDvMaintenRecordService;
import cn.iocoder.yudao.module.mes.service.dv.repair.MesDvRepairService;
import cn.iocoder.yudao.module.mes.service.md.workstation.MesMdWorkshopService;
import cn.iocoder.yudao.module.mes.service.wm.barcode.MesWmBarcodeService;
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.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 设备台账 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MesDvMachineryServiceImpl implements MesDvMachineryService {
 
    @Resource
    private MesDvMachineryMapper machineryMapper;
 
    @Resource
    @Lazy // 延迟加载,避免循环依赖
    private MesDvMachineryTypeService machineryTypeService;
    @Resource
    @Lazy
    private MesMdWorkshopService workshopService;
    @Resource
    private MesWmBarcodeService barcodeService;
    @Resource
    @Lazy
    private MesDvCheckPlanMachineryService checkPlanMachineryService;
    @Resource
    @Lazy
    private MesDvCheckRecordService checkRecordService;
    @Resource
    @Lazy
    private MesDvMaintenRecordService maintenRecordService;
    @Resource
    @Lazy
    private MesDvRepairService repairService;
 
    @Override
    public Long createMachinery(MesDvMachinerySaveReqVO createReqVO) {
        // 校验设备类型存在
        machineryTypeService.getMachineryType(createReqVO.getMachineryTypeId());
        // 校验车间存在
        workshopService.getWorkshop(createReqVO.getWorkshopId());
        // 校验编码唯一
        validateMachineryCodeUnique(null, createReqVO.getCode());
 
        // 插入
        MesDvMachineryDO machinery = BeanUtils.toBean(createReqVO, MesDvMachineryDO.class);
        machineryMapper.insert(machinery);
 
        // 自动生成条码
        barcodeService.autoGenerateBarcode(BarcodeBizTypeEnum.MACHINERY.getValue(),
                machinery.getId(), machinery.getCode(), machinery.getName());
        return machinery.getId();
    }
 
    @Override
    public void updateMachinery(MesDvMachinerySaveReqVO updateReqVO) {
        // 校验存在
        validateMachineryExists(updateReqVO.getId());
        // 校验设备类型存在
        machineryTypeService.getMachineryType(updateReqVO.getMachineryTypeId());
        // 校验车间存在
        workshopService.getWorkshop(updateReqVO.getWorkshopId());
        // 校验编码唯一
        validateMachineryCodeUnique(updateReqVO.getId(), updateReqVO.getCode());
 
        // 更新
        MesDvMachineryDO updateObj = BeanUtils.toBean(updateReqVO, MesDvMachineryDO.class);
        machineryMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteMachinery(Long id) {
        // 校验存在
        validateMachineryExists(id);
        // 校验关联数据
        if (checkPlanMachineryService.getCheckPlanMachineryCountByMachineryId(id) > 0) {
            throw exception(DV_MACHINERY_HAS_CHECK_PLAN);
        }
        if (checkRecordService.getCheckRecordCountByMachineryId(id) > 0) {
            throw exception(DV_MACHINERY_HAS_CHECK_RECORD);
        }
        if (maintenRecordService.getMaintenRecordCountByMachineryId(id) > 0) {
            throw exception(DV_MACHINERY_HAS_MAINTEN_RECORD);
        }
        if (repairService.getRepairCountByMachineryId(id) > 0) {
            throw exception(DV_MACHINERY_HAS_REPAIR);
        }
 
        // 删除
        machineryMapper.deleteById(id);
    }
 
    @Override
    public void validateMachineryExists(Long id) {
        if (machineryMapper.selectById(id) == null) {
            throw exception(DV_MACHINERY_NOT_EXISTS);
        }
    }
 
    private void validateMachineryCodeUnique(Long id, String code) {
        if (code == null) {
            return;
        }
        MesDvMachineryDO machinery = machineryMapper.selectByCode(code);
        if (machinery == null) {
            return;
        }
        if (ObjUtil.notEqual(machinery.getId(), id)) {
            throw exception(DV_MACHINERY_CODE_DUPLICATE);
        }
    }
 
    @Override
    public MesDvMachineryDO getMachinery(Long id) {
        return machineryMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesDvMachineryDO> getMachineryPage(MesDvMachineryPageReqVO pageReqVO) {
        // 处理设备类型树形查询:选择父类型时,同时查询所有子类型下的设备
        if (pageReqVO.getMachineryTypeId() != null) {
            List<MesDvMachineryTypeDO> children = machineryTypeService.getMachineryTypeChildrenList(
                    pageReqVO.getMachineryTypeId());
            Set<Long> typeIds = new HashSet<>();
            typeIds.add(pageReqVO.getMachineryTypeId());
            children.forEach(child -> typeIds.add(child.getId()));
            pageReqVO.setMachineryTypeIds(typeIds);
        }
        return machineryMapper.selectPage(pageReqVO);
    }
 
    @Override
    public Long getMachineryCountByMachineryTypeId(Long machineryTypeId) {
        return machineryMapper.selectCountByMachineryTypeId(machineryTypeId);
    }
 
    @Override
    public void updateMachineryLastCheckTime(Long machineryId, LocalDateTime lastCheckTime) {
        machineryMapper.updateById(new MesDvMachineryDO().setId(machineryId).setLastCheckTime(lastCheckTime));
    }
 
    @Override
    public void updateMachineryLastMaintenTime(Long machineryId, LocalDateTime lastMaintenTime) {
        machineryMapper.updateById(new MesDvMachineryDO().setId(machineryId).setLastMaintenTime(lastMaintenTime));
    }
 
    @Override
    public List<MesDvMachineryDO> getMachineryList() {
        return machineryMapper.selectList();
    }
 
    @Override
    public List<MesDvMachineryDO> getMachineryList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return machineryMapper.selectByIds(ids);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MesDvMachineryImportRespVO importMachineryList(List<MesDvMachineryImportExcelVO> importMachineryList,
                                                          boolean updateSupport) {
        // 1. 参数校验
        if (CollUtil.isEmpty(importMachineryList)) {
            throw exception(DV_MACHINERY_IMPORT_LIST_IS_EMPTY);
        }
 
        // 2. 批量加载设备类型和车间,构建编码到实体的映射
        List<MesDvMachineryTypeDO> allTypes = machineryTypeService.getMachineryTypeList(
                new cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.type.MesDvMachineryTypeListReqVO());
        Map<String, MesDvMachineryTypeDO> typeCodeMap = allTypes.stream()
                .collect(Collectors.toMap(MesDvMachineryTypeDO::getCode, t -> t, (a, b) -> a));
        List<MesMdWorkshopDO> allWorkshops = workshopService.getWorkshopListByStatus(
                cn.iocoder.yudao.framework.common.enums.CommonStatusEnum.ENABLE.getStatus());
        Map<String, MesMdWorkshopDO> workshopCodeMap = allWorkshops.stream()
                .collect(Collectors.toMap(MesMdWorkshopDO::getCode, w -> w, (a, b) -> a));
 
        // 3. 遍历,逐个创建 or 更新
        MesDvMachineryImportRespVO respVO = MesDvMachineryImportRespVO.builder()
                .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
                .failureCodes(new LinkedHashMap<>()).build();
        AtomicInteger index = new AtomicInteger(1);
        importMachineryList.forEach(importItem -> {
            int currentIndex = index.getAndIncrement();
            // 3.1 校验必填字段
            String key = StrUtil.blankToDefault(importItem.getCode(), "第 " + currentIndex + " 行");
            if (StrUtil.isBlank(importItem.getCode())) {
                respVO.getFailureCodes().put(key, "设备编码不能为空");
                return;
            }
            if (StrUtil.isBlank(importItem.getName())) {
                respVO.getFailureCodes().put(key, "设备名称不能为空");
                return;
            }
            // 3.2 校验设备类型编码
            if (StrUtil.isBlank(importItem.getMachineryTypeCode())) {
                respVO.getFailureCodes().put(key, "设备类型编码不能为空");
                return;
            }
            MesDvMachineryTypeDO machineryType = typeCodeMap.get(importItem.getMachineryTypeCode());
            if (machineryType == null) {
                respVO.getFailureCodes().put(key, "设备类型编码[" + importItem.getMachineryTypeCode() + "]不存在");
                return;
            }
            // 3.3 校验车间编码
            if (StrUtil.isBlank(importItem.getWorkshopCode())) {
                respVO.getFailureCodes().put(key, "车间编码不能为空");
                return;
            }
            MesMdWorkshopDO workshop = workshopCodeMap.get(importItem.getWorkshopCode());
            if (workshop == null) {
                respVO.getFailureCodes().put(key, "车间编码[" + importItem.getWorkshopCode() + "]不存在");
                return;
            }
 
            // 3.4 判断:创建 or 更新
            MesDvMachineryDO existMachinery = machineryMapper.selectByCode(importItem.getCode());
            if (existMachinery == null) {
                // 3.4.1 创建
                MesDvMachineryDO machinery = BeanUtils.toBean(importItem, MesDvMachineryDO.class);
                machinery.setMachineryTypeId(machineryType.getId());
                machinery.setWorkshopId(workshop.getId());
                machineryMapper.insert(machinery);
                // 自动生成条码
                barcodeService.autoGenerateBarcode(BarcodeBizTypeEnum.MACHINERY.getValue(),
                        machinery.getId(), machinery.getCode(), machinery.getName());
                respVO.getCreateCodes().add(importItem.getCode());
            } else if (updateSupport) {
                // 3.4.2 更新
                MesDvMachineryDO updateObj = BeanUtils.toBean(importItem, MesDvMachineryDO.class);
                updateObj.setId(existMachinery.getId());
                updateObj.setMachineryTypeId(machineryType.getId());
                updateObj.setWorkshopId(workshop.getId());
                machineryMapper.updateById(updateObj);
                respVO.getUpdateCodes().add(importItem.getCode());
            } else {
                // 不支持更新
                respVO.getFailureCodes().put(key, "设备编码已存在");
            }
        });
        return respVO;
    }
 
}