From 9aa69138831ac1770b7de2197efc0d099e339947 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Fri, 20 Jun 2025 15:12:34 +0800 Subject: [PATCH] fix promotion --- .../api/internal/logic/pay/paymentlogic.go | 21 ++++++++++++++++++- .../global_sourceinterceptor_middleware.go | 7 +++++-- .../api/internal/queue/paySuccessNotify.go | 21 ++++++++++++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/app/main/api/internal/logic/pay/paymentlogic.go b/app/main/api/internal/logic/pay/paymentlogic.go index cd39d7e..70f042c 100644 --- a/app/main/api/internal/logic/pay/paymentlogic.go +++ b/app/main/api/internal/logic/pay/paymentlogic.go @@ -143,7 +143,26 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 获取保存订单ID失败: %+v", lastInsertIdErr) } orderID = insertedOrderID - + promoteKey, ok := l.ctx.Value("promoteKey").(string) + if ok && promoteKey != "" { + url := fmt.Sprintf("%s/%s", l.svcCtx.Config.AdminPromotion.URLDomain, promoteKey) + promoteLink, err := l.svcCtx.AdminPromotionLinkModel.FindOneByUrl(l.ctx, url) + if err != nil && !errors.Is(err, model.ErrNotFound) { + return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 查找推广链接失败: %+v", err) + } + if promoteLink != nil { + promoteOrder := &model.AdminPromotionOrder{ + OrderId: orderID, + LinkId: promoteLink.Id, + UserId: userID, + AdminUserId: promoteLink.AdminUserId, + } + _, insertPromoteOrderErr := l.svcCtx.AdminPromotionOrderModel.Insert(l.ctx, nil, promoteOrder) + if insertPromoteOrderErr != nil { + return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 保存推广订单失败: %+v", insertPromoteOrderErr) + } + } + } if data.AgentIdentifier != "" { agent, parsingErr := l.agentParsing(data.AgentIdentifier) if parsingErr != nil { diff --git a/app/main/api/internal/middleware/global_sourceinterceptor_middleware.go b/app/main/api/internal/middleware/global_sourceinterceptor_middleware.go index c7197b3..88f2c2e 100644 --- a/app/main/api/internal/middleware/global_sourceinterceptor_middleware.go +++ b/app/main/api/internal/middleware/global_sourceinterceptor_middleware.go @@ -7,19 +7,22 @@ import ( const ( PlatformKey = "X-Platform" + PromoteKey = "X-Promote-Key" ) func GlobalSourceInterceptor(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // 获取请求头 X-Platform 的值 platform := r.Header.Get(PlatformKey) - + promoteValue := r.Header.Get(PromoteKey) // 将值放入新的 context 中 ctx := r.Context() if platform != "" { ctx = context.WithValue(ctx, "platform", platform) } - + if promoteValue != "" { + ctx = context.WithValue(ctx, "promoteKey", promoteValue) + } // 通过 r.WithContext 将更新后的 ctx 传递给后续的处理函数 r = r.WithContext(ctx) diff --git a/app/main/api/internal/queue/paySuccessNotify.go b/app/main/api/internal/queue/paySuccessNotify.go index dc22860..64aa144 100644 --- a/app/main/api/internal/queue/paySuccessNotify.go +++ b/app/main/api/internal/queue/paySuccessNotify.go @@ -5,6 +5,7 @@ import ( "database/sql" "encoding/hex" "encoding/json" + "errors" "fmt" "os" "regexp" @@ -139,7 +140,10 @@ func (l *PaySuccessNotifyUserHandler) ProcessTask(ctx context.Context, t *asynq. if err != nil { return l.handleError(ctx, err, order, query) } - + err = l.promotionOrderStats(ctx, order) + if err != nil { + logx.Errorf("处理推广订单统计失败,订单ID: %d, 错误: %v", order.Id, err) + } _, delErr := l.svcCtx.Redis.DelCtx(ctx, redisKey) if delErr != nil { logx.Errorf("删除Redis缓存失败,但任务已成功处理,订单ID: %d, 错误: %v", order.Id, delErr) @@ -296,6 +300,21 @@ func (l *PaySuccessNotifyUserHandler) createNullString(value string) sql.NullStr } } +// 处理推广订单统计 +func (l *PaySuccessNotifyUserHandler) promotionOrderStats(ctx context.Context, order *model.Order) error { + promotionOrder, err := l.svcCtx.AdminPromotionOrderModel.FindOneByOrderId(ctx, order.Id) + if err != nil && !errors.Is(err, model.ErrNotFound) { + return fmt.Errorf("获取推广订单失败: %+v", err) + } + if promotionOrder != nil { + err = l.svcCtx.AdminPromotionLinkStatsService.UpdatePaymentStats(ctx, promotionOrder.LinkId, float64(order.Amount)) + if err != nil { + return fmt.Errorf("更新推广链接支付统计失败: %+v", err) + } + } + return nil +} + // desensitizeParams 对敏感数据进行脱敏处理 func (l *PaySuccessNotifyUserHandler) desensitizeParams(data []byte) ([]byte, error) { // 解析JSON数据到map