133 lines
5.0 KiB
Go
133 lines
5.0 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"slogan-agent/common"
|
|
"slogan-agent/styleagent/consts"
|
|
"slogan-agent/styleagent/model/entity"
|
|
"strings"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
var PlanEffectImage = &planEffectImageDao{}
|
|
|
|
type planEffectImageDao struct{}
|
|
|
|
func init() {
|
|
ctx := context.Background()
|
|
_, err := common.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 := common.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 := common.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
|
|
}
|
|
common.CacheClear(ctx, common.DbPlan(), consts.TableNamePlanEffectImage)
|
|
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 := common.DbPlan().Exec(ctx, sb.String(), args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
common.CacheClear(ctx, common.DbPlan(), consts.TableNamePlanEffectImage)
|
|
return nil
|
|
}
|
|
|
|
func (d *planEffectImageDao) ListByPlan(ctx context.Context, planId int64) ([]*entity.PlanEffectImage, error) {
|
|
var list []*entity.PlanEffectImage
|
|
err := common.DbPlan().Model(consts.TableNamePlanEffectImage).Ctx(ctx).
|
|
Cache(gdb.CacheOption{Duration: common.CacheTTL(), Name: common.CacheName(consts.TableNamePlanEffectImage, "ListByPlan", planId)}).
|
|
Where("plan_id", planId).OrderAsc("id").Scan(&list)
|
|
return list, err
|
|
}
|
|
|
|
// CountByUserToday 统计用户当日已生成的效果图数量
|
|
// (先取用户方案 id 列表,再对效果图单表 IN 统计,拆两条单表 SQL,禁 JOIN)
|
|
func (d *planEffectImageDao) CountByUserToday(ctx context.Context, userId int64) (int, error) {
|
|
var planIds []int64
|
|
if err := common.DbPlan().Model(consts.TableNameOutfitPlan).Ctx(ctx).
|
|
Cache(gdb.CacheOption{Duration: common.CacheTTL(), Name: common.CacheName(consts.TableNameOutfitPlan, "CountByUserToday", userId)}).
|
|
Where("user_id", userId).Fields("id").Scan(&planIds); err != nil {
|
|
return 0, err
|
|
}
|
|
if len(planIds) == 0 {
|
|
return 0, nil
|
|
}
|
|
total := 0
|
|
for start := 0; start < len(planIds); start += 100 {
|
|
end := start + 100
|
|
if end > len(planIds) {
|
|
end = len(planIds)
|
|
}
|
|
n, err := common.DbPlan().Model(consts.TableNamePlanEffectImage).Ctx(ctx).
|
|
Cache(gdb.CacheOption{Duration: common.CacheTTL(), Name: common.CacheName(consts.TableNamePlanEffectImage, "CountByUserToday", userId, planIds[start:end])}).
|
|
WhereIn("plan_id", planIds[start:end]).
|
|
Where("date(created_at) = date('now','localtime')").
|
|
WhereIn("status", g.Slice{consts.EffectStatusDone, consts.EffectStatusRendering}).
|
|
Count()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
total += n
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
func (d *planEffectImageDao) UpdateStatus(ctx context.Context, id int64, status, url string) error {
|
|
_, err := common.DbPlan().Model(consts.TableNamePlanEffectImage).Ctx(ctx).Data(g.Map{
|
|
"status": status, "url": url, "updated_at": "datetime('now','localtime')",
|
|
}).Where("id", id).Update()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
common.CacheClear(ctx, common.DbPlan(), consts.TableNamePlanEffectImage)
|
|
return nil
|
|
}
|
|
|
|
func (d *planEffectImageDao) DeleteByPlan(ctx context.Context, planId int64) error {
|
|
_, err := common.DbPlan().Model(consts.TableNamePlanEffectImage).Ctx(ctx).
|
|
Unscoped().Where("plan_id", planId).Delete()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
common.CacheClear(ctx, common.DbPlan(), consts.TableNamePlanEffectImage)
|
|
return nil
|
|
}
|