package model

import (
	"gorm.io/datatypes"
	"gorm.io/gorm"
	"time"
)

// RechargeOrder 充值订单模型
// 记录用户发起充值请求后生成的订单，支持强充和普通充值两种类型
type RechargeOrder struct {
	ID                        uint           `gorm:"column:id;primaryKey" json:"id"`
	RechargeOrderNo           string         `gorm:"column:recharge_order_no;type:varchar(40);uniqueIndex;not null" json:"recharge_order_no"`
	UserID                    uint           `gorm:"column:user_id;not null;index" json:"user_id"`
	AssetWalletID             uint           `gorm:"column:asset_wallet_id;not null" json:"asset_wallet_id"`
	ResourceType              string         `gorm:"column:resource_type;type:varchar(20);not null" json:"resource_type"`
	ResourceID                uint           `gorm:"column:resource_id;not null" json:"resource_id"`
	IotCardID                 *uint          `gorm:"column:iot_card_id" json:"iot_card_id,omitempty"`
	DeviceID                  *uint          `gorm:"column:device_id" json:"device_id,omitempty"`
	Amount                    int64          `gorm:"column:amount;type:bigint;not null" json:"amount"`
	Status                    int            `gorm:"column:status;type:smallint;not null;default:0" json:"status"`
	PaidAt                    *time.Time     `gorm:"column:paid_at" json:"paid_at,omitempty"`
	ShopIDTag                 uint           `gorm:"column:shop_id_tag;not null;default:0" json:"shop_id_tag"`
	EnterpriseIDTag           *uint          `gorm:"column:enterprise_id_tag" json:"enterprise_id_tag,omitempty"`
	OperatorType              string         `gorm:"column:operator_type;type:varchar(30);not null" json:"operator_type"`
	Generation                int            `gorm:"column:generation;not null;default:1" json:"generation"`
	LinkedPackageIDs          datatypes.JSON `gorm:"column:linked_package_ids;type:jsonb" json:"linked_package_ids,omitempty"`
	LinkedOrderType           string         `gorm:"column:linked_order_type;type:varchar(20)" json:"linked_order_type,omitempty"`
	LinkedCarrierType         string         `gorm:"column:linked_carrier_type;type:varchar(20)" json:"linked_carrier_type,omitempty"`
	LinkedCarrierID           *uint          `gorm:"column:linked_carrier_id;type:bigint" json:"linked_carrier_id,omitempty"`
	AutoPurchaseStatus        string         `gorm:"column:auto_purchase_status;type:varchar(20)" json:"auto_purchase_status,omitempty"`
	AccumulatedRechargeSeries int64          `gorm:"column:accumulated_recharge_series;not null;default:0" json:"accumulated_recharge_series"`
	PaymentConfigID           *uint          `gorm:"column:payment_config_id;index" json:"payment_config_id,omitempty"`
	CreatedAt                 time.Time      `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt                 time.Time      `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP" json:"updated_at"`
	DeletedAt                 gorm.DeletedAt `gorm:"column:deleted_at;index" json:"deleted_at,omitempty"`
}

// TableName 指定表名
func (RechargeOrder) TableName() string {
	return "tb_recharge_order"
}

const (
	RechargeOrderStatusPending  = 0 // 待支付
	RechargeOrderStatusPaid     = 1 // 已支付
	RechargeOrderStatusClosed   = 2 // 已关闭
	RechargeOrderStatusRefunded = 3 // 已退款
)

const (
	AutoPurchaseStatusPending = "pending" // 待处理
	AutoPurchaseStatusSuccess = "success" // 成功
	AutoPurchaseStatusFailed  = "failed"  // 失败
)
