cy-policy-meta.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*!
  2. * 承延融合平台 v1.0 · 风险金字塔 + 9 险种共享元数据
  3. *
  4. * 用途:被 Calculator v3.0 / PolicySummary v1.0 / PolicyOrganizer v1.0 三方共享调用,
  5. * 提供风险金字塔三层结构、9 险种归类映射、需求保额简化计算、倒金字塔四档诊断。
  6. *
  7. * 引入方式:<script src="data/cy-policy-meta.js"></script>
  8. * 必须在 portraits-bundle.js / cy-renderer.js 之后引入。
  9. *
  10. * 立法依据:
  11. * - 错题本 #11 共享 JS:涉及 ≥3 HTML 必走 data/cy-*.js 模式
  12. * - 错题本 #9 双源数据:矩阵数字 100% 从 policies 实时算,绝不硬编码
  13. * - v2.2 实施方案 §三 + §七.5 倒金字塔四档报警阈值
  14. *
  15. * 暴露全局:
  16. * window.CY_RISK_TIERS · 三层金字塔元数据
  17. * window.CY_RISK_DIMENSIONS · 9 险种 → 三层映射
  18. * window.CYPolicyMeta.* · 工具函数集
  19. */
  20. (function () {
  21. 'use strict';
  22. // ════════════════════════════════════════════════════════════════════════
  23. // 1. 三层风险金字塔元数据(v2.1 §3.1)
  24. // ════════════════════════════════════════════════════════════════════════
  25. window.CY_RISK_TIERS = [
  26. {
  27. level: '基础风险',
  28. tier_id: 'base',
  29. type: '损失性风险',
  30. action: '进 / 防',
  31. problem: '没钱',
  32. color: '#E07A3B', // 🟧 基础橙
  33. color_light: '#FBE3D2',
  34. color_dark: '#9B4D1F',
  35. description: '意外、疾病、身故等不可预估的损失风险 · 解决"没钱"问题',
  36. population: '所有家庭 · 必配',
  37. risk_dimensions: ['意外险', '医疗险', '重疾险']
  38. },
  39. {
  40. level: '中端风险',
  41. tier_id: 'mid',
  42. type: '支出性风险',
  43. action: '出 / 增',
  44. problem: '花钱',
  45. color: '#D4A93A', // 🟨 中端浅金
  46. color_light: '#F5EBC8',
  47. color_dark: '#8B6F1A',
  48. description: '教育、养老等可预估的支出风险 + 主理人收入替代 · 解决"花钱"问题',
  49. population: '中产 + 中高净值',
  50. risk_dimensions: ['定期寿险', '教育年金', '养老年金']
  51. },
  52. {
  53. level: '高端风险',
  54. tier_id: 'high',
  55. type: '所有性风险',
  56. action: '保 / 传',
  57. problem: '留钱',
  58. color: '#7A3FA0', // 🟣 高端紫(v2026-06-14 与金字塔顶层 L1 对齐 · 原 #C9A84C 香槟金)
  59. color_light: '#EFE3F5',
  60. color_dark: '#4A1F70',
  61. description: '婚姻、税务、债务、传承等所有性风险 · 解决"留钱"问题',
  62. population: 'UHNW / 跨境配置',
  63. risk_dimensions: ['终身寿险']
  64. }
  65. ];
  66. // ════════════════════════════════════════════════════════════════════════
  67. // 2. 7 险种 → 三层 + 细分映射(v1.1 · 去掉护理险 / 满期金领取)
  68. // ════════════════════════════════════════════════════════════════════════
  69. window.CY_RISK_DIMENSIONS = [
  70. { key: '意外险', tier: '基础风险', tier_id: 'base', icon: '⚡', subcategories: ['意外伤害', '意外医疗'] },
  71. { key: '医疗险', tier: '基础风险', tier_id: 'base', icon: '🏥', subcategories: ['高端医疗', '国际医疗'] },
  72. { key: '重疾险', tier: '基础风险', tier_id: 'base', icon: '⚕', subcategories: ['轻中症', '重症', '重疾多次', '恶性肿瘤多次', '防癌'] },
  73. { key: '定期寿险', tier: '中端风险', tier_id: 'mid', icon: '🛡', subcategories: ['主理人收入替代'] },
  74. { key: '教育年金', tier: '中端风险', tier_id: 'mid', icon: '🎓', subcategories: ['子女教育'] },
  75. { key: '养老年金', tier: '中端风险', tier_id: 'mid', icon: '🌅', subcategories: ['养老支出'] },
  76. { key: '终身寿险', tier: '高端风险', tier_id: 'high', icon: '💎', subcategories: ['增额终身寿', '传统终身寿', 'IUL', 'VUL', 'PPLI'] }
  77. ];
  78. // ════════════════════════════════════════════════════════════════════════
  79. // 3. 兼容性映射:portraits-bundle.js 既有 product_type → 9 险种 key
  80. // ════════════════════════════════════════════════════════════════════════
  81. // 既有 portrait 的 product_type 用英文 enum,这里负责 mapping 到中文 9 险种
  82. // 不修改 portraits-bundle.js,只在读取时做转换
  83. const PRODUCT_TYPE_MAPPING = {
  84. 'PPLI': '终身寿险',
  85. 'VUL': '终身寿险',
  86. 'IUL': '终身寿险',
  87. 'whole_life': '终身寿险',
  88. 'term_life': '定期寿险',
  89. 'critical_illness': '重疾险',
  90. 'medical': '医疗险',
  91. 'international_health': '医疗险',
  92. 'health': '医疗险',
  93. 'accident': '意外险',
  94. 'long_term_care': '护理险',
  95. 'education_annuity': '教育年金',
  96. 'retirement_annuity': '养老年金',
  97. 'endowment': '满期金领取',
  98. 'maturity': '满期金领取'
  99. };
  100. function mapPolicyToRiskDimension(policy) {
  101. if (!policy) return null;
  102. // 优先用 risk_dimension 字段(新 portrait 直接给)
  103. if (policy.risk_dimension) return policy.risk_dimension;
  104. // fallback 到 product_type 映射
  105. const pt = (policy.product_type || '').toLowerCase();
  106. return PRODUCT_TYPE_MAPPING[policy.product_type] ||
  107. PRODUCT_TYPE_MAPPING[pt] ||
  108. '终身寿险'; // 兜底:未识别归到高端层(避免误算到基础层)
  109. }
  110. function getDimensionMeta(dimensionKey) {
  111. return window.CY_RISK_DIMENSIONS.find(d => d.key === dimensionKey) || null;
  112. }
  113. function getTierMeta(tierLevel) {
  114. return window.CY_RISK_TIERS.find(t => t.level === tierLevel || t.tier_id === tierLevel) || null;
  115. }
  116. // ════════════════════════════════════════════════════════════════════════
  117. // 4. 需求保额简化计算(Q4 决策:A4 Calculator 自动拉取作为基础值)
  118. // Phase 1 用简化版,Phase 2 升级 A3 智能推荐 + 代理人微调
  119. // ════════════════════════════════════════════════════════════════════════
  120. function computeMemberDemand(member, family, totalAumCents) {
  121. // 输入:family.members 中某成员 + family + 家族总资产(分单位)
  122. // 输出:9 险种 demand_cents(分单位 · 与 face_amount_cents 同单位)
  123. if (!member) return {};
  124. const age = member.age || 40;
  125. const isPrincipal = member.role === 'principal';
  126. const isMinor = member.age && member.age < 18;
  127. const isElder = age >= 60;
  128. // 年支出(优先 family.annual_expense_cny,否则按总资产 1.5% 估)
  129. const annualExpenseYuan = family && family.annual_expense_cny
  130. ? family.annual_expense_cny
  131. : Math.round((totalAumCents || 0) / 100 * 0.015);
  132. const annualExpenseCents = annualExpenseYuan * 100;
  133. // 家族总资产(元)
  134. const totalAumYuan = (totalAumCents || 0) / 100;
  135. const demand = {};
  136. // ── 🟧 基础风险层(每个家庭成员都需要)──
  137. // 意外险:年支出 × 5(覆盖 5 年家庭维持)
  138. demand['意外险'] = annualExpenseCents * 5;
  139. // 医疗险:固定 800 万(对齐 Calculator 高端医疗)
  140. demand['医疗险'] = 800 * 10000 * 100; // ¥800万 → cents
  141. // 重疾险:主理人 = 年支出 × 5,其他 = 年支出 × 3
  142. demand['重疾险'] = isPrincipal ? annualExpenseCents * 5 : annualExpenseCents * 3;
  143. // 未成年/老人重疾险减半
  144. if (isMinor) demand['重疾险'] = annualExpenseCents * 1;
  145. if (isElder && !isPrincipal) demand['重疾险'] = annualExpenseCents * 2;
  146. // ── 🟨 中端风险层(按家庭情况)──
  147. // 定期寿险:仅主理人(收入替代,简化为年支出 × 10)
  148. if (isPrincipal) {
  149. demand['定期寿险'] = annualExpenseCents * 10;
  150. } else {
  151. demand['定期寿险'] = 0;
  152. }
  153. // 教育年金:18 岁以下子女 → 单份 800 万,无未成年子女 = 0
  154. if (isMinor) {
  155. demand['教育年金'] = 800 * 10000 * 100; // ¥800万
  156. } else {
  157. demand['教育年金'] = 0;
  158. }
  159. // 养老年金:仅主理人 + 配偶,55 岁以下需要规划
  160. if ((isPrincipal || member.role === 'spouse') && age < 65) {
  161. demand['养老年金'] = annualExpenseCents * 15; // 退休后 30 年 × 50%
  162. } else {
  163. demand['养老年金'] = 0;
  164. }
  165. // ── 🟡 高端风险层(按家族总资产)──
  166. // 终身寿险:仅主理人 = 总资产 × 30%(传承隔离基线)
  167. if (isPrincipal && totalAumYuan > 0) {
  168. demand['终身寿险'] = Math.round(totalAumYuan * 0.30) * 100; // 元 → cents
  169. } else {
  170. demand['终身寿险'] = 0;
  171. }
  172. return demand;
  173. }
  174. // 为整个家族计算所有成员的 demand
  175. function computeFamilyDemand(portrait) {
  176. if (!portrait || !portrait.members) return {};
  177. const family = portrait.family || {};
  178. const totalAumCents = (portrait.assets || []).reduce((s, a) => {
  179. const FX = { CNY: 1, USD: 7.0, HKD: 0.9, SGD: 5.2, GBP: 8.8, EUR: 7.5 };
  180. const rate = FX[a.currency] || 1;
  181. return s + (a.value_cents || 0) * rate;
  182. }, 0);
  183. const result = {};
  184. portrait.members.forEach(m => {
  185. result[m.id] = computeMemberDemand(m, family, totalAumCents);
  186. });
  187. return result;
  188. }
  189. // ════════════════════════════════════════════════════════════════════════
  190. // 5. 矩阵聚合:把 policies[] 聚合成 (member × dimension) → 既有保额
  191. // ════════════════════════════════════════════════════════════════════════
  192. function aggregatePolicyMatrix(portrait) {
  193. if (!portrait || !portrait.policies) return {};
  194. const matrix = {};
  195. portrait.policies.forEach(p => {
  196. const insuredId = p.insured_member_id;
  197. if (!insuredId) return;
  198. const dimension = mapPolicyToRiskDimension(p);
  199. if (!matrix[insuredId]) matrix[insuredId] = {};
  200. // 货币换算到 CNY cents
  201. const FX = { CNY: 1, USD: 7.0, HKD: 0.9, SGD: 5.2, GBP: 8.8, EUR: 7.5 };
  202. const rate = FX[p.currency] || 1;
  203. const faceCnyCents = (p.face_amount_cents || 0) * rate;
  204. matrix[insuredId][dimension] = (matrix[insuredId][dimension] || 0) + faceCnyCents;
  205. });
  206. return matrix;
  207. }
  208. // 计算每个被保人在每个险种上的"既有 / 需求 / 加保 / 缺口比"
  209. function computeMatrixCells(portrait) {
  210. const existing = aggregatePolicyMatrix(portrait);
  211. const demand = computeFamilyDemand(portrait);
  212. const cells = {};
  213. (portrait.members || []).forEach(m => {
  214. cells[m.id] = {};
  215. window.CY_RISK_DIMENSIONS.forEach(dim => {
  216. const exist = (existing[m.id] && existing[m.id][dim.key]) || 0;
  217. const need = (demand[m.id] && demand[m.id][dim.key]) || 0;
  218. const gap = Math.max(0, need - exist);
  219. const coverage = need > 0 ? Math.min(1, exist / need) : (exist > 0 ? 1 : null);
  220. cells[m.id][dim.key] = {
  221. existing_cents: exist,
  222. demand_cents: need,
  223. gap_cents: gap,
  224. coverage: coverage, // null = 不需要,0-1 = 覆盖率
  225. tier: dim.tier,
  226. tier_id: dim.tier_id
  227. };
  228. });
  229. });
  230. return cells;
  231. }
  232. // ════════════════════════════════════════════════════════════════════════
  233. // 6. 单元格颜色判定(矩阵着色)
  234. // ════════════════════════════════════════════════════════════════════════
  235. function getCellStatus(cell) {
  236. if (!cell || cell.coverage === null) return 'na'; // ⚪ 不需要
  237. if (cell.coverage >= 1) return 'full'; // 🟢 充足
  238. if (cell.coverage >= 0.5) return 'partial'; // 🟡 部分
  239. return 'gap'; // 🔴 缺口
  240. }
  241. // ════════════════════════════════════════════════════════════════════════
  242. // 7. 倒金字塔诊断(v2.2 §七.5 四档报警 · Jack 拍板阈值)
  243. // ════════════════════════════════════════════════════════════════════════
  244. function computeTierCoverage(memberCells) {
  245. // 输入:某成员的 cells (computeMatrixCells 结果中的一行)
  246. // 输出:三层平均覆盖率 [base, mid, high] (0-1)
  247. const tierSums = { base: { exist: 0, need: 0 }, mid: { exist: 0, need: 0 }, high: { exist: 0, need: 0 } };
  248. Object.keys(memberCells || {}).forEach(dimKey => {
  249. const c = memberCells[dimKey];
  250. if (!c || !c.tier_id) return;
  251. tierSums[c.tier_id].exist += c.existing_cents || 0;
  252. tierSums[c.tier_id].need += c.demand_cents || 0;
  253. });
  254. const cov = (s) => s.need > 0 ? Math.min(1, s.exist / s.need) : (s.exist > 0 ? 1 : 0);
  255. return {
  256. base: cov(tierSums.base),
  257. mid: cov(tierSums.mid),
  258. high: cov(tierSums.high),
  259. _raw: tierSums
  260. };
  261. }
  262. function diagnoseRiskPyramid(memberCells) {
  263. // v2.2 §七.5 四档报警阈值
  264. const cov = computeTierCoverage(memberCells);
  265. if (cov.base < 0.10 && cov.high > 0.80) {
  266. return {
  267. level: 'severe',
  268. color: '#8B0000',
  269. bg: '#FBE3E3',
  270. icon: '🚨',
  271. message: '严重倒金字塔',
  272. diagnosis: '底层完全空虚 + 顶层重金投入 · 极不平衡 · 建议立即补充基础层',
  273. coverage: cov
  274. };
  275. }
  276. if (cov.base < 0.30 && cov.high > 0.50) {
  277. return {
  278. level: 'warning',
  279. color: '#D32F2F',
  280. bg: '#FCE4E4',
  281. icon: '⚠️',
  282. message: '倒金字塔风险',
  283. diagnosis: '底层薄弱 + 顶层偏重 · 结构倒置 · 建议先夯实基础',
  284. coverage: cov
  285. };
  286. }
  287. if (cov.base < 0.50) {
  288. return {
  289. level: 'caution',
  290. color: '#F9A825',
  291. bg: '#FFF8E1',
  292. icon: '⚡',
  293. message: '底层偏弱',
  294. diagnosis: '基础层未达健康线(50%) · 建议优先补充意外/医疗/重疾',
  295. coverage: cov
  296. };
  297. }
  298. if (cov.base >= 0.60 && cov.mid >= 0.30) {
  299. return {
  300. level: 'healthy',
  301. color: '#2E7D32',
  302. bg: '#E8F5E9',
  303. icon: '✅',
  304. message: '结构合理',
  305. diagnosis: '金字塔结构合理 · 各层覆盖均衡',
  306. coverage: cov
  307. };
  308. }
  309. return {
  310. level: 'neutral',
  311. color: '#9E9E9E',
  312. bg: '#F5F5F5',
  313. icon: '○',
  314. message: '中性',
  315. diagnosis: '覆盖度数据已收集 · 等待诊断',
  316. coverage: cov
  317. };
  318. }
  319. // ════════════════════════════════════════════════════════════════════════
  320. // 8. 风险评分(顶部 0-10 直观分数)
  321. // ════════════════════════════════════════════════════════════════════════
  322. function computeFamilyRiskScore(allCells) {
  323. // 输入:所有成员的 cells
  324. // 输出:0-10 分(加权:基础 50% / 中端 30% / 高端 20%)
  325. if (!allCells || Object.keys(allCells).length === 0) return null;
  326. const tierWeights = { base: 0.50, mid: 0.30, high: 0.20 };
  327. let totalWeight = 0;
  328. let weightedCov = 0;
  329. Object.values(allCells).forEach(memberCells => {
  330. const cov = computeTierCoverage(memberCells);
  331. ['base', 'mid', 'high'].forEach(tier => {
  332. weightedCov += cov[tier] * tierWeights[tier];
  333. totalWeight += tierWeights[tier];
  334. });
  335. });
  336. const avg = totalWeight > 0 ? weightedCov / totalWeight : 0;
  337. return Math.round(avg * 100) / 10; // 0-10 一位小数
  338. }
  339. // ════════════════════════════════════════════════════════════════════════
  340. // 9. 格式化工具(对齐 cy-renderer.js 风格)
  341. // ════════════════════════════════════════════════════════════════════════
  342. function formatCnyCents(cents, opts) {
  343. if (cents == null) return '—';
  344. const yuan = cents / 100;
  345. if (opts && opts.unit === 'wan') {
  346. const wan = yuan / 10000;
  347. if (wan === 0) return '—';
  348. if (wan < 1) return '¥ ' + Math.round(yuan).toLocaleString('zh-CN') + '元';
  349. return Math.round(wan).toLocaleString('zh-CN') + ' 万';
  350. }
  351. if (yuan >= 100000000) return (yuan / 100000000).toFixed(2) + ' 亿';
  352. if (yuan >= 10000) return Math.round(yuan / 10000).toLocaleString('zh-CN') + ' 万';
  353. return Math.round(yuan).toLocaleString('zh-CN') + ' 元';
  354. }
  355. function formatGapBadge(cell) {
  356. // 单元格右上角 [+加保] 文案
  357. if (!cell || cell.coverage === null || cell.gap_cents === 0) return '';
  358. return '+' + formatCnyCents(cell.gap_cents, { unit: 'wan' });
  359. }
  360. // ════════════════════════════════════════════════════════════════════════
  361. // 10. 跨页跳转参数(PolicySummary +CTA → Calculator 自动预填)
  362. // ════════════════════════════════════════════════════════════════════════
  363. function buildCalculatorJumpURL(member, dimension, gapCents) {
  364. // 用 sessionStorage 存预填参数(目标页加载时读)
  365. try {
  366. sessionStorage.setItem('cy_calc_prefill', JSON.stringify({
  367. insured_member_id: member && member.id,
  368. insured_name: member && member.name_cn,
  369. risk_dimension: dimension,
  370. gap_cents: gapCents,
  371. timestamp: Date.now()
  372. }));
  373. sessionStorage.setItem('cy_toast', '已携带「' + (member && member.name_cn || '客户') + ' · ' + dimension + ' 缺口」跳转 Calculator');
  374. } catch(e) {}
  375. if (window.navJumpTo) {
  376. window.navJumpTo('insurance_calculator');
  377. } else {
  378. console.warn('[cy-policy-meta] navJumpTo 不可用,无法跳转');
  379. }
  380. }
  381. // ════════════════════════════════════════════════════════════════════════
  382. // 暴露 API
  383. // ════════════════════════════════════════════════════════════════════════
  384. window.CYPolicyMeta = {
  385. // 元数据
  386. TIERS: window.CY_RISK_TIERS,
  387. DIMENSIONS: window.CY_RISK_DIMENSIONS,
  388. // 映射
  389. mapPolicyToRiskDimension: mapPolicyToRiskDimension,
  390. getDimensionMeta: getDimensionMeta,
  391. getTierMeta: getTierMeta,
  392. // 计算
  393. computeMemberDemand: computeMemberDemand,
  394. computeFamilyDemand: computeFamilyDemand,
  395. aggregatePolicyMatrix: aggregatePolicyMatrix,
  396. computeMatrixCells: computeMatrixCells,
  397. getCellStatus: getCellStatus,
  398. computeTierCoverage: computeTierCoverage,
  399. diagnoseRiskPyramid: diagnoseRiskPyramid,
  400. computeFamilyRiskScore: computeFamilyRiskScore,
  401. // 格式化
  402. formatCnyCents: formatCnyCents,
  403. formatGapBadge: formatGapBadge,
  404. // 跳转
  405. buildCalculatorJumpURL: buildCalculatorJumpURL
  406. };
  407. console.log('[cy-policy-meta] ✓ 风险金字塔元数据加载完毕 · 9 险种 + 3 金字塔层 + 倒金字塔四档诊断');
  408. })();