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
78
79
80
81
package com.ruoyi.basic.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basic.dto.TemplateInspectionItemDto;
import com.ruoyi.common.exception.base.BaseException;
import org.springframework.util.StringUtils;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
/** 解析 Luckysheet 原始记录模板中的“检验项”标记。 */
public final class StandardTemplateInspectionItemParser {
    private static final String INSPECTION_ITEM_MARK = "检验项";
 
    private StandardTemplateInspectionItemParser() {
    }
 
    public static List<TemplateInspectionItemDto> parse(Integer templateId, Integer templateSort, String thing) {
        if (!StringUtils.hasText(thing)) {
            throw new BaseException("原始记录模板内容不能为空");
        }
        JSONArray sheets;
        try {
            JSONObject root = JSON.parseObject(thing);
            sheets = root == null ? null : root.getJSONArray("data");
            if (sheets == null) {
                sheets = JSON.parseArray(thing);
            }
        } catch (Exception e) {
            throw new BaseException("原始记录模板格式错误");
        }
        List<TemplateInspectionItemDto> result = new ArrayList<>();
        Set<Integer> rows = new HashSet<>();
        if (sheets == null) {
            throw new BaseException("原始记录模板格式错误");
        }
        for (Object sheetObject : sheets) {
            JSONObject sheet = (JSONObject) sheetObject;
            JSONArray cells = sheet.getJSONArray("celldata");
            if (cells == null) {
                continue;
            }
            for (Object cellObject : cells) {
                JSONObject cell = (JSONObject) cellObject;
                JSONObject value = cell.getJSONObject("v");
                if (value == null || !isInspectionItem(value)) {
                    continue;
                }
                Integer row = cell.getInteger("r");
                String item = value.getString("v");
                if (!StringUtils.hasText(item)) {
                    item = value.getString("m");
                }
                if (row == null || !StringUtils.hasText(item)) {
                    throw new BaseException("“检验项”标记所在单元格必须填写检验项名称");
                }
                if (!rows.add(row)) {
                    throw new BaseException("同一模板行只能配置一个“检验项”标记");
                }
                TemplateInspectionItemDto dto = new TemplateInspectionItemDto();
                dto.setTemplateId(templateId);
                dto.setTemplateSort(templateSort);
                dto.setTemplateRowIndex(row);
                dto.setInspectionItem(item.trim());
                result.add(dto);
            }
        }
        result.sort(Comparator.comparing(TemplateInspectionItemDto::getTemplateRowIndex));
        return result;
    }
 
    private static boolean isInspectionItem(JSONObject value) {
        JSONObject ps = value.getJSONObject("ps");
        return ps != null && INSPECTION_ITEM_MARK.equals(ps.getString("value"));
    }
}