Explorar el Código

Merge remote-tracking branch 'origin/master'

24282 hace 1 año
padre
commit
8c904d4272

+ 0 - 6
sd-business/src/main/java/com/sd/business/entity/in/po/InOutStorageBom.java

@@ -41,11 +41,5 @@ public class InOutStorageBom extends BasePo implements InOutFun {
     @DecimalMin(value = "0.01", message = "出入库数量必须大于0")
     private BigDecimal quantity;
 
-    /**
-     * 单价
-     */
-    @NotNull(message = "出入库单价不能为空")
-    @DecimalMin(value = "0.01",message = "入库单价必须大于0")
-    private BigDecimal unitPrice;
 
 }

+ 4 - 9
sd-business/src/main/java/com/sd/business/entity/in/po/InOutStorageDetails.java

@@ -26,12 +26,7 @@ public class InOutStorageDetails extends BasePo {
     private Long inOutStorageId;
 
     /**
-     * 类型:1入库 0出库
-     */
-    private Long type;
-
-    /**
-     * 库存id
+     * 事业部id
      */
     private Long departmentId;
 
@@ -46,17 +41,17 @@ public class InOutStorageDetails extends BasePo {
     private Long bomSpecId;
 
     /**
-     * 入库金额
+     * 金额
      */
     private BigDecimal inSumPrice;
 
     /**
-     * 入库单价
+     * 单价
      */
     private BigDecimal inUnitPrice;
 
     /**
-     * 入库数量
+     * 数量
      */
     private BigDecimal quantity;
 

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

@@ -14,10 +14,4 @@ public interface InOutFun {
      */
     BigDecimal getQuantity();
 
-    /**
-     * 入库单价
-     * @return
-     */
-    BigDecimal getUnitPrice();
-
 }

+ 3 - 0
sd-business/src/main/java/com/sd/business/entity/inventory/po/BalancePriceNum.java

@@ -1,5 +1,7 @@
 package com.sd.business.entity.inventory.po;
 
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonInclude;
 import com.ruoyi.common.core.domain.BasePo;
 import lombok.Getter;
 import lombok.Setter;
@@ -8,6 +10,7 @@ import java.math.BigDecimal;
 
 @Getter
 @Setter
+@TableName("in_out_storage_details")
 public class BalancePriceNum extends BasePo {
 
     /**

+ 4 - 0
sd-business/src/main/java/com/sd/business/mapper/in/InOutStorageDetailsMapper.java

@@ -5,8 +5,12 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.sd.business.entity.in.vo.InOutStorageDetailsVo;
 import com.ruoyi.common.utils.wrapper.IWrapper;
+import com.sd.business.entity.inventory.po.BalancePriceNum;
 import org.apache.ibatis.annotations.Param;
 
+import java.math.BigDecimal;
+import java.util.List;
+
 
 /**
  * <p>

+ 6 - 6
sd-business/src/main/java/com/sd/business/mapper/inventory/InventoryMapper.java

@@ -12,6 +12,7 @@ import com.sd.business.entity.inventory.vo.QuantityByDepartmentVo;
 import com.sd.business.entity.inventory.vo.QuantityByWarehouseVo;
 import org.apache.ibatis.annotations.Param;
 
+import java.math.BigDecimal;
 import java.util.List;
 
 
@@ -43,15 +44,14 @@ public interface InventoryMapper extends BaseMapper<Inventory> {
     List<QuantityByDepartmentVo> getQuantityByDepartment();
 
     /**
-     * 根据规格id获取结存金额和结存数量
-     */
-    BalancePriceNum getPriceAndNum(@Param("bomSpecId") Long bomSpecId,
-                                   @Param("warehouseId") Long warehouseId);
-
-    /**
      * 获取出入库详情表的数据
      */
     List<InOutStorageDetails> getInOutDetails(@Param("warehouseId") Long warehouseId,
                                               @Param("departmentId") Long departmentId,
                                               @Param("bomSpecId") Long bomSpecId);
+
+    /**
+     * 根据bomSpecId获取结存数量
+     */
+    BigDecimal getQuantityByBomSpecId(@Param("bomSpecId") Long bomSpecId, @Param("warehouseId") Long warehouseId);
 }

+ 2 - 4
sd-business/src/main/java/com/sd/business/service/in/impl/InOutStorageDetailsServiceImpl.java

@@ -30,15 +30,13 @@ public class InOutStorageDetailsServiceImpl extends ServiceImpl<InOutStorageDeta
     public Page<InOutStorageDetailsVo> getPage(InOutStorageDetailsSelectDto dto) {
         IWrapper<InOutStorageDetails> wrapper = getWrapper();
         wrapper.orderByDesc("iosd", InOutStorageDetails::getId);
-        Page<InOutStorageDetailsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
-        return page;
+        return this.baseMapper.getPage(dto.getPage(), wrapper);
     }
 
     @Override
     public InOutStorageDetailsVo detail(Long id) {
         InOutStorageDetails InOutStorageDetails = this.getById(id);
-        InOutStorageDetailsVo result = BeanUtil.toBean(InOutStorageDetails, InOutStorageDetailsVo.class);
-        return result;
+        return BeanUtil.toBean(InOutStorageDetails, InOutStorageDetailsVo.class);
     }
 
     @Override

+ 18 - 14
sd-business/src/main/java/com/sd/business/service/in/impl/InOutStorageServiceImpl.java

@@ -41,6 +41,8 @@ import java.util.List;
  */
 @Service
 public class InOutStorageServiceImpl extends ServiceImpl<InOutStorageMapper, InOutStorage> implements InOutStorageService {
+    //单价逻辑还没做完,先拿个常量顶着
+    public static final BigDecimal UNITPRICE = BigDecimal.TEN;
 
     @Autowired
     private InOutStorageBomService inOutStorageBomService;
@@ -97,20 +99,22 @@ public class InOutStorageServiceImpl extends ServiceImpl<InOutStorageMapper, InO
         List<InOutStorageDetails> detailsList = new ArrayList<>();
         //进行for循环
         for (InOutStorageBom bom : inOutStorageBomList) {
-            //创建InOutStorageDetails对象
-            InOutStorageDetails details = new InOutStorageDetails();
-            //将数据进行填充
-            details.setInOutStorageId(bom.getInOutStorageId());
-            details.setInUnitPrice(bom.getUnitPrice());
-            details.setQuantity(bom.getQuantity());
-            details.setWarehouseId(inOutStorageDto.getWarehouseId());
-            details.setBomSpecId(bom.getBomSpecId());
-            details.setDepartmentId(inOutStorageDto.getDepartmentId());
-            details.setType(1L);
-            //计算入库金额
-            BigDecimal sumPrice = bom.getUnitPrice().multiply(bom.getQuantity());
-            details.setInSumPrice(sumPrice);
-            detailsList.add(details);
+            if (inOutStorageDto.getType().equals(1)){
+                //创建InOutStorageDetails对象
+                InOutStorageDetails details = new InOutStorageDetails();
+                //将数据进行填充
+                details.setInOutStorageId(bom.getInOutStorageId());
+                details.setInUnitPrice(UNITPRICE);
+                details.setQuantity(bom.getQuantity());
+                details.setWarehouseId(inOutStorageDto.getWarehouseId());
+                details.setBomSpecId(bom.getBomSpecId());
+                details.setDepartmentId(inOutStorageDto.getDepartmentId());
+
+                //计算入库金额
+                BigDecimal sumPrice = UNITPRICE.multiply(bom.getQuantity());
+                details.setInSumPrice(sumPrice);
+                detailsList.add(details);
+            }
         }
         //进行数据的添加
         detailsService.saveBatch(detailsList);

+ 130 - 60
sd-business/src/main/java/com/sd/business/service/inventory/impl/InventoryServiceImpl.java

@@ -1,5 +1,8 @@
 package com.sd.business.service.inventory.impl;
 
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.collection.CollectionUtil;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.exception.ServiceException;
@@ -16,10 +19,13 @@ import com.sd.business.entity.inventory.po.InventoryBackup;
 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.in.InOutStorageDetailsMapper;
 import com.sd.business.mapper.inventory.InventoryMapper;
 import com.sd.business.service.bom.BomSpecService;
 import com.sd.business.service.in.InOutStorageDetailsService;
+import com.sd.business.service.in.impl.InOutStorageServiceImpl;
 import com.sd.business.service.inventory.InventoryService;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -41,6 +47,7 @@ import java.util.stream.Collectors;
  * @author
  * @since 2023-07-03
  */
+@Slf4j
 @Service
 public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {
 
@@ -96,7 +103,7 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
             for (InOutFun inOutFun : list) {
                 Long bomSpecId = inOutFun.getBomSpecId();
                 BigDecimal quantity = inOutFun.getQuantity();
-                BigDecimal unitPrice = inOutFun.getUnitPrice();
+                BigDecimal unitPrice = InOutStorageServiceImpl.UNITPRICE;
                 Inventory inventory = map.get(bomSpecId);
 
                 if (inventory != null) {
@@ -111,28 +118,38 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
                     map.put(bomSpecId, inventory);
                 }
 
-                // 根据bom规格id和仓库id获取结存金额和结存数量
-                BalancePriceNum priceAndNum = baseMapper.getPriceAndNum(bomSpecId, warehouseId);
-                BigDecimal balanceNum = priceAndNum.getBalanceNum();
-                BigDecimal balancePrice = priceAndNum.getBalancePrice();
+                List<InOutStorageDetails> detailsList = getDetailsList(warehouseId, bomSpecIdList);
+                if (CollUtil.isEmpty(detailsList)){
+                    for (InOutStorageDetails details : detailsList) {
+                        details.setInSumPrice(BigDecimal.ZERO);
+                        details.setQuantity(BigDecimal.ZERO);
+                    }
+                }
+                BigDecimal sumPrice = detailsList.stream().map(InOutStorageDetails::getInSumPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
+                BigDecimal sumQuantity = detailsList.stream().map(InOutStorageDetails::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
 
                 // 计算最新的结存数量和结存金额
-                balanceNum = balanceNum.add(quantity);
+                sumQuantity = sumQuantity.add(quantity);
+
                 BigDecimal balancep = quantity.multiply(unitPrice);
-                balancePrice = balancePrice.add(balancep);
-                BigDecimal balanceUnitPrice = balancePrice.divide(balanceNum, 2, BigDecimal.ROUND_HALF_UP);
+                sumPrice = sumPrice.add(balancep);
+                BigDecimal balanceUnitPrice = sumPrice.divide(sumQuantity, 2, BigDecimal.ROUND_HALF_UP);
                 inventory.setBalanceUnitPrice(balanceUnitPrice);
             }
 
             saveOrUpdateBatch(inventoryList);
         }
-
     }
 
     @Override
     public void out(Long departmentId, Long warehouseId, List<? extends InOutFun> list) {
+        Map<Long, BigDecimal> quantityMap = list.stream().collect(Collectors.toMap(InOutFun::getBomSpecId, InOutFun::getQuantity));
+
         Map<Long, BigDecimal> map = list.stream().collect(Collectors.toMap(InOutFun::getBomSpecId, InOutFun::getQuantity));
+
         List<Long> bomSpecIdList = new ArrayList<>(map.keySet());
+        //填装不同bomSpecId的数据
+        Map<Long, List<InOutStorageDetails>> detailsHashMap = new HashMap<>();
 
         synchronized (this) {
             List<Inventory> inventoryList = getInventoryList(departmentId, warehouseId, bomSpecIdList);
@@ -151,67 +168,119 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
 
             //根据仓库id和规格id获取出入详细表数据
             List<InOutStorageDetails> detailsList = getDetailsList(warehouseId, bomSpecIdList);
-            List<InOutStorageDetails> updateList = new ArrayList<>();
-            List<InOutStorageDetails> outDetailsList = new ArrayList<>();
-            Map<Long,BigDecimal> outPriceMap = new HashMap<>();
+            //将数据为0的记录过滤掉
+            detailsList = detailsList.stream().filter(q -> q.getQuantity().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
 
-            for (int i = 0; i < detailsList.size(); i++) {
-                InOutStorageDetails details = detailsList.get(i);
+            if (CollUtil.isEmpty(detailsList)) {
+                throw new ServiceException("数据不存在");
             }
 
-            for (InOutStorageDetails details : detailsList) {
-                //如果数量足够减扣
-                BigDecimal outNum = map.get(details.getBomSpecId());
-                if (details.getQuantity().compareTo(outNum) >= 0){
+            detailsHashMap = detailsList.stream().collect(Collectors.groupingBy(InOutStorageDetails::getBomSpecId));
+
+            //存储需要修改的入库记录
+            List<InOutStorageDetails> updateList = new ArrayList<>();
+            //存储这次出库的金额
+            Map<Long, BigDecimal> outPriceMap = new HashMap<>();
+
+            for (Long bomSpecId : bomSpecIdList) {
+                List<InOutStorageDetails> outStorageDetails = detailsHashMap.get(bomSpecId);
+                int j = 0;
+                BigDecimal num;
+                BigDecimal quantity;
+
+                do {
+                    //获取这条记录剩余的数量
+                    InOutStorageDetails details = outStorageDetails.get(j);
+
+                    //如果该记录为最后一条记录
+                    if (j == outStorageDetails.size() - 1) {
+                        BigDecimal naxtQuantity = outStorageDetails.get(j).getQuantity();
+                        //判断库存是否够扣减.不够则抛异常
+                        if (naxtQuantity.compareTo(map.get(details.getBomSpecId())) < 0) {
+                            throw new ServiceException("库存不够!!");
+                        }
+                    }
+
+                    // 获取数据库中整体数量与出库数量对比
+                    BigDecimal allQuantity = baseMapper.getQuantityByBomSpecId(details.getBomSpecId(), details.getWarehouseId());
+                    if (allQuantity.compareTo(quantityMap.get(details.getBomSpecId())) < 0) {
+                        throw new ServiceException("库存不够!");
+                    }
+
+                    //获取这次一种规格的出库的数量
+                    quantity = map.get(details.getBomSpecId());
+
+                    //获取两次数量直接的差价
+                    num = details.getQuantity().subtract(quantity);
+
+                    //如果这条记录库存不足,则计算下一条记录
+                    if (num.compareTo(BigDecimal.ZERO) < 0) {
+                        addOutPriceMap(outPriceMap, map, details);
+
+                        details.setQuantity(BigDecimal.ZERO);
+                        details.setInSumPrice(BigDecimal.ZERO);
+
+                        updateList.add(details);
+                        map.put(details.getBomSpecId(), num.abs());
+                        j++;
+                        continue;
+                    }
                     //对数量进行减扣
-                    details.setQuantity(details.getQuantity().subtract(outNum));
+                    //BigDecimal detailsNum = details.getQuantity().subtract(num);
+                    details.setQuantity(num.abs());
+
+                    //数量减少了,那金额也得减少
+                    BigDecimal multiply = num.abs().multiply(details.getInUnitPrice());
+                    details.setInSumPrice(multiply);
 
                     //将这次出库的金额存储到map里面
-                    BigDecimal outPrice = details.getInUnitPrice().multiply(outNum);
-                    outPriceMap.put(details.getBomSpecId(),outPrice);
+                    addOutPriceMap(outPriceMap, map, details);
 
                     //将减扣数量后的对象修改回去
                     updateList.add(details);
-                    //创建一个新的对象
-                    InOutStorageDetails outStorageDetails = new InOutStorageDetails();
-                    //将数据进行拷贝
-                    BeanUtils.copyProperties(details,outStorageDetails);
-                    //修改需要修改的值
-                    outStorageDetails.setQuantity(outNum);
-                    outStorageDetails.setId(null);
-                    outStorageDetails.setType(0L);
-                    //添加记录
-                    outDetailsList.add(outStorageDetails);
-                    break;
-                }
-                // 数量不够减扣
-                // 获取剩余的数量
-                BigDecimal remainderNum = outNum.subtract(details.getQuantity());
-                if (remainderNum.compareTo(BigDecimal.ZERO) > 0) {
-                    details.setQuantity(BigDecimal.ZERO);
-                    updateList.add(details);
-                }
-                // TODO 这时候需要顺延到下一条记录,暂时没想好怎么顺
-                // 上面也许可以不用增强for,普通for试试
-            }
-            inOutStorageDetailsService.updateBatchById(updateList);
-            inOutStorageDetailsService.saveBatch(outDetailsList);
+                    j++;
 
-            for (Inventory inventory : inventoryList) {
-                // 获取之前的结存金额
-                BigDecimal balancePrice = inventory.getQuantity().multiply(inventory.getBalanceUnitPrice());
-                // 减去出库金额后的结存金额
-                BigDecimal newBalancePrice = balancePrice.subtract(outPriceMap.get(inventory.getBomSpecId()));
-                // 减去出库数量后的结存数量
-                inventory.setQuantity(inventory.getQuantity().subtract(map.get(inventory.getBomSpecId())));
-                // 获取最后的结存单价
-                BigDecimal balanceUnitPrice = newBalancePrice.divide(inventory.getQuantity(), 2, BigDecimal.ROUND_HALF_UP);
-                //将数据存入对象
-                inventory.setBalanceUnitPrice(balanceUnitPrice);
+                } while (num.compareTo(BigDecimal.ZERO) < 0 && j < outStorageDetails.size());
             }
+                inOutStorageDetailsService.updateBatchById(updateList);
+
+                for (Inventory inventory : inventoryList) {
+                    // 获取之前的结存金额
+                    BigDecimal balancePrice = inventory.getQuantity().multiply(inventory.getBalanceUnitPrice());
+                    // 减去出库金额后的结存金额
+                    BigDecimal newBalancePrice = BigDecimal.ZERO;
+                    if (outPriceMap.get(inventory.getBomSpecId()) == null) {
+                        newBalancePrice = balancePrice.subtract(BigDecimal.ZERO);
+                    } else {
+                        newBalancePrice = newBalancePrice.add(balancePrice.subtract(outPriceMap.get(inventory.getBomSpecId())));
+                    }
+                    // 减去出库数量后的结存数量
+                    inventory.setQuantity(inventory.getQuantity().subtract(quantityMap.get(inventory.getBomSpecId())));
+                    // 获取最后的结存单价
+                    BigDecimal balanceUnitPrice;
+                    if (inventory.getQuantity().compareTo(BigDecimal.ZERO) == 0) {
+                        balanceUnitPrice = BigDecimal.ZERO;
+                    } else {
+                        balanceUnitPrice = newBalancePrice.divide(inventory.getQuantity(), 2, BigDecimal.ROUND_HALF_UP);
+                    }
+                    //将数据存入对象
+                    inventory.setBalanceUnitPrice(balanceUnitPrice);
+                }
             updateBatchById(inventoryList);
         }
+    }
 
+    //将出库金额添加到map
+    private void addOutPriceMap(Map<Long, BigDecimal> outPriceMap, Map<Long, BigDecimal> map, InOutStorageDetails details) {
+        BigDecimal mapQuantity = map.get(details.getBomSpecId());
+        BigDecimal multiply = mapQuantity.multiply(details.getInUnitPrice());
+        if (outPriceMap.containsKey(details.getBomSpecId())) {
+            BigDecimal naxtPrice = outPriceMap.get(details.getBomSpecId());
+            BigDecimal addPrice = naxtPrice.add(multiply);
+            outPriceMap.put(details.getBomSpecId(), addPrice);
+        } else {
+            outPriceMap.put(details.getBomSpecId(), multiply);
+        }
     }
 
     private List<Inventory> getInventoryList(Long departmentId, Long warehouseId, List<Long> bomSpecIdList) {
@@ -225,9 +294,10 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
     private List<InOutStorageDetails> getDetailsList(Long warehouseId, List<Long> bomSpecIdList) {
         return inOutStorageDetailsService.list(q -> q
                 .eq(InOutStorageDetails::getWarehouseId, warehouseId)
-                .in(InOutStorageDetails::getBomSpecId, bomSpecIdList)
-                .isNotNull(InOutStorageDetails::getQuantity)
-        );
-    }
+                    .in(InOutStorageDetails::getBomSpecId, bomSpecIdList)
+                    .isNotNull(InOutStorageDetails::getQuantity)
+
+            );
+        }
 
 }

+ 1 - 0
sd-business/src/main/resources/mapper/in/InOutStorageBomMapper.xml

@@ -26,4 +26,5 @@
             ${ew.customSqlSegment}
     </select>
 
+
 </mapper>

+ 4 - 2
sd-business/src/main/resources/mapper/in/InOutStorageDetailsMapper.xml

@@ -5,8 +5,7 @@
         select
             iosd.id,
             iosd.in_out_storage_id,
-            iosd.type,
-            iosd.inventory_id,
+            iosd.department_id,
             iosd.warehouse_id,
             iosd.bom_spec_id,
             iosd.in_sum_price,
@@ -20,4 +19,7 @@
             ${ew.customSqlSegment}
     </select>
 
+
+
+
 </mapper>

+ 14 - 19
sd-business/src/main/resources/mapper/inventory/InventoryMapper.xml

@@ -46,25 +46,10 @@
         order by d.id
     </select>
 
-    <select id="getPriceAndNum" resultType="com.sd.business.entity.inventory.po.BalancePriceNum">
-        select sum(ifnull(i.in_sum_price, 0)) balancePrice
-              ,sum(ifnull(i.quantity, 0)) balanceNum
-        from in_out_storage_details i
-        <where>
-            <if test="bomSpecId != null">
-                and  bom_spec_id = #{bomSpecId}
-            </if>
-            <if test="warehouseId != null">
-                and warehouse_id = #{warehouseId}
-            </if>
-            and type = 1
-        </where>
-    </select>
-
     <select id="getInOutDetails" resultType="com.sd.business.entity.in.po.InOutStorageDetails">
         select i.quantity,
-               i.in_unit_price,
-               i.in_sum_price
+        i.in_unit_price,
+        i.in_sum_price
         from in_out_storage_details i
         <where>
             <if test="bomSpecId != null">
@@ -76,10 +61,20 @@
             <if test="departmentId != null">
                 and department_id = #{departmentId}
             </if>
-            and type = 1
-            and quantity > 0
         </where>
         order by i.create_time
     </select>
 
+    <select id="getQuantityByBomSpecId" resultType="java.math.BigDecimal" parameterType="java.lang.Long">
+        SELECT
+	        sum(quantity)
+        FROM
+	        in_out_storage_details
+        WHERE
+            warehouse_id = #{warehouseId}
+        AND
+	        bom_spec_id = #{bomSpecId}
+    </select>
+
+
 </mapper>