fix(invitation): 线下提号接入邀请返利并补发历史漏发,返利比例文案改读配置
- 线下提号(admin_pickups)不走订单结账流程,此前完全不发邀请返利;现于 Complete() 内按号主利润对半基数发放卖家侧返利 - 迁移 000086:invitation_rebate_ledgers 增加 source 列,唯一键调整为 (source, order_id, trade_role),支持提号来源且保持幂等 - 新增 cmd/rebate-backfill 幂等补发脚本:默认预演,加 -apply 才写库 - 返利阶段文案不再写死 60%/30%/15%,改为按本次实际生效比例生成(钱包流水备注同步修正) - 前台邀请页阶段标签/横幅/空态文案、移动端个人中心入口文案均改读接口配置,并修掉合法 0% 比例被 || 覆盖成 60% 的问题 - 新增单测:提号返利发放与幂等、cycleLabel 跟随后台配置
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -38,6 +39,10 @@ const (
|
||||
DefaultFirstMonthRate = 0.6000
|
||||
DefaultSecondMonthRate = 0.3000
|
||||
DefaultSubsequentRate = 0.1500
|
||||
|
||||
// 返利来源单据类型:source=biz_type 用于区分返利由哪类单据产生。
|
||||
RebateSourceRentalOrder = "rental_order"
|
||||
RebateSourcePickup = "pickup"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
@@ -433,7 +438,7 @@ func (r *Repository) ProcessOrderInvitationRebate(ctx context.Context, tx *gorm.
|
||||
BalanceType: "available",
|
||||
BizType: "invite_rebate",
|
||||
BizNo: order.OrderNo,
|
||||
Remark: fmt.Sprintf("邀请买家成交返利(订单 %s,%s)", order.OrderNo, cycleLabel(cycle)),
|
||||
Remark: fmt.Sprintf("邀请买家成交返利(订单 %s,%s)", order.OrderNo, cycleLabel(cycle, rate)),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -495,7 +500,7 @@ func (r *Repository) ProcessOrderInvitationRebate(ctx context.Context, tx *gorm.
|
||||
BalanceType: "available",
|
||||
BizType: "invite_rebate",
|
||||
BizNo: order.OrderNo,
|
||||
Remark: fmt.Sprintf("邀请卖家成交返利(订单 %s,%s)", order.OrderNo, cycleLabel(cycle)),
|
||||
Remark: fmt.Sprintf("邀请卖家成交返利(订单 %s,%s)", order.OrderNo, cycleLabel(cycle, rate)),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -508,17 +513,137 @@ func (r *Repository) ProcessOrderInvitationRebate(ctx context.Context, tx *gorm.
|
||||
return nil
|
||||
}
|
||||
|
||||
func cycleLabel(cycle string) string {
|
||||
// cycleLabel 生成返利周期的展示文案。
|
||||
// 比例必须取本次实际生效的 rate(后台/分销站可改配置),
|
||||
// 不能写死 60%/30%/15%,否则钱包流水备注会和实际比例不一致。
|
||||
func cycleLabel(cycle string, rate float64) string {
|
||||
percent := formatRebateRate(rate)
|
||||
switch cycle {
|
||||
case CycleFirstMonth:
|
||||
return "首月高额奖励 60%"
|
||||
return "首月奖励 " + percent
|
||||
case CycleSecondMonth:
|
||||
return "次月奖励 30%"
|
||||
return "次月奖励 " + percent
|
||||
case CycleSubsequent:
|
||||
return "长期奖励 " + percent
|
||||
default:
|
||||
return "长期奖励 15%"
|
||||
return "返利 " + percent
|
||||
}
|
||||
}
|
||||
|
||||
// formatRebateRate 把比例(0.6)格式化为展示文案(60%)。
|
||||
// 先按配置列精度 decimal(6,4) 规整,避免浮点尾差显示成 59.999999%。
|
||||
func formatRebateRate(rate float64) string {
|
||||
percent := math.Round(rate*10000) / 100
|
||||
return strconv.FormatFloat(percent, 'f', -1, 64) + "%"
|
||||
}
|
||||
|
||||
// SellerSideRebateInput 单边(卖家/服务提供方)返利结算入参。
|
||||
//
|
||||
// 租赁订单与线下提号共用同一套规则:按被邀请人实名生效时刻计算比例阶梯、
|
||||
// 用 (source, order_id, trade_role) 唯一键保证幂等、入账到邀请人钱包可用余额。
|
||||
// 差异只在来源标识与收益基数,因此统一走这里,避免两处各写一遍算钱逻辑。
|
||||
type SellerSideRebateInput struct {
|
||||
Source string // 来源单据类型:RebateSourceRentalOrder / RebateSourcePickup
|
||||
SourceID uint64 // 来源单据 ID
|
||||
SourceNo string // 来源单据编号(写入返利流水与钱包流水备注)
|
||||
SellerID uint64 // 交易人(被邀请人本人)
|
||||
GrossBasisCent int64 // 来源单据整体收益基数(分)
|
||||
BasisCent int64 // 单边收益基数(分),通常为 GrossBasisCent 对半
|
||||
SiteID uint64 // 成本分担站点(0=主站)
|
||||
RateReferenceAt time.Time // 比例阶梯的基准时刻(单据完成时刻);零值取当前时间
|
||||
SettledAt time.Time // 实际入账时刻;零值取当前时间
|
||||
RemarkPrefix string // 钱包流水备注前缀
|
||||
}
|
||||
|
||||
// AwardSellerSideRebateTx 在事务内为「交易人的邀请人」发放单边返利(幂等)。
|
||||
// 未命中生效邀请关系、邀请人未实名、返利配置关闭或金额为 0 时不写任何记录并返回 0。
|
||||
func AwardSellerSideRebateTx(tx *gorm.DB, in SellerSideRebateInput) (int64, error) {
|
||||
if tx == nil || in.SellerID == 0 || in.SourceID == 0 || in.BasisCent <= 0 {
|
||||
return 0, nil
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
var invitation model.UserInvitation
|
||||
if err := tx.WithContext(ctx).
|
||||
Where("invitee_id = ? AND status = ?", in.SellerID, StatusActive).
|
||||
First(&invitation).Error; err != nil {
|
||||
return 0, nil
|
||||
}
|
||||
if invitation.VerifiedAt == nil {
|
||||
return 0, nil
|
||||
}
|
||||
var inviter model.User
|
||||
if err := tx.WithContext(ctx).First(&inviter, invitation.InviterID).Error; err != nil {
|
||||
return 0, nil
|
||||
}
|
||||
if inviter.RealnameStatus != "verified" || inviter.ID == in.SellerID {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
repo := NewRepository(tx)
|
||||
cfg, _ := repo.GetRebateConfig(ctx, in.SiteID)
|
||||
if cfg != nil && !cfg.Enabled {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
referenceAt := in.RateReferenceAt
|
||||
if referenceAt.IsZero() {
|
||||
referenceAt = time.Now()
|
||||
}
|
||||
settledAt := in.SettledAt
|
||||
if settledAt.IsZero() {
|
||||
settledAt = time.Now()
|
||||
}
|
||||
rate, cycle, days := CalculateRebateRate(*invitation.VerifiedAt, referenceAt, cfg)
|
||||
rebateCent := int64(math.Round(float64(in.BasisCent) * rate))
|
||||
if rebateCent <= 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
grossBasis := in.GrossBasisCent
|
||||
if grossBasis <= 0 {
|
||||
grossBasis = in.BasisCent
|
||||
}
|
||||
ledger := model.InvitationRebateLedger{
|
||||
OrderID: in.SourceID,
|
||||
OrderNo: in.SourceNo,
|
||||
Source: in.Source,
|
||||
DistributionSiteID: in.SiteID,
|
||||
InviterID: invitation.InviterID,
|
||||
InviteeID: in.SellerID,
|
||||
TradeRole: "seller",
|
||||
OrderProfitBasisCent: grossBasis,
|
||||
SingleSideBasisCent: in.BasisCent,
|
||||
RateApplied: rate,
|
||||
DaysSinceVerified: days,
|
||||
CycleStage: cycle,
|
||||
RebateAmountCent: rebateCent,
|
||||
WalletLedgerNo: fmt.Sprintf("REB-S-%s-%d", in.SourceNo, invitation.InviterID),
|
||||
Status: "settled",
|
||||
SettledAt: settledAt,
|
||||
}
|
||||
if err := tx.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(&ledger).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if ledger.ID == 0 {
|
||||
// 同来源同角色已有返利记录:幂等跳过(重复调用、补发脚本重跑均安全)。
|
||||
return 0, nil
|
||||
}
|
||||
if err := wallet.AppendEntries(tx, wallet.Entry{
|
||||
UserID: invitation.InviterID,
|
||||
DistributionSiteID: in.SiteID,
|
||||
Direction: "in",
|
||||
AmountCent: rebateCent,
|
||||
BalanceType: "available",
|
||||
BizType: "invite_rebate",
|
||||
BizNo: in.SourceNo,
|
||||
Remark: fmt.Sprintf("%s(%s,%s)", in.RemarkPrefix, in.SourceNo, cycleLabel(cycle, rate)),
|
||||
}); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return rebateCent, nil
|
||||
}
|
||||
|
||||
// GetUserSummary 获取用户个人中心的邀请返利数据看板
|
||||
func (r *Repository) GetUserSummary(ctx context.Context, userID uint64, siteID uint64) (*UserInvitationSummaryDTO, error) {
|
||||
if r == nil || r.db == nil {
|
||||
@@ -835,6 +960,7 @@ func (r *Repository) ListAdminRebateLedgers(ctx context.Context, query AdminReba
|
||||
ID uint64 `gorm:"column:id"`
|
||||
OrderID uint64 `gorm:"column:order_id"`
|
||||
OrderNo string `gorm:"column:order_no"`
|
||||
Source string `gorm:"column:source"`
|
||||
DistributionSiteID uint64 `gorm:"column:distribution_site_id"`
|
||||
InviterID uint64 `gorm:"column:inviter_id"`
|
||||
InviterPhone string `gorm:"column:inviter_phone"`
|
||||
@@ -853,7 +979,7 @@ func (r *Repository) ListAdminRebateLedgers(ctx context.Context, query AdminReba
|
||||
|
||||
var rows []rawLedger
|
||||
offset := (query.Page - 1) * query.PageSize
|
||||
err := db.Select("irl.id, irl.order_id, irl.order_no, irl.distribution_site_id, irl.inviter_id, inviter.phone as inviter_phone, irl.invitee_id, invitee.phone as invitee_phone, irl.trade_role, irl.order_profit_basis_cent, irl.single_side_basis_cent, irl.rate_applied, irl.days_since_verified, irl.cycle_stage, irl.rebate_amount_cent, irl.status, irl.settled_at").
|
||||
err := db.Select("irl.id, irl.order_id, irl.order_no, irl.source, irl.distribution_site_id, irl.inviter_id, inviter.phone as inviter_phone, irl.invitee_id, invitee.phone as invitee_phone, irl.trade_role, irl.order_profit_basis_cent, irl.single_side_basis_cent, irl.rate_applied, irl.days_since_verified, irl.cycle_stage, irl.rebate_amount_cent, irl.status, irl.settled_at").
|
||||
Order("irl.id DESC").Offset(offset).Limit(query.PageSize).Scan(&rows).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
@@ -865,6 +991,7 @@ func (r *Repository) ListAdminRebateLedgers(ctx context.Context, query AdminReba
|
||||
"id": row.ID,
|
||||
"order_id": row.OrderID,
|
||||
"order_no": row.OrderNo,
|
||||
"source": row.Source,
|
||||
"distribution_site_id": row.DistributionSiteID,
|
||||
"inviter_id": row.InviterID,
|
||||
"inviter_phone": row.InviterPhone,
|
||||
|
||||
Reference in New Issue
Block a user