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
+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,
}
}