package dao

import (
	"server/global"
)

type ExaFileUploadAndDownload struct {
	global.GVA_MODEL
	Name string `json:"name" gorm:"comment:文件名"` // 文件名
	Url  string `json:"url" gorm:"comment:文件地址"` // 文件地址
	Tag  string `json:"tag" gorm:"comment:文件标签"` // 文件标签
	Key  string `json:"key" gorm:"comment:编号"`   // 编号
}

func (ExaFileUploadAndDownload) TableName() string {
	return "exa_file_upload_and_downloads"
}

// TODO:文件查询

// QueryFileById 查询文件 按id
func QueryFileById(id uint) (exaFileUploadAndDownload ExaFileUploadAndDownload, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&exaFileUploadAndDownload).Error
	return exaFileUploadAndDownload, err
}

// GetFileRecordInfoList 分页获取数据
func GetFileRecordInfoList(limit, offset int, keyword string) (fileLists []ExaFileUploadAndDownload, total int64, err error) {

	db := global.GVA_DB.Model(&ExaFileUploadAndDownload{})
	if len(keyword) > 0 {
		db = db.Where("name LIKE ?", "%"+keyword+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Order("updated_at desc").Find(&fileLists).Error
	return fileLists, total, err
}

// TODO:文件新增

// CreateFile 创建文件
func (e ExaFileUploadAndDownload) CreateFile() error {
	return global.GVA_DB.Create(&e).Error
}

// TODO:文件修改

// EditFileName 编辑文件名或者备注
func (e ExaFileUploadAndDownload) EditFileName() (fileFromDb ExaFileUploadAndDownload, err error) {
	err = global.GVA_DB.Where("id = ?", e.ID).First(&fileFromDb).Update("name", e.Name).Error
	return
}

// TODO:文件删除

// DeleteFile 删除文件
func (e ExaFileUploadAndDownload) DeleteFile() error {
	return global.GVA_DB.Where("id = ?", e.ID).Unscoped().Delete(&e).Error
}
