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"));
|
}
|
}
|