Parcourir la source

出入库时更新库存

24282 il y a 1 an
Parent
commit
58a6ada04d

+ 4 - 3
sd-business/src/main/java/com/sd/business/entity/in/emums/InDetailTypeEnum.java

@@ -15,9 +15,10 @@ public enum InDetailTypeEnum {
     PRODUCTION(2, "生产入库"),
     RETURN_GOODS(3, "退货入库"),
     GIVE_BACK(4, "归还入库"),
-    OTHER(5, "其他入库"),
-    ABANDON(6, "废弃入库"),
-    CHECK(7, "盘点入库"),
+    ALLOT(5, "调拨出库"),
+    OTHER(6, "其他入库"),
+    ABANDON(7, "废弃入库"),
+    CHECK(8, "盘点修正"),
     ;
 
     private static final Map<Integer, InDetailTypeEnum> map = new HashMap<>();

+ 2 - 2
sd-business/src/main/java/com/sd/business/entity/in/emums/OutDetailTypeEnum.java

@@ -17,8 +17,8 @@ public enum OutDetailTypeEnum {
     INVENTORY_LOSS(4, "盘亏出库"),
     HELP_ONESELF(5, "自用出库"),
     OTHER(6, "其他出库"),
-    CHECK(7, "盘点出库"),
-    PURCHASE(8, "采购出库"),
+    CHECK(7, "盘点修正"),
+    PURCHASE(8, "采购退货"),
     ;
 
     private static final Map<Integer, OutDetailTypeEnum> map = new HashMap<>();

+ 2 - 1
sd-business/src/main/java/com/sd/business/entity/in/po/InOutStorageBom.java

@@ -2,6 +2,7 @@ package com.sd.business.entity.in.po;
 
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.ruoyi.common.core.domain.BasePo;
+import com.sd.business.entity.inventory.bo.InOutFun;
 import lombok.Getter;
 import lombok.Setter;
 
@@ -20,7 +21,7 @@ import java.math.BigDecimal;
 @Getter
 @Setter
 @TableName("in_out_storage_bom")
-public class InOutStorageBom extends BasePo {
+public class InOutStorageBom extends BasePo implements InOutFun {
 
     /**
      * 出入库id

+ 17 - 0
sd-business/src/main/java/com/sd/business/entity/inventory/bo/InOutFun.java

@@ -0,0 +1,17 @@
+package com.sd.business.entity.inventory.bo;
+
+import java.math.BigDecimal;
+
+public interface InOutFun {
+
+    /**
+     * 出入库产品规格id
+     */
+    Long getBomSpecId();
+
+    /**
+     * 出入库数量
+     */
+    BigDecimal getQuantity();
+
+}

+ 5 - 5
sd-business/src/main/java/com/sd/business/entity/inventory/po/Inventory.java

@@ -31,6 +31,11 @@ public class Inventory extends BasePo {
     private Long bomSpecId;
 
     /**
+     * 事业部id
+     */
+    private Long departmentId;
+
+    /**
      * 结存单价
      */
     private BigDecimal balanceUnitPrice;
@@ -40,9 +45,4 @@ public class Inventory extends BasePo {
      */
     private BigDecimal quantity;
 
-    /**
-     * 事业部id
-     */
-    private Long departmentId;
-
 }

+ 13 - 3
sd-business/src/main/java/com/sd/business/service/in/impl/InOutStorageServiceImpl.java

@@ -14,6 +14,7 @@ import com.sd.business.entity.warehouse.po.Warehouse;
 import com.sd.business.mapper.in.InOutStorageMapper;
 import com.sd.business.service.in.InOutStorageBomService;
 import com.sd.business.service.in.InOutStorageService;
+import com.sd.business.service.inventory.InventoryService;
 import com.sd.business.util.CodeEnum;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -36,6 +37,9 @@ public class InOutStorageServiceImpl extends ServiceImpl<InOutStorageMapper, InO
     @Autowired
     private InOutStorageBomService inOutStorageBomService;
 
+    @Autowired
+    private InventoryService inventoryService;
+
     @Override
     public Page<InOutStorageVo> getPage(InOutStorageSelectDto dto) {
         IWrapper<InOutStorage> wrapper = getWrapper();
@@ -63,15 +67,21 @@ public class InOutStorageServiceImpl extends ServiceImpl<InOutStorageMapper, InO
     @Override
     public void add(InOutStorageDto inOutStorageDto) {
 
-        InOutTypeEnum inOutType = InOutTypeEnum.getInOutType(inOutStorageDto.getType());
-        if (inOutType.equals(InOutTypeEnum.IN)) {
+        List<InOutStorageBom> inOutStorageBomList = inOutStorageDto.getInOutStorageBomList();
+
+        if (InOutTypeEnum.getInOutType(inOutStorageDto.getType()).equals(InOutTypeEnum.IN)) {
+            // 赋值入库单号
             inOutStorageDto.setCode(CodeEnum.IN_CODE.getCode());
+            // 更新库存
+            inventoryService.in(inOutStorageDto.getDepartmentId(), inOutStorageDto.getWarehouseId(), inOutStorageBomList);
         } else {
+            // 赋值出库单号
             inOutStorageDto.setCode(CodeEnum.OUT_CODE.getCode());
+            // 更新库存
+            inventoryService.out(inOutStorageDto.getDepartmentId(), inOutStorageDto.getWarehouseId(), inOutStorageBomList);
         }
         this.save(inOutStorageDto);
 
-        List<InOutStorageBom> inOutStorageBomList = inOutStorageDto.getInOutStorageBomList();
         inOutStorageBomList.forEach(item -> item.setInOutStorageId(inOutStorageDto.getId()));
         inOutStorageBomService.saveBatch(inOutStorageBomList);
 

+ 19 - 0
sd-business/src/main/java/com/sd/business/service/inventory/InventoryService.java

@@ -2,6 +2,7 @@ package com.sd.business.service.inventory;
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.ruoyi.common.core.service.BaseService;
+import com.sd.business.entity.inventory.bo.InOutFun;
 import com.sd.business.entity.inventory.dto.InventorySelectDto;
 import com.sd.business.entity.inventory.po.Inventory;
 import com.sd.business.entity.inventory.vo.InventoryVo;
@@ -36,4 +37,22 @@ public interface InventoryService extends BaseService<Inventory> {
      */
     List<QuantityByDepartmentVo> getQuantityByDepartment();
 
+    /**
+     * 入库逻辑
+     *
+     * @param departmentId 事业部id
+     * @param warehouseId  仓库id
+     * @param list         入库明细
+     */
+    void in(Long departmentId, Long warehouseId, List<? extends InOutFun> list);
+
+    /**
+     * 出库逻辑
+     *
+     * @param departmentId 事业部id
+     * @param warehouseId  仓库id
+     * @param list         出库明细
+     */
+    void out(Long departmentId, Long warehouseId, List<? extends InOutFun> list);
+
 }

+ 76 - 0
sd-business/src/main/java/com/sd/business/service/inventory/impl/InventoryServiceImpl.java

@@ -2,9 +2,11 @@ package com.sd.business.service.inventory.impl;
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.wrapper.IWrapper;
 import com.sd.business.entity.bom.po.Bom;
 import com.sd.business.entity.bom.po.BomSpec;
+import com.sd.business.entity.inventory.bo.InOutFun;
 import com.sd.business.entity.inventory.dto.InventorySelectDto;
 import com.sd.business.entity.inventory.po.Inventory;
 import com.sd.business.entity.inventory.po.InventoryBackup;
@@ -12,10 +14,17 @@ import com.sd.business.entity.inventory.vo.InventoryVo;
 import com.sd.business.entity.inventory.vo.QuantityByDepartmentVo;
 import com.sd.business.entity.inventory.vo.QuantityByWarehouseVo;
 import com.sd.business.mapper.inventory.InventoryMapper;
+import com.sd.business.service.bom.BomSpecService;
 import com.sd.business.service.inventory.InventoryService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.math.BigDecimal;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 
 /**
@@ -29,6 +38,9 @@ import java.util.List;
 @Service
 public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {
 
+    @Autowired
+    private BomSpecService bomSpecService;
+
     @Override
     public Page<InventoryVo> getPage(InventorySelectDto dto, String tableName) {
         IWrapper<Inventory> wrapper = getWrapper();
@@ -63,5 +75,69 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
         return baseMapper.getQuantityByDepartment();
     }
 
+    @Override
+    public void in(Long departmentId, Long warehouseId, List<? extends InOutFun> list) {
+        List<Long> bomSpecIdList = list.stream().map(InOutFun::getBomSpecId).collect(Collectors.toList());
+
+        synchronized (this) {
+            List<Inventory> inventoryList = getInventoryList(departmentId, warehouseId, bomSpecIdList);
+            Map<Long, Inventory> map = inventoryList.stream().collect(Collectors.toMap(Inventory::getBomSpecId, Function.identity()));
+            for (InOutFun inOutFun : list) {
+                Long bomSpecId = inOutFun.getBomSpecId();
+                BigDecimal quantity = inOutFun.getQuantity();
+                Inventory inventory = map.get(bomSpecId);
+                if (inventory != null) {
+                    inventory.setQuantity(inventory.getQuantity().add(quantity));
+                } else {
+                    inventory = new Inventory();
+                    inventory.setWarehouseId(warehouseId);
+                    inventory.setDepartmentId(departmentId);
+                    inventory.setBomSpecId(bomSpecId);
+                    inventory.setQuantity(quantity);
+                    inventoryList.add(inventory);
+                    map.put(bomSpecId, inventory);
+                }
+            }
+            saveOrUpdateBatch(inventoryList);
+        }
+
+    }
+
+    @Override
+    public void out(Long departmentId, Long warehouseId, List<? extends InOutFun> list) {
+        Map<Long, BigDecimal> map = list.stream().collect(Collectors.toMap(InOutFun::getBomSpecId, InOutFun::getQuantity));
+        List<Long> bomSpecIdList = new ArrayList<>(map.keySet());
+
+        synchronized (this) {
+            List<Inventory> inventoryList = getInventoryList(departmentId, warehouseId, bomSpecIdList);
+
+            // 查询库存中是否存在bom
+            for (Inventory inventory : inventoryList) {
+                Long bomSpecId = inventory.getBomSpecId();
+                if (!bomSpecIdList.contains(bomSpecId)) {
+                    BomSpec bomSpec = bomSpecService.getById(bomSpecId);
+                    if (bomSpec == null) {
+                        throw new ServiceException("未知bom规格id:" + bomSpecId);
+                    }
+                    throw new ServiceException("出库失败,品名为:" + bomSpec.getName() + "的bom库存不足");
+                }
+            }
+
+            for (Inventory inventory : inventoryList) {
+                inventory.setQuantity(inventory.getQuantity().subtract(map.get(inventory.getBomSpecId())));
+            }
+            updateBatchById(inventoryList);
+        }
+
+    }
+
+    private List<Inventory> getInventoryList(Long departmentId, Long warehouseId, List<Long> bomSpecIdList) {
+        return list(q -> q
+                .eq(Inventory::getDepartmentId, departmentId)
+                .eq(Inventory::getWarehouseId, warehouseId)
+                .in(Inventory::getBomSpecId, bomSpecIdList)
+        );
+    }
+
 
 }