82 lines
3.3 KiB
Go
82 lines
3.3 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"36wisdom/biz/consts"
|
|
"36wisdom/biz/model/entity"
|
|
"36wisdom/common"
|
|
)
|
|
|
|
type strategyDao struct{ common.BaseDao }
|
|
|
|
var Strategy = &strategyDao{BaseDao: common.BaseDao{Table: consts.TableStrategy}}
|
|
|
|
func (d *strategyDao) Init(ctx context.Context) error {
|
|
_, err := g.DB().Exec(ctx, `
|
|
CREATE TABLE IF NOT EXISTS strategy (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
pinyin TEXT NOT NULL,
|
|
group_no INTEGER NOT NULL,
|
|
group_name TEXT NOT NULL,
|
|
meaning TEXT NOT NULL, -- 儿童语言释义
|
|
meaning_pinyin TEXT, -- 释义拼音(逐字对齐 JSON)
|
|
teach_content TEXT, -- 计策学堂:儿童化讲解(名称/释义/使用时机)
|
|
teach_content_pinyin TEXT,
|
|
teach_image TEXT,
|
|
teach_audio TEXT,
|
|
summary_q TEXT, -- 智慧总结反思题
|
|
summary_q_pinyin TEXT,
|
|
summary_options TEXT, -- 反思题选项 JSON(含正确答案)
|
|
summary_options_pinyin TEXT,
|
|
summary_audio TEXT,
|
|
icon TEXT,
|
|
sort_order INTEGER NOT NULL,
|
|
unlock_before INTEGER,
|
|
status INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_strategy_group ON strategy(group_no, sort_order);`)
|
|
common.EnsureColumn(ctx, consts.TableStrategy, "meaning_pinyin", "TEXT")
|
|
common.EnsureColumn(ctx, consts.TableStrategy, "teach_content_pinyin", "TEXT")
|
|
common.EnsureColumn(ctx, consts.TableStrategy, "summary_q_pinyin", "TEXT")
|
|
common.EnsureColumn(ctx, consts.TableStrategy, "summary_options_pinyin", "TEXT")
|
|
return err
|
|
}
|
|
|
|
// GetByPk 主键查询(无缓存);不存在返回 nil, nil。
|
|
func (d *strategyDao) GetByPk(ctx context.Context, id int64) (*entity.Strategy, error) {
|
|
return common.GetOne[entity.Strategy](d.Model().Ctx(ctx).WherePri(id))
|
|
}
|
|
|
|
// GetByPkCached 主键查询(内容缓存);不存在返回 nil, nil。
|
|
func (d *strategyDao) GetByPkCached(ctx context.Context, id int64) (*entity.Strategy, error) {
|
|
return common.GetOne[entity.Strategy](d.Model().Ctx(ctx).Cache(d.ContentCache(ctx)).WherePri(id))
|
|
}
|
|
|
|
// ListEnabled 启用计策(内容缓存),按分组 + 组内序号排序。
|
|
func (d *strategyDao) ListEnabled(ctx context.Context) ([]*entity.Strategy, error) {
|
|
return common.GetList[entity.Strategy](d.Model().Ctx(ctx).Cache(d.ContentCache(ctx)).
|
|
Where("status", consts.StatusEnabled).Order("group_no ASC, sort_order ASC"))
|
|
}
|
|
|
|
// ListAll 全部计策(含下架),按分组 + 组内序号排序。
|
|
func (d *strategyDao) ListAll(ctx context.Context) ([]*entity.Strategy, error) {
|
|
return common.GetList[entity.Strategy](d.Model().Ctx(ctx).Order("group_no ASC, sort_order ASC"))
|
|
}
|
|
|
|
// ListByIds 按 id 批量取(无缓存)。
|
|
func (d *strategyDao) ListByIds(ctx context.Context, ids []int64) ([]*entity.Strategy, error) {
|
|
if len(ids) == 0 {
|
|
return []*entity.Strategy{}, nil
|
|
}
|
|
return common.GetList[entity.Strategy](d.Model().Ctx(ctx).WhereIn("id", ids))
|
|
}
|
|
|
|
// GetByUnlockBefore 按解锁前置计策反查下一计(内容缓存);不存在返回 nil, nil。
|
|
func (d *strategyDao) GetByUnlockBefore(ctx context.Context, strategyId int64) (*entity.Strategy, error) {
|
|
return common.GetOne[entity.Strategy](d.Model().Ctx(ctx).Cache(d.ContentCache(ctx)).Where("unlock_before", strategyId))
|
|
}
|