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