package com.trafficaudit.dataimport.service;
|
|
import com.trafficaudit.dataimport.entity.VehicleTrackMileage;
|
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.CellType;
|
import org.apache.poi.ss.usermodel.DataFormatter;
|
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.HashSet;
|
import java.util.LinkedHashMap;
|
import java.util.LinkedHashSet;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
|
/**
|
* Parses city-level third-party mileage workbooks.
|
*
|
* <p>The city files are vehicle-by-day matrices. The sheet name is the
|
* enterprise alias, while the organization column may contain departments or
|
* branches, so the sheet name is the reliable grouping key.</p>
|
*/
|
final class CityTrackMileageWorkbookParser {
|
private static final DataFormatter FORMATTER = new DataFormatter();
|
private static final String ORGANIZATION = "\u7ec4\u7ec7\u540d\u79f0";
|
private static final String PLATE = "\u8f66\u724c\u53f7\u7801";
|
private static final String TOTAL_MILEAGE = "\u5408\u8ba1(\u5355\u4f4d:\u516c\u91cc)";
|
private static final String TOTAL = "\u5408\u8ba1";
|
private static final String SUMMARY_SHEET = "\u91cc\u7a0b\u6708\u62a5";
|
|
/*
|
* Xiaogan files use short sheet aliases. The map is intentionally
|
* explicit so that a page with several branch names is never guessed by
|
* an organization row. Other cities can use full sheet names and the
|
* generic exact/containment resolver below.
|
*/
|
private static final Map<String, String> XIAOGAN_ALIASES = new HashMap<>();
|
|
static {
|
XIAOGAN_ALIASES.put("\u4e50\u987a", "\u5b89\u9646\u5e02\u4e50\u987a\u5efa\u6750\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u957f\u7f28", "\u5927\u609f\u957f\u7f28\u8fd0\u8f93\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u5fb7\u90a6", "\u5fb7\u90a6\uff08\u6e56\u5317\uff09\u8fd0\u8f93\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u5b89\u5fc3", "\u6c49\u5ddd\u5e02\u5b89\u5fc3\u7269\u6d41\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u4e30\u6e90", "\u6e56\u5317\u4e30\u6e90\u7269\u6d41\u4f9b\u5e94\u94fe\u7ba1\u7406\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u6d77\u8fd0", "\u6e56\u5317\u6d77\u8fd0\u7269\u6d41\u8fd0\u8f93\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u5408\u5174", "\u6e56\u5317\u5408\u5174\u5305\u88c5\u5370\u5237\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u534e\u8000", "\u6e56\u5317\u534e\u8000\u660c\u987a\u7269\u6d41\u6709\u9650\u8d23\u4efb\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u6c47\u4e5d\u5bf0", "\u6e56\u5317\u6c47\u4e5d\u5bf0\u4f9b\u5e94\u94fe\u7269\u6d41\u7ba1\u7406\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u5609\u9633", "\u6e56\u5317\u5609\u9633\u7269\u6d41\u8fd0\u8f93\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u6377\u5b89", "\u6e56\u5317\u6377\u5b89\u8fd0\u8f93\u6709\u9650\u8d23\u4efb\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u9756\u680b", "\u6e56\u5317\u9756\u680b\u571f\u77f3\u65b9\u5de5\u7a0b\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u7533\u745e", "\u6e56\u5317\u7701\u7533\u745e\u8fd0\u8f93\u670d\u52a1\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u901a\u665f", "\u6e56\u5317\u901a\u665f\u7269\u6d41\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u4e91\u8d77", "\u6e56\u5317\u4e91\u8d77\u7269\u6d41\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u5b8f\u529b", "\u5b5d\u611f\u5b8f\u529b\u6c7d\u8f66\u8fd0\u8f93\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u742a\u665f", "\u5b5d\u611f\u742a\u665f\u571f\u77f3\u65b9\u5de5\u7a0b\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u8fd0\u901a", "\u5b5d\u611f\u8fd0\u901a\u5546\u8d38\u7269\u6d41\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u4f17\u5408", "\u5b5d\u611f\u4f17\u5408\u77ff\u4e1a\u7269\u6d41\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u5954\u9a70", "\u5e94\u57ce\u5e02\u5954\u9a70\u7269\u6d41\u8fd0\u8f93\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u987a\u51ef", "\u4e91\u68a6\u987a\u51ef\u7269\u6d41\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u987a\u6cfd", "\u4e91\u68a6\u53bf\u987a\u6cfd\u5b50\u6587\u7269\u6d41\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u4e2d\u71c3", "\u4e2d\u71c3\u5b8f\u9014\u7269\u6d41\u6709\u9650\u516c\u53f8");
|
XIAOGAN_ALIASES.put("\u91d1\u51e4\u51f0", "\u91d1\u51e4\u51f0\u7eb8\u4e1a\uff08\u5b5d\u611f\uff09\u6709\u9650\u516c\u53f8");
|
}
|
|
private CityTrackMileageWorkbookParser() {
|
}
|
|
static List<VehicleTrackMileage> parse(List<Workbook> workbooks, String period,
|
String cityName, Set<String> standardEnterpriseNames) {
|
Map<String, Aggregate> merged = new LinkedHashMap<>();
|
Map<String, String> standardByKey = new HashMap<>();
|
for (String name : standardEnterpriseNames) {
|
if (name != null && !name.trim().isEmpty()) {
|
standardByKey.put(normalizeName(name), name);
|
}
|
}
|
|
for (Workbook workbook : workbooks) {
|
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
|
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
Sheet sheet = workbook.getSheetAt(i);
|
Header header = findHeader(sheet, evaluator);
|
if (header == null) continue;
|
if (isSummarySheet(sheet, header, evaluator)) continue;
|
|
String pageKey = normalizeName(sheet.getSheetName());
|
String enterpriseName = resolveSheetEnterprise(sheet, header, evaluator, pageKey, standardByKey);
|
Aggregate aggregate = merged.computeIfAbsent(enterpriseName, k -> new Aggregate());
|
readSheet(sheet, header, evaluator, aggregate, pageKey);
|
}
|
}
|
|
if (merged.isEmpty()) {
|
throw new IllegalArgumentException("City track mileage workbook contains no enterprise data");
|
}
|
|
List<VehicleTrackMileage> rows = new ArrayList<>();
|
for (Map.Entry<String, Aggregate> entry : merged.entrySet()) {
|
Aggregate aggregate = entry.getValue();
|
if (aggregate.plates.isEmpty()) continue;
|
VehicleTrackMileage entity = new VehicleTrackMileage();
|
entity.setReportPeriod(period);
|
entity.setEnterpriseName(entry.getKey());
|
entity.setEnterpriseCategory(standardByKey.containsKey(normalizeName(entry.getKey()))
|
? VehicleTrackMileage.CATEGORY_ABOVE_SCALE
|
: VehicleTrackMileage.CATEGORY_UNREGISTERED);
|
entity.setDataSource(VehicleTrackMileage.SOURCE_CITY_THIRD_PARTY);
|
entity.setCityName(cityName);
|
entity.setMonthlyMileage(aggregate.mileage);
|
entity.setTrackedVehicles(aggregate.positivePlates.size());
|
rows.add(entity);
|
}
|
if (rows.isEmpty()) {
|
throw new IllegalArgumentException("City track mileage workbook contains no vehicle data");
|
}
|
return rows;
|
}
|
|
private static void readSheet(Sheet sheet, Header header, FormulaEvaluator evaluator,
|
Aggregate aggregate, String pageKey) {
|
for (int r = header.rowIndex + 1; r <= sheet.getLastRowNum(); r++) {
|
Row row = sheet.getRow(r);
|
if (row == null) continue;
|
String org = text(row, header.organizationColumn, evaluator);
|
String plate = text(row, header.plateColumn, evaluator);
|
if (org == null || plate == null || TOTAL.equals(org.trim())) continue;
|
plate = plate.trim();
|
if (plate.isEmpty()) continue;
|
|
double mileage = number(row, header.mileageColumn, evaluator, sheet.getSheetName());
|
aggregate.mileage += mileage;
|
aggregate.plates.add(plate);
|
if (mileage > 0) aggregate.positivePlates.add(plate);
|
}
|
}
|
|
private static boolean isSummarySheet(Sheet sheet, Header header, FormulaEvaluator evaluator) {
|
String name = sheet.getSheetName() == null ? "" : sheet.getSheetName().trim();
|
if (name.contains("\u6c47\u603b")) return true;
|
if (!name.contains(SUMMARY_SHEET)) return false;
|
|
Set<String> organizations = organizations(sheet, header, evaluator);
|
// The first Xiaogan sheet is a province-like summary matrix with dozens
|
// of organization labels. The separate Changying file has one
|
// organization in a sheet also named "里程月报" and must be imported.
|
return organizations.size() > 5;
|
}
|
|
private static String resolveEnterpriseName(String pageKey, Map<String, String> standardByKey) {
|
String alias = XIAOGAN_ALIASES.get(pageKey);
|
if (alias != null) return alias;
|
|
String exact = standardByKey.get(pageKey);
|
if (exact != null) return exact;
|
|
List<String> candidates = new ArrayList<>();
|
for (Map.Entry<String, String> entry : standardByKey.entrySet()) {
|
if (entry.getKey().contains(pageKey) || pageKey.contains(entry.getKey())) {
|
candidates.add(entry.getValue());
|
}
|
}
|
if (candidates.size() == 1) return candidates.get(0);
|
if (candidates.size() > 1) {
|
throw new IllegalArgumentException("Ambiguous city track enterprise for sheet: " + pageKey);
|
}
|
|
if (pageKey.endsWith("\u516c\u53f8") || pageKey.endsWith("\u4e2d\u5fc3")) {
|
return pageKey;
|
}
|
throw new IllegalArgumentException("Cannot resolve city track enterprise for sheet: " + pageKey);
|
}
|
|
private static String resolveSheetEnterprise(Sheet sheet, Header header, FormulaEvaluator evaluator,
|
String pageKey, Map<String, String> standardByKey) {
|
if (pageKey.contains(SUMMARY_SHEET)) {
|
Set<String> organizations = organizations(sheet, header, evaluator);
|
if (organizations.size() == 1) {
|
return resolveEnterpriseName(organizations.iterator().next(), standardByKey);
|
}
|
throw new IllegalArgumentException("City track summary sheet requires exactly one organization: " + pageKey);
|
}
|
return resolveEnterpriseName(pageKey, standardByKey);
|
}
|
|
private static Set<String> organizations(Sheet sheet, Header header, FormulaEvaluator evaluator) {
|
Set<String> organizations = new LinkedHashSet<>();
|
for (int r = header.rowIndex + 1; r <= sheet.getLastRowNum(); r++) {
|
Row row = sheet.getRow(r);
|
if (row == null) continue;
|
String org = text(row, header.organizationColumn, evaluator);
|
if (org != null && !TOTAL.equals(org.trim())) organizations.add(normalizeName(org));
|
}
|
return organizations;
|
}
|
|
private static Header findHeader(Sheet sheet, FormulaEvaluator evaluator) {
|
int last = Math.min(sheet.getLastRowNum(), 30);
|
for (int r = 0; r <= last; r++) {
|
Row row = sheet.getRow(r);
|
if (row == null) continue;
|
Map<String, Integer> columns = new HashMap<>();
|
for (Cell cell : row) {
|
String key = normalizeHeader(FORMATTER.formatCellValue(cell, evaluator));
|
if (!key.isEmpty() && !columns.containsKey(key)) {
|
columns.put(key, cell.getColumnIndex());
|
}
|
}
|
Integer organizationColumn = columns.get(ORGANIZATION);
|
Integer plateColumn = columns.get(PLATE);
|
Integer mileageColumn = columns.get(TOTAL_MILEAGE);
|
if (mileageColumn == null) mileageColumn = columns.get(TOTAL);
|
if (organizationColumn != null && plateColumn != null && mileageColumn != null) {
|
return new Header(r, organizationColumn, plateColumn, mileageColumn);
|
}
|
}
|
return null;
|
}
|
|
private static String text(Row row, Integer column, FormulaEvaluator evaluator) {
|
if (column == null) return null;
|
Cell cell = row.getCell(column);
|
if (cell == null) return null;
|
String value = FORMATTER.formatCellValue(cell, evaluator).trim();
|
return value.isEmpty() ? null : value;
|
}
|
|
private static double number(Row row, Integer column, FormulaEvaluator evaluator, String sheetName) {
|
Cell cell = row == null || column == null ? null : row.getCell(column);
|
if (cell == null || cell.getCellType() == CellType.BLANK) return 0.0;
|
if (cell.getCellType() == CellType.NUMERIC) return cell.getNumericCellValue();
|
if (cell.getCellType() == CellType.FORMULA && cell.getCachedFormulaResultType() == CellType.NUMERIC) {
|
return cell.getNumericCellValue();
|
}
|
String value = FORMATTER.formatCellValue(cell, evaluator).trim();
|
if (value.isEmpty()) return 0.0;
|
try {
|
return Double.parseDouble(value.replace(",", ""));
|
} catch (NumberFormatException e) {
|
throw new IllegalArgumentException("Invalid mileage in city track sheet=" + sheetName
|
+ ", row=" + (row.getRowNum() + 1) + ", value=" + value);
|
}
|
}
|
|
private static String normalizeName(String value) {
|
return normalizeHeader(value).replace("(", "").replace(")", "");
|
}
|
|
private static String normalizeHeader(String value) {
|
if (value == null) return "";
|
return value.replace('\u3000', ' ')
|
.replace('\uff08', '(')
|
.replace('\uff09', ')')
|
.replace('\uff1a', ':')
|
.replaceAll("\\s+", "")
|
.trim();
|
}
|
|
private static final class Aggregate {
|
private double mileage;
|
private final Set<String> plates = new LinkedHashSet<>();
|
private final Set<String> positivePlates = new LinkedHashSet<>();
|
}
|
|
private static final class Header {
|
private final int rowIndex;
|
private final int organizationColumn;
|
private final int plateColumn;
|
private final int mileageColumn;
|
|
private Header(int rowIndex, int organizationColumn, int plateColumn, int mileageColumn) {
|
this.rowIndex = rowIndex;
|
this.organizationColumn = organizationColumn;
|
this.plateColumn = plateColumn;
|
this.mileageColumn = mileageColumn;
|
}
|
}
|
}
|