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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package org.jeecg.modules.dnc.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.jeecg.weibo.exception.BusinessException;
import liquibase.pro.packaged.S;
import org.jeecg.modules.dnc.dto.ComponentHierarchy;
import org.jeecg.modules.dnc.dto.TransferPackage;
import org.jeecg.modules.dnc.entity.*;
import org.jeecg.modules.dnc.mapper.*;
import org.jeecg.modules.dnc.service.*;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.jeecg.modules.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
@Service
public class DataImportService {
    private static final Logger logger = LoggerFactory.getLogger(DataImportService.class);
 
    @Autowired
    private ProductInfoMapper productMapper;
 
    @Autowired
    private ComponentInfoMapper componentMapper;
 
    @Autowired
    private PartsInfoMapper partsMapper;
 
    @Autowired
    private ProcessSpecVersionMapper psvMapper;
 
    @Autowired
    private ProcessStreamMapper processMapper;
 
    @Autowired
    private WorkStepMapper workStepMapper;
 
    @Autowired
    private ProductMixMapper productMixMapper;
 
    @Autowired
    private PermissionStreamNewMapper permissionStreamNewMapper;
 
    @Autowired
    private DeviceManagementMapper deviceManagementMapper;
 
    @Autowired
    private DeviceTypeMapper deviceTypeMapper;
 
    @Autowired
    private DocInfoMapper docInfoMapper;
 
    @Autowired
    private DocFileMapper docFileMapper;
 
    @Autowired
    private DocRelativeMapper docRelativeMapper;
 
    @Autowired
    private CutterMapper cutterMapper;
 
    @Autowired
    private GuideCardBatchMapper guideCardBatchMapper;
 
    @Autowired
    private ISysUserService sysUserService;
 
    @Autowired
    private IMdcProductionService mdcProductionService;
 
    @Autowired
    private IProductPermissionService productPermissionService;
 
    @Autowired
    private IProductDepartmentService productDepartmentService;
 
    @Autowired
    private IComponentPermissionService componentPermissionService;
 
    @Autowired
    private IComponentDepartmentService componentDepartmentService;
 
    @Autowired
    private IPartsPermissionService partsPermissionService;
 
    @Autowired
    private IPartsDepartmentService partsDepartmentService;
 
    @Autowired
    private IProcessSpecVersionPermissionService processSpecVersionPermissionService;
 
    @Autowired
    private IProcessSpecVersionDepartmentService processSpecVersionDepartmentService;
 
    @Autowired
    private IProcessStreamPermissionService processStreamPermissionService;
 
    @Autowired
    private IProcessionDepartmentService processionDepartmentService;
 
    @Autowired
    private IWorkStepPermissionService workStepPermissionService;
 
    @Autowired
    private IWorkStepDepartmentService workStepDepartmentService;
 
    @Transactional(rollbackFor = Exception.class)
    public void importTransferPackage(TransferPackage transferPackage) {
        try {
            logger.info("开始导入传输包数据, 类型: {}", transferPackage.getDataType());
 
            // 保存产品
            if (transferPackage.getTraceChain() != null &&
                    transferPackage.getTraceChain().getProduct() != null) {
                saveProduct(transferPackage.getTraceChain().getProduct());
            }
 
            // 保存部件层级
            if (transferPackage.getTraceChain() != null &&
                    transferPackage.getTraceChain().getComponentHierarchy() != null) {
                saveComponentHierarchy(transferPackage.getTraceChain().getComponentHierarchy());
            }
 
            // 保存零件
            if (transferPackage.getTraceChain() != null &&
                    transferPackage.getTraceChain().getParts() != null) {
                saveParts(transferPackage.getTraceChain().getParts());
            }
 
            // 保存工艺规程
            if (transferPackage.getTraceChain() != null &&
                    transferPackage.getTraceChain().getProcessSpec() != null) {
                saveProcessSpec(transferPackage.getTraceChain().getProcessSpec());
            }
 
            // 保存工序
            if (transferPackage.getTraceChain() != null&&
                    transferPackage.getTraceChain().getProcess() != null) {
                saveProcess(transferPackage.getTraceChain().getProcess());
            }
 
            // 保存工步
            if (transferPackage.getTraceChain() != null&&
                    transferPackage.getTraceChain().getWorkStep() != null) {
                saveWorkSteps(transferPackage.getTraceChain().getWorkStep());
            }
 
            // 保存结构树
            if (transferPackage.getTraceChain() != null&&
                    transferPackage.getTraceChain().getTreePath() != null) {
                saveTreePath(transferPackage.getTraceChain().getTreePath());
            }
 
            //保存权限
            if (transferPackage.getTraceChain() != null&&
                    transferPackage.getTraceChain().getPermissionStreamNewList() != null) {
                savePermissionStreamNewList(transferPackage.getTraceChain().getPermissionStreamNewList());
            }
 
            // 保存设备类
            if (transferPackage.getTraceChain() != null&&
                    transferPackage.getTraceChain().getDeviceManagement() != null) {
                saveDeviceManagement(transferPackage.getTraceChain().getDeviceManagement());
            }
 
            // 保存设备类对应信息
            if (transferPackage.getTraceChain() != null&&
                    transferPackage.getTraceChain().getDeviceType() != null) {
                saveDeviceType(transferPackage.getTraceChain().getDeviceType());
            }
 
            // 保存文档
            if (transferPackage.getTraceChain() != null&&
                    transferPackage.getTraceChain().getDocInfo() != null) {
                saveDocInfo(transferPackage.getTraceChain().getDocInfo());
            }
 
            // 保存文件
            if (transferPackage.getTraceChain() !=null&&
                    transferPackage.getTraceChain().getDocFile() != null) {
                saveDocFile(transferPackage.getTraceChain().getDocFile());
            }
 
            // 保存文档文件对应关系
            if (transferPackage.getDocRelative() !=null){
                saveDocRelative(transferPackage.getDocRelative());
            }
 
            // 保存刀具系统
            if (transferPackage.getTraceChain() !=null&&
                    transferPackage.getTraceChain().getCutterList() != null) {
                saveCutterList(transferPackage.getTraceChain().getCutterList());
            }
 
            //保存数控程序加工确认表
            if (transferPackage.getTraceChain() !=null&&
                    transferPackage.getTraceChain().getGuideCardBatch() != null) {
                saveGuideCardBatch(transferPackage.getTraceChain().getGuideCardBatch());
            }
 
            logger.info("数据导入成功");
        } catch (DuplicateKeyException e) {
            logger.warn("主键冲突: {}", e.getMessage());
            throw new BusinessException("数据已存在,无法重复导入");
        } catch (DataIntegrityViolationException e) {
            logger.error("数据完整性违反: {}", e.getMessage());
            throw new BusinessException("数据不完整,请检查必填字段");
        } catch (Exception e) {
            logger.error("数据导入失败: {}", e.getMessage(), e);
            throw new BusinessException("数据导入失败: " + e.getMessage());
        }
    }
 
    private void saveProduct(ProductInfo product) {
        if (productMapper.selectById(product.getProductId()) == null) {
            productMapper.insert(product);
            logger.debug("产品已保存: {}", product.getProductId());
        } else {
            logger.debug("产品已存在: {}", product.getProductId());
        }
    }
 
    private void saveComponentHierarchy(ComponentHierarchy hierarchy) {
        for (ComponentInfo component : hierarchy.getComponents()) {
            if (componentMapper.selectById(component.getComponentId()) == null) {
                componentMapper.insert(component);
                logger.debug("部件已保存: {}", component.getComponentId());
            } else {
                logger.debug("部件已存在: {}", component.getComponentId());
            }
        }
    }
 
    private void saveParts(PartsInfo parts) {
        if (partsMapper.selectById(parts.getPartsId()) == null) {
            partsMapper.insert(parts);
            logger.debug("零件已保存: {}", parts.getPartsId());
        } else {
            logger.debug("零件已存在: {}", parts.getPartsId());
        }
    }
 
    private void saveProcessSpec(ProcessSpecVersion processSpec) {
        if (psvMapper.selectById(processSpec.getId()) == null) {
            psvMapper.insert(processSpec);
            logger.debug("工艺规程已保存: {}", processSpec.getId());
        } else {
            logger.debug("工艺规程已存在: {}", processSpec.getId());
        }
    }
 
    private void saveProcess(ProcessStream process) {
        if (processMapper.selectById(process.getProcessId()) == null) {
            processMapper.insert(process);
            logger.debug("工序已保存: {}", process.getProcessId());
        } else {
            logger.debug("工序已存在: {}", process.getProcessId());
        }
    }
 
    private void saveWorkSteps(WorkStep workStep) {
        if (workStepMapper.selectById(workStep.getId()) == null) {
            workStepMapper.insert(workStep);
            logger.debug("工步已保存: {}", workStep.getId());
        } else {
            logger.debug("工步已存在: {}", workStep.getId());
        }
    }
 
    private void saveTreePath(List<ProductMix> productMixList){
        for (ProductMix productMix : productMixList) {
            if (productMixMapper.selectById(productMix.getId()) == null) {
                productMixMapper.insert(productMix);
                logger.debug("产品组合已保存: {}", productMix.getId());
            } else {
                logger.debug("产品组合已存在: {}", productMix.getId());
            }
        }
    }
 
    private void savePermissionStreamNewList(List<PermissionStreamNew> permissionStreamNewList) {
        for (PermissionStreamNew permissionStreamNew : permissionStreamNewList) {
            if (permissionStreamNewMapper.selectById(permissionStreamNew.getId()) == null) {
                if (permissionStreamNew.getUserId() != null) {
                    String id=sysUserService.getUserByName(permissionStreamNew.getUserId()).getId();
                    if (id!=null){
                        permissionStreamNew.setUserId(id);
                    }
                }
                if (permissionStreamNew.getDepartId() != null) {
                    MdcProduction mdcProduction=mdcProductionService.findByOrgCode(permissionStreamNew.getDepartId());
                    if (mdcProduction!=null){
                        permissionStreamNew.setDepartId(mdcProduction.getId());
                    }
                }
                permissionStreamNewMapper.insert(permissionStreamNew);
                logger.debug("权限已保存: {}", permissionStreamNew.getId());
            }
        }
        //分批添加产品、部件、零件、工艺规程、工序、工步权限
        permissionStreamNewList.forEach(item -> {
            switch (item.getBusinessType()){
                case "1":
                    if (StrUtil.isNotEmpty(item.getUserId())){
                        ProductPermission productPermission = new ProductPermission();
                        productPermission.setProductId(item.getBusinessId());
                        productPermission.setUserId(item.getUserId());
                        productPermissionService.save(productPermission);
                    }else {
                        ProductDepartment productDepartment = new ProductDepartment();
                        productDepartment.setProductId(item.getBusinessId());
                        productDepartment.setDepartId(item.getDepartId());
                        productDepartmentService.save(productDepartment);
                    }
                    break;
                case "2":
 
                    if (StrUtil.isNotEmpty(item.getUserId())){
                        ComponentPermission componentPermission = new ComponentPermission();
                        componentPermission.setComponentId(item.getBusinessId());
                        componentPermission.setUserId(item.getUserId());
                        componentPermissionService.save(componentPermission);
                    }else {
                        ComponentDepartment componentDepartment = new ComponentDepartment();
                        componentDepartment.setComponentId(item.getBusinessId());
                        componentDepartment.setDepartId(item.getDepartId());
                        componentDepartmentService.save(componentDepartment);
                    }
                    break;
                case "3":
                    if (StrUtil.isNotEmpty(item.getUserId())){
                        PartsPermission partsPermission = new PartsPermission();
                        partsPermission.setPartsId(item.getBusinessId());
                        partsPermission.setUserId(item.getUserId());
                        partsPermissionService.save(partsPermission);
                    }else {
                        PartsDepartment partsDepartment = new PartsDepartment();
                        partsDepartment.setPartsId(item.getBusinessId());
                        partsDepartment.setDepartId(item.getDepartId());
                        partsDepartmentService.save(partsDepartment);
                    }
                    break;
                case "4":
                    if (StrUtil.isNotEmpty(item.getUserId())){
                        ProcessSpecVersionPermission processSpecVersionPermission = new ProcessSpecVersionPermission();
                        processSpecVersionPermission.setPsvId(item.getBusinessId());
                        processSpecVersionPermission.setUserId(item.getUserId());
                        processSpecVersionPermissionService.save(processSpecVersionPermission);
                    }else {
                        ProcessSpecVersionDepartment processSpecVersionDepartment = new ProcessSpecVersionDepartment();
                        processSpecVersionDepartment.setPsvId(item.getBusinessId());
                        processSpecVersionDepartment.setDepartId(item.getDepartId());
                        processSpecVersionDepartmentService.save(processSpecVersionDepartment);
                    }
                    break;
                case "5":
                    if (StrUtil.isNotEmpty(item.getUserId())){
                        ProcessionPermission processionPermission = new ProcessionPermission();
                        processionPermission.setProcessId(item.getBusinessId());
                        processionPermission.setUserId(item.getUserId());
                        processStreamPermissionService.save(processionPermission);
                    }else {
                        ProcessionDepartment processionDepartment = new ProcessionDepartment();
                        processionDepartment.setProcessId(item.getBusinessId());
                        processionDepartment.setDepartId(item.getDepartId());
                        processionDepartmentService.save(processionDepartment);
                    }
                    break;
                case  "6":
                    if (StrUtil.isNotEmpty(item.getUserId())){
                        WorkStepPermission workStepPermission = new WorkStepPermission();
                        workStepPermission.setStepId(item.getBusinessId());
                        workStepPermission.setUserId(item.getUserId());
                        workStepPermissionService.save(workStepPermission);
                    }else {
                        WorkStepDepartment workStepDepartment = new WorkStepDepartment();
                        workStepDepartment.setStepId(item.getBusinessId());
                        workStepDepartment.setDepartId(item.getDepartId());
                        workStepDepartmentService.save(workStepDepartment);
                    }
                    break;
                default:
            }
        });
    }
 
    private void saveDeviceManagement(DeviceManagement deviceManagement) {
        if (deviceManagementMapper.selectById(deviceManagement.getId()) == null) {
            deviceManagementMapper.insert(deviceManagement);
            logger.debug("设备类信息已保存: {}", deviceManagement.getId());
        } else {
            logger.debug("设备类信息已存在: {}", deviceManagement.getId());
        }
    }
 
    private void saveDeviceType(DeviceType deviceType) {
        if (deviceTypeMapper.selectById(deviceType.getId()) == null) {
            deviceTypeMapper.insert(deviceType);
            logger.debug("设备类已保存: {}", deviceType.getId());
        } else {
            logger.debug("设备类已存在: {}", deviceType.getId());
        }
    }
 
    private void saveDocInfo(DocInfo docInfo) {
        if (docInfoMapper.selectById(docInfo.getDocId()) == null) {
            docInfoMapper.insert(docInfo);
            logger.debug("文档已保存: {}", docInfo.getDocId());
        } else {
            logger.debug("文档已存在: {}", docInfo.getDocId());
        }
    }
 
    private void saveDocFile(DocFile docFile) {
        if (docFileMapper.selectById(docFile.getFileId()) == null) {
            docFileMapper.insert(docFile);
            logger.debug("文档文件已保存: {}", docFile.getFileId());
        } else {
            logger.debug("文档文件已存在: {}", docFile.getFileId());
        }
    }
 
    private void saveDocRelative(DocRelative docRelative) {
        if (docRelativeMapper.selectById(docRelative.getId()) == null) {
            docRelativeMapper.insert(docRelative);
            logger.debug("文档对应关系已保存: {}", docRelative.getId());
        } else {
            logger.debug("文档对应关系已存在: {}", docRelative.getId());
        }
    }
 
    private void saveCutterList(List<Cutter> cutterList) {
        for (Cutter cutter : cutterList) {
            if (cutterMapper.selectById(cutter.getId()) == null) {
                cutterMapper.insert(cutter);
                logger.debug("刀具已保存: {}", cutter.getId());
            } else {
                cutterMapper.updateById(cutter);
            }
        }
    }
 
    private void saveGuideCardBatch(GuideCardBatch guideCardBatch) {
        if (guideCardBatchMapper.selectById(guideCardBatch.getId()) == null) {
            guideCardBatch.setFlowStatus("1");
            guideCardBatchMapper.insert(guideCardBatch);
            logger.debug("刀片批次已保存: {}", guideCardBatch.getId());
        } else {
            logger.debug("刀片批次已存在: {}", guideCardBatch.getId());
        }
    }
}