Files
slogan/server/styleagent/dao/outfit_generation_task_dao.go
T
admin a6de9ebd12 Add 'server/' from commit 'e64421295fff83acbb6d6ab3d3b27f3ef8368f00'
git-subtree-dir: server
git-subtree-mainline: c4e617ada7
git-subtree-split: e64421295f
2026-08-04 15:02:35 +08:00

88 lines
3.5 KiB
Go

package dao
import (
"context"
"slogan-agent/styleagent/consts"
"slogan-agent/styleagent/model/entity"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
var OutfitGenTask = &outfitGenTaskDao{}
type outfitGenTaskDao struct{}
func init() {
ctx := context.Background()
_, err := dbPlan().Exec(ctx, `CREATE TABLE IF NOT EXISTS `+consts.TableNameOutfitGenTask+` (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
start_date TEXT NOT NULL DEFAULT '',
end_date TEXT NOT NULL DEFAULT '',
location TEXT NOT NULL DEFAULT '',
weather_snapshot TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL DEFAULT 'pending',
error TEXT NOT NULL DEFAULT '',
model_name TEXT NOT NULL DEFAULT '',
created_at DATETIME DEFAULT (datetime('now','localtime')),
updated_at DATETIME DEFAULT (datetime('now','localtime'))
)`)
if err != nil {
g.Log().Warningf(ctx, "create outfit_generation_task table failed: %v", err)
}
if _, err := dbPlan().Exec(ctx, "CREATE INDEX IF NOT EXISTS idx_slogan_gen_task_user ON "+consts.TableNameOutfitGenTask+"(user_id, created_at)"); err != nil {
g.Log().Warningf(ctx, "create index idx_slogan_gen_task_user failed: %v", err)
}
}
func (d *outfitGenTaskDao) Insert(ctx context.Context, data *entity.OutfitGenerationTask) (int64, error) {
r, err := dbPlan().Exec(ctx,
"INSERT INTO "+consts.TableNameOutfitGenTask+" (user_id, start_date, end_date, location, weather_snapshot, status, error, model_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, datetime('now','localtime'), datetime('now','localtime'))",
data.UserId, data.StartDate, data.EndDate, data.Location, data.WeatherSnapshot, data.Status, data.Error, data.ModelName)
if err != nil {
return 0, err
}
return r.LastInsertId()
}
func (d *outfitGenTaskDao) GetOne(ctx context.Context, id, userId int64) (*entity.OutfitGenerationTask, error) {
var t entity.OutfitGenerationTask
err := dbPlan().Model(consts.TableNameOutfitGenTask).Ctx(ctx).
Where("id", id).Where("user_id", userId).Scan(&t)
if err != nil || t.Id == 0 {
return nil, err
}
return &t, nil
}
func (d *outfitGenTaskDao) Update(ctx context.Context, id int64, data g.Map) error {
_, err := dbPlan().Model(consts.TableNameOutfitGenTask).Ctx(ctx).
Data(data).Where("id", id).Update()
return err
}
func (d *outfitGenTaskDao) UpdateStatus(ctx context.Context, id int64, status, errMsg string) error {
_, err := dbPlan().Model(consts.TableNameOutfitGenTask).Ctx(ctx).Data(g.Map{
"status": status, "error": errMsg, "updated_at": "datetime('now','localtime')",
}).Where("id", id).Update()
return err
}
// UpdateStatusTx 事务版本:方案落库事务内同步任务状态
func (d *outfitGenTaskDao) UpdateStatusTx(ctx context.Context, tx gdb.TX, id int64, status, errMsg string) error {
_, err := tx.Model(consts.TableNameOutfitGenTask).Ctx(ctx).Data(g.Map{
"status": status, "error": errMsg, "updated_at": "datetime('now','localtime')",
}).Where("id", id).Update()
return err
}
// ListUnfinished 返回未完成的任务(重启恢复用)
func (d *outfitGenTaskDao) ListUnfinished(ctx context.Context) ([]*entity.OutfitGenerationTask, error) {
var list []*entity.OutfitGenerationTask
err := dbPlan().Model(consts.TableNameOutfitGenTask).Ctx(ctx).
Where("status NOT IN (?)", g.Slice{consts.TaskStatusDone, consts.TaskStatusFailed}).
OrderAsc("id").Limit(50).Scan(&list)
return list, err
}