From d91869319ab114d24ba8ce66758ce60ea9c7ad71 Mon Sep 17 00:00:00 2001 From: BaoXuebin Date: Thu, 25 Nov 2021 15:41:28 +0800 Subject: [PATCH] add month stats api --- script/bql.go | 11 +++++-- server.go | 2 ++ service/stats.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/script/bql.go b/script/bql.go index e2a3653..10387df 100644 --- a/script/bql.go +++ b/script/bql.go @@ -100,9 +100,9 @@ func BQLQueryListByCustomSelect(ledgerConfig *Config, selectBql string, queryPar } func bqlRawQuery(ledgerConfig *Config, selectBql string, queryParamsPtr *QueryParams, queryResultPtr interface{}) (string, error) { - bql := "" + var bql string if selectBql == "" { - bql = "SELECT" + bql = "select" queryResultPtrType := reflect.TypeOf(queryResultPtr) queryResultType := queryResultPtrType.Elem() @@ -196,6 +196,13 @@ func parseResult(output string, queryResultPtr interface{}, selectOne bool) erro jsonName = field.Name } switch field.Type.Kind() { + case reflect.Int: + i, err := strconv.Atoi(strings.Trim(val, " ")) + if err != nil { + panic(err) + } + temp[jsonName] = i + break // decimal case reflect.String, reflect.Struct: v := strings.Trim(val, " ") diff --git a/server.go b/server.go index 5791194..97cf4c0 100644 --- a/server.go +++ b/server.go @@ -56,6 +56,7 @@ func RegisterRouter(router *gin.Engine) { authorized.GET("/stats/total", service.StatsTotal) authorized.GET("/stats/account/percent", service.StatsAccountPercent) authorized.GET("/stats/account/trend", service.StatsAccountTrend) + authorized.GET("/stats/month/total", service.StatsMonthTotal) authorized.GET("/transaction", service.QueryTransactions) authorized.GET("/transaction/payee", service.QueryTransactionPayees) authorized.GET("/transaction/template", service.QueryTransactionTemplates) @@ -67,6 +68,7 @@ func RegisterRouter(router *gin.Engine) { // 兼容旧版本 authorized.GET("/entry", service.QueryTransactions) authorized.GET("/payee", service.QueryTransactionPayees) + authorized.GET("/stats/month/incomeExpenses", service.StatsMonthTotal) } } diff --git a/service/stats.go b/service/stats.go index 0454c13..a717833 100644 --- a/service/stats.go +++ b/service/stats.go @@ -151,3 +151,82 @@ func StatsAccountTrend(c *gin.Context) { } OK(c, result) } + +type MonthTotalBQLResult struct { + Year int + Month int + Value string +} + +type MonthTotal struct { + Type string `json:"type"` + Month string `json:"month"` + Amount float64 `json:"amount"` + OperatingCurrency string `json:"operatingCurrency"` +} + +func StatsMonthTotal(c *gin.Context) { + ledgerConfig := script.GetLedgerConfigFromContext(c) + + monthSet := make(map[string]bool) + queryParams := script.QueryParams{ + AccountLike: "Income", + Where: true, + OrderBy: "year, month", + } + // 按月查询收入 + queryIncomeBql := fmt.Sprintf("select '\\', year, '\\', month, '\\', neg(sum(convert(value(position), '%s'))), '\\'", ledgerConfig.OperatingCurrency) + monthIncomeTotalResultList := make([]MonthTotalBQLResult, 0) + err := script.BQLQueryListByCustomSelect(ledgerConfig, queryIncomeBql, &queryParams, &monthIncomeTotalResultList) + if err != nil { + InternalError(c, err.Error()) + return + } + monthIncomeMap := make(map[string]MonthTotalBQLResult) + for _, income := range monthIncomeTotalResultList { + month := fmt.Sprintf("%d-%d", income.Year, income.Month) + monthSet[month] = true + monthIncomeMap[month] = income + } + + // 按月查询支出 + queryParams.AccountLike = "Expenses" + queryExpensesBql := fmt.Sprintf("select '\\', year, '\\', month, '\\', sum(convert(value(position), '%s')), '\\'", ledgerConfig.OperatingCurrency) + monthExpensesTotalResultList := make([]MonthTotalBQLResult, 0) + err = script.BQLQueryListByCustomSelect(ledgerConfig, queryExpensesBql, &queryParams, &monthExpensesTotalResultList) + if err != nil { + InternalError(c, err.Error()) + return + } + monthExpensesMap := make(map[string]MonthTotalBQLResult) + for _, expenses := range monthExpensesTotalResultList { + month := fmt.Sprintf("%d-%d", expenses.Year, expenses.Month) + monthSet[month] = true + monthExpensesMap[month] = expenses + } + + monthTotalResult := make([]MonthTotal, 0) + // 合并结果 + var monthIncome, monthExpenses MonthTotal + for month := range monthSet { + if monthIncomeMap[month].Value != "" { + fields := strings.Fields(monthIncomeMap[month].Value) + amount, _ := strconv.ParseFloat(fields[0], 64) + monthIncome = MonthTotal{Type: "收入", Month: month, Amount: amount, OperatingCurrency: fields[1]} + } else { + monthIncome = MonthTotal{Type: "收入", Month: month, Amount: 0, OperatingCurrency: ledgerConfig.OperatingCurrency} + } + monthTotalResult = append(monthTotalResult, monthIncome) + + if monthExpensesMap[month].Value != "" { + fields := strings.Fields(monthExpensesMap[month].Value) + amount, _ := strconv.ParseFloat(fields[0], 64) + monthExpenses = MonthTotal{Type: "支出", Month: month, Amount: amount, OperatingCurrency: fields[1]} + } else { + monthExpenses = MonthTotal{Type: "支出", Month: month, Amount: 0, OperatingCurrency: ledgerConfig.OperatingCurrency} + } + monthTotalResult = append(monthTotalResult, monthExpenses) + monthTotalResult = append(monthTotalResult, MonthTotal{Type: "结余", Month: month, Amount: monthIncome.Amount - monthExpenses.Amount, OperatingCurrency: ledgerConfig.OperatingCurrency}) + } + OK(c, monthTotalResult) +}