181 lines
6.8 KiB
Go
181 lines
6.8 KiB
Go
// consume.go S2-D2:消耗日汇总引擎——结算域事实源(T+1 思路的滚动重刷修正版)。
|
|
//
|
|
// 模型(评审 docs/review/s2-settle-table-review.md §三 + D2 修正):
|
|
// - 明细热窗仅 5 天,月结绝不能扫明细——billing_consume_log(账号×业务×天)
|
|
// 与 stats_channel_daily(通道×天)是结算域唯一消耗事实源(R2);
|
|
// - 72h 回执窗内明细状态会迁移(UNKNOWN→成功/失败,Day69/71),昨日行非
|
|
// 终值——单日一次性落账会失真,改为周期滚动重刷近 N 天幂等 UPSERT 收敛;
|
|
// - 口径:submit=COUNT(*) 全部;success/fail/unknown=SUM(status=N);
|
|
// fee_total=Σfee(条,Day70 片数口径);cost_total/est_balance 不写
|
|
// (R1:金额出账时刻推导,单一事实源纪律)。
|
|
// - 边界:某账号当日明细为 0 则不产生行(热窗内明细不删,无脏残留场景);
|
|
// 数据清退(S4 分区)后的历史行冻结,重刷窗不覆盖即可。
|
|
package settle
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
|
|
"go-sms/internal/model/entity"
|
|
)
|
|
|
|
// Service 结算域服务(D2 消耗日汇总;D3~D5 逐步扩展充值审核/出账/利润/对账)。
|
|
type Service struct{ db *gorm.DB }
|
|
|
|
// NewService 构造(仅 db 依赖,照 account.Service 极简口径)。
|
|
func NewService(db *gorm.DB) *Service { return &Service{db: db} }
|
|
|
|
// accountAggRow 账号维度聚合扫描 DTO(COUNT/SUM 扫 int64 再转实体 int)。
|
|
type accountAggRow struct {
|
|
AccountID int64
|
|
Business string
|
|
SubmitCount int64
|
|
SuccessCount int64
|
|
FailCount int64
|
|
UnknownCount int64
|
|
FeeTotal float64
|
|
}
|
|
|
|
// channelAggRow 通道维度聚合扫描 DTO(无 Σfee——R1 出账按条数×价推导,
|
|
// 通道侧无需费用列,cost_total 保持 0)。
|
|
type channelAggRow struct {
|
|
ChannelID int64
|
|
SubmitCount int64
|
|
SuccessCount int64
|
|
FailCount int64
|
|
UnknownCount int64
|
|
}
|
|
|
|
// AggregateDay 单日消耗汇总(幂等 UPSERT,滚动重刷安全):
|
|
// 账号×业务 → billing_consume_log(uk account_id+business+stat_date 覆盖更新);
|
|
// 通道 → stats_channel_daily(uk channel_id+stat_date)。
|
|
// [lo,hi) 半开时间窗规避 date() 方言差;SUM(status=N) MySQL/SQLite 双方言
|
|
// 布尔求和(stats_handler 同款)。返回两表落账行数(供指标/手动回补响应)。
|
|
func (s *Service) AggregateDay(ctx context.Context, day time.Time) (accountRows, channelRows int64, err error) {
|
|
lo := time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, day.Location())
|
|
hi := lo.AddDate(0, 0, 1)
|
|
|
|
// ① 账号×业务维度(GROUP BY 聚合一条 SQL;日汇总 job 一天数次,
|
|
// 非请求路径——性能三律约束的是业务查询页,此处窗口扫描可接受,
|
|
// S5 压测后如需再优化为覆盖索引三 COUNT)。
|
|
var accountAggs []accountAggRow
|
|
if err = s.db.WithContext(ctx).Table("message_detail").
|
|
Select("account_id, business, COUNT(*) AS submit_count, "+
|
|
"COALESCE(SUM(status = 2), 0) AS success_count, "+
|
|
"COALESCE(SUM(status = 3), 0) AS fail_count, "+
|
|
"COALESCE(SUM(status = 4), 0) AS unknown_count, "+
|
|
"COALESCE(SUM(fee), 0) AS fee_total").
|
|
Where("created_at >= ? AND created_at < ?", lo, hi).
|
|
Group("account_id, business").
|
|
Scan(&accountAggs).Error; err != nil {
|
|
return 0, 0, err
|
|
}
|
|
for _, r := range accountAggs {
|
|
row := entity.BillingConsumeLog{
|
|
AccountID: r.AccountID,
|
|
Business: r.Business,
|
|
StatDate: lo,
|
|
SubmitCount: int(r.SubmitCount),
|
|
SuccessCount: int(r.SuccessCount),
|
|
FailCount: int(r.FailCount),
|
|
UnknownCount: int(r.UnknownCount),
|
|
FeeTotal: r.FeeTotal,
|
|
}
|
|
if err = s.db.WithContext(ctx).Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{
|
|
{Name: "account_id"}, {Name: "business"}, {Name: "stat_date"},
|
|
},
|
|
DoUpdates: clause.AssignmentColumns([]string{
|
|
"submit_count", "success_count", "fail_count",
|
|
"unknown_count", "fee_total", "updated_at",
|
|
}),
|
|
}).Create(&row).Error; err != nil {
|
|
return accountRows, channelRows, err
|
|
}
|
|
accountRows++
|
|
}
|
|
|
|
// ② 通道维度(grain=通道×天,不加 business——通道价格体系不分业务,R2 评审;
|
|
// 不聚合 Σfee——上家账单按 billing_mode 条数口径,cost_total 保持 0,R1)。
|
|
var channelAggs []channelAggRow
|
|
if err = s.db.WithContext(ctx).Table("message_detail").
|
|
Select("channel_id, COUNT(*) AS submit_count, "+
|
|
"COALESCE(SUM(status = 2), 0) AS success_count, "+
|
|
"COALESCE(SUM(status = 3), 0) AS fail_count, "+
|
|
"COALESCE(SUM(status = 4), 0) AS unknown_count").
|
|
Where("created_at >= ? AND created_at < ?", lo, hi).
|
|
Group("channel_id").
|
|
Scan(&channelAggs).Error; err != nil {
|
|
return accountRows, channelRows, err
|
|
}
|
|
for _, r := range channelAggs {
|
|
row := entity.StatsChannelDaily{
|
|
ChannelID: r.ChannelID,
|
|
StatDate: lo,
|
|
SubmitCount: int(r.SubmitCount),
|
|
SuccessCount: int(r.SuccessCount),
|
|
FailCount: int(r.FailCount),
|
|
UnknownCount: int(r.UnknownCount),
|
|
}
|
|
if err = s.db.WithContext(ctx).Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "channel_id"}, {Name: "stat_date"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{
|
|
"submit_count", "success_count", "fail_count",
|
|
"unknown_count", "updated_at",
|
|
}),
|
|
}).Create(&row).Error; err != nil {
|
|
return accountRows, channelRows, err
|
|
}
|
|
channelRows++
|
|
}
|
|
return accountRows, channelRows, nil
|
|
}
|
|
|
|
// ConsumeQuery 消耗日汇总查询条件(To 按天闭区间,service 内转半开窗)。
|
|
type ConsumeQuery struct {
|
|
AccountID int64 // 0=全部;customer scope 由 handler 强制注入
|
|
Business string // ""=全部
|
|
From time.Time // 闭区间起(零值=不限)
|
|
To time.Time // 闭区间止(零值=不限)
|
|
Page int // 1 起
|
|
PageSize int // 默认 20,上限 100(handler 兜底)
|
|
}
|
|
|
|
// ListConsume 分页查询消耗日汇总(按 stat_date 倒序,新数据在前)。
|
|
func (s *Service) ListConsume(ctx context.Context, q ConsumeQuery) ([]entity.BillingConsumeLog, int64, error) {
|
|
db := s.db.WithContext(ctx).Model(&entity.BillingConsumeLog{})
|
|
if q.AccountID > 0 {
|
|
db = db.Where("account_id = ?", q.AccountID)
|
|
}
|
|
if q.Business != "" {
|
|
db = db.Where("business = ?", q.Business)
|
|
}
|
|
if !q.From.IsZero() {
|
|
// 按天归一(stat_date 恒为当日零点,带时刻参数会把今天的行误滤)。
|
|
f := time.Date(q.From.Year(), q.From.Month(), q.From.Day(), 0, 0, 0, 0, q.From.Location())
|
|
db = db.Where("stat_date >= ?", f)
|
|
}
|
|
if !q.To.IsZero() {
|
|
t := time.Date(q.To.Year(), q.To.Month(), q.To.Day(), 0, 0, 0, 0, q.To.Location())
|
|
db = db.Where("stat_date < ?", t.AddDate(0, 0, 1)) // 闭区间转半开(按天)
|
|
}
|
|
var total int64
|
|
if err := db.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if q.Page <= 0 {
|
|
q.Page = 1
|
|
}
|
|
if q.PageSize <= 0 || q.PageSize > 100 {
|
|
q.PageSize = 20
|
|
}
|
|
var list []entity.BillingConsumeLog
|
|
err := db.Order("stat_date DESC, account_id, business").
|
|
Limit(q.PageSize).Offset((q.Page - 1) * q.PageSize).
|
|
Find(&list).Error
|
|
return list, total, err
|
|
}
|