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 初意
parent a339e1afe8
commit 5159755109
1 changed files with 21 additions and 9 deletions

View File

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