feat: 卡流量改为日快照计量并统一停复机规则
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled

卡流量计量(按名单灰度,默认 off):
- 新增卡日快照与切换水位表(000241),按通道上游流量重置日换算自然月、重置日防旧值
- 已切换的卡按月对账产生增量,无生效中套餐只结算不发事件;第一次切换写水位
- 已切换卡读数有效即判定通道阈值,卡表本月用量用快照覆盖,日流量读快照
- 配置 JUNHONG_TRAFFIC_CALC_MODE/CARDS、JUNHONG_TRAFFIC_FLOW_SNAPSHOT_ENABLED

停复机规则(总开关 JUNHONG_STOP_RESUME_UNIFIED_RULE_ENABLED,默认关闭):
- 复机不看停机原因,持通道阈值锁仍拒绝;暂停轮询的卡不自动停复机
- 通道新增实名管控 realname_control_enabled(000242,默认 0),开启后不区分行业卡与实名策略
- 实名管控从 0 改 1 需确认,修改并入通道更新审计

OpenSpec Change: rebuild-card-traffic-metering-and-stop-resume
This commit is contained in:
邹进
2026-10-20 17:49:26 +09:00
parent 44a05cf687
commit 96771a204f
37 changed files with 1859 additions and 98 deletions

View File

@@ -0,0 +1,44 @@
package model
import "time"
// CardFlowDay 卡日流量快照:每卡每天一条本月自然月累计。
//
// MonthUsedMB 按通道上游流量重置日换算为自然月累计,同一天只增不减;SettledMB 是当天已结算的增量合计
// (发出的增量、无生效套餐时丢弃的增量、切换时视为老算法已结算的部分)。由观测链路原子写入,永久保留。
type CardFlowDay struct {
ID uint `gorm:"column:id;primaryKey" json:"id"`
IotCardID uint `gorm:"column:iot_card_id;type:bigint;not null;uniqueIndex:uk_card_flow_day_card_date" json:"iot_card_id"`
FlowDate time.Time `gorm:"column:flow_date;type:date;not null;uniqueIndex:uk_card_flow_day_card_date" json:"flow_date"`
MonthUsedMB float64 `gorm:"column:month_used_mb;type:numeric(14,3);not null;default:0" json:"month_used_mb"`
VendorUsedMB float64 `gorm:"column:vendor_used_mb;type:numeric(14,3);not null;default:0" json:"vendor_used_mb"`
SettledMB float64 `gorm:"column:settled_mb;type:numeric(14,3);not null;default:0" json:"settled_mb"`
StaleGuarded bool `gorm:"column:stale_guarded;not null;default:false" json:"stale_guarded"`
ObsCount int `gorm:"column:obs_count;not null;default:0" json:"obs_count"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime" json:"updated_at"`
}
// TableName 指定表名
func (CardFlowDay) TableName() string {
return "tb_card_flow_day"
}
// CardFlowSwitch 卡流量计量切换水位:卡第一次走新算法时写入,退回老算法时由维护者删除。
type CardFlowSwitch struct {
ID uint `gorm:"column:id;primaryKey" json:"id"`
IotCardID uint `gorm:"column:iot_card_id;type:bigint;not null;uniqueIndex:uk_card_flow_switch_card" json:"iot_card_id"`
SwitchedAt time.Time `gorm:"column:switched_at;type:timestamptz;not null" json:"switched_at"`
MonthUsedMB float64 `gorm:"column:month_used_mb;type:numeric(14,3);not null;default:0" json:"month_used_mb"`
VendorUsedMB float64 `gorm:"column:vendor_used_mb;type:numeric(14,3);not null;default:0" json:"vendor_used_mb"`
OldBaselineMB float64 `gorm:"column:old_baseline_mb;type:numeric(14,3);not null;default:0" json:"old_baseline_mb"`
OldMonthUsageMB float64 `gorm:"column:old_month_usage_mb;type:numeric(14,3);not null;default:0" json:"old_month_usage_mb"`
OldReadingAccepted bool `gorm:"column:old_reading_accepted;not null;default:false" json:"old_reading_accepted"`
OldIncrementMB float64 `gorm:"column:old_increment_mb;type:numeric(14,3);not null;default:0" json:"old_increment_mb"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime" json:"created_at"`
}
// TableName 指定表名
func (CardFlowSwitch) TableName() string {
return "tb_card_flow_switch"
}

View File

@@ -22,6 +22,9 @@ type Carrier struct {
TrafficThresholdValue *float64 `gorm:"column:traffic_threshold_value;type:numeric(18,2);comment:通道流量阈值数值,单位由 traffic_threshold_unit 决定" json:"traffic_threshold_value"`
// TrafficThresholdUnit 是阈值单位,取值 MB/GB(1 GB = 1024 MB),换算后与网关累计读数同单位比较;空字符串表示未配置。
TrafficThresholdUnit string `gorm:"column:traffic_threshold_unit;type:varchar(8);not null;default:'';comment:通道流量阈值单位 MB/GB,空字符串-未配置" json:"traffic_threshold_unit"`
// RealnameControlEnabled 是通道实名管控开关,取值 0-不管控(默认)1-管控;只用于停复机,
// 且只在停复机新规则总开关开启后参与判断,不复用 RealnameLinkType 等现有实名字段。
RealnameControlEnabled int `gorm:"column:realname_control_enabled;type:smallint;not null;default:0;comment:通道实名管控 0-不管控 1-管控" json:"realname_control_enabled"`
}
// TableName 指定表名

View File

@@ -13,15 +13,17 @@ type CreateCarrierRequest struct {
TrafficThresholdEnabled *int `json:"traffic_threshold_enabled" validate:"omitempty,oneof=0 1" description:"通道流量阈值是否启用 (0:禁用, 1:启用),仅超级管理员与平台账号可配置"`
TrafficThresholdValue *float64 `json:"traffic_threshold_value" validate:"omitempty,gt=0" description:"通道流量阈值数值,必须为正数,单位由 traffic_threshold_unit 决定,仅超级管理员与平台账号可配置"`
TrafficThresholdUnit *string `json:"traffic_threshold_unit" validate:"omitempty,oneof=MB GB" description:"通道流量阈值单位 (MB:兆字节, GB:吉字节,1 GB = 1024 MB),仅超级管理员与平台账号可配置"`
RealnameControlEnabled *int `json:"realname_control_enabled" validate:"omitempty,oneof=0 1" description:"通道实名管控 (0:不管控, 1:管控),默认0;只在停复机新规则开启后影响停复机:管控时未实名卡停机、实名后才复机。仅超级管理员与平台账号可配置"`
}
// HasTrafficThresholdFields 判断请求是否携带通道流量阈值字段。
// 非超管、非平台账号提交携带阈值字段的请求必须被整体拒绝。
// HasTrafficThresholdFields 判断请求是否携带仅平台账号可配置的通道字段(通道流量阈值、通道实名管控)。
// 非超管、非平台账号提交携带这些字段的请求必须被整体拒绝。
func (r *CreateCarrierRequest) HasTrafficThresholdFields() bool {
if r == nil {
return false
}
return r.TrafficThresholdEnabled != nil || r.TrafficThresholdValue != nil || r.TrafficThresholdUnit != nil
return r.TrafficThresholdEnabled != nil || r.TrafficThresholdValue != nil || r.TrafficThresholdUnit != nil ||
r.RealnameControlEnabled != nil
}
type UpdateCarrierRequest struct {
@@ -33,15 +35,18 @@ type UpdateCarrierRequest struct {
TrafficThresholdEnabled *int `json:"traffic_threshold_enabled" validate:"omitempty,oneof=0 1" description:"通道流量阈值是否启用 (0:禁用, 1:启用),仅超级管理员与平台账号可配置"`
TrafficThresholdValue *float64 `json:"traffic_threshold_value" validate:"omitempty,gt=0" description:"通道流量阈值数值,必须为正数,单位由 traffic_threshold_unit 决定,仅超级管理员与平台账号可配置"`
TrafficThresholdUnit *string `json:"traffic_threshold_unit" validate:"omitempty,oneof=MB GB" description:"通道流量阈值单位 (MB:兆字节, GB:吉字节,1 GB = 1024 MB),仅超级管理员与平台账号可配置"`
RealnameControlEnabled *int `json:"realname_control_enabled" validate:"omitempty,oneof=0 1" description:"通道实名管控 (0:不管控, 1:管控),默认0;只在停复机新规则开启后影响停复机:管控时未实名卡停机、实名后才复机。仅超级管理员与平台账号可配置"`
RealnameControlConfirm *bool `json:"realname_control_confirm" description:"把实名管控从 0 改为 1 时必须传 true,确认该通道未实名的开机卡会在下一次评估时被停机"`
}
// HasTrafficThresholdFields 判断请求是否携带通道流量阈值字段。
// 非超管、非平台账号提交携带阈值字段的请求必须被整体拒绝。
// HasTrafficThresholdFields 判断请求是否携带仅平台账号可配置的通道字段(通道流量阈值、通道实名管控)。
// 非超管、非平台账号提交携带这些字段的请求必须被整体拒绝。
func (r *UpdateCarrierRequest) HasTrafficThresholdFields() bool {
if r == nil {
return false
}
return r.TrafficThresholdEnabled != nil || r.TrafficThresholdValue != nil || r.TrafficThresholdUnit != nil
return r.TrafficThresholdEnabled != nil || r.TrafficThresholdValue != nil || r.TrafficThresholdUnit != nil ||
r.RealnameControlEnabled != nil
}
type CarrierListRequest struct {
@@ -70,6 +75,7 @@ type CarrierResponse struct {
TrafficThresholdEnabled *int `json:"traffic_threshold_enabled,omitempty" description:"通道流量阈值是否启用 (0:禁用, 1:启用),仅超级管理员与平台账号可见"`
TrafficThresholdValue *float64 `json:"traffic_threshold_value,omitempty" description:"通道流量阈值数值,单位由 traffic_threshold_unit 决定,未配置时不返回,仅超级管理员与平台账号可见"`
TrafficThresholdUnit *string `json:"traffic_threshold_unit,omitempty" description:"通道流量阈值单位 (MB:兆字节, GB:吉字节,1 GB = 1024 MB),未配置时不返回,仅超级管理员与平台账号可见"`
RealnameControlEnabled *int `json:"realname_control_enabled,omitempty" description:"通道实名管控 (0:不管控, 1:管控),仅超级管理员与平台账号可见"`
CreatedAt string `json:"created_at" description:"创建时间"`
UpdatedAt string `json:"updated_at" description:"更新时间"`
}