107 lines
4.2 KiB
Go
107 lines
4.2 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"36wisdom/biz/consts"
|
|
"36wisdom/biz/model/entity"
|
|
"36wisdom/common"
|
|
)
|
|
|
|
type nodeOptionDao struct{ common.BaseDao }
|
|
|
|
var NodeOption = &nodeOptionDao{BaseDao: common.BaseDao{Table: consts.TableNodeOption}}
|
|
|
|
func (d *nodeOptionDao) Init(ctx context.Context) error {
|
|
_, err := g.DB().Exec(ctx, `
|
|
CREATE TABLE IF NOT EXISTS node_option (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
node_id INTEGER NOT NULL,
|
|
text TEXT NOT NULL,
|
|
text_pinyin TEXT,
|
|
prop_id INTEGER,
|
|
audio TEXT,
|
|
next_node_id INTEGER,
|
|
feedback TEXT NOT NULL,
|
|
feedback_pinyin TEXT,
|
|
feedback_pros TEXT, -- 对比点评:好处
|
|
feedback_cons TEXT, -- 对比点评:不足/错过的更好选择
|
|
feedback_audio TEXT,
|
|
sort_order INTEGER NOT NULL DEFAULT 1,
|
|
status INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_option_node ON node_option(node_id);`)
|
|
common.EnsureColumn(ctx, consts.TableNodeOption, "text_pinyin", "TEXT")
|
|
common.EnsureColumn(ctx, consts.TableNodeOption, "feedback_pinyin", "TEXT")
|
|
common.EnsureColumn(ctx, consts.TableNodeOption, "feedback_pros", "TEXT")
|
|
common.EnsureColumn(ctx, consts.TableNodeOption, "feedback_cons", "TEXT")
|
|
return err
|
|
}
|
|
|
|
// GetByPk 主键查询(无缓存);不存在返回 nil, nil。
|
|
func (d *nodeOptionDao) GetByPk(ctx context.Context, id int64) (*entity.NodeOption, error) {
|
|
return common.GetOne[entity.NodeOption](d.Model().Ctx(ctx).WherePri(id))
|
|
}
|
|
|
|
// GetInNodeCached 节点下启用选项(内容缓存);不存在返回 nil, nil。
|
|
func (d *nodeOptionDao) GetInNodeCached(ctx context.Context, optionId, nodeId int64) (*entity.NodeOption, error) {
|
|
return common.GetOne[entity.NodeOption](d.Model().Ctx(ctx).Cache(d.ContentCache(ctx)).
|
|
Where("id", optionId).Where("node_id", nodeId).Where("status", consts.StatusEnabled))
|
|
}
|
|
|
|
// ListEnabledByNodeCached 节点下启用选项(内容缓存),按 sort_order 升序。
|
|
func (d *nodeOptionDao) ListEnabledByNodeCached(ctx context.Context, nodeId int64) ([]*entity.NodeOption, error) {
|
|
return common.GetList[entity.NodeOption](d.Model().Ctx(ctx).Cache(d.ContentCache(ctx)).
|
|
Where("node_id", nodeId).Where("status", consts.StatusEnabled).Order("sort_order ASC"))
|
|
}
|
|
|
|
// ListEnabledByNodeIdsCached 批量节点下启用选项(内容缓存),按 sort_order 升序。
|
|
func (d *nodeOptionDao) ListEnabledByNodeIdsCached(ctx context.Context, nodeIds []int64) ([]*entity.NodeOption, error) {
|
|
if len(nodeIds) == 0 {
|
|
return []*entity.NodeOption{}, nil
|
|
}
|
|
return common.GetList[entity.NodeOption](d.Model().Ctx(ctx).Cache(d.ContentCache(ctx)).
|
|
WhereIn("node_id", nodeIds).Where("status", consts.StatusEnabled).Order("sort_order ASC"))
|
|
}
|
|
|
|
// ListByOptionIds 按选项 id 批量取(终局判定用,不缓存)。
|
|
func (d *nodeOptionDao) ListByOptionIds(ctx context.Context, optionIds []int64) ([]*entity.NodeOption, error) {
|
|
if len(optionIds) == 0 {
|
|
return []*entity.NodeOption{}, nil
|
|
}
|
|
return common.GetList[entity.NodeOption](d.Model().Ctx(ctx).WhereIn("id", optionIds))
|
|
}
|
|
|
|
// ListByNode 节点下全部选项(含下架),按序号排序。
|
|
func (d *nodeOptionDao) ListByNode(ctx context.Context, nodeId int64) ([]*entity.NodeOption, error) {
|
|
return common.GetList[entity.NodeOption](d.Model().Ctx(ctx).Where("node_id", nodeId).Order("sort_order ASC"))
|
|
}
|
|
|
|
// ListByNodeIds 按节点 id 批量取(环检测用,不缓存)。
|
|
func (d *nodeOptionDao) ListByNodeIds(ctx context.Context, nodeIds []int64) ([]*entity.NodeOption, error) {
|
|
if len(nodeIds) == 0 {
|
|
return []*entity.NodeOption{}, nil
|
|
}
|
|
return common.GetList[entity.NodeOption](d.Model().Ctx(ctx).
|
|
WhereIn("node_id", nodeIds).Order("node_id ASC, sort_order ASC"))
|
|
}
|
|
|
|
// CountByNodeIds 各节点选项数(单表 GROUP BY 聚合)。
|
|
func (d *nodeOptionDao) CountByNodeIds(ctx context.Context, nodeIds []int64) (map[int64]int, error) {
|
|
m := make(map[int64]int, len(nodeIds))
|
|
if len(nodeIds) == 0 {
|
|
return m, nil
|
|
}
|
|
recs, err := d.Model().Ctx(ctx).WhereIn("node_id", nodeIds).Group("node_id").
|
|
Fields("node_id, COUNT(*) AS cnt").All()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, r := range recs {
|
|
m[r["node_id"].Int64()] = r["cnt"].Int()
|
|
}
|
|
return m, nil
|
|
}
|