Files
slogan/server/styleagent/dao/cps_category_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

58 lines
1.8 KiB
Go

package dao
import (
"context"
"slogan-agent/styleagent/consts"
"slogan-agent/styleagent/model/entity"
"github.com/gogf/gf/v2/frame/g"
)
var CpsCategory = &cpsCategoryDao{}
type cpsCategoryDao struct{}
func init() {
ctx := context.Background()
_, err := dbCps().Exec(ctx, `CREATE TABLE IF NOT EXISTS `+consts.TableNameCpsCategory+` (
id INTEGER PRIMARY KEY AUTOINCREMENT,
code TEXT NOT NULL UNIQUE,
name TEXT NOT NULL DEFAULT '',
parent_code TEXT NOT NULL DEFAULT '',
source TEXT NOT NULL DEFAULT '',
source_cat_id TEXT NOT NULL DEFAULT '',
sort INTEGER NOT NULL DEFAULT 0,
created_at DATETIME DEFAULT (datetime('now','localtime'))
)`)
if err != nil {
g.Log().Warningf(ctx, "create cps_category table failed: %v", err)
}
seedCpsCategories(ctx)
}
func seedCpsCategories(ctx context.Context) {
base := []struct{ code, name, source, sourceCatId string }{
{"beauty", "丽人", consts.CpsSourceMeituanOta, ""},
{"clothing", "服装", consts.CpsSourceMeituanOta, ""},
{"food", "餐厅", consts.CpsSourceMeituanOta, ""},
{"hotel", "酒店", consts.CpsSourceMeituanOta, ""},
{"ticket", "票务", consts.CpsSourceMeituanOta, ""},
{"digital", "数码", consts.CpsSourceJdEcom, ""},
}
for i, c := range base {
if _, err := dbCps().Exec(ctx,
"INSERT OR IGNORE INTO "+consts.TableNameCpsCategory+
" (code, name, parent_code, source, source_cat_id, sort) VALUES (?, ?, '', ?, ?, ?)",
c.code, c.name, c.source, c.sourceCatId, i); err != nil {
g.Log().Warningf(ctx, "seed cps_category %s failed: %v", c.code, err)
}
}
}
func (d *cpsCategoryDao) List(ctx context.Context) ([]*entity.CpsCategory, error) {
var list []*entity.CpsCategory
err := dbCps().Model(consts.TableNameCpsCategory).Ctx(ctx).
OrderAsc("sort").OrderAsc("id").Scan(&list)
return list, err
}