xiaoyi
4 天以前 05ab6e3f69a5e73624b2573db043f5923e8f7d8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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);
    }
 
}