From d8ca9ccd14fe70e83bd586468cbc3db569f91de2 Mon Sep 17 00:00:00 2001
From: whycxzp <perryhsu@163.com>
Date: 星期三, 10 八月 2022 11:42:10 +0800
Subject: [PATCH] 更新散装件审批

---
 src/main/java/com/whyc/service/WorksheetLinkService.java |  350 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 319 insertions(+), 31 deletions(-)

diff --git a/src/main/java/com/whyc/service/WorksheetLinkService.java b/src/main/java/com/whyc/service/WorksheetLinkService.java
index 2d58845..cd706d3 100644
--- a/src/main/java/com/whyc/service/WorksheetLinkService.java
+++ b/src/main/java/com/whyc/service/WorksheetLinkService.java
@@ -1,13 +1,26 @@
 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.WorksheetLink;
+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.Date;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
 
 @Service
 public class WorksheetLinkService {
@@ -18,6 +31,20 @@
     @Resource
     private WorksheetLinkMapper linkMapper;
 
+    @Autowired
+    private ProductBomApprovingMapper approvingMapper;
+
+    @Autowired
+    private ProductBomService bomService;
+
+    @Autowired
+    private ProductBomHistoryService historyService;
+
+    @Autowired
+    private ComponentProductHistoryService cphService;
+
+    @Autowired
+    private ComponentProductApprovingService cpAService;
 
     @Transactional
     public void audit(WorksheetLink link) {
@@ -25,20 +52,20 @@
         //鏇存柊鑺傜偣淇℃伅
         linkMapper.updateById(link);
         if(link.getLinkStatus() == 2){ //椹冲洖
-            //椹冲洖鍒板憳宸ヨ繘琛屽鐞�
-            //鏌ヨ宸ュ崟鐨勫憳宸�
-            Long createUserId = mainService.getInfoById(link.getMainId()).getCreateUserId();
-            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(),0);
+            //椤圭洰缁忕悊椹冲洖,椹冲洖鍗崇粓姝�
+            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);
+                //杩欎釜鏄棤鏁堢殑,鐢ㄦ埛璺熻鑹插姣�,涓嶇,涓嬮潰浠g爜姘镐笉鐢熸晥
+                if (parentLink.getDealUserId() == 1003) {
+                    mainService.updateEndStatusById(link.getMainId(),
+                            "缁忕悊椹冲洖,椹冲洖鍘熷洜:" + link.getDealReason() + ";鎬荤粡鐞嗛┏鍥�,椹冲洖鍘熷洜:" + parentLink.getDealReason(),
+                            0);
+                }
+            }
         }else{
             //杩涘叆涓嬩竴鑺傜偣,鎬荤粡鐞嗗鎵�
             WorksheetLink link2 = new WorksheetLink();
@@ -49,6 +76,7 @@
             link2.setDealDesc("宸ュ崟琚粡鐞嗗鏍搁�氳繃,淇℃伅:"+link.getDealReason());
             link2.setLinkStatus(0);
             link2.setEnableArchive(1);
+            link2.setRejectVersion(link.getRejectVersion());
             linkMapper.insert(link2);
             //鏇存柊涓昏〃鐘舵��
             mainService.updateStatusById(link.getMainId(),2);
@@ -57,27 +85,263 @@
 
     @Transactional
     public void approve(WorksheetLink link) {
+        //鏍规嵁鑺傜偣,鏌ヨ涓昏〃绫诲瀷
+        Integer type = mainService.getInfoById(link.getMainId()).getType();
         link.setDealTime(new Date());
         //鏇存柊鑺傜偣淇℃伅
         linkMapper.updateById(link);
         if(link.getLinkStatus() == 2){ //椹冲洖
-            //椹冲洖鍒板憳宸ヨ繘琛屽鐞�
-            //鏌ヨ宸ュ崟鐨勫憳宸�
-            Long createUserId = mainService.getInfoById(link.getMainId()).getCreateUserId();
-            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(),0);
-        }else{
+            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);
+            mainService.updateEndStatusById(link.getMainId(), "瀹岀粨", 5);
+            if (type.intValue() == EnumWorksheetType.ProductBom.getType()) { //浜у搧瀹℃壒
+                //灏嗕骇鍝佹枃浠跺鍒惰嚦姝e紡璺緞
+                QueryWrapper<ProductBomApproving> query = Wrappers.query();
+                query.eq("main_id", link.getMainId());
+                List<ProductBomApproving> approvingList = approvingMapper.selectList(query);
+
+                //澧炲姞->澧炲姞閮ㄤ欢(澧炲姞璁板綍,鍚屾椂鎵�鏈塭Version+1)
+                //淇敼->淇敼閮ㄤ欢鍥剧焊,淇敼閮ㄤ欢闈炲浘绾�(澧炲姞璁板綍,鍚屾椂淇敼闈炲師閮ㄤ欢鐨勬墍鏈塭Version+1)
+                //鍒犻櫎? TODO 闇�瑕佺害瀹氶�昏緫
+
+                //鏌ヨ閮ㄤ欢鏈�鏂扮殑鐗堟湰鍙�
+                ProductBom product = bomService.getProduct(approvingList.get(0).getParentModel());
+                int currentVersion = -1;
+                if (product != null) {
+                    currentVersion = product.getVersion();
+                }
+                Integer nextVersion = currentVersion + 1;
+                //鏇存柊鍒皃roduct_bom_history,澧炲姞杩涘幓鐨勯渶瑕乻Version鍜宔Version涓�鑷�
+                //澧炲姞鎵�鏈夐儴浠�,鎺掓煡鍑虹浉鍏崇殑鍘熼儴浠�,闈炰篃鏄洿鏂�
+                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);
+                    }
+                    //杞寲涓簆roductBomHistory
+                    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);
+                /*鏇存柊浜у搧鐨勫綋鍓嶇増鏈�,鏇存柊鍒版渶鏂扮殑鐗堟湰*/
+                //褰撳墠鐗堟湰鐨勬墍鏈塨om鐨別Version鏇存柊,鎺掗櫎琚慨鏀圭殑瀛愪欢
+                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);
+                }
+                //鏇存柊鏁d欢琛ㄥ綋鍓嶇増鏈�,鏇存柊eVersion
+                List<ComponentProductHistory> cphList = cphService.getListByParentModel(approvingList.get(0).getParentModel(),currentVersion);
+                cphService.updateVersionBatch(cphList);
+
+                /*鏇存柊鍒皃roduct_bom*/
+                //鏌ヨ鏂扮殑鐗堟湰
+                List<ProductBomHistory> newBomList = historyService.getListByParentModel(approvingList.get(0).getParentModel(), nextVersion);
+                bomService.updateNewBom(newBomList);
+
+                /*String projectDir = CommonUtil.getProjectDir();
+                FileUtil.copyDir()*/
+
+
+                /*灏嗕骇鍝乥om琛ㄧ殑url淇,鏇存柊鍒版寮忚〃*/
+                //瀹℃壒瀹屽悗,灏嗘湰娆$殑bom甯rl鐨勫叏閮ㄥ鍒跺埌姝e紡鏂囦欢澶逛腑
+                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();
+                    }
+                });
+                //鏇存柊姝e紡bom鐨勫搴攗rl
+                bomService.updateUrl(fileBomApprovingList);
+            }
+            else if(type.intValue() == EnumWorksheetType.Component.getType()){ //鏁h浠� TODO
+
+            }
+            else if(type.intValue() == EnumWorksheetType.ComponentProduct.getType()){ //鏇存柊鏁h浠�-浜у搧鍏崇郴
+                //鏌ユ壘鍒板搴旂殑鏁h浠�-浜у搧鍏崇郴鏁版嵁
+                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琛ㄥ巻鍙蹭腑瀵瑰簲鐨剆ubName 鐗堟湰鍙蜂笉鍙�(鎺掗櫎鏇存柊)
+                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<>();
+                //鏌ヨ鏈�鏂扮増鏈敓鏁堢殑鐩稿叧鏁h浠�
+                List<ComponentProductHistory> nowHistoryList = cphService.getListByParentModel(cpAList.get(0).getParentModel(), currentVersion);
+                //鏌ヨ鍘嗗彶涓渶鏂扮増鏈敓鏁堢殑bom
+                List<ProductBomHistory> nowBomHistoryList = historyService.getListByParentModel(cpAList.get(0).getParentModel(), currentVersion);
+
+                /*
+                瀵逛簬鍏宠仈鐨�,鍒ゆ柇褰撳墠鐗堟湰涓槸鍚﹀瓨鍦ㄦ浛鎹㈠叧绯�,瀛樺湪,鍒欑洿鎺ヤ笅涓増鏈彃鍏ュ叧鑱�;
+                鍚屾椂,鍏朵粬褰撳墠鐗堟湰鐨勭浉鍏虫暎瑁呬欢-浜у搧璁板綍鏇存柊鐗堟湰,鎺掗櫎瀛樺湪鐨勬浛鎹㈠叧绯�
+                */
+
+                /*
+                瀵逛簬鏇挎崲鐨�,鍒ゆ柇褰撳墠鐗堟湰涓槸鍚﹀瓨鍦ㄥ叧鑱斿叧绯�,瀛樺湪,鐩存帴涓嬩釜鐗堟湰鏇挎崲,璁板綍鎻掑叆;
+                鍚屾椂,褰撳墠鐗堟湰鐨勭浉鍏虫暎瑁呬欢-浜у搧璁板綍鏇存柊鐗堟湰,鎺掗櫎瀛樺湪鐨勫叧鑱斿叧绯�;
+                鍚屾椂,bom琛ㄦ洿鏂板埌涓嬩釜鐗堟湰鏃�,鎺掗櫎琚浛鎹欢
+                */
+
+                //瀵逛簬瑙i櫎鍏宠仈鐨�,褰撳墠鐗堟湰鐨勭浉鍏虫暎瑁呬欢-浜у搧璁板綍鏇存柊鐗堟湰,鎺掗櫎瑙i櫎鍏宠仈鐨勮褰�
+                //澶勭悊鎬濊矾:鍏堣В闄ゅ叧鑱�,鍐嶆浛鎹�,鍐嶅叧鑱�
+                if(removedList.size()!=0){
+                    removedList.forEach(remove -> {
+                        nowHistoryList.forEach(nowHistory -> {
+                            if (remove.getComponentId().intValue() == nowHistory.getComponentId()) {
+                                nowHistoryList.remove(nowHistory);
+                            }
+                        });
+                    });
+                }
+
+                //澶勭悊鏇挎崲
+                if(replacedList.size()!=0){
+                    replacedList.forEach(replace -> {
+                        //褰撳墠鐢熸晥鐨勬暎瑁呬欢-浜у搧鍒楄〃
+                        nowHistoryList.forEach(nowHistory -> {
+                            if (replace.getComponentId().intValue() == nowHistory.getComponentId()) {
+                                nowHistoryList.remove(nowHistory);
+                            }
+                        });
+                        //涓嬩釜鐗堟湰鏂板鐨勬暎瑁呬欢-浜у搧鍒楄〃
+                        ComponentProductHistory newHistory = new ComponentProductHistory();
+                        newHistory.setComponentId(replace.getComponentId());
+                        newHistory.setParentModel(replace.getParentModel());
+                        newHistory.setSubName(replace.getSubName());
+                        newHistory.setCreateTime(new Date());
+                        newHistory.setLinkType(2);
+                        newHistory.setSVersion(nextVersion);
+                        newHistory.setEVersion(nextVersion);
+                        newHistoryList.add(newHistory);
+                        //褰撳墠鐢熸晥鐨刡om鍒楄〃
+                        nowBomHistoryList.forEach(nowBomHistory->{
+                            if(replace.getSubName().equals(nowBomHistory.getSubName())){
+                                nowBomHistoryList.remove(nowBomHistory);
+                            }
+                        });
+                    });
+                }
+
+                //澶勭悊鍏宠仈
+                addedList.forEach(add -> {
+                    nowHistoryList.forEach(nowHistory -> {
+                        if (add.getComponentId().intValue() == nowHistory.getComponentId()) {
+                            nowHistoryList.remove(nowHistory);
+                        }
+                    });
+                });
+
+                //澶勭悊瀹屾垚,杩涜琛ㄥ崟鏁版嵁鏇存柊,鍒嗕负nowHistoryList,newHistoryList,nowBomHistoryList
+                //nowHistoryList.forEach(nowHis->nowHis.setEVersion(nextVersion));
+                newHistoryList.forEach(newHis->{newHis.setEVersion(nextVersion);newHis.setSVersion(nextVersion);});
+                nowBomHistoryList.forEach(nowBomHis->nowBomHis.setEVersion(nextVersion));
+
+                cphService.updateVersionBatch(nowHistoryList);
+                cphService.insertBatch(newHistoryList);
+                historyService.updateVersionBatch(nowBomHistoryList);
+                //鏇存柊ProductBom
+                List<ProductBomHistory> newBomList = historyService.getListByParentModel(cpAList.get(0).getParentModel(), nextVersion);
+                bomService.updateNewBom(newBomList);
+            }
         }
     }
 
@@ -89,4 +353,28 @@
     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);
+    }
 }

--
Gitblit v1.9.1