|
@@ -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)
|
|
|
+
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
}
|