Update stats.go

商户消费排行某个月份返回无效值 解决方案  [#14 ](https://github.com/BaoXuebin/beancount-gs/issues/14)
This commit is contained in:
RenLangMan 2024-03-08 13:00:30 +08:00 committed by GitHub
parent a339e1afe8
commit 9183b3e402
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 9 deletions

View File

@ -442,25 +442,37 @@ func StatsPayee(c *gin.Context) {
result := make([]StatsPayeeResult, 0) result := make([]StatsPayeeResult, 0)
for _, l := range statsPayeeQueryResultList { for _, l := range statsPayeeQueryResultList {
// 交易账户名称非空
if l.Payee != "" { if l.Payee != "" {
payee := StatsPayeeResult{ payee := StatsPayeeResult{
Payee: l.Payee, Payee: l.Payee,
Currency: ledgerConfig.OperatingCurrency, Currency: ledgerConfig.OperatingCurrency,
} }
//查询交易次数
if statsQuery.Type == "cot" { if statsQuery.Type == "cot" {
payee.Value = json.Number(decimal.NewFromInt32(l.Count).String()) payee.Value = json.Number(decimal.NewFromInt32(l.Count).String())
} else { } else {
//查询交易金额,要过滤掉空白交易金额的科目,
// 比如 记账购买后又全额退款导致科目交易条目数>0但是累计金额=0
if l.Position != "" {
// 读取交易金额相关信息
fields := strings.Fields(l.Position) fields := strings.Fields(l.Position)
// 交易金额
total, err := decimal.NewFromString(fields[0]) total, err := decimal.NewFromString(fields[0])
// 错误处理
if err != nil { if err != nil {
panic(err) panic(err)
} }
if statsQuery.Type == "avg" { if statsQuery.Type == "avg" {
// 如果是查询平均交易金额
payee.Value = json.Number(total.Div(decimal.NewFromInt32(l.Count)).Round(2).String()) payee.Value = json.Number(total.Div(decimal.NewFromInt32(l.Count)).Round(2).String())
} else { } else {
// 如果是查询总交易金额
payee.Value = json.Number(fields[0]) payee.Value = json.Number(fields[0])
} }
} }
}
result = append(result, payee) result = append(result, payee)
} }
} }