Files
junhong_cmp_fiber/openspec/specs/iot-card/spec.md
huang cebcada950
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m16s
refactor: 流量系统重构 — 增量累加算法 + 日粒度缓冲 + 旧详单清理
核心改造:
- 增量算法:流量计算从覆盖式改为增量累加(gateway - lastReading),支持上游运营商重置检测
- 日流量缓冲:insertDataUsageRecord 改为 Redis INCRBYFLOAT,每日凌晨落盘到 tb_card_daily_usage
- 运营商:新增 data_reset_day 字段(联通=27,其余=1)
- IoT卡:新增 last_gateway_reading_mb 字段存储上次网关读数
- 查询层:新建 TrafficQueryService 合并 Redis(今日)+ DB(历史)数据源
- 清理:删除 DataUsageRecord model/store,移除 polling_handler 旧引用

迁移:000094-000097(carrier字段、iot_card字段、数据初始化、日流量表)
2026-03-30 09:59:30 +08:00

38 lines
2.2 KiB
Markdown
Raw 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.
## MODIFIED Requirements
### Requirement: IoT 卡网关读数记录
`tb_iot_card` SHALL 包含 `last_gateway_reading_mb` 字段FLOAT, 默认 0记录上次轮询时网关返回的流量读数用于计算增量。
#### Scenario: 轮询更新网关读数
- **WHEN** 轮询系统获取到网关流量值
- **THEN** 系统 SHALL 将 `last_gateway_reading_mb` 更新为本次网关返回值(无论增量是否 > 0
### Requirement: 月流量增量累加
`current_month_usage_mb` SHALL 使用增量累加(`+= increment`)而非直接覆盖(`= gatewayValue`)。增量 = 当前网关读数 - 上次网关读数(`last_gateway_reading_mb`)。
#### Scenario: 正常流量增长
- **WHEN** 上次读数 100MB本次读数 105MB
- **THEN** `current_month_usage_mb` SHALL 增加 5MB而非被覆盖为 105MB
- **AND** `data_usage_mb`卡生命周期总用量SHALL 同步增加 5MB
#### Scenario: 上游自然月重置
- **WHEN** 上次读数 500MB本次读数 10MB且在运营商重置日窗口内
- **THEN** `increment` SHALL 为 10MB本次原始值即为增量`current_month_usage_mb += 10`
#### Scenario: 非重置日异常下降
- **WHEN** 上次读数 500MB本次读数 10MB但不在重置日窗口内
- **THEN** `increment` SHALL 为 0记录 Warn 日志,`current_month_usage_mb` 不变
### Requirement: 跨自然月重置
当检测到系统跨自然月时,`current_month_usage_mb` SHALL 重置为 0不再等于 `gatewayFlowMB``last_month_total_mb` SHALL 记录上月累计值。
#### Scenario: 跨月轮询
- **WHEN** 上次轮询在 3 月,本次轮询在 4 月
- **THEN** `last_month_total_mb` = 原 `current_month_usage_mb``current_month_usage_mb` = 0`current_month_start_date` 更新为本月 1 日
### Requirement: 新卡首次轮询
新入库的卡 `last_gateway_reading_mb` 默认为 0首次轮询的增量 = 网关返回的全量值。这是预期行为。批量导入已有使用量的卡时,导入脚本应同步设置 `last_gateway_reading_mb`
### Requirement: 增量函数合并
`calculateFlowUpdates()``calculateFlowIncrement()` SHALL 合并为一个函数,返回 `(updates map[string]any, increment float64)`。消除两个独立增量计算函数的不一致风险。