| | |
| | | import com.ruoyi.sales.mapper.*; |
| | | import com.ruoyi.sales.pojo.*; |
| | | import com.ruoyi.sales.service.ISalesLedgerService; |
| | | import com.ruoyi.stock.dto.StockInventoryDto; |
| | | import com.ruoyi.stock.mapper.StockInventoryMapper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | // 4. 处理子表数据 |
| | | List<SalesLedgerProduct> productList = salesLedgerDto.getProductData(); |
| | | if (productList != null && !productList.isEmpty()) { |
| | | handleSalesLedgerProducts(salesLedger.getId(), productList, salesLedgerDto.getType()); |
| | | handleSalesLedgerProducts(salesLedger.getId(), productList, salesLedgerDto.getType(), salesLedgerDto.isProduce()); |
| | | updateMainContractAmount( |
| | | salesLedger.getId(), |
| | | productList, |
| | |
| | | } |
| | | |
| | | |
| | | private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) { |
| | | private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type, boolean isProduce) { |
| | | // 按ID分组,区分新增和更新的记录 |
| | | Map<Boolean, List<SalesLedgerProduct>> partitionedProducts = products.stream() |
| | | .peek(p -> p.setSalesLedgerId(salesLedgerId)) |
| | |
| | | List<SalesLedgerProduct> updateList = partitionedProducts.get(true); |
| | | List<SalesLedgerProduct> insertList = partitionedProducts.get(false); |
| | | |
| | | List<StockInventoryDto> stockInventoryDtos = stockInventoryMapper.selectProductList(); |
| | | // List<StockInventoryDto> stockInventoryDtos = stockInventoryMapper.selectProductList(); |
| | | |
| | | // 执行更新操作 |
| | | if (!updateList.isEmpty()) { |
| | | for (SalesLedgerProduct product : updateList) { |
| | | // 查询库存数量 |
| | | SalesLedgerProduct oldProduct = salesLedgerProductMapper.selectById(product.getId()); |
| | | BigDecimal quantityChange = product.getQuantity().subtract(oldProduct.getQuantity()); |
| | | |
| | | // 如果数量增加了,检查库存 |
| | | if (quantityChange.compareTo(BigDecimal.ZERO) > 0) { |
| | | for (StockInventoryDto stockInventoryDto : stockInventoryDtos) { |
| | | if (stockInventoryDto.getProductId().equals(product.getId())) { |
| | | if (quantityChange.compareTo(stockInventoryDto.getQualitity()) > 0) { |
| | | throw new RuntimeException("库存不足"); |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | // // 查询库存数量 |
| | | // SalesLedgerProduct oldProduct = salesLedgerProductMapper.selectById(product.getId()); |
| | | // BigDecimal quantityChange = product.getQuantity().subtract(oldProduct.getQuantity()); |
| | | // |
| | | // // 如果数量增加了,检查库存 |
| | | // if (quantityChange.compareTo(BigDecimal.ZERO) > 0) { |
| | | // for (StockInventoryDto stockInventoryDto : stockInventoryDtos) { |
| | | // if (stockInventoryDto.getProductId().equals(product.getId())) { |
| | | // if (quantityChange.compareTo(stockInventoryDto.getQualitity()) > 0) { |
| | | // throw new RuntimeException("库存不足"); |
| | | // } |
| | | // break; |
| | | // } |
| | | // } |
| | | // } |
| | | product.setType(type); |
| | | salesLedgerProductMapper.updateById(product); |
| | | } |
| | |
| | | // 执行插入操作 |
| | | if (!insertList.isEmpty()) { |
| | | for (SalesLedgerProduct salesLedgerProduct : insertList) { |
| | | for (StockInventoryDto stockInventoryDto : stockInventoryDtos) { |
| | | if (stockInventoryDto.getProductId().equals(salesLedgerProduct.getId())) { |
| | | if (salesLedgerProduct.getQuantity().compareTo(stockInventoryDto.getQualitity()) > 0) { |
| | | throw new RuntimeException("库存不足"); |
| | | } |
| | | } |
| | | } |
| | | // for (StockInventoryDto stockInventoryDto : stockInventoryDtos) { |
| | | // if (stockInventoryDto.getProductId().equals(salesLedgerProduct.getId())) { |
| | | // if (salesLedgerProduct.getQuantity().compareTo(stockInventoryDto.getQualitity()) > 0) { |
| | | // throw new RuntimeException("库存不足"); |
| | | // } |
| | | // } |
| | | // } |
| | | salesLedgerProduct.setType(type); |
| | | salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity()); |
| | | salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice()); |
| | | salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProduct.getTaxInclusiveTotalPrice()); |
| | | salesLedgerProductMapper.insert(salesLedgerProduct); |
| | | // 添加生产数据 |
| | | if (isProduce) { |
| | | salesLedgerProductServiceImpl.addProductionData(salesLedgerProduct); |
| | | } |
| | | } |
| | | } |
| | | } |