temp
This commit is contained in:
46
internal/domains/finance/repositories/finance_repository.go
Normal file
46
internal/domains/finance/repositories/finance_repository.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tyapi-server/internal/domains/finance/entities"
|
||||
"tyapi-server/internal/shared/interfaces"
|
||||
)
|
||||
|
||||
// WalletRepository 钱包仓储接口
|
||||
type WalletRepository interface {
|
||||
interfaces.Repository[entities.Wallet]
|
||||
|
||||
// 钱包管理
|
||||
FindByUserID(ctx context.Context, userID string) (*entities.Wallet, error)
|
||||
ExistsByUserID(ctx context.Context, userID string) (bool, error)
|
||||
|
||||
// 余额操作
|
||||
UpdateBalance(ctx context.Context, userID string, balance interface{}) error
|
||||
AddBalance(ctx context.Context, userID string, amount interface{}) error
|
||||
SubtractBalance(ctx context.Context, userID string, amount interface{}) error
|
||||
|
||||
// 统计查询
|
||||
GetTotalBalance(ctx context.Context) (interface{}, error)
|
||||
GetActiveWalletCount(ctx context.Context) (int64, error)
|
||||
}
|
||||
|
||||
// UserSecretsRepository 用户密钥仓储接口
|
||||
type UserSecretsRepository interface {
|
||||
interfaces.Repository[entities.UserSecrets]
|
||||
|
||||
// 密钥管理
|
||||
FindByUserID(ctx context.Context, userID string) (*entities.UserSecrets, error)
|
||||
FindByAccessID(ctx context.Context, accessID string) (*entities.UserSecrets, error)
|
||||
ExistsByUserID(ctx context.Context, userID string) (bool, error)
|
||||
ExistsByAccessID(ctx context.Context, accessID string) (bool, error)
|
||||
|
||||
// 密钥操作
|
||||
UpdateLastUsedAt(ctx context.Context, accessID string) error
|
||||
DeactivateByUserID(ctx context.Context, userID string) error
|
||||
RegenerateAccessKey(ctx context.Context, userID string, accessID, accessKey string) error
|
||||
|
||||
// 过期密钥清理
|
||||
GetExpiredSecrets(ctx context.Context) ([]entities.UserSecrets, error)
|
||||
DeleteExpiredSecrets(ctx context.Context) error
|
||||
}
|
||||
410
internal/domains/finance/repositories/gorm_finance_repository.go
Normal file
410
internal/domains/finance/repositories/gorm_finance_repository.go
Normal file
@@ -0,0 +1,410 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"tyapi-server/internal/domains/finance/entities"
|
||||
"tyapi-server/internal/shared/interfaces"
|
||||
)
|
||||
|
||||
// GormWalletRepository 钱包GORM仓储实现
|
||||
type GormWalletRepository struct {
|
||||
db *gorm.DB
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewGormWalletRepository 创建钱包GORM仓储
|
||||
func NewGormWalletRepository(db *gorm.DB, logger *zap.Logger) *GormWalletRepository {
|
||||
return &GormWalletRepository{
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建钱包
|
||||
func (r *GormWalletRepository) Create(ctx context.Context, wallet entities.Wallet) error {
|
||||
r.logger.Info("创建钱包", zap.String("user_id", wallet.UserID))
|
||||
return r.db.WithContext(ctx).Create(&wallet).Error
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取钱包
|
||||
func (r *GormWalletRepository) GetByID(ctx context.Context, id string) (entities.Wallet, error) {
|
||||
var wallet entities.Wallet
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&wallet).Error
|
||||
return wallet, err
|
||||
}
|
||||
|
||||
// Update 更新钱包
|
||||
func (r *GormWalletRepository) Update(ctx context.Context, wallet entities.Wallet) error {
|
||||
r.logger.Info("更新钱包", zap.String("id", wallet.ID))
|
||||
return r.db.WithContext(ctx).Save(&wallet).Error
|
||||
}
|
||||
|
||||
// Delete 删除钱包
|
||||
func (r *GormWalletRepository) Delete(ctx context.Context, id string) error {
|
||||
r.logger.Info("删除钱包", zap.String("id", id))
|
||||
return r.db.WithContext(ctx).Delete(&entities.Wallet{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
// SoftDelete 软删除钱包
|
||||
func (r *GormWalletRepository) SoftDelete(ctx context.Context, id string) error {
|
||||
r.logger.Info("软删除钱包", zap.String("id", id))
|
||||
return r.db.WithContext(ctx).Delete(&entities.Wallet{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
// Restore 恢复钱包
|
||||
func (r *GormWalletRepository) Restore(ctx context.Context, id string) error {
|
||||
r.logger.Info("恢复钱包", zap.String("id", id))
|
||||
return r.db.WithContext(ctx).Unscoped().Model(&entities.Wallet{}).Where("id = ?", id).Update("deleted_at", nil).Error
|
||||
}
|
||||
|
||||
// Count 统计钱包数量
|
||||
func (r *GormWalletRepository) Count(ctx context.Context, options interfaces.CountOptions) (int64, error) {
|
||||
var count int64
|
||||
query := r.db.WithContext(ctx).Model(&entities.Wallet{})
|
||||
|
||||
if options.Filters != nil {
|
||||
for key, value := range options.Filters {
|
||||
query = query.Where(key+" = ?", value)
|
||||
}
|
||||
}
|
||||
|
||||
if options.Search != "" {
|
||||
query = query.Where("user_id LIKE ?", "%"+options.Search+"%")
|
||||
}
|
||||
|
||||
return count, query.Count(&count).Error
|
||||
}
|
||||
|
||||
// Exists 检查钱包是否存在
|
||||
func (r *GormWalletRepository) Exists(ctx context.Context, id string) (bool, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&entities.Wallet{}).Where("id = ?", id).Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// CreateBatch 批量创建钱包
|
||||
func (r *GormWalletRepository) CreateBatch(ctx context.Context, wallets []entities.Wallet) error {
|
||||
r.logger.Info("批量创建钱包", zap.Int("count", len(wallets)))
|
||||
return r.db.WithContext(ctx).Create(&wallets).Error
|
||||
}
|
||||
|
||||
// GetByIDs 根据ID列表获取钱包
|
||||
func (r *GormWalletRepository) GetByIDs(ctx context.Context, ids []string) ([]entities.Wallet, error) {
|
||||
var wallets []entities.Wallet
|
||||
err := r.db.WithContext(ctx).Where("id IN ?", ids).Find(&wallets).Error
|
||||
return wallets, err
|
||||
}
|
||||
|
||||
// UpdateBatch 批量更新钱包
|
||||
func (r *GormWalletRepository) UpdateBatch(ctx context.Context, wallets []entities.Wallet) error {
|
||||
r.logger.Info("批量更新钱包", zap.Int("count", len(wallets)))
|
||||
return r.db.WithContext(ctx).Save(&wallets).Error
|
||||
}
|
||||
|
||||
// DeleteBatch 批量删除钱包
|
||||
func (r *GormWalletRepository) DeleteBatch(ctx context.Context, ids []string) error {
|
||||
r.logger.Info("批量删除钱包", zap.Strings("ids", ids))
|
||||
return r.db.WithContext(ctx).Delete(&entities.Wallet{}, "id IN ?", ids).Error
|
||||
}
|
||||
|
||||
// List 获取钱包列表
|
||||
func (r *GormWalletRepository) List(ctx context.Context, options interfaces.ListOptions) ([]entities.Wallet, error) {
|
||||
var wallets []entities.Wallet
|
||||
query := r.db.WithContext(ctx).Model(&entities.Wallet{})
|
||||
|
||||
if options.Filters != nil {
|
||||
for key, value := range options.Filters {
|
||||
query = query.Where(key+" = ?", value)
|
||||
}
|
||||
}
|
||||
|
||||
if options.Search != "" {
|
||||
query = query.Where("user_id LIKE ?", "%"+options.Search+"%")
|
||||
}
|
||||
|
||||
if options.Sort != "" {
|
||||
order := "ASC"
|
||||
if options.Order != "" {
|
||||
order = options.Order
|
||||
}
|
||||
query = query.Order(options.Sort + " " + order)
|
||||
}
|
||||
|
||||
if options.Page > 0 && options.PageSize > 0 {
|
||||
offset := (options.Page - 1) * options.PageSize
|
||||
query = query.Offset(offset).Limit(options.PageSize)
|
||||
}
|
||||
|
||||
return wallets, query.Find(&wallets).Error
|
||||
}
|
||||
|
||||
// WithTx 使用事务
|
||||
func (r *GormWalletRepository) WithTx(tx interface{}) interfaces.Repository[entities.Wallet] {
|
||||
if gormTx, ok := tx.(*gorm.DB); ok {
|
||||
return &GormWalletRepository{
|
||||
db: gormTx,
|
||||
logger: r.logger,
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// FindByUserID 根据用户ID查找钱包
|
||||
func (r *GormWalletRepository) FindByUserID(ctx context.Context, userID string) (*entities.Wallet, error) {
|
||||
var wallet entities.Wallet
|
||||
err := r.db.WithContext(ctx).Where("user_id = ?", userID).First(&wallet).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &wallet, nil
|
||||
}
|
||||
|
||||
// ExistsByUserID 检查用户钱包是否存在
|
||||
func (r *GormWalletRepository) ExistsByUserID(ctx context.Context, userID string) (bool, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&entities.Wallet{}).Where("user_id = ?", userID).Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// UpdateBalance 更新余额
|
||||
func (r *GormWalletRepository) UpdateBalance(ctx context.Context, userID string, balance interface{}) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.Wallet{}).Where("user_id = ?", userID).Update("balance", balance).Error
|
||||
}
|
||||
|
||||
// AddBalance 增加余额
|
||||
func (r *GormWalletRepository) AddBalance(ctx context.Context, userID string, amount interface{}) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.Wallet{}).Where("user_id = ?", userID).Update("balance", gorm.Expr("balance + ?", amount)).Error
|
||||
}
|
||||
|
||||
// SubtractBalance 减少余额
|
||||
func (r *GormWalletRepository) SubtractBalance(ctx context.Context, userID string, amount interface{}) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.Wallet{}).Where("user_id = ?", userID).Update("balance", gorm.Expr("balance - ?", amount)).Error
|
||||
}
|
||||
|
||||
// GetTotalBalance 获取总余额
|
||||
func (r *GormWalletRepository) GetTotalBalance(ctx context.Context) (interface{}, error) {
|
||||
var total decimal.Decimal
|
||||
err := r.db.WithContext(ctx).Model(&entities.Wallet{}).Select("COALESCE(SUM(balance), 0)").Scan(&total).Error
|
||||
return total, err
|
||||
}
|
||||
|
||||
// GetActiveWalletCount 获取激活钱包数量
|
||||
func (r *GormWalletRepository) GetActiveWalletCount(ctx context.Context) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&entities.Wallet{}).Where("is_active = ?", true).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GormUserSecretsRepository 用户密钥GORM仓储实现
|
||||
type GormUserSecretsRepository struct {
|
||||
db *gorm.DB
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewGormUserSecretsRepository 创建用户密钥GORM仓储
|
||||
func NewGormUserSecretsRepository(db *gorm.DB, logger *zap.Logger) *GormUserSecretsRepository {
|
||||
return &GormUserSecretsRepository{
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建用户密钥
|
||||
func (r *GormUserSecretsRepository) Create(ctx context.Context, secrets entities.UserSecrets) error {
|
||||
r.logger.Info("创建用户密钥", zap.String("user_id", secrets.UserID))
|
||||
return r.db.WithContext(ctx).Create(&secrets).Error
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取用户密钥
|
||||
func (r *GormUserSecretsRepository) GetByID(ctx context.Context, id string) (entities.UserSecrets, error) {
|
||||
var secrets entities.UserSecrets
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&secrets).Error
|
||||
return secrets, err
|
||||
}
|
||||
|
||||
// Update 更新用户密钥
|
||||
func (r *GormUserSecretsRepository) Update(ctx context.Context, secrets entities.UserSecrets) error {
|
||||
r.logger.Info("更新用户密钥", zap.String("id", secrets.ID))
|
||||
return r.db.WithContext(ctx).Save(&secrets).Error
|
||||
}
|
||||
|
||||
// Delete 删除用户密钥
|
||||
func (r *GormUserSecretsRepository) Delete(ctx context.Context, id string) error {
|
||||
r.logger.Info("删除用户密钥", zap.String("id", id))
|
||||
return r.db.WithContext(ctx).Delete(&entities.UserSecrets{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
// SoftDelete 软删除用户密钥
|
||||
func (r *GormUserSecretsRepository) SoftDelete(ctx context.Context, id string) error {
|
||||
r.logger.Info("软删除用户密钥", zap.String("id", id))
|
||||
return r.db.WithContext(ctx).Delete(&entities.UserSecrets{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
// Restore 恢复用户密钥
|
||||
func (r *GormUserSecretsRepository) Restore(ctx context.Context, id string) error {
|
||||
r.logger.Info("恢复用户密钥", zap.String("id", id))
|
||||
return r.db.WithContext(ctx).Unscoped().Model(&entities.UserSecrets{}).Where("id = ?", id).Update("deleted_at", nil).Error
|
||||
}
|
||||
|
||||
// Count 统计用户密钥数量
|
||||
func (r *GormUserSecretsRepository) Count(ctx context.Context, options interfaces.CountOptions) (int64, error) {
|
||||
var count int64
|
||||
query := r.db.WithContext(ctx).Model(&entities.UserSecrets{})
|
||||
|
||||
if options.Filters != nil {
|
||||
for key, value := range options.Filters {
|
||||
query = query.Where(key+" = ?", value)
|
||||
}
|
||||
}
|
||||
|
||||
if options.Search != "" {
|
||||
query = query.Where("user_id LIKE ? OR access_id LIKE ?", "%"+options.Search+"%", "%"+options.Search+"%")
|
||||
}
|
||||
|
||||
return count, query.Count(&count).Error
|
||||
}
|
||||
|
||||
// Exists 检查用户密钥是否存在
|
||||
func (r *GormUserSecretsRepository) Exists(ctx context.Context, id string) (bool, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&entities.UserSecrets{}).Where("id = ?", id).Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// CreateBatch 批量创建用户密钥
|
||||
func (r *GormUserSecretsRepository) CreateBatch(ctx context.Context, secrets []entities.UserSecrets) error {
|
||||
r.logger.Info("批量创建用户密钥", zap.Int("count", len(secrets)))
|
||||
return r.db.WithContext(ctx).Create(&secrets).Error
|
||||
}
|
||||
|
||||
// GetByIDs 根据ID列表获取用户密钥
|
||||
func (r *GormUserSecretsRepository) GetByIDs(ctx context.Context, ids []string) ([]entities.UserSecrets, error) {
|
||||
var secrets []entities.UserSecrets
|
||||
err := r.db.WithContext(ctx).Where("id IN ?", ids).Find(&secrets).Error
|
||||
return secrets, err
|
||||
}
|
||||
|
||||
// UpdateBatch 批量更新用户密钥
|
||||
func (r *GormUserSecretsRepository) UpdateBatch(ctx context.Context, secrets []entities.UserSecrets) error {
|
||||
r.logger.Info("批量更新用户密钥", zap.Int("count", len(secrets)))
|
||||
return r.db.WithContext(ctx).Save(&secrets).Error
|
||||
}
|
||||
|
||||
// DeleteBatch 批量删除用户密钥
|
||||
func (r *GormUserSecretsRepository) DeleteBatch(ctx context.Context, ids []string) error {
|
||||
r.logger.Info("批量删除用户密钥", zap.Strings("ids", ids))
|
||||
return r.db.WithContext(ctx).Delete(&entities.UserSecrets{}, "id IN ?", ids).Error
|
||||
}
|
||||
|
||||
// List 获取用户密钥列表
|
||||
func (r *GormUserSecretsRepository) List(ctx context.Context, options interfaces.ListOptions) ([]entities.UserSecrets, error) {
|
||||
var secrets []entities.UserSecrets
|
||||
query := r.db.WithContext(ctx).Model(&entities.UserSecrets{})
|
||||
|
||||
if options.Filters != nil {
|
||||
for key, value := range options.Filters {
|
||||
query = query.Where(key+" = ?", value)
|
||||
}
|
||||
}
|
||||
|
||||
if options.Search != "" {
|
||||
query = query.Where("user_id LIKE ? OR access_id LIKE ?", "%"+options.Search+"%", "%"+options.Search+"%")
|
||||
}
|
||||
|
||||
if options.Sort != "" {
|
||||
order := "ASC"
|
||||
if options.Order != "" {
|
||||
order = options.Order
|
||||
}
|
||||
query = query.Order(options.Sort + " " + order)
|
||||
}
|
||||
|
||||
if options.Page > 0 && options.PageSize > 0 {
|
||||
offset := (options.Page - 1) * options.PageSize
|
||||
query = query.Offset(offset).Limit(options.PageSize)
|
||||
}
|
||||
|
||||
return secrets, query.Find(&secrets).Error
|
||||
}
|
||||
|
||||
// WithTx 使用事务
|
||||
func (r *GormUserSecretsRepository) WithTx(tx interface{}) interfaces.Repository[entities.UserSecrets] {
|
||||
if gormTx, ok := tx.(*gorm.DB); ok {
|
||||
return &GormUserSecretsRepository{
|
||||
db: gormTx,
|
||||
logger: r.logger,
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// FindByUserID 根据用户ID查找密钥
|
||||
func (r *GormUserSecretsRepository) FindByUserID(ctx context.Context, userID string) (*entities.UserSecrets, error) {
|
||||
var secrets entities.UserSecrets
|
||||
err := r.db.WithContext(ctx).Where("user_id = ?", userID).First(&secrets).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &secrets, nil
|
||||
}
|
||||
|
||||
// FindByAccessID 根据访问ID查找密钥
|
||||
func (r *GormUserSecretsRepository) FindByAccessID(ctx context.Context, accessID string) (*entities.UserSecrets, error) {
|
||||
var secrets entities.UserSecrets
|
||||
err := r.db.WithContext(ctx).Where("access_id = ?", accessID).First(&secrets).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &secrets, nil
|
||||
}
|
||||
|
||||
// ExistsByUserID 检查用户密钥是否存在
|
||||
func (r *GormUserSecretsRepository) ExistsByUserID(ctx context.Context, userID string) (bool, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&entities.UserSecrets{}).Where("user_id = ?", userID).Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// ExistsByAccessID 检查访问ID是否存在
|
||||
func (r *GormUserSecretsRepository) ExistsByAccessID(ctx context.Context, accessID string) (bool, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&entities.UserSecrets{}).Where("access_id = ?", accessID).Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// UpdateLastUsedAt 更新最后使用时间
|
||||
func (r *GormUserSecretsRepository) UpdateLastUsedAt(ctx context.Context, accessID string) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.UserSecrets{}).Where("access_id = ?", accessID).Update("last_used_at", time.Now()).Error
|
||||
}
|
||||
|
||||
// DeactivateByUserID 停用用户密钥
|
||||
func (r *GormUserSecretsRepository) DeactivateByUserID(ctx context.Context, userID string) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.UserSecrets{}).Where("user_id = ?", userID).Update("is_active", false).Error
|
||||
}
|
||||
|
||||
// RegenerateAccessKey 重新生成访问密钥
|
||||
func (r *GormUserSecretsRepository) RegenerateAccessKey(ctx context.Context, userID string, accessID, accessKey string) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.UserSecrets{}).Where("user_id = ?", userID).Updates(map[string]interface{}{
|
||||
"access_id": accessID,
|
||||
"access_key": accessKey,
|
||||
"updated_at": time.Now(),
|
||||
}).Error
|
||||
}
|
||||
|
||||
// GetExpiredSecrets 获取过期的密钥
|
||||
func (r *GormUserSecretsRepository) GetExpiredSecrets(ctx context.Context) ([]entities.UserSecrets, error) {
|
||||
var secrets []entities.UserSecrets
|
||||
err := r.db.WithContext(ctx).Where("expires_at IS NOT NULL AND expires_at < ?", time.Now()).Find(&secrets).Error
|
||||
return secrets, err
|
||||
}
|
||||
|
||||
// DeleteExpiredSecrets 删除过期的密钥
|
||||
func (r *GormUserSecretsRepository) DeleteExpiredSecrets(ctx context.Context) error {
|
||||
return r.db.WithContext(ctx).Where("expires_at IS NOT NULL AND expires_at < ?", time.Now()).Delete(&entities.UserSecrets{}).Error
|
||||
}
|
||||
Reference in New Issue
Block a user