| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- package com.etotem.num.service;
- import com.etotem.num.entity.SystemConfig;
- import com.etotem.num.repository.SystemConfigRepository;
- import org.springframework.stereotype.Service;
- import javax.annotation.PostConstruct;
- import java.time.LocalDateTime;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- @Service
- public class ConfigService {
- private final SystemConfigRepository configRepository;
- private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();
- public ConfigService(SystemConfigRepository configRepository) {
- this.configRepository = configRepository;
- }
- /**
- * Load all configs into memory cache on startup (US-8.1 §3-4).
- * If DB is empty, auto-initialize with seed data.
- */
- @PostConstruct
- public void loadAllToCache() {
- cache.clear();
- List<SystemConfig> all = configRepository.findAll();
- if (all.isEmpty()) {
- initSeedData();
- all = configRepository.findAll();
- }
- all.forEach(cfg -> cache.put(cfg.getConfigKey(), cfg.getConfigValue()));
- }
- private void initSeedData() {
- Map<String, SeedEntry> seeds = new LinkedHashMap<>();
- seeds.put("pricing.practitioner.seed", new SeedEntry("131400", "price", "能量师种子价(分)"));
- seeds.put("pricing.practitioner.standard", new SeedEntry("198600", "price", "能量师标准价(分)"));
- seeds.put("pricing.practitioner.seed_limit", new SeedEntry("300", "int", "种子价名额上限"));
- seeds.put("pricing.practitioner.seed_period_end", new SeedEntry("2027-12-31T23:59:59", "string", "种子价有效期截止时间"));
- seeds.put("pricing.annual", new SeedEntry("13100", "price", "C端年费(分)"));
- seeds.put("commission.practitioner.l1", new SeedEntry("50000", "price", "B端一级佣金—能量师推荐(分)"));
- seeds.put("commission.practitioner.l1_cend", new SeedEntry("20000", "price", "B端一级佣金—C端推荐(分)"));
- seeds.put("commission.practitioner.l2", new SeedEntry("10000", "price", "B端二级佣金(分)"));
- seeds.put("commission.annual.direct_rate", new SeedEntry("4000", "percent", "C端直接佣金比例(万分比)"));
- seeds.put("commission.annual.upstream_rate", new SeedEntry("500", "percent", "C端上级佣金比例(万分比)"));
- // US-9.5/9.6 EPIC 9: 人工方案配置参数
- seeds.put("commerce.category.practitioner_plan.commission_rate", new SeedEntry("3000", "percent", "人工方案平台佣金率(万分比)"));
- seeds.put("commission.practitioner_plan.referral_rate", new SeedEntry("2000", "percent", "人工方案分销佣金占比(万分比)"));
- seeds.put("commission.practitioner_plan.upstream_rate", new SeedEntry("500", "percent", "人工方案上级佣金占比(万分比)"));
- seeds.put("plan_request.enabled", new SeedEntry("true", "bool", "人工方案功能开关"));
- seeds.put("plan_request.max_negotiation_rounds", new SeedEntry("5", "int", "最大协商轮次"));
- seeds.put("plan_request.auto_cancel_hours", new SeedEntry("72", "int", "协商超时自动取消(小时)"));
- seeds.put("plan_request.min_price", new SeedEntry("5000", "price", "方案最低价(分)"));
- seeds.put("plan_request.max_price", new SeedEntry("999900", "price", "方案最高价(分)"));
- // Dify Workflow config (US-3.1)
- seeds.put("dify.workflow.app_id", new SeedEntry("", "string", "Dify Workflow App ID"));
- // Quota configs
- seeds.put("quota_chart", new SeedEntry("3", "int", "每日能量盘生成次数(非VIP)"));
- seeds.put("quota_chat", new SeedEntry("3", "int", "每日AI解读次数(非VIP)"));
- seeds.put("quota_share", new SeedEntry("1", "int", "每日分享次数(非VIP)"));
- seeds.put("quota_academic", new SeedEntry("1", "int", "每日学业方向分析次数(非VIP)"));
- // Super member pricing & commission (EPIC 10)
- seeds.put("pricing.super_member", new SeedEntry("131400", "price", "超级会员年费(分)"));
- seeds.put("commission.super_member.direct_rate", new SeedEntry("3000", "percent", "超级会员直推佣金率(千分比)"));
- seeds.put("commission.super_member.upstream_rate", new SeedEntry("500", "percent", "超级会员上级佣金率(千分比)"));
- // Dify additional Workflow configs
- seeds.put("dify.workflow.relation_app_id", new SeedEntry("", "string", "Dify 关系合盘解读 Workflow App ID"));
- seeds.put("dify.workflow.number_analysis_app_id", new SeedEntry("", "string", "Dify 八星号码分析 Workflow App ID"));
- // Dify Chatflow config for relation analysis (v20260604.2 redesign)
- seeds.put("dify.chatflow.relation_app_id", new SeedEntry("", "string", "Dify 关系分析 Chatflow App ID"));
- // Relation type configuration (v20260604.2 redesign)
- seeds.put("relation.types", new SeedEntry(
- "[{\"value\":\"spouse\",\"label\":\"夫妻\"},{\"value\":\"parent_child\",\"label\":\"亲子\"},{\"value\":\"colleague\",\"label\":\"同事\"},{\"value\":\"friend\",\"label\":\"朋友\"},{\"value\":\"superior\",\"label\":\"上下级\"},{\"value\":\"parent\",\"label\":\"父母\"},{\"value\":\"child\",\"label\":\"子女\"},{\"value\":\"grandparent\",\"label\":\"祖父母\"},{\"value\":\"grandchild\",\"label\":\"孙子女\"}]",
- "json", "关系类型配置(JSON数组)"));
- seeds.forEach((key, entry) -> {
- SystemConfig cfg = new SystemConfig();
- cfg.setConfigKey(key);
- cfg.setConfigValue(entry.value);
- cfg.setValueType(entry.type);
- cfg.setDescription(entry.desc);
- configRepository.save(cfg);
- });
- }
- /**
- * Returns all configs as full entity list (for admin list endpoint).
- */
- public List<SystemConfig> getAllConfigs() {
- return configRepository.findAllByOrderByConfigKeyAsc();
- }
- /**
- * Reset a config key to its seed value. Returns true if found.
- */
- public boolean resetToDefault(String configKey) {
- SeedEntry seed = getAllSeeds().get(configKey);
- if (seed == null) return false;
- setValue(configKey, seed.value);
- return true;
- }
- /**
- * Reset multiple config keys to seed values (within a card scope).
- */
- public void resetBatch(List<String> configKeys) {
- Map<String, String> seedValues = new HashMap<>();
- Map<String, SeedEntry> allSeeds = getAllSeeds();
- configKeys.forEach(key -> {
- SeedEntry entry = allSeeds.get(key);
- if (entry != null) {
- seedValues.put(key, entry.value);
- }
- });
- batchUpdate(seedValues);
- }
- private Map<String, SeedEntry> getAllSeeds() {
- Map<String, SeedEntry> seeds = new LinkedHashMap<>();
- seeds.put("pricing.practitioner.seed", new SeedEntry("131400", "price", "能量师种子价(分)"));
- seeds.put("pricing.practitioner.standard", new SeedEntry("198600", "price", "能量师标准价(分)"));
- seeds.put("pricing.practitioner.seed_limit", new SeedEntry("300", "int", "种子价名额上限"));
- seeds.put("pricing.practitioner.seed_period_end", new SeedEntry("2027-12-31T23:59:59", "string", "种子价有效期截止时间"));
- seeds.put("pricing.annual", new SeedEntry("13100", "price", "C端年费(分)"));
- seeds.put("commission.practitioner.l1", new SeedEntry("50000", "price", "B端一级佣金—能量师推荐(分)"));
- seeds.put("commission.practitioner.l1_cend", new SeedEntry("20000", "price", "B端一级佣金—C端推荐(分)"));
- seeds.put("commission.practitioner.l2", new SeedEntry("10000", "price", "B端二级佣金(分)"));
- seeds.put("commission.annual.direct_rate", new SeedEntry("4000", "percent", "C端直接佣金比例(万分比)"));
- seeds.put("commission.annual.upstream_rate", new SeedEntry("500", "percent", "C端上级佣金比例(万分比)"));
- seeds.put("commerce.category.practitioner_plan.commission_rate", new SeedEntry("3000", "percent", "人工方案平台佣金率(万分比)"));
- seeds.put("commission.practitioner_plan.referral_rate", new SeedEntry("2000", "percent", "人工方案分销佣金占比(万分比)"));
- seeds.put("commission.practitioner_plan.upstream_rate", new SeedEntry("500", "percent", "人工方案上级佣金占比(万分比)"));
- seeds.put("plan_request.enabled", new SeedEntry("true", "bool", "人工方案功能开关"));
- seeds.put("plan_request.max_negotiation_rounds", new SeedEntry("5", "int", "最大协商轮次"));
- seeds.put("plan_request.auto_cancel_hours", new SeedEntry("72", "int", "协商超时自动取消(小时)"));
- seeds.put("plan_request.min_price", new SeedEntry("5000", "price", "方案最低价(分)"));
- seeds.put("plan_request.max_price", new SeedEntry("999900", "price", "方案最高价(分)"));
- seeds.put("dify.workflow.app_id", new SeedEntry("", "string", "Dify Workflow App ID"));
- seeds.put("quota_chart", new SeedEntry("3", "int", "每日能量盘生成次数(非VIP)"));
- seeds.put("quota_chat", new SeedEntry("3", "int", "每日AI解读次数(非VIP)"));
- seeds.put("quota_share", new SeedEntry("1", "int", "每日分享次数(非VIP)"));
- seeds.put("quota_academic", new SeedEntry("1", "int", "每日学业方向分析次数(非VIP)"));
- // Super member pricing & commission (EPIC 10)
- seeds.put("pricing.super_member", new SeedEntry("131400", "price", "超级会员年费(分)"));
- seeds.put("commission.super_member.direct_rate", new SeedEntry("3000", "percent", "超级会员直推佣金率(千分比)"));
- seeds.put("commission.super_member.upstream_rate", new SeedEntry("500", "percent", "超级会员上级佣金率(千分比)"));
- // Dify additional Workflow configs
- seeds.put("dify.workflow.relation_app_id", new SeedEntry("", "string", "Dify 关系合盘解读 Workflow App ID"));
- seeds.put("dify.workflow.number_analysis_app_id", new SeedEntry("", "string", "Dify 八星号码分析 Workflow App ID"));
- // Dify Chatflow config for relation analysis (v20260604.2 redesign)
- seeds.put("dify.chatflow.relation_app_id", new SeedEntry("", "string", "Dify 关系分析 Chatflow App ID"));
- // Relation type configuration (v20260604.2 redesign)
- seeds.put("relation.types", new SeedEntry(
- "[{\"value\":\"spouse\",\"label\":\"夫妻\"},{\"value\":\"parent_child\",\"label\":\"亲子\"},{\"value\":\"colleague\",\"label\":\"同事\"},{\"value\":\"friend\",\"label\":\"朋友\"},{\"value\":\"superior\",\"label\":\"上下级\"},{\"value\":\"parent\",\"label\":\"父母\"},{\"value\":\"child\",\"label\":\"子女\"},{\"value\":\"grandparent\",\"label\":\"祖父母\"},{\"value\":\"grandchild\",\"label\":\"孙子女\"}]",
- "json", "关系类型配置(JSON数组)"));
- return seeds;
- }
- private static class SeedEntry {
- final String value;
- final String type;
- final String desc;
- SeedEntry(String value, String type, String desc) {
- this.value = value;
- this.type = type;
- this.desc = desc;
- }
- }
- /**
- * Get config value by key, returning default if not found.
- * Reads from memory cache (O(1)), falls back to DB.
- */
- public String getValue(String key, String defaultValue) {
- String val = cache.get(key);
- if (val != null) return val;
- // Cache miss → try DB
- return configRepository.findByConfigKey(key)
- .map(cfg -> {
- cache.put(key, cfg.getConfigValue());
- return cfg.getConfigValue();
- })
- .orElse(defaultValue);
- }
- /**
- * Same as getValue - alias for API clarity (US-8.1 §4 naming).
- */
- public String getString(String key, String defaultValue) {
- return getValue(key, defaultValue);
- }
- /**
- * Get config value as integer.
- */
- public int getInt(String key, int defaultValue) {
- String val = getValue(key, null);
- if (val == null) return defaultValue;
- try {
- return Integer.parseInt(val);
- } catch (NumberFormatException e) {
- return defaultValue;
- }
- }
- /**
- * Set config value (upsert) and update cache immediately (US-8.1 §6).
- */
- public void setValue(String key, String value) {
- SystemConfig config = configRepository.findByConfigKey(key).orElse(new SystemConfig());
- config.setConfigKey(key);
- config.setConfigValue(value);
- config.setUpdatedAt(LocalDateTime.now());
- configRepository.save(config);
- cache.put(key, value);
- }
- /**
- * Batch update multiple configs in one transaction (US-8.1 §7).
- */
- public void batchUpdate(Map<String, String> configs) {
- configs.forEach(this::setValue);
- }
- /**
- * Get all config as a map.
- */
- public Map<String, String> getAll() {
- Map<String, String> result = new HashMap<>();
- for (SystemConfig config : configRepository.findAll()) {
- result.put(config.getConfigKey(), config.getConfigValue());
- }
- return result;
- }
- /**
- * Get all config as a map from cache (faster).
- */
- public Map<String, String> getAllCached() {
- return new HashMap<>(cache);
- }
- /**
- * Get daily quota for a specific action type.
- */
- public int getQuota(String type) {
- Map<String, Integer> defaults = new HashMap<>();
- defaults.put("chart", 3);
- defaults.put("chat", 3);
- defaults.put("share", 1);
- defaults.put("academic", 1);
- return getInt("quota_" + type, defaults.getOrDefault(type, 3));
- }
- }
|