64 lines
2.1 KiB
Go
64 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"
|
||
)
|
||
|
||
// RouteStat 路径流水聚合行(关卡统计用,单表 GROUP BY 结果)。
|
||
type RouteStat struct {
|
||
NodeId int64 `json:"node_id" orm:"node_id"`
|
||
OptionId int64 `json:"option_id" orm:"option_id"`
|
||
ResultType int `json:"result_type" orm:"result_type"`
|
||
Cnt int `json:"cnt" orm:"cnt"`
|
||
}
|
||
|
||
type userRouteLogDao struct{ common.BaseDao }
|
||
|
||
var UserRouteLog = &userRouteLogDao{BaseDao: common.BaseDao{Table: consts.TableUserRouteLog}}
|
||
|
||
func (d *userRouteLogDao) Init(ctx context.Context) error {
|
||
_, err := g.DB().Exec(ctx, `
|
||
CREATE TABLE IF NOT EXISTS user_route_log (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
child_id INTEGER NOT NULL,
|
||
level_id INTEGER NOT NULL,
|
||
node_id INTEGER NOT NULL,
|
||
option_id INTEGER NOT NULL DEFAULT 0,
|
||
result_type INTEGER NOT NULL DEFAULT 0,
|
||
created_at DATETIME
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_route_log_child ON user_route_log(child_id, level_id, created_at);
|
||
CREATE INDEX IF NOT EXISTS idx_route_log_level ON user_route_log(level_id, node_id, option_id);`)
|
||
return err
|
||
}
|
||
|
||
// ListFinalsByChildLevel 孩子某关的终局路径(result_type > 0,按 id 升序,不缓存)。
|
||
func (d *userRouteLogDao) ListFinalsByChildLevel(ctx context.Context, childId, levelId int64) ([]*entity.UserRouteLog, error) {
|
||
return common.GetList[entity.UserRouteLog](d.Model().Ctx(ctx).
|
||
Where("child_id", childId).Where("level_id", levelId).
|
||
WhereGT("result_type", 0).Order("id ASC"))
|
||
}
|
||
|
||
// RouteStatsByLevel 关卡路径流水聚合(node × option × result_type 计数,不缓存)。
|
||
func (d *userRouteLogDao) RouteStatsByLevel(ctx context.Context, levelId int64) ([]*RouteStat, error) {
|
||
recs, err := d.Model().Ctx(ctx).Fields("node_id, option_id, result_type, COUNT(*) AS cnt").
|
||
Where("level_id", levelId).Group("node_id, option_id, result_type").All()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(recs) == 0 {
|
||
return []*RouteStat{}, nil
|
||
}
|
||
var items []*RouteStat
|
||
if err = recs.Structs(&items); err != nil {
|
||
return nil, err
|
||
}
|
||
return items, nil
|
||
}
|