package entity

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

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

// SchemaFileChunk demo对象
type SchemaFileChunk schema.FileChunk

// ToFileChunk 转换为demo实体
func (a SchemaFileChunk) ToFileChunk() *FileChunk {
	item := &FileChunk{
		RecordID: a.RecordID,
		Hash:     a.Hash,
		Path:     a.Path,
		Name:     a.Name,
		Total:    a.Total,
		Current:  a.Current,
		Size:     a.Size,
		Creator:  &a.Creator,
		FileHash: a.FileHash,
		PastTime: a.PastTime,
	}
	return item
}

// ToSchemaFileChunk 转换为demo对象
func (a FileChunk) ToSchemaFileChunk() *schema.FileChunk {
	item := &schema.FileChunk{
		RecordID:  a.RecordID,
		Hash:      a.Hash,
		Path:      a.Path,
		Name:      a.Name,
		Total:     a.Total,
		Current:   a.Current,
		Size:      a.Size,
		Creator:   *a.Creator,
		CreatedAt: a.CreatedAt,
		FileHash:  a.FileHash,
		PastTime:  a.PastTime,
	}
	return item
}

// FileChunk demo实体
type FileChunk struct {
	gorm.Model
	RecordID string    `gorm:"column:record_id;size:32;index;"` // 记录内码
	Hash     string    `gorm:"column:hash;size:36;index"`
	FileHash string    `gorm:"column:file_hash;size:36;index"`
	Path     string    `gorm:"column:path;size:250"`
	Name     string    `gorm:"column:name;size:200"`
	Total    int       `gorm:"column:total"`
	Current  int       `gorm:"column:current"`
	Size     int64     `gorm:"column:size"`
	Creator  *string   `gorm:"column:creator;size:32;"` // 创建者
	PastTime time.Time `gorm:"column:past_time;"`       //文件块过期的时间
}

// FileChunks demo列表
type FileChunks []*FileChunk

// ToSchemaFileChunks 转换为demo对象列表
func (a FileChunks) ToSchemaFileChunks() []*schema.FileChunk {
	list := make([]*schema.FileChunk, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaFileChunk()
	}
	return list
}
