邀请
This commit is contained in:
@@ -67,22 +67,53 @@ func NewActivateOrderLogic(svc *svc.ServiceContext) *ActivateOrderLogic {
|
||||
// It handles the complete workflow of activating a paid order including validation,
|
||||
// processing based on order type, and finalization.
|
||||
func (l *ActivateOrderLogic) ProcessTask(ctx context.Context, task *asynq.Task) error {
|
||||
logger.WithContext(ctx).Info("[ActivateOrderLogic] 开始处理订单激活任务",
|
||||
logger.Field("payload", string(task.Payload())))
|
||||
|
||||
payload, err := l.parsePayload(ctx, task.Payload())
|
||||
if err != nil {
|
||||
return nil // Log and continue
|
||||
logger.WithContext(ctx).Error("[ActivateOrderLogic] 解析 payload 失败,跳过任务",
|
||||
logger.Field("error", err.Error()))
|
||||
return nil // payload 解析失败不重试,因为重试也会失败
|
||||
}
|
||||
|
||||
logger.WithContext(ctx).Info("[ActivateOrderLogic] 正在验证订单",
|
||||
logger.Field("order_no", payload.OrderNo))
|
||||
|
||||
orderInfo, err := l.validateAndGetOrder(ctx, payload.OrderNo)
|
||||
if err != nil {
|
||||
return nil // Log and continue
|
||||
// 如果订单不存在或状态不对,不重试
|
||||
if errors.Is(err, ErrInvalidOrderStatus) {
|
||||
logger.WithContext(ctx).Info("[ActivateOrderLogic] 订单状态不是已支付,跳过",
|
||||
logger.Field("order_no", payload.OrderNo))
|
||||
return nil
|
||||
}
|
||||
// 数据库查询失败,应该重试
|
||||
logger.WithContext(ctx).Error("[ActivateOrderLogic] 查询订单失败,将重试",
|
||||
logger.Field("order_no", payload.OrderNo),
|
||||
logger.Field("error", err.Error()))
|
||||
return err
|
||||
}
|
||||
|
||||
logger.WithContext(ctx).Info("[ActivateOrderLogic] 订单验证通过,开始处理",
|
||||
logger.Field("order_no", orderInfo.OrderNo),
|
||||
logger.Field("order_type", orderInfo.Type),
|
||||
logger.Field("user_id", orderInfo.UserId))
|
||||
|
||||
if err = l.processOrderByType(ctx, orderInfo); err != nil {
|
||||
logger.WithContext(ctx).Error("[ActivateOrderLogic] Process task failed", logger.Field("error", err.Error()))
|
||||
return nil
|
||||
logger.WithContext(ctx).Error("[ActivateOrderLogic] 处理订单失败,将重试",
|
||||
logger.Field("order_no", orderInfo.OrderNo),
|
||||
logger.Field("order_type", orderInfo.Type),
|
||||
logger.Field("error", err.Error()))
|
||||
return err // 返回 err 允许 asynq 重试
|
||||
}
|
||||
|
||||
l.finalizeCouponAndOrder(ctx, orderInfo)
|
||||
|
||||
logger.WithContext(ctx).Info("[ActivateOrderLogic] ✅ 订单激活成功",
|
||||
logger.Field("order_no", orderInfo.OrderNo),
|
||||
logger.Field("order_type", orderInfo.Type),
|
||||
logger.Field("user_id", orderInfo.UserId))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -355,7 +386,7 @@ func (l *ActivateOrderLogic) createUserSubscription(ctx context.Context, orderIn
|
||||
// This runs asynchronously to avoid blocking the main order processing flow.
|
||||
func (l *ActivateOrderLogic) handleCommission(ctx context.Context, userInfo *user.User, orderInfo *order.Order) {
|
||||
if !l.shouldProcessCommission(userInfo, orderInfo.IsNew) {
|
||||
l.grantGiftDaysToBothParties(ctx, userInfo)
|
||||
l.grantGiftDaysToBothParties(ctx, userInfo, orderInfo.OrderNo)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -423,12 +454,12 @@ func (l *ActivateOrderLogic) handleCommission(ctx context.Context, userInfo *use
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ActivateOrderLogic) grantGiftDaysToBothParties(ctx context.Context, referee *user.User) {
|
||||
func (l *ActivateOrderLogic) grantGiftDaysToBothParties(ctx context.Context, referee *user.User, orderNo string) {
|
||||
giftDays := l.svc.Config.Invite.GiftDays
|
||||
if giftDays <= 0 || referee == nil || referee.Id == 0 {
|
||||
return
|
||||
}
|
||||
_ = l.grantGiftDays(ctx, referee, int(giftDays))
|
||||
_ = l.grantGiftDays(ctx, referee, int(giftDays), orderNo, "邀请赠送")
|
||||
if referee.RefererId == 0 {
|
||||
return
|
||||
}
|
||||
@@ -436,10 +467,10 @@ func (l *ActivateOrderLogic) grantGiftDaysToBothParties(ctx context.Context, ref
|
||||
if err != nil || referer == nil {
|
||||
return
|
||||
}
|
||||
_ = l.grantGiftDays(ctx, referer, int(giftDays))
|
||||
_ = l.grantGiftDays(ctx, referer, int(giftDays), orderNo, "邀请赠送")
|
||||
}
|
||||
|
||||
func (l *ActivateOrderLogic) grantGiftDays(ctx context.Context, u *user.User, days int) error {
|
||||
func (l *ActivateOrderLogic) grantGiftDays(ctx context.Context, u *user.User, days int, orderNo string, remark string) error {
|
||||
if u == nil || days <= 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -451,7 +482,28 @@ func (l *ActivateOrderLogic) grantGiftDays(ctx context.Context, u *user.User, da
|
||||
return err
|
||||
}
|
||||
activeSubscribe.ExpireTime = activeSubscribe.ExpireTime.Add(time.Duration(days) * 24 * time.Hour)
|
||||
return l.svc.UserModel.UpdateSubscribe(ctx, activeSubscribe)
|
||||
err = l.svc.UserModel.UpdateSubscribe(ctx, activeSubscribe)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Insert system log
|
||||
giftLog := &log.Gift{
|
||||
Type: log.GiftTypeIncrease,
|
||||
OrderNo: orderNo,
|
||||
SubscribeId: activeSubscribe.Id,
|
||||
Amount: int64(days),
|
||||
Balance: u.Balance,
|
||||
Remark: remark,
|
||||
Timestamp: time.Now().UnixMilli(),
|
||||
}
|
||||
content, _ := giftLog.Marshal()
|
||||
return l.svc.LogModel.Insert(ctx, &log.SystemLog{
|
||||
Type: log.TypeGift.Uint8(),
|
||||
Date: time.Now().Format("2006-01-02"),
|
||||
ObjectID: u.Id,
|
||||
Content: string(content),
|
||||
})
|
||||
}
|
||||
|
||||
// shouldProcessCommission determines if commission should be processed based on
|
||||
|
||||
Reference in New Issue
Block a user