From 7870932a47ec408b2f7712c15c01b47d18a24741 Mon Sep 17 00:00:00 2001 From: BaoXuebin Date: Fri, 24 Dec 2021 11:55:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8D=9F=E7=9B=8A=E8=B4=A6=E6=88=B7=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E6=B7=BB=E5=8A=A0=E6=9C=88=E5=92=8C=E5=B9=B4=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E7=BB=B4=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/bql.go | 5 ++++- service/stats.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/script/bql.go b/script/bql.go index 3c75a7c..895e291 100644 --- a/script/bql.go +++ b/script/bql.go @@ -3,11 +3,13 @@ package script import ( "encoding/json" "fmt" - "github.com/gin-gonic/gin" + "log" "os/exec" "reflect" "strconv" "strings" + + "github.com/gin-gonic/gin" ) type QueryParams struct { @@ -96,6 +98,7 @@ func BQLQueryListByCustomSelect(ledgerConfig *Config, selectBql string, queryPar if err != nil { return err } + log.Println(output) err = parseResult(output, queryResultPtr, false) if err != nil { return err diff --git a/service/stats.go b/service/stats.go index 0c59e27..25671c7 100644 --- a/service/stats.go +++ b/service/stats.go @@ -138,8 +138,12 @@ func StatsAccountTrend(c *gin.Context) { Where: true, } var bql string - if statsQuery.Type == "avg" { + if statsQuery.Type == "day" { bql = fmt.Sprintf("SELECT '\\', date, '\\', sum(convert(value(position), '%s')), '\\'", ledgerConfig.OperatingCurrency) + } else if statsQuery.Type == "month" { + bql = fmt.Sprintf("SELECT '\\', year, '-', month, '\\', sum(convert(value(position), '%s')), '\\'", ledgerConfig.OperatingCurrency) + } else if statsQuery.Type == "year" { + bql = fmt.Sprintf("SELECT '\\', year, '\\', sum(convert(value(position), '%s')), '\\'", ledgerConfig.OperatingCurrency) } else if statsQuery.Type == "sum" { bql = fmt.Sprintf("SELECT '\\', date, '\\', convert(balance, '%s'), '\\'", ledgerConfig.OperatingCurrency) } else { @@ -156,9 +160,28 @@ func StatsAccountTrend(c *gin.Context) { result := make([]AccountTrendResult, 0) for _, stats := range statsResultList { - fields := strings.Fields(stats.Value) + commodities := strings.Split(stats.Value, ",") + // 多币种的处理方式:例如 75799.78 USD, 18500.00 IRAUSD, 176 VACHR + // 选择账本默认(ledgerConfig.OperatingCurrency)币种的值 + var selectedCommodity = commodities[0] + for _, commodity := range commodities { + if strings.Contains(commodity, " "+ledgerConfig.OperatingCurrency) { + selectedCommodity = commodity + break + } + } + + fields := strings.Fields(selectedCommodity) amount, _ := decimal.NewFromString(fields[0]) - result = append(result, AccountTrendResult{Date: stats.Key, Amount: json.Number(amount.Round(2).String()), OperatingCurrency: fields[1]}) + + var date = stats.Key + // 月格式化日期 + if statsQuery.Type == "month" { + yearMonth := strings.Split(date, "-") + date = fmt.Sprintf("%s-%s", strings.Trim(yearMonth[0], " "), strings.Trim(yearMonth[1], " ")) + } + + result = append(result, AccountTrendResult{Date: date, Amount: json.Number(amount.Round(2).String()), OperatingCurrency: fields[1]}) } OK(c, result) }