Files
junhong_cmp_fiber/internal/task/polling_utils.go
break 88cc5e96ec
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m32s
暂存
2026-08-06 09:35:00 +08:00

78 lines
2.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package task
import (
"context"
"strconv"
"time"
"github.com/bytedance/sonic"
"go.uber.org/zap"
"gorm.io/gorm"
"github.com/break/junhong_cmp_fiber/pkg/auditcontext"
"github.com/break/junhong_cmp_fiber/pkg/constants"
)
// withPollingWorkerAuditContext 为实际改变业务事实的轮询任务补充真实系统操作者与链路。
func withPollingWorkerAuditContext(ctx context.Context, taskType, taskName, correlationID string) context.Context {
if correlationID == "" {
correlationID = taskType
}
return auditcontext.With(ctx, auditcontext.Context{
ActorKind: constants.AuditActorSystemTask, ActorID: taskType,
ActorName: taskName, Source: constants.AuditSourceWorker, CorrelationID: correlationID,
})
}
// shortTaskType 从完整任务类型中提取简短名称(如 polling:carddata → carddata
func shortTaskType(fullTaskType string) string {
for i := len(fullTaskType) - 1; i >= 0; i-- {
if fullTaskType[i] == ':' {
return fullTaskType[i+1:]
}
}
return fullTaskType
}
// parseTaskPayload 解析轮询任务载荷,返回 cardID
func parseTaskPayload(payload []byte, logger *zap.Logger) (uint, bool) {
var p struct {
CardID string `json:"card_id"`
}
if err := sonic.Unmarshal(payload, &p); err != nil {
logger.Error("解析任务载荷失败", zap.Error(err))
return 0, false
}
id, parseErr := strconv.ParseUint(p.CardID, 10, 64)
if parseErr != nil {
logger.Error("解析卡ID失败", zap.String("card_id", p.CardID), zap.Error(parseErr))
return 0, false
}
return uint(id), true
}
// boolToStr 布尔值转字符串Redis hash 存储用)
func boolToStr(b bool) string {
if b {
return "1"
}
return "0"
}
// isNotFound 判断是否为 GORM 记录不存在错误
func isNotFound(err error) bool {
return err == gorm.ErrRecordNotFound
}
// isResetWindow 判断今天是否在运营商流量重置日窗口内
// 窗口 = 重置日当天 + 前一天(容错网关数据延迟上报)
func isResetWindow(now time.Time, resetDay int) bool {
today := now.Day()
if today == resetDay {
return true
}
resetDate := time.Date(now.Year(), now.Month(), resetDay, 0, 0, 0, 0, now.Location())
prevDay := resetDate.AddDate(0, 0, -1).Day()
return today == prevDay
}