| | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.stream.Collectors; |
| | |
| | | public AjaxResult getProductInventory(SalesLedgerProduct salesLedgerProduct){ |
| | | List<SalesLedgerProduct> list = salesLedgerProductService.selectSalesLedgerProductList(salesLedgerProduct); |
| | | if(CollectionUtils.isEmpty(list)){ |
| | | return AjaxResult.error("该产品不存在"); |
| | | throw new RuntimeException("该产品不存在"); |
| | | } |
| | | List<ProcurementPageDto> procurementPageDtoList = new ArrayList<>(); |
| | | list.forEach(item -> { |
| | | ProcurementPageDto procurementDto = new ProcurementPageDto(); |
| | | procurementDto.setSalesLedgerProductId(String.valueOf(item.getId())); |
| | | Page<ProcurementPageDto> page = new Page<>(1, 50); |
| | | IPage<ProcurementPageDto> procurementPageDtoIPage = procurementRecordService.listPage(page, procurementDto); |
| | | procurementPageDtoList.addAll(procurementPageDtoIPage.getRecords()); |
| | | }); |
| | | if(!CollectionUtils.isEmpty(procurementPageDtoList)){ |
| | | return AjaxResult.error("该产品库存不存在"); |
| | | |
| | | Map<Long, BigDecimal> requiredQuantities = list.stream() |
| | | .filter(p -> p.getProductModelId() != null) |
| | | .collect(Collectors.groupingBy( |
| | | SalesLedgerProduct::getProductModelId, |
| | | Collectors.mapping(SalesLedgerProduct::getQuantity, Collectors.reducing(BigDecimal.ZERO, (a, b) -> a.add(b != null ? b : BigDecimal.ZERO))) |
| | | )); |
| | | |
| | | for (Map.Entry<Long, BigDecimal> entry : requiredQuantities.entrySet()) { |
| | | Long modelId = entry.getKey(); |
| | | BigDecimal neededQty = entry.getValue(); |
| | | |
| | | ProcurementPageDto queryDto = new ProcurementPageDto(); |
| | | queryDto.setProductModelId(modelId); |
| | | |
| | | // 获取该规格型号的所有库存记录 |
| | | IPage<ProcurementPageDto> inventoryPage = procurementRecordService.listPage(new Page<>(1, -1), queryDto); |
| | | |
| | | // 汇总所有批次的可用库存 |
| | | BigDecimal totalStock = inventoryPage.getRecords().stream() |
| | | .map(ProcurementPageDto::getInboundNum0) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | if (totalStock.compareTo(neededQty) < 0) { |
| | | String modelName = list.stream() |
| | | .filter(p -> modelId.equals(p.getProductModelId())) |
| | | .map(SalesLedgerProduct::getSpecificationModel) |
| | | .findFirst() |
| | | .orElse("未知型号"); |
| | | return AjaxResult.error("产品 [" + modelName + "] 库存不足,所需: " + neededQty + ", 当前总库存: " + totalStock); |
| | | } |
| | | } |
| | | AtomicInteger num = new AtomicInteger(); |
| | | list.forEach(item -> { |
| | | procurementPageDtoList.forEach(procurementPageDto -> { |
| | | if(String.valueOf(item.getId().intValue()).equals(procurementPageDto.getSalesLedgerProductId())){ |
| | | if (item.getQuantity().compareTo(procurementPageDto.getInboundNum0()) <= 0) { |
| | | num.getAndIncrement(); |
| | | } |
| | | } |
| | | }); |
| | | }); |
| | | if(num.get() == list.size()){ |
| | | return AjaxResult.success("该产品库存充足"); |
| | | } |
| | | return AjaxResult.error("该产品库存不足"); |
| | | |
| | | return AjaxResult.success("该产品库存充足"); |
| | | } |
| | | |
| | | /** |
| | | * 根据采购合同号查询详情 |
| | | */ |
| | | @GetMapping("/getSalesByCode") |
| | | public AjaxResult getSalesByCode(SalesLedgerDto salesLedgerDto) { |
| | | return AjaxResult.success(salesLedgerService.getSalesByCode(salesLedgerDto)); |
| | | } |
| | | } |