feat: 分层轮询、重置日前加密与激活前流量结算
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m37s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m37s
- 轮询配置新增套餐状态、资产状态、重置日前窗口与窗口内流量间隔(迁移 248),间隔三态:正数按间隔、0 不检查、空继承 - 配置按 priority, id 排序合并;在库与已销售无套餐未匹配时告警不入队 - 新增优先级整体重排、匹配预览、立即重算到期时间(异步只提前)、分层统计与重置日前加密统计接口 - 已切换日快照计量的卡流量按观测时间归属,激活前用量只记已结算 - 订单购买与实名激活补齐资产级激活锁,激活前对已切换卡结算一次流量,失败只告警不阻塞,结算不触发通道阈值判定
This commit is contained in:
85
internal/service/order/pre_activation.go
Normal file
85
internal/service/order/pre_activation.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// PreActivationSettler 在资产从无生效主套餐变为有生效主套餐之前做一次流量结算(尽力而为,不阻塞)。
|
||||
type PreActivationSettler interface {
|
||||
SettleBeforeActivation(ctx context.Context, carrierType string, carrierID uint)
|
||||
}
|
||||
|
||||
// activationLockWait 是订单路径等待资产级激活锁的最长时间;超时照常继续(行锁与流量按观测时间归属兜底正确性)。
|
||||
const activationLockWait = 5 * time.Second
|
||||
|
||||
// SetPreActivationSettler 注入激活前流量结算器;未注入时不做结算。
|
||||
func (s *Service) SetPreActivationSettler(settler PreActivationSettler) {
|
||||
s.preActivationSettler = settler
|
||||
}
|
||||
|
||||
// prepareActivation 在订单激活事务开始前执行:抢资产级激活锁(与排队、指定、队首、实名激活共用同一把锁),
|
||||
// 再做激活前流量结算。返回的释放函数须在激活事务提交或回滚后调用。外部网络调用不得放进数据库事务(ENG-TX-001)。
|
||||
func (s *Service) prepareActivation(ctx context.Context, order *model.Order) func() {
|
||||
carrierType, carrierID, ok := orderCarrier(order)
|
||||
if !ok {
|
||||
return func() {}
|
||||
}
|
||||
release := s.acquireActivationLock(ctx, carrierType, carrierID)
|
||||
if s.preActivationSettler != nil {
|
||||
s.preActivationSettler.SettleBeforeActivation(ctx, carrierType, carrierID)
|
||||
}
|
||||
return release
|
||||
}
|
||||
|
||||
func orderCarrier(order *model.Order) (string, uint, bool) {
|
||||
if order == nil {
|
||||
return "", 0, false
|
||||
}
|
||||
if order.OrderType == model.OrderTypeSingleCard && order.IotCardID != nil {
|
||||
return constants.AssetTypeIotCard, *order.IotCardID, true
|
||||
}
|
||||
if order.OrderType == model.OrderTypeDevice && order.DeviceID != nil {
|
||||
return constants.AssetTypeDevice, *order.DeviceID, true
|
||||
}
|
||||
return "", 0, false
|
||||
}
|
||||
|
||||
// acquireActivationLock 在最长 activationLockWait 内抢锁;抢不到时记日志并返回空释放函数。
|
||||
func (s *Service) acquireActivationLock(ctx context.Context, carrierType string, carrierID uint) func() {
|
||||
if s.redis == nil {
|
||||
return func() {}
|
||||
}
|
||||
key := constants.RedisPackageActivationLockKey(carrierType, carrierID)
|
||||
token := uuid.NewString()
|
||||
deadline := time.Now().Add(activationLockWait)
|
||||
for {
|
||||
locked, err := s.redis.SetNX(ctx, key, token, constants.PackageActivationLockSeconds*time.Second).Result()
|
||||
if err == nil && locked {
|
||||
return func() {
|
||||
releaseCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 2*time.Second)
|
||||
defer cancel()
|
||||
// 只释放自己持有的锁,避免锁过期后误删他人的锁。
|
||||
if current, getErr := s.redis.Get(releaseCtx, key).Result(); getErr == nil && current == token {
|
||||
s.redis.Del(releaseCtx, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil || time.Now().After(deadline) {
|
||||
s.logger.Warn("订单激活未能获取资产级激活锁,继续执行(行锁与流量按观测时间归属兜底)",
|
||||
zap.String("carrier_type", carrierType), zap.Uint("carrier_id", carrierID), zap.Error(err))
|
||||
return func() {}
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return func() {}
|
||||
case <-time.After(200 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@ type WechatConfigServiceInterface interface {
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
preActivationSettler PreActivationSettler
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
orderStore *postgres.OrderStore
|
||||
@@ -1176,6 +1177,8 @@ func (s *Service) createOrderWithWalletPayment(ctx context.Context, order *model
|
||||
var existingOrderID uint
|
||||
walletDebitAttempted := false
|
||||
|
||||
release := s.prepareActivation(ctx, order)
|
||||
defer release()
|
||||
err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
matchedOrderID, err := lockAndFindRecentWalletOrder(ctx, tx, order.IdempotencyKey)
|
||||
if err != nil {
|
||||
@@ -1239,6 +1242,8 @@ func (s *Service) createOrderWithActivation(ctx context.Context, order *model.Or
|
||||
if s.billCreation == nil {
|
||||
return errors.New(errors.CodeInternalError, "员工代收款建账能力未配置")
|
||||
}
|
||||
release := s.prepareActivation(ctx, order)
|
||||
defer release()
|
||||
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(order).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "创建代购订单失败")
|
||||
@@ -1727,6 +1732,8 @@ func (s *Service) WalletPay(ctx context.Context, orderID uint, buyerType string,
|
||||
}
|
||||
return err
|
||||
}
|
||||
release := s.prepareActivation(ctx, order)
|
||||
defer release()
|
||||
agentWalletDebitAttempted := false
|
||||
defer func() {
|
||||
s.recordOrderFailure(ctx, constants.AuditActionOrderWalletPaid, "钱包支付订单失败", order, err)
|
||||
@@ -1974,6 +1981,8 @@ func (s *Service) HandlePaymentCallback(ctx context.Context, orderNo string, pay
|
||||
return err
|
||||
}
|
||||
|
||||
release := s.prepareActivation(ctx, order)
|
||||
defer release()
|
||||
now := time.Now()
|
||||
beforeOrder := *order
|
||||
shouldResumeAfterPayment := false
|
||||
@@ -2059,6 +2068,8 @@ func (s *Service) HandlePaymentRecordCallback(ctx context.Context, paymentNo str
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "查询订单失败")
|
||||
}
|
||||
|
||||
release := s.prepareActivation(ctx, order)
|
||||
defer release()
|
||||
now := time.Now()
|
||||
beforePayment := *payment
|
||||
beforeOrder := *order
|
||||
|
||||
@@ -35,6 +35,55 @@ type ActivationService struct {
|
||||
observationSeriesEvents cardObservationApp.SeriesEventWriter
|
||||
priorityEvents priorityapp.PriorityEventWriter
|
||||
auditWriter *audit.Writer
|
||||
preActivationSettler PreActivationSettler // 激活前流量结算,可选
|
||||
}
|
||||
|
||||
// PreActivationSettler 在资产从无生效主套餐变为有生效主套餐之前做一次流量结算(尽力而为,不阻塞激活)。
|
||||
type PreActivationSettler interface {
|
||||
SettleBeforeActivation(ctx context.Context, carrierType string, carrierID uint)
|
||||
}
|
||||
|
||||
// SetPreActivationSettler 注入激活前流量结算器;未注入时不做结算。
|
||||
func (s *ActivationService) SetPreActivationSettler(settler PreActivationSettler) {
|
||||
s.preActivationSettler = settler
|
||||
}
|
||||
|
||||
// settleBeforeActivation 在激活锁内、激活事务开始前执行激活前流量结算;外部调用不得放进数据库事务(ENG-TX-001)。
|
||||
func (s *ActivationService) settleBeforeActivation(ctx context.Context, carrierType string, carrierID uint) {
|
||||
if s.preActivationSettler != nil {
|
||||
s.preActivationSettler.SettleBeforeActivation(ctx, carrierType, carrierID)
|
||||
}
|
||||
}
|
||||
|
||||
// waitActivationLock 实名激活抢资产级激活锁:最多等待 5 秒,抢不到时照常继续(不能跳过,否则实名回调触发的激活会丢失)。
|
||||
func (s *ActivationService) waitActivationLock(ctx context.Context, carrierType string, carrierID uint) func() {
|
||||
if s.redis == nil {
|
||||
return func() {}
|
||||
}
|
||||
key := constants.RedisPackageActivationLockKey(carrierType, carrierID)
|
||||
token := time.Now().String()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
locked, err := s.redis.SetNX(ctx, key, token, constants.PackageActivationLockSeconds*time.Second).Result()
|
||||
if err == nil && locked {
|
||||
return func() {
|
||||
releaseCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 2*time.Second)
|
||||
defer cancel()
|
||||
if current, getErr := s.redis.Get(releaseCtx, key).Result(); getErr == nil && current == token {
|
||||
s.redis.Del(releaseCtx, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil || time.Now().After(deadline) {
|
||||
s.logger.Warn("实名激活未能获取资产级激活锁,继续执行", zap.String("carrier_type", carrierType), zap.Uint("carrier_id", carrierID), zap.Error(err))
|
||||
return func() {}
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return func() {}
|
||||
case <-time.After(200 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetLifecycleAudit 注入套餐权益生命周期统一审计 Writer。
|
||||
@@ -102,6 +151,10 @@ func (s *ActivationService) ActivateByRealname(ctx context.Context, carrierType
|
||||
return nil
|
||||
}
|
||||
|
||||
release := s.waitActivationLock(ctx, carrierType, carrierID)
|
||||
defer release()
|
||||
s.settleBeforeActivation(ctx, carrierType, carrierID)
|
||||
|
||||
now := time.Now()
|
||||
|
||||
activated := false
|
||||
@@ -219,7 +272,7 @@ func (s *ActivationService) ActivateQueuedPackage(ctx context.Context, carrierTy
|
||||
// 使用 Redis 分布式锁避免并发
|
||||
lockKey := constants.RedisPackageActivationLockKey(carrierType, carrierID)
|
||||
lockValue := time.Now().String()
|
||||
locked, err := s.redis.SetNX(ctx, lockKey, lockValue, 30*time.Second).Result()
|
||||
locked, err := s.redis.SetNX(ctx, lockKey, lockValue, constants.PackageActivationLockSeconds*time.Second).Result()
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeRedisError, err, "获取分布式锁失败")
|
||||
}
|
||||
@@ -233,6 +286,8 @@ func (s *ActivationService) ActivateQueuedPackage(ctx context.Context, carrierTy
|
||||
|
||||
activated := false
|
||||
var failedUsage *model.PackageUsage
|
||||
s.settleBeforeActivation(ctx, carrierType, carrierID)
|
||||
|
||||
err = s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// 任务 9.5: 检测并标记过期的主套餐
|
||||
now := time.Now()
|
||||
@@ -336,7 +391,7 @@ func (s *ActivationService) ActivateSpecificPackage(ctx context.Context, package
|
||||
// 获取分布式锁
|
||||
lockKey := constants.RedisPackageActivationLockKey(carrierType, carrierID)
|
||||
lockValue := time.Now().String()
|
||||
locked, err := s.redis.SetNX(ctx, lockKey, lockValue, 30*time.Second).Result()
|
||||
locked, err := s.redis.SetNX(ctx, lockKey, lockValue, constants.PackageActivationLockSeconds*time.Second).Result()
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeRedisError, err, "获取分布式锁失败")
|
||||
}
|
||||
@@ -356,6 +411,8 @@ func (s *ActivationService) ActivateSpecificPackage(ctx context.Context, package
|
||||
|
||||
// 事务内激活
|
||||
activated := false
|
||||
s.settleBeforeActivation(ctx, carrierType, carrierID)
|
||||
|
||||
err = s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// 再次幂等检查(防止锁释放期间被其他操作激活)
|
||||
var currentUsage model.PackageUsage
|
||||
@@ -419,7 +476,7 @@ func (s *ActivationService) ActivateNextPendingMainPackage(ctx context.Context,
|
||||
|
||||
lockKey := constants.RedisPackageActivationLockKey(carrierType, carrierID)
|
||||
lockValue := time.Now().String()
|
||||
locked, err := s.redis.SetNX(ctx, lockKey, lockValue, 30*time.Second).Result()
|
||||
locked, err := s.redis.SetNX(ctx, lockKey, lockValue, constants.PackageActivationLockSeconds*time.Second).Result()
|
||||
if err != nil {
|
||||
return false, errors.Wrap(errors.CodeRedisError, err, "获取分布式锁失败")
|
||||
}
|
||||
@@ -433,6 +490,8 @@ func (s *ActivationService) ActivateNextPendingMainPackage(ctx context.Context,
|
||||
|
||||
activated := false
|
||||
var failedUsage *model.PackageUsage
|
||||
s.settleBeforeActivation(ctx, carrierType, carrierID)
|
||||
|
||||
err = s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
hasActive, err := s.hasActiveMainPackage(ctx, tx, carrierType, carrierID)
|
||||
if err != nil {
|
||||
|
||||
@@ -73,6 +73,8 @@ func pollingConfigState(config *model.PollingConfig) map[string]any {
|
||||
"realname_check_interval": config.RealnameCheckInterval, "carddata_check_interval": config.CarddataCheckInterval,
|
||||
"package_check_interval": config.PackageCheckInterval, "protect_check_interval": config.ProtectCheckInterval,
|
||||
"card_status_check_interval": config.CardStatusCheckInterval, "status": config.Status,
|
||||
"package_state": config.PackageState, "asset_status": config.AssetStatus,
|
||||
"pre_reset_window": config.PreResetWindow, "pre_reset_carddata_interval": config.PreResetCarddataInterval,
|
||||
"description": config.Description,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
auditinfra "github.com/break/junhong_cmp_fiber/internal/infrastructure/audit"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
pollingcore "github.com/break/junhong_cmp_fiber/internal/polling"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
@@ -25,6 +26,8 @@ type ConfigService struct {
|
||||
auditWriter *auditinfra.Writer
|
||||
redis *redis.Client
|
||||
logger *zap.Logger
|
||||
configMgr *pollingcore.PollingConfigManager
|
||||
queueClient TaskEnqueuer
|
||||
}
|
||||
|
||||
// SetAudit 注入轮询配置事务与统一审计 Writer。
|
||||
@@ -67,28 +70,28 @@ func (s *ConfigService) Create(ctx context.Context, req *dto.CreatePollingConfig
|
||||
return nil, appErr
|
||||
}
|
||||
|
||||
// 验证检查间隔(至少一个不为空)
|
||||
if req.RealnameCheckInterval == nil && req.CarddataCheckInterval == nil &&
|
||||
req.PackageCheckInterval == nil && req.ProtectCheckInterval == nil &&
|
||||
req.CardStatusCheckInterval == nil {
|
||||
return nil, errors.New(errors.CodeInvalidParam, "至少需要配置一种检查间隔")
|
||||
}
|
||||
|
||||
config := &model.PollingConfig{
|
||||
ConfigName: req.ConfigName,
|
||||
CardCondition: req.CardCondition,
|
||||
CardCategory: req.CardCategory,
|
||||
CarrierID: req.CarrierID,
|
||||
Priority: req.Priority,
|
||||
RealnameCheckInterval: req.RealnameCheckInterval,
|
||||
CarddataCheckInterval: req.CarddataCheckInterval,
|
||||
PackageCheckInterval: req.PackageCheckInterval,
|
||||
ProtectCheckInterval: req.ProtectCheckInterval,
|
||||
CardStatusCheckInterval: req.CardStatusCheckInterval,
|
||||
Status: 1,
|
||||
Description: req.Description,
|
||||
CreatedBy: ¤tUserID,
|
||||
UpdatedBy: ¤tUserID,
|
||||
ConfigName: req.ConfigName,
|
||||
CardCondition: req.CardCondition,
|
||||
CardCategory: req.CardCategory,
|
||||
CarrierID: req.CarrierID,
|
||||
Priority: req.Priority,
|
||||
RealnameCheckInterval: req.RealnameCheckInterval,
|
||||
CarddataCheckInterval: req.CarddataCheckInterval,
|
||||
PackageCheckInterval: req.PackageCheckInterval,
|
||||
ProtectCheckInterval: req.ProtectCheckInterval,
|
||||
CardStatusCheckInterval: req.CardStatusCheckInterval,
|
||||
PackageState: req.PackageState,
|
||||
AssetStatus: req.AssetStatus,
|
||||
PreResetWindow: req.PreResetWindow,
|
||||
PreResetCarddataInterval: req.PreResetCarddataInterval,
|
||||
Status: 1,
|
||||
Description: req.Description,
|
||||
CreatedBy: ¤tUserID,
|
||||
UpdatedBy: ¤tUserID,
|
||||
}
|
||||
if err := validatePollingConfig(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err := runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
|
||||
@@ -114,7 +117,9 @@ func (s *ConfigService) Create(ctx context.Context, req *dto.CreatePollingConfig
|
||||
}
|
||||
|
||||
s.notifyConfigChanged(ctx, "created")
|
||||
return s.toResponse(config), nil
|
||||
resp := s.toResponse(config)
|
||||
resp.Warnings = s.priorityWarnings(ctx, config)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Get 获取轮询配置详情
|
||||
@@ -189,9 +194,27 @@ func (s *ConfigService) Update(ctx context.Context, id uint, req *dto.UpdatePoll
|
||||
if req.CardStatusCheckInterval != nil {
|
||||
config.CardStatusCheckInterval = req.CardStatusCheckInterval
|
||||
}
|
||||
if req.PackageState != nil {
|
||||
config.PackageState = req.PackageState
|
||||
}
|
||||
if req.AssetStatus != nil {
|
||||
config.AssetStatus = req.AssetStatus
|
||||
}
|
||||
if req.PreResetWindow != nil {
|
||||
config.PreResetWindow = req.PreResetWindow
|
||||
}
|
||||
if req.PreResetCarddataInterval != nil {
|
||||
config.PreResetCarddataInterval = req.PreResetCarddataInterval
|
||||
}
|
||||
if req.Description != nil {
|
||||
config.Description = *req.Description
|
||||
}
|
||||
if err := applyPollingConfigClearFields(config, req.ClearFields); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validatePollingConfig(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.UpdatedBy = ¤tUserID
|
||||
|
||||
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
|
||||
@@ -219,7 +242,9 @@ func (s *ConfigService) Update(ctx context.Context, id uint, req *dto.UpdatePoll
|
||||
}
|
||||
|
||||
s.notifyConfigChanged(ctx, "updated")
|
||||
return s.toResponse(config), nil
|
||||
resp := s.toResponse(config)
|
||||
resp.Warnings = s.priorityWarnings(ctx, config)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Delete 删除轮询配置
|
||||
@@ -368,20 +393,24 @@ func (s *ConfigService) ListEnabled(ctx context.Context) ([]*dto.PollingConfigRe
|
||||
// toResponse 转换为响应 DTO
|
||||
func (s *ConfigService) toResponse(c *model.PollingConfig) *dto.PollingConfigResponse {
|
||||
return &dto.PollingConfigResponse{
|
||||
ID: c.ID,
|
||||
ConfigName: c.ConfigName,
|
||||
CardCondition: c.CardCondition,
|
||||
CardCategory: c.CardCategory,
|
||||
CarrierID: c.CarrierID,
|
||||
Priority: c.Priority,
|
||||
RealnameCheckInterval: c.RealnameCheckInterval,
|
||||
CarddataCheckInterval: c.CarddataCheckInterval,
|
||||
PackageCheckInterval: c.PackageCheckInterval,
|
||||
ProtectCheckInterval: c.ProtectCheckInterval,
|
||||
CardStatusCheckInterval: c.CardStatusCheckInterval,
|
||||
Status: c.Status,
|
||||
Description: c.Description,
|
||||
CreatedAt: c.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: c.UpdatedAt.Format(time.RFC3339),
|
||||
ID: c.ID,
|
||||
ConfigName: c.ConfigName,
|
||||
CardCondition: c.CardCondition,
|
||||
CardCategory: c.CardCategory,
|
||||
CarrierID: c.CarrierID,
|
||||
Priority: c.Priority,
|
||||
RealnameCheckInterval: c.RealnameCheckInterval,
|
||||
CarddataCheckInterval: c.CarddataCheckInterval,
|
||||
PackageCheckInterval: c.PackageCheckInterval,
|
||||
ProtectCheckInterval: c.ProtectCheckInterval,
|
||||
CardStatusCheckInterval: c.CardStatusCheckInterval,
|
||||
PackageState: c.PackageState,
|
||||
AssetStatus: c.AssetStatus,
|
||||
PreResetWindow: c.PreResetWindow,
|
||||
PreResetCarddataInterval: c.PreResetCarddataInterval,
|
||||
Status: c.Status,
|
||||
Description: c.Description,
|
||||
CreatedAt: c.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: c.UpdatedAt.Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
|
||||
419
internal/service/polling/config_tier_service.go
Normal file
419
internal/service/polling/config_tier_service.go
Normal file
@@ -0,0 +1,419 @@
|
||||
package polling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/google/uuid"
|
||||
"github.com/hibiken/asynq"
|
||||
"gorm.io/gorm"
|
||||
|
||||
auditinfra "github.com/break/junhong_cmp_fiber/internal/infrastructure/audit"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
pollingcore "github.com/break/junhong_cmp_fiber/internal/polling"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
)
|
||||
|
||||
// tierStatsCacheKey 是分层统计结果的缓存 Key(5 分钟)。
|
||||
const tierStatsCacheKey = "polling:tier_stats"
|
||||
|
||||
// TaskEnqueuer 是提交异步任务的最小端口(由 pkg/queue.Client 实现)。
|
||||
type TaskEnqueuer interface {
|
||||
EnqueueTask(ctx context.Context, taskType string, payload interface{}, opts ...asynq.Option) error
|
||||
}
|
||||
|
||||
// SetTierDeps 注入分层轮询所需的配置管理器与异步队列;未注入时预览、重算与统计不可用。
|
||||
func (s *ConfigService) SetTierDeps(configMgr *pollingcore.PollingConfigManager, queueClient TaskEnqueuer) {
|
||||
s.configMgr = configMgr
|
||||
s.queueClient = queueClient
|
||||
}
|
||||
|
||||
// UpdatePriorities 按给定顺序把配置优先级重排为 1、2、3…(事务内完成并逐条写统一审计),仅超级管理员可用。
|
||||
func (s *ConfigService) UpdatePriorities(ctx context.Context, req *dto.UpdatePollingConfigPrioritiesRequest) error {
|
||||
if middleware.GetUserTypeFromContext(ctx) != constants.UserTypeSuperAdmin {
|
||||
return errors.New(errors.CodeForbidden, "仅超级管理员可调整轮询配置优先级")
|
||||
}
|
||||
operatorID := middleware.GetUserIDFromContext(ctx)
|
||||
seen := map[uint]bool{}
|
||||
for _, id := range req.OrderedIDs {
|
||||
if id == 0 || seen[id] {
|
||||
return errors.New(errors.CodeInvalidParam, "轮询配置优先级参数不合法:配置 ID 不能为空或重复")
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
err := runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
|
||||
for index, id := range req.OrderedIDs {
|
||||
var cfg model.PollingConfig
|
||||
if err := tx.WithContext(ctx).First(&cfg, id).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return errors.New(errors.CodePollingConfigNotFound, "轮询配置不存在:"+strconv.FormatUint(uint64(id), 10))
|
||||
}
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "查询轮询配置失败")
|
||||
}
|
||||
priority := index + 1
|
||||
if cfg.Priority == priority {
|
||||
continue
|
||||
}
|
||||
before := cfg
|
||||
if err := tx.WithContext(ctx).Model(&model.PollingConfig{}).Where("id = ?", id).
|
||||
Updates(map[string]any{"priority": priority, "updated_by": operatorID, "updated_at": time.Now()}).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "调整轮询配置优先级失败")
|
||||
}
|
||||
cfg.Priority = priority
|
||||
if err := writePollingAudit(ctx, tx, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingConfigUpdated, Summary: "调整轮询配置优先级",
|
||||
ResourceType: constants.AuditResourcePollingConfig, ResourceID: cfg.ID,
|
||||
ResourceKey: pollingManualTriggerKey(cfg.ID), DisplayName: cfg.ConfigName,
|
||||
OperatorID: operatorID, IdentitySnapshot: pollingConfigIdentity(&cfg),
|
||||
BeforeData: pollingConfigState(&before), AfterData: pollingConfigState(&cfg),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.notifyConfigChanged(ctx, "priorities_updated")
|
||||
return nil
|
||||
}
|
||||
|
||||
// MatchPreview 返回卡的匹配维度、命中配置与各检查类型最终间隔及来源;可带未保存修改预演,不写任何数据。
|
||||
func (s *ConfigService) MatchPreview(ctx context.Context, req *dto.PollingConfigMatchPreviewRequest) (*dto.PollingConfigMatchPreviewResponse, error) {
|
||||
if err := requirePlatformReader(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.configMgr == nil {
|
||||
return nil, errors.New(errors.CodeServiceUnavailable, "轮询配置匹配预览能力未配置")
|
||||
}
|
||||
iccid := strings.TrimSpace(req.ICCID)
|
||||
var card model.IotCard
|
||||
if err := s.db.WithContext(ctx).Where("iccid = ?", iccid).First(&card).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, errors.New(errors.CodeNotFound, "IoT 卡不存在")
|
||||
}
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询 IoT 卡失败")
|
||||
}
|
||||
configs, err := s.configStore.ListEnabled(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询轮询配置失败")
|
||||
}
|
||||
configs, drafts, err := applyPreviewOverrides(configs, req.Overrides)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tier := s.configMgr.TierResolver(); tier != nil {
|
||||
tier.Invalidate(card.ID)
|
||||
}
|
||||
matched, sources := s.configMgr.PreviewIntervalSources(configs, &card)
|
||||
facts := s.configMgr.TierFactsOf(&card)
|
||||
resp := &dto.PollingConfigMatchPreviewResponse{
|
||||
Card: dto.PollingCardMatchFacts{
|
||||
CardID: card.ID, ICCID: card.ICCID, CardCondition: pollingcore.CardConditionOf(&card), CardCategory: card.CardCategory,
|
||||
CarrierID: card.CarrierID, AssetStatus: card.AssetStatus, EnablePolling: card.EnablePolling,
|
||||
},
|
||||
Matched: []dto.PollingConfigBrief{}, Intervals: []dto.PollingIntervalPreview{},
|
||||
}
|
||||
if facts.Known {
|
||||
resp.Card.PackageState = facts.PackageState
|
||||
resp.Card.PackageStateName = constants.PollingPackageStateName(facts.PackageState)
|
||||
}
|
||||
for _, cfg := range matched {
|
||||
resp.Matched = append(resp.Matched, dto.PollingConfigBrief{ID: cfg.ID, ConfigName: cfg.ConfigName, Priority: cfg.Priority, Draft: drafts[cfg]})
|
||||
}
|
||||
firstID := uint(0)
|
||||
if len(matched) > 0 {
|
||||
firstID = matched[0].ID
|
||||
}
|
||||
for _, taskType := range previewTaskTypes {
|
||||
item := dto.PollingIntervalPreview{TaskType: taskType, TaskTypeName: previewTaskTypeName(taskType), Mode: "unmatched"}
|
||||
if source, ok := sources[taskType]; ok {
|
||||
item.ConfigID, item.ConfigName = source.ConfigID, source.Config.ConfigName
|
||||
item.Inherited = source.ConfigID != firstID
|
||||
if source.Disabled {
|
||||
item.Mode = "disabled"
|
||||
} else {
|
||||
item.Mode, item.Interval = "interval", source.Interval
|
||||
}
|
||||
}
|
||||
resp.Intervals = append(resp.Intervals, item)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
var previewTaskTypes = []string{constants.TaskTypePollingCardStatus, constants.TaskTypePollingCarddata, constants.TaskTypePollingRealname, constants.TaskTypePollingPackage, constants.TaskTypePollingProtect}
|
||||
|
||||
// applyPreviewOverrides 把未保存修改合并进启用配置快照并重新排序;返回草稿配置标记。
|
||||
func applyPreviewOverrides(configs []*model.PollingConfig, overrides []dto.PollingConfigOverride) ([]*model.PollingConfig, map[*model.PollingConfig]bool, error) {
|
||||
drafts := map[*model.PollingConfig]bool{}
|
||||
if len(overrides) == 0 {
|
||||
return configs, drafts, nil
|
||||
}
|
||||
byID := map[uint]int{}
|
||||
out := make([]*model.PollingConfig, 0, len(configs)+len(overrides))
|
||||
for _, cfg := range configs {
|
||||
byID[cfg.ID] = len(out)
|
||||
out = append(out, cfg)
|
||||
}
|
||||
removed := map[int]bool{}
|
||||
for i := range overrides {
|
||||
item := overrides[i]
|
||||
if item.ID != 0 && item.Disabled {
|
||||
if idx, ok := byID[item.ID]; ok {
|
||||
removed[idx] = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
draft := &model.PollingConfig{
|
||||
ID: item.ID, ConfigName: item.ConfigName, CardCondition: item.CardCondition, CardCategory: item.CardCategory,
|
||||
CarrierID: item.CarrierID, Priority: item.Priority, RealnameCheckInterval: item.RealnameCheckInterval,
|
||||
CarddataCheckInterval: item.CarddataCheckInterval, PackageCheckInterval: item.PackageCheckInterval,
|
||||
ProtectCheckInterval: item.ProtectCheckInterval, CardStatusCheckInterval: item.CardStatusCheckInterval,
|
||||
PackageState: item.PackageState, AssetStatus: item.AssetStatus, PreResetWindow: item.PreResetWindow,
|
||||
PreResetCarddataInterval: item.PreResetCarddataInterval, Status: 1,
|
||||
}
|
||||
if draft.ConfigName == "" {
|
||||
draft.ConfigName = "未保存的配置"
|
||||
}
|
||||
if err := validatePollingConfig(draft); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
drafts[draft] = true
|
||||
if idx, ok := byID[item.ID]; ok && item.ID != 0 {
|
||||
out[idx] = draft
|
||||
continue
|
||||
}
|
||||
out = append(out, draft)
|
||||
}
|
||||
filtered := make([]*model.PollingConfig, 0, len(out))
|
||||
for idx, cfg := range out {
|
||||
if !removed[idx] {
|
||||
filtered = append(filtered, cfg)
|
||||
}
|
||||
}
|
||||
sort.SliceStable(filtered, func(i, j int) bool {
|
||||
if filtered[i].Priority != filtered[j].Priority {
|
||||
return filtered[i].Priority < filtered[j].Priority
|
||||
}
|
||||
// 草稿(ID 为 0)排在同优先级已有配置之后。
|
||||
if filtered[i].ID == 0 || filtered[j].ID == 0 {
|
||||
return filtered[j].ID == 0 && filtered[i].ID != 0
|
||||
}
|
||||
return filtered[i].ID < filtered[j].ID
|
||||
})
|
||||
return filtered, drafts, nil
|
||||
}
|
||||
|
||||
// CreateRecalculateTask 创建"立即重算到期时间"异步任务(由 Worker 执行),仅超级管理员可用。
|
||||
func (s *ConfigService) CreateRecalculateTask(ctx context.Context, req *dto.PollingRecalculateDueRequest) (*dto.PollingRecalculateDueResponse, error) {
|
||||
if middleware.GetUserTypeFromContext(ctx) != constants.UserTypeSuperAdmin {
|
||||
return nil, errors.New(errors.CodeForbidden, "仅超级管理员可重算轮询到期时间")
|
||||
}
|
||||
if s.queueClient == nil || s.redis == nil {
|
||||
return nil, errors.New(errors.CodeServiceUnavailable, "重算到期时间能力未配置")
|
||||
}
|
||||
taskID := uuid.NewString()
|
||||
carrier := ""
|
||||
if req.CarrierID != nil {
|
||||
carrier = strconv.FormatUint(uint64(*req.CarrierID), 10)
|
||||
}
|
||||
now := time.Now().Format(time.RFC3339)
|
||||
key := constants.RedisPollingRecalcProgressKey(taskID)
|
||||
pipe := s.redis.Pipeline()
|
||||
pipe.HSet(ctx, key, map[string]any{"status": constants.PollingRecalcStatusPending, "carrier_id": carrier, "created_at": now})
|
||||
pipe.Expire(ctx, key, constants.PollingRecalcProgressTTLSeconds*time.Second)
|
||||
if _, err := pipe.Exec(ctx); err != nil {
|
||||
return nil, errors.Wrap(errors.CodeRedisError, err, "创建重算任务失败")
|
||||
}
|
||||
if err := s.queueClient.EnqueueTask(ctx, constants.TaskTypePollingRecalculateDue,
|
||||
pollingcore.RecalculateDuePayload{TaskID: taskID, CarrierID: req.CarrierID},
|
||||
asynq.Queue(constants.QueueDataCleanup), asynq.MaxRetry(0), asynq.Timeout(30*time.Minute)); err != nil {
|
||||
_ = s.redis.HSet(ctx, key, "status", constants.PollingRecalcStatusFailed, "error", "任务入队失败").Err()
|
||||
return nil, errors.Wrap(errors.CodeServiceUnavailable, err, "重算任务入队失败")
|
||||
}
|
||||
return &dto.PollingRecalculateDueResponse{TaskID: taskID, Status: constants.PollingRecalcStatusPending, CarrierID: carrier, CreatedAt: now}, nil
|
||||
}
|
||||
|
||||
// GetRecalculateTask 查询重算到期时间任务进度。
|
||||
func (s *ConfigService) GetRecalculateTask(ctx context.Context, taskID string) (*dto.PollingRecalculateDueResponse, error) {
|
||||
if s.redis == nil {
|
||||
return nil, errors.New(errors.CodeServiceUnavailable, "重算到期时间能力未配置")
|
||||
}
|
||||
values, err := s.redis.HGetAll(ctx, constants.RedisPollingRecalcProgressKey(taskID)).Result()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeRedisError, err, "查询重算任务失败")
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return nil, errors.New(errors.CodeNotFound, "重算任务不存在或已过期")
|
||||
}
|
||||
atoi := func(v string) int { n, _ := strconv.Atoi(v); return n }
|
||||
return &dto.PollingRecalculateDueResponse{
|
||||
TaskID: taskID, Status: values["status"], CarrierID: values["carrier_id"], Total: atoi(values["total"]),
|
||||
Processed: atoi(values["processed"]), Advanced: atoi(values["advanced"]), Error: values["error"],
|
||||
CreatedAt: values["created_at"], StartedAt: values["started_at"], FinishedAt: values["finished_at"],
|
||||
}, nil
|
||||
}
|
||||
|
||||
// TierStats 统计开启轮询的卡按首个命中配置与套餐状态的分布,以及应告警的未匹配卡数;结果缓存 5 分钟。
|
||||
func (s *ConfigService) TierStats(ctx context.Context) (*dto.PollingTierStatsResponse, error) {
|
||||
if err := requirePlatformReader(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.configMgr == nil {
|
||||
return nil, errors.New(errors.CodeServiceUnavailable, "分层统计能力未配置")
|
||||
}
|
||||
if s.redis != nil {
|
||||
if raw, err := s.redis.Get(ctx, tierStatsCacheKey).Bytes(); err == nil {
|
||||
var cached dto.PollingTierStatsResponse
|
||||
if sonic.Unmarshal(raw, &cached) == nil {
|
||||
return &cached, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
configs, err := s.configStore.ListEnabled(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询轮询配置失败")
|
||||
}
|
||||
names := map[uint]string{0: "未匹配任何配置"}
|
||||
for _, cfg := range configs {
|
||||
names[cfg.ID] = cfg.ConfigName
|
||||
}
|
||||
resp := &dto.PollingTierStatsResponse{ByPackageState: map[string]int{}, ByConfig: []dto.PollingTierStatItem{}}
|
||||
counts := map[uint]int{}
|
||||
var lastID uint
|
||||
for {
|
||||
var cards []*model.IotCard
|
||||
if err := s.db.WithContext(ctx).Where("id > ? AND enable_polling = true AND deleted_at IS NULL", lastID).
|
||||
Order("id ASC").Limit(1000).Find(&cards).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询轮询卡失败")
|
||||
}
|
||||
if len(cards) == 0 {
|
||||
break
|
||||
}
|
||||
lastID = cards[len(cards)-1].ID
|
||||
if tier := s.configMgr.TierResolver(); tier != nil {
|
||||
tier.Prefill(ctx, cards)
|
||||
}
|
||||
for _, card := range cards {
|
||||
resp.TotalCards++
|
||||
facts := s.configMgr.TierFactsOf(card)
|
||||
state := "unknown"
|
||||
if facts.Known {
|
||||
state = facts.PackageState
|
||||
}
|
||||
resp.ByPackageState[state]++
|
||||
matched, _ := s.configMgr.PreviewIntervalSources(configs, card)
|
||||
if len(matched) == 0 {
|
||||
counts[0]++
|
||||
if card.AssetStatus == constants.AssetStatusInStock {
|
||||
resp.UnmatchedInStock++
|
||||
} else if card.AssetStatus == constants.AssetStatusSold && facts.Known && facts.PackageState == constants.PollingPackageStateNone {
|
||||
resp.UnmatchedSoldNoPackage++
|
||||
}
|
||||
continue
|
||||
}
|
||||
counts[matched[0].ID]++
|
||||
}
|
||||
}
|
||||
for id, n := range counts {
|
||||
resp.ByConfig = append(resp.ByConfig, dto.PollingTierStatItem{ConfigID: id, ConfigName: names[id], Cards: n})
|
||||
}
|
||||
sort.Slice(resp.ByConfig, func(i, j int) bool { return resp.ByConfig[i].Cards > resp.ByConfig[j].Cards })
|
||||
resp.ComputedAt = time.Now().Format(time.RFC3339)
|
||||
if s.redis != nil {
|
||||
if raw, err := sonic.Marshal(resp); err == nil {
|
||||
_ = s.redis.Set(ctx, tierStatsCacheKey, raw, 5*time.Minute).Err()
|
||||
}
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// previewTaskTypeName 返回检查类型中文名称(保护期检查在优先轮询名称表之外单独处理)。
|
||||
func previewTaskTypeName(taskType string) string {
|
||||
if taskType == constants.TaskTypePollingProtect {
|
||||
return "保护期检查"
|
||||
}
|
||||
return constants.PollingPriorityTaskTypeName(taskType)
|
||||
}
|
||||
|
||||
// PreResetStats 按运营商账户统计重置日前加密效果:上一个与即将到来的重置周期内,窗口内查到流量的卡数、
|
||||
// 有生效套餐但窗口内未查到的卡数,以及最后一次查询距重置时刻的分布。
|
||||
func (s *ConfigService) PreResetStats(ctx context.Context) (*dto.PollingPreResetStatsResponse, error) {
|
||||
if err := requirePlatformReader(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.configMgr == nil || s.redis == nil {
|
||||
return nil, errors.New(errors.CodeServiceUnavailable, "重置日前加密统计能力未配置")
|
||||
}
|
||||
var rows []struct {
|
||||
CarrierID uint
|
||||
CarrierName string
|
||||
Active int
|
||||
}
|
||||
err := s.db.WithContext(ctx).Raw(`
|
||||
SELECT c.carrier_id, COALESCE(ca.carrier_name, '') AS carrier_name,
|
||||
COUNT(*) FILTER (WHERE (c.is_standalone AND EXISTS (
|
||||
SELECT 1 FROM tb_package_usage u WHERE u.iot_card_id = c.id AND u.status = ? AND u.deleted_at IS NULL))
|
||||
OR (NOT c.is_standalone AND EXISTS (
|
||||
SELECT 1 FROM tb_device_sim_binding b JOIN tb_package_usage u ON u.device_id = b.device_id AND u.status = ? AND u.deleted_at IS NULL
|
||||
WHERE b.iot_card_id = c.id AND b.bind_status = 1 AND b.deleted_at IS NULL))) AS active
|
||||
FROM tb_iot_card c LEFT JOIN tb_carrier ca ON ca.id = c.carrier_id
|
||||
WHERE c.enable_polling = true AND c.deleted_at IS NULL
|
||||
GROUP BY c.carrier_id, ca.carrier_name ORDER BY c.carrier_id`,
|
||||
constants.PackageUsageStatusActive, constants.PackageUsageStatusActive).Scan(&rows).Error
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "统计有生效套餐的卡失败")
|
||||
}
|
||||
resp := &dto.PollingPreResetStatsResponse{Carriers: []dto.PollingPreResetCarrierStat{}}
|
||||
now := time.Now()
|
||||
location, _ := time.LoadLocation("Asia/Shanghai")
|
||||
for _, row := range rows {
|
||||
previous, upcoming := s.configMgr.ResetMoments(ctx, row.CarrierID, now)
|
||||
item := dto.PollingPreResetCarrierStat{CarrierID: row.CarrierID, CarrierName: row.CarrierName}
|
||||
item.Previous = s.preResetCycle(ctx, row.CarrierID, previous, row.Active, location)
|
||||
item.Upcoming = s.preResetCycle(ctx, row.CarrierID, upcoming, row.Active, location)
|
||||
resp.Carriers = append(resp.Carriers, item)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *ConfigService) preResetCycle(ctx context.Context, carrierID uint, reset time.Time, active int, location *time.Location) dto.PollingPreResetCycleStat {
|
||||
date := reset.Format("20060102")
|
||||
stat := dto.PollingPreResetCycleStat{ResetAt: reset.In(location).Format("2006-01-02 15:04"), ActivePackageCards: active,
|
||||
LastOffsetBuckets: map[string]int{"le_5": 0, "le_15": 0, "le_30": 0, "le_60": 0, "gt_60": 0}}
|
||||
if n, err := s.redis.PFCount(ctx, constants.RedisPollingPreResetCheckedKey(carrierID, date)).Result(); err == nil {
|
||||
stat.CheckedInWindow = n
|
||||
}
|
||||
if missing := int64(active) - stat.CheckedInWindow; missing > 0 {
|
||||
stat.NotCheckedInWindow = missing
|
||||
}
|
||||
if values, err := s.redis.HVals(ctx, constants.RedisPollingPreResetOffsetKey(carrierID, date)).Result(); err == nil {
|
||||
for _, raw := range values {
|
||||
minutes, convErr := strconv.Atoi(raw)
|
||||
if convErr != nil {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case minutes <= 5:
|
||||
stat.LastOffsetBuckets["le_5"]++
|
||||
case minutes <= 15:
|
||||
stat.LastOffsetBuckets["le_15"]++
|
||||
case minutes <= 30:
|
||||
stat.LastOffsetBuckets["le_30"]++
|
||||
case minutes <= 60:
|
||||
stat.LastOffsetBuckets["le_60"]++
|
||||
default:
|
||||
stat.LastOffsetBuckets["gt_60"]++
|
||||
}
|
||||
}
|
||||
}
|
||||
return stat
|
||||
}
|
||||
139
internal/service/polling/config_validation.go
Normal file
139
internal/service/polling/config_validation.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package polling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
)
|
||||
|
||||
// validatePollingConfig 校验轮询配置:间隔为 0(不检查)或不小于最小间隔;至少配置一种间隔;
|
||||
// 重置日前加密的两个字段成对配置;匹配"无套餐且已销售"或"在库"的配置卡状态检查间隔必须为正数。
|
||||
// 现有 Handler 未调用校验器,校验在服务层完成。
|
||||
func validatePollingConfig(cfg *model.PollingConfig) error {
|
||||
intervals := []struct {
|
||||
name string
|
||||
value *int
|
||||
min int
|
||||
}{
|
||||
{"实名检查间隔", cfg.RealnameCheckInterval, 30},
|
||||
{"流量检查间隔", cfg.CarddataCheckInterval, 60},
|
||||
{"套餐检查间隔", cfg.PackageCheckInterval, 60},
|
||||
{"保护期一致性检查间隔", cfg.ProtectCheckInterval, 30},
|
||||
{"卡状态检查间隔", cfg.CardStatusCheckInterval, 30},
|
||||
}
|
||||
configured := false
|
||||
for _, item := range intervals {
|
||||
if item.value == nil {
|
||||
continue
|
||||
}
|
||||
configured = true
|
||||
if *item.value < 0 || (*item.value > 0 && *item.value < item.min) {
|
||||
return errors.New(errors.CodeInvalidParam, fmt.Sprintf("轮询配置参数不合法:%s须为 0(不检查)或不小于 %d 秒", item.name, item.min))
|
||||
}
|
||||
}
|
||||
if !configured {
|
||||
return errors.New(errors.CodeInvalidParam, "至少需要配置一种检查间隔")
|
||||
}
|
||||
if cfg.PackageState != nil && *cfg.PackageState != "" && !validPackageState(*cfg.PackageState) {
|
||||
return errors.New(errors.CodeInvalidParam, "轮询配置参数不合法:套餐状态取值不合法")
|
||||
}
|
||||
if cfg.AssetStatus != nil && (*cfg.AssetStatus < constants.AssetStatusInStock || *cfg.AssetStatus > constants.AssetStatusDeactivated) {
|
||||
return errors.New(errors.CodeInvalidParam, "轮询配置参数不合法:资产状态取值不合法")
|
||||
}
|
||||
if (cfg.PreResetWindow == nil) != (cfg.PreResetCarddataInterval == nil) {
|
||||
return errors.New(errors.CodeInvalidParam, "轮询配置参数不合法:重置日前窗口长度与窗口内流量间隔须同时配置或同时为空")
|
||||
}
|
||||
if cfg.PreResetWindow != nil && (*cfg.PreResetWindow < 60 || *cfg.PreResetCarddataInterval < 60) {
|
||||
return errors.New(errors.CodeInvalidParam, "轮询配置参数不合法:重置日前窗口长度与窗口内流量间隔不小于 60 秒")
|
||||
}
|
||||
if requiresCardStatusCheck(cfg) && (cfg.CardStatusCheckInterval == nil || *cfg.CardStatusCheckInterval <= 0) {
|
||||
return errors.New(errors.CodeInvalidParam, "轮询配置参数不合法:匹配无套餐已销售卡或在库卡的配置必须配置卡状态检查间隔(正数),用于发现上游开机后停机")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// requiresCardStatusCheck 判断配置是否匹配必须兜底查卡状态的层:在库,或无套餐且已销售。
|
||||
func requiresCardStatusCheck(cfg *model.PollingConfig) bool {
|
||||
if cfg.AssetStatus != nil && *cfg.AssetStatus == constants.AssetStatusInStock {
|
||||
return true
|
||||
}
|
||||
return cfg.AssetStatus != nil && *cfg.AssetStatus == constants.AssetStatusSold &&
|
||||
cfg.PackageState != nil && *cfg.PackageState == constants.PollingPackageStateNone
|
||||
}
|
||||
|
||||
func validPackageState(state string) bool {
|
||||
for _, item := range constants.PollingPackageStates {
|
||||
if item == state {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// applyPollingConfigClearFields 把指定字段清空为 NULL:间隔清空即改为继承,匹配条件清空即不限。
|
||||
func applyPollingConfigClearFields(cfg *model.PollingConfig, fields []string) error {
|
||||
for _, field := range fields {
|
||||
switch field {
|
||||
case "realname_check_interval":
|
||||
cfg.RealnameCheckInterval = nil
|
||||
case "carddata_check_interval":
|
||||
cfg.CarddataCheckInterval = nil
|
||||
case "package_check_interval":
|
||||
cfg.PackageCheckInterval = nil
|
||||
case "card_status_check_interval":
|
||||
cfg.CardStatusCheckInterval = nil
|
||||
case "carrier_id":
|
||||
cfg.CarrierID = nil
|
||||
case "package_state":
|
||||
cfg.PackageState = nil
|
||||
case "asset_status":
|
||||
cfg.AssetStatus = nil
|
||||
case "pre_reset_window":
|
||||
cfg.PreResetWindow = nil
|
||||
case "pre_reset_carddata_interval":
|
||||
cfg.PreResetCarddataInterval = nil
|
||||
default:
|
||||
return errors.New(errors.CodeInvalidParam, "轮询配置参数不合法:不支持清空字段 "+field)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// priorityWarnings 返回与其他启用配置优先级相同且匹配条件可能重叠的提示;只提示不拦截。
|
||||
func (s *ConfigService) priorityWarnings(ctx context.Context, cfg *model.PollingConfig) []string {
|
||||
others, err := s.configStore.ListEnabled(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var warnings []string
|
||||
for _, other := range others {
|
||||
if other.ID == cfg.ID || other.Priority != cfg.Priority || !conditionsMayOverlap(cfg, other) {
|
||||
continue
|
||||
}
|
||||
warnings = append(warnings, fmt.Sprintf("与配置「%s」优先级同为 %d 且匹配条件可能重叠,相同优先级按配置 ID 升序生效", other.ConfigName, cfg.Priority))
|
||||
}
|
||||
return warnings
|
||||
}
|
||||
|
||||
// conditionsMayOverlap 判断两条配置是否可能匹配同一张卡:任一维度两边都填且取值不同即不重叠。
|
||||
func conditionsMayOverlap(a, b *model.PollingConfig) bool {
|
||||
if a.CardCondition != "" && b.CardCondition != "" && a.CardCondition != b.CardCondition {
|
||||
return false
|
||||
}
|
||||
if a.CardCategory != "" && b.CardCategory != "" && a.CardCategory != b.CardCategory {
|
||||
return false
|
||||
}
|
||||
if a.CarrierID != nil && b.CarrierID != nil && *a.CarrierID != *b.CarrierID {
|
||||
return false
|
||||
}
|
||||
if a.AssetStatus != nil && b.AssetStatus != nil && *a.AssetStatus != *b.AssetStatus {
|
||||
return false
|
||||
}
|
||||
if a.PackageState != nil && b.PackageState != nil && *a.PackageState != *b.PackageState {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user