gaoluyang
昨天 7787f57521297df847bfd5f2b9af410b3fdac67e
basic-server/src/main/java/com/ruoyi/basic/utils/StandardTemplateInspectionItemParser.java
@@ -13,9 +13,10 @@
import java.util.List;
import java.util.Set;
/** 解析 Luckysheet 原始记录模板中的“检验项”标记。 */
/** 解析 Luckysheet 原始记录模板中的“检验项”;未标记时回退到“导出值”行。 */
public final class StandardTemplateInspectionItemParser {
    private static final String INSPECTION_ITEM_MARK = "检验项";
    private static final String EXPORT_VALUE_MARK = "导出值";
    private StandardTemplateInspectionItemParser() {
    }
@@ -34,11 +35,22 @@
        } catch (Exception e) {
            throw new BaseException("原始记录模板格式错误");
        }
        List<TemplateInspectionItemDto> result = new ArrayList<>();
        Set<Integer> rows = new HashSet<>();
        if (sheets == null) {
            throw new BaseException("原始记录模板格式错误");
        }
        List<TemplateInspectionItemDto> result = parseMarkedCells(sheets, templateId, templateSort,
                INSPECTION_ITEM_MARK);
        if (result.isEmpty()) {
            result = parseMarkedCells(sheets, templateId, templateSort, EXPORT_VALUE_MARK);
        }
        result.sort(Comparator.comparing(TemplateInspectionItemDto::getTemplateRowIndex));
        return result;
    }
    private static List<TemplateInspectionItemDto> parseMarkedCells(JSONArray sheets, Integer templateId,
                                                                     Integer templateSort, String mark) {
        List<TemplateInspectionItemDto> result = new ArrayList<>();
        Set<Integer> rows = new HashSet<>();
        for (Object sheetObject : sheets) {
            JSONObject sheet = (JSONObject) sheetObject;
            JSONArray cells = sheet.getJSONArray("celldata");
@@ -48,16 +60,19 @@
            for (Object cellObject : cells) {
                JSONObject cell = (JSONObject) cellObject;
                JSONObject value = cell.getJSONObject("v");
                if (value == null || !isInspectionItem(value)) {
                if (value == null || !hasMark(value, mark)) {
                    continue;
                }
                Integer row = cell.getInteger("r");
                String item = getCellText(value);
                if (row == null || !StringUtils.hasText(item)) {
                    throw new BaseException("“检验项”标记所在单元格必须填写检验项名称");
                    throw new BaseException("“" + mark + "”标记所在单元格必须填写检验项名称");
                }
                if (!rows.add(row)) {
                    throw new BaseException("同一模板行只能配置一个“检验项”标记");
                    if (EXPORT_VALUE_MARK.equals(mark)) {
                        continue;
                    }
                    throw new BaseException("同一模板行只能配置一个“" + mark + "”标记");
                }
                TemplateInspectionItemDto dto = new TemplateInspectionItemDto();
                dto.setTemplateId(templateId);
@@ -67,7 +82,6 @@
                result.add(dto);
            }
        }
        result.sort(Comparator.comparing(TemplateInspectionItemDto::getTemplateRowIndex));
        return result;
    }
@@ -101,8 +115,8 @@
        return richText.toString();
    }
    private static boolean isInspectionItem(JSONObject value) {
    private static boolean hasMark(JSONObject value, String mark) {
        JSONObject ps = value.getJSONObject("ps");
        return ps != null && INSPECTION_ITEM_MARK.equals(ps.getString("value"));
        return ps != null && mark.equals(ps.getString("value"));
    }
}