export function pickField(obj, ...keys) { if (!obj) return undefined for (let i = 0; i < keys.length; i += 1) { const key = keys[i] if (obj[key] !== undefined && obj[key] !== null) return obj[key] } return undefined } export function formatMoney(val) { if (val === null || val === undefined || val === '') return '0.00' return Number(val).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) } export function formatNumber(val) { if (val === null || val === undefined || val === '') return '0' return Number(val).toLocaleString('zh-CN') } export function formatPercent(val, digits) { const d = digits == null ? 2 : digits return Number(val || 0).toFixed(d) } export function pad2(n) { return String(n).padStart(2, '0') } /** 默认区间:当月 1 日 → 今天 */ export function getDefaultDateRange() { const now = new Date() const year = now.getFullYear() const month = now.getMonth() + 1 const day = now.getDate() return [`${year}-${pad2(month)}-01`, `${year}-${pad2(month)}-${pad2(day)}`] } export function dateToStatisticsMonth(dateStr) { if (!dateStr) return '' return String(dateStr).replace(/-/g, '').slice(0, 6) } export function barWidth(value, max, minPercent = 8) { const m = Number(max) || 0 const v = Number(value) || 0 if (m <= 0) return minPercent return Math.max(minPercent, Math.round((v / m) * 1000) / 10) } export function dealTypeTone(name) { const n = String(name || '') if (n.indexOf('女神') >= 0) return 'red' if (n.indexOf('其他') >= 0) return 'blue' if (n.indexOf('科美') >= 0) return 'purple' if (n.indexOf('医美') >= 0) return 'pink' if (n.indexOf('生美') >= 0) return 'blue' return 'gray' } export function dealTypeHint(name) { const n = String(name || '') if (n.indexOf('女神') >= 0) return '女神卡引流卡成交' if (n.indexOf('其他') >= 0) return '其他生美项目成交' if (n.indexOf('科美') >= 0) return '科美项目成交' if (n.indexOf('医美') >= 0) return '医美项目成交' return '' } export function isFunnelPayload(data) { if (!data || typeof data !== 'object') return false return !!( pickField(data, 'SummaryKpis', 'summaryKpis') || pickField(data, 'FunnelSteps', 'funnelSteps') || pickField(data, 'LifeBeautySplit', 'lifeBeautySplit') || pickField(data, 'GoddessUpgradeFlow', 'goddessUpgradeFlow') ) } export function unwrapAcquisitionFunnel(res) { const candidates = [res, res && res.data, res && res.data && res.data.data] for (let i = 0; i < candidates.length; i += 1) { if (isFunnelPayload(candidates[i])) return candidates[i] } if (res && res.data && typeof res.data === 'object') return res.data return res && typeof res === 'object' ? res : {} } export function normalizeAcquisitionFunnel(data) { const payload = unwrapAcquisitionFunnel(data) if (!payload || typeof payload !== 'object') { return { summaryKpis: {}, funnelSteps: [], dropOffs: {}, dealTypeDistribution: [], lifeBeautySplit: {}, dealTypeUpgradeRates: [], goddessUpgradeFlow: {}, goddessUpgradeTopItems: [], otherLifeUpgradeFlow: {}, otherLifeUpgradeTopItems: [], techMedicalSupplement: {}, metricNote: '', conclusions: [] } } const techMedical = pickField(payload, 'TechMedicalSupplement', 'techMedicalSupplement') || {} const conclusions = pickField(payload, 'Conclusions', 'conclusions') || [] return { summaryKpis: pickField(payload, 'SummaryKpis', 'summaryKpis') || {}, funnelSteps: pickField(payload, 'FunnelSteps', 'funnelSteps') || [], dropOffs: pickField(payload, 'DropOffs', 'dropOffs') || {}, dealTypeDistribution: pickField(payload, 'DealTypeDistribution', 'dealTypeDistribution') || [], lifeBeautySplit: pickField(payload, 'LifeBeautySplit', 'lifeBeautySplit') || {}, dealTypeUpgradeRates: pickField(payload, 'DealTypeUpgradeRates', 'dealTypeUpgradeRates') || [], goddessUpgradeFlow: pickField(payload, 'GoddessUpgradeFlow', 'goddessUpgradeFlow') || {}, goddessUpgradeTopItems: pickField(payload, 'GoddessUpgradeTopItems', 'goddessUpgradeTopItems') || [], otherLifeUpgradeFlow: pickField(payload, 'OtherLifeUpgradeFlow', 'otherLifeUpgradeFlow') || {}, otherLifeUpgradeTopItems: pickField(payload, 'OtherLifeUpgradeTopItems', 'otherLifeUpgradeTopItems') || [], techMedicalSupplement: { ...techMedical, techUpgradeTopItems: pickField(techMedical, 'TechUpgradeTopItems', 'techUpgradeTopItems') || [], medicalUpgradeTopItems: pickField(techMedical, 'MedicalUpgradeTopItems', 'medicalUpgradeTopItems') || [] }, metricNote: pickField(payload, 'MetricNote', 'metricNote') || '', conclusions: Array.isArray(conclusions) ? conclusions.filter(Boolean) : [] } }