6 天以前 66c6ab9883ecb5e0595f02fec6b66e962d1fbf67
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);
        }
    }
@@ -507,7 +655,7 @@
    private void handlePurchaseApprovalFinished(ApprovalInstance instance, String status) {
        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectOne(
                new LambdaQueryWrapper<PurchaseLedger>()
                        .eq(PurchaseLedger::getPurchaseContractNumber, instance.getTitle())
                        .eq(PurchaseLedger::getId, instance.getBusinessId())
                        .last("limit 1")
        );
        if (purchaseLedger == null) {
@@ -545,7 +693,7 @@
    private void handleSalesQuotationApprovalFinished(ApprovalInstance instance, String status) {
        SalesQuotation salesQuote = salesQuotationMapper.selectOne(
                new LambdaQueryWrapper<SalesQuotation>()
                        .eq(SalesQuotation::getQuotationNo, instance.getTitle())
                        .eq(SalesQuotation::getId, instance.getBusinessId())
                        .last("limit 1")
        );
        if (salesQuote == null) {
@@ -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>()