优化支付日志和配置删除提示
This commit is contained in:
@@ -46,6 +46,7 @@ type ConfigDTO struct {
|
||||
Status string `json:"status"`
|
||||
Environment string `json:"environment"`
|
||||
BusinessTags []string `json:"business_tags"`
|
||||
ReferenceCount int64 `json:"reference_count"`
|
||||
TotalTransactions int64 `json:"total_transactions"`
|
||||
TotalAmountCent int64 `json:"total_amount_cent"`
|
||||
LastUsedAt *string `json:"last_used_at"`
|
||||
|
||||
@@ -66,6 +66,9 @@ func (r *Repository) List(ctx context.Context, query ListQuery) ([]ConfigDTO, in
|
||||
if err := r.applySuccessfulPaymentStats(ctx, dtos); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if err := r.applyReferenceCounts(ctx, dtos); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return dtos, total, nil
|
||||
}
|
||||
@@ -87,6 +90,9 @@ func (r *Repository) FindByID(ctx context.Context, id uint64, includeSecret bool
|
||||
if err := r.applySuccessfulPaymentStats(ctx, statDTOs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.applyReferenceCounts(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{
|
||||
@@ -145,6 +151,29 @@ func (r *Repository) applySuccessfulPaymentStats(ctx context.Context, dtos []Con
|
||||
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
|
||||
}
|
||||
|
||||
@@ -121,6 +121,20 @@ func TestListUsesSuccessfulPaymentStats(t *testing.T) {
|
||||
BizType: "order_pay",
|
||||
Status: "closed",
|
||||
},
|
||||
{
|
||||
PaymentNo: "PAY-LEGACY",
|
||||
OrderID: 3,
|
||||
OrderNo: "ORD-LEGACY",
|
||||
UserID: 2,
|
||||
PaymentConfigID: 0,
|
||||
Provider: "lakala",
|
||||
MerchantID: "M1",
|
||||
ThirdOrderID: "PAY-LEGACY",
|
||||
PayWay: "WXZF",
|
||||
AmountCent: 10130,
|
||||
BizType: "order_pay",
|
||||
Status: "failed",
|
||||
},
|
||||
}
|
||||
if err := db.Create(&payments).Error; err != nil {
|
||||
t.Fatalf("create payments failed: %v", err)
|
||||
@@ -137,6 +151,9 @@ func TestListUsesSuccessfulPaymentStats(t *testing.T) {
|
||||
if items[0].TotalTransactions != 1 || items[0].TotalAmountCent != 10130 {
|
||||
t.Fatalf("success stats = %d/%d, want 1/10130", items[0].TotalTransactions, items[0].TotalAmountCent)
|
||||
}
|
||||
if items[0].ReferenceCount != 3 {
|
||||
t.Fatalf("reference_count = %d, want 3", items[0].ReferenceCount)
|
||||
}
|
||||
if items[0].LastUsedAt == nil {
|
||||
t.Fatal("last success pay time should not be nil")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user