67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"36wisdom/biz/consts"
|
|
"36wisdom/biz/model/entity"
|
|
"36wisdom/common"
|
|
)
|
|
|
|
type elementDao struct{ common.BaseDao }
|
|
|
|
var Element = &elementDao{BaseDao: common.BaseDao{Table: consts.TableElement}}
|
|
|
|
func (d *elementDao) Init(ctx context.Context) error {
|
|
_, err := g.DB().Exec(ctx, `
|
|
CREATE TABLE IF NOT EXISTS element (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
e_type INTEGER NOT NULL,
|
|
name TEXT NOT NULL,
|
|
name_pinyin TEXT,
|
|
image TEXT,
|
|
audio TEXT,
|
|
description TEXT,
|
|
description_pinyin TEXT,
|
|
status INTEGER NOT NULL DEFAULT 1,
|
|
sort_order INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_element_type ON element(e_type);`)
|
|
common.EnsureColumn(ctx, consts.TableElement, "name_pinyin", "TEXT")
|
|
common.EnsureColumn(ctx, consts.TableElement, "description_pinyin", "TEXT")
|
|
return err
|
|
}
|
|
|
|
// GetByPk 主键查询(无缓存);不存在返回 nil, nil。
|
|
func (d *elementDao) GetByPk(ctx context.Context, id int64) (*entity.Element, error) {
|
|
return common.GetOne[entity.Element](d.Model().Ctx(ctx).WherePri(id))
|
|
}
|
|
|
|
// ListEnabledByIdsCached 按 id 批量取启用元素(内容缓存)。
|
|
func (d *elementDao) ListEnabledByIdsCached(ctx context.Context, ids []int64) ([]*entity.Element, error) {
|
|
if len(ids) == 0 {
|
|
return []*entity.Element{}, nil
|
|
}
|
|
return common.GetList[entity.Element](d.Model().Ctx(ctx).Cache(d.ContentCache(ctx)).
|
|
WhereIn("id", ids).Where("status", consts.StatusEnabled))
|
|
}
|
|
|
|
// ListAll 元素列表(eType=0 全部),按类型 + 序号排序。
|
|
func (d *elementDao) ListAll(ctx context.Context, eType int) ([]*entity.Element, error) {
|
|
m := d.Model().Ctx(ctx)
|
|
if eType > 0 {
|
|
m = m.Where("e_type", eType)
|
|
}
|
|
return common.GetList[entity.Element](m.Order("e_type ASC, sort_order ASC"))
|
|
}
|
|
|
|
// ListByIds 按 id 批量取(含下架,无缓存)。
|
|
func (d *elementDao) ListByIds(ctx context.Context, ids []int64) ([]*entity.Element, error) {
|
|
if len(ids) == 0 {
|
|
return []*entity.Element{}, nil
|
|
}
|
|
return common.GetList[entity.Element](d.Model().Ctx(ctx).WhereIn("id", ids))
|
|
}
|