feat: 卡流量改为日快照计量并统一停复机规则
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
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:
130
internal/store/postgres/card_flow_day_store.go
Normal file
130
internal/store/postgres/card_flow_day_store.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/domain/cardflow"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
)
|
||||
|
||||
// cardFlowDateLayout 是快照日期参数格式,按上海日期字符串传给数据库,避免时区换算。
|
||||
const cardFlowDateLayout = "2006-01-02"
|
||||
|
||||
// CardFlowDayStore 卡日流量快照存储。
|
||||
type CardFlowDayStore struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewCardFlowDayStore 创建卡日流量快照存储。
|
||||
func NewCardFlowDayStore(db *gorm.DB) *CardFlowDayStore {
|
||||
return &CardFlowDayStore{db: db}
|
||||
}
|
||||
|
||||
// WithTx 返回复用当前事务的快照存储。
|
||||
func (s *CardFlowDayStore) WithTx(tx *gorm.DB) *CardFlowDayStore {
|
||||
return &CardFlowDayStore{db: tx}
|
||||
}
|
||||
|
||||
// CardFlowSnapshotWrite 是一次快照写入的取值。
|
||||
type CardFlowSnapshotWrite struct {
|
||||
CardID uint
|
||||
FlowDate time.Time
|
||||
MonthUsedMB float64
|
||||
VendorUsedMB float64
|
||||
StaleGuarded bool
|
||||
}
|
||||
|
||||
// Upsert 原子写入当天快照:本月累计取已有值与本次值中的较大者,防旧值标记只要当天触发过就保留。
|
||||
func (s *CardFlowDayStore) Upsert(ctx context.Context, write CardFlowSnapshotWrite) error {
|
||||
return s.db.WithContext(ctx).Exec(`
|
||||
INSERT INTO tb_card_flow_day (iot_card_id, flow_date, month_used_mb, vendor_used_mb, stale_guarded, obs_count, created_at, updated_at)
|
||||
VALUES (?, ?::date, ?, ?, ?, 1, NOW(), NOW())
|
||||
ON CONFLICT (iot_card_id, flow_date) DO UPDATE SET
|
||||
month_used_mb = GREATEST(tb_card_flow_day.month_used_mb, EXCLUDED.month_used_mb),
|
||||
vendor_used_mb = EXCLUDED.vendor_used_mb,
|
||||
stale_guarded = tb_card_flow_day.stale_guarded OR EXCLUDED.stale_guarded,
|
||||
obs_count = tb_card_flow_day.obs_count + 1,
|
||||
updated_at = NOW()`,
|
||||
write.CardID, write.FlowDate.Format(cardFlowDateLayout), write.MonthUsedMB, write.VendorUsedMB, write.StaleGuarded,
|
||||
).Error
|
||||
}
|
||||
|
||||
// MaxBetween 返回 [from, to] 日期闭区间内的最大本月累计,没有快照时返回 0。
|
||||
func (s *CardFlowDayStore) MaxBetween(ctx context.Context, cardID uint, from, to time.Time) (float64, error) {
|
||||
if to.Before(from) {
|
||||
return 0, nil
|
||||
}
|
||||
var max float64
|
||||
err := s.db.WithContext(ctx).Model(&model.CardFlowDay{}).
|
||||
Select("COALESCE(MAX(month_used_mb), 0)").
|
||||
Where("iot_card_id = ? AND flow_date >= ?::date AND flow_date <= ?::date",
|
||||
cardID, from.Format(cardFlowDateLayout), to.Format(cardFlowDateLayout)).
|
||||
Scan(&max).Error
|
||||
return max, err
|
||||
}
|
||||
|
||||
// SettledBetween 返回 [from, to] 日期闭区间内的已结算合计。
|
||||
func (s *CardFlowDayStore) SettledBetween(ctx context.Context, cardID uint, from, to time.Time) (float64, error) {
|
||||
var sum float64
|
||||
err := s.db.WithContext(ctx).Model(&model.CardFlowDay{}).
|
||||
Select("COALESCE(SUM(settled_mb), 0)").
|
||||
Where("iot_card_id = ? AND flow_date >= ?::date AND flow_date <= ?::date",
|
||||
cardID, from.Format(cardFlowDateLayout), to.Format(cardFlowDateLayout)).
|
||||
Scan(&sum).Error
|
||||
return sum, err
|
||||
}
|
||||
|
||||
// AddSettled 把增量计入当天已结算;当天快照行必须已由 Upsert 写入。
|
||||
func (s *CardFlowDayStore) AddSettled(ctx context.Context, cardID uint, flowDate time.Time, deltaMB float64) error {
|
||||
if deltaMB <= 0 {
|
||||
return nil
|
||||
}
|
||||
return s.db.WithContext(ctx).Exec(
|
||||
"UPDATE tb_card_flow_day SET settled_mb = settled_mb + ?, updated_at = NOW() WHERE iot_card_id = ? AND flow_date = ?::date",
|
||||
deltaMB, cardID, flowDate.Format(cardFlowDateLayout),
|
||||
).Error
|
||||
}
|
||||
|
||||
// MonthContext 查询换算与防旧值需要的已有快照取值:本月重置日前最大、上月最大与上月重置日前最大。
|
||||
func (s *CardFlowDayStore) MonthContext(ctx context.Context, cardID uint, flowDate time.Time, resetDay int) (cardflow.MonthContext, error) {
|
||||
monthStart := cardflow.MonthStart(flowDate)
|
||||
prevStart := monthStart.AddDate(0, -1, 0)
|
||||
prevEnd := monthStart.AddDate(0, 0, -1)
|
||||
var result cardflow.MonthContext
|
||||
var err error
|
||||
if result.PrevMonthMax, err = s.MaxBetween(ctx, cardID, prevStart, prevEnd); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if resetDay <= 1 {
|
||||
return result, nil
|
||||
}
|
||||
if result.ThisMonthBeforeResetMax, err = s.MaxBetween(ctx, cardID, monthStart, monthStart.AddDate(0, 0, resetDay-2)); err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.PrevMonthBeforeResetMax, err = s.MaxBetween(ctx, cardID, prevStart, prevStart.AddDate(0, 0, resetDay-2))
|
||||
return result, err
|
||||
}
|
||||
|
||||
// MonthTotals 返回截至 flowDate 的本月最大快照与本月已结算合计,用于按月对账。
|
||||
func (s *CardFlowDayStore) MonthTotals(ctx context.Context, cardID uint, flowDate time.Time) (monthMax float64, settled float64, err error) {
|
||||
monthStart := cardflow.MonthStart(flowDate)
|
||||
monthEnd := monthStart.AddDate(0, 1, -1)
|
||||
if monthMax, err = s.MaxBetween(ctx, cardID, monthStart, monthEnd); err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
settled, err = s.SettledBetween(ctx, cardID, monthStart, monthEnd)
|
||||
return monthMax, settled, err
|
||||
}
|
||||
|
||||
// ListBetween 返回 [from, to] 日期闭区间内的快照,按日期升序。
|
||||
func (s *CardFlowDayStore) ListBetween(ctx context.Context, cardID uint, from, to time.Time) ([]model.CardFlowDay, error) {
|
||||
var rows []model.CardFlowDay
|
||||
err := s.db.WithContext(ctx).
|
||||
Where("iot_card_id = ? AND flow_date >= ?::date AND flow_date <= ?::date",
|
||||
cardID, from.Format(cardFlowDateLayout), to.Format(cardFlowDateLayout)).
|
||||
Order("flow_date ASC").Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
43
internal/store/postgres/card_flow_switch_store.go
Normal file
43
internal/store/postgres/card_flow_switch_store.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
stderrors "errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
)
|
||||
|
||||
// CardFlowSwitchStore 卡流量计量切换水位存储。
|
||||
type CardFlowSwitchStore struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewCardFlowSwitchStore 创建切换水位存储。
|
||||
func NewCardFlowSwitchStore(db *gorm.DB) *CardFlowSwitchStore {
|
||||
return &CardFlowSwitchStore{db: db}
|
||||
}
|
||||
|
||||
// WithTx 返回复用当前事务的切换水位存储。
|
||||
func (s *CardFlowSwitchStore) WithTx(tx *gorm.DB) *CardFlowSwitchStore {
|
||||
return &CardFlowSwitchStore{db: tx}
|
||||
}
|
||||
|
||||
// Get 返回卡的切换水位;不存在时返回 nil、nil。
|
||||
func (s *CardFlowSwitchStore) Get(ctx context.Context, cardID uint) (*model.CardFlowSwitch, error) {
|
||||
var row model.CardFlowSwitch
|
||||
err := s.db.WithContext(ctx).Where("iot_card_id = ?", cardID).First(&row).Error
|
||||
if stderrors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// Create 写入切换水位;同卡已存在时由唯一索引拒绝,调用方随事务回滚后重试。
|
||||
func (s *CardFlowSwitchStore) Create(ctx context.Context, row *model.CardFlowSwitch) error {
|
||||
return s.db.WithContext(ctx).Create(row).Error
|
||||
}
|
||||
Reference in New Issue
Block a user