178 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			178 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package entities
 | ||
| 
 | ||
| import (
 | ||
| 	"errors"
 | ||
| 	"time"
 | ||
| 
 | ||
| 	"github.com/google/uuid"
 | ||
| 	"github.com/shopspring/decimal"
 | ||
| 	"gorm.io/gorm"
 | ||
| )
 | ||
| 
 | ||
| // RechargeType 充值类型枚举
 | ||
| type RechargeType string
 | ||
| 
 | ||
| const (
 | ||
| 	RechargeTypeAlipay   RechargeType = "alipay"   // 支付宝充值
 | ||
| 	RechargeTypeTransfer RechargeType = "transfer" // 对公转账
 | ||
| 	RechargeTypeGift     RechargeType = "gift"     // 赠送
 | ||
| )
 | ||
| 
 | ||
| // RechargeStatus 充值状态枚举
 | ||
| type RechargeStatus string
 | ||
| 
 | ||
| const (
 | ||
| 	RechargeStatusPending   RechargeStatus = "pending"   // 待处理
 | ||
| 	RechargeStatusSuccess   RechargeStatus = "success"   // 成功
 | ||
| 	RechargeStatusFailed    RechargeStatus = "failed"    // 失败
 | ||
| 	RechargeStatusCancelled RechargeStatus = "cancelled" // 已取消
 | ||
| )
 | ||
| 
 | ||
| // RechargeRecord 充值记录实体
 | ||
| // 记录用户的各种充值操作,包括支付宝充值、对公转账、赠送等
 | ||
| type RechargeRecord struct {
 | ||
| 	// 基础标识
 | ||
| 	ID     string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"充值记录唯一标识"`
 | ||
| 	UserID string `gorm:"type:varchar(36);not null;index" json:"user_id" comment:"充值用户ID"`
 | ||
| 
 | ||
| 	// 充值信息
 | ||
| 	Amount       decimal.Decimal `gorm:"type:decimal(20,8);not null" json:"amount" comment:"充值金额"`
 | ||
| 	RechargeType RechargeType    `gorm:"type:varchar(20);not null;index" json:"recharge_type" comment:"充值类型"`
 | ||
| 	Status       RechargeStatus  `gorm:"type:varchar(20);not null;default:'pending';index" json:"status" comment:"充值状态"`
 | ||
| 
 | ||
| 	// 订单号字段(根据充值类型使用不同字段)
 | ||
| 	AlipayOrderID   *string `gorm:"type:varchar(64);uniqueIndex" json:"alipay_order_id,omitempty" comment:"支付宝订单号"`
 | ||
| 	TransferOrderID *string `gorm:"type:varchar(64);uniqueIndex" json:"transfer_order_id,omitempty" comment:"转账订单号"`
 | ||
| 
 | ||
| 	// 通用字段
 | ||
| 	Notes string `gorm:"type:varchar(500)" json:"notes,omitempty" comment:"备注信息"`
 | ||
| 
 | ||
| 	// 时间戳字段
 | ||
| 	CreatedAt time.Time      `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
 | ||
| 	UpdatedAt time.Time      `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间"`
 | ||
| 	DeletedAt gorm.DeletedAt `gorm:"index" json:"-" comment:"软删除时间"`
 | ||
| }
 | ||
| 
 | ||
| // TableName 指定数据库表名
 | ||
| func (RechargeRecord) TableName() string {
 | ||
| 	return "recharge_records"
 | ||
| }
 | ||
| 
 | ||
| // BeforeCreate GORM钩子:创建前自动生成UUID
 | ||
| func (r *RechargeRecord) BeforeCreate(tx *gorm.DB) error {
 | ||
| 	if r.ID == "" {
 | ||
| 		r.ID = uuid.New().String()
 | ||
| 	}
 | ||
| 	return nil
 | ||
| }
 | ||
| 
 | ||
| // IsPending 检查是否为待处理状态
 | ||
| func (r *RechargeRecord) IsPending() bool {
 | ||
| 	return r.Status == RechargeStatusPending
 | ||
| }
 | ||
| 
 | ||
| // IsSuccess 检查是否为成功状态
 | ||
| func (r *RechargeRecord) IsSuccess() bool {
 | ||
| 	return r.Status == RechargeStatusSuccess
 | ||
| }
 | ||
| 
 | ||
| // IsFailed 检查是否为失败状态
 | ||
| func (r *RechargeRecord) IsFailed() bool {
 | ||
| 	return r.Status == RechargeStatusFailed
 | ||
| }
 | ||
| 
 | ||
| // IsCancelled 检查是否为已取消状态
 | ||
| func (r *RechargeRecord) IsCancelled() bool {
 | ||
| 	return r.Status == RechargeStatusCancelled
 | ||
| }
 | ||
| 
 | ||
| // MarkSuccess 标记为成功
 | ||
| func (r *RechargeRecord) MarkSuccess() {
 | ||
| 	r.Status = RechargeStatusSuccess
 | ||
| }
 | ||
| 
 | ||
| // MarkFailed 标记为失败
 | ||
| func (r *RechargeRecord) MarkFailed() {
 | ||
| 	r.Status = RechargeStatusFailed
 | ||
| }
 | ||
| 
 | ||
| // MarkCancelled 标记为已取消
 | ||
| func (r *RechargeRecord) MarkCancelled() {
 | ||
| 	r.Status = RechargeStatusCancelled
 | ||
| }
 | ||
| 
 | ||
| // ValidatePaymentMethod 验证支付方式:支付宝订单号和转账订单号只能有一个存在
 | ||
| func (r *RechargeRecord) ValidatePaymentMethod() error {
 | ||
| 	hasAlipay := r.AlipayOrderID != nil && *r.AlipayOrderID != ""
 | ||
| 	hasTransfer := r.TransferOrderID != nil && *r.TransferOrderID != ""
 | ||
| 
 | ||
| 	if hasAlipay && hasTransfer {
 | ||
| 		return errors.New("支付宝订单号和转账订单号不能同时存在")
 | ||
| 	}
 | ||
| 
 | ||
| 	if !hasAlipay && !hasTransfer {
 | ||
| 		return errors.New("必须提供支付宝订单号或转账订单号")
 | ||
| 	}
 | ||
| 
 | ||
| 	return nil
 | ||
| }
 | ||
| 
 | ||
| // GetOrderID 获取订单号(根据充值类型返回对应的订单号)
 | ||
| func (r *RechargeRecord) GetOrderID() string {
 | ||
| 	switch r.RechargeType {
 | ||
| 	case RechargeTypeAlipay:
 | ||
| 		if r.AlipayOrderID != nil {
 | ||
| 			return *r.AlipayOrderID
 | ||
| 		}
 | ||
| 	case RechargeTypeTransfer:
 | ||
| 		if r.TransferOrderID != nil {
 | ||
| 			return *r.TransferOrderID
 | ||
| 		}
 | ||
| 	}
 | ||
| 	return ""
 | ||
| }
 | ||
| 
 | ||
| // SetAlipayOrderID 设置支付宝订单号
 | ||
| func (r *RechargeRecord) SetAlipayOrderID(orderID string) {
 | ||
| 	r.AlipayOrderID = &orderID
 | ||
| }
 | ||
| 
 | ||
| // SetTransferOrderID 设置转账订单号
 | ||
| func (r *RechargeRecord) SetTransferOrderID(orderID string) {
 | ||
| 	r.TransferOrderID = &orderID
 | ||
| }
 | ||
| 
 | ||
| // NewAlipayRechargeRecord 工厂方法 - 创建支付宝充值记录
 | ||
| func NewAlipayRechargeRecord(userID string, amount decimal.Decimal, alipayOrderID string) *RechargeRecord {
 | ||
| 	return &RechargeRecord{
 | ||
| 		UserID:        userID,
 | ||
| 		Amount:        amount,
 | ||
| 		RechargeType:  RechargeTypeAlipay,
 | ||
| 		Status:        RechargeStatusPending,
 | ||
| 		AlipayOrderID: &alipayOrderID,
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| // NewTransferRechargeRecord 工厂方法 - 创建对公转账充值记录
 | ||
| func NewTransferRechargeRecord(userID string, amount decimal.Decimal, transferOrderID, notes string) *RechargeRecord {
 | ||
| 	return &RechargeRecord{
 | ||
| 		UserID:          userID,
 | ||
| 		Amount:          amount,
 | ||
| 		RechargeType:    RechargeTypeTransfer,
 | ||
| 		Status:          RechargeStatusPending,
 | ||
| 		TransferOrderID: &transferOrderID,
 | ||
| 		Notes:           notes,
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| // NewGiftRechargeRecord 工厂方法 - 创建赠送充值记录
 | ||
| func NewGiftRechargeRecord(userID string, amount decimal.Decimal, notes string) *RechargeRecord {
 | ||
| 	return &RechargeRecord{
 | ||
| 		UserID:       userID,
 | ||
| 		Amount:       amount,
 | ||
| 		RechargeType: RechargeTypeGift,
 | ||
| 		Status:       RechargeStatusSuccess, // 赠送直接标记为成功
 | ||
| 		Notes:        notes,
 | ||
| 	}
 | ||
| }
 |