import { defineStore } from 'pinia' import { ref } from 'vue' import * as api from '@/api/stats' import { getCurrentMonth } from '@/utils/format' // 重新导出类型以保持向后兼容 export type Overview = api.Overview export type CategoryStat = api.CategoryStat export type TrendPoint = api.TrendPoint export const useStatsStore = defineStore('stats', () => { const overview = ref({ expense: 0, income: 0, count: 0, daily: 0 }) const categoryStats = ref([]) const trendData = ref([]) async function fetchOverview(month?: string) { try { const data = await api.getOverview({ month: month || getCurrentMonth() }) // 确保数据字段是数字类型 overview.value = { expense: Number(data?.expense) || 0, income: Number(data?.income) || 0, count: Number(data?.count) || 0, daily: Number(data?.daily) || 0 } } catch { overview.value = { expense: 0, income: 0, count: 0, daily: 0 } } } async function fetchCategoryStats(month?: string, type: string = 'expense') { try { const data = await api.getCategoryStats(month || getCurrentMonth(), type) // 确保返回的是数组,且金额是数字 categoryStats.value = Array.isArray(data) ? data.map(item => ({ ...item, amount: Number(item.amount) || 0, count: Number(item.count) || 0 })) : [] } catch { categoryStats.value = [] } } async function fetchTrend(month?: string, type: string = 'expense') { try { const data = await api.getTrend(month || getCurrentMonth(), type) // 确保返回的是数组,且金额是数字 trendData.value = Array.isArray(data) ? data.map(item => ({ ...item, amount: Number(item.amount) || 0 })) : [] } catch { trendData.value = [] } } return { overview, categoryStats, trendData, fetchOverview, fetchCategoryStats, fetchTrend } })