From 4099f7eca0b4db29243e8a5c12735ae9a0976714 Mon Sep 17 00:00:00 2001 From: ziming Date: Fri, 13 Jun 2025 10:32:55 +0800 Subject: [PATCH 01/13] timeSliceQuery --- internal/biz/timeslicequery/query.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/biz/timeslicequery/query.go b/internal/biz/timeslicequery/query.go index 1b626f1..5126621 100644 --- a/internal/biz/timeslicequery/query.go +++ b/internal/biz/timeslicequery/query.go @@ -2,7 +2,6 @@ package timeslicequery import ( "context" - "github.com/go-kratos/kratos/v2/log" "voucher/internal/biz/bo" ) @@ -40,7 +39,6 @@ func (v *Query) queryUsed(ctx context.Context, order *bo.OrderBo, notifyNum *int func (v *Query) queryExpired(ctx context.Context, order *bo.OrderBo) error { if order.Status.IsExpired() { - log.Warnf("券状态已是已过期,忽略不处理,orderNo:%s", order.OrderNo) return nil } From 41e041156673c77eb4dced56e80a6067235c4b1c Mon Sep 17 00:00:00 2001 From: ziming Date: Fri, 13 Jun 2025 10:43:55 +0800 Subject: [PATCH 02/13] timeSliceQuery --- internal/biz/cron_notice.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/biz/cron_notice.go b/internal/biz/cron_notice.go index b59f2c3..13d5af9 100644 --- a/internal/biz/cron_notice.go +++ b/internal/biz/cron_notice.go @@ -153,6 +153,7 @@ func (v *VoucherBiz) ExecuteNotice(ctx context.Context, req *bo.FindInBatchesUse "searchTime": req.StartTime.Format(time.DateTime) + "到" + req.EndTime.Format(time.DateTime), "num": num, "notifyNum": notifyNum, + "errNum": errNum, "elapsed": time.Now().Sub(start).String(), } log.Warnf("订单定时通知,%+v", logFields) From 2fc97caa7cfdf88b0b5ad7fa1835d7c31370f8f3 Mon Sep 17 00:00:00 2001 From: ziming Date: Fri, 13 Jun 2025 17:32:15 +0800 Subject: [PATCH 03/13] timeSliceQuery --- internal/biz/timeslicequery/mq.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/biz/timeslicequery/mq.go b/internal/biz/timeslicequery/mq.go index 87dc580..d1388a8 100644 --- a/internal/biz/timeslicequery/mq.go +++ b/internal/biz/timeslicequery/mq.go @@ -123,8 +123,8 @@ func (v *Query) Consumer(ctx context.Context, msg string) error { } executeEnd := time.Now() - log.Warnf("微信券查询处理,耗时:%s,结束时间%s,manager%s", executeEnd.Sub(executeStart).String(), executeEnd.String(), reqStr) - fmt.Printf("微信券查询处理,耗时:%s,结束时间%s,manager%s", executeEnd.Sub(executeStart).String(), executeEnd.String(), reqStr) + log.Warnf("微信券查询处理,耗时:%s,结束时间%s,manager:%s", executeEnd.Sub(executeStart).String(), executeEnd.String(), reqStr) + fmt.Printf("微信券查询处理,耗时:%s,结束时间%s,manager:%s", executeEnd.Sub(executeStart).String(), executeEnd.String(), reqStr) return nil } From 8b85ab529569481c07f55b9eb8a72848e3fc62f8 Mon Sep 17 00:00:00 2001 From: ziming Date: Fri, 13 Jun 2025 17:36:39 +0800 Subject: [PATCH 04/13] timeSliceQuery --- internal/biz/timeslicequery/mq.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/biz/timeslicequery/mq.go b/internal/biz/timeslicequery/mq.go index d1388a8..feb4bf5 100644 --- a/internal/biz/timeslicequery/mq.go +++ b/internal/biz/timeslicequery/mq.go @@ -119,7 +119,6 @@ func (v *Query) Consumer(ctx context.Context, msg string) error { if err = v.execute(ctx, req); err != nil { log.Errorf("微信券查询处理,失败:%s,msg:%s,err:%v", executeStartStr, msg, err) - return fmt.Errorf("微信券查询处理,失败:%s,msg:%s,err:%v", executeStartStr, msg, err) } executeEnd := time.Now() From 387703f7611e66acca9e78a874299766e8905c6b Mon Sep 17 00:00:00 2001 From: ziming Date: Fri, 13 Jun 2025 17:50:29 +0800 Subject: [PATCH 05/13] timeSliceQuery --- internal/biz/timeslicequery/mq.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/biz/timeslicequery/mq.go b/internal/biz/timeslicequery/mq.go index feb4bf5..18f250d 100644 --- a/internal/biz/timeslicequery/mq.go +++ b/internal/biz/timeslicequery/mq.go @@ -114,8 +114,8 @@ func (v *Query) Consumer(ctx context.Context, msg string) error { executeStart := time.Now() executeStartStr := executeStart.Format(time.DateTime) - log.Warnf("微信券查询处理,开始:%s,msg:%s,manager%s", executeStartStr, msg, reqStr) - fmt.Printf("微信券查询处理,开始:%s,msg:%s,manager%s", executeStartStr, msg, reqStr) + log.Warnf("微信券查询处理,开始:%s,msg:%s,manager:%s", executeStartStr, msg, reqStr) + fmt.Printf("微信券查询处理,开始:%s,msg:%s,manager:%s", executeStartStr, msg, reqStr) if err = v.execute(ctx, req); err != nil { log.Errorf("微信券查询处理,失败:%s,msg:%s,err:%v", executeStartStr, msg, err) From cfac0a3be95fa86c563a21c771462cd3c564d053 Mon Sep 17 00:00:00 2001 From: ziming Date: Fri, 13 Jun 2025 18:10:36 +0800 Subject: [PATCH 06/13] timeSliceQuery --- internal/biz/timeslicequery/execute.go | 13 ++++++++++--- internal/biz/timeslicequery/mq.go | 17 +++-------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/internal/biz/timeslicequery/execute.go b/internal/biz/timeslicequery/execute.go index 5f6e11d..fd00569 100644 --- a/internal/biz/timeslicequery/execute.go +++ b/internal/biz/timeslicequery/execute.go @@ -12,16 +12,23 @@ import ( func (v *Query) execute(ctx context.Context, req *timeslice.Manager) error { + start := time.Now() + managerStartStr := req.StartTime.Format(time.DateTime) managerEndStr := req.EndTime.Format(time.DateTime) + log.Warnf("微信券查询处理,%s到%s,开始", managerStartStr, managerEndStr) + fmt.Printf("微信券查询处理,%s到%s,开始\n", managerStartStr, managerEndStr) + taskCount, err := timeslice.NewManager(v.callbackFunc).Run(ctx, req) if err != nil { log.Errorf("微信券查询处理,%s到%s,失败:%v", managerStartStr, managerEndStr, err) } - fmt.Printf("微信券查询处理,%s到%s,总任务数:%d\n", managerStartStr, managerEndStr, taskCount) - log.Warnf("微信券查询处理,%s到%s,总任务数:%d", managerStartStr, managerEndStr, taskCount) + elapsed := time.Now().Sub(start).String() + + log.Warnf("微信券查询处理,%s到%s,总任务数:%d,总耗时:%s", managerStartStr, managerEndStr, taskCount, elapsed) + fmt.Printf("微信券查询处理,%s到%s,总任务数:%d,总耗时:%s\n", managerStartStr, managerEndStr, taskCount, elapsed) return nil } @@ -76,7 +83,7 @@ func (v *Query) callbackFunc(ctx context.Context, req *timeslice.Task) error { end := time.Now() - logFields := map[string]interface{}{ + logFields := map[string]any{ "searchTime": currentStartTimeStr + "到" + currentEndTimeStr, "num": num, "notifyNum": notifyNum, diff --git a/internal/biz/timeslicequery/mq.go b/internal/biz/timeslicequery/mq.go index 18f250d..9db519b 100644 --- a/internal/biz/timeslicequery/mq.go +++ b/internal/biz/timeslicequery/mq.go @@ -106,24 +106,13 @@ func (v *Query) Consumer(ctx context.Context, msg string) error { req, err := v.getManager(msg) if err != nil { - return err + log.Errorf("微信券查询处理,前置参数处理失败,msg:%s,err:%v", msg, err) + return nil } - reqStr := req.String() - - executeStart := time.Now() - executeStartStr := executeStart.Format(time.DateTime) - - log.Warnf("微信券查询处理,开始:%s,msg:%s,manager:%s", executeStartStr, msg, reqStr) - fmt.Printf("微信券查询处理,开始:%s,msg:%s,manager:%s", executeStartStr, msg, reqStr) - if err = v.execute(ctx, req); err != nil { - log.Errorf("微信券查询处理,失败:%s,msg:%s,err:%v", executeStartStr, msg, err) + log.Errorf("微信券查询处理,失败,msg:%s,err:%v", msg, err) } - executeEnd := time.Now() - log.Warnf("微信券查询处理,耗时:%s,结束时间%s,manager:%s", executeEnd.Sub(executeStart).String(), executeEnd.String(), reqStr) - fmt.Printf("微信券查询处理,耗时:%s,结束时间%s,manager:%s", executeEnd.Sub(executeStart).String(), executeEnd.String(), reqStr) - return nil } From ba8d3492732219ebd10190cf3f4565d325304dbf Mon Sep 17 00:00:00 2001 From: ziming Date: Fri, 13 Jun 2025 18:11:40 +0800 Subject: [PATCH 07/13] timeSliceQuery --- internal/biz/timeslicequery/execute.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/biz/timeslicequery/execute.go b/internal/biz/timeslicequery/execute.go index fd00569..389ca47 100644 --- a/internal/biz/timeslicequery/execute.go +++ b/internal/biz/timeslicequery/execute.go @@ -87,6 +87,7 @@ func (v *Query) callbackFunc(ctx context.Context, req *timeslice.Task) error { "searchTime": currentStartTimeStr + "到" + currentEndTimeStr, "num": num, "notifyNum": notifyNum, + "errNum": errNum, "elapsed": end.Sub(start).String(), } log.Warnf("微信券查询处理,%s到%s,taskId:%d,处理完毕:%+v", startTimeStr, endTimeStr, req.TaskID, logFields) From cb1135e9c8dcebca2a1c76cf3a7eda0bffa5d19d Mon Sep 17 00:00:00 2001 From: ziming Date: Fri, 13 Jun 2025 18:12:42 +0800 Subject: [PATCH 08/13] timeSliceQuery --- internal/biz/timeslicequery/execute.go | 16 ++++++++-------- internal/biz/timeslicequery/mq.go | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/biz/timeslicequery/execute.go b/internal/biz/timeslicequery/execute.go index 389ca47..55e5acd 100644 --- a/internal/biz/timeslicequery/execute.go +++ b/internal/biz/timeslicequery/execute.go @@ -17,18 +17,18 @@ func (v *Query) execute(ctx context.Context, req *timeslice.Manager) error { managerStartStr := req.StartTime.Format(time.DateTime) managerEndStr := req.EndTime.Format(time.DateTime) - log.Warnf("微信券查询处理,%s到%s,开始", managerStartStr, managerEndStr) - fmt.Printf("微信券查询处理,%s到%s,开始\n", managerStartStr, managerEndStr) + log.Warnf("微信券查询,%s到%s,开始", managerStartStr, managerEndStr) + fmt.Printf("微信券查询,%s到%s,开始\n", managerStartStr, managerEndStr) taskCount, err := timeslice.NewManager(v.callbackFunc).Run(ctx, req) if err != nil { - log.Errorf("微信券查询处理,%s到%s,失败:%v", managerStartStr, managerEndStr, err) + log.Errorf("微信券查询,%s到%s,失败:%v", managerStartStr, managerEndStr, err) } elapsed := time.Now().Sub(start).String() - log.Warnf("微信券查询处理,%s到%s,总任务数:%d,总耗时:%s", managerStartStr, managerEndStr, taskCount, elapsed) - fmt.Printf("微信券查询处理,%s到%s,总任务数:%d,总耗时:%s\n", managerStartStr, managerEndStr, taskCount, elapsed) + log.Warnf("微信券查询,%s到%s,总任务数:%d,总耗时:%s", managerStartStr, managerEndStr, taskCount, elapsed) + fmt.Printf("微信券查询,%s到%s,总任务数:%d,总耗时:%s\n", managerStartStr, managerEndStr, taskCount, elapsed) return nil } @@ -69,10 +69,10 @@ func (v *Query) callbackFunc(ctx context.Context, req *timeslice.Task) error { "stock_id": order.BatchNo, "err": err.Error(), } - log.Errorf("微信券查询处理,%s到%s,taskId:%d,错误:%+v", startTimeStr, endTimeStr, req.TaskID, logFields) + log.Errorf("微信券查询,%s到%s,taskId:%d,错误:%+v", startTimeStr, endTimeStr, req.TaskID, logFields) if errNum > 20 { - return fmt.Errorf("微信券查询处理,%s到%s,第%d个任务,已经连续发生20次错误%+v", startTimeStr, endTimeStr, req.TaskID, logFields) + return fmt.Errorf("微信券查询,%s到%s,第%d个任务,已经连续发生20次错误%+v", startTimeStr, endTimeStr, req.TaskID, logFields) } } @@ -90,7 +90,7 @@ func (v *Query) callbackFunc(ctx context.Context, req *timeslice.Task) error { "errNum": errNum, "elapsed": end.Sub(start).String(), } - log.Warnf("微信券查询处理,%s到%s,taskId:%d,处理完毕:%+v", startTimeStr, endTimeStr, req.TaskID, logFields) + log.Warnf("微信券查询,%s到%s,taskId:%d,处理完毕:%+v", startTimeStr, endTimeStr, req.TaskID, logFields) return err } diff --git a/internal/biz/timeslicequery/mq.go b/internal/biz/timeslicequery/mq.go index 9db519b..65d8462 100644 --- a/internal/biz/timeslicequery/mq.go +++ b/internal/biz/timeslicequery/mq.go @@ -106,12 +106,12 @@ func (v *Query) Consumer(ctx context.Context, msg string) error { req, err := v.getManager(msg) if err != nil { - log.Errorf("微信券查询处理,前置参数处理失败,msg:%s,err:%v", msg, err) + log.Errorf("微信券查询,前置参数处理失败,msg:%s,err:%v", msg, err) return nil } if err = v.execute(ctx, req); err != nil { - log.Errorf("微信券查询处理,失败,msg:%s,err:%v", msg, err) + log.Errorf("微信券查询,失败,msg:%s,err:%v", msg, err) } return nil From 792ce2343533a1d79a036b7a9250d6c85ac13a6e Mon Sep 17 00:00:00 2001 From: ziming Date: Mon, 16 Jun 2025 08:55:31 +0800 Subject: [PATCH 09/13] timeSliceQuery 8 --- internal/biz/cron_notice.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/biz/cron_notice.go b/internal/biz/cron_notice.go index 13d5af9..e2fe857 100644 --- a/internal/biz/cron_notice.go +++ b/internal/biz/cron_notice.go @@ -88,7 +88,7 @@ func (v *VoucherBiz) timeSliceQuery(ctx context.Context, startTime, endTime time duration := 2 * time.Hour eg := new(errgroup.Group) - eg.SetLimit(10) + eg.SetLimit(8) for start := startTime; start.Before(endTime); start = start.Add(duration) { From 87820f68368f63e921bbd03ddcd362d23d2dc597 Mon Sep 17 00:00:00 2001 From: ziming Date: Mon, 16 Jun 2025 09:41:28 +0800 Subject: [PATCH 10/13] timeSliceQuery 8 --- internal/biz/cron_notice.go | 32 ++++++++++++++++++------- internal/data/mixrepoimpl/cmb.go | 41 +++++++++++++++++++++++--------- internal/pkg/request/request.go | 12 ++++++++++ 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/internal/biz/cron_notice.go b/internal/biz/cron_notice.go index e2fe857..5de2e3f 100644 --- a/internal/biz/cron_notice.go +++ b/internal/biz/cron_notice.go @@ -7,6 +7,7 @@ import ( "github.com/go-kratos/kratos/v2/log" "github.com/redis/go-redis/v9" "golang.org/x/sync/errgroup" + "runtime" "time" "voucher/internal/biz/bo" "voucher/internal/biz/vo" @@ -115,7 +116,12 @@ func (v *VoucherBiz) timeSliceQuery(ctx context.Context, startTime, endTime time defer func() { if err := recover(); err != nil { - log.Errorf("订单定时通知,发生错误:req:%+v,err:%v", req, err) + + // 获取当前堆栈信息 + buf := make([]byte, 1024) + stackSize := runtime.Stack(buf, false) + + log.Errorf("订单定时通知,发生错误:req:%+v,err:%v\n堆栈信息:\n%s", req, err, string(buf[:stackSize])) } }() @@ -133,15 +139,22 @@ func (v *VoucherBiz) ExecuteNotice(ctx context.Context, req *bo.FindInBatchesUse num := 0 notifyNum := 0 errNum := 0 + emptyNum := 0 err := v.OrderRepo.FindInBatches(ctx, req, func(ctx context.Context, rows []*bo.OrderBo) error { for _, order := range rows { + if order == nil { + emptyNum += 1 + log.Errorf("订单对象为 nil, req:%+v", req) + continue + } + num += 1 if err := v.notice(ctx, order, ¬ifyNum); err != nil { errNum += 1 - log.Error(err) + log.Errorf("订单定时通知,err:%v", err) } } @@ -154,6 +167,7 @@ func (v *VoucherBiz) ExecuteNotice(ctx context.Context, req *bo.FindInBatchesUse "num": num, "notifyNum": notifyNum, "errNum": errNum, + "emptyNum": emptyNum, "elapsed": time.Now().Sub(start).String(), } log.Warnf("订单定时通知,%+v", logFields) @@ -186,14 +200,14 @@ func (v *VoucherBiz) notice(ctx context.Context, order *bo.OrderBo, notifyNum *i Type: order.Type, } - if err = v.cmbNotice(ctx, order, orderNotify, notifyNum); err != nil { + if err = v.request(ctx, order, orderNotify, notifyNum); err != nil { return err } return v.UpdateOrderStatus(ctx, order.ID, status) } -func (v *VoucherBiz) cmbNotice(ctx context.Context, order *bo.OrderBo, orderNotify *bo.OrderNotifyBo, notifyNum *int) error { +func (v *VoucherBiz) request(ctx context.Context, order *bo.OrderBo, orderNotify *bo.OrderNotifyBo, notifyNum *int) error { if !orderNotify.Event.CanNotify() { return nil // 不可通知,忽略 @@ -206,13 +220,13 @@ func (v *VoucherBiz) cmbNotice(ctx context.Context, order *bo.OrderBo, orderNoti return err } - reply, err := v.CmbMixRepo.Request(ctx, request, order.NotifyUrl) - if err != nil { - return fmt.Errorf("订单定时通知,orderNo:%s,outBizNo:%s,stockId:%s,err:%s", order.OrderNo, order.OutBizNo, order.BatchNo, err.Error()) + if request == nil { + return fmt.Errorf("request is nil,orderNo:%s,outBizNo:%s,stockId:%s,err:%s", order.OrderNo, order.OutBizNo, order.BatchNo) } - if reply.RespCode != vo.CmbResponseStatusSuccess.GetValue() { - return errors.New("订单定时通知,招行返回错误:" + reply.RespMsg) + _, err = v.CmbMixRepo.Request(ctx, request, order.NotifyUrl) + if err != nil { + return fmt.Errorf("orderNo:%s,outBizNo:%s,stockId:%s,err:%s", order.OrderNo, order.OutBizNo, order.BatchNo, err.Error()) } return nil diff --git a/internal/data/mixrepoimpl/cmb.go b/internal/data/mixrepoimpl/cmb.go index 570e024..d786431 100644 --- a/internal/data/mixrepoimpl/cmb.go +++ b/internal/data/mixrepoimpl/cmb.go @@ -24,10 +24,29 @@ import ( type CmbMixRepoImpl struct { bc *conf.Bootstrap + + // 连接池复用(优化网络开销) + options *request.Options } func NewCmbMixRepoImpl(bc *conf.Bootstrap) mixrepos.CmbMixRepo { - return &CmbMixRepoImpl{bc: bc} + + h := http.Header{ + "Content-Type": []string{"application/json"}, + } + hc := &http.Client{ + Timeout: 20 * time.Second, + Transport: &http.Transport{ + MaxIdleConns: 100, // 最大空闲连接数 + MaxIdleConnsPerHost: 20, // 每个主机的最大空闲连接数 + IdleConnTimeout: 30 * time.Second, // 空闲连接超时时间 + }, + } + + return &CmbMixRepoImpl{ + bc: bc, + options: request.NewOptions(request.WithHeaders(h), request.WithHttpClient(hc)), + } } func (c *CmbMixRepoImpl) recordBody(ctx context.Context) { @@ -262,26 +281,26 @@ func (s *CmbMixRepoImpl) Request(ctx context.Context, req *v1.CmbRequest, uri st uv.Set(kv.Key, fmt.Sprintf("%v", kv.Value)) } - h := http.Header{ - "Content-Type": []string{"application/x-www-form-urlencoded"}, + requestUrl := uri + "?" + uv.Encode() + + _, bodyBytes, err := request.POST(ctx, requestUrl, nil, s.options) + if err != nil { + log.Errorf("请求掌上生活错误,url:%s,err:%v", requestUrl, err) + return nil, err } - r := uri + "?" + uv.Encode() - - _, bodyBytes, err := request.Post(ctx, r, nil, request.WithHeaders(h), request.WithTimeout(time.Second*20)) - if err != nil { - log.Errorf("请求掌上生活报错,url:%s,err:%v", r, err) - return nil, err + if len(bodyBytes) == 0 { + return nil, errors.New("请求掌上生活错误,请求失败,返回数据为空") } var response *v1.CmbReply if err = json.Unmarshal(bodyBytes, &response); err != nil { - log.Errorf("请求掌上生活返回数据解析报错:%s,url:%s,bodyBytes:%s", err.Error(), r, string(bodyBytes)) + log.Errorf("请求掌上生活错误,数据解析报错:%s,url:%s,bodyBytes:%s", err.Error(), requestUrl, string(bodyBytes)) return nil, err } if response.RespCode != vo.CmbResponseStatusSuccess.GetValue() { - log.Errorf("请求掌上生活返回报错:msg:%s,url:%s,bodyBytes:%s", response.RespMsg, r, string(bodyBytes)) + log.Errorf("请求掌上生活错误:msg:%s,url:%s,bodyBytes:%s", response.RespMsg, requestUrl, string(bodyBytes)) return nil, fmt.Errorf(response.RespMsg) } diff --git a/internal/pkg/request/request.go b/internal/pkg/request/request.go index 28da386..f1b1d59 100644 --- a/internal/pkg/request/request.go +++ b/internal/pkg/request/request.go @@ -12,6 +12,8 @@ import ( type Options struct { Headers http.Header + HttpClient *http.Client + StatusCodeFunc func(int) bool Timeout time.Duration @@ -51,12 +53,22 @@ func WithHeaders(headers http.Header) Option { } } +func WithHttpClient(httpClient *http.Client) Option { + return func(options *Options) { + options.HttpClient = httpClient + } +} + func WithStatusCodeFunc(statusCodeFunc func(int) bool) Option { return func(options *Options) { options.StatusCodeFunc = statusCodeFunc } } +func POST(ctx context.Context, url string, body []byte, options *Options) (http.Header, []byte, error) { + return Request(ctx, http.MethodPost, url, body, options) +} + func Post(ctx context.Context, url string, body []byte, options ...Option) (http.Header, []byte, error) { return Request(ctx, http.MethodPost, url, body, NewOptions(options...)) } From 881600758d975beee2f7bf95b81a6537067c8362 Mon Sep 17 00:00:00 2001 From: ziming Date: Mon, 16 Jun 2025 10:01:37 +0800 Subject: [PATCH 11/13] timeSliceQuery 8 --- internal/data/mixrepoimpl/cmb.go | 57 ++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/internal/data/mixrepoimpl/cmb.go b/internal/data/mixrepoimpl/cmb.go index d786431..00ee914 100644 --- a/internal/data/mixrepoimpl/cmb.go +++ b/internal/data/mixrepoimpl/cmb.go @@ -281,32 +281,69 @@ func (s *CmbMixRepoImpl) Request(ctx context.Context, req *v1.CmbRequest, uri st uv.Set(kv.Key, fmt.Sprintf("%v", kv.Value)) } - requestUrl := uri + "?" + uv.Encode() - - _, bodyBytes, err := request.POST(ctx, requestUrl, nil, s.options) - if err != nil { - log.Errorf("请求掌上生活错误,url:%s,err:%v", requestUrl, err) - return nil, err + h := http.Header{ + "Content-Type": []string{"application/x-www-form-urlencoded"}, } - if len(bodyBytes) == 0 { - return nil, errors.New("请求掌上生活错误,请求失败,返回数据为空") + r := uri + "?" + uv.Encode() + + _, bodyBytes, err := request.Post(ctx, r, nil, request.WithHeaders(h), request.WithTimeout(time.Second*20)) + if err != nil { + log.Errorf("请求掌上生活报错,url:%s,err:%v", r, err) + return nil, err } var response *v1.CmbReply if err = json.Unmarshal(bodyBytes, &response); err != nil { - log.Errorf("请求掌上生活错误,数据解析报错:%s,url:%s,bodyBytes:%s", err.Error(), requestUrl, string(bodyBytes)) + log.Errorf("请求掌上生活返回数据解析报错:%s,url:%s,bodyBytes:%s", err.Error(), r, string(bodyBytes)) return nil, err } if response.RespCode != vo.CmbResponseStatusSuccess.GetValue() { - log.Errorf("请求掌上生活错误:msg:%s,url:%s,bodyBytes:%s", response.RespMsg, requestUrl, string(bodyBytes)) + log.Errorf("请求掌上生活返回报错:msg:%s,url:%s,bodyBytes:%s", response.RespMsg, r, string(bodyBytes)) return nil, fmt.Errorf(response.RespMsg) } return response, nil } +// +//func (s *CmbMixRepoImpl) Request(ctx context.Context, req *v1.CmbRequest, uri string) (*v1.CmbReply, error) { +// +// kvRows := helper.SortStructFieldsByKey(req) +// +// uv := url.Values{} +// +// for _, kv := range kvRows { +// uv.Set(kv.Key, fmt.Sprintf("%v", kv.Value)) +// } +// +// requestUrl := uri + "?" + uv.Encode() +// +// _, bodyBytes, err := request.POST(ctx, requestUrl, nil, s.options) +// if err != nil { +// log.Errorf("请求掌上生活错误,url:%s,err:%v", requestUrl, err) +// return nil, err +// } +// +// if len(bodyBytes) == 0 { +// return nil, errors.New("请求掌上生活错误,请求失败,返回数据为空") +// } +// +// var response *v1.CmbReply +// if err = json.Unmarshal(bodyBytes, &response); err != nil { +// log.Errorf("请求掌上生活错误,数据解析报错:%s,url:%s,bodyBytes:%s", err.Error(), requestUrl, string(bodyBytes)) +// return nil, err +// } +// +// if response.RespCode != vo.CmbResponseStatusSuccess.GetValue() { +// log.Errorf("请求掌上生活返回报错:msg:%s,url:%s,bodyBytes:%s", response.RespMsg, requestUrl, string(bodyBytes)) +// return nil, fmt.Errorf(response.RespMsg) +// } +// +// return response, nil +//} + func (s *CmbMixRepoImpl) Decrypt(_ context.Context, encryptBody string) (string, error) { if len(s.bc.Cmb.CmbSm2Pik) == 0 { From be358c9984be584db6fca20d0293d67b094e9fdd Mon Sep 17 00:00:00 2001 From: ziming Date: Mon, 16 Jun 2025 10:05:23 +0800 Subject: [PATCH 12/13] timeSliceQuery 8 --- internal/data/mixrepoimpl/cmb.go | 37 -------------------------------- 1 file changed, 37 deletions(-) diff --git a/internal/data/mixrepoimpl/cmb.go b/internal/data/mixrepoimpl/cmb.go index 00ee914..a35b4f2 100644 --- a/internal/data/mixrepoimpl/cmb.go +++ b/internal/data/mixrepoimpl/cmb.go @@ -307,43 +307,6 @@ func (s *CmbMixRepoImpl) Request(ctx context.Context, req *v1.CmbRequest, uri st return response, nil } -// -//func (s *CmbMixRepoImpl) Request(ctx context.Context, req *v1.CmbRequest, uri string) (*v1.CmbReply, error) { -// -// kvRows := helper.SortStructFieldsByKey(req) -// -// uv := url.Values{} -// -// for _, kv := range kvRows { -// uv.Set(kv.Key, fmt.Sprintf("%v", kv.Value)) -// } -// -// requestUrl := uri + "?" + uv.Encode() -// -// _, bodyBytes, err := request.POST(ctx, requestUrl, nil, s.options) -// if err != nil { -// log.Errorf("请求掌上生活错误,url:%s,err:%v", requestUrl, err) -// return nil, err -// } -// -// if len(bodyBytes) == 0 { -// return nil, errors.New("请求掌上生活错误,请求失败,返回数据为空") -// } -// -// var response *v1.CmbReply -// if err = json.Unmarshal(bodyBytes, &response); err != nil { -// log.Errorf("请求掌上生活错误,数据解析报错:%s,url:%s,bodyBytes:%s", err.Error(), requestUrl, string(bodyBytes)) -// return nil, err -// } -// -// if response.RespCode != vo.CmbResponseStatusSuccess.GetValue() { -// log.Errorf("请求掌上生活返回报错:msg:%s,url:%s,bodyBytes:%s", response.RespMsg, requestUrl, string(bodyBytes)) -// return nil, fmt.Errorf(response.RespMsg) -// } -// -// return response, nil -//} - func (s *CmbMixRepoImpl) Decrypt(_ context.Context, encryptBody string) (string, error) { if len(s.bc.Cmb.CmbSm2Pik) == 0 { From 89e0d8d59803e5da3d2080812ac1bf2761243920 Mon Sep 17 00:00:00 2001 From: ziming Date: Mon, 16 Jun 2025 10:43:03 +0800 Subject: [PATCH 13/13] timeSliceQuery 8 --- internal/biz/repo/order.go | 2 +- internal/biz/wechat_notify.go | 2 +- internal/data/repoimpl/order.go | 9 +++------ internal/server/wechat_consumer.go | 3 ++- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/internal/biz/repo/order.go b/internal/biz/repo/order.go index ace3a2f..99306dd 100644 --- a/internal/biz/repo/order.go +++ b/internal/biz/repo/order.go @@ -14,7 +14,7 @@ type OrderRepo interface { FindInBatches(ctx context.Context, w *bo.FindInBatchesUseBo, fun func(ctx context.Context, rows []*bo.OrderBo) error) error GetByOutBizNo(ctx context.Context, t vo.OrderType, outBizNo string) (*bo.OrderBo, error) GetByOrderNo(ctx context.Context, orderNo string) (*bo.OrderBo, error) - GetByVoucherNo(ctx context.Context, merchantNo, batchNo, voucherNo string) (*bo.OrderBo, error) + GetByCouponId(ctx context.Context, merchantNo, batchNo, voucherNo string) (*bo.OrderBo, error) GetByTransactionId(ctx context.Context, stockCreatorMchId, stockID, transactionId string) (*bo.OrderBo, error) Create(ctx context.Context, req *bo.OrderBo) (*bo.OrderBo, error) GetByID(ctx context.Context, id uint64) (*bo.OrderBo, error) diff --git a/internal/biz/wechat_notify.go b/internal/biz/wechat_notify.go index 25509df..e71eb38 100644 --- a/internal/biz/wechat_notify.go +++ b/internal/biz/wechat_notify.go @@ -42,7 +42,7 @@ func (v *VoucherBiz) WechatNotifyConsumer(ctx context.Context, tag string, req * func (this *VoucherBiz) getOrder(ctx context.Context, req *bo.WechatVoucherNotifyBo) (*bo.OrderBo, error) { - order, err := this.OrderRepo.GetByVoucherNo(ctx, req.PlainText.StockCreatorMchid, req.PlainText.StockID, req.PlainText.CouponID) + order, err := this.OrderRepo.GetByCouponId(ctx, req.PlainText.StockCreatorMchid, req.PlainText.StockID, req.PlainText.CouponID) if err != nil { diff --git a/internal/data/repoimpl/order.go b/internal/data/repoimpl/order.go index 916fc5a..ea74f08 100644 --- a/internal/data/repoimpl/order.go +++ b/internal/data/repoimpl/order.go @@ -212,20 +212,17 @@ func (p *OrderRepoImpl) GetByOrderNo(ctx context.Context, orderNo string) (*bo.O return p.ToBo(info), nil } -func (p *OrderRepoImpl) GetByVoucherNo(ctx context.Context, merchantNo, batchNo, voucherNo string) (*bo.OrderBo, error) { +func (p *OrderRepoImpl) GetByCouponId(ctx context.Context, merchantNo, batchNo, voucherNo string) (*bo.OrderBo, error) { info := &model.Order{} tx := p.DB(ctx).Where(model.Order{MerchantNo: merchantNo, BatchNo: batchNo, VoucherNo: voucherNo}).First(&info) if tx.Error != nil { - if errors.Is(tx.Error, gorm.ErrRecordNotFound) { - return nil, err2.ErrorDbNotFound("订单数据不存在") - } - return nil, fmt.Errorf("db fail %w", tx.Error) + return nil, tx.Error } if tx.RowsAffected == 0 { - return nil, err2.ErrorDbNotFound("订单数据不存在") + return nil, gorm.ErrRecordNotFound } return p.ToBo(info), nil diff --git a/internal/server/wechat_consumer.go b/internal/server/wechat_consumer.go index 3017144..5a0ba87 100644 --- a/internal/server/wechat_consumer.go +++ b/internal/server/wechat_consumer.go @@ -152,7 +152,8 @@ func (w *WechatNotifyConsumer) processMessage(msg mq_http_sdk.ConsumeMessageEntr ctx := context.Background() if err := w.voucherService.WechatUseNotifyConsumer(ctx, msg.MessageTag, msg.MessageBody); err != nil { - log.Errorf("微信回调消费接收消息成功,处理失败 messageId:%s, messageTag:%s, message:%s, err:%+v", msg.MessageId, msg.MessageTag, msg.MessageBody, err) + //log.Errorf("微信回调消费接收消息成功,处理失败 messageId:%s, messageTag:%s, message:%s, err:%+v", msg.MessageId, msg.MessageTag, msg.MessageBody, err) + log.Errorf("微信回调消费接收消息成功,处理失败 messageId:%s, err:%+v", msg.MessageId, err) } }