From 6edf66a5995c2155c8a75f3a1e12e0f55a5b1389 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 9 Jul 2023 14:22:47 +0800 Subject: [PATCH] code refactor --- src/stores/statistics.js | 112 ++++++++++++++++++ .../mobile/statistics/TransactionPage.vue | 111 +---------------- 2 files changed, 114 insertions(+), 109 deletions(-) diff --git a/src/stores/statistics.js b/src/stores/statistics.js index d5f7ecd0..2448dddb 100644 --- a/src/stores/statistics.js +++ b/src/stores/statistics.js @@ -302,6 +302,84 @@ export const useStatisticsStore = defineStore('statistics', { totalNonNegativeAmount: totalNonNegativeAmount, items: allDataItems } + }, + statisticsData(state) { + let combinedData = { + items: [], + totalAmount: 0 + }; + + if (state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseByAccount.type || + state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseByPrimaryCategory.type || + state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseBySecondaryCategory.type || + state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeByAccount.type || + state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeByPrimaryCategory.type || + state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeBySecondaryCategory.type) { + combinedData = state.statisticsItemsByTransactionStatisticsData; + } else if (state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.AccountTotalAssets.type || + state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.AccountTotalLiabilities.type) { + combinedData = state.statisticsItemsByAccountsData; + } + + const allStatisticsItems = []; + + for (let id in combinedData.items) { + if (!Object.prototype.hasOwnProperty.call(combinedData.items, id)) { + continue; + } + + const data = combinedData.items[id]; + + if (data.totalAmount > 0) { + data.percent = data.totalAmount * 100 / combinedData.totalNonNegativeAmount; + } else { + data.percent = 0; + } + + if (data.percent < 0) { + data.percent = 0; + } + + allStatisticsItems.push(data); + } + + if (state.transactionStatisticsFilter.sortingType === statisticsConstants.allSortingTypes.DisplayOrder.type) { + allStatisticsItems.sort(function (data1, data2) { + for (let i = 0; i < Math.min(data1.displayOrders.length, data2.displayOrders.length); i++) { + if (data1.displayOrders[i] !== data2.displayOrders[i]) { + return data1.displayOrders[i] - data2.displayOrders[i]; // asc + } + } + + return data1.name.localeCompare(data2.name, undefined, { // asc + numeric: true, + sensitivity: 'base' + }); + }); + } else if (state.transactionStatisticsFilter.sortingType === statisticsConstants.allSortingTypes.Name.type) { + allStatisticsItems.sort(function (data1, data2) { + return data1.name.localeCompare(data2.name, undefined, { // asc + numeric: true, + sensitivity: 'base' + }); + }); + } else { + allStatisticsItems.sort(function (data1, data2) { + if (data1.totalAmount !== data2.totalAmount) { + return data2.totalAmount - data1.totalAmount; // desc + } + + return data1.name.localeCompare(data2.name, undefined, { // asc + numeric: true, + sensitivity: 'base' + }); + }); + } + + return { + totalAmount: combinedData.totalAmount, + items: allStatisticsItems + }; } }, actions: { @@ -401,6 +479,40 @@ export const useStatisticsStore = defineStore('statistics', { this.transactionStatisticsFilter.sortingType = filter.sortingType; } }, + getTransactionListPageParams(item) { + const querys = []; + + if (this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeByAccount.type + || this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeByPrimaryCategory.type + || this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeBySecondaryCategory.type) { + querys.push('type=2'); + } else if (this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseByAccount.type + || this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseByPrimaryCategory.type + || this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseBySecondaryCategory.type) { + querys.push('type=3'); + } + + if (this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeByAccount.type + || this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseByAccount.type + || this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.AccountTotalAssets.type + || this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.AccountTotalLiabilities.type) { + querys.push('accountId=' + item.id); + } else if (this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeByPrimaryCategory.type + || this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeBySecondaryCategory.type + || this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseByPrimaryCategory.type + || this.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseBySecondaryCategory.type) { + querys.push('categoryId=' + item.id); + } + + if (this.transactionStatisticsFilter.chartDataType !== statisticsConstants.allChartDataTypes.AccountTotalAssets.type + && this.transactionStatisticsFilter.chartDataType !== statisticsConstants.allChartDataTypes.AccountTotalLiabilities.type) { + querys.push('dateType=' + this.transactionStatisticsFilter.dateType); + querys.push('minTime=' + this.transactionStatisticsFilter.startTime); + querys.push('maxTime=' + this.transactionStatisticsFilter.endTime); + } + + return querys.join('&'); + }, loadTransactionStatistics({ force }) { const self = this; diff --git a/src/views/mobile/statistics/TransactionPage.vue b/src/views/mobile/statistics/TransactionPage.vue index a4fcae6c..623a04f7 100644 --- a/src/views/mobile/statistics/TransactionPage.vue +++ b/src/views/mobile/statistics/TransactionPage.vue @@ -342,83 +342,7 @@ export default { return this.$t('Total Amount'); }, statisticsData() { - const self = this; - let combinedData = { - items: [], - totalAmount: 0 - }; - - if (self.query.chartDataType === self.allChartDataTypes.ExpenseByAccount.type || - self.query.chartDataType === self.allChartDataTypes.ExpenseByPrimaryCategory.type || - self.query.chartDataType === self.allChartDataTypes.ExpenseBySecondaryCategory.type || - self.query.chartDataType === self.allChartDataTypes.IncomeByAccount.type || - self.query.chartDataType === self.allChartDataTypes.IncomeByPrimaryCategory.type || - self.query.chartDataType === self.allChartDataTypes.IncomeBySecondaryCategory.type) { - combinedData = this.statisticsStore.statisticsItemsByTransactionStatisticsData; - } else if (self.query.chartDataType === self.allChartDataTypes.AccountTotalAssets.type || - self.query.chartDataType === self.allChartDataTypes.AccountTotalLiabilities.type) { - combinedData = this.statisticsStore.statisticsItemsByAccountsData; - } - - const allStatisticsItems = []; - - for (let id in combinedData.items) { - if (!Object.prototype.hasOwnProperty.call(combinedData.items, id)) { - continue; - } - - const data = combinedData.items[id]; - - if (data.totalAmount > 0) { - data.percent = data.totalAmount * 100 / combinedData.totalNonNegativeAmount; - } else { - data.percent = 0; - } - - if (data.percent < 0) { - data.percent = 0; - } - - allStatisticsItems.push(data); - } - - if (self.query.sortingType === this.allSortingTypes.DisplayOrder.type) { - allStatisticsItems.sort(function (data1, data2) { - for (let i = 0; i < Math.min(data1.displayOrders.length, data2.displayOrders.length); i++) { - if (data1.displayOrders[i] !== data2.displayOrders[i]) { - return data1.displayOrders[i] - data2.displayOrders[i]; // asc - } - } - - return data1.name.localeCompare(data2.name, undefined, { // asc - numeric: true, - sensitivity: 'base' - }); - }); - } else if (self.query.sortingType === this.allSortingTypes.Name.type) { - allStatisticsItems.sort(function (data1, data2) { - return data1.name.localeCompare(data2.name, undefined, { // asc - numeric: true, - sensitivity: 'base' - }); - }); - } else { - allStatisticsItems.sort(function (data1, data2) { - if (data1.totalAmount !== data2.totalAmount) { - return data2.totalAmount - data1.totalAmount; // desc - } - - return data1.name.localeCompare(data2.name, undefined, { // asc - numeric: true, - sensitivity: 'base' - }); - }); - } - - return { - totalAmount: combinedData.totalAmount, - items: allStatisticsItems - }; + return this.statisticsStore.statisticsData; }, showAmountInChart() { if (!this.showAccountBalance @@ -739,38 +663,7 @@ export default { return formatPercent(value, precision, lowPrecisionValue); }, getItemLinkUrl(item) { - const querys = []; - - if (this.query.chartDataType === this.allChartDataTypes.IncomeByAccount.type - || this.query.chartDataType === this.allChartDataTypes.IncomeByPrimaryCategory.type - || this.query.chartDataType === this.allChartDataTypes.IncomeBySecondaryCategory.type) { - querys.push('type=2'); - } else if (this.query.chartDataType === this.allChartDataTypes.ExpenseByAccount.type - || this.query.chartDataType === this.allChartDataTypes.ExpenseByPrimaryCategory.type - || this.query.chartDataType === this.allChartDataTypes.ExpenseBySecondaryCategory.type) { - querys.push('type=3'); - } - - if (this.query.chartDataType === this.allChartDataTypes.IncomeByAccount.type - || this.query.chartDataType === this.allChartDataTypes.ExpenseByAccount.type - || this.query.chartDataType === this.allChartDataTypes.AccountTotalAssets.type - || this.query.chartDataType === this.allChartDataTypes.AccountTotalLiabilities.type) { - querys.push('accountId=' + item.id); - } else if (this.query.chartDataType === this.allChartDataTypes.IncomeByPrimaryCategory.type - || this.query.chartDataType === this.allChartDataTypes.IncomeBySecondaryCategory.type - || this.query.chartDataType === this.allChartDataTypes.ExpenseByPrimaryCategory.type - || this.query.chartDataType === this.allChartDataTypes.ExpenseBySecondaryCategory.type) { - querys.push('categoryId=' + item.id); - } - - if (this.query.chartDataType !== this.allChartDataTypes.AccountTotalAssets.type - && this.query.chartDataType !== this.allChartDataTypes.AccountTotalLiabilities.type) { - querys.push('dateType=' + this.query.dateType); - querys.push('minTime=' + this.query.startTime); - querys.push('maxTime=' + this.query.endTime); - } - - return '/transaction/list?' + querys.join('&'); + return `/transaction/list?${this.statisticsStore.getTransactionListPageParams(item)}`; } } };