req 副本

This commit is contained in:
ziming 2025-06-17 09:21:59 +08:00
parent 89e0d8d598
commit 1dc83a8c23
1 changed files with 19 additions and 28 deletions

View File

@ -7,7 +7,6 @@ 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"
@ -100,13 +99,13 @@ func (v *VoucherBiz) timeSliceQuery(ctx context.Context, startTime, endTime time
end = end.Add(-1 * time.Second)
}
eg.Go(func() error {
req := &bo.FindInBatchesUseBo{
StartTime: &start,
EndTime: &end,
}
eg.Go(func() error {
select {
case <-ctx.Done():
return ctx.Err()
@ -116,12 +115,7 @@ func (v *VoucherBiz) timeSliceQuery(ctx context.Context, startTime, endTime time
defer func() {
if err := recover(); err != nil {
// 获取当前堆栈信息
buf := make([]byte, 1024)
stackSize := runtime.Stack(buf, false)
log.Errorf("订单定时通知,发生错误:req:%+v,err:%v\n堆栈信息:\n%s", req, err, string(buf[:stackSize]))
log.Errorf("订单定时通知,发生错误:req:%+v,err:%v", req, err)
}
}()
@ -138,22 +132,13 @@ 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, &notifyNum); err != nil {
errNum += 1
if err := v.orderNotice(ctx, order, &notifyNum); err != nil {
log.Errorf("订单定时通知,err:%v", err)
}
@ -166,8 +151,6 @@ 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,
"emptyNum": emptyNum,
"elapsed": time.Now().Sub(start).String(),
}
log.Warnf("订单定时通知,%+v", logFields)
@ -175,6 +158,15 @@ func (v *VoucherBiz) ExecuteNotice(ctx context.Context, req *bo.FindInBatchesUse
return err
}
func (v *VoucherBiz) orderNotice(ctx context.Context, order *bo.OrderBo, notifyNum *int) (respErr error) {
if order == nil {
return fmt.Errorf("订单对象为 nil")
}
return v.notice(ctx, order, notifyNum)
}
func (v *VoucherBiz) notice(ctx context.Context, order *bo.OrderBo, notifyNum *int) error {
// 批量通知不做数据存储,量会很大
@ -213,8 +205,6 @@ func (v *VoucherBiz) request(ctx context.Context, order *bo.OrderBo, orderNotify
return nil // 不可通知,忽略
}
*notifyNum += 1
request, err := v.Cmb.NotifyRequest(ctx, order, orderNotify)
if err != nil {
return err
@ -224,8 +214,9 @@ func (v *VoucherBiz) request(ctx context.Context, order *bo.OrderBo, orderNotify
return fmt.Errorf("request is nil,orderNo:%s,outBizNo:%s,stockId:%s,err:%s", order.OrderNo, order.OutBizNo, order.BatchNo)
}
_, err = v.CmbMixRepo.Request(ctx, request, order.NotifyUrl)
if err != nil {
*notifyNum += 1
if _, err = v.CmbMixRepo.Request(ctx, request, order.NotifyUrl); err != nil {
return fmt.Errorf("orderNo:%s,outBizNo:%s,stockId:%s,err:%s", order.OrderNo, order.OutBizNo, order.BatchNo, err.Error())
}