优化订单支付

This commit is contained in:
yml
2026-06-08 23:56:25 +08:00
parent 870ea72996
commit a2a0489158
20 changed files with 884 additions and 5 deletions
@@ -3,11 +3,11 @@ package wallet
import (
"crypto/rand"
"fmt"
"math"
"time"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/timeutil"
"hfb_sys/backend/pkg/money"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@@ -260,7 +260,7 @@ func applyEntry(account *model.WalletAccount, entry Entry) (float64, error) {
}
func roundWalletMoney(value float64) float64 {
return math.Round(value)
return money.Round(value)
}
func toAccountDTO(account model.WalletAccount) *AccountDTO {
@@ -31,6 +31,34 @@ func TestApplyEntryRoundsMoneyBeforeComparing(t *testing.T) {
}
}
func TestApplyEntryKeepsWalletMoneyAtJiaoPrecision(t *testing.T) {
account := model.WalletAccount{
UserID: 8,
AvailableBalance: 0,
}
entry := Entry{
UserID: 8,
Direction: "in",
Amount: 2.30,
BalanceType: "available",
}
balanceAfter, err := applyEntry(&account, entry)
if err != nil {
t.Fatalf("applyEntry() error = %v", err)
}
if balanceAfter != 2.3 {
t.Fatalf("balanceAfter = %v, want 2.3", balanceAfter)
}
if account.AvailableBalance != 2.3 {
t.Fatalf("AvailableBalance = %v, want 2.3", account.AvailableBalance)
}
if rounded := roundWalletMoney(2.36); rounded != 2.4 {
t.Fatalf("roundWalletMoney(2.36) = %v, want 2.4", rounded)
}
}
func TestNewLedgerNoUsesReadableFormat(t *testing.T) {
ledgerNo, err := newLedgerNo()
if err != nil {