From e0fe81e3a9ce10afb6f2375d3352275956ef6588 Mon Sep 17 00:00:00 2001 From: BaoXuebin Date: Wed, 22 Dec 2021 23:13:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9C=88=E6=94=B6=E6=94=AF=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E6=8C=89=E6=97=A5=E6=9C=9F=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + service/stats.go | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 397d8eb..75c0798 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea .git +.vscode bindata.go *.exe gin.log diff --git a/service/stats.go b/service/stats.go index 3b6a59a..0c59e27 100644 --- a/service/stats.go +++ b/service/stats.go @@ -3,11 +3,13 @@ package service import ( "encoding/json" "fmt" + "sort" + "strings" + "time" + "github.com/beancount-gs/script" "github.com/gin-gonic/gin" "github.com/shopspring/decimal" - "sort" - "strings" ) type YearMonth struct { @@ -222,6 +224,19 @@ type MonthTotal struct { Amount json.Number `json:"amount"` OperatingCurrency string `json:"operatingCurrency"` } +type MonthTotalSort []MonthTotal + +func (s MonthTotalSort) Len() int { + return len(s) +} +func (s MonthTotalSort) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} +func (s MonthTotalSort) Less(i, j int) bool { + iYearMonth, _ := time.Parse("2006-1", s[i].Month) + jYearMonth, _ := time.Parse("2006-1", s[j].Month) + return iYearMonth.Before(jYearMonth) +} func StatsMonthTotal(c *gin.Context) { ledgerConfig := script.GetLedgerConfigFromContext(c) @@ -289,6 +304,7 @@ func StatsMonthTotal(c *gin.Context) { monthTotalResult = append(monthTotalResult, monthExpenses) monthTotalResult = append(monthTotalResult, MonthTotal{Type: "结余", Month: month, Amount: json.Number(monthIncomeAmount.Sub(monthExpensesAmount).Round(2).String()), OperatingCurrency: ledgerConfig.OperatingCurrency}) } + sort.Sort(MonthTotalSort(monthTotalResult)) OK(c, monthTotalResult) }