feat(tenant): 增加双站共享货架与跨站下单

This commit is contained in:
yml
2026-11-15 15:45:00 +08:00
parent abf0fce89b
commit 4eb2bc253b
20 changed files with 767 additions and 31 deletions
+56 -6
View File
@@ -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"