temp
This commit is contained in:
@@ -1,140 +0,0 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
// WalletInfo 钱包信息
|
||||
type WalletInfo struct {
|
||||
ID string `json:"id"` // 钱包ID
|
||||
UserID string `json:"user_id"` // 用户ID
|
||||
IsActive bool `json:"is_active"` // 是否激活
|
||||
Balance decimal.Decimal `json:"balance"` // 余额
|
||||
CreatedAt time.Time `json:"created_at"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updated_at"` // 更新时间
|
||||
}
|
||||
|
||||
// UserSecretsInfo 用户密钥信息
|
||||
type UserSecretsInfo struct {
|
||||
ID string `json:"id"` // 密钥ID
|
||||
UserID string `json:"user_id"` // 用户ID
|
||||
AccessID string `json:"access_id"` // 访问ID
|
||||
AccessKey string `json:"access_key"` // 访问密钥
|
||||
IsActive bool `json:"is_active"` // 是否激活
|
||||
LastUsedAt *time.Time `json:"last_used_at"` // 最后使用时间
|
||||
ExpiresAt *time.Time `json:"expires_at"` // 过期时间
|
||||
CreatedAt time.Time `json:"created_at"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updated_at"` // 更新时间
|
||||
}
|
||||
|
||||
// CreateWalletRequest 创建钱包请求
|
||||
type CreateWalletRequest struct {
|
||||
UserID string `json:"user_id" binding:"required"` // 用户ID
|
||||
}
|
||||
|
||||
// CreateWalletResponse 创建钱包响应
|
||||
type CreateWalletResponse struct {
|
||||
Wallet WalletInfo `json:"wallet"` // 钱包信息
|
||||
}
|
||||
|
||||
// GetWalletRequest 获取钱包请求
|
||||
type GetWalletRequest struct {
|
||||
UserID string `form:"user_id" binding:"required"` // 用户ID
|
||||
}
|
||||
|
||||
// UpdateWalletRequest 更新钱包请求
|
||||
type UpdateWalletRequest struct {
|
||||
UserID string `json:"user_id" binding:"required"` // 用户ID
|
||||
Balance decimal.Decimal `json:"balance"` // 余额
|
||||
IsActive *bool `json:"is_active"` // 是否激活
|
||||
}
|
||||
|
||||
// RechargeRequest 充值请求
|
||||
type RechargeRequest struct {
|
||||
UserID string `json:"user_id" binding:"required"` // 用户ID
|
||||
Amount decimal.Decimal `json:"amount" binding:"required"` // 充值金额
|
||||
}
|
||||
|
||||
// RechargeResponse 充值响应
|
||||
type RechargeResponse struct {
|
||||
WalletID string `json:"wallet_id"` // 钱包ID
|
||||
Amount decimal.Decimal `json:"amount"` // 充值金额
|
||||
Balance decimal.Decimal `json:"balance"` // 充值后余额
|
||||
}
|
||||
|
||||
// WithdrawRequest 提现请求
|
||||
type WithdrawRequest struct {
|
||||
UserID string `json:"user_id" binding:"required"` // 用户ID
|
||||
Amount decimal.Decimal `json:"amount" binding:"required"` // 提现金额
|
||||
}
|
||||
|
||||
// WithdrawResponse 提现响应
|
||||
type WithdrawResponse struct {
|
||||
WalletID string `json:"wallet_id"` // 钱包ID
|
||||
Amount decimal.Decimal `json:"amount"` // 提现金额
|
||||
Balance decimal.Decimal `json:"balance"` // 提现后余额
|
||||
}
|
||||
|
||||
// CreateUserSecretsRequest 创建用户密钥请求
|
||||
type CreateUserSecretsRequest struct {
|
||||
UserID string `json:"user_id" binding:"required"` // 用户ID
|
||||
ExpiresAt *time.Time `json:"expires_at"` // 过期时间
|
||||
}
|
||||
|
||||
// CreateUserSecretsResponse 创建用户密钥响应
|
||||
type CreateUserSecretsResponse struct {
|
||||
Secrets UserSecretsInfo `json:"secrets"` // 密钥信息
|
||||
}
|
||||
|
||||
// GetUserSecretsRequest 获取用户密钥请求
|
||||
type GetUserSecretsRequest struct {
|
||||
UserID string `form:"user_id" binding:"required"` // 用户ID
|
||||
}
|
||||
|
||||
// RegenerateAccessKeyRequest 重新生成访问密钥请求
|
||||
type RegenerateAccessKeyRequest struct {
|
||||
UserID string `json:"user_id" binding:"required"` // 用户ID
|
||||
ExpiresAt *time.Time `json:"expires_at"` // 过期时间
|
||||
}
|
||||
|
||||
// RegenerateAccessKeyResponse 重新生成访问密钥响应
|
||||
type RegenerateAccessKeyResponse struct {
|
||||
AccessID string `json:"access_id"` // 新的访问ID
|
||||
AccessKey string `json:"access_key"` // 新的访问密钥
|
||||
}
|
||||
|
||||
// DeactivateUserSecretsRequest 停用用户密钥请求
|
||||
type DeactivateUserSecretsRequest struct {
|
||||
UserID string `json:"user_id" binding:"required"` // 用户ID
|
||||
}
|
||||
|
||||
// WalletTransactionRequest 钱包交易请求
|
||||
type WalletTransactionRequest struct {
|
||||
FromUserID string `json:"from_user_id" binding:"required"` // 转出用户ID
|
||||
ToUserID string `json:"to_user_id" binding:"required"` // 转入用户ID
|
||||
Amount decimal.Decimal `json:"amount" binding:"required"` // 交易金额
|
||||
Notes string `json:"notes"` // 交易备注
|
||||
}
|
||||
|
||||
// WalletTransactionResponse 钱包交易响应
|
||||
type WalletTransactionResponse struct {
|
||||
TransactionID string `json:"transaction_id"` // 交易ID
|
||||
FromUserID string `json:"from_user_id"` // 转出用户ID
|
||||
ToUserID string `json:"to_user_id"` // 转入用户ID
|
||||
Amount decimal.Decimal `json:"amount"` // 交易金额
|
||||
FromBalance decimal.Decimal `json:"from_balance"` // 转出后余额
|
||||
ToBalance decimal.Decimal `json:"to_balance"` // 转入后余额
|
||||
Notes string `json:"notes"` // 交易备注
|
||||
CreatedAt time.Time `json:"created_at"` // 交易时间
|
||||
}
|
||||
|
||||
// WalletStatsResponse 钱包统计响应
|
||||
type WalletStatsResponse struct {
|
||||
TotalWallets int64 `json:"total_wallets"` // 总钱包数
|
||||
ActiveWallets int64 `json:"active_wallets"` // 激活钱包数
|
||||
TotalBalance decimal.Decimal `json:"total_balance"` // 总余额
|
||||
TodayTransactions int64 `json:"today_transactions"` // 今日交易数
|
||||
TodayVolume decimal.Decimal `json:"today_volume"` // 今日交易量
|
||||
}
|
||||
@@ -1,12 +1,18 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"tyapi-server/internal/domains/finance/entities"
|
||||
"tyapi-server/internal/domains/finance/repositories"
|
||||
)
|
||||
|
||||
// FinanceService 财务领域服务
|
||||
// 负责财务相关的业务逻辑,包括钱包管理、余额操作等
|
||||
type FinanceService struct {
|
||||
walletRepo repositories.WalletRepository
|
||||
logger *zap.Logger
|
||||
@@ -22,3 +28,133 @@ func NewFinanceService(
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateWallet 创建钱包
|
||||
func (s *FinanceService) CreateWallet(ctx context.Context, userID string) (*entities.Wallet, error) {
|
||||
// 检查用户是否已有钱包
|
||||
existingWallet, err := s.walletRepo.GetByUserID(ctx, userID)
|
||||
if err == nil && existingWallet != nil {
|
||||
return nil, fmt.Errorf("用户已有钱包")
|
||||
}
|
||||
|
||||
// 创建钱包
|
||||
wallet := &entities.Wallet{
|
||||
UserID: userID,
|
||||
Balance: decimal.Zero,
|
||||
IsActive: true,
|
||||
WalletType: "MAIN",
|
||||
}
|
||||
|
||||
createdWallet, err := s.walletRepo.Create(ctx, *wallet)
|
||||
if err != nil {
|
||||
s.logger.Error("创建钱包失败", zap.Error(err))
|
||||
return nil, fmt.Errorf("创建钱包失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("钱包创建成功",
|
||||
zap.String("wallet_id", createdWallet.ID),
|
||||
zap.String("user_id", userID),
|
||||
)
|
||||
|
||||
return &createdWallet, nil
|
||||
}
|
||||
|
||||
// GetWallet 获取钱包信息
|
||||
func (s *FinanceService) GetWallet(ctx context.Context, userID string) (*entities.Wallet, error) {
|
||||
wallet, err := s.walletRepo.GetByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("钱包不存在: %w", err)
|
||||
}
|
||||
return wallet, nil
|
||||
}
|
||||
|
||||
// GetWalletByID 根据ID获取钱包
|
||||
func (s *FinanceService) GetWalletByID(ctx context.Context, walletID string) (*entities.Wallet, error) {
|
||||
wallet, err := s.walletRepo.GetByID(ctx, walletID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("钱包不存在: %w", err)
|
||||
}
|
||||
return &wallet, nil
|
||||
}
|
||||
|
||||
// RechargeWallet 充值钱包
|
||||
func (s *FinanceService) RechargeWallet(ctx context.Context, userID string, amount float64) error {
|
||||
if amount <= 0 {
|
||||
return fmt.Errorf("充值金额必须大于0")
|
||||
}
|
||||
|
||||
wallet, err := s.walletRepo.GetByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("钱包不存在: %w", err)
|
||||
}
|
||||
|
||||
// 更新余额
|
||||
amountDecimal := decimal.NewFromFloat(amount)
|
||||
wallet.AddBalance(amountDecimal)
|
||||
|
||||
if err := s.walletRepo.Update(ctx, *wallet); err != nil {
|
||||
s.logger.Error("充值失败", zap.Error(err))
|
||||
return fmt.Errorf("充值失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("钱包充值成功",
|
||||
zap.String("wallet_id", wallet.ID),
|
||||
zap.String("user_id", userID),
|
||||
zap.Float64("amount", amount),
|
||||
zap.String("new_balance", wallet.GetFormattedBalance()),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeductWallet 扣减钱包余额
|
||||
func (s *FinanceService) DeductWallet(ctx context.Context, userID string, amount float64) error {
|
||||
if amount <= 0 {
|
||||
return fmt.Errorf("扣减金额必须大于0")
|
||||
}
|
||||
|
||||
wallet, err := s.walletRepo.GetByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("钱包不存在: %w", err)
|
||||
}
|
||||
|
||||
amountDecimal := decimal.NewFromFloat(amount)
|
||||
if err := wallet.SubtractBalance(amountDecimal); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.walletRepo.Update(ctx, *wallet); err != nil {
|
||||
s.logger.Error("扣减失败", zap.Error(err))
|
||||
return fmt.Errorf("扣减失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("钱包扣减成功",
|
||||
zap.String("wallet_id", wallet.ID),
|
||||
zap.String("user_id", userID),
|
||||
zap.Float64("amount", amount),
|
||||
zap.String("new_balance", wallet.GetFormattedBalance()),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetWalletBalance 获取钱包余额
|
||||
func (s *FinanceService) GetWalletBalance(ctx context.Context, userID string) (float64, error) {
|
||||
wallet, err := s.walletRepo.GetByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("钱包不存在: %w", err)
|
||||
}
|
||||
balance, _ := wallet.Balance.Float64()
|
||||
return balance, nil
|
||||
}
|
||||
|
||||
// CheckWalletBalance 检查钱包余额是否足够
|
||||
func (s *FinanceService) CheckWalletBalance(ctx context.Context, userID string, amount float64) (bool, error) {
|
||||
wallet, err := s.walletRepo.GetByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("钱包不存在: %w", err)
|
||||
}
|
||||
|
||||
amountDecimal := decimal.NewFromFloat(amount)
|
||||
return wallet.HasSufficientBalance(amountDecimal), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user