## 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)`。消除两个独立增量计算函数的不一致风险。