115 lines
3.4 KiB
Go
115 lines
3.4 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"observer-server/biz/consts"
|
|
"observer-server/biz/model/entity"
|
|
"observer-server/common"
|
|
)
|
|
|
|
// AnnotateTask 标注众包任务表 DAO:管理端下发/停用,App 端只读 published。
|
|
type annotateTaskDao struct{}
|
|
|
|
var AnnotateTask = &annotateTaskDao{}
|
|
|
|
func init() {
|
|
ctx := context.Background()
|
|
_, err := g.DB().Exec(ctx, `CREATE TABLE IF NOT EXISTS annotate_task (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
dataset_id INTEGER NOT NULL,
|
|
name TEXT NOT NULL,
|
|
status TEXT NOT NULL DEFAULT 'published',
|
|
created_at TEXT NOT NULL
|
|
)`)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// InsertInTx 事务内下发任务(与批量占用同事务,service 编排多表一致性)
|
|
func (d *annotateTaskDao) InsertInTx(ctx context.Context, tx gdb.TX, m *entity.AnnotateTask) (int64, error) {
|
|
res, err := g.DB().Model(consts.TableAnnotateTask).Ctx(ctx).TX(tx).Data(g.Map{
|
|
"dataset_id": m.DatasetId,
|
|
"name": m.Name,
|
|
"status": m.Status,
|
|
"created_at": m.CreatedAt,
|
|
}).Insert()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.LastInsertId()
|
|
}
|
|
|
|
// GetById 按主键取
|
|
func (d *annotateTaskDao) GetById(ctx context.Context, id int64) (*entity.AnnotateTask, error) {
|
|
var one *entity.AnnotateTask
|
|
err := g.DB().Model(consts.TableAnnotateTask).Ctx(ctx).Where("id", id).Scan(&one)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return one, nil
|
|
}
|
|
|
|
// GetPublishedByDataset 某数据集当前 published 任务(下发查重:同数据集同时只允许一个开放任务)
|
|
func (d *annotateTaskDao) GetPublishedByDataset(ctx context.Context, datasetId int64) (*entity.AnnotateTask, error) {
|
|
var one *entity.AnnotateTask
|
|
err := g.DB().Model(consts.TableAnnotateTask).Ctx(ctx).
|
|
Where("dataset_id", datasetId).
|
|
Where("status", consts.AnnotateTaskPublished).
|
|
OrderDesc("id").Scan(&one)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return one, nil
|
|
}
|
|
|
|
// ListPublished 全部开放任务(App 任务列表)
|
|
func (d *annotateTaskDao) ListPublished(ctx context.Context) ([]*entity.AnnotateTask, error) {
|
|
var list []*entity.AnnotateTask
|
|
err := g.DB().Model(consts.TableAnnotateTask).Ctx(ctx).
|
|
Where("status", consts.AnnotateTaskPublished).
|
|
OrderDesc("id").Scan(&list)
|
|
if err != nil {
|
|
if common.IsNoRows(err) {
|
|
return []*entity.AnnotateTask{}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
// Page 管理端任务分页(datasetId>0 按数据集过滤——详情页 tab 内嵌展示;id 倒序)
|
|
func (d *annotateTaskDao) Page(ctx context.Context, datasetId int64, page, size int) ([]*entity.AnnotateTask, int64, error) {
|
|
base := func() *gdb.Model {
|
|
m := g.DB().Model(consts.TableAnnotateTask).Ctx(ctx)
|
|
if datasetId > 0 {
|
|
m = m.Where("dataset_id", datasetId)
|
|
}
|
|
return m
|
|
}
|
|
total, err := base().Count()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var list []*entity.AnnotateTask
|
|
err = base().OrderDesc("id").Limit((page-1)*size, size).Scan(&list)
|
|
if err != nil {
|
|
if common.IsNoRows(err) {
|
|
return []*entity.AnnotateTask{}, int64(total), nil
|
|
}
|
|
return nil, 0, err
|
|
}
|
|
return list, int64(total), nil
|
|
}
|
|
|
|
// Stop 停用任务(整体不可再领取;已领取未提交的可继续提交)
|
|
func (d *annotateTaskDao) Stop(ctx context.Context, id int64) error {
|
|
_, err := g.DB().Model(consts.TableAnnotateTask).Ctx(ctx).Where("id", id).
|
|
Data(g.Map{"status": consts.AnnotateTaskStopped}).Update()
|
|
return err
|
|
}
|