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:
@@ -291,3 +291,11 @@ func realnameChangedEventID(cardID uint, observationID string) string {
|
||||
prefix := "card-realname:"
|
||||
return outboxid.Stable(prefix, strconv.FormatUint(uint64(cardID), 10)+":"+observationID+":changed")
|
||||
}
|
||||
|
||||
// TrafficSwitched 判定卡是否已切换到日快照计量(激活前流量结算只对已切换的卡执行)。
|
||||
func (s *Service) TrafficSwitched(cardID uint) bool {
|
||||
if s == nil {
|
||||
return false
|
||||
}
|
||||
return s.trafficCalc.Switched(cardID)
|
||||
}
|
||||
|
||||
@@ -104,6 +104,10 @@ func (s *Service) ApplyTrafficObservation(ctx context.Context, observation domai
|
||||
if switched {
|
||||
evaluateThreshold = switchedThresholdEvaluable(flow, decision.ReadingAccepted)
|
||||
}
|
||||
if observation.Metadata.Scene == constants.CardObservationScenePreActivationSettlement {
|
||||
// 激活前结算只记已结算用量,不触发停复机评估;下一次正常轮询观测照常判定。
|
||||
evaluateThreshold = false
|
||||
}
|
||||
if evaluateThreshold && s.channelThreshold != nil {
|
||||
// 未切换卡只使用本次已接受的网关累计读数,异常下降保护命中的观测不参与判定;
|
||||
// 已切换卡读数有效且未被重置日防旧值处理即判定,不再受卡表基线影响。
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package cardobservation
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -115,7 +117,7 @@ func (p *TrafficCalcPolicy) expandItem(ctx context.Context, db *gorm.DB, item st
|
||||
// cardUsesDevicePackage 与套餐扣减的载体解析同口径:卡自身没有生效中套餐、且当前绑定设备有生效中套餐时,
|
||||
// 卡的流量扣到设备套餐上。
|
||||
func cardUsesDevicePackage(ctx context.Context, db *gorm.DB, cardID uint) (bool, error) {
|
||||
ownActive, err := countActivePackages(ctx, db, "iot_card_id = ?", cardID)
|
||||
ownActive, err := countActivePackages(ctx, db, "iot_card_id = ?", cardID, time.Time{})
|
||||
if err != nil || ownActive > 0 {
|
||||
return false, err
|
||||
}
|
||||
@@ -123,13 +125,15 @@ func cardUsesDevicePackage(ctx context.Context, db *gorm.DB, cardID uint) (bool,
|
||||
if err != nil || !bound {
|
||||
return false, err
|
||||
}
|
||||
deviceActive, err := countActivePackages(ctx, db, "device_id = ?", deviceID)
|
||||
deviceActive, err := countActivePackages(ctx, db, "device_id = ?", deviceID, time.Time{})
|
||||
return deviceActive > 0, err
|
||||
}
|
||||
|
||||
// hasActivePackageForCard 判定卡本次增量是否有可扣的生效中套餐:卡自身或当前绑定设备存在生效中套餐。
|
||||
func hasActivePackageForCard(ctx context.Context, db *gorm.DB, cardID uint) (bool, error) {
|
||||
ownActive, err := countActivePackages(ctx, db, "iot_card_id = ?", cardID)
|
||||
// observedAt 非零时只认激活时间不晚于观测时间的生效套餐(流量按观测时间归属):
|
||||
// 观测早于套餐激活的用量属于激活前,只记已结算、不扣新套餐;激活时间为空的历史套餐视为早已生效。
|
||||
func hasActivePackageForCard(ctx context.Context, db *gorm.DB, cardID uint, observedAt time.Time) (bool, error) {
|
||||
ownActive, err := countActivePackages(ctx, db, "iot_card_id = ?", cardID, observedAt)
|
||||
if err != nil || ownActive > 0 {
|
||||
return ownActive > 0, err
|
||||
}
|
||||
@@ -137,16 +141,19 @@ func hasActivePackageForCard(ctx context.Context, db *gorm.DB, cardID uint) (boo
|
||||
if err != nil || !bound {
|
||||
return false, err
|
||||
}
|
||||
deviceActive, err := countActivePackages(ctx, db, "device_id = ?", deviceID)
|
||||
deviceActive, err := countActivePackages(ctx, db, "device_id = ?", deviceID, observedAt)
|
||||
return deviceActive > 0, err
|
||||
}
|
||||
|
||||
func countActivePackages(ctx context.Context, db *gorm.DB, carrierCondition string, carrierID uint) (int64, error) {
|
||||
func countActivePackages(ctx context.Context, db *gorm.DB, carrierCondition string, carrierID uint, observedAt time.Time) (int64, error) {
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Model(&model.PackageUsage{}).
|
||||
query := db.WithContext(ctx).Model(&model.PackageUsage{}).
|
||||
Where(carrierCondition, carrierID).
|
||||
Where("status = ?", constants.PackageUsageStatusActive).
|
||||
Count(&count).Error
|
||||
Where("status = ?", constants.PackageUsageStatusActive)
|
||||
if !observedAt.IsZero() {
|
||||
query = query.Where("activated_at IS NULL OR activated_at <= ?", observedAt)
|
||||
}
|
||||
err := query.Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ func (s *Service) applySwitchedFlow(ctx context.Context, tx *gorm.DB, card *mode
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询卡本月流量结算失败")
|
||||
}
|
||||
hasPackage, pkgErr := hasActivePackageForCard(ctx, tx, card.ID)
|
||||
hasPackage, pkgErr := hasActivePackageForCard(ctx, tx, card.ID, observation.Metadata.ObservedAt)
|
||||
if pkgErr != nil {
|
||||
// 判断出错按有套餐处理,保持不丢量。
|
||||
s.warnLogger().Warn("判断卡是否有生效中套餐失败,按有套餐处理", zap.Uint("card_id", card.ID), zap.Error(pkgErr))
|
||||
|
||||
Reference in New Issue
Block a user