fix(camel_oil): 优化时间格式处理及错误码使用
- 移除无用日志打印,减少冗余信息 - 统一使用标准时间格式 "2006-01-02T15:04" 替代不规范格式 - 调整时间跨度校验,禁止结束时间小于开始时间 - 使用 gcode 标准错误码替代普通错误信息 - 规范Redis键的生成逻辑,确保一致性 - 更新相关单元测试,匹配时间格式调整
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"time"
|
||||
|
||||
v1 "kami/api/camel_oil/v1"
|
||||
@@ -19,12 +20,7 @@ func (s *sCamelOil) GetPrefetchOrderLogs(ctx context.Context, req *v1.GetPrefetc
|
||||
// 计算时间范围跨度
|
||||
duration := req.EndTime.Time.Sub(req.StartTime.Time)
|
||||
if duration <= 0 {
|
||||
return nil, gerror.New("结束时间必须大于开始时间")
|
||||
}
|
||||
|
||||
// 限制查询时间范围不超过24小时
|
||||
if duration.Hours() > 24 {
|
||||
return nil, gerror.New("查询时间范围不能超过24小时")
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "结束时间必须大于开始时间")
|
||||
}
|
||||
|
||||
// Redis key 前缀
|
||||
@@ -35,8 +31,7 @@ func (s *sCamelOil) GetPrefetchOrderLogs(ctx context.Context, req *v1.GetPrefetc
|
||||
// 遍历时间范围内的每一分钟
|
||||
currentTime := req.StartTime
|
||||
for currentTime.Before(req.EndTime) || currentTime.Equal(req.EndTime) {
|
||||
timeKey := currentTime.Format("c")
|
||||
redisKey := redisKeyPrefix + timeKey
|
||||
redisKey := redisKeyPrefix + currentTime.Layout("2006-01-02T15:04")
|
||||
|
||||
// 从Redis获取日志数据
|
||||
logData, err := cache.NewCache().Get(ctx, redisKey)
|
||||
@@ -80,7 +75,7 @@ func (s *sCamelOil) GetPrefetchOrderLogs(ctx context.Context, req *v1.GetPrefetc
|
||||
})
|
||||
}
|
||||
|
||||
currentTime = currentTime.Add(time.Minute)
|
||||
currentTime = currentTime.Add(gtime.M)
|
||||
}
|
||||
|
||||
glog.Infof(ctx, "获取到预拉取订单日志 %d 条", len(logs))
|
||||
@@ -109,9 +104,7 @@ func (s *sCamelOil) SavePrefetchOrderLog(ctx context.Context, respStr string) {
|
||||
}
|
||||
|
||||
// 生成Redis key (按分钟级别)
|
||||
now := gtime.Now()
|
||||
timeKey := now.Format("c")
|
||||
redisKey := fmt.Sprintf("camel_oil:prefetch:logs:%s", timeKey)
|
||||
redisKey := fmt.Sprintf("camel_oil:prefetch:logs:%s", gtime.Now().Layout("2006-01-02T15:04"))
|
||||
|
||||
// 获取当前分钟已有的日志
|
||||
var logs []map[string]interface{}
|
||||
|
||||
@@ -30,7 +30,7 @@ func Test_sCamelOil_PrefetchOrderConcurrently(t *testing.T) {
|
||||
|
||||
func Test_sCamelOil_MatchPrefetchOrder(t *testing.T) {
|
||||
now := gtime.Now()
|
||||
timeKey := now.Format("c")
|
||||
timeKey := now.Layout("2006-01-02T15:04")
|
||||
t.Log(timeKey)
|
||||
|
||||
//result, err := (&sCamelOil{}).MatchPrefetchOrder(t.Context(), utils.GenerateRandomUUID(), 100)
|
||||
|
||||
@@ -249,7 +249,6 @@ func (c *Client) QueryCamelOilCardAvailableDenominations(ctx context.Context, to
|
||||
Goods []Good `json:"goods"`
|
||||
}{}
|
||||
respStr := resp.ReadAllString()
|
||||
glog.Info(ctx, "查询面额", respStr)
|
||||
if err = json.Unmarshal([]byte(respStr), &queryRespStruct); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user