|
|
@@ -0,0 +1,475 @@
|
|
|
+package com.jeeplus.assess.powerPrice.service;
|
|
|
+
|
|
|
+import cn.hutool.crypto.digest.DigestUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.jeeplus.assess.powerPrice.domain.MaterialBoweiyunSaveData;
|
|
|
+import com.jeeplus.assess.powerPrice.domain.MaterialBoweiyunPriceRecord;
|
|
|
+import com.jeeplus.assess.powerPrice.mapper.MaterialBoweiyunSaveDataMapper;
|
|
|
+import com.jeeplus.assess.powerPrice.mapper.MaterialBoweiyunPriceRecordMapper;
|
|
|
+import com.jeeplus.assess.powerPrice.service.dto.PowerPriceModeDTO;
|
|
|
+import com.jeeplus.assess.powerPrice.service.dto.PowerPriceOptionsDTO;
|
|
|
+import com.jeeplus.assess.powerPrice.service.dto.PowerPricePointDTO;
|
|
|
+import com.jeeplus.assess.powerPrice.service.dto.PowerPriceQueryDTO;
|
|
|
+import com.jeeplus.common.redis.RedisUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeParseException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.TreeMap;
|
|
|
+import java.util.TreeSet;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 电力设备价格的查询与写入服务。
|
|
|
+ *
|
|
|
+ * 查询侧提供分页列表、走势图和下拉选项;写入侧只做幂等 upsert,
|
|
|
+ * 数据由同步服务(BoweiyunDataSyncService)从 Python 数据文件解析后准备。
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class MaterialBoweiyunPriceRecordService extends ServiceImpl<MaterialBoweiyunPriceRecordMapper, MaterialBoweiyunPriceRecord> {
|
|
|
+
|
|
|
+ /** 下拉选项缓存 key:导入新文件后主动重建,不设过期时间;DTO 加字段时升版本避免读到旧缓存 */
|
|
|
+ private static final String OPTIONS_CACHE_KEY = "power-price:options:v2";
|
|
|
+
|
|
|
+ /** 走势最多返回的点数,防止异常查询把图表撑爆 */
|
|
|
+ private static final int HISTORY_LIMIT = 500;
|
|
|
+
|
|
|
+ /** 单条 upsert SQL 的最大行数,避免一次提交的数据包过大 */
|
|
|
+ private static final int SQL_BATCH_SIZE = 250;
|
|
|
+
|
|
|
+ /** 与 Python 一致的空白归一化;UNICODE_CHARACTER_CLASS 让 \s 也匹配全角空格 */
|
|
|
+ private static final Pattern WHITESPACE = Pattern.compile("\\s+", Pattern.UNICODE_CHARACTER_CLASS);
|
|
|
+
|
|
|
+ /** 页签固定顺序与中文名 */
|
|
|
+ private static final Map<String, String> MODE_NAMES = new LinkedHashMap<>();
|
|
|
+
|
|
|
+ static {
|
|
|
+ MODE_NAMES.put("national", "国网信息价");
|
|
|
+ MODE_NAMES.put("quota", "定额站信息价");
|
|
|
+ MODE_NAMES.put("province", "各省信息价");
|
|
|
+ MODE_NAMES.put("market", "市场价");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private RedisUtils redisUtils;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private MaterialBoweiyunSaveDataMapper saveDataMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询。按导入时间倒序,同一时间导入的记录按主键倒序兜底。
|
|
|
+ *
|
|
|
+ * 两个排序方向都必须是 DESC:与 idx_mbpr_list_time(del_flag, mode_code, create_time, id)
|
|
|
+ * 的最左前缀方向一致时,MySQL 能反向扫索引直接取前 N 行;混用 ASC 会退化成 filesort。
|
|
|
+ */
|
|
|
+ public IPage<MaterialBoweiyunPriceRecord> pageQuery(Page<MaterialBoweiyunPriceRecord> page, PowerPriceQueryDTO query) {
|
|
|
+ QueryWrapper<MaterialBoweiyunPriceRecord> wrapper = new QueryWrapper<>();
|
|
|
+ buildWrapper(query, true, wrapper);
|
|
|
+ wrapper.orderByDesc("create_time").orderByDesc("id");
|
|
|
+ return this.page(page, wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单物料走势:页签与名称必填,规格/单位/地区由前端按当前行带入,全部精确匹配。
|
|
|
+ *
|
|
|
+ * 不按年份/期数过滤,返回该物料的全部期数;市场价的供应商/品牌不参与条件,
|
|
|
+ * 只用于弹窗展示。按导入时间升序返回,最多 500 个点。
|
|
|
+ */
|
|
|
+ public List<PowerPricePointDTO> history(PowerPriceQueryDTO query) {
|
|
|
+ if (StringUtils.isBlank(query.getModeCode()) || StringUtils.isBlank(query.getMaterialName())) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ QueryWrapper<MaterialBoweiyunPriceRecord> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.select("province", "city", "particular_year", "time_version",
|
|
|
+ "tax_free_price", "tax_price", "market_value_name", "scraped_at");
|
|
|
+ buildWrapper(query, false, wrapper);
|
|
|
+ wrapper.orderByAsc("create_time").last("LIMIT " + HISTORY_LIMIT);
|
|
|
+
|
|
|
+ return this.list(wrapper).stream().map(record -> {
|
|
|
+ PowerPricePointDTO point = new PowerPricePointDTO();
|
|
|
+ point.setProvince(record.getProvince());
|
|
|
+ point.setCity(record.getCity());
|
|
|
+ point.setParticularYear(record.getParticularYear());
|
|
|
+ point.setTimeVersion(record.getTimeVersion());
|
|
|
+ point.setTaxFreePrice(record.getTaxFreePrice());
|
|
|
+ point.setTaxPrice(record.getTaxPrice());
|
|
|
+ point.setMarketValueName(record.getMarketValueName());
|
|
|
+ point.setScrapedAt(record.getScrapedAt());
|
|
|
+ return point;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 筛选框下拉选项;第一次查询(或 Redis 被清空后)才回源查库。 */
|
|
|
+ public PowerPriceOptionsDTO options() {
|
|
|
+ PowerPriceOptionsDTO cached = readOptionsCache();
|
|
|
+ if (cached != null) {
|
|
|
+ return cached;
|
|
|
+ }
|
|
|
+ PowerPriceOptionsDTO dto = loadOptions();
|
|
|
+ writeOptionsCache(dto);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导入入库后刷新下拉选项:先查库重建再覆盖,查库失败时保留旧缓存。
|
|
|
+ */
|
|
|
+ public void refreshQueryCache() {
|
|
|
+ PowerPriceOptionsDTO dto;
|
|
|
+ try {
|
|
|
+ dto = loadOptions();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("导入后重建下拉选项缓存失败,保留旧缓存: {}", e.getMessage());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ writeOptionsCache(dto);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量幂等写入。
|
|
|
+ *
|
|
|
+ * 1. 校验 dedupe_key,缺失或非法时按冻结公式重算;
|
|
|
+ * 2. 同一批内按 dedupe_key 去重;
|
|
|
+ * 3. 按 sqlBatchSize 分片执行,避免单条 SQL 过大。
|
|
|
+ *
|
|
|
+ * @return 实际提交给数据库的记录数
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int upsertBatch(List<MaterialBoweiyunPriceRecord> records) {
|
|
|
+ if (records == null || records.isEmpty()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ Map<String, MaterialBoweiyunPriceRecord> unique = new LinkedHashMap<>();
|
|
|
+ for (MaterialBoweiyunPriceRecord record : records) {
|
|
|
+ if (!isValidDedupeKey(record.getDedupeKey())) {
|
|
|
+ record.setDedupeKey(buildDedupeKey(record));
|
|
|
+ }
|
|
|
+ unique.putIfAbsent(record.getDedupeKey(), record);
|
|
|
+ }
|
|
|
+ List<MaterialBoweiyunPriceRecord> list = new ArrayList<>(unique.values());
|
|
|
+ for (int from = 0; from < list.size(); from += SQL_BATCH_SIZE) {
|
|
|
+ int to = Math.min(from + SQL_BATCH_SIZE, list.size());
|
|
|
+ baseMapper.upsertBatch(list.subList(from, to));
|
|
|
+ }
|
|
|
+ return list.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ /** dedupe_key 必须是大写化后仍匹配的 64 位小写十六进制。 */
|
|
|
+ public static boolean isValidDedupeKey(String key) {
|
|
|
+ if (key == null || key.length() != 64) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < key.length(); i++) {
|
|
|
+ char c = key.charAt(i);
|
|
|
+ if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按冻结公式重算 dedupe_key(与 Python app/power/dedupe.py 保持一致):
|
|
|
+ * sha256(site \u001f mode \u001f province \u001f city \u001f year \u001f version \u001f name \u001f spec \u001f unit)。
|
|
|
+ * 名称/规格/单位压缩连续空白,其余字段只 trim。
|
|
|
+ */
|
|
|
+ public static String buildDedupeKey(MaterialBoweiyunPriceRecord record) {
|
|
|
+ // Java 字符串没有 \x 转义,单元分隔符 0x1F 必须写成 \u001f
|
|
|
+ String raw = String.join("\u001f",
|
|
|
+ "boweiyun",
|
|
|
+ trim(record.getModeCode()),
|
|
|
+ trim(record.getProvince()),
|
|
|
+ trim(record.getCity()),
|
|
|
+ trim(record.getParticularYear()),
|
|
|
+ trim(record.getTimeVersion()),
|
|
|
+ normalizeText(record.getMaterialName()),
|
|
|
+ normalizeText(record.getOriginalSpec()),
|
|
|
+ normalizeText(record.getOriginalUnit()));
|
|
|
+ return DigestUtil.sha256Hex(raw);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String trim(String value) {
|
|
|
+ return value == null ? "" : value.trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String normalizeText(String value) {
|
|
|
+ return value == null ? "" : WHITESPACE.matcher(value.trim()).replaceAll(" ");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组装查询条件。空字符串一律忽略。
|
|
|
+ *
|
|
|
+ * @param fuzzyName true=名称模糊匹配(列表);false=名称/规格/单位精确匹配(走势)
|
|
|
+ */
|
|
|
+ private void buildWrapper(PowerPriceQueryDTO query, boolean fuzzyName, QueryWrapper<MaterialBoweiyunPriceRecord> wrapper) {
|
|
|
+ wrapper.in(!splitValues(query.getModeCode()).isEmpty(), "mode_code", splitValues(query.getModeCode()));
|
|
|
+ wrapper.in(!splitValues(query.getProvince()).isEmpty(), "province", splitValues(query.getProvince()));
|
|
|
+ wrapper.in(!splitValues(query.getCity()).isEmpty(), "city", splitValues(query.getCity()));
|
|
|
+ wrapper.in(!splitValues(query.getParticularYear()).isEmpty(),
|
|
|
+ "particular_year", splitValues(query.getParticularYear()));
|
|
|
+ wrapper.in(!splitValues(query.getTimeVersion()).isEmpty(),
|
|
|
+ "time_version", splitValues(query.getTimeVersion()));
|
|
|
+ wrapper.in(!splitValues(query.getFirCatalog()).isEmpty(),
|
|
|
+ "fir_catalog", splitValues(query.getFirCatalog()));
|
|
|
+ wrapper.in(!splitValues(query.getSecoCatalog()).isEmpty(),
|
|
|
+ "seco_catalog", splitValues(query.getSecoCatalog()));
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(query.getMaterialName())) {
|
|
|
+ if (fuzzyName) {
|
|
|
+ wrapper.like("material_name", query.getMaterialName().trim());
|
|
|
+ } else {
|
|
|
+ wrapper.eq("material_name", query.getMaterialName().trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(query.getOriginalSpec())) {
|
|
|
+ wrapper.eq("original_spec", query.getOriginalSpec().trim());
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(query.getOriginalUnit())) {
|
|
|
+ wrapper.eq("original_unit", query.getOriginalUnit().trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 导入日期区间必须成对出现;结束日补到 23:59:59,避免只查到 0 点
|
|
|
+ String[] dates = query.getCreateDates();
|
|
|
+ if (dates != null && dates.length == 2
|
|
|
+ && StringUtils.isNotBlank(dates[0]) && StringUtils.isNotBlank(dates[1])) {
|
|
|
+ String start = normalizeDay(dates[0], " 00:00:00");
|
|
|
+ String end = normalizeDay(dates[1], " 23:59:59");
|
|
|
+ if (start != null && end != null) {
|
|
|
+ wrapper.between("create_time", start, end);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 校验 yyyy-MM-dd 并拼上时间部分;非法日期返回 null(条件忽略)。 */
|
|
|
+ private static String normalizeDay(String raw, String timeSuffix) {
|
|
|
+ String text = raw == null ? "" : raw.trim();
|
|
|
+ if (text.length() >= 10) {
|
|
|
+ text = text.substring(0, 10);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ LocalDate.parse(text);
|
|
|
+ } catch (DateTimeParseException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return text + timeSuffix;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<String> splitValues(String raw) {
|
|
|
+ if (StringUtils.isBlank(raw)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return Arrays.stream(raw.split(","))
|
|
|
+ .map(String::trim)
|
|
|
+ .filter(StringUtils::isNotBlank)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 回源查询下拉选项。
|
|
|
+ *
|
|
|
+ * 页签/年份/期数从登记表查(行数很少);地区、一级/二级分类从价格表查去重组合,
|
|
|
+ * 地区只取三列走 idx_mbpr_list,分类只取四列走 idx_mbpr_catalog,不扫整行。
|
|
|
+ */
|
|
|
+ private PowerPriceOptionsDTO loadOptions() {
|
|
|
+ QueryWrapper<MaterialBoweiyunSaveData> registryWrapper = new QueryWrapper<>();
|
|
|
+ registryWrapper.select("DISTINCT mode_code, mode_name, particular_year, time_version");
|
|
|
+ List<Map<String, Object>> registryRows = saveDataMapper.selectMaps(registryWrapper);
|
|
|
+
|
|
|
+ QueryWrapper<MaterialBoweiyunPriceRecord> geoWrapper = new QueryWrapper<>();
|
|
|
+ geoWrapper.select("DISTINCT mode_code, province, city");
|
|
|
+ List<Map<String, Object>> geoRows = baseMapper.selectMaps(geoWrapper);
|
|
|
+
|
|
|
+ // 分类下拉:一级/二级成对去重,走 idx_mbpr_catalog;三级分类不参与筛选
|
|
|
+ QueryWrapper<MaterialBoweiyunPriceRecord> catalogWrapper = new QueryWrapper<>();
|
|
|
+ catalogWrapper.select("DISTINCT mode_code, fir_catalog, seco_catalog");
|
|
|
+ List<Map<String, Object>> catalogRows = baseMapper.selectMaps(catalogWrapper);
|
|
|
+
|
|
|
+ Set<String> provinces = new TreeSet<>();
|
|
|
+ Map<String, Set<String>> citySets = new TreeMap<>();
|
|
|
+ Map<String, Set<String>> modeProvinceSets = new TreeMap<>();
|
|
|
+ Map<String, Map<String, Set<String>>> modeCitySets = new TreeMap<>();
|
|
|
+
|
|
|
+ for (Map<String, Object> row : geoRows) {
|
|
|
+ String mode = stringValue(row, "mode_code").trim();
|
|
|
+ String province = stringValue(row, "province").trim();
|
|
|
+ String city = stringValue(row, "city").trim();
|
|
|
+ if (StringUtils.isBlank(province)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ provinces.add(province);
|
|
|
+ if (StringUtils.isNotBlank(mode)) {
|
|
|
+ modeProvinceSets.computeIfAbsent(mode, key -> new TreeSet<>()).add(province);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(city)) {
|
|
|
+ citySets.computeIfAbsent(province, key -> new TreeSet<>()).add(city);
|
|
|
+ if (StringUtils.isNotBlank(mode)) {
|
|
|
+ modeCitySets.computeIfAbsent(mode, key -> new TreeMap<>())
|
|
|
+ .computeIfAbsent(province, key -> new TreeSet<>()).add(city);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<String> years = new TreeSet<>(Comparator.reverseOrder());
|
|
|
+ Map<String, Map<String, Set<String>>> versionSets = new TreeMap<>();
|
|
|
+ for (Map<String, Object> row : registryRows) {
|
|
|
+ String mode = stringValue(row, "mode_code").trim();
|
|
|
+ String year = stringValue(row, "particular_year").trim();
|
|
|
+ String version = stringValue(row, "time_version").trim();
|
|
|
+ if (StringUtils.isNotBlank(year)) {
|
|
|
+ years.add(year);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(mode) && StringUtils.isNotBlank(year) && StringUtils.isNotBlank(version)) {
|
|
|
+ versionSets.computeIfAbsent(mode, key -> new TreeMap<>(Comparator.reverseOrder()))
|
|
|
+ .computeIfAbsent(year, key -> new TreeSet<>(VERSION_COMPARATOR)).add(version);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 分类层级:一级去重;二级挂在一级下;再按页签各存一份,供前端本地级联
|
|
|
+ Set<String> firCatalogs = new TreeSet<>();
|
|
|
+ Map<String, Set<String>> secoSets = new TreeMap<>();
|
|
|
+ Map<String, Set<String>> modeFirSets = new TreeMap<>();
|
|
|
+ Map<String, Map<String, Set<String>>> modeFirSecoSets = new TreeMap<>();
|
|
|
+ for (Map<String, Object> row : catalogRows) {
|
|
|
+ String mode = stringValue(row, "mode_code").trim();
|
|
|
+ String fir = stringValue(row, "fir_catalog").trim();
|
|
|
+ String seco = stringValue(row, "seco_catalog").trim();
|
|
|
+ if (StringUtils.isBlank(fir)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ firCatalogs.add(fir);
|
|
|
+ if (StringUtils.isNotBlank(seco)) {
|
|
|
+ secoSets.computeIfAbsent(fir, key -> new TreeSet<>()).add(seco);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(mode)) {
|
|
|
+ modeFirSets.computeIfAbsent(mode, key -> new TreeSet<>()).add(fir);
|
|
|
+ if (StringUtils.isNotBlank(seco)) {
|
|
|
+ modeFirSecoSets.computeIfAbsent(mode, key -> new TreeMap<>())
|
|
|
+ .computeIfAbsent(fir, key -> new TreeSet<>()).add(seco);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ PowerPriceOptionsDTO dto = new PowerPriceOptionsDTO();
|
|
|
+ dto.setModes(MODE_NAMES.entrySet().stream()
|
|
|
+ .map(entry -> new PowerPriceModeDTO(entry.getKey(), entry.getValue()))
|
|
|
+ .collect(Collectors.toList()));
|
|
|
+ dto.setProvinces(new ArrayList<>(provinces));
|
|
|
+ dto.setCitiesByProvince(toListMap(citySets));
|
|
|
+ dto.setProvincesByMode(toListMap(modeProvinceSets));
|
|
|
+
|
|
|
+ Map<String, Map<String, List<String>>> citiesByModeProvince = new TreeMap<>();
|
|
|
+ modeCitySets.forEach((mode, byProvince) -> citiesByModeProvince.put(mode, toListMap(byProvince)));
|
|
|
+ dto.setCitiesByModeProvince(citiesByModeProvince);
|
|
|
+
|
|
|
+ dto.setYears(new ArrayList<>(years));
|
|
|
+
|
|
|
+ Map<String, Map<String, List<String>>> versions = new TreeMap<>();
|
|
|
+ versionSets.forEach((mode, byYear) -> {
|
|
|
+ Map<String, List<String>> target = new LinkedHashMap<>();
|
|
|
+ byYear.forEach((year, values) -> target.put(year, new ArrayList<>(values)));
|
|
|
+ versions.put(mode, target);
|
|
|
+ });
|
|
|
+ dto.setTimeVersionsByModeYear(versions);
|
|
|
+
|
|
|
+ dto.setFirCatalogs(new ArrayList<>(firCatalogs));
|
|
|
+ dto.setSecoCatalogsByFir(toListMap(secoSets));
|
|
|
+ dto.setFirCatalogsByMode(toListMap(modeFirSets));
|
|
|
+
|
|
|
+ Map<String, Map<String, List<String>>> secoByModeFir = new TreeMap<>();
|
|
|
+ modeFirSecoSets.forEach((mode, byFir) -> secoByModeFir.put(mode, toListMap(byFir)));
|
|
|
+ dto.setSecoCatalogsByModeFir(secoByModeFir);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 期数排序:月份按数字升序,季度/半年/全年按自然顺序插在对应位置。
|
|
|
+ * 不同页签的期数不会混在同一列表里,这里只需保证同一页签内顺序合理。
|
|
|
+ */
|
|
|
+ private static final Comparator<String> VERSION_COMPARATOR = (left, right) -> {
|
|
|
+ int rankCompare = Integer.compare(versionRank(left), versionRank(right));
|
|
|
+ return rankCompare != 0 ? rankCompare : left.compareTo(right);
|
|
|
+ };
|
|
|
+
|
|
|
+ private static int versionRank(String version) {
|
|
|
+ String text = version == null ? "" : version.trim();
|
|
|
+ switch (text) {
|
|
|
+ case "上半年":
|
|
|
+ return 25;
|
|
|
+ case "一季度":
|
|
|
+ return 21;
|
|
|
+ case "二季度":
|
|
|
+ return 22;
|
|
|
+ case "三季度":
|
|
|
+ return 23;
|
|
|
+ case "四季度":
|
|
|
+ return 24;
|
|
|
+ case "下半年":
|
|
|
+ return 27;
|
|
|
+ case "全年":
|
|
|
+ return 30;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (text.matches("\\d{1,2}")) {
|
|
|
+ return Integer.parseInt(text);
|
|
|
+ }
|
|
|
+ return 100;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Map<String, List<String>> toListMap(Map<String, Set<String>> source) {
|
|
|
+ Map<String, List<String>> target = new TreeMap<>();
|
|
|
+ source.forEach((key, values) -> target.put(key, new ArrayList<>(values)));
|
|
|
+ return target;
|
|
|
+ }
|
|
|
+
|
|
|
+ private PowerPriceOptionsDTO readOptionsCache() {
|
|
|
+ try {
|
|
|
+ Object value = redisUtils.get(OPTIONS_CACHE_KEY);
|
|
|
+ return value instanceof PowerPriceOptionsDTO ? (PowerPriceOptionsDTO) value : null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 缓存里可能是旧版本 DTO,反序列化失败时清掉并回源
|
|
|
+ log.warn("读取下拉选项缓存失败,改为直接查询: {}", e.getMessage());
|
|
|
+ try {
|
|
|
+ redisUtils.delete(OPTIONS_CACHE_KEY);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ // 清缓存失败不影响本次查询
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void writeOptionsCache(PowerPriceOptionsDTO dto) {
|
|
|
+ try {
|
|
|
+ redisUtils.set(OPTIONS_CACHE_KEY, dto);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("写入下拉选项缓存失败: {}", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** selectMaps 的 key 大小写依赖驱动,这里做不区分大小写的取值。 */
|
|
|
+ private static String stringValue(Map<String, Object> row, String column) {
|
|
|
+ for (Map.Entry<String, Object> entry : row.entrySet()) {
|
|
|
+ if (column.equalsIgnoreCase(entry.getKey())) {
|
|
|
+ return entry.getValue() == null ? "" : String.valueOf(entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+}
|