396 lines
13 KiB
Go
396 lines
13 KiB
Go
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) {
|
|
raw, err := json.Marshal(map[string]any{
|
|
"price_breakdown": map[string]any{
|
|
"seller_coin_base_price": 233,
|
|
"seller_total_price": 353,
|
|
"platform_markup_amount": 30,
|
|
"consumable_price": 120,
|
|
"seller_ratio": 42.9,
|
|
"buyer_ratio": 35.1,
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("构造资产快照失败: %v", err)
|
|
}
|
|
|
|
snapshot := buildPickupPriceSnapshot(
|
|
model.RentalListing{PriceCent: 38300},
|
|
model.GameAccount{HafCoinAmount: 100000000, AssetSummary: datatypes.JSON(raw)},
|
|
)
|
|
|
|
assertInt64(t, "网站售价", snapshot.ListingPriceCent, 38300)
|
|
assertInt64(t, "号主纯比价格", snapshot.OwnerPurePriceCent, 23300)
|
|
assertInt64(t, "额外物品价格", snapshot.OwnerExtraItemPriceCent, 12000)
|
|
assertInt64(t, "号主总价", snapshot.OwnerTotalPriceCent, 35300)
|
|
assertInt64(t, "兼容号主价", snapshot.OwnerPriceCent, 35300)
|
|
assertInt64(t, "网站加价", snapshot.WebsiteProfitCent, 3000)
|
|
if snapshot.SellerRatio != 42.9 || snapshot.BuyerRatio != 35.1 {
|
|
t.Fatalf("比例 = %.2f/%.2f, want 42.90/35.10", snapshot.SellerRatio, snapshot.BuyerRatio)
|
|
}
|
|
}
|
|
|
|
func TestBuildPickupPriceSnapshotFallsBackToSplitSum(t *testing.T) {
|
|
raw, err := json.Marshal(map[string]any{
|
|
"price_breakdown": map[string]any{
|
|
"seller_coin_base_price": 200,
|
|
"consumable_price": 50,
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("构造资产快照失败: %v", err)
|
|
}
|
|
|
|
snapshot := buildPickupPriceSnapshot(
|
|
model.RentalListing{PriceCent: 30000},
|
|
model.GameAccount{AssetSummary: datatypes.JSON(raw)},
|
|
)
|
|
|
|
assertInt64(t, "拆分合计总价", snapshot.OwnerTotalPriceCent, 25000)
|
|
assertInt64(t, "纯比价格", snapshot.OwnerPurePriceCent, 20000)
|
|
assertInt64(t, "额外物品价格", snapshot.OwnerExtraItemPriceCent, 5000)
|
|
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 TestRepositoryListAdminFiltersShopName(t *testing.T) {
|
|
repo, db := newPickupTestRepo(t)
|
|
first := seedPickup(t, db, StatusPickingUp)
|
|
if err := db.Model(&first).Updates(map[string]any{
|
|
"platform": "淘宝",
|
|
"shop_name": "大锤一店",
|
|
}).Error; err != nil {
|
|
t.Fatalf("设置第一条提号店铺失败: %v", err)
|
|
}
|
|
second := first
|
|
second.ID = 0
|
|
second.PickupNo = "PK-PROFIT-002"
|
|
second.ShopName = "大锤二店"
|
|
if err := db.Create(&second).Error; err != nil {
|
|
t.Fatalf("创建第二条提号记录失败: %v", err)
|
|
}
|
|
|
|
result, err := repo.ListAdmin(t.Context(), AdminPickupQuery{ShopName: " 大锤一店 "})
|
|
if err != nil {
|
|
t.Fatalf("ListAdmin() error = %v", err)
|
|
}
|
|
if result.Total != 1 || len(result.Items) != 1 {
|
|
t.Fatalf("店铺筛选结果 = total %d, items %d, want 1", result.Total, len(result.Items))
|
|
}
|
|
if result.Items[0].ShopName != "大锤一店" {
|
|
t.Fatalf("店铺名称 = %q, want 大锤一店", result.Items[0].ShopName)
|
|
}
|
|
}
|
|
|
|
func TestRepositoryListShopNamesReturnsDistinctPlatformOptions(t *testing.T) {
|
|
repo, db := newPickupTestRepo(t)
|
|
first := seedPickup(t, db, StatusPickingUp)
|
|
if err := db.Model(&first).Updates(map[string]any{
|
|
"platform": "淘宝",
|
|
"shop_name": "大锤一店",
|
|
}).Error; err != nil {
|
|
t.Fatalf("设置第一条提号店铺失败: %v", err)
|
|
}
|
|
for index, row := range []model.AdminPickup{
|
|
{PickupNo: "PK-SHOP-002", ListingID: first.ListingID, AccountID: first.AccountID, OwnerID: first.OwnerID, AdminID: 1, Platform: "淘宝", ShopName: "大锤一店", Status: StatusPickingUp},
|
|
{PickupNo: "PK-SHOP-003", ListingID: first.ListingID, AccountID: first.AccountID, OwnerID: first.OwnerID, AdminID: 1, Platform: "拼多多", ShopName: "大锤二店", Status: StatusPickingUp},
|
|
} {
|
|
if err := db.Create(&row).Error; err != nil {
|
|
t.Fatalf("创建第 %d 条店铺选项失败: %v", index+2, err)
|
|
}
|
|
}
|
|
|
|
names, err := repo.ListShopNames(t.Context(), "淘宝")
|
|
if err != nil {
|
|
t.Fatalf("ListShopNames() error = %v", err)
|
|
}
|
|
if len(names) != 1 || names[0] != "大锤一店" {
|
|
t.Fatalf("淘宝店铺选项 = %v, want [大锤一店]", names)
|
|
}
|
|
}
|
|
|
|
func TestRepositoryListAvailableListingsFiltersAccountSource(t *testing.T) {
|
|
repo, db := newPickupTestRepo(t)
|
|
internal := seedAvailableListing(t, db, "LST-SOURCE-INTERNAL", "站内账号", false)
|
|
external := seedAvailableListing(t, db, "LST-SOURCE-EXTERNAL", "外部账号", true)
|
|
if err := db.Create(&model.ListingUpload{
|
|
UploaderName: "外部号主",
|
|
SourceChannel: "淘宝",
|
|
ListingID: &external.ID,
|
|
Status: "listing_created",
|
|
}).Error; err != nil {
|
|
t.Fatalf("创建外部上传记录失败: %v", err)
|
|
}
|
|
|
|
all, err := repo.ListAvailableListings(t.Context(), AvailableListingQuery{Page: 1, PageSize: 20})
|
|
if err != nil {
|
|
t.Fatalf("ListAvailableListings() error = %v", err)
|
|
}
|
|
if all.Total != 2 || len(all.Items) != 2 {
|
|
t.Fatalf("全部来源结果 = total %d, items %d, want 2", all.Total, len(all.Items))
|
|
}
|
|
|
|
externalResult, err := repo.ListAvailableListings(t.Context(), AvailableListingQuery{
|
|
SourceType: ListingSourceExternal,
|
|
Page: 1,
|
|
PageSize: 20,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("筛选外部上传 error = %v", err)
|
|
}
|
|
if externalResult.Total != 1 || len(externalResult.Items) != 1 {
|
|
t.Fatalf("外部上传结果 = total %d, items %d, want 1", externalResult.Total, len(externalResult.Items))
|
|
}
|
|
externalItem := externalResult.Items[0]
|
|
if externalItem.ID != external.ID || !externalItem.IsExternalUpload {
|
|
t.Fatalf("外部上传账号 = id %d, external %t, want id %d", externalItem.ID, externalItem.IsExternalUpload, external.ID)
|
|
}
|
|
if externalItem.AccountSource != AccountSourceExternalPlatformManaged || externalItem.SourceChannel != "淘宝" {
|
|
t.Fatalf("外部上传来源 = %q/%q", externalItem.AccountSource, externalItem.SourceChannel)
|
|
}
|
|
if externalItem.HandoffMode != "platform" || externalItem.SettlementMode != "platform_managed" {
|
|
t.Fatalf("外部上传代管模式 = %q/%q", externalItem.HandoffMode, externalItem.SettlementMode)
|
|
}
|
|
|
|
internalResult, err := repo.ListAvailableListings(t.Context(), AvailableListingQuery{
|
|
SourceType: ListingSourceInternal,
|
|
Page: 1,
|
|
PageSize: 20,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("筛选站内上传 error = %v", err)
|
|
}
|
|
if internalResult.Total != 1 || len(internalResult.Items) != 1 {
|
|
t.Fatalf("站内上传结果 = total %d, items %d, want 1", internalResult.Total, len(internalResult.Items))
|
|
}
|
|
internalItem := internalResult.Items[0]
|
|
if internalItem.ID != internal.ID || internalItem.IsExternalUpload || internalItem.AccountSource != AccountSourceInternal {
|
|
t.Fatalf("站内上传账号 = id %d, external %t, source %q", internalItem.ID, internalItem.IsExternalUpload, internalItem.AccountSource)
|
|
}
|
|
}
|
|
|
|
func TestRepositoryCreateSnapshotsExternalAccountSource(t *testing.T) {
|
|
repo, db := newPickupTestRepo(t)
|
|
listing := seedAvailableListing(t, db, "LST-SOURCE-CREATE", "待提号外部账号", true)
|
|
upload := model.ListingUpload{
|
|
UploaderName: "外部号主",
|
|
SourceChannel: "闲鱼",
|
|
ListingID: &listing.ID,
|
|
Status: "listing_created",
|
|
}
|
|
if err := db.Create(&upload).Error; err != nil {
|
|
t.Fatalf("创建外部上传记录失败: %v", err)
|
|
}
|
|
|
|
item, err := repo.Create(t.Context(), CreateRequest{
|
|
ListingID: listing.ID,
|
|
Platform: "微信",
|
|
ProfitAmountCent: 1200,
|
|
}, 99, auditlog.Meta{RequestID: "req-source"})
|
|
if err != nil {
|
|
t.Fatalf("Create() error = %v", err)
|
|
}
|
|
if item.AccountSource != AccountSourceExternalPlatformManaged || item.SourceChannel != "闲鱼" {
|
|
t.Fatalf("创建来源快照 = %q/%q", item.AccountSource, item.SourceChannel)
|
|
}
|
|
|
|
if err := db.Delete(&upload).Error; err != nil {
|
|
t.Fatalf("删除上传关联失败: %v", err)
|
|
}
|
|
stored, err := repo.FindByID(t.Context(), item.ID)
|
|
if err != nil {
|
|
t.Fatalf("FindByID() error = %v", err)
|
|
}
|
|
if stored.AccountSource != AccountSourceExternalPlatformManaged || stored.SourceChannel != "闲鱼" {
|
|
t.Fatalf("关联删除后的来源快照 = %q/%q", stored.AccountSource, stored.SourceChannel)
|
|
}
|
|
}
|
|
|
|
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.ListingUpload{},
|
|
&model.AdminPickup{},
|
|
&model.AuditLog{},
|
|
&model.Notification{},
|
|
&model.ListingStatusEvent{},
|
|
); err != nil {
|
|
t.Fatalf("迁移测试表失败: %v", err)
|
|
}
|
|
return NewRepository(db), db
|
|
}
|
|
|
|
func seedAvailableListing(t *testing.T, db *gorm.DB, listingNo, title string, external bool) model.RentalListing {
|
|
t.Helper()
|
|
owner := model.User{Phone: "188" + listingNo}
|
|
if err := db.Create(&owner).Error; err != nil {
|
|
t.Fatalf("创建候选号主失败: %v", err)
|
|
}
|
|
account := model.GameAccount{
|
|
OwnerID: owner.ID,
|
|
ServerRegion: "测试区",
|
|
LoginPlatform: "微信",
|
|
Title: title,
|
|
Status: "published",
|
|
}
|
|
if err := db.Create(&account).Error; err != nil {
|
|
t.Fatalf("创建候选账号失败: %v", err)
|
|
}
|
|
listing := model.RentalListing{
|
|
ListingNo: listingNo,
|
|
AccountID: account.ID,
|
|
OwnerID: owner.ID,
|
|
PriceCent: 10000,
|
|
Status: "published",
|
|
ReviewStatus: "approved",
|
|
HandoffMode: "owner",
|
|
SettlementMode: "owner_wallet",
|
|
}
|
|
if external {
|
|
listing.HandoffMode = "platform"
|
|
listing.SettlementMode = "platform_managed"
|
|
}
|
|
if err := db.Create(&listing).Error; err != nil {
|
|
t.Fatalf("创建候选上架记录失败: %v", err)
|
|
}
|
|
return listing
|
|
}
|
|
|
|
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
|
|
}
|