117 lines
4.5 KiB
Go
117 lines
4.5 KiB
Go
// billing.go Day 57:计费确认(实施计划 5.3/A-5.2~A-5.4,基于 Day54 R1 预扣口径)。
|
|
// R1 语义=提交即实扣(Hold=Redis DECRBY/MySQL 行锁,提交链资金已出):
|
|
//
|
|
// 成功(2) → 无资金动作(「预扣=实扣」半边,零差额验收);
|
|
// 失败(3) → Refund(fee)(A-5.3 预扣金额退回,余额恢复);
|
|
// 未知(4) → Refund(fee)(A-5.4 冻结释放;UNKNOWN 回执/48h 补偿扫描同口径)。
|
|
//
|
|
// msg_id 幂等锚=detail「首到终态锁定」(consumer.finishDetail WHERE status=1
|
|
// RowsAffected=1 分支才调本Confirm)——重复回执/乱序重放走不到此处,
|
|
// 每条明细资金动作至多一次;本函数自身不承诺幂等(裸调两次=两次退款)。
|
|
// task 联动:回执终态覆写三计数(COUNT BY status 聚合覆写,幂等防双计——
|
|
// 下发侧 done 的 success=sent 是提交口径中间态,回执口径为最终事实)+
|
|
// 无在途兜底完成(拦截 5 不入三计数,FE intercepted_count 单列口径)。
|
|
package report
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"time"
|
|
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/pkg/logger"
|
|
"go-sms/internal/service/balance"
|
|
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Billing 计费确认器(bal=nil 跳过资金动作,计数联动仍生效——测试/未装配)。
|
|
type Billing struct {
|
|
db *gorm.DB
|
|
bal balance.Store
|
|
}
|
|
|
|
// NewBilling 构造。
|
|
func NewBilling(db *gorm.DB, bal balance.Store) *Billing {
|
|
return &Billing{db: db, bal: bal}
|
|
}
|
|
|
|
// Confirm detail 首次终态锁定后的资金动作+task 计数联动。
|
|
// 失败不向上抛:终态已锁定,Kafka 重试重放走不到计费分支,抛错只会无谓
|
|
// 重试→死信;两级 Store(Redis→MySQL 回退)均失败的窗口记 Error 日志,
|
|
// 资损方向=少退不少扣(平台安全),对账差额告警+人工补偿兜底(R1 评审口径)。
|
|
func (b *Billing) Confirm(ctx context.Context, detailID int64, status int8) {
|
|
var d entity.MessageDetail
|
|
if err := b.db.WithContext(ctx).First(&d, detailID).Error; err != nil {
|
|
logger.L().Error("billing_detail_load_failed",
|
|
zap.Int64("detail_id", detailID), zap.Error(err))
|
|
return
|
|
}
|
|
if (status == 3 || status == 4) && b.bal != nil {
|
|
fee := int64(d.Fee) // R1 整型条数口径(Fee=segments),零舍入
|
|
reason := "回执失败退款"
|
|
if status == 4 {
|
|
reason = "UNKNOWN冻结释放"
|
|
}
|
|
refID := strconv.FormatInt(detailID, 10) // 审计可追溯(幂等锚在终态锁定)
|
|
if err := b.bal.Refund(ctx, d.AccountID, fee, refID, reason); err != nil {
|
|
logger.L().Error("billing_refund_failed",
|
|
zap.Int64("detail_id", detailID), zap.Int64("account_id", d.AccountID),
|
|
zap.Int64("fee", fee), zap.Int8("status", status),
|
|
zap.String("ref_id", refID),
|
|
zap.String("direction", "少退不少扣,对账兜底"), zap.Error(err))
|
|
}
|
|
}
|
|
b.syncTask(ctx, d.TaskID)
|
|
}
|
|
|
|
// syncTask 回执终态计数覆写+无在途兜底完成(聚合覆写天然防重放双计)。
|
|
func (b *Billing) syncTask(ctx context.Context, taskID int64) {
|
|
type stAgg struct {
|
|
Status int8
|
|
N int64
|
|
}
|
|
var rows []stAgg
|
|
if err := b.db.WithContext(ctx).Model(&entity.MessageDetail{}).
|
|
Select("status, COUNT(*) AS n").
|
|
Where("task_id = ?", taskID).
|
|
Group("status").Scan(&rows).Error; err != nil {
|
|
logger.L().Error("billing_task_agg_failed", zap.Int64("task_id", taskID), zap.Error(err))
|
|
return
|
|
}
|
|
var s, f, u, inflight int64
|
|
for _, r := range rows {
|
|
switch r.Status {
|
|
case 2:
|
|
s += r.N
|
|
case 3:
|
|
f += r.N
|
|
case 4:
|
|
u += r.N
|
|
case 0, 1: // 待发/已提交=在途(审核拒绝任务停留 0,由审核域终态,不受此处完成)
|
|
inflight += r.N
|
|
}
|
|
}
|
|
if err := b.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
|
Where("id = ?", taskID).
|
|
Updates(map[string]any{
|
|
"success_count": s, "fail_count": f, "unknown_count": u,
|
|
}).Error; err != nil {
|
|
logger.L().Error("billing_task_counters_failed", zap.Int64("task_id", taskID), zap.Error(err))
|
|
return
|
|
}
|
|
if inflight == 0 {
|
|
// 无在途兜底完成:下发侧 done(status 1→2)为准,此处仅补写缺口;
|
|
// WHERE 含 status IN (0,1) 幂等(已 done 的任务不再翻写)。
|
|
res := b.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
|
Where("id = ? AND status IN (0, 1) AND finished_at IS NULL", taskID).
|
|
Updates(map[string]any{"status": 2, "finished_at": time.Now()})
|
|
if res.Error == nil && res.RowsAffected == 1 {
|
|
logger.L().Info("billing_task_finished_by_report", zap.Int64("task_id", taskID))
|
|
} else if res.Error != nil {
|
|
logger.L().Error("billing_task_finish_failed", zap.Int64("task_id", taskID), zap.Error(res.Error))
|
|
}
|
|
}
|
|
}
|