|
@@ -58,7 +58,11 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -1119,18 +1123,33 @@ public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements
|
|
|
|
|
|
//操作普通库存
|
|
|
if (stockType.equals(StockType.QUANTITY)) {
|
|
|
- String setSql = inOutType != InOutType.SET ? "quantity = quantity %s %s" : "quantity %s %s";
|
|
|
+ String setQuantitySqlTemplate = inOutType != InOutType.SET ? "quantity = quantity %s %s" : "quantity %s %s";
|
|
|
+ String setQuantitySql = String.format(setQuantitySqlTemplate, opStr, inOutBo.getQuantity());
|
|
|
boolean update = update(q -> q
|
|
|
.eq(Stock::getProductId, inOutBo.getProductId())
|
|
|
.eq(Stock::getWarehouseId, warehouseId)
|
|
|
- .setSql(String.format(setSql, opStr, inOutBo.getQuantity()))
|
|
|
+ .setSql(setQuantitySql)
|
|
|
.apply(inOutType.equals(InOutType.OUT), "quantity - {0} >= 0", inOutBo.getQuantity())
|
|
|
);
|
|
|
+
|
|
|
if (!update) {
|
|
|
- if (inOutType.equals(InOutType.OUT)) {
|
|
|
- if (oldStock.getQuantity().subtract(inOutBo.getQuantity()).compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+ if (inOutType.equals(InOutType.OUT) && oldStock.getQuantity().subtract(inOutBo.getQuantity()).compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+
|
|
|
+ // 尝试自动组合
|
|
|
+ automaticCombination(productInfo, oldStock.getQuantity(), inOutBo.getQuantity(), warehouseId);
|
|
|
+
|
|
|
+ // 再次尝试修改库存
|
|
|
+ update = update(q -> q
|
|
|
+ .eq(Stock::getProductId, inOutBo.getProductId())
|
|
|
+ .eq(Stock::getWarehouseId, warehouseId)
|
|
|
+ .setSql(setQuantitySql)
|
|
|
+ );
|
|
|
+
|
|
|
+ // 修改失败则抛出异常
|
|
|
+ if (!update) {
|
|
|
throw new ServiceException("产品库存不足无法出库:", productInfo.getName());
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
throw new ServiceException("库存操作失败:" + productInfo.getName());
|
|
|
}
|
|
@@ -1186,4 +1205,43 @@ public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void automaticCombination(ProductInfo productInfo, BigDecimal inventoryQuantity, BigDecimal outboundQuantity, Long warehouseId) {
|
|
|
+
|
|
|
+ //库存不足判断是否是组合
|
|
|
+ String victoriatouristJson = productInfo.getVictoriatouristJson();
|
|
|
+
|
|
|
+ JSONObject json = ObjectUtil.isNotEmpty(victoriatouristJson) ? JSONObject.parseObject(victoriatouristJson) : new JSONObject();
|
|
|
+
|
|
|
+ Integer combination = json.getInteger("combination");
|
|
|
+
|
|
|
+ if (combination != 1) {
|
|
|
+ //不是组合直接报错
|
|
|
+ throw new ServiceException("产品库存不足无法出库:", productInfo.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算差多少 自动组合差的那部分
|
|
|
+ BigDecimal differenceQuantity = outboundQuantity.subtract(inventoryQuantity);
|
|
|
+
|
|
|
+ //获取产品组合信息
|
|
|
+ JSONArray productCombinationListArr = json.getJSONArray("productCombinationList");
|
|
|
+ Assert.notEmpty(productCombinationListArr, "产品组合信息为空");
|
|
|
+ List<JSONObject> productCombinationList = productCombinationListArr.toJavaList(JSONObject.class);
|
|
|
+ List<Long> linkProductIds = productCombinationList.stream().map(item -> item.getLong("linkProductId")).collect(Collectors.toList());
|
|
|
+ Map<Long, Stock> combinationStockMap = this.mapKEntity(Stock::getProductId, q -> q
|
|
|
+ .eq(Stock::getWarehouseId, warehouseId)
|
|
|
+ .in(Stock::getProductId, linkProductIds));
|
|
|
+
|
|
|
+ //检查是否满足自动组合条件
|
|
|
+ for (JSONObject jsonObject : productCombinationList) {
|
|
|
+ Stock stockInfo = combinationStockMap.get(jsonObject.getLong("linkProductId"));
|
|
|
+ BigDecimal requiredQuantity = jsonObject.getBigDecimal("linkQuantity").multiply(differenceQuantity);
|
|
|
+ if (ObjectUtil.isEmpty(stockInfo) || stockInfo.getQuantity().compareTo(requiredQuantity) < 0) {
|
|
|
+ throw new ServiceException("该组合产品,子产品库存不足无法自动组合出库:" + productInfo.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建组合信息
|
|
|
+ wmsService.autoCombination(productInfo.getId(), differenceQuantity, warehouseId);
|
|
|
+ }
|
|
|
+
|
|
|
}
|