Files
EdgeAPI/internal/db/models/file_dao.go

90 lines
1.6 KiB
Go
Raw Normal View History

2020-09-13 20:37:28 +08:00
package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
FileStateEnabled = 1 // 已启用
FileStateDisabled = 0 // 已禁用
)
type FileDAO dbs.DAO
func NewFileDAO() *FileDAO {
return dbs.NewDAO(&FileDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeFiles",
Model: new(File),
PkName: "id",
},
}).(*FileDAO)
}
2020-10-13 20:05:13 +08:00
var SharedFileDAO *FileDAO
func init() {
dbs.OnReady(func() {
SharedFileDAO = NewFileDAO()
})
}
2020-09-13 20:37:28 +08:00
// 启用条目
func (this *FileDAO) EnableFile(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", FileStateEnabled).
Update()
return err
}
// 禁用条目
func (this *FileDAO) DisableFile(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", FileStateDisabled).
Update()
return err
}
// 查找启用中的条目
func (this *FileDAO) FindEnabledFile(id int64) (*File, error) {
result, err := this.Query().
Pk(id).
Attr("state", FileStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*File), err
}
// 创建文件
2020-11-04 15:51:32 +08:00
func (this *FileDAO) CreateFile(businessType, description string, filename string, size int64) (int64, error) {
2020-09-13 20:37:28 +08:00
op := NewFileOperator()
op.Type = businessType
op.Description = description
op.State = FileStateEnabled
2020-11-04 15:51:32 +08:00
op.Size = size
2020-09-13 20:37:28 +08:00
op.Filename = filename
2020-12-09 20:44:05 +08:00
err := this.Save(op)
2020-09-13 20:37:28 +08:00
if err != nil {
return 0, err
}
2020-11-04 15:51:32 +08:00
return types.Int64(op.Id), nil
2020-09-13 20:37:28 +08:00
}
2020-11-04 15:51:32 +08:00
// 将文件置为已完成
func (this *FileDAO) UpdateFileIsFinished(fileId int64) error {
_, err := this.Query().
Pk(fileId).
Set("isFinished", true).
Update()
return err
2020-09-13 20:37:28 +08:00
}