From e196f42e9a977c0b2f8452dce875b110fb76e92f Mon Sep 17 00:00:00 2001
From: whyclxw <810412026@qq.com>
Date: 星期六, 28 六月 2025 16:40:41 +0800
Subject: [PATCH] 实时数据统计曲线-核容设备初稿

---
 src/main/java/com/whyc/service/WorkflowMainService.java |  169 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 155 insertions(+), 14 deletions(-)

diff --git a/src/main/java/com/whyc/service/WorkflowMainService.java b/src/main/java/com/whyc/service/WorkflowMainService.java
index db7566a..8f5a728 100644
--- a/src/main/java/com/whyc/service/WorkflowMainService.java
+++ b/src/main/java/com/whyc/service/WorkflowMainService.java
@@ -20,9 +20,12 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.ServletContext;
+import java.io.File;
+import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -244,14 +247,34 @@
      */
 
     @Transactional
-    public Response submit(WorkflowMain main){
+    public Response submit(WorkflowMain main, List<MultipartFile> fileList, List<MultipartFile> picList) throws IOException {
         //1.鎻愪氦鍒板崟鎹鎵规祦绋�
         //濡傛灉瀛樺湪鍏宠仈鍗曟嵁id,棣栧厛鏍¢獙鎻愪氦鐨勬暟閲�
         Date now = new Date();
-        WorkflowMain mainRelated = getById(main.getRelatedId());
+        List<WorkflowDevice>  deviceListRelatedInDB = deviceService.getByMainId(main.getRelatedId());
+        List<WorkflowDevice> deviceList = main.getDeviceList();
         if(main.getRelatedId() != null) {
-            if (main.getQuantity() > mainRelated.getQuantityUnprocessed()) {
-                return new Response().setII(1, "鍏ュ簱鏁伴噺涓嶈兘澶т簬缁翠慨鐢宠鍏宠仈鍗曟嵁鐨勬湭澶勭悊鏁伴噺");
+            //閬嶅巻deviceList,濡傛灉deviceList鍐呯殑瀵硅薄鐨剄uantity鍊煎ぇ浜� deviceListRelatedInDB鍐呭璞″垪琛ㄤ腑鐨勫璞℃墍鏈夊瓧娈电浉鍚岀殑璁板綍鐨剄uantityUnprocessed鍊�,鍒欒繑鍥為敊璇�
+            for (int i = 0; i < deviceList.size(); i++) {
+                WorkflowDevice device = deviceList.get(i);
+                for (int j = 0; j < deviceListRelatedInDB.size(); j++) {
+                    WorkflowDevice deviceInDB = deviceListRelatedInDB.get(j);
+                    if (device.getName().equals(deviceInDB.getName())
+                            && device.getModel().equals(deviceInDB.getModel())
+                            && device.getVersion().equals(deviceInDB.getVersion())
+                            && device.getBrand().equals(deviceInDB.getBrand())
+                            && device.getType().equals(deviceInDB.getType())
+                            && device.getSupplier().equals(deviceInDB.getSupplier())
+                    ) {
+                        if (device.getQuantity() > deviceInDB.getQuantityUnprocessed()) {
+                            return new Response().setII(1, "鍏ュ簱鏁伴噺涓嶈兘澶т簬缁翠慨鐢宠鍏宠仈鍗曟嵁鐨勬湭澶勭悊鏁伴噺");
+                        }else{
+                            //褰撳墠璁惧鐨勬牎楠岀粨鏉�,杩涘叆涓嬩竴涓澶囨牎楠�
+                            break;
+                        }
+                    }
+
+                }
             }
         }
         //涓昏〃鎻掑叆
@@ -269,16 +292,76 @@
         main.setOrderId(orderId);
         main.setTitle(title);
         main.setCreateUserId(user.getId());
+        main.setCreateUserName(user.getName());
         main.setCreateTime(now);
         main.setBeginTime(now);
         main.setStatus(mainStatus);
         main.setProcessStage(process.getStage());
         if(main.getType() == WorkflowTypeEnum.DEVICE_REPAIR.getType().intValue()){ //缁翠慨鐢宠鍗�,闇�濉叆
-            main.setQuantityUnprocessed(main.getQuantity());
+            for (int i = 0; i < deviceList.size(); i++) {
+                deviceList.get(i).setQuantityUnprocessed(deviceList.get(i).getQuantity());
+            }
         }
+        //濡傛灉瀛樺湪闄勪欢鍜屽浘鐗�,鍒欏瓨鍏ュ搴斿瓧娈�
+        //瀵瑰瓨鍌ㄨ矾寰勮繘琛屽畾涔�
+        String timeFormat = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM_DD_HH_MM_SS_UNION, now);
+        String dirMonth = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM, now);
+        String fileDirPath = CommonUtil.getRootFile() + "workflow" + File.separator + dirMonth;
+        File fileDir = new File(fileDirPath);
+        //濡傛灉鏂囦欢澶逛笉瀛樺湪鍒欏垱寤�
+        if (!fileDir.exists()) {
+            fileDir.mkdirs();
+        }
+        StringBuilder fileUrlSb = new StringBuilder();
+        StringBuilder fileNameSb = new StringBuilder();
+        if (fileList != null && fileList.size() > 0) {
+            for (int i = 0; i < fileList.size(); i++) {
+                MultipartFile multipartFile = fileList.get(i);
+                String fileName = multipartFile.getOriginalFilename();
+                //灏唂ileName涓彲鑳藉瓨鍦ㄧ殑,鍘绘帀
+                fileName = fileName.replace(",","");
+                String filePath = fileDirPath + File.separator + timeFormat+"_"+fileName;
+
+                multipartFile.transferTo(new File(filePath));
+                String split = "pis_file"+File.separator+"workflow";
+                String fileUrl = File.separator + filePath.substring(filePath.indexOf(split));
+                if(i == fileList.size()-1) {
+                    fileUrlSb.append(fileUrl);
+                    fileNameSb.append(fileName);
+                }else {
+                    fileUrlSb.append(fileUrl).append(",");
+                    fileNameSb.append(fileName).append(",");
+                }
+            }
+        }
+        main.setFileUrl(fileUrlSb.toString());
+        main.setFileName(fileNameSb.toString());
+
+        StringBuilder picUrlSb = new StringBuilder();
+        StringBuilder picNameSb = new StringBuilder();
+        if (picList != null && picList.size() > 0) {
+            for (int i = 0; i < picList.size(); i++) {
+                MultipartFile multipartFile = picList.get(i);
+                String picName = multipartFile.getOriginalFilename();
+                //灏唒icName涓彲鑳藉瓨鍦ㄧ殑,鍘绘帀
+                picName = picName.replace(",","");
+                String picPath = fileDirPath + File.separator + picName;
+                multipartFile.transferTo(new File(picPath));
+                String split = "pis_file"+File.separator+"workflow";
+                String picUrl = File.separator + picPath.substring(picPath.indexOf(split));
+                if(i == picList.size()-1) {
+                    picUrlSb.append(picUrl);
+                    picNameSb.append(picName);
+                }else {
+                    picUrlSb.append(picUrl).append(",");
+                    picNameSb.append(picName).append(",");
+                }
+            }
+        }
+        main.setPicUrl(picUrlSb.toString());
+        main.setPicName(picNameSb.toString());
         add(main);
         //涓昏〃鍏宠仈鐨勭墿鏂欐彃鍏�
-        List<WorkflowDevice> deviceList = main.getDeviceList();
         for (WorkflowDevice device : deviceList) {
             device.setMainId(main.getId());
         }
@@ -309,14 +392,25 @@
         linkService.add(link);
 
         //濡傛灉瀛樺湪鍏宠仈鍗曟嵁id,澶勭悊鍏宠仈鍗曟嵁鍙婅嚜鍔ㄧ敓鎴愭柊鍗曟嵁
-        checkRelatedAndDone(main, now, mainRelated, user);
+        checkRelatedAndDone(main, now, deviceListRelatedInDB, user);
         return new Response().setII(1,"鎻愪氦瀹屾垚");
     }
 
-    private void checkRelatedAndDone(WorkflowMain main, Date now, WorkflowMain mainRelated, User user) {
+    private void checkRelatedAndDone(WorkflowMain main, Date now, List<WorkflowDevice> deviceListRelatedInDB, User user) {
         if(main.getRelatedId() != null){
-            //鍒ゆ柇鏄敵璇风殑鍏ュ簱,杩樻槸鎶ュ簾.
-            if (main.getQuantity() < mainRelated.getQuantityUnprocessed()){ //璇存槑闇�瑕佽嚜鍔ㄧ敓鎴愬叆搴撴垨鑰呮姤搴熷崟鎹�
+            //鐢宠鐨勬暟閲�<鍏宠仈鍗曟嵁鐨勬湭澶勭悊鏁伴噺,鍒欒鏄庢湁鍓╀綑. 闇�瑕佽嚜鍔ㄧ敓鎴愬绔嬪伐鍗�
+            List<WorkflowDevice> deviceList = main.getDeviceList();
+            //璁$畻deviceListn鍐卶uantity灞炴�у�肩殑鍜�
+            int sumQuantity = 0;
+            for (WorkflowDevice device : deviceList) {
+                sumQuantity += device.getQuantity();
+            }
+            //璁$畻deviceListRelatedInDBn鍐卶uantityUnprocessed灞炴�у�肩殑鍜�
+            int sumQuantityUnprocessedInDB = 0;
+            for (WorkflowDevice device : deviceListRelatedInDB) {
+                sumQuantityUnprocessedInDB += device.getQuantityUnprocessed();
+            }
+            if (sumQuantity < sumQuantityUnprocessedInDB){ //璇存槑闇�瑕佽嚜鍔ㄧ敓鎴愬叆搴撴垨鑰呮姤搴熷崟鎹�
                 //闇�瑕佽嚜鍔ㄧ敓鎴愭姤搴熺殑鐢宠
                 WorkflowMain mainAuto = new WorkflowMain();
                 WorkflowTypeEnum typeEnumAuto;
@@ -326,10 +420,52 @@
                 mainAuto.setCreateUserId(user.getId());
                 mainAuto.setCreateTime(now);
                 mainAuto.setBeginTime(now);
-                mainAuto.setQuantity(mainRelated.getQuantityUnprocessed()- main.getQuantity());
                 mainAuto.setProcessStage(processAuto.getStage());
                 mainAuto.setStatus(statusAuto);
                 mainAuto.setRelatedId(main.getRelatedId());
+                //鏁寸悊涓昏〃鐨勮澶囬檮琛�
+                List<WorkflowDevice> deviceListAuto = new ArrayList<>();
+                //閬嶅巻deviceListRelatedInDB,鍑忓幓deviceList涓敵璇风殑鏁伴噺
+                for (WorkflowDevice deviceInDB : deviceListRelatedInDB) {
+                    boolean deviceExists = false;
+                    for (WorkflowDevice device : deviceList) {
+                        if (device.getName().equals(deviceInDB.getName())
+                                && device.getModel().equals(deviceInDB.getModel())
+                                && device.getVersion().equals(deviceInDB.getVersion())
+                                && device.getBrand().equals(deviceInDB.getBrand())
+                                && device.getType().equals(deviceInDB.getType())
+                                && device.getSupplier().equals(deviceInDB.getSupplier())
+                        ){
+                            deviceExists = true;
+                            if (deviceInDB.getQuantityUnprocessed()-device.getQuantity() > 0){
+                                WorkflowDevice deviceAuto = new WorkflowDevice();
+                                deviceAuto.setName(deviceInDB.getName());
+                                deviceAuto.setModel(deviceInDB.getModel());
+                                deviceAuto.setVersion(deviceInDB.getVersion());
+                                deviceAuto.setQuantity(deviceInDB.getQuantityUnprocessed()-device.getQuantity());
+                                deviceAuto.setBrand(deviceInDB.getBrand());
+                                deviceAuto.setType(deviceInDB.getType());
+                                deviceAuto.setSupplier(deviceInDB.getSupplier());
+
+                                deviceListAuto.add(deviceAuto);
+                            }else{
+                                break;
+                            }
+                        }
+                    }
+                    if (!deviceExists){
+                        WorkflowDevice deviceAuto = new WorkflowDevice();
+                        deviceAuto.setName(deviceInDB.getName());
+                        deviceAuto.setModel(deviceInDB.getModel());
+                        deviceAuto.setVersion(deviceInDB.getVersion());
+                        deviceAuto.setQuantity(deviceInDB.getQuantityUnprocessed());
+                        deviceAuto.setBrand(deviceInDB.getBrand());
+                        deviceAuto.setType(deviceInDB.getType());
+                        deviceAuto.setSupplier(deviceInDB.getSupplier());
+
+                        deviceListAuto.add(deviceAuto);
+                    }
+                }
 
                 if(main.getType() == WorkflowTypeEnum.DEVICE_IN.getType().intValue()) { //鐢宠鐨勫叆搴�
                     mainAuto.setTaskDesc("鎻愪氦浜嗙淮淇悗鐨勫叆搴撶敵璇�,绯荤粺鑷姩鐢熸垚鍓╀綑鏁伴噺鐨勬姤搴熺敵璇�");
@@ -343,6 +479,11 @@
                 mainAuto.setTitle(titleAuto);
                 mainAuto.setType(typeEnumAuto.getType());
                 add(mainAuto);
+                deviceListAuto.forEach(device -> {
+                    device.setMainId(mainAuto.getId());
+                });
+                //鎻掑叆涓昏〃鐨勮澶囬檮琛�
+                deviceService.addBatch(deviceListAuto);
 
                 //鑷姩鑺傜偣鐢熸垚
                 WorkflowLink linkAuto = new WorkflowLink();
@@ -368,8 +509,9 @@
             }else{
                 relatedMain.setEndReason("鐢ㄦ埛鎻愪氦缁翠慨鐢宠鍚庣殑鎶ュ簾鐢宠,缁翠慨娴佺▼缁撴潫");
             }
-            relatedMain.setQuantityUnprocessed(0);
             updateById(relatedMain);
+            //鏇存柊鍏宠仈鍗曟嵁鐨勮澶囧墿浣欐湭澶勭悊鏁伴噺涓�0
+            deviceService.setQuantityUnprocessedZero(main.getRelatedId());
         }
     }
 
@@ -420,12 +562,11 @@
         mapper.update((WorkflowMain) ActionUtil.objeNull,update);
     }
 
-    public void resetRepairStatus(Integer relatedId, Integer quantity) {
+    public void resetRepairStatus(Integer relatedId) {
         UpdateWrapper<WorkflowMain> update = Wrappers.update();
         update.set("status",WorkflowEnum.MAIN_STATUS_WAIT_FOR_DEALING.getValue())
                 .set("end_time",null)
                 .set("end_reason",null)
-                .set("quantity_unprocessed",quantity)
                 .eq("id",relatedId);
         mapper.update((WorkflowMain) ActionUtil.objeNull,update);
     }

--
Gitblit v1.9.1