|
|
@@ -176,12 +176,21 @@ public class InventoryService extends GenericProvider {
|
|
|
errorMsg = "数量格式错误:" + shuLiangStr;
|
|
|
break;
|
|
|
}
|
|
|
- if (shuLiang <= 0) {
|
|
|
+ if (shuLiang == 0) {
|
|
|
checkFlag = false;
|
|
|
- errorMsg = "数量必须大于0";
|
|
|
+ errorMsg = "数量不能为0";
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
+ // 标记是否为负数及绝对值,供后续分支使用
|
|
|
+ boolean isNegative = shuLiang < 0;
|
|
|
+ int absShuLiang = Math.abs(shuLiang);
|
|
|
+
|
|
|
+ // 负数在 start/back 阶段不涉及预扣,静默跳过本条记录
|
|
|
+ if (isNegative && ("start".equals(operate) || "back".equals(operate))) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
// 2. 查询库存
|
|
|
String inventoryId = StrUtil.str(detail.get("inventory_id"));
|
|
|
String batchNum = StrUtil.str(detail.get("pi_hao_"));
|
|
|
@@ -208,14 +217,13 @@ public class InventoryService extends GenericProvider {
|
|
|
|
|
|
// 手动判断空值:检查是否为 null 或者 去除空格后长度为0
|
|
|
if (rawRemain == null || rawRemain.trim().isEmpty()) {
|
|
|
- // 情况1:为空时,直接赋值为当前库存数量 quantity
|
|
|
+ // 情况1:为空时,直接赋值为当前数量(负数时自动为负值字符串,代数语义正确)
|
|
|
remainAmount = shuLiangStr;
|
|
|
} else {
|
|
|
- // 情况2:不为空时,解析为整数,加上当前库存数量 quantity
|
|
|
+ // 情况2:不为空时,解析为整数,加上当前数量(负数时代数运算自动做减法)
|
|
|
try {
|
|
|
- // 去除首尾空格并转为整数
|
|
|
int currentVal = Integer.parseInt(rawRemain.trim());
|
|
|
- int newVal = currentVal + Integer.parseInt(shuLiangStr);
|
|
|
+ int newVal = currentVal + shuLiang;
|
|
|
remainAmount = String.valueOf(newVal);
|
|
|
} catch (NumberFormatException e) {
|
|
|
remainAmount = shuLiangStr;
|
|
|
@@ -238,27 +246,34 @@ public class InventoryService extends GenericProvider {
|
|
|
|
|
|
// 3. 逻辑判断
|
|
|
if ("start".equals(operate)) {
|
|
|
- if ((quantity - withhold) < shuLiang) {
|
|
|
+ if ((quantity - withhold) < absShuLiang) {
|
|
|
checkFlag = false;
|
|
|
- errorMsg = name + " 可用库存不足 (可用:" + (quantity - withhold) + ", 需:" + shuLiang + ")";
|
|
|
+ errorMsg = name + " 可用库存不足 (可用:" + (quantity - withhold) + ", 需:" + absShuLiang + ")";
|
|
|
break;
|
|
|
}
|
|
|
- newWithhold = withhold + shuLiang;
|
|
|
+ newWithhold = withhold + absShuLiang;
|
|
|
} else if ("back".equals(operate)) {
|
|
|
- if (withhold < shuLiang) {
|
|
|
+ if (withhold < absShuLiang) {
|
|
|
checkFlag = false;
|
|
|
- errorMsg = name + " 预扣数量不足 (预扣:" + withhold + ", 需:" + shuLiang + ")";
|
|
|
+ errorMsg = name + " 预扣数量不足 (预扣:" + withhold + ", 需:" + absShuLiang + ")";
|
|
|
break;
|
|
|
}
|
|
|
- newWithhold = withhold - shuLiang;
|
|
|
+ newWithhold = withhold - absShuLiang;
|
|
|
} else if ("end".equals(operate)) {
|
|
|
- if (quantity < shuLiang || withhold < shuLiang) {
|
|
|
- checkFlag = false;
|
|
|
- errorMsg = name + " 库存或预扣不足 (库存:" + quantity + ", 预扣:" + withhold + ", 需:" + shuLiang + ")";
|
|
|
- break;
|
|
|
+ if (isNegative) {
|
|
|
+ // 负数 end:直接加库存,预扣不变,无需校验
|
|
|
+ newQuantity = quantity + absShuLiang;
|
|
|
+ newWithhold = withhold;
|
|
|
+ } else {
|
|
|
+ // 正数 end:
|
|
|
+ if (quantity < absShuLiang || withhold < absShuLiang) {
|
|
|
+ checkFlag = false;
|
|
|
+ errorMsg = name + " 库存或预扣不足 (库存:" + quantity + ", 预扣:" + withhold + ", 需:" + absShuLiang + ")";
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ newQuantity = quantity - absShuLiang;
|
|
|
+ newWithhold = withhold - absShuLiang;
|
|
|
}
|
|
|
- newQuantity = quantity - shuLiang;
|
|
|
- newWithhold = withhold - shuLiang;
|
|
|
isEnd = true;
|
|
|
} else {
|
|
|
checkFlag = false;
|
|
|
@@ -486,30 +501,23 @@ public class InventoryService extends GenericProvider {
|
|
|
if ("y".equals(deleteRecord) && retVal != null && retVal.startsWith("-1^")) {
|
|
|
logger.warn("业务校验失败且开启自动清理模式 (deleteRecord=y)。开始删除入库记录主子表脏数据,ID: {}", id);
|
|
|
try {
|
|
|
- // 调用删除方法
|
|
|
orderService.deleteErrorRecord(id);
|
|
|
-
|
|
|
- // 可选:在返回信息中追加提示,告知用户已执行清理
|
|
|
retVal = retVal + " (系统已自动撤销并删除无效申请记录)";
|
|
|
-
|
|
|
} catch (Exception e) {
|
|
|
logger.error("自动清理失败! ID: {}, 错误: {}", id, e.getMessage(), e);
|
|
|
// 如果清理本身也报错,追加警告信息到返回值,但不中断流程(因为业务本来就失败了)
|
|
|
retVal = retVal + " (警告:自动清理执行异常,请手动检查数据!)" ;
|
|
|
- // 如果希望清理失败时向上抛出异常,可以取消下面这行的注释
|
|
|
- // throw new Exception("业务失败且自动清理也失败", e);
|
|
|
}
|
|
|
}
|
|
|
return retVal;
|
|
|
}
|
|
|
|
|
|
- // 辅助方法:安全转 Integer
|
|
|
+ // 安全转 Integer
|
|
|
private int parseInteger(Object obj) {
|
|
|
if (obj == null) {
|
|
|
return 0;
|
|
|
}
|
|
|
- String s = String.valueOf(obj).trim(); // 使用原生 String.valueOf
|
|
|
- // 原生判断空
|
|
|
+ String s = String.valueOf(obj).trim();
|
|
|
if (s == null || s.length() == 0) {
|
|
|
return 0;
|
|
|
}
|