lxw
2022-08-17 43ded7d2ae74356ff8a53a7805d20b7dbfa6a729
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
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.mapper.ProductBomApprovingMapper;
import com.whyc.mapper.WorksheetLinkMapper;
import com.whyc.pojo.*;
import com.whyc.util.CommonUtil;
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.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
 
@Service
public class WorksheetLinkService {
 
    @Resource
    private WorksheetMainService mainService;
 
    @Resource
    private WorksheetLinkMapper linkMapper;
 
    @Autowired
    private ProductBomApprovingMapper approvingMapper;
 
    @Autowired
    private ProductBomService bomService;
 
    @Autowired
    private ProductBomHistoryService historyService;
 
    @Autowired
    private ComponentProductHistoryService cphService;
 
    @Autowired
    private ComponentProductApprovingService cpAService;
 
    @Autowired
    private ComponentApprovingService cAService;
 
    @Autowired
    private ComponentService cService;
 
    @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.Component.getType())){
                cAService.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);
            link2.setRejectVersion(link.getRejectVersion());
            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);
                link2.setRejectVersion(link.getRejectVersion());
                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()) { //产品审批
                //将产品文件复制至正式路径
                QueryWrapper<ProductBomApproving> query = Wrappers.query();
                query.eq("main_id", link.getMainId());
                List<ProductBomApproving> approvingList = approvingMapper.selectList(query);
 
                //增加->增加部件(增加记录,同时所有eVersion+1)
                //修改->修改部件图纸,修改部件非图纸(增加记录,同时修改非原部件的所有eVersion+1)
                //删除? TODO 需要约定逻辑
 
                //查询部件最新的版本号
                ProductBom product = bomService.getProduct(approvingList.get(0).getParentModel());
                int currentVersion = -1;
                if (product != null) {
                    currentVersion = product.getVersion();
                }
                Integer nextVersion = currentVersion + 1;
                //更新到product_bom_history,增加进去的需要sVersion和eVersion一致
                //增加所有部件,排查出相关的原部件,非也是更新
                List<ProductBomHistory> currentHistoryList = historyService.getListByParentModel(approvingList.get(0).getParentModel(), currentVersion);
                List<String> currentSubNameList = currentHistoryList.stream().map(ProductBomHistory::getSubName).collect(Collectors.toList());
 
                List<ProductBomHistory> newHistoryList = new LinkedList<>();
                approvingList.forEach(approvingBom -> {
                    if (currentSubNameList.contains(approvingBom.getSubName())) {
                        approvingBom.setVersion(1);
                    } else {
                        approvingBom.setVersion(0);
                    }
                    //转化为productBomHistory
                    ProductBomHistory his = new ProductBomHistory();
                    his.setCategory(approvingBom.getCategory());
                    his.setCreateDate(approvingBom.getCreateDate());
                    his.setDwgUrl(approvingBom.getDwgUrl());
                    his.setEVersion(nextVersion);
                    his.setFileUrl(approvingBom.getFileUrl());
                    his.setMaterial(approvingBom.getMaterial());
                    his.setNotes(approvingBom.getNotes());
                    his.setParentCode(approvingBom.getParentCode());
                    his.setParentModel(approvingBom.getParentModel());
                    his.setParentName(approvingBom.getParentName());
                    his.setParentVersion(approvingBom.getParentVersion());
                    his.setPictureUrl(approvingBom.getPictureUrl());
                    his.setProducer(approvingBom.getProducer());
                    his.setQuantity(approvingBom.getQuantity());
                    his.setSubCode(approvingBom.getSubCode());
                    his.setSubModel(approvingBom.getSubModel());
                    his.setSubName(approvingBom.getSubName());
                    his.setSurfaceDetail(approvingBom.getSurfaceDetail());
                    his.setSVersion(nextVersion);
                    his.setThickness(approvingBom.getThickness());
                    his.setType(approvingBom.getType());
                    his.setUnit(approvingBom.getUnit());
                    his.setUpUserId(approvingBom.getUpUserId());
 
                    newHistoryList.add(his);
                });
                //本次审核中子件被修改的子件集合
                List<String> approvingUpdateSubNameList = approvingList.stream().filter(approvingBom -> approvingBom.getVersion() == 1).map(ProductBomApproving::getSubName).collect(Collectors.toList());
 
                historyService.addBatch(newHistoryList);
                /*更新产品的当前版本,更新到最新的版本*/
                //当前版本的所有bom的eVersion更新,排除被修改的子件
                List<ProductBomHistory> newVersionCurrentHistoryList = currentHistoryList.stream()
                        .filter(currentHistory -> !approvingUpdateSubNameList.contains(currentHistory.getSubName()))
                        .collect(Collectors.toList());
                newVersionCurrentHistoryList.forEach(history -> {
                    history.setEVersion(nextVersion);
                });
                if (newVersionCurrentHistoryList.size() != 0) {
                    historyService.updateVersionBatch(newVersionCurrentHistoryList);
                }
                //更新散件表当前版本,更新eVersion
                List<ComponentProductHistory> cphList = cphService.getListByParentModel(approvingList.get(0).getParentModel(),currentVersion);
                if(cphList.size()!=0){cphService.updateVersionBatch(cphList);}
 
                /*更新到product_bom*/
                //查询新的版本
                List<ProductBomHistory> newBomList = historyService.getListByParentModel(approvingList.get(0).getParentModel(), nextVersion);
                bomService.updateNewBom(newBomList);
 
                /*String projectDir = CommonUtil.getProjectDir();
                FileUtil.copyDir()*/
 
 
                /*将产品bom表的url修正,更新到正式表*/
                //审批完后,将本次的bom带url的全部复制到正式文件夹中
                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.Component.getType()){ //散装件
                List<ComponentApproving> approvingList = cAService.getListByMainId(link.getMainId());
                List<Component> componentList = new LinkedList<>();
                //区分是新增还是删除
                Integer componentId = approvingList.get(0).getComponentId();
                if(componentId==null) { //新增
                    //将散装件从审批表中转移到正式表
                    approvingList.forEach(approve -> {
                        Component component = new Component();
                        component.setCategory(approve.getCategory());
                        component.setSubCode(approve.getSubCode());
                        component.setSubName(approve.getSubName());
                        component.setSubModel(approve.getSubModel());
                        component.setUnit(approve.getUnit());
                        component.setQuantity(approve.getQuantity());
                        component.setProducer(approve.getProducer());
                        component.setNotes(approve.getNotes());
                        component.setCreateDate(new Date());
                        component.setStatus(1);
                        componentList.add(component);
                    });
                    cService.insertBatch(componentList);
                }else{ //删除
                    List<Integer> componentIdList = approvingList.stream().map(ComponentApproving::getComponentId).collect(Collectors.toList());
                    cService.deleteBatch(componentIdList);
                }
                //更新散装件审批子表中type为完结
                cAService.endStatus(link.getMainId());
            }
            else if(type.intValue() == EnumWorksheetType.ComponentProduct.getType()){ //更新散装件-产品关系
                //查找到对应的散装件-产品关系数据
                List<ComponentProductApproving> cpAList = cpAService.getListByMainId(link.getMainId());
                //查询部件最新的版本号
                ProductBom product = bomService.getProduct(cpAList.get(0).getParentModel());
                int currentVersion = -1;
                if (product != null) {
                    currentVersion = product.getVersion();
                }
                Integer nextVersion = currentVersion + 1;
                //关联/接触关联,更新版本
                //替换,更新版本,同时bom表历史中对应的subName 版本号不变(排除更新)
                Map<Integer, List<ComponentProductApproving>> linkTypeMap = cpAList.stream().collect(Collectors.groupingBy(ComponentProductApproving::getLinkType));
                List<ComponentProductApproving> addedList = new LinkedList<>();
                List<ComponentProductApproving> replacedList = new LinkedList<>();
                List<ComponentProductApproving> removedList = new LinkedList<>();
                linkTypeMap.forEach((linkType,list)->{
                    if(linkType == 1){
                        addedList.addAll(list);
                    }else if(linkType == 2){
                        replacedList.addAll(list);
                    }else{
                        removedList.addAll(list);
                    }
                });
                List<ComponentProductHistory> newHistoryList = new LinkedList<>();
                //查询最新版本生效的相关散装件
                List<ComponentProductHistory> nowHistoryList = cphService.getListByParentModel(cpAList.get(0).getParentModel(), currentVersion);
                //查询历史中最新版本生效的bom
                List<ProductBomHistory> nowBomHistoryList = historyService.getListByParentModel(cpAList.get(0).getParentModel(), currentVersion);
 
                /*
                对于关联的,判断当前版本中是否存在替换关系,存在,则直接下个版本插入关联;
                同时,其他当前版本的相关散装件-产品记录更新版本,排除存在的替换关系
                */
 
                /*
                对于替换的,判断当前版本中是否存在关联关系,存在,直接下个版本替换,记录插入;
                同时,当前版本的相关散装件-产品记录更新版本,排除存在的关联关系;
                同时,bom表更新到下个版本时,排除被替换件
                */
 
                //对于解除关联的,当前版本的相关散装件-产品记录更新版本,排除解除关联的记录
                //处理思路:先解除关联,再替换,再关联
                if(removedList.size()!=0){
                    removedList.forEach(remove -> {
                        /*nowHistoryList.forEach(nowHistory -> {
                            if (remove.getComponentId().intValue() == nowHistory.getComponentId()) {
                                nowHistoryList.remove(nowHistory);
                            }
                        });*/
                        Iterator<ComponentProductHistory> iterator = nowHistoryList.iterator();
                        while (iterator.hasNext()){
                            ComponentProductHistory next = iterator.next();
                            if(remove.getComponentId().intValue() == next.getComponentId()
                                &&remove.getSubName().equals(next.getSubName())
                            ){
                                iterator.remove();
                                break;
                            }
                        }
                    });
                }
 
                //处理替换
                if(replacedList.size()!=0){
                    replacedList.forEach(replace -> {
                        //当前生效的散装件-产品列表
                        /*nowHistoryList.forEach(nowHistory -> {
                            if (replace.getComponentId().intValue() == nowHistory.getComponentId()) {
                                nowHistoryList.remove(nowHistory);
                            }
                        });*/
                        if(nowHistoryList.size()!=0) {
                            Iterator<ComponentProductHistory> iterator = nowHistoryList.iterator();
                            while (iterator.hasNext()) {
                                ComponentProductHistory next = iterator.next();
                                if (replace.getComponentId().intValue() == next.getComponentId()
                                        && replace.getSubName().equals(next.getSubName())
                                ) {
                                    iterator.remove();
                                    break;
                                }
                            }
                        }
                        //下个版本新增替换的散装件-产品列表
                        ComponentProductHistory newHistory = new ComponentProductHistory();
                        newHistory.setComponentId(replace.getComponentId());
                        newHistory.setParentModel(replace.getParentModel());
                        newHistory.setSubName(replace.getSubName());
                        newHistory.setQuantity(replace.getQuantity());
                        newHistory.setCreateTime(new Date());
                        newHistory.setLinkType(2);
                        newHistory.setSVersion(nextVersion);
                        newHistory.setEVersion(nextVersion);
                        newHistoryList.add(newHistory);
                        //当前生效的bom列表
                        /*nowBomHistoryList.forEach(nowBomHistory->{
                            if(replace.getSubName().equals(nowBomHistory.getSubName())){
                                nowBomHistoryList.remove(nowBomHistory);
                            }
                        });*/
                        Iterator<ProductBomHistory> iterator1 = nowBomHistoryList.iterator();
                        while (iterator1.hasNext()){
                            if(replace.getSubName().equals(iterator1.next().getSubName())){
                                iterator1.remove();
                                break;
                            }
                        }
                    });
                }
 
                //处理关联
                addedList.forEach(add -> {
                    /*nowHistoryList.forEach(nowHistory -> {
                        if (add.getComponentId().intValue() == nowHistory.getComponentId()) {
                            nowHistoryList.remove(nowHistory);
                        }
                    });*/
                    if(nowHistoryList.size()!=0) {
                        Iterator<ComponentProductHistory> iterator = nowHistoryList.iterator();
                        while (iterator.hasNext()) {
                            ComponentProductHistory next = iterator.next();
                            if (add.getComponentId().intValue() == next.getComponentId()
                                    && add.getSubName().equals(next.getSubName())
                            ) {
                                iterator.remove();
                                break;
                            }
                        }
                    }
 
                    ComponentProductHistory newHistory = new ComponentProductHistory();
                    newHistory.setComponentId(add.getComponentId());
                    newHistory.setParentModel(add.getParentModel());
                    newHistory.setSubName(add.getSubName());
                    newHistory.setQuantity(add.getQuantity());
                    newHistory.setCreateTime(new Date());
                    newHistory.setLinkType(1);
                    newHistory.setSVersion(nextVersion);
                    newHistory.setEVersion(nextVersion);
                    newHistoryList.add(newHistory);
 
                });
 
                //处理完成,进行表单数据更新,分为nowHistoryList,newHistoryList,nowBomHistoryList
                //nowHistoryList.forEach(nowHis->nowHis.setEVersion(nextVersion));
                if(newHistoryList.size()!=0){newHistoryList.forEach(newHis->{newHis.setEVersion(nextVersion);newHis.setSVersion(nextVersion);});}
                if(nowBomHistoryList.size()!=0){nowBomHistoryList.forEach(nowBomHis->nowBomHis.setEVersion(nextVersion));}
 
                if(nowHistoryList.size()!=0){cphService.updateVersionBatch(nowHistoryList);}
                if(newHistoryList.size()!=0){cphService.insertBatch(newHistoryList);}
                if(nowBomHistoryList.size()!=0){historyService.updateVersionBatch(nowBomHistoryList);}
                //更新ProductBom
                List<ProductBomHistory> newBomList = historyService.getListByParentModel(cpAList.get(0).getParentModel(), nextVersion);
                bomService.updateNewBom(newBomList);
            }
            else if(type.intValue() == EnumWorksheetType.ProductSoftware.getType()){
                //转移记录
            }
        }
    }
 
    /**
     * 获取节点信息(包含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);
    }
}