优化线下提号利润修改

This commit is contained in:
yml2213
2026-07-25 14:53:11 +08:00
parent 7ca68e6b8b
commit 9bb57b7094
10 changed files with 404 additions and 6 deletions
@@ -2,11 +2,16 @@ package pickup
import (
"encoding/json"
"errors"
"testing"
"hfb_sys/backend/internal/auditlog"
"hfb_sys/backend/internal/model"
"gorm.io/datatypes"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func TestBuildPickupPriceSnapshotSplitsOwnerPrice(t *testing.T) {
@@ -62,9 +67,137 @@ func TestBuildPickupPriceSnapshotFallsBackToSplitSum(t *testing.T) {
assertInt64(t, "网站加价兜底", snapshot.WebsiteProfitCent, 5000)
}
func TestRepositoryUpdateProfitAllowsPickingUp(t *testing.T) {
repo, db := newPickupTestRepo(t)
pickup := seedPickup(t, db, StatusPickingUp)
item, err := repo.UpdateProfit(t.Context(), pickup.ID, UpdateProfitRequest{
ProfitAmountCent: 2500,
Reason: "线下扣点调整",
}, 99, auditlog.Meta{RequestID: "req-profit"})
if err != nil {
t.Fatalf("UpdateProfit() error = %v", err)
}
assertInt64(t, "修改后利润", item.ProfitAmountCent, 2500)
var stored model.AdminPickup
if err := db.First(&stored, pickup.ID).Error; err != nil {
t.Fatalf("查询提号失败: %v", err)
}
assertInt64(t, "数据库利润", stored.ProfitAmountCent, 2500)
var audit model.AuditLog
if err := db.Where("action = ?", "pickup_profit_update").First(&audit).Error; err != nil {
t.Fatalf("查询审计日志失败: %v", err)
}
var detail map[string]any
if err := json.Unmarshal(audit.Detail, &detail); err != nil {
t.Fatalf("解析审计日志失败: %v", err)
}
assertJSONNumber(t, "旧利润", detail["old_profit_amount_cent"], 1000)
assertJSONNumber(t, "新利润", detail["new_profit_amount_cent"], 2500)
if detail["reason"] != "线下扣点调整" {
t.Fatalf("reason = %v, want 线下扣点调整", detail["reason"])
}
}
func TestRepositoryUpdateProfitRejectsCompleted(t *testing.T) {
repo, db := newPickupTestRepo(t)
pickup := seedPickup(t, db, StatusCompleted)
_, err := repo.UpdateProfit(t.Context(), pickup.ID, UpdateProfitRequest{ProfitAmountCent: 2500}, 99, auditlog.Meta{})
if !errors.Is(err, ErrPickupNotPickingUp) {
t.Fatalf("UpdateProfit() error = %v, want ErrPickupNotPickingUp", err)
}
var stored model.AdminPickup
if err := db.First(&stored, pickup.ID).Error; err != nil {
t.Fatalf("查询提号失败: %v", err)
}
assertInt64(t, "已完成提号利润", stored.ProfitAmountCent, 1000)
var auditCount int64
if err := db.Model(&model.AuditLog{}).Where("action = ?", "pickup_profit_update").Count(&auditCount).Error; err != nil {
t.Fatalf("统计审计日志失败: %v", err)
}
if auditCount != 0 {
t.Fatalf("审计日志数量 = %d, want 0", auditCount)
}
}
func assertInt64(t *testing.T, name string, got, want int64) {
t.Helper()
if got != want {
t.Fatalf("%s = %d, want %d", name, got, want)
}
}
func assertJSONNumber(t *testing.T, name string, got any, want float64) {
t.Helper()
value, ok := got.(float64)
if !ok || value != want {
t.Fatalf("%s = %v, want %.0f", name, got, want)
}
}
func newPickupTestRepo(t *testing.T) (*Repository, *gorm.DB) {
t.Helper()
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
t.Fatalf("打开测试数据库失败: %v", err)
}
if err := db.AutoMigrate(
&model.User{},
&model.GameAccount{},
&model.RentalListing{},
&model.AdminPickup{},
&model.AuditLog{},
); err != nil {
t.Fatalf("迁移测试表失败: %v", err)
}
return NewRepository(db), db
}
func seedPickup(t *testing.T, db *gorm.DB, status string) model.AdminPickup {
t.Helper()
owner := model.User{Phone: "18800000000"}
if err := db.Create(&owner).Error; err != nil {
t.Fatalf("创建号主失败: %v", err)
}
account := model.GameAccount{
OwnerID: owner.ID,
ServerRegion: "测试区",
LoginPlatform: "微信",
Title: "测试账号",
Status: "rented",
}
if err := db.Create(&account).Error; err != nil {
t.Fatalf("创建账号失败: %v", err)
}
listing := model.RentalListing{
ListingNo: "LST-PROFIT-001",
AccountID: account.ID,
OwnerID: owner.ID,
PriceCent: 10000,
Status: "rented",
ReviewStatus: "approved",
}
if err := db.Create(&listing).Error; err != nil {
t.Fatalf("创建上架记录失败: %v", err)
}
pickup := model.AdminPickup{
PickupNo: "PK-PROFIT-001",
ListingID: listing.ID,
AccountID: account.ID,
OwnerID: owner.ID,
AdminID: 1,
ProfitAmountCent: 1000,
Status: status,
}
if err := db.Create(&pickup).Error; err != nil {
t.Fatalf("创建提号记录失败: %v", err)
}
return pickup
}