修复支付取消与成功统计
This commit is contained in:
@@ -2,7 +2,9 @@ package paymentconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
|
||||
@@ -61,6 +63,9 @@ func (r *Repository) List(ctx context.Context, query ListQuery) ([]ConfigDTO, in
|
||||
}
|
||||
dtos = append(dtos, dto)
|
||||
}
|
||||
if err := r.applySuccessfulPaymentStats(ctx, dtos); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return dtos, total, nil
|
||||
}
|
||||
@@ -78,6 +83,11 @@ func (r *Repository) FindByID(ctx context.Context, id uint64, includeSecret bool
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
statDTOs := []ConfigDTO{dto}
|
||||
if err := r.applySuccessfulPaymentStats(ctx, statDTOs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto = statDTOs[0]
|
||||
if includeSecret {
|
||||
if err := appendAuditLog(r.db.WithContext(ctx), actorID, "payment_config.view_secret", item.ID, meta, map[string]any{
|
||||
"name": item.Name,
|
||||
@@ -90,6 +100,103 @@ func (r *Repository) FindByID(ctx context.Context, id uint64, includeSecret bool
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
type paymentConfigSuccessStat struct {
|
||||
ConfigID uint64
|
||||
TotalTransactions int64
|
||||
TotalAmountCent int64
|
||||
LastPaidAt nullableTime
|
||||
}
|
||||
|
||||
// applySuccessfulPaymentStats 使用真实成功支付流水覆盖配置统计,避免仅打开二维码也被算作成交。
|
||||
func (r *Repository) applySuccessfulPaymentStats(ctx context.Context, dtos []ConfigDTO) error {
|
||||
if len(dtos) == 0 {
|
||||
return nil
|
||||
}
|
||||
ids := make([]uint64, 0, len(dtos))
|
||||
indexByID := make(map[uint64]int, len(dtos))
|
||||
for idx, dto := range dtos {
|
||||
ids = append(ids, dto.ID)
|
||||
indexByID[dto.ID] = idx
|
||||
dtos[idx].TotalTransactions = 0
|
||||
dtos[idx].TotalAmountCent = 0
|
||||
dtos[idx].LastUsedAt = nil
|
||||
}
|
||||
|
||||
var rows []paymentConfigSuccessStat
|
||||
if err := r.db.WithContext(ctx).Model(&model.PaymentOrder{}).
|
||||
Select("payment_config_id AS config_id, COUNT(*) AS total_transactions, COALESCE(SUM(amount_cent), 0) AS total_amount_cent, MAX(paid_at) AS last_paid_at").
|
||||
Where("payment_config_id IN ? AND biz_type = ? AND status = ?", ids, "order_pay", "paid").
|
||||
Group("payment_config_id").
|
||||
Scan(&rows).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, row := range rows {
|
||||
idx, ok := indexByID[row.ConfigID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
dtos[idx].TotalTransactions = row.TotalTransactions
|
||||
dtos[idx].TotalAmountCent = row.TotalAmountCent
|
||||
if row.LastPaidAt.Time != nil {
|
||||
value := row.LastPaidAt.Time.Format(time.RFC3339)
|
||||
dtos[idx].LastUsedAt = &value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type nullableTime struct {
|
||||
Time *time.Time
|
||||
}
|
||||
|
||||
// Value 实现 driver.Valuer,让 GORM 能把 nullableTime 当作普通扫描字段处理。
|
||||
func (nt nullableTime) Value() (driver.Value, error) {
|
||||
if nt.Time == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return *nt.Time, nil
|
||||
}
|
||||
|
||||
// Scan 兼容 MySQL 的 time.Time 和 SQLite 聚合函数返回的字符串时间。
|
||||
func (nt *nullableTime) Scan(value any) error {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
nt.Time = nil
|
||||
case time.Time:
|
||||
nt.Time = &v
|
||||
case []byte:
|
||||
return nt.scanString(string(v))
|
||||
case string:
|
||||
return nt.scanString(v)
|
||||
default:
|
||||
nt.Time = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nt *nullableTime) scanString(value string) error {
|
||||
if value == "" {
|
||||
nt.Time = nil
|
||||
return nil
|
||||
}
|
||||
for _, layout := range []string{
|
||||
time.RFC3339Nano,
|
||||
time.RFC3339,
|
||||
"2006-01-02 15:04:05.999999999-07:00",
|
||||
"2006-01-02 15:04:05-07:00",
|
||||
"2006-01-02 15:04:05.999999999",
|
||||
"2006-01-02 15:04:05",
|
||||
} {
|
||||
parsed, err := time.Parse(layout, value)
|
||||
if err == nil {
|
||||
nt.Time = &parsed
|
||||
return nil
|
||||
}
|
||||
}
|
||||
nt.Time = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindRuntimeByID 根据配置 ID 查询运行时配置,不写审计日志,供支付单回溯原配置使用。
|
||||
func (r *Repository) FindRuntimeByID(ctx context.Context, id uint64, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
|
||||
Reference in New Issue
Block a user