From 66c6ab9883ecb5e0595f02fec6b66e962d1fbf67 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 03 六月 2026 10:12:23 +0800
Subject: [PATCH] Merge branch 'dev_New_pro' into dev_宁夏_英泽防锈

---
 src/main/java/com/ruoyi/approve/service/impl/ApprovalInstanceServiceImpl.java |  268 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 257 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/ruoyi/approve/service/impl/ApprovalInstanceServiceImpl.java b/src/main/java/com/ruoyi/approve/service/impl/ApprovalInstanceServiceImpl.java
index 6bb732e..931159e 100644
--- a/src/main/java/com/ruoyi/approve/service/impl/ApprovalInstanceServiceImpl.java
+++ b/src/main/java/com/ruoyi/approve/service/impl/ApprovalInstanceServiceImpl.java
@@ -12,6 +12,8 @@
 import com.ruoyi.approve.mapper.ApprovalInstanceMapper;
 import com.ruoyi.approve.mapper.ApprovalTemplateNodeApproverMapper;
 import com.ruoyi.approve.mapper.FinReimbursementMapper;
+import com.ruoyi.approve.mapper.VehicleBorrowRecordMapper;
+import com.ruoyi.approve.mapper.VehicleMapper;
 import com.ruoyi.approve.pojo.*;
 import com.ruoyi.approve.service.*;
 import com.ruoyi.approve.utils.ApproveProcessConfigNodeUtils;
@@ -86,6 +88,8 @@
     private final SalesQuotationMapper salesQuotationMapper;
     private final ShippingInfoMapper shippingInfoMapper;
     private final QualityInspectHelper qualityInspectHelper;
+    private final VehicleBorrowRecordMapper vehicleBorrowRecordMapper;
+    private final VehicleMapper vehicleMapper;
     private final EnterpriseNewsScopeUserMapper enterpriseNewsScopeUserMapper;
     private final SysUserMapper sysUserMapper;
     private final SysUserDeptMapper sysUserDeptMapper;
@@ -105,6 +109,30 @@
         }
         records.forEach(vo -> {
             vo.setBusinessName(TypeEnums.getLabelByValue(vo.getBusinessType()));
+
+            // 鏍规嵁涓氬姟绫诲瀷鏌ヨ瀵瑰簲鐨勫崟鍙�
+            if (vo.getBusinessType() != null && vo.getBusinessId() != null) {
+                if (TypeEnums.PURCHASE_APPROVAL.getCode().equals(vo.getBusinessType())) {
+                    // 閲囪喘瀹℃壒 - 鏌ヨ閲囪喘鍗曞彿
+                    PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(vo.getBusinessId());
+                    System.out.println("涓氬姟绫诲瀷锛�" + purchaseLedger.getPurchaseContractNumber());
+                    if (purchaseLedger != null) {
+                        vo.setPurchaseContractNumber(purchaseLedger.getPurchaseContractNumber());
+                    }
+                } else if (TypeEnums.QUOTATION_APPROVAL.getCode().equals(vo.getBusinessType())) {
+                    // 鎶ヤ环瀹℃壒 - 鏌ヨ鎶ヤ环鍗曞彿
+                    SalesQuotation salesQuotation = salesQuotationMapper.selectById(vo.getBusinessId());
+                    if (salesQuotation != null) {
+                        vo.setQuotationNo(salesQuotation.getQuotationNo());
+                    }
+                } else if (TypeEnums.SHIPPING_APPROVAL.getCode().equals(vo.getBusinessType())) {
+                    // 鍙戣揣瀹℃壒 - 鏌ヨ鍙戣揣鍗曞彿
+                    ShippingInfo shippingInfo = shippingInfoMapper.selectById(vo.getBusinessId());
+                    if (shippingInfo != null) {
+                        vo.setShippingNo(shippingInfo.getShippingNo());
+                    }
+                }
+            }
         });
         Long currentUserId = SecurityUtils.getUserId();
 
@@ -140,7 +168,7 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Boolean add(ApprovalInstanceDto approvalInstanceDto) {
-        String instanceNo = OrderUtils.countTodayByCreateTime(approvalInstanceMapper, "SP", "instance_no");
+        String instanceNo = OrderUtils.countTodayByCreateTime(approvalInstanceMapper, "SP", "instance_no", approvalInstanceDto.getCreateTime() != null ? approvalInstanceDto.getCreateTime() : LocalDateTime.now());
         approvalInstanceDto.setInstanceNo(instanceNo);
         approvalInstanceDto.setStatus("PENDING");
         approvalInstanceDto.setCurrentLevel(1);
@@ -270,6 +298,87 @@
         return approveAndMoveNext(instance, currentNode, approvalInstanceDto, now);
     }
 
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public R autoApprove(Long instanceId) {
+        if (instanceId == null) {
+            return R.fail("瀹℃壒瀹炰緥 ID 涓嶈兘涓虹┖");
+        }
+
+        ApprovalInstance instance = getPendingApprovalInstance(instanceId);
+        if (instance == null) {
+            return R.fail("瀹℃壒瀹炰緥涓嶅瓨鍦�");
+        }
+        if ("REJECTED".equals(instance.getStatus())) {
+            return R.fail("瀹℃壒宸查┏鍥烇紝鏃犳硶鑷姩閫氳繃");
+        }
+        if ("APPROVED".equals(instance.getStatus())) {
+            return R.ok("瀹℃壒宸插畬鎴�");
+        }
+
+        ApprovalInstanceDto autoApproveDto = new ApprovalInstanceDto();
+        autoApproveDto.setId(instanceId);
+        autoApproveDto.setApproveComment("绯荤粺鑷姩瀹℃壒");
+
+        int loopCount = 0;
+        while (loopCount++ < 20) {
+            ApprovalInstance currentInstance = getPendingApprovalInstance(instanceId);
+            if (currentInstance == null) {
+                return R.fail("瀹℃壒瀹炰緥涓嶅瓨鍦�");
+            }
+            if ("APPROVED".equals(currentInstance.getStatus())) {
+                return R.ok("瀹℃壒宸插畬鎴�");
+            }
+            if ("REJECTED".equals(currentInstance.getStatus())) {
+                return R.fail("瀹℃壒宸查┏鍥烇紝鏃犳硶鑷姩閫氳繃");
+            }
+
+            ApprovalInstanceNode currentNode = approveProcessConfigNodeUtils.getCurrentNode(currentInstance.getId());
+            if (currentNode == null) {
+                currentInstance.setStatus("APPROVED");
+                currentInstance.setFinishTime(LocalDateTime.now());
+                this.updateById(currentInstance);
+                handleBusinessAfterApprovalFinished(currentInstance);
+                return R.ok("瀹℃壒宸插畬鎴�");
+            }
+
+            List<ApprovalTask> pendingTasks = approvalTaskService.list(
+                    Wrappers.<ApprovalTask>lambdaQuery()
+                            .eq(ApprovalTask::getInstanceId, currentInstance.getId())
+                            .eq(ApprovalTask::getNodeId, currentNode.getId())
+                            .eq(ApprovalTask::getTaskStatus, "PENDING")
+                            .eq(ApprovalTask::getDeleted, 0)
+            );
+
+            LocalDateTime now = LocalDateTime.now();
+            for (ApprovalTask currentTask : pendingTasks) {
+                if (!updateCurrentTask(autoApproveDto, "APPROVED", currentTask, now)) {
+                    return R.fail("褰撳墠浠诲姟宸茶澶勭悊锛岃鍒锋柊鍚庨噸璇�");
+                }
+                saveApprovalRecord(
+                        currentInstance.getId(),
+                        currentNode.getId(),
+                        currentTask.getId(),
+                        0L,
+                        "绯荤粺鑷姩瀹℃壒",
+                        "APPROVED",
+                        autoApproveDto.getApproveComment()
+                );
+            }
+
+            if (!approveProcessConfigNodeUtils.canProceedToNextLevel(currentInstance.getId(), currentNode.getApproveType())) {
+                return R.ok("瀹℃壒鎴愬姛锛岀瓑寰呭叾浠栧鎵逛汉澶勭悊");
+            }
+
+            R moveResult = moveToNextLevel(currentInstance, currentNode, autoApproveDto, now, false);
+            if (!R.isSuccess(moveResult)) {
+                return moveResult;
+            }
+        }
+
+        return R.fail("鑷姩瀹℃壒寰幆娆℃暟瓒呴檺");
+    }
+
     private String normalizeApproveAction(String approveAction) {
         if (!StringUtils.hasText(approveAction)) {
             return null;
@@ -315,6 +424,8 @@
         instance.setStatus("REJECTED");
         instance.setFinishTime(now);
         this.updateById(instance);
+        // 缁熶竴澶勭悊涓氬姟鐘舵�佹洿鏂�
+        handleBusinessAfterApprovalFinished(instance);
         // 椹冲洖瀵瑰簲鐨勪紒涓氭柊闂伙紝 宸梾鎶ラ攢
         if (instance.getBusinessType().equals(TypeEnums.ENTERPRISE_NEWS_APPROVAL.getCode())) {
             enterpriseNewsMapper.update(
@@ -328,6 +439,26 @@
                             .eq(FinReimbursement::getId, instance.getBusinessId())
                             .set(FinReimbursement::getBillStatus, "REJECTED")
             );
+        } else if (TypeEnums.VEHICLE_BORROW_APPROVAL.getCode().equals(instance.getBusinessType())) {
+            vehicleBorrowRecordMapper.update(
+                    null,
+                    new LambdaUpdateWrapper<VehicleBorrowRecord>()
+                            .eq(VehicleBorrowRecord::getId, instance.getBusinessId())
+                            .set(VehicleBorrowRecord::getBorrowStatus, "REJECTED")
+                            .set(VehicleBorrowRecord::getApprovalInstanceId, instance.getId())
+            );
+            VehicleBorrowRecord borrowRecord = vehicleBorrowRecordMapper.selectById(instance.getBusinessId());
+            if (borrowRecord != null) {
+                syncVehicleBorrowStatus(borrowRecord.getVehicleId());
+            }
+        } else if (TypeEnums.VEHICLE_DELAY_APPROVAL.getCode().equals(instance.getBusinessType())) {
+            vehicleBorrowRecordMapper.update(
+                    null,
+                    new LambdaUpdateWrapper<VehicleBorrowRecord>()
+                            .eq(VehicleBorrowRecord::getId, instance.getBusinessId())
+                            .set(VehicleBorrowRecord::getExtendStatus, "REJECTED")
+                            .set(VehicleBorrowRecord::getExtendApprovalInstanceId, null)
+            );
         }
         return R.ok("瀹℃壒宸查┏鍥�");
     }
@@ -336,6 +467,14 @@
                                  ApprovalInstanceNode currentNode,
                                  ApprovalInstanceDto approvalInstanceDto,
                                  LocalDateTime now) {
+        return moveToNextLevel(instance, currentNode, approvalInstanceDto, now, true);
+    }
+
+    private R moveToNextLevel(ApprovalInstance instance,
+                              ApprovalInstanceNode currentNode,
+                              ApprovalInstanceDto approvalInstanceDto,
+                              LocalDateTime now,
+                              boolean notifyNextNode) {
         if (!updateCurrentNodeStatus(currentNode.getId(), "APPROVED", now)) {
             return R.ok("褰撳墠鑺傜偣宸插鐞嗗畬鎴�");
         }
@@ -359,14 +498,16 @@
             instance.setCurrentLevel(nextLevel);
             instance.setStatus("PENDING");
             this.updateById(instance);
-            List<ApprovalTask> nextTasks = approvalTaskService.list(
-                    Wrappers.<ApprovalTask>lambdaQuery()
-                            .eq(ApprovalTask::getInstanceId, instance.getId())
-                            .eq(ApprovalTask::getNodeId, nextInstanceNode.getId())
-                            .eq(ApprovalTask::getTaskStatus, "PENDING")
-                            .eq(ApprovalTask::getDeleted, 0)
-            );
-            sendApproveNotice(instance, nextTasks);
+            if (notifyNextNode) {
+                List<ApprovalTask> nextTasks = approvalTaskService.list(
+                        Wrappers.<ApprovalTask>lambdaQuery()
+                                .eq(ApprovalTask::getInstanceId, instance.getId())
+                                .eq(ApprovalTask::getNodeId, nextInstanceNode.getId())
+                                .eq(ApprovalTask::getTaskStatus, "PENDING")
+                                .eq(ApprovalTask::getDeleted, 0)
+                );
+                sendApproveNotice(instance, nextTasks);
+            }
             return R.ok("瀹℃壒鎴愬姛锛屽凡娴佽浆鍒颁笅涓�鑺傜偣");
         }
 
@@ -390,7 +531,9 @@
         instance.setStatus("PENDING");
         this.updateById(instance);
         approveProcessConfigNodeUtils.createCurrentNodeAndTasks(instance, false);
-        sendApproveNotice(instance, approveProcessConfigNodeUtils.getCurrentPendingTasks(approvalInstanceDto.getId()));
+        if (notifyNextNode) {
+            sendApproveNotice(instance, approveProcessConfigNodeUtils.getCurrentPendingTasks(approvalInstanceDto.getId()));
+        }
         return R.ok("瀹℃壒鎴愬姛锛屽凡娴佽浆鍒颁笅涓�鑺傜偣");
     }
 
@@ -445,6 +588,11 @@
         }
         if (TypeEnums.ENTERPRISE_NEWS_APPROVAL.getCode().equals(businessType)) {
             handleNewsApprovalFinished(instance, status);
+            return;
+        }
+        if (TypeEnums.VEHICLE_BORROW_APPROVAL.getCode().equals(businessType)
+                || TypeEnums.VEHICLE_DELAY_APPROVAL.getCode().equals(businessType)) {
+            handleVehicleBorrowApprovalFinished(instance, status);
         }
     }
 
@@ -565,7 +713,7 @@
     private void handleShippingApprovalFinished(ApprovalInstance instance, String status) {
         ShippingInfo shippingInfo = shippingInfoMapper.selectOne(
                 new LambdaQueryWrapper<ShippingInfo>()
-                        .eq(ShippingInfo::getId, instance.getTitle())
+                        .eq(ShippingInfo::getShippingNo, instance.getTitle())
                         .orderByDesc(ShippingInfo::getCreateTime)
                         .last("limit 1")
         );
@@ -586,6 +734,104 @@
         shippingInfoMapper.updateById(shippingInfo);
     }
 
+    private void handleVehicleBorrowApprovalFinished(ApprovalInstance instance, String status) {
+        if (instance == null || instance.getBusinessId() == null) {
+            return;
+        }
+
+        VehicleBorrowRecord record = vehicleBorrowRecordMapper.selectById(instance.getBusinessId());
+        if (record == null || Integer.valueOf(1).equals(record.getDeleted())) {
+            return;
+        }
+
+        if (TypeEnums.VEHICLE_BORROW_APPROVAL.getCode().equals(instance.getBusinessType())) {
+            if ("APPROVED".equals(status)) {
+                Vehicle vehicle = vehicleMapper.selectById(record.getVehicleId());
+                if (vehicle == null) {
+                    throw new ServiceException("杞﹁締涓嶅瓨鍦�");
+                }
+                VehicleBorrowRecord update = new VehicleBorrowRecord();
+                update.setId(record.getId());
+                update.setBorrowStatus("BORROWING");
+                update.setApprovedTime(instance.getFinishTime());
+                update.setApprovalInstanceId(instance.getId());
+                vehicleBorrowRecordMapper.updateById(update);
+                syncVehicleBorrowStatus(vehicle.getId());
+                return;
+            }
+            if ("REJECTED".equals(status)) {
+                VehicleBorrowRecord update = new VehicleBorrowRecord();
+                update.setId(record.getId());
+                update.setBorrowStatus("REJECTED");
+                update.setApprovalInstanceId(instance.getId());
+                vehicleBorrowRecordMapper.updateById(update);
+                syncVehicleBorrowStatus(record.getVehicleId());
+                return;
+            }
+            if ("PENDING".equals(status)) {
+                VehicleBorrowRecord update = new VehicleBorrowRecord();
+                update.setId(record.getId());
+                update.setBorrowStatus("IN_APPROVAL");
+                update.setApprovalInstanceId(instance.getId());
+                vehicleBorrowRecordMapper.updateById(update);
+            }
+            return;
+        }
+
+        if (TypeEnums.VEHICLE_DELAY_APPROVAL.getCode().equals(instance.getBusinessType())) {
+            if ("APPROVED".equals(status)) {
+                vehicleBorrowRecordMapper.update(
+                        null,
+                        new LambdaUpdateWrapper<VehicleBorrowRecord>()
+                                .eq(VehicleBorrowRecord::getId, record.getId())
+                                .set(VehicleBorrowRecord::getBorrowStatus, "BORROWING")
+                                .set(VehicleBorrowRecord::getExtendStatus, "APPROVED")
+                                .set(VehicleBorrowRecord::getExtendApprovedTime, instance.getFinishTime())
+                                .set(VehicleBorrowRecord::getPlannedReturnTime, record.getExtendTargetReturnTime())
+                                .set(VehicleBorrowRecord::getExtendApprovalInstanceId, null)
+                );
+                return;
+            }
+            if ("REJECTED".equals(status)) {
+                vehicleBorrowRecordMapper.update(
+                        null,
+                        new LambdaUpdateWrapper<VehicleBorrowRecord>()
+                                .eq(VehicleBorrowRecord::getId, record.getId())
+                                .set(VehicleBorrowRecord::getExtendStatus, "REJECTED")
+                                .set(VehicleBorrowRecord::getExtendApprovalInstanceId, null)
+                );
+                return;
+            }
+            if ("PENDING".equals(status)) {
+                VehicleBorrowRecord update = new VehicleBorrowRecord();
+                update.setId(record.getId());
+                update.setExtendStatus("PENDING");
+                update.setExtendApprovalInstanceId(instance.getId());
+                vehicleBorrowRecordMapper.updateById(update);
+            }
+        }
+    }
+
+    private void syncVehicleBorrowStatus(Long vehicleId) {
+        if (vehicleId == null) {
+            return;
+        }
+        long activeBorrowCount = vehicleBorrowRecordMapper.selectCount(
+                new LambdaQueryWrapper<VehicleBorrowRecord>()
+                        .eq(VehicleBorrowRecord::getVehicleId, vehicleId)
+                        .eq(VehicleBorrowRecord::getDeleted, 0)
+                        .in(VehicleBorrowRecord::getBorrowStatus, "IN_APPROVAL", "BORROWING")
+        );
+        Vehicle vehicle = vehicleMapper.selectById(vehicleId);
+        if (vehicle == null) {
+            throw new ServiceException("杞﹁締涓嶅瓨鍦�");
+        }
+        Vehicle vehicleUpdate = new Vehicle();
+        vehicleUpdate.setId(vehicleId);
+        vehicleUpdate.setStatus(activeBorrowCount > 0 ? "IN_USE" : "IDLE");
+        vehicleMapper.updateById(vehicleUpdate);
+    }
+
     private List<ApprovalTask> createNodeAndTasks(ApprovalInstance instance, ApprovalTemplateNode templateNode) {
         List<ApprovalTemplateNodeApprover> approvers = approvalTemplateNodeApproverMapper.selectList(
                 new LambdaQueryWrapper<ApprovalTemplateNodeApprover>()

--
Gitblit v1.9.3