Files
hfb_sys/backend/internal/modules/withdrawal/service.go
T
2026-06-06 10:08:39 +08:00

108 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package withdrawal
import "errors"
var (
ErrDependencyUnavailable = errors.New("dependency unavailable")
ErrWithdrawalNotFound = errors.New("withdrawal not found")
ErrInvalidAmount = errors.New("invalid amount")
ErrInsufficientBalance = errors.New("insufficient balance")
ErrMinWithdrawalAmount = errors.New("amount below minimum withdrawal")
ErrMaxWithdrawalAmount = errors.New("amount exceeds maximum withdrawal")
ErrWithdrawalLocked = errors.New("withdrawal status locked")
ErrUnauthorized = errors.New("unauthorized")
)
const (
MinWithdrawalAmount = 10.0 // 最低提现金额
MaxWithdrawalAmount = 5000.0 // 单笔最高提现金额
WithdrawalFeeRate = 0.0 // 手续费率(暂时0%
)
type Service struct {
repo *Repository
}
func NewService(repo *Repository) *Service {
return &Service{repo: repo}
}
// 用户端方法
func (s *Service) Create(userID uint64, req CreateWithdrawalRequest) (*WithdrawalDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
// 验证金额
if req.Amount < MinWithdrawalAmount {
return nil, ErrMinWithdrawalAmount
}
if req.Amount > MaxWithdrawalAmount {
return nil, ErrMaxWithdrawalAmount
}
return s.repo.Create(userID, req)
}
func (s *Service) List(userID uint64, page, pageSize int) (*PaginatedResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
if page < 1 {
page = 1
}
if pageSize < 1 || pageSize > 100 {
pageSize = 20
}
return s.repo.List(userID, page, pageSize)
}
func (s *Service) FindByID(userID, id uint64) (*WithdrawalDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.FindByID(userID, id)
}
func (s *Service) Cancel(userID, id uint64) error {
if s.repo == nil {
return ErrDependencyUnavailable
}
return s.repo.Cancel(userID, id)
}
// 管理员端方法
func (s *Service) AdminList(query AdminListQuery) (*AdminPaginatedResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
if query.Page < 1 {
query.Page = 1
}
if query.Size < 1 || query.Size > 100 {
query.Size = 20
}
return s.repo.AdminList(query)
}
func (s *Service) AdminFindByID(id uint64) (*WithdrawalDetailDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.AdminFindByID(id)
}
func (s *Service) Review(adminID, id uint64, req ReviewWithdrawalRequest) (*WithdrawalDetailDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.Review(adminID, id, req)
}
func (s *Service) ConfirmPayment(adminID, id uint64, req ConfirmPaymentRequest) (*WithdrawalDetailDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.ConfirmPayment(adminID, id, req)
}