feat(tenant): 隔离邀请返利与推广追踪数据
This commit is contained in:
@@ -287,21 +287,27 @@ func (r *Repository) GetRebateConfig(ctx context.Context, siteID uint64) (*model
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var cfg model.InvitationRebateConfig
|
||||
if siteID > 0 {
|
||||
err := r.db.WithContext(ctx).Where("distribution_site_id = ?", siteID).First(&cfg).Error
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND distribution_site_id = ?", tenantID, siteID).
|
||||
First(&cfg).Error
|
||||
if err == nil {
|
||||
return &cfg, nil
|
||||
}
|
||||
}
|
||||
// 回退主站配置 (distribution_site_id = 0)
|
||||
err := r.db.WithContext(ctx).Where("distribution_site_id = 0").First(&cfg).Error
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND distribution_site_id = 0", tenantID).
|
||||
First(&cfg).Error
|
||||
if err == nil {
|
||||
return &cfg, nil
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// 返回默认硬编码
|
||||
return &model.InvitationRebateConfig{
|
||||
TenantID: tenantID,
|
||||
DistributionSiteID: 0,
|
||||
FirstMonthRate: DefaultFirstMonthRate,
|
||||
SecondMonthRate: DefaultSecondMonthRate,
|
||||
@@ -317,13 +323,17 @@ func (r *Repository) UpdateRebateConfig(ctx context.Context, siteID uint64, req
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var cfg model.InvitationRebateConfig
|
||||
err := r.db.WithContext(ctx).Where("distribution_site_id = ?", siteID).First(&cfg).Error
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND distribution_site_id = ?", tenantID, siteID).
|
||||
First(&cfg).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
cfg = model.InvitationRebateConfig{
|
||||
TenantID: tenantID,
|
||||
DistributionSiteID: siteID,
|
||||
FirstMonthRate: req.FirstMonthRate,
|
||||
SecondMonthRate: req.SecondMonthRate,
|
||||
@@ -333,7 +343,7 @@ func (r *Repository) UpdateRebateConfig(ctx context.Context, siteID uint64, req
|
||||
if req.Enabled != nil {
|
||||
cfg.Enabled = *req.Enabled
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Select("DistributionSiteID", "FirstMonthRate", "SecondMonthRate", "SubsequentRate", "Enabled").Create(&cfg).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Select("TenantID", "DistributionSiteID", "FirstMonthRate", "SecondMonthRate", "SubsequentRate", "Enabled").Create(&cfg).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cfg, nil
|
||||
@@ -619,7 +629,7 @@ func AwardSellerSideRebateTx(tx *gorm.DB, in SellerSideRebateInput) (int64, erro
|
||||
if tx == nil || in.SellerID == 0 || in.SourceID == 0 || in.BasisCent <= 0 {
|
||||
return 0, nil
|
||||
}
|
||||
ctx := context.Background()
|
||||
ctx := invitationTenantContextForUser(tx, in.SellerID)
|
||||
|
||||
var invitation model.UserInvitation
|
||||
if err := tx.WithContext(ctx).
|
||||
@@ -1334,7 +1344,7 @@ func ProcessOrderInvitationRebateWithOptionsTx(tx *gorm.DB, orderID uint64, opts
|
||||
return nil
|
||||
}
|
||||
repo := NewRepository(tx)
|
||||
return repo.processOrderInvitationRebate(context.Background(), tx, orderID, opts)
|
||||
return repo.processOrderInvitationRebate(invitationTenantContextForOrder(tx, orderID), tx, orderID, opts)
|
||||
}
|
||||
|
||||
// OnUserRealnameVerifiedTx 便捷事务入口,供实名认证通过时调用
|
||||
@@ -1343,5 +1353,25 @@ func OnUserRealnameVerifiedTx(tx *gorm.DB, userID uint64) error {
|
||||
return nil
|
||||
}
|
||||
repo := NewRepository(tx)
|
||||
return repo.OnUserRealnameVerified(context.Background(), userID)
|
||||
return repo.OnUserRealnameVerified(invitationTenantContextForUser(tx, userID), userID)
|
||||
}
|
||||
|
||||
func invitationTenantContextForOrder(tx *gorm.DB, orderID uint64) context.Context {
|
||||
ctx := context.Background()
|
||||
var tenantID uint64
|
||||
_ = tx.Table("rental_orders").Select("buyer_tenant_id").Where("id = ?", orderID).Scan(&tenantID).Error
|
||||
if tenantID > 0 {
|
||||
ctx = tenancy.WithScope(ctx, tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func invitationTenantContextForUser(tx *gorm.DB, userID uint64) context.Context {
|
||||
ctx := context.Background()
|
||||
var tenantID uint64
|
||||
_ = tx.Model(&model.User{}).Select("tenant_id").Where("id = ?", userID).Scan(&tenantID).Error
|
||||
if tenantID > 0 {
|
||||
ctx = tenancy.WithScope(ctx, tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user