| | |
| | | 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() { |
| | | } |
| | |
| | | } 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"); |
| | |
| | | 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); |
| | |
| | | result.add(dto); |
| | | } |
| | | } |
| | | result.sort(Comparator.comparing(TemplateInspectionItemDto::getTemplateRowIndex)); |
| | | return result; |
| | | } |
| | | |
| | |
| | | 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")); |
| | | } |
| | | } |