import { ILedgerRecord } from '@/types/ledger.ts' import { IStatsChart } from 'vue3-common/types' export const statsByField = (records: ILedgerRecord[], field: keyof ILedgerRecord): IStatsChart[] => { const statsMap = new Map() records.forEach((record) => { const fieldValue = record[field] as string const filedStats = record.totalPrice as number const currentTotal = statsMap.get(fieldValue) || 0 // 计算当前字段对应的统计数据 statsMap.set(fieldValue, currentTotal + filedStats) }) return Array.from(statsMap.entries()) .map(([name, value]) => ({ name, value: Number(value.toFixed(2)) })) .sort((a, b) => b.value - a.value) .slice(0, 5) } export const getLedgerSortByPrice = (records: ILedgerRecord[]) => { return records.sort((a, b) => b?.totalPrice - a?.totalPrice) }