refactor(money): 统一金额分为纯整数工具

This commit is contained in:
yml2213
2026-10-30 14:33:35 +08:00
parent 85bff95042
commit 37e1980d2d
7 changed files with 156 additions and 179 deletions
@@ -8,7 +8,6 @@ import (
"strings"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/pkg/money"
"gorm.io/datatypes"
)
@@ -462,7 +461,11 @@ func extractListingObjectKey(fileURL string) string {
return key
}
// roundMoney 使用统一的角精度(0.1元)
// roundMoney 是历史外部上传/审核链路的兼容实现,仍按 0.1 元精度工作。
// 卖家发布链路已改走整数分/基点计算,不再调用该函数。
func roundMoney(value float64) float64 {
return money.Round(value)
if value <= 0 || math.IsNaN(value) || math.IsInf(value, 0) {
return 0
}
return math.Round(value*10) / 10
}
+15 -7
View File
@@ -8,7 +8,6 @@ import (
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/modules/rentergrowth"
"hfb_sys/backend/pkg/money"
"gorm.io/datatypes"
)
@@ -486,23 +485,32 @@ func readOrderSnapshotAssetNumber(raw datatypes.JSON, key string) float64 {
return readJSONNumber(snapshot[key])
}
// roundMoney 使用统一的角精度(0.1元)
// roundMoney 是订单历史链路的角精度兼容实现;新金额链路应直接使用整数分。
func roundMoney(value float64) float64 {
return money.Round(value)
if value <= 0 || math.IsNaN(value) || math.IsInf(value, 0) {
return 0
}
return math.Round(value*10) / 10
}
func roundQuantity(value float64) float64 {
return math.Round(value*100) / 100
}
// minMoney 返回较小金额(角精度)
// minMoney 返回较小金额(角精度兼容)。
func minMoney(a float64, b float64) float64 {
return money.Min(a, b)
if a < b {
return a
}
return b
}
// maxMoney 返回较大金额(角精度)
// maxMoney 返回较大金额(角精度兼容)。
func maxMoney(a float64, b float64) float64 {
return money.Max(a, b)
if a > b {
return a
}
return b
}
func minCent(a int64, b int64) int64 {
@@ -10,7 +10,6 @@ import (
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/timeutil"
"hfb_sys/backend/pkg/money"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@@ -264,10 +263,6 @@ func applyEntry(account *model.WalletAccount, entry Entry) (int64, error) {
}
}
func roundWalletMoney(yuan float64) float64 {
return money.Round(yuan)
}
func toAccountDTO(account model.WalletAccount) *AccountDTO {
return &AccountDTO{
UserID: account.UserID,
@@ -53,10 +53,6 @@ func TestApplyEntryKeepsWalletMoneyAtJiaoPrecision(t *testing.T) {
if account.AvailableBalanceCent != 230 {
t.Fatalf("AvailableBalanceCent = %d, want 230", account.AvailableBalanceCent)
}
if rounded := roundWalletMoney(2.36); rounded != 2.4 {
t.Fatalf("roundWalletMoney(2.36) = %v, want 2.4", rounded)
}
}
func TestNewLedgerNoUsesReadableFormat(t *testing.T) {