feat(admin): 优化商品与支付配置列表
This commit is contained in:
@@ -2,9 +2,7 @@ package paymentconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
|
||||
@@ -63,10 +61,8 @@ 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
|
||||
}
|
||||
if err := r.applyReferenceCounts(ctx, dtos); err != nil {
|
||||
|
||||
if err := r.applyPaymentReferenceFlags(ctx, dtos); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
@@ -86,14 +82,12 @@ 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 {
|
||||
|
||||
dtos := []ConfigDTO{dto}
|
||||
if err := r.applyPaymentReferenceFlags(ctx, dtos); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.applyReferenceCounts(ctx, statDTOs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto = statDTOs[0]
|
||||
dto = dtos[0]
|
||||
if includeSecret {
|
||||
if err := appendAuditLog(r.db.WithContext(ctx), actorID, "payment_config.view_secret", item.ID, meta, map[string]any{
|
||||
"name": item.Name,
|
||||
@@ -106,15 +100,9 @@ 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 {
|
||||
// applyPaymentReferenceFlags 批量标记配置是否已有支付单,用于前端提前禁用删除。
|
||||
// 真正的删除保护仍由 Delete 按包含历史数据的口径校验。
|
||||
func (r *Repository) applyPaymentReferenceFlags(ctx context.Context, dtos []ConfigDTO) error {
|
||||
if len(dtos) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -123,109 +111,26 @@ func (r *Repository) applySuccessfulPaymentStats(ctx context.Context, dtos []Con
|
||||
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
|
||||
dtos[idx].HasPaymentRecords = false
|
||||
}
|
||||
|
||||
var rows []paymentConfigSuccessStat
|
||||
var referencedIDs []uint64
|
||||
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 {
|
||||
Where("payment_config_id IN ?", ids).
|
||||
Distinct().
|
||||
Pluck("payment_config_id", &referencedIDs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, row := range rows {
|
||||
idx, ok := indexByID[row.ConfigID]
|
||||
for _, id := range referencedIDs {
|
||||
idx, ok := indexByID[id]
|
||||
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
|
||||
}
|
||||
dtos[idx].HasPaymentRecords = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// applyReferenceCounts 使用删除保护的同一口径统计配置被支付记录引用的次数。
|
||||
func (r *Repository) applyReferenceCounts(ctx context.Context, dtos []ConfigDTO) error {
|
||||
for idx := range dtos {
|
||||
count, err := r.referenceCount(ctx, dtos[idx])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dtos[idx].ReferenceCount = count
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) referenceCount(ctx context.Context, dto ConfigDTO) (int64, error) {
|
||||
var count int64
|
||||
db := r.db.WithContext(ctx).Model(&model.PaymentOrder{}).
|
||||
Where("payment_config_id = ?", dto.ID).
|
||||
Or("(payment_config_id = ? OR payment_config_id IS NULL) AND provider = ? AND merchant_id = ? AND pay_way = ?", 0, dto.Provider, dto.MerchantID, dto.PayWay)
|
||||
if err := db.Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, 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 与字符串时间。
|
||||
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