fix: 月收支统计按日期排序

This commit is contained in:
BaoXuebin 2021-12-22 23:13:37 +08:00
parent 4533b73607
commit e0fe81e3a9
2 changed files with 19 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.idea .idea
.git .git
.vscode
bindata.go bindata.go
*.exe *.exe
gin.log gin.log

View File

@ -3,11 +3,13 @@ package service
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"sort"
"strings"
"time"
"github.com/beancount-gs/script" "github.com/beancount-gs/script"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
"sort"
"strings"
) )
type YearMonth struct { type YearMonth struct {
@ -222,6 +224,19 @@ type MonthTotal struct {
Amount json.Number `json:"amount"` Amount json.Number `json:"amount"`
OperatingCurrency string `json:"operatingCurrency"` 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) { func StatsMonthTotal(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c) ledgerConfig := script.GetLedgerConfigFromContext(c)
@ -289,6 +304,7 @@ func StatsMonthTotal(c *gin.Context) {
monthTotalResult = append(monthTotalResult, monthExpenses) monthTotalResult = append(monthTotalResult, monthExpenses)
monthTotalResult = append(monthTotalResult, MonthTotal{Type: "结余", Month: month, Amount: json.Number(monthIncomeAmount.Sub(monthExpensesAmount).Round(2).String()), OperatingCurrency: ledgerConfig.OperatingCurrency}) 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) OK(c, monthTotalResult)
} }