| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- /*!
- * 承延融合平台 v1.0 · 风险金字塔 + 9 险种共享元数据
- *
- * 用途:被 Calculator v3.0 / PolicySummary v1.0 / PolicyOrganizer v1.0 三方共享调用,
- * 提供风险金字塔三层结构、9 险种归类映射、需求保额简化计算、倒金字塔四档诊断。
- *
- * 引入方式:<script src="data/cy-policy-meta.js"></script>
- * 必须在 portraits-bundle.js / cy-renderer.js 之后引入。
- *
- * 立法依据:
- * - 错题本 #11 共享 JS:涉及 ≥3 HTML 必走 data/cy-*.js 模式
- * - 错题本 #9 双源数据:矩阵数字 100% 从 policies 实时算,绝不硬编码
- * - v2.2 实施方案 §三 + §七.5 倒金字塔四档报警阈值
- *
- * 暴露全局:
- * window.CY_RISK_TIERS · 三层金字塔元数据
- * window.CY_RISK_DIMENSIONS · 9 险种 → 三层映射
- * window.CYPolicyMeta.* · 工具函数集
- */
- (function () {
- 'use strict';
- // ════════════════════════════════════════════════════════════════════════
- // 1. 三层风险金字塔元数据(v2.1 §3.1)
- // ════════════════════════════════════════════════════════════════════════
- window.CY_RISK_TIERS = [
- {
- level: '基础风险',
- tier_id: 'base',
- type: '损失性风险',
- action: '进 / 防',
- problem: '没钱',
- color: '#E07A3B', // 🟧 基础橙
- color_light: '#FBE3D2',
- color_dark: '#9B4D1F',
- description: '意外、疾病、身故等不可预估的损失风险 · 解决"没钱"问题',
- population: '所有家庭 · 必配',
- risk_dimensions: ['意外险', '医疗险', '重疾险']
- },
- {
- level: '中端风险',
- tier_id: 'mid',
- type: '支出性风险',
- action: '出 / 增',
- problem: '花钱',
- color: '#D4A93A', // 🟨 中端浅金
- color_light: '#F5EBC8',
- color_dark: '#8B6F1A',
- description: '教育、养老等可预估的支出风险 + 主理人收入替代 · 解决"花钱"问题',
- population: '中产 + 中高净值',
- risk_dimensions: ['定期寿险', '教育年金', '养老年金']
- },
- {
- level: '高端风险',
- tier_id: 'high',
- type: '所有性风险',
- action: '保 / 传',
- problem: '留钱',
- color: '#7A3FA0', // 🟣 高端紫(v2026-06-14 与金字塔顶层 L1 对齐 · 原 #C9A84C 香槟金)
- color_light: '#EFE3F5',
- color_dark: '#4A1F70',
- description: '婚姻、税务、债务、传承等所有性风险 · 解决"留钱"问题',
- population: 'UHNW / 跨境配置',
- risk_dimensions: ['终身寿险']
- }
- ];
- // ════════════════════════════════════════════════════════════════════════
- // 2. 7 险种 → 三层 + 细分映射(v1.1 · 去掉护理险 / 满期金领取)
- // ════════════════════════════════════════════════════════════════════════
- window.CY_RISK_DIMENSIONS = [
- { key: '意外险', tier: '基础风险', tier_id: 'base', icon: '⚡', subcategories: ['意外伤害', '意外医疗'] },
- { key: '医疗险', tier: '基础风险', tier_id: 'base', icon: '🏥', subcategories: ['高端医疗', '国际医疗'] },
- { key: '重疾险', tier: '基础风险', tier_id: 'base', icon: '⚕', subcategories: ['轻中症', '重症', '重疾多次', '恶性肿瘤多次', '防癌'] },
- { key: '定期寿险', tier: '中端风险', tier_id: 'mid', icon: '🛡', subcategories: ['主理人收入替代'] },
- { key: '教育年金', tier: '中端风险', tier_id: 'mid', icon: '🎓', subcategories: ['子女教育'] },
- { key: '养老年金', tier: '中端风险', tier_id: 'mid', icon: '🌅', subcategories: ['养老支出'] },
- { key: '终身寿险', tier: '高端风险', tier_id: 'high', icon: '💎', subcategories: ['增额终身寿', '传统终身寿', 'IUL', 'VUL', 'PPLI'] }
- ];
- // ════════════════════════════════════════════════════════════════════════
- // 3. 兼容性映射:portraits-bundle.js 既有 product_type → 9 险种 key
- // ════════════════════════════════════════════════════════════════════════
- // 既有 portrait 的 product_type 用英文 enum,这里负责 mapping 到中文 9 险种
- // 不修改 portraits-bundle.js,只在读取时做转换
- const PRODUCT_TYPE_MAPPING = {
- 'PPLI': '终身寿险',
- 'VUL': '终身寿险',
- 'IUL': '终身寿险',
- 'whole_life': '终身寿险',
- 'term_life': '定期寿险',
- 'critical_illness': '重疾险',
- 'medical': '医疗险',
- 'international_health': '医疗险',
- 'health': '医疗险',
- 'accident': '意外险',
- 'long_term_care': '护理险',
- 'education_annuity': '教育年金',
- 'retirement_annuity': '养老年金',
- 'endowment': '满期金领取',
- 'maturity': '满期金领取'
- };
- function mapPolicyToRiskDimension(policy) {
- if (!policy) return null;
- // 优先用 risk_dimension 字段(新 portrait 直接给)
- if (policy.risk_dimension) return policy.risk_dimension;
- // fallback 到 product_type 映射
- const pt = (policy.product_type || '').toLowerCase();
- return PRODUCT_TYPE_MAPPING[policy.product_type] ||
- PRODUCT_TYPE_MAPPING[pt] ||
- '终身寿险'; // 兜底:未识别归到高端层(避免误算到基础层)
- }
- function getDimensionMeta(dimensionKey) {
- return window.CY_RISK_DIMENSIONS.find(d => d.key === dimensionKey) || null;
- }
- function getTierMeta(tierLevel) {
- return window.CY_RISK_TIERS.find(t => t.level === tierLevel || t.tier_id === tierLevel) || null;
- }
- // ════════════════════════════════════════════════════════════════════════
- // 4. 需求保额简化计算(Q4 决策:A4 Calculator 自动拉取作为基础值)
- // Phase 1 用简化版,Phase 2 升级 A3 智能推荐 + 代理人微调
- // ════════════════════════════════════════════════════════════════════════
- function computeMemberDemand(member, family, totalAumCents) {
- // 输入:family.members 中某成员 + family + 家族总资产(分单位)
- // 输出:9 险种 demand_cents(分单位 · 与 face_amount_cents 同单位)
- if (!member) return {};
- const age = member.age || 40;
- const isPrincipal = member.role === 'principal';
- const isMinor = member.age && member.age < 18;
- const isElder = age >= 60;
- // 年支出(优先 family.annual_expense_cny,否则按总资产 1.5% 估)
- const annualExpenseYuan = family && family.annual_expense_cny
- ? family.annual_expense_cny
- : Math.round((totalAumCents || 0) / 100 * 0.015);
- const annualExpenseCents = annualExpenseYuan * 100;
- // 家族总资产(元)
- const totalAumYuan = (totalAumCents || 0) / 100;
- const demand = {};
- // ── 🟧 基础风险层(每个家庭成员都需要)──
- // 意外险:年支出 × 5(覆盖 5 年家庭维持)
- demand['意外险'] = annualExpenseCents * 5;
- // 医疗险:固定 800 万(对齐 Calculator 高端医疗)
- demand['医疗险'] = 800 * 10000 * 100; // ¥800万 → cents
- // 重疾险:主理人 = 年支出 × 5,其他 = 年支出 × 3
- demand['重疾险'] = isPrincipal ? annualExpenseCents * 5 : annualExpenseCents * 3;
- // 未成年/老人重疾险减半
- if (isMinor) demand['重疾险'] = annualExpenseCents * 1;
- if (isElder && !isPrincipal) demand['重疾险'] = annualExpenseCents * 2;
- // ── 🟨 中端风险层(按家庭情况)──
- // 定期寿险:仅主理人(收入替代,简化为年支出 × 10)
- if (isPrincipal) {
- demand['定期寿险'] = annualExpenseCents * 10;
- } else {
- demand['定期寿险'] = 0;
- }
- // 教育年金:18 岁以下子女 → 单份 800 万,无未成年子女 = 0
- if (isMinor) {
- demand['教育年金'] = 800 * 10000 * 100; // ¥800万
- } else {
- demand['教育年金'] = 0;
- }
- // 养老年金:仅主理人 + 配偶,55 岁以下需要规划
- if ((isPrincipal || member.role === 'spouse') && age < 65) {
- demand['养老年金'] = annualExpenseCents * 15; // 退休后 30 年 × 50%
- } else {
- demand['养老年金'] = 0;
- }
- // ── 🟡 高端风险层(按家族总资产)──
- // 终身寿险:仅主理人 = 总资产 × 30%(传承隔离基线)
- if (isPrincipal && totalAumYuan > 0) {
- demand['终身寿险'] = Math.round(totalAumYuan * 0.30) * 100; // 元 → cents
- } else {
- demand['终身寿险'] = 0;
- }
- return demand;
- }
- // 为整个家族计算所有成员的 demand
- function computeFamilyDemand(portrait) {
- if (!portrait || !portrait.members) return {};
- const family = portrait.family || {};
- const totalAumCents = (portrait.assets || []).reduce((s, a) => {
- const FX = { CNY: 1, USD: 7.0, HKD: 0.9, SGD: 5.2, GBP: 8.8, EUR: 7.5 };
- const rate = FX[a.currency] || 1;
- return s + (a.value_cents || 0) * rate;
- }, 0);
- const result = {};
- portrait.members.forEach(m => {
- result[m.id] = computeMemberDemand(m, family, totalAumCents);
- });
- return result;
- }
- // ════════════════════════════════════════════════════════════════════════
- // 5. 矩阵聚合:把 policies[] 聚合成 (member × dimension) → 既有保额
- // ════════════════════════════════════════════════════════════════════════
- function aggregatePolicyMatrix(portrait) {
- if (!portrait || !portrait.policies) return {};
- const matrix = {};
- portrait.policies.forEach(p => {
- const insuredId = p.insured_member_id;
- if (!insuredId) return;
- const dimension = mapPolicyToRiskDimension(p);
- if (!matrix[insuredId]) matrix[insuredId] = {};
- // 货币换算到 CNY cents
- const FX = { CNY: 1, USD: 7.0, HKD: 0.9, SGD: 5.2, GBP: 8.8, EUR: 7.5 };
- const rate = FX[p.currency] || 1;
- const faceCnyCents = (p.face_amount_cents || 0) * rate;
- matrix[insuredId][dimension] = (matrix[insuredId][dimension] || 0) + faceCnyCents;
- });
- return matrix;
- }
- // 计算每个被保人在每个险种上的"既有 / 需求 / 加保 / 缺口比"
- function computeMatrixCells(portrait) {
- const existing = aggregatePolicyMatrix(portrait);
- const demand = computeFamilyDemand(portrait);
- const cells = {};
- (portrait.members || []).forEach(m => {
- cells[m.id] = {};
- window.CY_RISK_DIMENSIONS.forEach(dim => {
- const exist = (existing[m.id] && existing[m.id][dim.key]) || 0;
- const need = (demand[m.id] && demand[m.id][dim.key]) || 0;
- const gap = Math.max(0, need - exist);
- const coverage = need > 0 ? Math.min(1, exist / need) : (exist > 0 ? 1 : null);
- cells[m.id][dim.key] = {
- existing_cents: exist,
- demand_cents: need,
- gap_cents: gap,
- coverage: coverage, // null = 不需要,0-1 = 覆盖率
- tier: dim.tier,
- tier_id: dim.tier_id
- };
- });
- });
- return cells;
- }
- // ════════════════════════════════════════════════════════════════════════
- // 6. 单元格颜色判定(矩阵着色)
- // ════════════════════════════════════════════════════════════════════════
- function getCellStatus(cell) {
- if (!cell || cell.coverage === null) return 'na'; // ⚪ 不需要
- if (cell.coverage >= 1) return 'full'; // 🟢 充足
- if (cell.coverage >= 0.5) return 'partial'; // 🟡 部分
- return 'gap'; // 🔴 缺口
- }
- // ════════════════════════════════════════════════════════════════════════
- // 7. 倒金字塔诊断(v2.2 §七.5 四档报警 · Jack 拍板阈值)
- // ════════════════════════════════════════════════════════════════════════
- function computeTierCoverage(memberCells) {
- // 输入:某成员的 cells (computeMatrixCells 结果中的一行)
- // 输出:三层平均覆盖率 [base, mid, high] (0-1)
- const tierSums = { base: { exist: 0, need: 0 }, mid: { exist: 0, need: 0 }, high: { exist: 0, need: 0 } };
- Object.keys(memberCells || {}).forEach(dimKey => {
- const c = memberCells[dimKey];
- if (!c || !c.tier_id) return;
- tierSums[c.tier_id].exist += c.existing_cents || 0;
- tierSums[c.tier_id].need += c.demand_cents || 0;
- });
- const cov = (s) => s.need > 0 ? Math.min(1, s.exist / s.need) : (s.exist > 0 ? 1 : 0);
- return {
- base: cov(tierSums.base),
- mid: cov(tierSums.mid),
- high: cov(tierSums.high),
- _raw: tierSums
- };
- }
- function diagnoseRiskPyramid(memberCells) {
- // v2.2 §七.5 四档报警阈值
- const cov = computeTierCoverage(memberCells);
- if (cov.base < 0.10 && cov.high > 0.80) {
- return {
- level: 'severe',
- color: '#8B0000',
- bg: '#FBE3E3',
- icon: '🚨',
- message: '严重倒金字塔',
- diagnosis: '底层完全空虚 + 顶层重金投入 · 极不平衡 · 建议立即补充基础层',
- coverage: cov
- };
- }
- if (cov.base < 0.30 && cov.high > 0.50) {
- return {
- level: 'warning',
- color: '#D32F2F',
- bg: '#FCE4E4',
- icon: '⚠️',
- message: '倒金字塔风险',
- diagnosis: '底层薄弱 + 顶层偏重 · 结构倒置 · 建议先夯实基础',
- coverage: cov
- };
- }
- if (cov.base < 0.50) {
- return {
- level: 'caution',
- color: '#F9A825',
- bg: '#FFF8E1',
- icon: '⚡',
- message: '底层偏弱',
- diagnosis: '基础层未达健康线(50%) · 建议优先补充意外/医疗/重疾',
- coverage: cov
- };
- }
- if (cov.base >= 0.60 && cov.mid >= 0.30) {
- return {
- level: 'healthy',
- color: '#2E7D32',
- bg: '#E8F5E9',
- icon: '✅',
- message: '结构合理',
- diagnosis: '金字塔结构合理 · 各层覆盖均衡',
- coverage: cov
- };
- }
- return {
- level: 'neutral',
- color: '#9E9E9E',
- bg: '#F5F5F5',
- icon: '○',
- message: '中性',
- diagnosis: '覆盖度数据已收集 · 等待诊断',
- coverage: cov
- };
- }
- // ════════════════════════════════════════════════════════════════════════
- // 8. 风险评分(顶部 0-10 直观分数)
- // ════════════════════════════════════════════════════════════════════════
- function computeFamilyRiskScore(allCells) {
- // 输入:所有成员的 cells
- // 输出:0-10 分(加权:基础 50% / 中端 30% / 高端 20%)
- if (!allCells || Object.keys(allCells).length === 0) return null;
- const tierWeights = { base: 0.50, mid: 0.30, high: 0.20 };
- let totalWeight = 0;
- let weightedCov = 0;
- Object.values(allCells).forEach(memberCells => {
- const cov = computeTierCoverage(memberCells);
- ['base', 'mid', 'high'].forEach(tier => {
- weightedCov += cov[tier] * tierWeights[tier];
- totalWeight += tierWeights[tier];
- });
- });
- const avg = totalWeight > 0 ? weightedCov / totalWeight : 0;
- return Math.round(avg * 100) / 10; // 0-10 一位小数
- }
- // ════════════════════════════════════════════════════════════════════════
- // 9. 格式化工具(对齐 cy-renderer.js 风格)
- // ════════════════════════════════════════════════════════════════════════
- function formatCnyCents(cents, opts) {
- if (cents == null) return '—';
- const yuan = cents / 100;
- if (opts && opts.unit === 'wan') {
- const wan = yuan / 10000;
- if (wan === 0) return '—';
- if (wan < 1) return '¥ ' + Math.round(yuan).toLocaleString('zh-CN') + '元';
- return Math.round(wan).toLocaleString('zh-CN') + ' 万';
- }
- if (yuan >= 100000000) return (yuan / 100000000).toFixed(2) + ' 亿';
- if (yuan >= 10000) return Math.round(yuan / 10000).toLocaleString('zh-CN') + ' 万';
- return Math.round(yuan).toLocaleString('zh-CN') + ' 元';
- }
- function formatGapBadge(cell) {
- // 单元格右上角 [+加保] 文案
- if (!cell || cell.coverage === null || cell.gap_cents === 0) return '';
- return '+' + formatCnyCents(cell.gap_cents, { unit: 'wan' });
- }
- // ════════════════════════════════════════════════════════════════════════
- // 10. 跨页跳转参数(PolicySummary +CTA → Calculator 自动预填)
- // ════════════════════════════════════════════════════════════════════════
- function buildCalculatorJumpURL(member, dimension, gapCents) {
- // 用 sessionStorage 存预填参数(目标页加载时读)
- try {
- sessionStorage.setItem('cy_calc_prefill', JSON.stringify({
- insured_member_id: member && member.id,
- insured_name: member && member.name_cn,
- risk_dimension: dimension,
- gap_cents: gapCents,
- timestamp: Date.now()
- }));
- sessionStorage.setItem('cy_toast', '已携带「' + (member && member.name_cn || '客户') + ' · ' + dimension + ' 缺口」跳转 Calculator');
- } catch(e) {}
- if (window.navJumpTo) {
- window.navJumpTo('insurance_calculator');
- } else {
- console.warn('[cy-policy-meta] navJumpTo 不可用,无法跳转');
- }
- }
- // ════════════════════════════════════════════════════════════════════════
- // 暴露 API
- // ════════════════════════════════════════════════════════════════════════
- window.CYPolicyMeta = {
- // 元数据
- TIERS: window.CY_RISK_TIERS,
- DIMENSIONS: window.CY_RISK_DIMENSIONS,
- // 映射
- mapPolicyToRiskDimension: mapPolicyToRiskDimension,
- getDimensionMeta: getDimensionMeta,
- getTierMeta: getTierMeta,
- // 计算
- computeMemberDemand: computeMemberDemand,
- computeFamilyDemand: computeFamilyDemand,
- aggregatePolicyMatrix: aggregatePolicyMatrix,
- computeMatrixCells: computeMatrixCells,
- getCellStatus: getCellStatus,
- computeTierCoverage: computeTierCoverage,
- diagnoseRiskPyramid: diagnoseRiskPyramid,
- computeFamilyRiskScore: computeFamilyRiskScore,
- // 格式化
- formatCnyCents: formatCnyCents,
- formatGapBadge: formatGapBadge,
- // 跳转
- buildCalculatorJumpURL: buildCalculatorJumpURL
- };
- console.log('[cy-policy-meta] ✓ 风险金字塔元数据加载完毕 · 9 险种 + 3 金字塔层 + 倒金字塔四档诊断');
- })();
|