package entity

import (
	"context"
	"gorm.io/gorm"
	"gxt-file-server/app/schema"
)

// GetFileHistoryDB 获取demo存储
func GetFileHistoryDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return getDBWithModel(ctx, defDB, FileHistory{})
}

// SchemaFileHistory demo对象
type SchemaFileHistory schema.FileHistory

// ToFileHistory 转换为demo实体
func (a SchemaFileHistory) ToFileHistory() *FileHistory {
	item := &FileHistory{
		RecordID:     a.RecordID,
		Hash:         a.Hash,
		Path:         a.Path,
		FileName:     a.FileName,
		FileSize:     a.FileSize,
		IsPersistent: a.IsPersistent,
		Creator:      &a.Creator,
		FileHash:     a.FileHash,
	}
	return item
}

// ToSchemaFileHistory 转换为demo对象
func (a FileHistory) ToSchemaFileHistory() *schema.FileHistory {
	item := &schema.FileHistory{
		RecordID:     a.RecordID,
		Creator:      *a.Creator,
		Hash:         a.Hash,
		Path:         a.Path,
		FileName:     a.FileName,
		FileSize:     a.FileSize,
		IsPersistent: a.IsPersistent,
		CreatedAt:    a.CreatedAt,
		FileHash:     a.FileHash,
	}
	return item
}

// FileHistory demo实体
type FileHistory struct {
	gorm.Model
	RecordID     string  `gorm:"column:record_id;size:32;index;"` // 记录内码
	Hash         string  `gorm:"column:hash;index;"`
	Path         string  `gorm:"column:path;size:250"`
	FileSize     int64   `gorm:"column:file_size"`
	FileName     string  `gorm:"column:file_name;size:200"`
	IsPersistent int     `gorm:"column:is_persistent"`    // 是否持久化
	Creator      *string `gorm:"column:creator;size:32;"` // 创建者
	FileHash     string  `gorm:"column:file_hash;"`
}

// FileHistories demo列表
type FileHistories []*FileHistory

// ToSchemaFileHistories 转换为demo对象列表
func (a FileHistories) ToSchemaFileHistories() []*schema.FileHistory {
	list := make([]*schema.FileHistory, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaFileHistory()
	}
	return list
}
