From 48d03c16cee20f2377094794ee05b5f7af3d953a Mon Sep 17 00:00:00 2001 From: ziming Date: Thu, 12 Jun 2025 10:57:20 +0800 Subject: [PATCH] timeSliceQueryPush --- internal/biz/bo/order_bo.go | 1 - internal/biz/cron_notice.go | 29 +++++++- internal/data/repoimpl/order.go | 1 - internal/pkg/script/script_test.go | 104 +++++++++++++++++++++++++++++ internal/service/script.go | 4 ++ 5 files changed, 136 insertions(+), 3 deletions(-) diff --git a/internal/biz/bo/order_bo.go b/internal/biz/bo/order_bo.go index 753c706..7643f14 100644 --- a/internal/biz/bo/order_bo.go +++ b/internal/biz/bo/order_bo.go @@ -42,7 +42,6 @@ type OrderCreateReqBo struct { } type FindInBatchesUseBo struct { - Type vo.OrderType StartTime *time.Time EndTime *time.Time } diff --git a/internal/biz/cron_notice.go b/internal/biz/cron_notice.go index 605cb34..1bcb91c 100644 --- a/internal/biz/cron_notice.go +++ b/internal/biz/cron_notice.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/go-kratos/kratos/v2/log" "github.com/redis/go-redis/v9" + "golang.org/x/sync/errgroup" "time" "voucher/internal/biz/bo" "voucher/internal/biz/vo" @@ -30,7 +31,6 @@ func (v *VoucherBiz) Notice(ctx context.Context) error { endTime := time.Date(noticeEndDay.Year(), noticeEndDay.Month(), noticeEndDay.Day(), 23, 59, 59, 0, noticeEndDay.Location()) req := &bo.FindInBatchesUseBo{ - Type: vo.OrderTypeCmb, StartTime: &startTime, EndTime: &endTime, } @@ -38,6 +38,33 @@ func (v *VoucherBiz) Notice(ctx context.Context) error { return v.ExecuteNotice(ctx, req) } +func (v *VoucherBiz) timeSliceQueryPush(ctx context.Context, startTime, endTime time.Time) error { + + duration := 1 * time.Hour + + eg := new(errgroup.Group) + eg.SetLimit(5) + + for start := startTime; start.Before(endTime); start = start.Add(duration) { + + end := start.Add(duration) // 计算每次请求的结束时间 + if end.After(endTime) { + end = endTime + } + + req := &bo.FindInBatchesUseBo{ + StartTime: &start, + EndTime: &end, + } + + eg.Go(func() error { + return v.ExecuteNotice(ctx, req) + }) + } + + return eg.Wait() // 仅返回第一个错误 +} + func (v *VoucherBiz) ExecuteNotice(ctx context.Context, req *bo.FindInBatchesUseBo) error { return v.OrderRepo.FindInBatches(ctx, req, func(ctx context.Context, rows []*bo.OrderBo) error { diff --git a/internal/data/repoimpl/order.go b/internal/data/repoimpl/order.go index 713dd57..eab59c3 100644 --- a/internal/data/repoimpl/order.go +++ b/internal/data/repoimpl/order.go @@ -69,7 +69,6 @@ func (p *OrderRepoImpl) FinFailByStockIdInBatches(ctx context.Context, batchNo s result := p.DB(ctx). Where("batch_no = ?", batchNo). Where("status = ?", vo.OrderStatusFail.GetValue()). - //Where("remark <> ?", "error: code = 500 reason = WechatFAIL message = 微信返回错误:该用户账号异常,无法领券。商家可联系微信支付或让用户联系微信支付客服处理。 metadat"). FindInBatches(&results, 100, func(tx *gorm.DB, batch int) error { return fun(ctx, p.ToBos(results)) }) diff --git a/internal/pkg/script/script_test.go b/internal/pkg/script/script_test.go index 8fa5d22..dfc90cb 100644 --- a/internal/pkg/script/script_test.go +++ b/internal/pkg/script/script_test.go @@ -1,8 +1,12 @@ package script import ( + "encoding/json" + "fmt" + "golang.org/x/sync/errgroup" "testing" "time" + "voucher/internal/biz/bo" ) func Test_script(t *testing.T) { @@ -51,3 +55,103 @@ func Test_script2(t *testing.T) { t.Error(err) } } + +func Test_moreTime(t *testing.T) { + + startTime, err := time.Parse(time.DateTime, "2025-05-31 00:00:00") + if err != nil { + t.Error(err) + return + } + + endTime, err := time.Parse(time.DateTime, "2025-05-31 10:00:00") + if err != nil { + t.Error(err) + return + } + + //duration := 240 * time.Hour + duration := 1 * time.Hour + + for start := startTime; start.Before(endTime); start = start.Add(duration) { + + end := start.Add(duration) // 计算每次请求的结束时间 + if end.After(endTime) { + end = endTime + } + + // 创建请求体 + requestBody := map[string]any{ + "start_time": start.Format(time.DateTime), + "end_time": end.Format(time.DateTime), + "go_num": 2, // 并发数量 + "time_slice_hours": 1, // 时间间隔 + "product_no": "", + } + + // 将请求体转换为 JSON 格式 + bodyBytes, err2 := json.Marshal(requestBody) + if err2 != nil { + t.Error(err) + return + } + + fmt.Printf("body:%s\n", string(bodyBytes)) + } +} + +func Test_goMoreTime(t *testing.T) { + + startTime, err := time.Parse(time.DateTime, "2025-05-01 00:00:00") + if err != nil { + t.Error(err) + return + } + + //endTime, err := time.Parse(time.DateTime, "2025-05-31 23:59:59") + endTime, err := time.Parse(time.DateTime, "2025-05-01 23:59:59") + if err != nil { + t.Error(err) + return + } + + duration := 1 * time.Hour + + eg := new(errgroup.Group) + eg.SetLimit(5) + + for start := startTime; start.Before(endTime); start = start.Add(duration) { + + end := start.Add(duration) // 计算每次请求的结束时间 + if end.After(endTime) { + end = endTime + } + + req := &bo.FindInBatchesUseBo{ + StartTime: &start, + EndTime: &end, + } + + // 将请求体转换为 JSON 格式 + reqStr, err2 := json.Marshal(req) + if err2 != nil { + t.Error(err) + return + } + + eg.Go(func() error { + // 任务逻辑... + time.Sleep(2 * time.Second) + return fmt.Errorf("任务失败") + }) + + fmt.Printf("%s\n", string(reqStr)) + } + + err = eg.Wait() // 仅返回第一个错误 + if err != nil { + fmt.Println(err) + } else { + fmt.Println("所有任务完成") + } +} diff --git a/internal/service/script.go b/internal/service/script.go index c1cb2f0..a4cf1c4 100644 --- a/internal/service/script.go +++ b/internal/service/script.go @@ -90,6 +90,10 @@ func (this *CmbService) TimeSliceQueryPush(ctx http.Context) error { return fmt.Errorf("start_time or end_time is empty") } + if req.GoNum > 10 { + return fmt.Errorf("协程数量不能大于10") + } + _, err = this.timeSliceQuery.Push(ctx, req) if err != nil { return err