Files
sweet-hut-ui/src/utils/home/ledgerUtil.ts
2025-11-04 22:35:53 +08:00

29 lines
866 B
TypeScript

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<string, number>()
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)
}