timeSliceQueryPush

This commit is contained in:
ziming 2025-06-12 10:57:20 +08:00
parent 540d873eac
commit 48d03c16ce
5 changed files with 136 additions and 3 deletions

View File

@ -42,7 +42,6 @@ type OrderCreateReqBo struct {
}
type FindInBatchesUseBo struct {
Type vo.OrderType
StartTime *time.Time
EndTime *time.Time
}

View File

@ -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 {

View File

@ -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))
})

View File

@ -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("所有任务完成")
}
}

View File

@ -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