feat(tenant): 增加双站共享货架与跨站下单
This commit is contained in:
@@ -10,6 +10,9 @@ import (
|
||||
|
||||
type OrderDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
BuyerTenantID uint64 `json:"buyer_tenant_id"`
|
||||
SourceTenantID uint64 `json:"source_tenant_id"`
|
||||
PaymentTenantID uint64 `json:"payment_tenant_id"`
|
||||
OrderNo string `json:"order_no"`
|
||||
ListingID uint64 `json:"listing_id"`
|
||||
ListingNo string `json:"listing_no"`
|
||||
@@ -28,6 +31,7 @@ type OrderDTO struct {
|
||||
DisplayAmountCent int64 `json:"display_amount_cent"`
|
||||
RentAmountCent *int64 `json:"rent_amount_cent,omitempty"`
|
||||
OwnerRentAmountCent *int64 `json:"owner_rent_amount_cent,omitempty"`
|
||||
SourceAmountCent int64 `json:"source_amount_cent"`
|
||||
DepositAmountCent int64 `json:"deposit_amount_cent"`
|
||||
DepositOriginalAmountCent int64 `json:"deposit_original_amount_cent"`
|
||||
DepositWaivedAmountCent int64 `json:"deposit_waived_amount_cent"`
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"hfb_sys/backend/internal/modules/rentergrowth"
|
||||
smsnode "hfb_sys/backend/internal/modules/sms"
|
||||
"hfb_sys/backend/internal/processlog"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
@@ -35,6 +36,32 @@ func (r *Repository) Create(ctx context.Context, renterID uint64, req CreateRequ
|
||||
if listing.OwnerID == renterID {
|
||||
return ErrCannotRentOwnListing
|
||||
}
|
||||
var renter model.User
|
||||
if err := tx.Select("id, tenant_id, register_track_code").First(&renter, renterID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
requestTenantID := tenancy.IDFromContext(ctx)
|
||||
if requestTenantID > 0 && renter.TenantID != requestTenantID {
|
||||
return ErrPermissionDenied
|
||||
}
|
||||
buyerTenantID := renter.TenantID
|
||||
if requestTenantID > 0 {
|
||||
buyerTenantID = requestTenantID
|
||||
}
|
||||
tenantOfferPrice, tenantOfferFound, err := loadTenantOfferPriceTx(tx, buyerTenantID, listing.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if requestTenantID > 0 && !tenantOfferFound {
|
||||
return ErrListingUnavailable
|
||||
}
|
||||
sourceTenantID := listing.SourceTenantID
|
||||
if sourceTenantID == 0 {
|
||||
_ = tx.Model(&model.User{}).
|
||||
Select("tenant_id").
|
||||
Where("id = ?", listing.OwnerID).
|
||||
Scan(&sourceTenantID).Error
|
||||
}
|
||||
|
||||
var account model.GameAccount
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&account, listing.AccountID).Error; err != nil {
|
||||
@@ -64,9 +91,13 @@ func (r *Repository) Create(ctx context.Context, renterID uint64, req CreateRequ
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pricing := buildOrderPricing(listing, account)
|
||||
basePriceCent := listing.PriceCent
|
||||
sitePriceCent, _, siteRatio := listingpkg.ApplyShelfRatio(account.HafCoinAmount, listing.PriceCent, shelfRatioDelta)
|
||||
effectiveListing := listing
|
||||
if tenantOfferFound && tenantOfferPrice != nil && *tenantOfferPrice > 0 {
|
||||
effectiveListing.PriceCent = *tenantOfferPrice
|
||||
}
|
||||
pricing := buildOrderPricing(effectiveListing, account)
|
||||
basePriceCent := effectiveListing.PriceCent
|
||||
sitePriceCent, _, siteRatio := listingpkg.ApplyShelfRatio(account.HafCoinAmount, effectiveListing.PriceCent, shelfRatioDelta)
|
||||
shelfMarkupCent := sitePriceCent - basePriceCent
|
||||
if sitePriceCent != listing.PriceCent {
|
||||
pricing.RentAmountCent = sitePriceCent
|
||||
@@ -118,12 +149,14 @@ func (r *Repository) Create(ctx context.Context, renterID uint64, req CreateRequ
|
||||
}
|
||||
sourceTrackCode := sanitizeTrackCode(req.TrackCode)
|
||||
if sourceTrackCode == "" {
|
||||
var u model.User
|
||||
if err := tx.Select("register_track_code").First(&u, renterID).Error; err == nil && u.RegisterTrackCode != "" {
|
||||
sourceTrackCode = sanitizeTrackCode(u.RegisterTrackCode)
|
||||
if renter.RegisterTrackCode != "" {
|
||||
sourceTrackCode = sanitizeTrackCode(renter.RegisterTrackCode)
|
||||
}
|
||||
}
|
||||
order := model.RentalOrder{
|
||||
BuyerTenantID: buyerTenantID,
|
||||
SourceTenantID: sourceTenantID,
|
||||
PaymentTenantID: buyerTenantID,
|
||||
OrderNo: orderNo,
|
||||
ListingID: listing.ID,
|
||||
AccountID: listing.AccountID,
|
||||
@@ -132,6 +165,7 @@ func (r *Repository) Create(ctx context.Context, renterID uint64, req CreateRequ
|
||||
EstimatedDurationHours: rentHours,
|
||||
RentAmountCent: pricing.RentAmountCent,
|
||||
OwnerRentAmountCent: pricing.OwnerRentAmountCent,
|
||||
SourceAmountCent: pricing.OwnerRentAmountCent,
|
||||
DepositAmountCent: depositWaiver.PaidCent,
|
||||
DepositOriginalAmountCent: depositOriginalAmountCent,
|
||||
DepositWaivedAmountCent: depositWaiver.WaivedCent,
|
||||
@@ -200,6 +234,22 @@ func (r *Repository) Create(ctx context.Context, renterID uint64, req CreateRequ
|
||||
return r.FindForUser(ctx, renterID, createdID, req.DistributionSiteCode)
|
||||
}
|
||||
|
||||
func loadTenantOfferPriceTx(tx *gorm.DB, tenantID uint64, listingID uint64) (*int64, bool, error) {
|
||||
if tenantID == 0 {
|
||||
return nil, false, nil
|
||||
}
|
||||
var offer model.TenantListingOffer
|
||||
err := tx.Where("tenant_id = ? AND listing_id = ? AND display_status = ?", tenantID, listingID, "enabled").
|
||||
First(&offer).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return offer.PriceCent, true, nil
|
||||
}
|
||||
|
||||
func normalizedCompensationPlan(plan string) string {
|
||||
if plan == "insurance" {
|
||||
return "insurance"
|
||||
|
||||
@@ -67,6 +67,9 @@ func (row orderRow) toAdminDTO() OrderDTO {
|
||||
platformFeeCent := row.PlatformFeeCent
|
||||
return OrderDTO{
|
||||
ID: row.ID,
|
||||
BuyerTenantID: row.BuyerTenantID,
|
||||
SourceTenantID: row.SourceTenantID,
|
||||
PaymentTenantID: row.PaymentTenantID,
|
||||
OrderNo: row.OrderNo,
|
||||
ListingID: row.ListingID,
|
||||
ListingNo: row.ListingNo,
|
||||
@@ -85,6 +88,7 @@ func (row orderRow) toAdminDTO() OrderDTO {
|
||||
DisplayAmountCent: row.RentAmountCent,
|
||||
RentAmountCent: &rentAmountCent,
|
||||
OwnerRentAmountCent: &ownerRentAmountCent,
|
||||
SourceAmountCent: row.SourceAmountCent,
|
||||
DepositAmountCent: row.DepositAmountCent,
|
||||
DepositOriginalAmountCent: effectiveDepositOriginalAmountCent(row.RentalOrder),
|
||||
DepositWaivedAmountCent: row.DepositWaivedAmountCent,
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func orderTenantContext(tenantID uint64) context.Context {
|
||||
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
func createOrderTenantListing(t *testing.T, db *gorm.DB, tenantIDs ...uint64) (*model.RentalListing, []model.User) {
|
||||
t.Helper()
|
||||
var tenants []model.Tenant
|
||||
for index, tenantID := range tenantIDs {
|
||||
tenant := model.Tenant{
|
||||
Code: "tenant_" + string(rune('a'+index)),
|
||||
Name: "租户",
|
||||
Status: "active",
|
||||
}
|
||||
tenant.ID = tenantID
|
||||
if err := db.Create(&tenant).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tenants = append(tenants, tenant)
|
||||
}
|
||||
owner := model.User{TenantID: tenants[0].ID, Phone: "13000000001", Status: "active"}
|
||||
if err := db.Create(&owner).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
account := model.GameAccount{
|
||||
SourceTenantID: tenants[0].ID, OwnerID: owner.ID, ServerRegion: "国服",
|
||||
LoginPlatform: "steam", Title: "跨租户商品", Status: "published",
|
||||
AssetSummary: datatypes.JSON([]byte(`{
|
||||
"price_breakdown": {
|
||||
"seller_total_price": 80,
|
||||
"platform_markup_amount": 20
|
||||
}
|
||||
}`)),
|
||||
}
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
listing := model.RentalListing{
|
||||
SourceTenantID: tenants[0].ID, ListingNo: "L-ORDER-TENANT-1",
|
||||
AccountID: account.ID, OwnerID: owner.ID, PriceCent: 10000,
|
||||
Status: listingStatusPublished, ReviewStatus: listingReviewStatusApproved,
|
||||
}
|
||||
if err := db.Create(&listing).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for index, tenantID := range tenantIDs {
|
||||
var price *int64
|
||||
if index > 0 {
|
||||
value := int64(10000 + index*2000)
|
||||
price = &value
|
||||
}
|
||||
if err := db.Create(&model.TenantListingOffer{
|
||||
TenantID: tenantID, ListingID: listing.ID, DisplayStatus: "enabled", PriceCent: price,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
renters := make([]model.User, 0, len(tenantIDs)-1)
|
||||
for index, tenantID := range tenantIDs[1:] {
|
||||
renter := model.User{
|
||||
TenantID: tenantID,
|
||||
Phone: "1300000001" + string(rune('0'+index+1)),
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&renter).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
renters = append(renters, renter)
|
||||
}
|
||||
return &listing, renters
|
||||
}
|
||||
|
||||
func TestCreateOrderUsesBuyerTenantOfferAndSnapshotsTenantRoles(t *testing.T) {
|
||||
db := setupOrderTestDB(t)
|
||||
listing, renters := createOrderTenantListing(t, db, 101, 202, 303)
|
||||
repo := NewRepository(db)
|
||||
|
||||
noOfferTenant := model.Tenant{ID: 404, Code: "tenant_no_offer", Name: "无货架租户", Status: "active"}
|
||||
if err := db.Create(&noOfferTenant).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
noOfferRenter := model.User{TenantID: noOfferTenant.ID, Phone: "13000000019", Status: "active"}
|
||||
if err := db.Create(&noOfferRenter).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := repo.Create(orderTenantContext(noOfferTenant.ID), noOfferRenter.ID, CreateRequest{ListingID: listing.ID}); !errors.Is(err, ErrListingUnavailable) {
|
||||
t.Fatalf("tenant without offer error = %v, want ErrListingUnavailable", err)
|
||||
}
|
||||
dto, err := repo.Create(orderTenantContext(202), renters[0].ID, CreateRequest{ListingID: listing.ID})
|
||||
if err != nil {
|
||||
t.Fatalf("create cross-tenant order: %v", err)
|
||||
}
|
||||
if dto.BuyerTenantID != 202 || dto.SourceTenantID != 101 || dto.PaymentTenantID != 202 {
|
||||
t.Fatalf("order tenant snapshots = buyer:%d source:%d payment:%d",
|
||||
dto.BuyerTenantID, dto.SourceTenantID, dto.PaymentTenantID)
|
||||
}
|
||||
if dto.BasePriceCent != 12000 || dto.ShelfMarkupCent != 0 {
|
||||
t.Fatalf("offer price snapshot = base:%d markup:%d", dto.BasePriceCent, dto.ShelfMarkupCent)
|
||||
}
|
||||
if dto.SourceAmountCent != 8000 {
|
||||
t.Fatalf("source amount snapshot = %d, want 8000", dto.SourceAmountCent)
|
||||
}
|
||||
var saved model.RentalOrder
|
||||
if err := db.First(&saved, dto.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if saved.BuyerTenantID != 202 || saved.SourceTenantID != 101 || saved.PaymentTenantID != 202 {
|
||||
t.Fatalf("saved tenant roles = %+v", saved)
|
||||
}
|
||||
if saved.OwnerRentAmountCent != saved.SourceAmountCent {
|
||||
t.Fatalf("source amount = %d, owner rent = %d", saved.SourceAmountCent, saved.OwnerRentAmountCent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentTenantsCanOnlyRentListingOnce(t *testing.T) {
|
||||
db := setupOrderTestDB(t)
|
||||
listing, renters := createOrderTenantListing(t, db, 101, 202, 303)
|
||||
repo := NewRepository(db)
|
||||
|
||||
start := make(chan struct{})
|
||||
results := make(chan error, 2)
|
||||
var wg sync.WaitGroup
|
||||
for index, tenantID := range []uint64{202, 303} {
|
||||
wg.Add(1)
|
||||
go func(renterID uint64, tenantID uint64) {
|
||||
defer wg.Done()
|
||||
<-start
|
||||
_, err := repo.Create(orderTenantContext(tenantID), renterID, CreateRequest{ListingID: listing.ID})
|
||||
results <- err
|
||||
}(renters[index].ID, tenantID)
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
close(results)
|
||||
|
||||
var success, unavailable int
|
||||
for err := range results {
|
||||
switch {
|
||||
case err == nil:
|
||||
success++
|
||||
case errors.Is(err, ErrListingUnavailable):
|
||||
unavailable++
|
||||
default:
|
||||
t.Fatalf("unexpected create order error: %v", err)
|
||||
}
|
||||
}
|
||||
if success != 1 || unavailable != 1 {
|
||||
t.Fatalf("concurrent order result = success:%d unavailable:%d", success, unavailable)
|
||||
}
|
||||
var count int64
|
||||
if err := db.Model(&model.RentalOrder{}).Where("listing_id = ?", listing.ID).Count(&count).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("order count = %d, want 1", count)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user