git-subtree-dir: server git-subtree-mainline:c4e617ada7git-subtree-split:e64421295f
97 lines
3.6 KiB
Go
97 lines
3.6 KiB
Go
package dao
|
||
|
||
import (
|
||
"context"
|
||
"slogan-agent/styleagent/consts"
|
||
"slogan-agent/styleagent/model/entity"
|
||
"strings"
|
||
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
)
|
||
|
||
var PlanEffectImage = &planEffectImageDao{}
|
||
|
||
type planEffectImageDao struct{}
|
||
|
||
func init() {
|
||
ctx := context.Background()
|
||
_, err := dbPlan().Exec(ctx, `CREATE TABLE IF NOT EXISTS `+consts.TableNamePlanEffectImage+` (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
plan_id INTEGER NOT NULL,
|
||
angle TEXT NOT NULL DEFAULT '',
|
||
url TEXT NOT NULL DEFAULT '',
|
||
status TEXT NOT NULL DEFAULT 'pending',
|
||
prompt_snapshot 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 plan_effect_image table failed: %v", err)
|
||
}
|
||
if _, err := dbPlan().Exec(ctx, "CREATE INDEX IF NOT EXISTS idx_slogan_effect_plan ON "+consts.TableNamePlanEffectImage+"(plan_id)"); err != nil {
|
||
g.Log().Warningf(ctx, "create index idx_slogan_effect_plan failed: %v", err)
|
||
}
|
||
}
|
||
|
||
func (d *planEffectImageDao) Insert(ctx context.Context, data *entity.PlanEffectImage) (int64, error) {
|
||
r, err := dbPlan().Exec(ctx,
|
||
"INSERT INTO "+consts.TableNamePlanEffectImage+" (plan_id, angle, url, status, prompt_snapshot, created_at, updated_at) VALUES (?, ?, ?, ?, ?, datetime('now','localtime'), datetime('now','localtime'))",
|
||
data.PlanId, data.Angle, data.Url, data.Status, data.PromptSnapshot)
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
return r.LastInsertId()
|
||
}
|
||
|
||
// InsertBatch 批量插入(单条 multi-row SQL,缓存命中已生成的记录直接落库)
|
||
func (d *planEffectImageDao) InsertBatch(ctx context.Context, list []*entity.PlanEffectImage) error {
|
||
if len(list) == 0 {
|
||
return nil
|
||
}
|
||
var sb strings.Builder
|
||
sb.WriteString("INSERT INTO " + consts.TableNamePlanEffectImage +
|
||
" (plan_id, angle, url, status, prompt_snapshot, created_at, updated_at) VALUES ")
|
||
args := make([]any, 0, len(list)*5)
|
||
for i, it := range list {
|
||
if i > 0 {
|
||
sb.WriteString(",")
|
||
}
|
||
sb.WriteString("(?,?,?,?,?,datetime('now','localtime'),datetime('now','localtime'))")
|
||
args = append(args, it.PlanId, it.Angle, it.Url, it.Status, it.PromptSnapshot)
|
||
}
|
||
_, err := dbPlan().Exec(ctx, sb.String(), args...)
|
||
return err
|
||
}
|
||
|
||
func (d *planEffectImageDao) ListByPlan(ctx context.Context, planId int64) ([]*entity.PlanEffectImage, error) {
|
||
var list []*entity.PlanEffectImage
|
||
err := dbPlan().Model(consts.TableNamePlanEffectImage).Ctx(ctx).
|
||
Where("plan_id", planId).OrderAsc("id").Scan(&list)
|
||
return list, err
|
||
}
|
||
|
||
// CountByUserToday 统计用户当日已生成的效果图数量(join outfit_plan 拿 user_id)
|
||
func (d *planEffectImageDao) CountByUserToday(ctx context.Context, userId int64) (int, error) {
|
||
n, err := dbPlan().Model(consts.TableNamePlanEffectImage+" p").
|
||
InnerJoin(consts.TableNameOutfitPlan+" o", "p.plan_id = o.id").
|
||
Ctx(ctx).
|
||
Where("o.user_id", userId).
|
||
Where("date(p.created_at) = date('now','localtime')").
|
||
Where("p.status IN (?)", g.Slice{consts.EffectStatusDone, consts.EffectStatusRendering}).
|
||
Count()
|
||
return n, err
|
||
}
|
||
|
||
func (d *planEffectImageDao) UpdateStatus(ctx context.Context, id int64, status, url string) error {
|
||
_, err := dbPlan().Model(consts.TableNamePlanEffectImage).Ctx(ctx).Data(g.Map{
|
||
"status": status, "url": url, "updated_at": "datetime('now','localtime')",
|
||
}).Where("id", id).Update()
|
||
return err
|
||
}
|
||
|
||
func (d *planEffectImageDao) DeleteByPlan(ctx context.Context, planId int64) error {
|
||
_, err := dbPlan().Model(consts.TableNamePlanEffectImage).Ctx(ctx).
|
||
Unscoped().Where("plan_id", planId).Delete()
|
||
return err
|
||
}
|