85 lines
3.1 KiB
Go
85 lines
3.1 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"slogan-agent/styleagent/consts"
|
|
"slogan-agent/styleagent/model/entity"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
var WardrobeItem = &wardrobeItemDao{}
|
|
|
|
type wardrobeItemDao struct{}
|
|
|
|
func init() {
|
|
ctx := context.Background()
|
|
_, err := g.DB().Exec(ctx, `CREATE TABLE IF NOT EXISTS `+consts.TableNameWardrobeItem+` (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
photo_url TEXT NOT NULL DEFAULT '',
|
|
category TEXT NOT NULL DEFAULT '',
|
|
season TEXT NOT NULL DEFAULT '四季',
|
|
style_tags TEXT NOT NULL DEFAULT '',
|
|
color_info TEXT NOT NULL DEFAULT '',
|
|
status INTEGER NOT NULL DEFAULT 1,
|
|
created_at DATETIME DEFAULT (datetime('now','localtime'))
|
|
)`)
|
|
if err != nil {
|
|
g.Log().Warningf(ctx, "create wardrobe_item table failed: %v", err)
|
|
}
|
|
// 容错迁移:CREATE TABLE IF NOT EXISTS 不给旧库加列,duplicate column 错误可忽略
|
|
if _, err := g.DB().Exec(ctx, "ALTER TABLE "+consts.TableNameWardrobeItem+
|
|
" ADD COLUMN name TEXT NOT NULL DEFAULT ''"); err != nil {
|
|
g.Log().Warningf(ctx, "migrate wardrobe_item.name skipped: %v", err)
|
|
}
|
|
_, _ = g.DB().Exec(ctx, "CREATE INDEX IF NOT EXISTS idx_slogan_wardrobe_user ON "+consts.TableNameWardrobeItem+"(user_id, category)")
|
|
}
|
|
|
|
func (d *wardrobeItemDao) Insert(ctx context.Context, data *entity.WardrobeItem) (int64, error) {
|
|
r, err := g.DB().Exec(ctx,
|
|
"INSERT INTO "+consts.TableNameWardrobeItem+" (user_id, photo_url, name, category, season, style_tags, color_info, status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, datetime('now','localtime'))",
|
|
data.UserId, data.PhotoUrl, data.Name, data.Category, data.Season, data.StyleTags, data.ColorInfo, data.Status)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
func (d *wardrobeItemDao) ListByUser(ctx context.Context, userId int64, category string) ([]*entity.WardrobeItem, error) {
|
|
m := g.DB().Model(consts.TableNameWardrobeItem).Ctx(ctx).Where("user_id", userId).Where("status", 1)
|
|
if category != "" {
|
|
m = m.Where("category", category)
|
|
}
|
|
var list []*entity.WardrobeItem
|
|
err := m.OrderAsc("id").Scan(&list)
|
|
return list, err
|
|
}
|
|
|
|
func (d *wardrobeItemDao) ListAllByUser(ctx context.Context, userId int64) ([]*entity.WardrobeItem, error) {
|
|
var list []*entity.WardrobeItem
|
|
err := g.DB().Model(consts.TableNameWardrobeItem).Ctx(ctx).
|
|
Where("user_id", userId).Where("status", 1).OrderAsc("id").Scan(&list)
|
|
return list, err
|
|
}
|
|
|
|
func (d *wardrobeItemDao) GetOne(ctx context.Context, id, userId int64) (*entity.WardrobeItem, error) {
|
|
var w entity.WardrobeItem
|
|
err := g.DB().Model(consts.TableNameWardrobeItem).Ctx(ctx).
|
|
Where("id", id).Where("user_id", userId).Scan(&w)
|
|
if err != nil || w.Id == 0 {
|
|
return nil, err
|
|
}
|
|
return &w, nil
|
|
}
|
|
|
|
func (d *wardrobeItemDao) Update(ctx context.Context, id int64, data map[string]any) error {
|
|
_, err := g.DB().Model(consts.TableNameWardrobeItem).Ctx(ctx).Data(data).Where("id", id).Update()
|
|
return err
|
|
}
|
|
|
|
func (d *wardrobeItemDao) Delete(ctx context.Context, id int64) error {
|
|
_, err := g.DB().Model(consts.TableNameWardrobeItem).Ctx(ctx).Unscoped().Where("id", id).Delete()
|
|
return err
|
|
}
|