| | |
| | | private final StockInRecordService stockInRecordService; |
| | | private final StockOutRecordService stockOutRecordService; |
| | | private final StockUtils stockUtils; |
| | | private final ShipmentApprovalMapper shipmentApprovalMapper; |
| | | |
| | | @Autowired |
| | | private IApproveProcessService approveProcessService; |
| | | |
| | |
| | | public List getTopFiveList() { |
| | | // 查询原始数据 |
| | | LambdaQueryWrapper<SalesLedger> queryWrapper = Wrappers.lambdaQuery(); |
| | | queryWrapper.select(SalesLedger::getCustomerId, SalesLedger::getCustomerName, SalesLedger::getContractAmount).orderByDesc(SalesLedger::getContractAmount); |
| | | queryWrapper.select(SalesLedger::getCustomerId, SalesLedger::getCustomerName, SalesLedger::getContractAmount) |
| | | .orderByDesc(SalesLedger::getContractAmount) |
| | | .ne(SalesLedger::getReviewStatus, 2); // 排除反审核数据 |
| | | List<SalesLedger> records = salesLedgerMapper.selectList(queryWrapper); |
| | | |
| | | // 按客户ID分组并聚合金额 |
| | |
| | | if (CollectionUtils.isEmpty(idList)) { |
| | | return 0; |
| | | } |
| | | // 校验:已审核的订单不能删除 |
| | | List<SalesLedger> ledgers = salesLedgerMapper.selectBatchIds(idList); |
| | | for (SalesLedger ledger : ledgers) { |
| | | if (ledger.getReviewStatus() != null && ledger.getReviewStatus() == 1) { |
| | | throw new ServiceException("已审核的订单不能删除:" + ledger.getSalesContractNo()); |
| | | } |
| | | } |
| | | // 删除销售管理数据 |
| | | LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.in(SalesLedgerProduct::getSalesLedgerId, idList).select(SalesLedgerProduct::getId); |
| | |
| | | if (salesLedger.getDeliveryStatus() == 5) { |
| | | throw new ServiceException("订单已发货,禁止编辑"); |
| | | } |
| | | // 查询数据库中的原始记录用于校验 |
| | | SalesLedger existingLedger = salesLedgerMapper.selectById(salesLedger.getId()); |
| | | if (salesLedger.getReviewStatus() != null && salesLedger.getReviewStatus() == 1) { |
| | | // 审核操作:校验审核人不能是录入人(仅对未审核→已审核的转换) |
| | | if (existingLedger != null && existingLedger.getReviewStatus() != null && existingLedger.getReviewStatus() == 0) { |
| | | Long currentUserId = SecurityUtils.getUserId(); |
| | | String entryPerson = existingLedger.getEntryPerson(); |
| | | if (entryPerson != null && entryPerson.equals(String.valueOf(currentUserId))) { |
| | | throw new ServiceException("不能审核本人录入的订单"); |
| | | } |
| | | } |
| | | salesLedger.setReviewStatus(salesLedgerDto.getReviewStatus()); |
| | | } else if (salesLedger.getReviewStatus() != null && salesLedger.getReviewStatus() == 2) { |
| | | handleCounterReview(salesLedger); |
| | | } else { |
| | | // 未审核状态的编辑操作:校验已审核的订单不能编辑 |
| | | if (existingLedger != null && existingLedger.getReviewStatus() != null && existingLedger.getReviewStatus() == 1) { |
| | | throw new ServiceException("已审核的订单不能编辑"); |
| | | } |
| | | salesLedger.setReviewStatus(0); |
| | | } |
| | | salesLedgerMapper.updateById(salesLedger); |
| | |
| | | |
| | | // 6. 处理原订单的库存数据(生成反审出入库记录) |
| | | processOriginalOrderStock(originalSalesLedgerId); |
| | | |
| | | // 7. 清除原订单的质检记录 |
| | | clearQualityInspectRecords(originalSalesLedgerId); |
| | | |
| | | // 8. 清除原订单的发货信息和发货审批记录 |
| | | clearShippingAndApprovalRecords(originalSalesLedgerId); |
| | | |
| | | // 9. 取消原订单相关的审批流程 |
| | | cancelApproveProcesses(originalSalesLedgerId, originalLedger.getSalesContractNo()); |
| | | } |
| | | |
| | | /** |
| | | * 清除原订单的质检记录 |
| | | */ |
| | | private void clearQualityInspectRecords(Long originalSalesLedgerId) { |
| | | // 删除与原订单关联的质检记录 |
| | | qualityInspectMapper.delete( |
| | | Wrappers.<QualityInspect>lambdaQuery() |
| | | .eq(QualityInspect::getPurchaseLedgerId, originalSalesLedgerId) |
| | | ); |
| | | } |
| | | |
| | | /** |
| | | * 清除原订单的发货信息和发货审批记录 |
| | | */ |
| | | private void clearShippingAndApprovalRecords(Long originalSalesLedgerId) { |
| | | // 1. 查询原订单的所有发货信息 |
| | | List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList( |
| | | Wrappers.<ShippingInfo>lambdaQuery() |
| | | .eq(ShippingInfo::getSalesLedgerId, originalSalesLedgerId) |
| | | ); |
| | | |
| | | // 2. 删除发货审批记录 |
| | | if (!CollectionUtils.isEmpty(shippingInfos)) { |
| | | List<Long> shippingInfoIds = shippingInfos.stream() |
| | | .map(ShippingInfo::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | shipmentApprovalMapper.delete( |
| | | Wrappers.<ShipmentApproval>lambdaQuery() |
| | | .eq(ShipmentApproval::getSalesLedgerId, originalSalesLedgerId) |
| | | .or() |
| | | .in(ShipmentApproval::getShippingInfoId, shippingInfoIds) |
| | | ); |
| | | |
| | | // 3. 删除发货信息记录 |
| | | shippingInfoMapper.delete( |
| | | Wrappers.<ShippingInfo>lambdaQuery() |
| | | .eq(ShippingInfo::getSalesLedgerId, originalSalesLedgerId) |
| | | ); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 取消原订单相关的审批流程 |
| | | */ |
| | | private void cancelApproveProcesses(Long originalSalesLedgerId, String originalSalesContractNo) { |
| | | // 取消入库审批流程 |
| | | List<ApproveProcess> stockInApproveProcesses = approveProcessService.list( |
| | | new LambdaQueryWrapper<ApproveProcess>() |
| | | .eq(ApproveProcess::getApproveType, ApproveTypeEnum.STOCK_IN.getCode()) |
| | | .like(ApproveProcess::getApproveRemark, "salesStock:" + originalSalesLedgerId + ":") |
| | | .eq(ApproveProcess::getApproveDelete, 0) |
| | | ); |
| | | |
| | | for (ApproveProcess process : stockInApproveProcesses) { |
| | | process.setApproveStatus(3); // 设置为审批失败状态 |
| | | process.setApproveDelete(1); // 标记为已删除 |
| | | approveProcessService.updateById(process); |
| | | } |
| | | |
| | | // 取消发货审批流程 |
| | | List<ApproveProcess> deliveryApproveProcesses = approveProcessService.list( |
| | | new LambdaQueryWrapper<ApproveProcess>() |
| | | .eq(ApproveProcess::getApproveType, 7) // 发货审批类型 |
| | | .like(ApproveProcess::getApproveReason, "发货审批:" + originalSalesContractNo) |
| | | .eq(ApproveProcess::getApproveDelete, 0) |
| | | ); |
| | | |
| | | for (ApproveProcess process : deliveryApproveProcesses) { |
| | | process.setApproveStatus(3); // 设置为审批失败状态 |
| | | process.setApproveDelete(1); // 标记为已删除 |
| | | approveProcessService.updateById(process); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理原订单的库存数据 |
| | | * 1. 对原订单的入库数据生成反审出库记录(销售-反审出库) |
| | | * 2. 对原订单的出库数据生成反审入库记录(销售-反审入库) |
| | | * 3. 更新库存表数据 |
| | | * 1. 删除原订单的所有入库记录,并扣减库存 |
| | | * 2. 删除原订单的所有出库记录,并增加库存 |
| | | */ |
| | | private void processOriginalOrderStock(Long originalSalesLedgerId) { |
| | | // 1. 查询原订单的所有入库记录 |
| | |
| | | .eq(StockInRecord::getSalesLedgerId, originalSalesLedgerId) |
| | | ); |
| | | |
| | | // 2. 对每条入库记录生成对应的反审出库记录 |
| | | // 2. 删除入库记录并扣减库存 |
| | | for (StockInRecord stockInRecord : stockInRecords) { |
| | | // 2.1 创建反审出库记录 |
| | | StockOutRecord stockOutRecord = new StockOutRecord(); |
| | | stockOutRecord.setOutboundBatches(OrderUtils.countTodayByCreateTime(stockOutRecordMapper, "CK")); // 生成出库批次号 |
| | | stockOutRecord.setStockOutNum(stockInRecord.getStockInNum()); // 出库数量等于原入库数量 |
| | | stockOutRecord.setRecordId(stockInRecord.getId()); // 记录原入库记录ID |
| | | stockOutRecord.setRecordType(StockOutQualifiedRecordTypeEnum.SALE_COUNTER_REVIEW_STOCK_OUT.getCode()); // 设置为销售反审出库类型 |
| | | stockOutRecord.setProductModelId(stockInRecord.getProductModelId()); // 产品规格ID |
| | | stockOutRecord.setRemark("销售-反审出库"); // 备注 |
| | | stockOutRecord.setType(stockInRecord.getType()); // 类型(合格/不合格) |
| | | stockOutRecord.setSalesLedgerId(stockInRecord.getSalesLedgerId()); // 销售订单ID |
| | | stockOutRecord.setSalesLedgerProductId(stockInRecord.getSalesLedgerProductId()); // 销售订单产品ID |
| | | |
| | | // 2.2 插入反审出库记录 |
| | | stockOutRecordMapper.insert(stockOutRecord); |
| | | |
| | | // 2.3 从库存表中扣减相应数量 |
| | | // 从库存表中扣减相应数量 |
| | | StockInventoryDto stockInventoryDto = new StockInventoryDto(); |
| | | stockInventoryDto.setProductModelId(stockOutRecord.getProductModelId()); |
| | | stockInventoryDto.setQualitity(stockOutRecord.getStockOutNum()); |
| | | stockInventoryDto.setProductModelId(stockInRecord.getProductModelId()); |
| | | stockInventoryDto.setQualitity(stockInRecord.getStockInNum()); |
| | | stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto); |
| | | } |
| | | |
| | | // 3. 查询原订单的所有出库记录 |
| | | // 3. 删除所有入库记录 |
| | | stockInRecordMapper.delete( |
| | | Wrappers.<StockInRecord>lambdaQuery() |
| | | .eq(StockInRecord::getSalesLedgerId, originalSalesLedgerId) |
| | | ); |
| | | |
| | | // 4. 查询原订单的所有出库记录 |
| | | List<StockOutRecord> stockOutRecords = stockOutRecordMapper.selectList( |
| | | Wrappers.<StockOutRecord>lambdaQuery() |
| | | .eq(StockOutRecord::getSalesLedgerId, originalSalesLedgerId) |
| | | ); |
| | | |
| | | // 4. 对每条出库记录生成对应的反审入库记录 |
| | | // 5. 删除出库记录并增加库存 |
| | | for (StockOutRecord stockOutRecord : stockOutRecords) { |
| | | // 4.1 创建反审入库记录 |
| | | StockInRecord stockInRecord = new StockInRecord(); |
| | | stockInRecord.setInboundBatches(OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK")); // 生成入库批次号 |
| | | stockInRecord.setStockInNum(stockOutRecord.getStockOutNum()); // 入库数量等于原出库数量 |
| | | stockInRecord.setRecordId(stockOutRecord.getId()); // 记录原出库记录ID |
| | | stockInRecord.setRecordType(StockInQualifiedRecordTypeEnum.SALE_COUNTER_REVIEW_STOCK_IN.getCode()); // 设置为销售反审入库类型 |
| | | stockInRecord.setProductModelId(stockOutRecord.getProductModelId()); // 产品规格ID |
| | | stockInRecord.setRemark("销售-反审入库"); // 备注 |
| | | stockInRecord.setType(stockOutRecord.getType()); // 类型(合格/不合格) |
| | | stockInRecord.setSalesLedgerId(stockOutRecord.getSalesLedgerId()); // 销售订单ID |
| | | stockInRecord.setSalesLedgerProductId(stockOutRecord.getSalesLedgerProductId()); // 销售订单产品ID |
| | | |
| | | // 4.2 插入反审入库记录 |
| | | stockInRecordMapper.insert(stockInRecord); |
| | | |
| | | // 4.3 向库存表中增加相应数量 |
| | | // 向库存表中增加相应数量 |
| | | StockInventoryDto stockInventoryDto = new StockInventoryDto(); |
| | | stockInventoryDto.setProductModelId(stockInRecord.getProductModelId()); |
| | | stockInventoryDto.setQualitity(stockInRecord.getStockInNum()); |
| | | stockInventoryDto.setProductModelId(stockOutRecord.getProductModelId()); |
| | | stockInventoryDto.setQualitity(stockOutRecord.getStockOutNum()); |
| | | stockInventoryMapper.updateAddStockInventory(stockInventoryDto); |
| | | } |
| | | |
| | | // 6. 删除所有出库记录 |
| | | stockOutRecordMapper.delete( |
| | | Wrappers.<StockOutRecord>lambdaQuery() |
| | | .eq(StockOutRecord::getSalesLedgerId, originalSalesLedgerId) |
| | | ); |
| | | } |
| | | } |