whyclxw
2 天以前 12154b62b42df29173cdc54d7fd35d02d9a6422b
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.config.EnumWorksheetType;
import com.whyc.dto.Response;
import com.whyc.mapper.ProductBomApprovingMapper;
import com.whyc.mapper.WorksheetLinkMapper;
import com.whyc.pojo.*;
import com.whyc.util.CommonUtil;
import com.whyc.util.FileUtil;
import com.whyc.util.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class WorksheetLinkService {
 
    @Resource
    private WorksheetMainService mainService;
 
    @Resource
    private WorksheetLinkMapper linkMapper;
    //lxw修改
    @Autowired(required = false)
    private ProductBomApprovingMapper approvingMapper;
 
    @Autowired
    private ProductBomService bomService;
 
    @Autowired
    private ProductApprovingService paService;
 
    @Autowired
    private ProductService productService;
 
    @Autowired
    private ProductBomHistoryService historyService;
 
    @Autowired
    private ProductBomApprovingService pbaService;
 
    @Autowired
    private MaterialProductHistoryService mphService;
 
    @Autowired
    private MaterialProductApprovingService cpAService;
 
    @Autowired
    private MaterialApprovingService mAService;
 
    @Autowired
    private MaterialService mService;
 
    @Autowired
    private ProductSoftwareApprovingService productSoftwareApprovingService;
 
    @Autowired
    private ProductSoftwareService productSoftwareService;
 
    @Autowired
    private ProductHistoryService phService;
 
    @Autowired
    private ProcedureDocApprovingService procedureDocApprovingService;
 
    @Autowired
    private ProcedureDocService procedureDocService;
 
    @Transactional
    public void audit(WorksheetLink link) {
        link.setDealTime(new Date());
        //更新节点信息
        linkMapper.updateById(link);
        if(link.getLinkStatus() == 2){ //驳回
            //查看是否为物料审批,是则需要更新物料审批表中的状态为完结
            if(mainService.getInfoById(link.getMainId()).getType().equals(EnumWorksheetType.Material.getType())){
                mAService.endStatus(link.getMainId());
            }
            //项目经理驳回,驳回即终止
            mainService.updateEndStatusById(link.getMainId(),"经理驳回,驳回原因:"+link.getDealReason(),0);
            //项目经理驳回前,还曾经过总经理,则需要增加总经理驳回原因
            if(link.getParentId()!=0) {
                QueryWrapper<WorksheetLink> query = Wrappers.query();
                query.eq("id", link.getParentId());
                WorksheetLink parentLink = linkMapper.selectOne(query);
                //这个是无效的,用户跟角色对比,不管,下面代码永不生效
                if (parentLink.getDealUserId() == 1003) {
                    mainService.updateEndStatusById(link.getMainId(),
                            "经理驳回,驳回原因:" + link.getDealReason() + ";总经理驳回,驳回原因:" + parentLink.getDealReason(),
                            0);
                }
            }
        }else{
            //进入下一节点,总经理审批
            WorksheetLink link2 = new WorksheetLink();
            link2.setMainId(link.getMainId());
            link2.setParentId(link.getId());
            link2.setDealUserId(link.getNextUser());
            link2.setDealType(2);
            link2.setDealDesc("工单被经理审核通过,信息:"+link.getDealReason());
            link2.setLinkStatus(0);
            link2.setEnableArchive(1);
            linkMapper.insert(link2);
            //更新主表状态
            mainService.updateStatusById(link.getMainId(),2);
        }
    }
 
    @Transactional
    public void approve(WorksheetLink link) {
        //根据节点,查询主表类型
        Integer type = mainService.getInfoById(link.getMainId()).getType();
        link.setDealTime(new Date());
        //更新节点信息
        linkMapper.updateById(link);
        if(link.getLinkStatus() == 2){ //驳回
            if(link.getParentId()!=0) {
                //查询对应的项目经理
                Long createUserId = linkMapper.selectById(link.getParentId()).getDealUserId();
                WorksheetLink link2 = new WorksheetLink();
                link2.setMainId(link.getMainId());
                link2.setParentId(link.getId());
                link2.setDealUserId(createUserId);
                link2.setDealType(0);
                link2.setDealDesc("工单被总经理驳回,驳回信息:" + link.getDealReason());
                link2.setLinkStatus(0);
                link2.setEnableArchive(0);
                linkMapper.insert(link2);
                //更新主表状态
                mainService.updateStatusById(link.getMainId(), 1);
            }else{
                //说明是项目经理提交的,驳回即终止
                mainService.updateEndStatusById(link.getMainId(),"总经理驳回,驳回原因:"+link.getDealReason(),0);
            }
        }else {
            //审批通过,更新主表状态
            mainService.updateEndStatusById(link.getMainId(), "完结", 5);
            if (type.intValue() == EnumWorksheetType.ProductBom.getType()) { //产品审批
 
                ProductApproving  productApproving = paService.getByMainId(link.getMainId());
                List<ProductBomApproving> approvingList = pbaService.getList(productApproving.getId());
                //查询产品最新的版本号
                String parentModel = productApproving.getParentModel();
                String parentCode = productApproving.getParentCode();
                String customCode = productApproving.getCustomCode();
                ProductHistory latestProduct = phService.getLatestVersion(parentCode, customCode);
                ProductHistory enabledProduct = phService.getEnabledByParentCodeAndCustomCode(parentCode, customCode);
                int currentVersion = 0;
                if (latestProduct != null) {
                    currentVersion = latestProduct.getVersion();
                }
                Integer nextVersion = currentVersion + 1;
                //产品物料关系迁移
                //查询生效版本的关联关系
                if(latestProduct!=null &&enabledProduct!=null) {
                    List<MaterialProductHistory> mpList = mphService.getListByParentCodeAndCustomCodeAndVersion(parentCode, customCode, enabledProduct.getVersion());
                    if (latestProduct.getVersion().intValue() == enabledProduct.getVersion()) {
                        //最新版本生效,关联关系版本连着的
                        mphService.updateVersionBatch(mpList);
                    } else {
                        //旧版本生效,关联关系版本不连着
                        mpList.forEach(mp -> {
                            mp.setSVersion(nextVersion);
                            mp.setEVersion(nextVersion);
                        });
                        mphService.insertBatch(mpList);
                    }
                }
 
                //将产品文件复制至正式路径
                //文件转移,未跟子件挂钩的所有图纸图片转移到产品版本下:doc_file/product/{产品型号}/standard或者{customCode}}/{version}/
                //跟子件挂钩的转移到子件图纸下:doc_file/material/
                //存储本次审批文件夹绝对路径
                String fileUrl = productApproving.getFileUrl();
                File file = new File(fileUrl);
                List<String> fileUrlList = new LinkedList<>();
                List<String> dwgUrlList = null;
                List<String> picUrlList = null;
                //存于物料下,bom内有对应
                List<String> materialUrlList = new LinkedList<>();
                //存于产品下,bom内没对应
                List<String> productUrlList = new LinkedList<>();
 
                List<Material> materialDwgUrlNameList = new LinkedList<>();
 
                fileUrlList = FileUtil.getStaticFilePath(file,fileUrlList);
                //图纸dwg 子件/产品
                dwgUrlList = fileUrlList.stream().filter(url->url.contains(".dwg")).collect(Collectors.toList());
                picUrlList = fileUrlList.stream().filter(url->url.contains(".png") || url.contains(".jpeg")).collect(Collectors.toList());
 
                dwgUrlList.forEach(dwgUrl->{
                    boolean existFlag = false;
                    for (ProductBomApproving approving :approvingList){
                        String filename = dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1, dwgUrl.length() - 4);
                        String fileFullName = dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1);
                        if(approving.getSubModel().toUpperCase().equals(filename.toUpperCase())){
                            materialUrlList.add(dwgUrl);
                            existFlag = true;
 
                            Material temp = new Material();
                            temp.setSubModel(approving.getSubModel());
                            temp.setNotes("doc_file" + File.separator + "material"
                                    + File.separator + fileFullName);
                            materialDwgUrlNameList.add(temp);
                            break;
                        }
                    }
                    if(!existFlag) {
                        productUrlList.add(dwgUrl);
                    }
                });
 
                //一定是有对应物料的,从bom内剥离的
                picUrlList.forEach(picUrl->{
                    for (ProductBomApproving approving :approvingList){
                        String filename = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1, picUrl.length() - 4);
                        String fileFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
                        if(approving.getSubModel().toUpperCase().equals(filename.toUpperCase())){
                            approving.setPictureUrl("doc_file" + File.separator + "material"
                            + File.separator + fileFullName);
                            break;
                        }
                    }
                });
 
                //转移路径
                String projectDir = CommonUtil.getProjectDir();
                //doc_file/product/{产品型号}/standard或者{customCode}}/{version}/
                //跟子件挂钩的转移到子件图纸下:doc_file/material/
                String customCodeString = null;
                if(customCode!=null && !customCode.equals("")) {
                    customCodeString = customCode;
                }else{
                    customCodeString = "standard";
                }
                String productDir = projectDir + File.separator + "doc_file" + File.separator + "product" + File.separator + parentModel
                        + File.separator + customCodeString + File.separator +nextVersion;
                String materialDir = projectDir + File.separator + "doc_file" + File.separator + "material";
                File productDirFile = new File(productDir);
                File materialFile = new File(materialDir);
                if(!productDirFile.exists()){
                    productDirFile.mkdirs();
                }
                if(!materialFile.exists()){
                    materialFile.mkdirs();
                }
                productUrlList.forEach(productUrl->{
                    String dwgName = productUrl.substring(productUrl.lastIndexOf(File.separator) + 1, productUrl.length() - 4);
 
                    try {
                        FileCopyUtils.copy(new File(productUrl),new File(productDir+File.separator+dwgName+".dwg"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
 
                materialUrlList.forEach(materialUrl->{
                    String dwgName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1, materialUrl.length() - 4);
                    try {
                        FileCopyUtils.copy(new File(materialUrl),new File(materialDir+File.separator+dwgName+".dwg"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
 
                picUrlList.forEach(picUrl->{
                    String picFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
                    try {
                        FileCopyUtils.copy(new File(picUrl),new File(materialDir+File.separator+picFullName));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
 
 
                //转移审批表数据到历史表/最新版本表 product_history/product_bom_history/product/product_bom
                ProductHistory productHistory = new ProductHistory();
                productHistory.setParentCode(parentCode);
                productHistory.setParentName(productApproving.getParentName());
                productHistory.setParentModel(parentModel);
                productHistory.setNotes(productApproving.getNotes());
                productHistory.setCustomCode(customCode);
                productHistory.setCreateTime(new Date());
                productHistory.setVersion(nextVersion);
                productHistory.setEnabled(1);
 
                Product productNew = new Product();
                productNew.setParentCode(parentCode);
                productNew.setParentName(productApproving.getParentName());
                productNew.setParentModel(parentModel);
                productNew.setNotes(productApproving.getNotes());
                productNew.setCustomCode(customCode);
                productNew.setCreateTime(new Date());
                productNew.setVersion(nextVersion);
 
                //实际生效的只有insert,因为状态设置为未启用已经在提交审批时变更了
                phService.insertAndUpdateEnabled(productHistory);
 
                productService.insert(productNew);
 
                List<ProductBomHistory> productBomHistoryList = new LinkedList<>();
                List<ProductBom> productBomList = new LinkedList<>();
                approvingList.forEach(approving->{
                    ProductBomHistory bomHistory = new ProductBomHistory();
                    ProductBom bom = new ProductBom();
 
                    bom.setProductId(productNew.getId());
                    bom.setCategory(approving.getCategory());
                    bom.setCreateDate(new Date());
                    bom.setDwgUrl(approving.getDwgUrl());
                    bom.setFileUrl(approving.getFileUrl());
                    bom.setMaterial(approving.getMaterial());
                    bom.setNotes(approving.getNotes());
                    bom.setPictureUrl(approving.getPictureUrl());
                    bom.setProducer(approving.getProducer());
                    bom.setQuantity(approving.getQuantity());
                    bom.setSubCode(approving.getSubCode());
                    bom.setSubModel(approving.getSubModel());
                    bom.setSubName(approving.getSubName());
                    bom.setSurfaceDetail(approving.getSurfaceDetail());
                    bom.setThickness(approving.getThickness());
                    bom.setType(approving.getType());
                    bom.setUnit(approving.getUnit());
                    //bom.setUpUserId(approving.getUpUserId());
                    bom.setVersion(nextVersion);
 
                    bomHistory.setProductId(productHistory.getId());
                    bomHistory.setCategory(approving.getCategory());
                    bomHistory.setCreateDate(new Date());
                    bomHistory.setDwgUrl(approving.getDwgUrl());
                    bomHistory.setFileUrl(approving.getFileUrl());
                    bomHistory.setMaterial(approving.getMaterial());
                    bomHistory.setNotes(approving.getNotes());
                    bomHistory.setPictureUrl(approving.getPictureUrl());
                    bomHistory.setProducer(approving.getProducer());
                    bomHistory.setQuantity(approving.getQuantity());
                    bomHistory.setSubCode(approving.getSubCode());
                    bomHistory.setSubModel(approving.getSubModel());
                    bomHistory.setSubName(approving.getSubName());
                    bomHistory.setSurfaceDetail(approving.getSurfaceDetail());
                    bomHistory.setThickness(approving.getThickness());
                    bomHistory.setType(approving.getType());
                    bomHistory.setUnit(approving.getUnit());
                    //bomHistory.setUpUserId(approving.getUpUserId());
                    bomHistory.setVersion(nextVersion);
 
                    productBomHistoryList.add(bomHistory);
                    productBomList.add(bom);
                });
                historyService.addBatch(productBomHistoryList);
                bomService.insertBatch(productBomList);
 
                //物料表中不存在的,则添加到物料表中去
                List<String> codeList = approvingList.stream().map(ProductBomApproving::getSubCode).collect(Collectors.toList());
                List<String> existCodeList = mService.getListByCodeList(codeList);
                List<Material> materialList = new LinkedList<>();
                approvingList.forEach(approving->{
                    if(!existCodeList.contains(approving.getSubCode())){ //这个审批bom中的物料不在物料管理内
                        Material temp = new Material();
                        //temp.setCategory(approving.getCategory());
                        temp.setCreateDate(new Date());
                        temp.setDwgUrl(approving.getDwgUrl());
                        temp.setFileUrl(approving.getFileUrl());
                        //temp.setMaterial(approving.getMaterial());
                        temp.setNotes(approving.getNotes());
                        temp.setPictureUrl(approving.getPictureUrl());
                        //temp.setProducer(approving.getProducer());
                        //TODO quantity
                        //temp.setQuantity(approving.getQuantity());
                        temp.setStatus(1);
                        temp.setSubCode(approving.getSubCode());
                        temp.setSubModel(approving.getSubModel());
                        temp.setSubName(approving.getSubName());
                        //temp.setSurfaceDetail(approving.getSurfaceDetail());
                        //temp.setThickness(approving.getThickness());
                        temp.setType(approving.getType());
                        temp.setUnit(approving.getUnit());
                        materialList.add(temp);
                    }
                });
                if(materialList.size()!=0) {
                    mService.insertBatch(materialList);
                }
                //将dwg图纸,全部更新到对应的记录url中
                if(materialDwgUrlNameList.size()!=0) {
                    mService.updateDwgUrl(materialDwgUrlNameList);
                }
                /*List<ProductBomApproving> fileBomApprovingList = approvingList.stream()
                        .filter(productBomApproving ->
                                productBomApproving.getPictureUrl() != null || productBomApproving.getDwgUrl() != null
                        ).collect(Collectors.toList());
                String projectDir = CommonUtil.getProjectDir();
                fileBomApprovingList.forEach(fileBom -> {
                    try {
                        String pictureUrl = fileBom.getPictureUrl();
                        String dwgUrl = fileBom.getDwgUrl();
                        if (pictureUrl != null) {
                            String newPictureUrl = "doc_file" + File.separator + "product" + File.separator + fileBom.getParentModel() + File.separator + pictureUrl.substring(pictureUrl.lastIndexOf(File.separator) + 1);
                            File newPictureDir = new File(projectDir + File.separator + "doc_file" + File.separator + "product" + File.separator + fileBom.getParentModel());
                            if (!newPictureDir.exists()) {
                                newPictureDir.mkdirs();
                            }
                            FileCopyUtils.copy(new File(projectDir + File.separator + pictureUrl),
                                    new File(projectDir + File.separator + newPictureUrl));
                            fileBom.setPictureUrl(newPictureUrl);
                        }
                        if (dwgUrl != null) {
                            String newDwgUrl = "doc_file" + File.separator + "product" + File.separator + fileBom.getParentModel() + File.separator + dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1);
                            File newDwgDir = new File(projectDir + File.separator + "doc_file" + File.separator + "product" + File.separator + fileBom.getParentModel());
                            if (!newDwgDir.exists()) {
                                newDwgDir.mkdirs();
                            }
                            FileCopyUtils.copy(new File(projectDir + File.separator + dwgUrl),
                                    new File(projectDir + File.separator + newDwgUrl));
                            fileBom.setDwgUrl(newDwgUrl);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
                //更新正式bom的对应url
                bomService.updateUrl(fileBomApprovingList);*/
            }
            else if(type.intValue() == EnumWorksheetType.Material.getType()){ //物料
                List<MaterialApproving> approvingList = mAService.getListByMainId(link.getMainId());
                List<Material> materialList = new LinkedList<>();
                //区分是新增还是删除
                Integer materialId = approvingList.get(0).getMaterialId();
                if(materialId==null) { //新增
                    String fileUrl = approvingList.get(0).getFileUrl();
                    File file = new File(fileUrl);
 
                    List<String> fileUrlList = new LinkedList<>();
                    List<String> dwgUrlList = null;
                    List<String> materialUrlList = new LinkedList<>();
                    List<Material> materialDwgUrlNameList = new LinkedList<>();
 
                    fileUrlList = FileUtil.getStaticFilePath(file,fileUrlList);
                    dwgUrlList = fileUrlList.stream().filter(url->url.contains(".dwg")).collect(Collectors.toList());
                    //图纸与物料model对应,则图纸需要保留并转移到正式文件夹
                    //同时,保留图纸对应的物料model和正式文件夹中的http url
                    dwgUrlList.forEach(dwgUrl-> {
                        approvingList.forEach(approving -> {
                            String filename = dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1, dwgUrl.length() - 4);
                            String fileFullName = dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1);
                            if (approving.getSubModel().toUpperCase().equals(filename.toUpperCase())) {
                                materialUrlList.add(dwgUrl);
 
                                Material temp = new Material();
                                temp.setSubModel(approving.getSubModel());
                                temp.setNotes("doc_file" + File.separator + "material"
                                        + File.separator + fileFullName);
                                materialDwgUrlNameList.add(temp);
                            }
                        });
                    });
                    String projectDir = CommonUtil.getProjectDir();
                    String materialDir = projectDir + File.separator + "doc_file" + File.separator + "material";
                    File materialFile = new File(materialDir);
                    if(!materialFile.exists()){
                        materialFile.mkdirs();
                    }
 
                    materialUrlList.forEach(materialUrl->{
                        String dwgName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1, materialUrl.length() - 4);
                        try {
                            FileCopyUtils.copy(new File(materialUrl),new File(materialDir+File.separator+dwgName+".dwg"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
                    //将物料从审批表中转移到正式表
                    approvingList.forEach(approve -> {
                        Material material = new Material();
                        //material.setCategory(approve.getCategory());
                        material.setSubCode(approve.getSubCode());
                        material.setSubName(approve.getSubName());
                        material.setSubModel(approve.getSubModel());
                        material.setUnit(approve.getUnit());
                        //TODO quantity
                        //material.setQuantity(approve.getQuantity());
                        //material.setProducer(approve.getProducer());
                        material.setNotes(approve.getNotes());
                        material.setCreateDate(new Date());
                        material.setStatus(1);
                        materialDwgUrlNameList.forEach(materialDwgUrlName->{
                            if(materialDwgUrlName.getSubModel().equals(approve.getSubModel())){
                                material.setDwgUrl(materialDwgUrlName.getDwgUrl());
                            }
                        });
                        materialList.add(material);
                    });
                    mService.insertBatch(materialList);
                }else{ //删除
                    List<Integer> materialIdList = approvingList.stream().map(MaterialApproving::getMaterialId).collect(Collectors.toList());
                    mService.deleteBatch(materialIdList);
                }
                //更新物料审批子表中type为完结
                mAService.endStatus(link.getMainId());
            }
            else if(type.intValue() == EnumWorksheetType.MaterialProduct.getType()){ //更新物料-产品关系
                //查找到对应的物料-产品关系数据
                List<MaterialProductApproving> cpAList = cpAService.getListByMainId(link.getMainId());
                //当前版本
                int version = cpAList.get(0).getVersion();
                //新增关联/移除关联
                Map<Integer, List<MaterialProductApproving>> linkTypeMap = cpAList.stream().collect(Collectors.groupingBy(MaterialProductApproving::getLinkType));
                List<MaterialProductHistory> addedList = new LinkedList<>();
                List<MaterialProductApproving> removedList = new LinkedList<>();
                linkTypeMap.forEach((linkType,list)->{
                    if(linkType == 1){
                        list.forEach(approving->{
                            MaterialProductHistory his = new MaterialProductHistory();
                            his.setParentCode(approving.getParentCode());
                            his.setCustomCode(approving.getCustomCode());
                            his.setSVersion(approving.getVersion());
                            his.setEVersion(approving.getVersion());
                            his.setSubMaterialId(approving.getSubMaterialId());
                            his.setQuantity(approving.getQuantity());
                            his.setMaterialId(approving.getMaterialId());
                            his.setCreateTime(new Date());
 
                            addedList.add(his);
                        });
                    }else{
                        removedList.addAll(list);
                    }
                });
                //新增关联
                if(addedList.size()!=0) {
                    mphService.insertBatch(addedList);
                }
                //移除关联
                if(removedList.size()!=0) {
                    mphService.updateVersionSubtractBatch(removedList);
                }
            }
            else if(type.intValue() == EnumWorksheetType.ProductSoftware.getType()){ //软件下载
                //转移记录到正式表
                ProductSoftwareApproving productSoftwareApproving = productSoftwareApprovingService.getByMainId(link.getMainId());
                ProductSoftware productSoftware = new ProductSoftware();
                productSoftware.setParentModel(productSoftwareApproving.getParentModel());
                productSoftware.setSoftwareName(productSoftwareApproving.getSoftwareName());
                productSoftware.setSoftwareUrl(productSoftwareApproving.getSoftwareUrl());
                productSoftware.setSubmitTime(productSoftwareApproving.getCreateTime());
                productSoftware.setCreateTime(new Date());
 
                productSoftwareService.insert(productSoftware);
            }
        }
    }
 
    /**
     * 获取节点信息(包含main表)列表
     * @param id
     * @return
     */
    public List<WorksheetLink> getInfoList(Long id) {
        return linkMapper.getInfoList(id);
    }
 
    /**
     *
     * @param userId 总经理对应的userId
     * @param statusExp 0:未审批,1:已审批(包含状态值1,2)
     * @return
     */
    public List<WorksheetMain> getInfoList2(Long userId, int statusExp) {
        return linkMapper.getInfoList2(userId,statusExp);
    }
 
    /**
     *
     * @param userId 总经理对应的userId
     * @param statusExp 0:未审批,1:已审批(包含状态值1,2)
     * @return
     */
    public List<WorksheetMain> getInfoList3(Long userId, int statusExp) {
        return linkMapper.getInfoList3(userId,statusExp);
    }
 
    public DocUser getApprovingUser(Integer mainId) {
        return linkMapper.getApprovingUser(mainId);
    }
 
    @Transactional
    public Response confirmProductProcedureAndSOP(WorksheetLink link) throws IOException {
        Integer mainId = linkMapper.selectById(link.getId()).getMainId();
        //更新子表
        linkMapper.updateById(link);
        if(link.getLinkStatus() ==1){
            //查询当前单据子表是否已经全部同意
            QueryWrapper<WorksheetLink> query = Wrappers.query();
            query.eq("main_id",mainId);
            List<WorksheetLink> linkList = linkMapper.selectList(query);
            List<WorksheetLink> rejectList = linkList.stream().filter(linkFilter -> linkFilter.getLinkStatus() != 1).collect(Collectors.toList());
            if(rejectList == null){
                //都为1,已确认
                mainService.updateEndStatusById(mainId,"已确认",5);
                //文件转移到审批完成后的正式路径 文件保存在/doc_file/procedure/xxx.doc
                ProcedureDocApproving approving = procedureDocApprovingService.getByMainId(mainId);
                String fileRelativePath = approving.getFile();
                String projectDir = CommonUtil.getProjectDir();
                String inFilePath = projectDir + File.separator + fileRelativePath;
                String outFilePath = (projectDir + File.separator + fileRelativePath).replace("_approving","");
                WordUtil.fillValue(inFilePath,outFilePath,Arrays.asList(approving.getFzr(),approving.getBz(),approving.getZz(),approving.getPb()));
                //删除之前预览产生的中间文件
                String filename = fileRelativePath.substring(fileRelativePath.lastIndexOf(File.separator)+1,fileRelativePath.lastIndexOf("."));
                File approvingDir = new File(projectDir + File.separator + "procedure_approving");
                FilenameFilter filenameFilter = new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String name) {
                        name = name.substring(0,name.lastIndexOf("."));
                        if(name.contains(filename) && !name.equals(filename)){
                            return true;
                        }
                        return false;
                    }
                };
                File[] files = approvingDir.listFiles(filenameFilter);
                for (File file : files) {
                    file.delete();
                }
 
                //流程文件数据转移
                ProcedureDoc procedureDoc = new ProcedureDoc();
                procedureDoc.setName(approving.getName());
                procedureDoc.setDescription(approving.getDescription());
                procedureDoc.setVersion(approving.getVersion());
                procedureDoc.setFzr(approving.getFzr());
                procedureDoc.setBz(approving.getBz());
                procedureDoc.setType(approving.getType());
                procedureDoc.setCreateTime(new Date());
                procedureDoc.setFile(fileRelativePath.replace("_approving",""));
                procedureDocService.insert(procedureDoc);
            }
        }else{
            mainService.updateEndStatusById(mainId,"已驳回",0);
        }
        return new Response().set(1);
    }
}