完成剩余模块 Context 超时控制改造

This commit is contained in:
yml2213
2026-06-10 12:58:59 +08:00
parent d2858c529d
commit 6ae8f0e830
24 changed files with 370 additions and 323 deletions
@@ -1,6 +1,7 @@
package paymentaccount
import (
"context"
"encoding/json"
"errors"
"strings"
@@ -22,9 +23,10 @@ func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
func (r *Repository) List(userID uint64, page, pageSize int) (*PaginatedResult, error) {
func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
db := r.db.WithContext(ctx)
var total int64
if err := r.db.Model(&model.UserPaymentAccount{}).
if err := db.Model(&model.UserPaymentAccount{}).
Where("user_id = ? AND status = ?", userID, "active").
Count(&total).Error; err != nil {
return nil, err
@@ -32,7 +34,7 @@ func (r *Repository) List(userID uint64, page, pageSize int) (*PaginatedResult,
offset := (page - 1) * pageSize
var accounts []model.UserPaymentAccount
if err := r.db.Where("user_id = ? AND status = ?", userID, "active").
if err := db.Where("user_id = ? AND status = ?", userID, "active").
Order("is_default DESC, created_at DESC").
Offset(offset).Limit(pageSize).
Find(&accounts).Error; err != nil {
@@ -56,9 +58,9 @@ func (r *Repository) List(userID uint64, page, pageSize int) (*PaginatedResult,
}, nil
}
func (r *Repository) FindByID(userID, id uint64) (*PaymentAccountDTO, error) {
func (r *Repository) FindByID(ctx context.Context, userID, id uint64) (*PaymentAccountDTO, error) {
var account model.UserPaymentAccount
if err := r.db.Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
if err := r.db.WithContext(ctx).Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrAccountNotFound
@@ -68,7 +70,8 @@ func (r *Repository) FindByID(userID, id uint64) (*PaymentAccountDTO, error) {
return r.toDTO(account)
}
func (r *Repository) Create(userID uint64, req CreatePaymentAccountRequest) (*PaymentAccountDTO, error) {
func (r *Repository) Create(ctx context.Context, userID uint64, req CreatePaymentAccountRequest) (*PaymentAccountDTO, error) {
db := r.db.WithContext(ctx)
// 加密账号
encryptedNo, err := crypto.Encrypt(req.AccountNo)
if err != nil {
@@ -84,7 +87,7 @@ func (r *Repository) Create(userID uint64, req CreatePaymentAccountRequest) (*Pa
// 如果是第一个账号,自动设为默认
isDefault := false
var count int64
r.db.Model(&model.UserPaymentAccount{}).Where("user_id = ? AND status = ?", userID, "active").Count(&count)
db.Model(&model.UserPaymentAccount{}).Where("user_id = ? AND status = ?", userID, "active").Count(&count)
if count == 0 {
isDefault = true
}
@@ -101,16 +104,17 @@ func (r *Repository) Create(userID uint64, req CreatePaymentAccountRequest) (*Pa
Status: "active",
}
if err := r.db.Create(&account).Error; err != nil {
if err := db.Create(&account).Error; err != nil {
return nil, err
}
return r.FindByID(userID, account.ID)
return r.FindByID(ctx, userID, account.ID)
}
func (r *Repository) Update(userID, id uint64, req UpdatePaymentAccountRequest) (*PaymentAccountDTO, error) {
func (r *Repository) Update(ctx context.Context, userID, id uint64, req UpdatePaymentAccountRequest) (*PaymentAccountDTO, error) {
db := r.db.WithContext(ctx)
var account model.UserPaymentAccount
if err := r.db.Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
if err := db.Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrAccountNotFound
@@ -131,24 +135,25 @@ func (r *Repository) Update(userID, id uint64, req UpdatePaymentAccountRequest)
if req.IsDefault != nil && *req.IsDefault {
// 先取消其他默认账号
r.db.Model(&model.UserPaymentAccount{}).
db.Model(&model.UserPaymentAccount{}).
Where("user_id = ? AND id != ?", userID, id).
Update("is_default", false)
updates["is_default"] = true
}
if len(updates) > 0 {
if err := r.db.Model(&account).Updates(updates).Error; err != nil {
if err := db.Model(&account).Updates(updates).Error; err != nil {
return nil, err
}
}
return r.FindByID(userID, id)
return r.FindByID(ctx, userID, id)
}
func (r *Repository) Delete(userID, id uint64) error {
func (r *Repository) Delete(ctx context.Context, userID, id uint64) error {
db := r.db.WithContext(ctx)
var account model.UserPaymentAccount
if err := r.db.Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
if err := db.Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ErrAccountNotFound
@@ -157,13 +162,13 @@ func (r *Repository) Delete(userID, id uint64) error {
}
// 软删除
return r.db.Model(&account).Update("status", "disabled").Error
return db.Model(&account).Update("status", "disabled").Error
}
func (r *Repository) SetDefault(userID, id uint64) error {
func (r *Repository) SetDefault(ctx context.Context, userID, id uint64) error {
// 验证账号存在
var account model.UserPaymentAccount
if err := r.db.Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
if err := r.db.WithContext(ctx).Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ErrAccountNotFound
@@ -171,7 +176,7 @@ func (r *Repository) SetDefault(userID, id uint64) error {
return err
}
return r.db.Transaction(func(tx *gorm.DB) error {
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// 取消其他默认账号
if err := tx.Model(&model.UserPaymentAccount{}).
Where("user_id = ? AND id != ?", userID, id).
@@ -183,17 +188,18 @@ func (r *Repository) SetDefault(userID, id uint64) error {
})
}
func (r *Repository) CountByUser(userID uint64) (int64, error) {
func (r *Repository) CountByUser(ctx context.Context, userID uint64) (int64, error) {
var count int64
err := r.db.Model(&model.UserPaymentAccount{}).
err := r.db.WithContext(ctx).Model(&model.UserPaymentAccount{}).
Where("user_id = ? AND status = ?", userID, "active").
Count(&count).Error
return count, err
}
func (r *Repository) ValidateRealname(userID uint64, accountName string) error {
func (r *Repository) ValidateRealname(ctx context.Context, userID uint64, accountName string) error {
var user model.User
if err := r.db.First(&user, userID).Error; err != nil {
db := r.db.WithContext(ctx)
if err := db.First(&user, userID).Error; err != nil {
return err
}
@@ -203,7 +209,7 @@ func (r *Repository) ValidateRealname(userID uint64, accountName string) error {
// 获取实名信息
var realname model.UserRealname
if err := r.db.Where("user_id = ? AND status = ?", userID, "verified").
if err := db.Where("user_id = ? AND status = ?", userID, "verified").
First(&realname).Error; err != nil {
return ErrRealnameRequired
}
@@ -303,9 +309,9 @@ func maskAccountNo(accountNo, accountType string) string {
}
// 获取解密后的账号(仅供内部使用,如提现申请时)
func (r *Repository) GetDecryptedAccountNo(userID, id uint64) (string, error) {
func (r *Repository) GetDecryptedAccountNo(ctx context.Context, userID, id uint64) (string, error) {
var account model.UserPaymentAccount
if err := r.db.Where("id = ? AND user_id = ?", id, userID).First(&account).Error; err != nil {
if err := r.db.WithContext(ctx).Where("id = ? AND user_id = ?", id, userID).First(&account).Error; err != nil {
return "", err
}
return crypto.Decrypt(account.AccountNo)