package cn.iocoder.yudao.module.mes.util;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.json.JSONArray;
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* MES 工序产出物料配置解析工具
|
*
|
* <p>mes_pro_route_process.output_item_id / mes_pro_work_order_process.output_item_id 字段
|
* 同时兼容两种存储格式:
|
* <ul>
|
* <li>新格式(含归属产品):[{"itemId":231,"productId":231}]</li>
|
* <li>旧格式(纯物料编号,逗号分隔):231,232</li>
|
* </ul>
|
*
|
* @author 超级管理员
|
*/
|
public class MesOutputItemUtils {
|
|
private MesOutputItemUtils() {
|
}
|
|
/**
|
* 解析字段中的全部产出物料编号(不含归属产品)
|
*/
|
public static List<Long> parseItemIds(String raw) {
|
List<Long> ids = new ArrayList<>();
|
if (raw == null || raw.isBlank()) {
|
return ids;
|
}
|
String text = raw.trim();
|
if (text.startsWith("[")) {
|
try {
|
JSONArray array = JSONUtil.parseArray(text);
|
for (Object element : array) {
|
if (!(element instanceof JSONObject)) {
|
continue;
|
}
|
Long itemId = ((JSONObject) element).getLong("itemId");
|
if (itemId != null) {
|
ids.add(itemId);
|
}
|
}
|
return ids;
|
} catch (Exception ignored) {
|
// 解析失败按逗号分隔兜底
|
ids.clear();
|
}
|
}
|
for (String s : text.split(",")) {
|
String t = s.trim();
|
if (t.isEmpty()) {
|
continue;
|
}
|
try {
|
ids.add(Long.valueOf(t));
|
} catch (NumberFormatException ignored) {
|
// 跳过非法值
|
}
|
}
|
return ids;
|
}
|
|
/**
|
* 取第一个产出物料编号,解析不到返回 null
|
*/
|
public static Long firstItemId(String raw) {
|
List<Long> ids = parseItemIds(raw);
|
return CollUtil.isEmpty(ids) ? null : ids.get(0);
|
}
|
|
}
|