ConfigService.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package com.etotem.num.service;
  2. import com.etotem.num.entity.SystemConfig;
  3. import com.etotem.num.repository.SystemConfigRepository;
  4. import org.springframework.stereotype.Service;
  5. import javax.annotation.PostConstruct;
  6. import java.time.LocalDateTime;
  7. import java.util.HashMap;
  8. import java.util.LinkedHashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.concurrent.ConcurrentHashMap;
  12. @Service
  13. public class ConfigService {
  14. private final SystemConfigRepository configRepository;
  15. private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();
  16. public ConfigService(SystemConfigRepository configRepository) {
  17. this.configRepository = configRepository;
  18. }
  19. /**
  20. * Load all configs into memory cache on startup (US-8.1 §3-4).
  21. * If DB is empty, auto-initialize with seed data.
  22. */
  23. @PostConstruct
  24. public void loadAllToCache() {
  25. cache.clear();
  26. List<SystemConfig> all = configRepository.findAll();
  27. if (all.isEmpty()) {
  28. initSeedData();
  29. all = configRepository.findAll();
  30. }
  31. all.forEach(cfg -> cache.put(cfg.getConfigKey(), cfg.getConfigValue()));
  32. }
  33. private void initSeedData() {
  34. Map<String, SeedEntry> seeds = new LinkedHashMap<>();
  35. seeds.put("pricing.practitioner.seed", new SeedEntry("131400", "price", "能量师种子价(分)"));
  36. seeds.put("pricing.practitioner.standard", new SeedEntry("198600", "price", "能量师标准价(分)"));
  37. seeds.put("pricing.practitioner.seed_limit", new SeedEntry("300", "int", "种子价名额上限"));
  38. seeds.put("pricing.practitioner.seed_period_end", new SeedEntry("2027-12-31T23:59:59", "string", "种子价有效期截止时间"));
  39. seeds.put("pricing.annual", new SeedEntry("13100", "price", "C端年费(分)"));
  40. seeds.put("commission.practitioner.l1", new SeedEntry("50000", "price", "B端一级佣金—能量师推荐(分)"));
  41. seeds.put("commission.practitioner.l1_cend", new SeedEntry("20000", "price", "B端一级佣金—C端推荐(分)"));
  42. seeds.put("commission.practitioner.l2", new SeedEntry("10000", "price", "B端二级佣金(分)"));
  43. seeds.put("commission.annual.direct_rate", new SeedEntry("4000", "percent", "C端直接佣金比例(万分比)"));
  44. seeds.put("commission.annual.upstream_rate", new SeedEntry("500", "percent", "C端上级佣金比例(万分比)"));
  45. // US-9.5/9.6 EPIC 9: 人工方案配置参数
  46. seeds.put("commerce.category.practitioner_plan.commission_rate", new SeedEntry("3000", "percent", "人工方案平台佣金率(万分比)"));
  47. seeds.put("commission.practitioner_plan.referral_rate", new SeedEntry("2000", "percent", "人工方案分销佣金占比(万分比)"));
  48. seeds.put("commission.practitioner_plan.upstream_rate", new SeedEntry("500", "percent", "人工方案上级佣金占比(万分比)"));
  49. seeds.put("plan_request.enabled", new SeedEntry("true", "bool", "人工方案功能开关"));
  50. seeds.put("plan_request.max_negotiation_rounds", new SeedEntry("5", "int", "最大协商轮次"));
  51. seeds.put("plan_request.auto_cancel_hours", new SeedEntry("72", "int", "协商超时自动取消(小时)"));
  52. seeds.put("plan_request.min_price", new SeedEntry("5000", "price", "方案最低价(分)"));
  53. seeds.put("plan_request.max_price", new SeedEntry("999900", "price", "方案最高价(分)"));
  54. // Dify Workflow config (US-3.1)
  55. seeds.put("dify.workflow.app_id", new SeedEntry("", "string", "Dify Workflow App ID"));
  56. // Quota configs
  57. seeds.put("quota_chart", new SeedEntry("3", "int", "每日能量盘生成次数(非VIP)"));
  58. seeds.put("quota_chat", new SeedEntry("3", "int", "每日AI解读次数(非VIP)"));
  59. seeds.put("quota_share", new SeedEntry("1", "int", "每日分享次数(非VIP)"));
  60. seeds.put("quota_academic", new SeedEntry("1", "int", "每日学业方向分析次数(非VIP)"));
  61. // Super member pricing & commission (EPIC 10)
  62. seeds.put("pricing.super_member", new SeedEntry("131400", "price", "超级会员年费(分)"));
  63. seeds.put("commission.super_member.direct_rate", new SeedEntry("3000", "percent", "超级会员直推佣金率(千分比)"));
  64. seeds.put("commission.super_member.upstream_rate", new SeedEntry("500", "percent", "超级会员上级佣金率(千分比)"));
  65. // Dify additional Workflow configs
  66. seeds.put("dify.workflow.relation_app_id", new SeedEntry("", "string", "Dify 关系合盘解读 Workflow App ID"));
  67. seeds.put("dify.workflow.number_analysis_app_id", new SeedEntry("", "string", "Dify 八星号码分析 Workflow App ID"));
  68. // Dify Chatflow config for relation analysis (v20260604.2 redesign)
  69. seeds.put("dify.chatflow.relation_app_id", new SeedEntry("", "string", "Dify 关系分析 Chatflow App ID"));
  70. // Relation type configuration (v20260604.2 redesign)
  71. seeds.put("relation.types", new SeedEntry(
  72. "[{\"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\":\"孙子女\"}]",
  73. "json", "关系类型配置(JSON数组)"));
  74. seeds.forEach((key, entry) -> {
  75. SystemConfig cfg = new SystemConfig();
  76. cfg.setConfigKey(key);
  77. cfg.setConfigValue(entry.value);
  78. cfg.setValueType(entry.type);
  79. cfg.setDescription(entry.desc);
  80. configRepository.save(cfg);
  81. });
  82. }
  83. /**
  84. * Returns all configs as full entity list (for admin list endpoint).
  85. */
  86. public List<SystemConfig> getAllConfigs() {
  87. return configRepository.findAllByOrderByConfigKeyAsc();
  88. }
  89. /**
  90. * Reset a config key to its seed value. Returns true if found.
  91. */
  92. public boolean resetToDefault(String configKey) {
  93. SeedEntry seed = getAllSeeds().get(configKey);
  94. if (seed == null) return false;
  95. setValue(configKey, seed.value);
  96. return true;
  97. }
  98. /**
  99. * Reset multiple config keys to seed values (within a card scope).
  100. */
  101. public void resetBatch(List<String> configKeys) {
  102. Map<String, String> seedValues = new HashMap<>();
  103. Map<String, SeedEntry> allSeeds = getAllSeeds();
  104. configKeys.forEach(key -> {
  105. SeedEntry entry = allSeeds.get(key);
  106. if (entry != null) {
  107. seedValues.put(key, entry.value);
  108. }
  109. });
  110. batchUpdate(seedValues);
  111. }
  112. private Map<String, SeedEntry> getAllSeeds() {
  113. Map<String, SeedEntry> seeds = new LinkedHashMap<>();
  114. seeds.put("pricing.practitioner.seed", new SeedEntry("131400", "price", "能量师种子价(分)"));
  115. seeds.put("pricing.practitioner.standard", new SeedEntry("198600", "price", "能量师标准价(分)"));
  116. seeds.put("pricing.practitioner.seed_limit", new SeedEntry("300", "int", "种子价名额上限"));
  117. seeds.put("pricing.practitioner.seed_period_end", new SeedEntry("2027-12-31T23:59:59", "string", "种子价有效期截止时间"));
  118. seeds.put("pricing.annual", new SeedEntry("13100", "price", "C端年费(分)"));
  119. seeds.put("commission.practitioner.l1", new SeedEntry("50000", "price", "B端一级佣金—能量师推荐(分)"));
  120. seeds.put("commission.practitioner.l1_cend", new SeedEntry("20000", "price", "B端一级佣金—C端推荐(分)"));
  121. seeds.put("commission.practitioner.l2", new SeedEntry("10000", "price", "B端二级佣金(分)"));
  122. seeds.put("commission.annual.direct_rate", new SeedEntry("4000", "percent", "C端直接佣金比例(万分比)"));
  123. seeds.put("commission.annual.upstream_rate", new SeedEntry("500", "percent", "C端上级佣金比例(万分比)"));
  124. seeds.put("commerce.category.practitioner_plan.commission_rate", new SeedEntry("3000", "percent", "人工方案平台佣金率(万分比)"));
  125. seeds.put("commission.practitioner_plan.referral_rate", new SeedEntry("2000", "percent", "人工方案分销佣金占比(万分比)"));
  126. seeds.put("commission.practitioner_plan.upstream_rate", new SeedEntry("500", "percent", "人工方案上级佣金占比(万分比)"));
  127. seeds.put("plan_request.enabled", new SeedEntry("true", "bool", "人工方案功能开关"));
  128. seeds.put("plan_request.max_negotiation_rounds", new SeedEntry("5", "int", "最大协商轮次"));
  129. seeds.put("plan_request.auto_cancel_hours", new SeedEntry("72", "int", "协商超时自动取消(小时)"));
  130. seeds.put("plan_request.min_price", new SeedEntry("5000", "price", "方案最低价(分)"));
  131. seeds.put("plan_request.max_price", new SeedEntry("999900", "price", "方案最高价(分)"));
  132. seeds.put("dify.workflow.app_id", new SeedEntry("", "string", "Dify Workflow App ID"));
  133. seeds.put("quota_chart", new SeedEntry("3", "int", "每日能量盘生成次数(非VIP)"));
  134. seeds.put("quota_chat", new SeedEntry("3", "int", "每日AI解读次数(非VIP)"));
  135. seeds.put("quota_share", new SeedEntry("1", "int", "每日分享次数(非VIP)"));
  136. seeds.put("quota_academic", new SeedEntry("1", "int", "每日学业方向分析次数(非VIP)"));
  137. // Super member pricing & commission (EPIC 10)
  138. seeds.put("pricing.super_member", new SeedEntry("131400", "price", "超级会员年费(分)"));
  139. seeds.put("commission.super_member.direct_rate", new SeedEntry("3000", "percent", "超级会员直推佣金率(千分比)"));
  140. seeds.put("commission.super_member.upstream_rate", new SeedEntry("500", "percent", "超级会员上级佣金率(千分比)"));
  141. // Dify additional Workflow configs
  142. seeds.put("dify.workflow.relation_app_id", new SeedEntry("", "string", "Dify 关系合盘解读 Workflow App ID"));
  143. seeds.put("dify.workflow.number_analysis_app_id", new SeedEntry("", "string", "Dify 八星号码分析 Workflow App ID"));
  144. // Dify Chatflow config for relation analysis (v20260604.2 redesign)
  145. seeds.put("dify.chatflow.relation_app_id", new SeedEntry("", "string", "Dify 关系分析 Chatflow App ID"));
  146. // Relation type configuration (v20260604.2 redesign)
  147. seeds.put("relation.types", new SeedEntry(
  148. "[{\"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\":\"孙子女\"}]",
  149. "json", "关系类型配置(JSON数组)"));
  150. return seeds;
  151. }
  152. private static class SeedEntry {
  153. final String value;
  154. final String type;
  155. final String desc;
  156. SeedEntry(String value, String type, String desc) {
  157. this.value = value;
  158. this.type = type;
  159. this.desc = desc;
  160. }
  161. }
  162. /**
  163. * Get config value by key, returning default if not found.
  164. * Reads from memory cache (O(1)), falls back to DB.
  165. */
  166. public String getValue(String key, String defaultValue) {
  167. String val = cache.get(key);
  168. if (val != null) return val;
  169. // Cache miss → try DB
  170. return configRepository.findByConfigKey(key)
  171. .map(cfg -> {
  172. cache.put(key, cfg.getConfigValue());
  173. return cfg.getConfigValue();
  174. })
  175. .orElse(defaultValue);
  176. }
  177. /**
  178. * Same as getValue - alias for API clarity (US-8.1 §4 naming).
  179. */
  180. public String getString(String key, String defaultValue) {
  181. return getValue(key, defaultValue);
  182. }
  183. /**
  184. * Get config value as integer.
  185. */
  186. public int getInt(String key, int defaultValue) {
  187. String val = getValue(key, null);
  188. if (val == null) return defaultValue;
  189. try {
  190. return Integer.parseInt(val);
  191. } catch (NumberFormatException e) {
  192. return defaultValue;
  193. }
  194. }
  195. /**
  196. * Set config value (upsert) and update cache immediately (US-8.1 §6).
  197. */
  198. public void setValue(String key, String value) {
  199. SystemConfig config = configRepository.findByConfigKey(key).orElse(new SystemConfig());
  200. config.setConfigKey(key);
  201. config.setConfigValue(value);
  202. config.setUpdatedAt(LocalDateTime.now());
  203. configRepository.save(config);
  204. cache.put(key, value);
  205. }
  206. /**
  207. * Batch update multiple configs in one transaction (US-8.1 §7).
  208. */
  209. public void batchUpdate(Map<String, String> configs) {
  210. configs.forEach(this::setValue);
  211. }
  212. /**
  213. * Get all config as a map.
  214. */
  215. public Map<String, String> getAll() {
  216. Map<String, String> result = new HashMap<>();
  217. for (SystemConfig config : configRepository.findAll()) {
  218. result.put(config.getConfigKey(), config.getConfigValue());
  219. }
  220. return result;
  221. }
  222. /**
  223. * Get all config as a map from cache (faster).
  224. */
  225. public Map<String, String> getAllCached() {
  226. return new HashMap<>(cache);
  227. }
  228. /**
  229. * Get daily quota for a specific action type.
  230. */
  231. public int getQuota(String type) {
  232. Map<String, Integer> defaults = new HashMap<>();
  233. defaults.put("chart", 3);
  234. defaults.put("chat", 3);
  235. defaults.put("share", 1);
  236. defaults.put("academic", 1);
  237. return getInt("quota_" + type, defaults.getOrDefault(type, 3));
  238. }
  239. }