Files
klp-oa/scaner/src/main/java/org/example/ScanDataUtil.java
2025-07-31 14:44:10 +08:00

34 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package org.example;
import org.json.JSONObject;
import java.math.BigDecimal;
public class ScanDataUtil {
/**
* 解析扫码内容并构造请求体
* @param scanStr 扫码内容格式入库单id_仓库id_物料id_数量
* @param itemType 物品类型
* @param itemId 物品ID
* @param unit 单位
* @param remark 备注(可选)
* @param fromWarehouseId 源库位ID可选
* @param recordType 记录类型0/1
* @return JSON字符串
*/
public static String buildRequestJson(String scanStr, String itemType, String remark, Long fromWarehouseId, Integer recordType) {
String[] parts = scanStr.split("_");
if (parts.length != 4) throw new IllegalArgumentException("扫码内容格式不正确应为入库单id_仓库id_物料id_数量");
JSONObject obj = new JSONObject();
obj.put("stockIoId", Long.valueOf(parts[0]));
obj.put("warehouseId", Long.valueOf(parts[1]));
obj.put("itemId", Long.valueOf(parts[2]));
obj.put("quantity", new BigDecimal(parts[3]));
obj.put("itemType", itemType);
obj.put("unit", 'g'); // 单位为null
obj.put("batchNo", 100); // 批次号为null
obj.put("remark", remark);
obj.put("fromWarehouseId", fromWarehouseId);
obj.put("recordType", recordType);
return obj.toString();
}
}