61 lines
4.5 KiB
TypeScript
61 lines
4.5 KiB
TypeScript
import type { SalaryDepartmentMember } from './salary-department-contract'
|
||
import type { SalaryOrganizationUnit } from './salary-organization-department'
|
||
|
||
export type MarketIntentExclusion = 'new-product' | 'immature-window' | 'zero-baseline' | 'unsupported-platform' | 'no-source-data' | 'invalid-intent'
|
||
export const MARKET_INTENT_EXCLUSION_LABELS: Record<MarketIntentExclusion, string> = {
|
||
'new-product': '需求类型为新品,不参与市场验证计分',
|
||
'immature-window': '完结后未满15个自然日',
|
||
'zero-baseline': '7日意向率均值为0',
|
||
'unsupported-platform': '平台未接入经营数据',
|
||
'no-source-data': '没有关联的经营数据',
|
||
'invalid-intent': '无法计算有效环比',
|
||
}
|
||
export type MarketIntentRecord = {
|
||
key: string; workflowId: string; approvalNumber: string | null; productId: string
|
||
brand: string; completedAt: string; intentRateChange: number
|
||
sevenDayIntentRate: number; fifteenDayIntentRate: number
|
||
demandType?: string | null
|
||
participants?: SalaryDepartmentMember[]
|
||
}
|
||
export type MarketIntentCandidate = Omit<MarketIntentRecord, 'intentRateChange' | 'sevenDayIntentRate' | 'fifteenDayIntentRate'> & {
|
||
intentRateChange: number | null; sevenDayIntentRate: number | null; fifteenDayIntentRate: number | null
|
||
invalidReason: MarketIntentExclusion | null
|
||
}
|
||
export type MarketIntentEvidenceRecord = MarketIntentCandidate & {
|
||
status: 'selected' | 'valid-not-selected' | 'excluded' | 'invalid'
|
||
}
|
||
export type MarketIntentEvidence = {
|
||
kind: 'market-intent-top3'; month: string; candidateCount: number; validCount: number
|
||
excludedCounts: Record<MarketIntentExclusion, number>
|
||
records: MarketIntentEvidenceRecord[]
|
||
selected: MarketIntentRecord[]; averageChange: number | null; rawScore: number | null
|
||
departmentScope?: { department: SalaryOrganizationUnit | null; members: SalaryDepartmentMember[] }
|
||
}
|
||
/** Input is a ratio: 0.0874 means 8.74%. Never round before averaging or weighting. */
|
||
export function marketIntentScore(change: number): number {
|
||
if (change < -0.2) return 0
|
||
if (change >= 0.2) return 120
|
||
return 100 + change * 100
|
||
}
|
||
export function summarizeMarketIntent(month: string, records: MarketIntentCandidate[]): MarketIntentEvidence {
|
||
const unique = Array.from(new Map(records.map(item => [item.key, item])).values())
|
||
const valid = unique.filter((item): item is MarketIntentCandidate & MarketIntentRecord => item.invalidReason === null)
|
||
.sort((a, b) => b.intentRateChange - a.intentRateChange || a.key.localeCompare(b.key))
|
||
const rejected = unique.filter(item => item.invalidReason !== null)
|
||
.sort((a, b) => Date.parse(b.completedAt) - Date.parse(a.completedAt) || a.key.localeCompare(b.key))
|
||
const newProducts = rejected.filter(item => item.invalidReason === 'new-product')
|
||
const invalid = rejected.filter(item => item.invalidReason !== 'new-product')
|
||
const selected = valid.slice(0, 3).map(({ invalidReason: _, ...item }) => item)
|
||
const excludedCounts: MarketIntentEvidence['excludedCounts'] = { 'new-product': 0, 'immature-window': 0, 'zero-baseline': 0, 'unsupported-platform': 0, 'no-source-data': 0, 'invalid-intent': 0 }
|
||
for (const item of rejected) excludedCounts[item.invalidReason!]++
|
||
const averageChange = selected.length ? selected.reduce((sum, item) => sum + item.intentRateChange, 0) / selected.length : null
|
||
return { kind: 'market-intent-top3', month, validCount: valid.length, candidateCount: unique.length,
|
||
records: [...valid.map((item, index): MarketIntentEvidenceRecord => ({ ...item, status: index < 3 ? 'selected' : 'valid-not-selected' })),
|
||
...newProducts.map((item): MarketIntentEvidenceRecord => ({ ...item, status: 'excluded' })),
|
||
...invalid.map((item): MarketIntentEvidenceRecord => ({ ...item, status: 'invalid' }))],
|
||
excludedCounts, selected, averageChange, rawScore: averageChange === null ? null : marketIntentScore(averageChange) }
|
||
}
|
||
export const MARKET_INTENT_SOURCE_LABEL = '业绩转化 · 上月最高3条意向率环比'
|
||
export const LEGACY_MARKET_INTENT_DESCRIPTION = '员工参与的当月已完结流程按流程×商品ID取最高3条有效意向率环比均值,不足3条按实际数量平均;低于−20%为0分,−20%至20%为100加环比百分点,20%及以上为120分。'
|
||
export const MARKET_INTENT_DESCRIPTION = '员工参与的薪资月上月已完结流程按流程×商品ID取最高3条有效意向率环比均值,不足3条按实际数量平均;完结后7日、15日观察窗口可跨月。低于−20%为0分,−20%至20%为100加环比百分点,20%及以上为120分。'
|