FengChaoYu 7 mēneši atpakaļ
vecāks
revīzija
b01e7e5805

+ 35 - 0
mall-server-api/src/main/java/com/gree/mall/manager/logic/material/manage/WebsitSalesLogic.java

@@ -1015,6 +1015,9 @@ public class WebsitSalesLogic {
                 Map<String, List<WebsitSalesItemBean>> itemMap = salesItemBeanList.stream()
                         .collect(Collectors.groupingBy(WebsitSalesItemBean::getSalesId));
 
+                // 装本次导入检查的对应辅材库存
+                Map<String, WebsitStock> websitStockMap = new HashMap<>();
+
                 // 注入销售单和明细
                 for (WebsitSalesBean websitSalesBean : salesBeanList) {
                     WebsitSales sales = new WebsitSales();
@@ -1024,11 +1027,43 @@ public class WebsitSalesLogic {
 
                     List<WebsitSalesItemBean> itemBeans = itemMap.get(sales.getSalesId());
 
+                    // 获取网点辅材库存
+                    final List<WebsitStock> websitStockList = websitStockService.lambdaQuery()
+                            .eq(WebsitStock::getCompanyWechatId, sales.getCompanyWechatId())
+                            .eq(WebsitStock::getWebsitId, sales.getWebsitId())
+                            .eq(WebsitStock::getStorageId, sales.getStorageId())
+                            .eq(WebsitStock::getGoodsType, WebsitGoodsTypeEnum.M.getKey())
+                            .in(WebsitStock::getGoodsId, itemBeans.stream()
+                                    .map(WebsitSalesItemBean::getGoodsId)
+                                    .collect(Collectors.toList()))
+                            .list();
+
+                    Map<String, WebsitStock> stockMap = websitStockList.stream()
+                            .collect(Collectors.toMap(WebsitStock::getGoodsId, Function.identity()));
 
                     for (WebsitSalesItemBean itemBean : itemBeans) {
                         WebsitSalesItem item = new WebsitSalesItem();
                         BeanUtils.copyProperties(itemBean, item);
 
+                        String stockKey = sales.getCompanyWechatId() + sales.getWebsitId() + sales.getStorageId() + item.getGoodsId();
+                        WebsitStock curStock;
+                        if (websitStockMap.containsKey(stockKey)) {
+                            curStock = websitStockMap.get(stockKey);
+                        } else {
+                            curStock = stockMap.get(item.getGoodsId());
+                        }
+
+                        if (Objects.isNull(curStock)) {
+                            throw new RemoteServiceException(sales.getWebsitName() + " 仓库 " + sales.getStorageName() + " 没有”" + item.getGoodsName() + "”库存信息");
+                        }
+
+                        if (item.getSalesQty().compareTo(curStock.getQty()) > 0) {
+                            throw new RemoteServiceException(sales.getWebsitName() + " 仓库 " + sales.getStorageName() + " 的“" + item.getGoodsName() + "“库存数量" + curStock.getQty() + "少于销售数量");
+                        }
+
+                        curStock.setQty(curStock.getQty().subtract(item.getSalesQty()));
+                        websitStockMap.put(stockKey, curStock);
+
                         totalAmount = totalAmount.add(item.getSaleAmount());
                         item.setCompanyWechatId(sales.getCompanyWechatId());
 

+ 4 - 12
mall-server-api/src/main/java/com/gree/mall/manager/logic/material/stock/MaterialGoodsStockLogic.java

@@ -200,30 +200,22 @@ public class MaterialGoodsStockLogic {
     }
 
     private void saveWebsitStock(WebsitStockDTO websitStockDTO, List<WebsitStockDTO> stockList) {
-        //        System.out.println("处理网点库存入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
         // 查询存在的配件库存
         List<WebsitStock> existWebsitStockList = materialGoodsStockCMapper.queryExistWebsitStockList(websitStockDTO.getCompanyWechatId(),
                 websitStockDTO.getWebsitId(), websitStockDTO.getStorageId(), stockList);
 
-//        System.out.println("计算库存入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
         // 全部商品库存
-        Map<String, WebsitStock> existShopStockMap = this.createNotExistStock(stockList, existWebsitStockList);
+        Map<String, WebsitStock> existWebsitStockMap = this.createNotExistStock(stockList, existWebsitStockList);
         // 计算商品库存
-//        List<PartsShopStock> shopStockList = new ArrayList<>();
         List<WebsitStockAcc> shopStockAccList = new ArrayList<>();
-        this.computeWebsitStock(stockList, existShopStockMap, null, shopStockAccList);
+        this.computeWebsitStock(stockList, existWebsitStockMap, null, shopStockAccList);
 
         if (CollectionUtil.isEmpty(shopStockAccList)) {
             throw new RemoteServiceException("商品库存处理失败");
         }
-//        System.out.println("计算商品库存出口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
-//        System.out.println("保存商品库存入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
-//        partsShopStockService.saveOrUpdateBatch(shopStockList);
-        websitStockService.saveOrUpdateBatch(existShopStockMap.values());
-//        System.out.println("保存商品库存出口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
-//        System.out.println("保存商品库存三级帐入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
+
+        websitStockService.saveOrUpdateBatch(existWebsitStockMap.values());
         websitStockAccService.saveBatch(shopStockAccList);
-//        System.out.println("保存商品库存三级帐出口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
     }
 
     private Map<String, WebsitStock> createNotExistStock(List<WebsitStockDTO> stockList, List<WebsitStock> existWebsitStockList) {