package model

import (
	"database/sql/driver"
	"encoding/json"
	"time"

	"gorm.io/gorm"
)

// AssetPackageBatchOrderTask 资产套餐批量订购任务。
type AssetPackageBatchOrderTask struct {
	ID              uint                              `gorm:"column:id;primaryKey" json:"id"`
	TaskNo          string                            `gorm:"column:task_no;type:varchar(50);not null;uniqueIndex" json:"task_no"`
	PackageID       uint                              `gorm:"column:package_id;not null;index" json:"package_id"`
	PackageCode     string                            `gorm:"column:package_code;type:varchar(100);not null;default:''" json:"package_code"`
	PackageName     string                            `gorm:"column:package_name;type:varchar(255);not null;default:''" json:"package_name"`
	PaymentMethod   string                            `gorm:"column:payment_method;type:varchar(20);not null" json:"payment_method"`
	FileName        string                            `gorm:"column:file_name;type:varchar(255);not null;default:''" json:"file_name"`
	StorageKey      string                            `gorm:"column:storage_key;type:varchar(500);not null" json:"storage_key"`
	VoucherKeys     StringJSONBArray                  `gorm:"column:voucher_keys;type:jsonb;not null;default:'[]'" json:"voucher_keys"`
	Status          int                               `gorm:"column:status;type:int;not null;default:1;index" json:"status"`
	TotalCount      int                               `gorm:"column:total_count;not null;default:0" json:"total_count"`
	SuccessCount    int                               `gorm:"column:success_count;not null;default:0" json:"success_count"`
	FailCount       int                               `gorm:"column:fail_count;not null;default:0" json:"fail_count"`
	ResultItems     AssetPackageBatchOrderResultItems `gorm:"column:result_items;type:jsonb;not null;default:'[]'" json:"result_items"`
	ErrorMessage    string                            `gorm:"column:error_message;type:text;not null;default:''" json:"error_message"`
	CreatorUserType int                               `gorm:"column:creator_user_type;type:int;not null;default:0" json:"creator_user_type"`
	CreatorShopID   uint                              `gorm:"column:creator_shop_id;not null;default:0" json:"creator_shop_id"`
	CreatorName     string                            `gorm:"column:creator_name;type:varchar(100);not null;default:''" json:"creator_name"`
	StartedAt       *time.Time                        `gorm:"column:started_at" json:"started_at"`
	CompletedAt     *time.Time                        `gorm:"column:completed_at" json:"completed_at"`
	Creator         uint                              `gorm:"column:creator;not null;default:0" json:"creator"`
	Updater         uint                              `gorm:"column:updater;not null;default:0" json:"updater"`
	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 (AssetPackageBatchOrderTask) TableName() string {
	return "tb_asset_package_batch_order_task"
}

// AssetPackageBatchOrderResultItem 批量订购单行结果。
type AssetPackageBatchOrderResultItem struct {
	Line            int    `json:"line"`
	AssetIdentifier string `json:"asset_identifier"`
	Status          int    `json:"status"`
	OrderID         uint   `json:"order_id,omitempty"`
	OrderNo         string `json:"order_no,omitempty"`
	Amount          int64  `json:"amount,omitempty"`
	Reason          string `json:"reason,omitempty"`
}

// AssetPackageBatchOrderResultItems 批量订购单行结果集合。
type AssetPackageBatchOrderResultItems []AssetPackageBatchOrderResultItem

// Value 将批量订购单行结果序列化为 JSONB。
func (items AssetPackageBatchOrderResultItems) Value() (driver.Value, error) {
	if items == nil {
		return "[]", nil
	}
	return json.Marshal(items)
}

// Scan 从 JSONB 读取批量订购单行结果。
func (items *AssetPackageBatchOrderResultItems) Scan(value any) error {
	if value == nil {
		*items = AssetPackageBatchOrderResultItems{}
		return nil
	}
	data, ok := value.([]byte)
	if !ok {
		return nil
	}
	return json.Unmarshal(data, items)
}
