完成剩余模块 Context 超时控制改造
This commit is contained in:
@@ -25,7 +25,7 @@ func (h *Handler) List(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
|
||||
result, err := h.service.List(userID, page, pageSize)
|
||||
result, err := h.service.List(c.Request.Context(), userID, page, pageSize)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
@@ -46,7 +46,7 @@ func (h *Handler) FindByID(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
account, err := h.service.FindByID(userID, id)
|
||||
account, err := h.service.FindByID(c.Request.Context(), userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
@@ -68,7 +68,7 @@ func (h *Handler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
account, err := h.service.Create(userID, req)
|
||||
account, err := h.service.Create(c.Request.Context(), userID, req)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
@@ -95,7 +95,7 @@ func (h *Handler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
account, err := h.service.Update(userID, id, req)
|
||||
account, err := h.service.Update(c.Request.Context(), userID, id, req)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
@@ -116,7 +116,7 @@ func (h *Handler) Delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.Delete(userID, id); err != nil {
|
||||
if err := h.service.Delete(c.Request.Context(), userID, id); err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
@@ -136,7 +136,7 @@ func (h *Handler) SetDefault(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.SetDefault(userID, id); err != nil {
|
||||
if err := h.service.SetDefault(c.Request.Context(), userID, id); err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package paymentaccount
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
@@ -19,7 +22,7 @@ func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) List(userID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
func (s *Service) List(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -29,52 +32,52 @@ func (s *Service) List(userID uint64, page, pageSize int) (*PaginatedResult, err
|
||||
if pageSize < 1 || pageSize > 100 {
|
||||
pageSize = 20
|
||||
}
|
||||
return s.repo.List(userID, page, pageSize)
|
||||
return s.repo.List(ctx, userID, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *Service) FindByID(userID, id uint64) (*PaymentAccountDTO, error) {
|
||||
func (s *Service) FindByID(ctx context.Context, userID, id uint64) (*PaymentAccountDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.FindByID(userID, id)
|
||||
return s.repo.FindByID(ctx, userID, id)
|
||||
}
|
||||
|
||||
func (s *Service) Create(userID uint64, req CreatePaymentAccountRequest) (*PaymentAccountDTO, error) {
|
||||
func (s *Service) Create(ctx context.Context, userID uint64, req CreatePaymentAccountRequest) (*PaymentAccountDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
// 验证实名状态
|
||||
if err := s.repo.ValidateRealname(userID, req.AccountName); err != nil {
|
||||
if err := s.repo.ValidateRealname(ctx, userID, req.AccountName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 检查账号数量限制(最多5个)
|
||||
count, err := s.repo.CountByUser(userID)
|
||||
count, err := s.repo.CountByUser(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count >= 5 {
|
||||
return nil, ErrAccountLimit
|
||||
}
|
||||
return s.repo.Create(userID, req)
|
||||
return s.repo.Create(ctx, userID, req)
|
||||
}
|
||||
|
||||
func (s *Service) Update(userID, id uint64, req UpdatePaymentAccountRequest) (*PaymentAccountDTO, error) {
|
||||
func (s *Service) Update(ctx context.Context, userID, id uint64, req UpdatePaymentAccountRequest) (*PaymentAccountDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.Update(userID, id, req)
|
||||
return s.repo.Update(ctx, userID, id, req)
|
||||
}
|
||||
|
||||
func (s *Service) Delete(userID, id uint64) error {
|
||||
func (s *Service) Delete(ctx context.Context, userID, id uint64) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.Delete(userID, id)
|
||||
return s.repo.Delete(ctx, userID, id)
|
||||
}
|
||||
|
||||
func (s *Service) SetDefault(userID, id uint64) error {
|
||||
func (s *Service) SetDefault(ctx context.Context, userID, id uint64) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.SetDefault(userID, id)
|
||||
return s.repo.SetDefault(ctx, userID, id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user