fix(pickup): 支持天域分销货源提号结算

This commit is contained in:
yml
2026-11-24 14:41:49 +08:00
parent b63f5d771b
commit 8d8f6f773e
5 changed files with 435 additions and 221 deletions
+82 -76
View File
@@ -50,57 +50,60 @@ var (
)
type PickupDTO struct {
ID uint64 `json:"id"`
TenantID uint64 `json:"tenant_id"`
TenantCode string `json:"tenant_code"`
TenantName string `json:"tenant_name"`
SourceTenantID uint64 `json:"source_tenant_id"`
SourceTenantCode string `json:"source_tenant_code"`
SourceTenantName string `json:"source_tenant_name"`
IsCrossTenant bool `json:"is_cross_tenant"`
PickupNo string `json:"pickup_no"`
ListingID uint64 `json:"listing_id"`
ListingNo string `json:"listing_no"`
AccountID uint64 `json:"account_id"`
AccountTitle string `json:"account_title"`
ServerRegion string `json:"server_region"`
LoginPlatform string `json:"login_platform"`
OwnerID uint64 `json:"owner_id"`
OwnerPhone string `json:"owner_phone"`
AdminID uint64 `json:"admin_id"`
Operator string `json:"operator"`
Platform string `json:"platform"`
ThirdPartyOrderNo string `json:"third_party_order_no"`
ShopName string `json:"shop_name"`
RawShopName string `json:"raw_shop_name"`
AccountSource string `json:"account_source"`
SourceChannel string `json:"source_channel"`
SettlementMode string `json:"settlement_mode"`
ListingPriceCent int64 `json:"listing_price_cent"`
OrderAmountCent int64 `json:"order_amount_cent"`
DepositAmountCent int64 `json:"deposit_amount_cent"`
OwnerPriceCent int64 `json:"owner_price_cent"`
WebsiteProfitCent int64 `json:"website_profit_cent"`
ProfitAmountCent int64 `json:"profit_amount_cent"`
ProfitAdjustmentCent int64 `json:"profit_adjustment_cent"`
EffectiveProfitAmountCent int64 `json:"effective_profit_amount_cent"`
SellerRatio float64 `json:"seller_ratio"`
BuyerRatio float64 `json:"buyer_ratio"`
AccountSnapshot datatypes.JSON `json:"account_snapshot,omitempty"`
SettleAmountCent int64 `json:"settle_amount_cent"`
SettleAdjustmentCent int64 `json:"settle_adjustment_cent"`
EffectiveSettleAmountCent int64 `json:"effective_settle_amount_cent"`
Status string `json:"status"`
OfflineSettlementStatus string `json:"offline_settlement_status"`
OfflineSettlementRemark string `json:"offline_settlement_remark"`
ActualPayoutOperator string `json:"offline_settlement_actual_operator"`
OfflineSettledBy *uint64 `json:"offline_settled_by,omitempty"`
OfflineSettledAt *time.Time `json:"offline_settled_at,omitempty"`
Remark string `json:"remark"`
CompleteRemark string `json:"complete_remark"`
CreatedAt time.Time `json:"created_at"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
CancelledAt *time.Time `json:"cancelled_at,omitempty"`
ID uint64 `json:"id"`
TenantID uint64 `json:"tenant_id"`
TenantCode string `json:"tenant_code"`
TenantName string `json:"tenant_name"`
SourceTenantID uint64 `json:"source_tenant_id"`
SourceTenantCode string `json:"source_tenant_code"`
SourceTenantName string `json:"source_tenant_name"`
SourceDistributionSiteID uint64 `json:"source_distribution_site_id"`
SourceDistributionSiteCode string `json:"source_distribution_site_code"`
SourceDistributionSiteName string `json:"source_distribution_site_name"`
IsCrossTenant bool `json:"is_cross_tenant"`
PickupNo string `json:"pickup_no"`
ListingID uint64 `json:"listing_id"`
ListingNo string `json:"listing_no"`
AccountID uint64 `json:"account_id"`
AccountTitle string `json:"account_title"`
ServerRegion string `json:"server_region"`
LoginPlatform string `json:"login_platform"`
OwnerID uint64 `json:"owner_id"`
OwnerPhone string `json:"owner_phone"`
AdminID uint64 `json:"admin_id"`
Operator string `json:"operator"`
Platform string `json:"platform"`
ThirdPartyOrderNo string `json:"third_party_order_no"`
ShopName string `json:"shop_name"`
RawShopName string `json:"raw_shop_name"`
AccountSource string `json:"account_source"`
SourceChannel string `json:"source_channel"`
SettlementMode string `json:"settlement_mode"`
ListingPriceCent int64 `json:"listing_price_cent"`
OrderAmountCent int64 `json:"order_amount_cent"`
DepositAmountCent int64 `json:"deposit_amount_cent"`
OwnerPriceCent int64 `json:"owner_price_cent"`
WebsiteProfitCent int64 `json:"website_profit_cent"`
ProfitAmountCent int64 `json:"profit_amount_cent"`
ProfitAdjustmentCent int64 `json:"profit_adjustment_cent"`
EffectiveProfitAmountCent int64 `json:"effective_profit_amount_cent"`
SellerRatio float64 `json:"seller_ratio"`
BuyerRatio float64 `json:"buyer_ratio"`
AccountSnapshot datatypes.JSON `json:"account_snapshot,omitempty"`
SettleAmountCent int64 `json:"settle_amount_cent"`
SettleAdjustmentCent int64 `json:"settle_adjustment_cent"`
EffectiveSettleAmountCent int64 `json:"effective_settle_amount_cent"`
Status string `json:"status"`
OfflineSettlementStatus string `json:"offline_settlement_status"`
OfflineSettlementRemark string `json:"offline_settlement_remark"`
ActualPayoutOperator string `json:"offline_settlement_actual_operator"`
OfflineSettledBy *uint64 `json:"offline_settled_by,omitempty"`
OfflineSettledAt *time.Time `json:"offline_settled_at,omitempty"`
Remark string `json:"remark"`
CompleteRemark string `json:"complete_remark"`
CreatedAt time.Time `json:"created_at"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
CancelledAt *time.Time `json:"cancelled_at,omitempty"`
}
type FinancialAdjustmentDTO struct {
@@ -253,31 +256,34 @@ type SellerPickupQuery struct {
}
type AvailableListingDTO struct {
ID uint64 `json:"id"`
SourceTenantID uint64 `json:"source_tenant_id"`
SourceTenantCode string `json:"source_tenant_code"`
SourceTenantName string `json:"source_tenant_name"`
IsCrossTenant bool `json:"is_cross_tenant"`
ListingNo string `json:"listing_no"`
AccountID uint64 `json:"account_id"`
AccountTitle string `json:"account_title"`
ServerRegion string `json:"server_region"`
LoginPlatform string `json:"login_platform"`
OwnerID uint64 `json:"owner_id"`
OwnerPhone string `json:"owner_phone"`
ListingPriceCent int64 `json:"listing_price_cent"`
OwnerPriceCent int64 `json:"owner_price_cent"`
OwnerPurePriceCent int64 `json:"owner_pure_price_cent"`
OwnerExtraItemPriceCent int64 `json:"owner_extra_item_price_cent"`
OwnerTotalPriceCent int64 `json:"owner_total_price_cent"`
WebsiteProfitCent int64 `json:"website_profit_cent"`
SellerRatio float64 `json:"seller_ratio"`
BuyerRatio float64 `json:"buyer_ratio"`
AccountSource string `json:"account_source"`
IsExternalUpload bool `json:"is_external_upload"`
SourceChannel string `json:"source_channel"`
HandoffMode string `json:"handoff_mode"`
SettlementMode string `json:"settlement_mode"`
ID uint64 `json:"id"`
SourceTenantID uint64 `json:"source_tenant_id"`
SourceTenantCode string `json:"source_tenant_code"`
SourceTenantName string `json:"source_tenant_name"`
SourceDistributionSiteID uint64 `json:"source_distribution_site_id"`
SourceDistributionSiteCode string `json:"source_distribution_site_code"`
SourceDistributionSiteName string `json:"source_distribution_site_name"`
IsCrossTenant bool `json:"is_cross_tenant"`
ListingNo string `json:"listing_no"`
AccountID uint64 `json:"account_id"`
AccountTitle string `json:"account_title"`
ServerRegion string `json:"server_region"`
LoginPlatform string `json:"login_platform"`
OwnerID uint64 `json:"owner_id"`
OwnerPhone string `json:"owner_phone"`
ListingPriceCent int64 `json:"listing_price_cent"`
OwnerPriceCent int64 `json:"owner_price_cent"`
OwnerPurePriceCent int64 `json:"owner_pure_price_cent"`
OwnerExtraItemPriceCent int64 `json:"owner_extra_item_price_cent"`
OwnerTotalPriceCent int64 `json:"owner_total_price_cent"`
WebsiteProfitCent int64 `json:"website_profit_cent"`
SellerRatio float64 `json:"seller_ratio"`
BuyerRatio float64 `json:"buyer_ratio"`
AccountSource string `json:"account_source"`
IsExternalUpload bool `json:"is_external_upload"`
SourceChannel string `json:"source_channel"`
HandoffMode string `json:"handoff_mode"`
SettlementMode string `json:"settlement_mode"`
}
type AvailableListingQuery struct {
+194 -145
View File
@@ -134,17 +134,18 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest, adminID uint
return err
}
if err := appendPickupEvent(tx, pickup, "pickup_created", adminID, "管理员创建线下提号。", pickup.Remark, map[string]any{
"tenant_id": pickup.TenantID,
"source_tenant_id": pickup.SourceTenantID,
"listing_price_cent": pickup.ListingPriceCent,
"order_amount_cent": pickup.OrderAmountCent,
"deposit_amount_cent": pickup.DepositAmountCent,
"profit_amount_cent": pickup.ProfitAmountCent,
"account_source": pickup.AccountSource,
"source_channel": pickup.SourceChannel,
"settlement_mode": pickup.SettlementMode,
"operator": pickup.Operator,
"third_party_order_no": pickup.ThirdPartyOrderNo,
"tenant_id": pickup.TenantID,
"source_tenant_id": pickup.SourceTenantID,
"source_distribution_site_id": pickupDistributionSiteID(listing),
"listing_price_cent": pickup.ListingPriceCent,
"order_amount_cent": pickup.OrderAmountCent,
"deposit_amount_cent": pickup.DepositAmountCent,
"profit_amount_cent": pickup.ProfitAmountCent,
"account_source": pickup.AccountSource,
"source_channel": pickup.SourceChannel,
"settlement_mode": pickup.SettlementMode,
"operator": pickup.Operator,
"third_party_order_no": pickup.ThirdPartyOrderNo,
}, map[string]any{}); err != nil {
return err
}
@@ -193,19 +194,20 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest, adminID uint
BizID: &bid,
Meta: meta,
Detail: map[string]any{
"pickup_no": pickupNo,
"tenant_id": pickup.TenantID,
"source_tenant_id": pickup.SourceTenantID,
"listing_id": listing.ID,
"platform": pickup.Platform,
"third_party_order_no": pickup.ThirdPartyOrderNo,
"shop_name": pickup.ShopName,
"operator": pickup.Operator,
"account_source": pickup.AccountSource,
"source_channel": pickup.SourceChannel,
"order_amount_cent": pickup.OrderAmountCent,
"deposit_amount_cent": pickup.DepositAmountCent,
"profit_cent": pickup.ProfitAmountCent,
"pickup_no": pickupNo,
"tenant_id": pickup.TenantID,
"source_tenant_id": pickup.SourceTenantID,
"source_distribution_site_id": pickupDistributionSiteID(listing),
"listing_id": listing.ID,
"platform": pickup.Platform,
"third_party_order_no": pickup.ThirdPartyOrderNo,
"shop_name": pickup.ShopName,
"operator": pickup.Operator,
"account_source": pickup.AccountSource,
"source_channel": pickup.SourceChannel,
"order_amount_cent": pickup.OrderAmountCent,
"deposit_amount_cent": pickup.DepositAmountCent,
"profit_cent": pickup.ProfitAmountCent,
},
}); err != nil {
return err
@@ -303,13 +305,15 @@ func (r *Repository) Complete(ctx context.Context, pickupID uint64, req Complete
}
} else {
if err := wallet.AppendEntries(tx, wallet.Entry{
UserID: pickup.OwnerID,
Direction: "in",
AmountCent: req.SettleAmountCent,
BalanceType: "available",
BizType: "admin_pickup_settle",
BizNo: pickup.PickupNo,
Remark: fmt.Sprintf("管理员提号结算:%s", pickup.PickupNo),
TenantID: pickup.SourceTenantID,
UserID: pickup.OwnerID,
DistributionSiteID: pickupDistributionSiteID(listing),
Direction: "in",
AmountCent: req.SettleAmountCent,
BalanceType: "available",
BizType: "admin_pickup_settle",
BizNo: pickup.PickupNo,
Remark: fmt.Sprintf("管理员提号结算:%s", pickup.PickupNo),
}); err != nil {
return err
}
@@ -650,14 +654,20 @@ func (r *Repository) CreateFinancialAdjustment(ctx context.Context, pickupID uin
// 站内钱包的补款在创建调整时立即到账;扣款统一转待追回,避免余额被扣成负数。
if pickup.SettlementMode == SettlementModeOwnerWallet && settleDeltaCent > 0 {
var listing model.RentalListing
if err := tx.Select("id", "distribution_site_id").First(&listing, pickup.ListingID).Error; err != nil {
return err
}
if err := wallet.AppendEntries(tx, wallet.Entry{
UserID: pickup.OwnerID,
Direction: "in",
AmountCent: settleDeltaCent,
BalanceType: "available",
BizType: "admin_pickup_adjustment",
BizNo: fmt.Sprintf("%s-A%d", pickup.PickupNo, adjustment.ID),
Remark: fmt.Sprintf("管理员提号结算补款:%s", pickup.PickupNo),
TenantID: pickup.SourceTenantID,
UserID: pickup.OwnerID,
DistributionSiteID: pickupDistributionSiteID(listing),
Direction: "in",
AmountCent: settleDeltaCent,
BalanceType: "available",
BizType: "admin_pickup_adjustment",
BizNo: fmt.Sprintf("%s-A%d", pickup.PickupNo, adjustment.ID),
Remark: fmt.Sprintf("管理员提号结算补款:%s", pickup.PickupNo),
}); err != nil {
return err
}
@@ -1567,10 +1577,23 @@ func (r *Repository) ListAvailableListings(ctx context.Context, query AvailableL
Joins("JOIN game_accounts AS a ON a.id = l.account_id").
Joins("JOIN users AS owner ON owner.id = l.owner_id").
Joins("LEFT JOIN tenants AS source_tenant ON source_tenant.id = l.source_tenant_id").
Joins("LEFT JOIN distribution_sites AS source_site ON source_site.id = l.distribution_site_id").
Joins("LEFT JOIN (?) AS latest_upload ON latest_upload.listing_id = l.id", latestUploads).
Joins("LEFT JOIN listing_uploads AS lu ON lu.id = latest_upload.upload_id").
Where("l.status = ? AND l.review_status = ? AND l.in_transaction = ?", "published", "approved", false).
Where("(l.distribution_site_id IS NULL OR l.distribution_site_id = 0)")
Where("l.status = ? AND l.review_status = ? AND l.in_transaction = ?", "published", "approved", false)
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
q = q.Where(`(
l.distribution_site_id IS NULL
OR l.distribution_site_id = 0
OR EXISTS (
SELECT 1
FROM tenant_listing_offers AS tlo
WHERE tlo.listing_id = l.id
AND tlo.tenant_id = ?
AND tlo.display_status = 'enabled'
)
)`, tenantID)
}
if kw := strings.TrimSpace(query.Keyword); kw != "" {
upper := strings.ToUpper(kw)
if strings.HasPrefix(upper, "L") || (len(kw) >= 12 && isAllDigits(kw)) {
@@ -1594,6 +1617,9 @@ func (r *Repository) ListAvailableListings(ctx context.Context, query AvailableL
if err := buildQuery().Select(`l.id, l.source_tenant_id,
COALESCE(source_tenant.code, '') AS source_tenant_code,
COALESCE(source_tenant.name, '') AS source_tenant_name,
COALESCE(source_site.id, 0) AS source_distribution_site_id,
COALESCE(source_site.site_code, '') AS source_distribution_site_code,
COALESCE(source_site.site_name, '') AS source_distribution_site_name,
l.listing_no, l.account_id, l.owner_id, l.price_cent AS listing_price_cent,
a.title AS account_title, a.server_region, a.login_platform, a.haf_coin_amount, a.asset_summary,
owner.phone AS owner_phone, l.handoff_mode, l.settlement_mode,
@@ -1627,13 +1653,17 @@ func (r *Repository) baseQuery(ctx context.Context) *gorm.DB {
COALESCE(pickup_tenant.name, '') AS tenant_name,
COALESCE(source_tenant.code, '') AS source_tenant_code,
COALESCE(source_tenant.name, '') AS source_tenant_name,
COALESCE(source_site.id, 0) AS source_distribution_site_id,
COALESCE(source_site.site_code, '') AS source_distribution_site_code,
COALESCE(source_site.site_name, '') AS source_distribution_site_name,
COALESCE((SELECT SUM(fa.profit_delta_cent) FROM admin_pickup_financial_adjustments AS fa WHERE fa.pickup_id = p.id), 0) AS profit_adjustment_cent,
COALESCE((SELECT SUM(fa.settle_delta_cent) FROM admin_pickup_financial_adjustments AS fa WHERE fa.pickup_id = p.id), 0) AS settle_adjustment_cent`).
Joins("JOIN rental_listings AS l ON l.id = p.listing_id").
Joins("JOIN game_accounts AS a ON a.id = p.account_id").
Joins("JOIN users AS owner ON owner.id = p.owner_id").
Joins("LEFT JOIN tenants AS pickup_tenant ON pickup_tenant.id = p.tenant_id").
Joins("LEFT JOIN tenants AS source_tenant ON source_tenant.id = p.source_tenant_id")
Joins("LEFT JOIN tenants AS source_tenant ON source_tenant.id = p.source_tenant_id").
Joins("LEFT JOIN distribution_sites AS source_site ON source_site.id = l.distribution_site_id")
}
func (r *Repository) adminBaseQuery(ctx context.Context) *gorm.DB {
@@ -1642,72 +1672,78 @@ func (r *Repository) adminBaseQuery(ctx context.Context) *gorm.DB {
type pickupRow struct {
model.AdminPickup
TenantCode string
TenantName string
SourceTenantCode string
SourceTenantName string
ListingNo string
AccountTitle string
ServerRegion string
LoginPlatform string
OwnerPhone string
ProfitAdjustmentCent int64
SettleAdjustmentCent int64
TenantCode string
TenantName string
SourceTenantCode string
SourceTenantName string
SourceDistributionSiteID uint64
SourceDistributionSiteCode string
SourceDistributionSiteName string
ListingNo string
AccountTitle string
ServerRegion string
LoginPlatform string
OwnerPhone string
ProfitAdjustmentCent int64
SettleAdjustmentCent int64
}
func (row pickupRow) toDTO() PickupDTO {
return PickupDTO{
ID: row.ID,
TenantID: row.TenantID,
TenantCode: row.TenantCode,
TenantName: row.TenantName,
SourceTenantID: row.SourceTenantID,
SourceTenantCode: row.SourceTenantCode,
SourceTenantName: row.SourceTenantName,
IsCrossTenant: row.TenantID > 0 && row.SourceTenantID > 0 && row.TenantID != row.SourceTenantID,
PickupNo: row.PickupNo,
ListingID: row.ListingID,
ListingNo: row.ListingNo,
AccountID: row.AccountID,
AccountTitle: row.AccountTitle,
ServerRegion: row.ServerRegion,
LoginPlatform: row.LoginPlatform,
OwnerID: row.OwnerID,
OwnerPhone: row.OwnerPhone,
AdminID: row.AdminID,
Operator: row.Operator,
Platform: row.Platform,
ThirdPartyOrderNo: row.ThirdPartyOrderNo,
ShopName: row.ShopName,
RawShopName: row.RawShopName,
AccountSource: row.AccountSource,
SourceChannel: row.SourceChannel,
SettlementMode: row.SettlementMode,
ListingPriceCent: row.ListingPriceCent,
OrderAmountCent: row.OrderAmountCent,
DepositAmountCent: row.DepositAmountCent,
OwnerPriceCent: row.OwnerPriceCent,
WebsiteProfitCent: row.WebsiteProfitCent,
ProfitAmountCent: row.ProfitAmountCent,
ProfitAdjustmentCent: row.ProfitAdjustmentCent,
EffectiveProfitAmountCent: row.ProfitAmountCent + row.ProfitAdjustmentCent,
SellerRatio: row.SellerRatio,
BuyerRatio: row.BuyerRatio,
AccountSnapshot: row.AccountSnapshot,
SettleAmountCent: row.SettleAmountCent,
SettleAdjustmentCent: row.SettleAdjustmentCent,
EffectiveSettleAmountCent: row.SettleAmountCent + row.SettleAdjustmentCent,
Status: row.Status,
OfflineSettlementStatus: row.OfflineSettlementStatus,
OfflineSettlementRemark: row.OfflineSettlementRemark,
ActualPayoutOperator: row.ActualPayoutOperator,
OfflineSettledBy: row.OfflineSettledBy,
OfflineSettledAt: row.OfflineSettledAt,
Remark: row.Remark,
CompleteRemark: row.CompleteRemark,
CreatedAt: row.CreatedAt,
CompletedAt: row.CompletedAt,
CancelledAt: row.CancelledAt,
ID: row.ID,
TenantID: row.TenantID,
TenantCode: row.TenantCode,
TenantName: row.TenantName,
SourceTenantID: row.SourceTenantID,
SourceTenantCode: row.SourceTenantCode,
SourceTenantName: row.SourceTenantName,
SourceDistributionSiteID: row.SourceDistributionSiteID,
SourceDistributionSiteCode: row.SourceDistributionSiteCode,
SourceDistributionSiteName: row.SourceDistributionSiteName,
IsCrossTenant: row.TenantID > 0 && row.SourceTenantID > 0 && row.TenantID != row.SourceTenantID,
PickupNo: row.PickupNo,
ListingID: row.ListingID,
ListingNo: row.ListingNo,
AccountID: row.AccountID,
AccountTitle: row.AccountTitle,
ServerRegion: row.ServerRegion,
LoginPlatform: row.LoginPlatform,
OwnerID: row.OwnerID,
OwnerPhone: row.OwnerPhone,
AdminID: row.AdminID,
Operator: row.Operator,
Platform: row.Platform,
ThirdPartyOrderNo: row.ThirdPartyOrderNo,
ShopName: row.ShopName,
RawShopName: row.RawShopName,
AccountSource: row.AccountSource,
SourceChannel: row.SourceChannel,
SettlementMode: row.SettlementMode,
ListingPriceCent: row.ListingPriceCent,
OrderAmountCent: row.OrderAmountCent,
DepositAmountCent: row.DepositAmountCent,
OwnerPriceCent: row.OwnerPriceCent,
WebsiteProfitCent: row.WebsiteProfitCent,
ProfitAmountCent: row.ProfitAmountCent,
ProfitAdjustmentCent: row.ProfitAdjustmentCent,
EffectiveProfitAmountCent: row.ProfitAmountCent + row.ProfitAdjustmentCent,
SellerRatio: row.SellerRatio,
BuyerRatio: row.BuyerRatio,
AccountSnapshot: row.AccountSnapshot,
SettleAmountCent: row.SettleAmountCent,
SettleAdjustmentCent: row.SettleAdjustmentCent,
EffectiveSettleAmountCent: row.SettleAmountCent + row.SettleAdjustmentCent,
Status: row.Status,
OfflineSettlementStatus: row.OfflineSettlementStatus,
OfflineSettlementRemark: row.OfflineSettlementRemark,
ActualPayoutOperator: row.ActualPayoutOperator,
OfflineSettledBy: row.OfflineSettledBy,
OfflineSettledAt: row.OfflineSettledAt,
Remark: row.Remark,
CompleteRemark: row.CompleteRemark,
CreatedAt: row.CreatedAt,
CompletedAt: row.CompletedAt,
CancelledAt: row.CancelledAt,
}
}
@@ -1751,25 +1787,35 @@ func pickupRequiresOfflineSettlement(pickup model.AdminPickup) bool {
return pickup.SettlementMode == SettlementModePlatformManaged
}
func pickupDistributionSiteID(listing model.RentalListing) uint64 {
if listing.DistributionSiteID == nil {
return 0
}
return *listing.DistributionSiteID
}
type availableListingRow struct {
ID uint64
SourceTenantID uint64
SourceTenantCode string
SourceTenantName string
ListingNo string
AccountID uint64
AccountTitle string
ServerRegion string
LoginPlatform string
OwnerID uint64
OwnerPhone string
ListingPriceCent int64
HafCoinAmount int64
AssetSummary datatypes.JSON
IsExternalUpload bool
SourceChannel string
HandoffMode string
SettlementMode string
ID uint64
SourceTenantID uint64
SourceTenantCode string
SourceTenantName string
SourceDistributionSiteID uint64
SourceDistributionSiteCode string
SourceDistributionSiteName string
ListingNo string
AccountID uint64
AccountTitle string
ServerRegion string
LoginPlatform string
OwnerID uint64
OwnerPhone string
ListingPriceCent int64
HafCoinAmount int64
AssetSummary datatypes.JSON
IsExternalUpload bool
SourceChannel string
HandoffMode string
SettlementMode string
}
func (row availableListingRow) toDTO() AvailableListingDTO {
@@ -1782,30 +1828,33 @@ func (row availableListingRow) toDTO() AvailableListingDTO {
accountSource = AccountSourceExternalPlatformManaged
}
return AvailableListingDTO{
ID: row.ID,
SourceTenantID: row.SourceTenantID,
SourceTenantCode: row.SourceTenantCode,
SourceTenantName: row.SourceTenantName,
ListingNo: row.ListingNo,
AccountID: row.AccountID,
AccountTitle: row.AccountTitle,
ServerRegion: row.ServerRegion,
LoginPlatform: row.LoginPlatform,
OwnerID: row.OwnerID,
OwnerPhone: row.OwnerPhone,
ListingPriceCent: snapshot.ListingPriceCent,
OwnerPriceCent: snapshot.OwnerPriceCent,
OwnerPurePriceCent: snapshot.OwnerPurePriceCent,
OwnerExtraItemPriceCent: snapshot.OwnerExtraItemPriceCent,
OwnerTotalPriceCent: snapshot.OwnerTotalPriceCent,
WebsiteProfitCent: snapshot.WebsiteProfitCent,
SellerRatio: snapshot.SellerRatio,
BuyerRatio: snapshot.BuyerRatio,
AccountSource: accountSource,
IsExternalUpload: row.IsExternalUpload,
SourceChannel: row.SourceChannel,
HandoffMode: row.HandoffMode,
SettlementMode: row.SettlementMode,
ID: row.ID,
SourceTenantID: row.SourceTenantID,
SourceTenantCode: row.SourceTenantCode,
SourceTenantName: row.SourceTenantName,
SourceDistributionSiteID: row.SourceDistributionSiteID,
SourceDistributionSiteCode: row.SourceDistributionSiteCode,
SourceDistributionSiteName: row.SourceDistributionSiteName,
ListingNo: row.ListingNo,
AccountID: row.AccountID,
AccountTitle: row.AccountTitle,
ServerRegion: row.ServerRegion,
LoginPlatform: row.LoginPlatform,
OwnerID: row.OwnerID,
OwnerPhone: row.OwnerPhone,
ListingPriceCent: snapshot.ListingPriceCent,
OwnerPriceCent: snapshot.OwnerPriceCent,
OwnerPurePriceCent: snapshot.OwnerPurePriceCent,
OwnerExtraItemPriceCent: snapshot.OwnerExtraItemPriceCent,
OwnerTotalPriceCent: snapshot.OwnerTotalPriceCent,
WebsiteProfitCent: snapshot.WebsiteProfitCent,
SellerRatio: snapshot.SellerRatio,
BuyerRatio: snapshot.BuyerRatio,
AccountSource: accountSource,
IsExternalUpload: row.IsExternalUpload,
SourceChannel: row.SourceChannel,
HandoffMode: row.HandoffMode,
SettlementMode: row.SettlementMode,
}
}
@@ -56,6 +56,47 @@ func seedPickupTenantListing(t *testing.T, db *gorm.DB, tenantID uint64, suffix
return listing
}
func seedPickupDistributionListing(t *testing.T, db *gorm.DB, tenantID, siteID uint64, suffix string) model.RentalListing {
t.Helper()
owner := model.User{
TenantID: tenantID,
DistributionSiteID: siteID,
Phone: "1890000" + suffix,
Nickname: "分销号主" + suffix,
Status: "active",
}
if err := db.Create(&owner).Error; err != nil {
t.Fatal(err)
}
account := model.GameAccount{
SourceTenantID: tenantID,
OwnerID: owner.ID,
ServerRegion: "测试区",
LoginPlatform: "微信",
Title: "天域账号" + suffix,
Status: "published",
}
if err := db.Create(&account).Error; err != nil {
t.Fatal(err)
}
listing := model.RentalListing{
SourceTenantID: tenantID,
ListingNo: "LST-PICKUP-DIST-" + suffix,
AccountID: account.ID,
OwnerID: owner.ID,
PriceCent: 12000,
Status: "published",
ReviewStatus: "approved",
HandoffMode: "owner",
SettlementMode: "owner_wallet",
DistributionSiteID: &siteID,
}
if err := db.Create(&listing).Error; err != nil {
t.Fatal(err)
}
return listing
}
func TestPickupTenantScopeSupportsCrossTenantCreationAndSeparateLists(t *testing.T) {
db := database.NewTestDB()
tenants := []model.Tenant{
@@ -150,6 +191,110 @@ func TestPickupTenantScopeSupportsCrossTenantCreationAndSeparateLists(t *testing
}
}
func TestPickupSupportsSharedDistributionSourceAcrossTenants(t *testing.T) {
db := database.NewTestDB()
tenants := []model.Tenant{
{Code: "pickup_dist_source", Name: "货源站", Status: "active"},
{Code: "pickup_dist_a", Name: "提号A站", Status: "active"},
{Code: "pickup_dist_b", Name: "提号B站", Status: "active"},
{Code: "pickup_dist_other", Name: "未铺货站", Status: "active"},
}
if err := db.Create(&tenants).Error; err != nil {
t.Fatal(err)
}
site := model.DistributionSite{
SiteCode: "tianyu_pickup",
SiteName: "天域商行",
Domain: "tianyu-pickup.example.com",
Status: "active",
}
if err := db.Create(&site).Error; err != nil {
t.Fatal(err)
}
sourceTenant, tenantA, tenantB, otherTenant := tenants[0], tenants[1], tenants[2], tenants[3]
listing := seedPickupDistributionListing(t, db, sourceTenant.ID, site.ID, "01")
listingNumber := "202611230328"
if err := db.Model(&listing).Update("listing_no", listingNumber).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&[]model.TenantListingOffer{
{TenantID: tenantA.ID, ListingID: listing.ID, DisplayStatus: "enabled"},
{TenantID: tenantB.ID, ListingID: listing.ID, DisplayStatus: "enabled"},
}).Error; err != nil {
t.Fatal(err)
}
repo := NewRepository(db)
for _, tc := range []struct {
name string
ctx context.Context
crossTenant bool
}{
{name: "A站", ctx: pickupTenantContext(tenantA.ID), crossTenant: true},
{name: "B站", ctx: pickupTenantContext(tenantB.ID), crossTenant: true},
} {
available, err := repo.ListAvailableListings(tc.ctx, AvailableListingQuery{
Keyword: listingNumber,
Page: 1,
PageSize: 20,
})
if err != nil {
t.Fatalf("%s 查询可提号商品: %v", tc.name, err)
}
if len(available.Items) != 1 {
t.Fatalf("%s 可提号商品 = %+v, want 天域商品", tc.name, available.Items)
}
item := available.Items[0]
if item.ID != listing.ID ||
item.SourceDistributionSiteID != site.ID ||
item.SourceDistributionSiteName != site.SiteName ||
item.IsCrossTenant != tc.crossTenant {
t.Fatalf("%s 天域货源快照 = %+v", tc.name, item)
}
}
otherAvailable, err := repo.ListAvailableListings(pickupTenantContext(otherTenant.ID), AvailableListingQuery{Page: 1, PageSize: 20})
if err != nil {
t.Fatal(err)
}
if len(otherAvailable.Items) != 0 {
t.Fatalf("未铺货经营租户看到了天域商品: %+v", otherAvailable.Items)
}
pickup, err := repo.Create(pickupTenantContext(tenantB.ID), CreateRequest{ListingID: listing.ID}, 9, auditlog.Meta{})
if err != nil {
t.Fatalf("从 B 站创建天域货源提号: %v", err)
}
if _, err := repo.Complete(pickupTenantContext(tenantB.ID), pickup.ID, CompleteRequest{
SettleAmountCent: 8000,
}, 9, auditlog.Meta{}); err != nil {
t.Fatalf("完成天域货源提号: %v", err)
}
var ledger model.WalletLedger
if err := db.Where("biz_type = ? AND biz_no = ?", "admin_pickup_settle", pickup.PickupNo).First(&ledger).Error; err != nil {
t.Fatalf("查询天域货源结算流水: %v", err)
}
if ledger.TenantID != sourceTenant.ID || ledger.DistributionSiteID != site.ID {
t.Fatalf("天域货源结算流水归属 = %+v, want tenant=%d site=%d", ledger, sourceTenant.ID, site.ID)
}
adjustedSettleAmount := int64(9000)
if _, err := repo.CreateFinancialAdjustment(pickupTenantContext(tenantB.ID), pickup.ID, FinancialAdjustmentRequest{
SettleAmountCent: &adjustedSettleAmount,
Reason: "补发天域货源结算差额",
}, 9, auditlog.Meta{}); err != nil {
t.Fatalf("创建天域货源结算补款: %v", err)
}
var adjustmentLedger model.WalletLedger
if err := db.Where("biz_type = ? AND biz_no LIKE ?", "admin_pickup_adjustment", pickup.PickupNo+"-A%").
First(&adjustmentLedger).Error; err != nil {
t.Fatalf("查询天域货源补款流水: %v", err)
}
if adjustmentLedger.TenantID != sourceTenant.ID || adjustmentLedger.DistributionSiteID != site.ID {
t.Fatalf("天域货源补款流水归属 = %+v, want tenant=%d site=%d", adjustmentLedger, sourceTenant.ID, site.ID)
}
}
func TestPickupShopsAreSeparatedByTenant(t *testing.T) {
db := database.NewTestDB()
tenants := []model.Tenant{
@@ -16,6 +16,9 @@ export interface AdminPickup {
source_tenant_id: number
source_tenant_code: string
source_tenant_name: string
source_distribution_site_id: number
source_distribution_site_code: string
source_distribution_site_name: string
is_cross_tenant: boolean
pickup_no: string
listing_id: number
@@ -101,6 +104,9 @@ export interface AvailableListing {
source_tenant_id: number
source_tenant_code: string
source_tenant_name: string
source_distribution_site_id: number
source_distribution_site_code: string
source_distribution_site_name: string
is_cross_tenant: boolean
listing_no: string
account_id: number
@@ -609,11 +609,19 @@ function siteName(code: string, name: string) {
}
function pickupSiteText(row: AdminPickup) {
if (row.source_distribution_site_id > 0) {
const source = siteName(row.source_distribution_site_code, row.source_distribution_site_name)
return row.is_cross_tenant ? `跨站 · ${source}` : `本站 · ${source}`
}
if (!row.is_cross_tenant) return '本站货源'
return `跨站 · ${siteName(row.source_tenant_code, row.source_tenant_name)}`
}
function listingSiteText(item: AvailableListing) {
if (item.source_distribution_site_id > 0) {
const source = siteName(item.source_distribution_site_code, item.source_distribution_site_name)
return item.is_cross_tenant ? `跨站 · ${source}` : `本站 · ${source}`
}
const source = siteName(item.source_tenant_code, item.source_tenant_name)
return item.is_cross_tenant ? `跨站 · ${source}` : `本站 · ${source}`
}