import { defineStore } from 'pinia' import { IBillBook, IBillCategory, IBillPay, IBillRecord, IBillType } from '@/types/bill' import { addBillRecordApi, deleteBillRecordApi, queryBillBookApi, queryBillCategoryApi, queryBillPayApi, queryBillRecordApi, queryBillStatsApi, queryBillSummaryApi, searchBillSummaryApi, updateBillRecordApi } from '@/apis/bill' import { formatDate, getCurrentMonth, getCurrentYear, getStartEndDate } from 'vue3-common/utils/dateUtil' import { IDateType, ImagePathEnum } from '@/types' import { IDialog, IStatsChart, IHandleApi } from 'vue3-common/types' import { ElMessage } from 'element-plus' export const useBillStore = defineStore('bill', { state: () => ({ billRecordList: [] as IBillRecord[], currentBillRecord: { id: 0, bookName: '', type: '', category: '', date: '', location: '', payAccount: '', amount: 0, content: '', remark: '', imageList: [] } as IBillRecord, billPayList: [] as IBillPay[], billBookList: [] as IBillBook[], billCategoryList: [] as IBillCategory[], billType: 'expensive' as IBillType, billDateType: 'month' as IDateType, billDateList: [] as string[], billImagePath: ImagePathEnum.BILL, billQueryForm: { billBook: 'all', billYear: getCurrentYear(), billMonth: getCurrentMonth() }, billSearchForm: { content: '', category: '', location: '' }, billDateStatsList: [] as IStatsChart[], billBookStatsList: [] as IStatsChart[], billCategoryStatsList: [] as IStatsChart[], billApiType: 'ADD' as IHandleApi, billDialog: { title: '', visible: false, data: {} } as IDialog, isRefresh: false, isLoading: false }), actions: { async initBillInfo() { await this.queryBillPay() await this.queryBillBook() await this.queryBillCategory() }, async queryBillPay() { this.billPayList = await queryBillPayApi() }, async queryBillBook() { this.billBookList = await queryBillBookApi() }, async queryBillCategory() { this.billCategoryList = await queryBillCategoryApi() }, async queryBillRecord(id: number) { this.currentBillRecord = await queryBillRecordApi(id) }, async queryBillSummary(bookName: string) { this.billRecordList = await queryBillSummaryApi({ bookName, type: '', startDate: this.billDateList[0], endDate: this.billDateList[1] }) }, async queryBillStats(bookName: string) { this.billDateStatsList = await queryBillStatsApi(this.billDateType, { bookName, type: this.billType, startDate: this.billDateList[0], endDate: this.billDateList[1] }) this.billBookStatsList = await queryBillStatsApi('book', { bookName: '', type: this.billType, startDate: this.billDateList[0], endDate: this.billDateList[1] }) this.billCategoryStatsList = await queryBillStatsApi('category', { bookName, type: this.billType, startDate: this.billDateList[0], endDate: this.billDateList[1] }) }, async handleBillApi(billRecord: IBillRecord) { switch (this.billApiType) { case 'ADD': await addBillRecordApi(billRecord) ElMessage.success('新增账单成功') break case 'UPDATE': await updateBillRecordApi(billRecord.id as number, billRecord) ElMessage.success('更新账单成功') break case 'DELETE': await deleteBillRecordApi(billRecord.id as number) ElMessage.success('删除账单成功') break default: break } this.billDialog.visible = false }, getBillDateList() { const { billYear, billMonth } = this.billQueryForm const date = this.billDateType === 'year' ? billYear : billMonth this.billDateList = getStartEndDate(date, this.billDateType) }, async refreshInfo(isStats = false) { this.isLoading = true const bookName = this.billQueryForm.billBook === 'all' ? '' : this.billQueryForm.billBook this.getBillDateList() if (isStats) { await this.queryBillStats(bookName) } else { await this.queryBillSummary(bookName) } this.isRefresh = !this.isRefresh this.isLoading = false }, async searchInfo() { this.isLoading = true this.billRecordList = await searchBillSummaryApi(this.billSearchForm) this.isLoading = false }, getEmptyBillRecord(): IBillRecord { return { amount: 0, bookName: '', category: '', content: '', date: formatDate(new Date()), location: '', payAccount: '', remark: '', imageList: [], type: '' } } } })