|
@@ -1,8 +1,6 @@
|
|
|
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;
|
|
@@ -13,26 +11,23 @@ import com.sd.business.entity.in.po.InOutStorageDetails;
|
|
|
import com.sd.business.entity.inventory.bo.InOutFun;
|
|
|
import com.sd.business.entity.inventory.dto.InventorySelectDto;
|
|
|
import com.sd.business.entity.inventory.dto.QuantityByWarehouseDto;
|
|
|
-import com.sd.business.entity.inventory.po.BalancePriceNum;
|
|
|
import com.sd.business.entity.inventory.po.Inventory;
|
|
|
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;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Function;
|
|
@@ -117,7 +112,7 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
inventoryList.add(inventory);
|
|
|
map.put(bomSpecId, inventory);
|
|
|
}
|
|
|
- List<InOutStorageDetails> detailsList = getDetailsList(warehouseId, bomSpecIdList);
|
|
|
+ List<InOutStorageDetails> detailsList = getInOutStorageDetailsMap(warehouseId, bomSpecIdList);
|
|
|
if (CollUtil.isEmpty(detailsList)){
|
|
|
for (InOutStorageDetails details : detailsList) {
|
|
|
details.setInSumPrice(BigDecimal.ZERO);
|
|
@@ -142,22 +137,28 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
|
|
|
@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);
|
|
|
+
|
|
|
+ // 获取bom规格id和结存明细列表map
|
|
|
+ Map<Long, List<InOutStorageDetails>> inOutStorageDetailsMap = getInOutStorageDetailsMap(warehouseId, bomSpecIdList);
|
|
|
+
|
|
|
+ // 存储需要修改的入库记录
|
|
|
+ List<InOutStorageDetails> updateList = new ArrayList<>();
|
|
|
+
|
|
|
// 查询库存中是否存在bom
|
|
|
for (Inventory inventory : inventoryList) {
|
|
|
+
|
|
|
Long bomSpecId = inventory.getBomSpecId();
|
|
|
+
|
|
|
if (!bomSpecIdList.contains(bomSpecId)) {
|
|
|
BomSpec bomSpec = bomSpecService.getById(bomSpecId);
|
|
|
if (bomSpec == null) {
|
|
@@ -165,111 +166,68 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
}
|
|
|
throw new ServiceException("出库失败,品名为:" + bomSpec.getName() + "的bom库存不足");
|
|
|
}
|
|
|
- }
|
|
|
- //根据仓库id和规格id获取出入详细表数据
|
|
|
- List<InOutStorageDetails> detailsList = getDetailsList(warehouseId, bomSpecIdList);
|
|
|
-
|
|
|
- //将数据为0的记录过滤掉
|
|
|
- //detailsList = detailsList.stream().filter(q -> q.getQuantity().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
|
|
|
- // 根据条件过滤detailsList,得到detailsList中details.getQuantity()不为0的对象列表
|
|
|
- detailsList = detailsList.stream()
|
|
|
- .filter(details -> details.getQuantity().compareTo(BigDecimal.ZERO) > 0)
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- if (CollUtil.isEmpty(detailsList)) {
|
|
|
- throw new ServiceException(departmentId + "和" + warehouseId + "的数据为null");
|
|
|
- }
|
|
|
- detailsHashMap = detailsList.stream()
|
|
|
- .collect(Collectors.groupingBy(InOutStorageDetails::getBomSpecId));
|
|
|
|
|
|
- //存储需要修改的入库记录
|
|
|
- List<InOutStorageDetails> updateList = new ArrayList<>();
|
|
|
- //存储这次出库的金额
|
|
|
- Map<Long, BigDecimal> outPriceMap = new HashMap<>();
|
|
|
+ List<InOutStorageDetails> outStorageDetails = inOutStorageDetailsMap.get(bomSpecId);
|
|
|
|
|
|
- for (Long bomSpecId : bomSpecIdList) {
|
|
|
- List<InOutStorageDetails> outStorageDetails = detailsHashMap.get(bomSpecId);
|
|
|
+ // 循环次数
|
|
|
int j = 0;
|
|
|
- BigDecimal num;
|
|
|
- BigDecimal quantity;
|
|
|
+
|
|
|
+ // 需要出库数量
|
|
|
+ BigDecimal quantity = map.get(bomSpecId);
|
|
|
+
|
|
|
do {
|
|
|
- //获取这条记录剩余的数量
|
|
|
- InOutStorageDetails details = outStorageDetails.get(j);
|
|
|
|
|
|
- // 获取数据库中整体数量与出库数量对比
|
|
|
- BigDecimal allQuantity = baseMapper.getQuantityByBomSpecId(details.getBomSpecId(), details.getWarehouseId());
|
|
|
- if (allQuantity.compareTo(quantityMap.get(details.getBomSpecId())) < 0) {
|
|
|
- throw new ServiceException(details.getBomSpecId() + "这种规格的库存不够,库存数量为" + details.getQuantity());
|
|
|
+ if (outStorageDetails.size() == j) {
|
|
|
+ throw new ServiceException("数据异常,无法计算库存单价");
|
|
|
}
|
|
|
|
|
|
- //如果该记录为最后一条记录
|
|
|
- if (j == outStorageDetails.size() - 1) {
|
|
|
- BigDecimal naxtQuantity = outStorageDetails.get(j).getQuantity();
|
|
|
- //判断库存是否够扣减.不够则抛异常
|
|
|
- if (naxtQuantity.compareTo(map.get(details.getBomSpecId())) < 0) {
|
|
|
- throw new ServiceException(details.getBomSpecId() + "这种规格的库存不够,库存数量为" + details.getQuantity());
|
|
|
- }
|
|
|
+ // 获取这条记录剩余的数量
|
|
|
+ InOutStorageDetails inOutStorageDetails = outStorageDetails.get(j);
|
|
|
+
|
|
|
+ // 单条可出库数量
|
|
|
+ BigDecimal tempQuantity = inOutStorageDetails.getQuantity();
|
|
|
+
|
|
|
+ if (tempQuantity.compareTo(quantity) >= 0) {
|
|
|
+ inOutStorageDetails.setQuantity(tempQuantity.subtract(quantity));
|
|
|
+ quantity = BigDecimal.ZERO;
|
|
|
+ } else {
|
|
|
+ inOutStorageDetails.setQuantity(BigDecimal.ZERO);
|
|
|
+ quantity = tempQuantity.subtract(quantity);
|
|
|
}
|
|
|
- //获取这次一种规格的出库的数量
|
|
|
- quantity = map.get(details.getBomSpecId());
|
|
|
|
|
|
- //获取两次数量直接的差价
|
|
|
- num = details.getQuantity().subtract(quantity);
|
|
|
+ updateList.add(inOutStorageDetails);
|
|
|
|
|
|
- //如果这条记录库存不足,则计算下一条记录
|
|
|
- if (num.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
- addOutPriceMap(outPriceMap, map, details);
|
|
|
+ j++;
|
|
|
|
|
|
- details.setQuantity(BigDecimal.ZERO);
|
|
|
- details.setInSumPrice(BigDecimal.ZERO);
|
|
|
+ } while (quantity.compareTo(BigDecimal.ZERO) > 0);
|
|
|
|
|
|
- updateList.add(details);
|
|
|
- map.put(details.getBomSpecId(), num.abs());
|
|
|
- j++;
|
|
|
- continue;
|
|
|
- }
|
|
|
- //将这次出库的金额存储到map里面
|
|
|
- addOutPriceMap(outPriceMap, map, details);
|
|
|
+ // 库存总数量
|
|
|
+ BigDecimal totalInventory = outStorageDetails.stream()
|
|
|
+ .map(InOutStorageDetails::getQuantity)
|
|
|
+ .filter(item -> item.compareTo(BigDecimal.ZERO) > 0)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
|
- //对数量进行减扣
|
|
|
- //BigDecimal detailsNum = details.getQuantity().subtract(num);
|
|
|
- details.setQuantity(num.abs());
|
|
|
+ // 库存总金额
|
|
|
+ BigDecimal totalInventoryAmount = outStorageDetails.stream()
|
|
|
+ .filter(item -> item.getQuantity().compareTo(BigDecimal.ZERO) > 0)
|
|
|
+ .map(item -> item.getQuantity().multiply(item.getInUnitPrice()))
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
|
- //数量减少了,那金额也得减少
|
|
|
- BigDecimal multiply = num.abs().multiply(details.getInUnitPrice());
|
|
|
- details.setInSumPrice(multiply);
|
|
|
+ // 结存单价
|
|
|
+ BigDecimal balanceUnitPrice = totalInventoryAmount.divide(totalInventory, 2, RoundingMode.HALF_UP);
|
|
|
|
|
|
- //将减扣数量后的对象修改回去
|
|
|
- updateList.add(details);
|
|
|
- j++;
|
|
|
- } while (num.compareTo(BigDecimal.ZERO) < 0 && j < outStorageDetails.size());
|
|
|
+ // 赋值结存单价
|
|
|
+ inventory.setBalanceUnitPrice(balanceUnitPrice);
|
|
|
}
|
|
|
- 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);
|
|
|
- }
|
|
|
+
|
|
|
+ // 修改计算结存单价的出入库明细表
|
|
|
+ inOutStorageDetailsService.updateBatchById(updateList);
|
|
|
+
|
|
|
+ // 修改库存数量以及结存单价
|
|
|
updateBatchById(inventoryList);
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
//将出库金额添加到map
|
|
@@ -301,13 +259,14 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- private List<InOutStorageDetails> getDetailsList(Long warehouseId, List<Long> bomSpecIdList) {
|
|
|
- return inOutStorageDetailsService.list(q -> q
|
|
|
+ private Map<Long, List<InOutStorageDetails>> getInOutStorageDetailsMap(Long warehouseId, List<Long> bomSpecIdList) {
|
|
|
+ List<InOutStorageDetails> list = inOutStorageDetailsService.list(q -> q
|
|
|
.eq(InOutStorageDetails::getWarehouseId, warehouseId)
|
|
|
- .eq(InOutStorageDetails::getDelFlag, 0)
|
|
|
.in(InOutStorageDetails::getBomSpecId, bomSpecIdList)
|
|
|
- .isNotNull(InOutStorageDetails::getQuantity)
|
|
|
- );
|
|
|
- }
|
|
|
+ .gt(InOutStorageDetails::getQuantity, BigDecimal.ZERO)
|
|
|
+ );
|
|
|
+
|
|
|
+ return list.stream().collect(Collectors.groupingBy(InOutStorageDetails::getBomSpecId));
|
|
|
+ }
|
|
|
|
|
|
}
|