|
@@ -57,13 +57,13 @@ func DeleteCategory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
}
|
|
|
|
|
|
func CategoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
- page, size, query := UtilQueryPageSize(c)
|
|
|
+ _, _, query := UtilQueryPageSize(c)
|
|
|
return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
CollectName: repo.CollectionCategory,
|
|
|
- Page: page,
|
|
|
- Size: size,
|
|
|
- Query: query,
|
|
|
- Sort: bson.D{bson.E{Key: "sort", Value: 1}, bson.E{Key: "createTime", Value: 1}},
|
|
|
+ // Page: page,
|
|
|
+ // Size: size,
|
|
|
+ Query: query,
|
|
|
+ Sort: bson.D{bson.E{Key: "sort", Value: 1}, bson.E{Key: "createTime", Value: 1}},
|
|
|
})
|
|
|
}
|
|
|
|
|
@@ -87,3 +87,53 @@ func UpdateCategory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
}
|
|
|
return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionCategory, cate.Id.Hex(), &cate)
|
|
|
}
|
|
|
+
|
|
|
+// model模块下
|
|
|
+type Category struct {
|
|
|
+ Id primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
|
|
|
+ Pid string `bson:"pid,omitempty" json:"pid"` // 分类的上层id // 默认为top
|
|
|
+ Name string `bson:"name,omitempty" json:"name"`
|
|
|
+ Sort *int `bson:"sort,omitempty" json:"sort"` // 排序,使用创建时间联合排序。排序按升序排,默认为0
|
|
|
+ Type string `bson:"type,omitempty" json:"type"` // 分类类型,系列/课程/章节/知识点
|
|
|
+ Scope string `bson:"scope,omitempty" json:"scope"` // 分类所属的数据库 模块唯一
|
|
|
+ CreateTime time.Time `bson:"createTime,omitempty" json:"createTime"`
|
|
|
+ UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime"`
|
|
|
+ Children []*Category `bson:"children,omitempty" json:"children"` // 存储树桩结构
|
|
|
+}
|
|
|
+
|
|
|
+func CategoryParseList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ // 查询所有数据
|
|
|
+ cates := []*model.Category{}
|
|
|
+ err := repo.RepoSeachDocs(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionCategory,
|
|
|
+ Query: repo.Map{},
|
|
|
+ Sort: bson.M{"sort": -1, "createTime": 1}, // sort降序排列:大的值排在前面,默认为0 || createTime升序排列:小的值排在前面
|
|
|
+ }, cates)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建一个映射来存储所有的分类
|
|
|
+ cateMap := make(map[string]*model.Category)
|
|
|
+ for _, cate := range cates {
|
|
|
+ cateMap[cate.Id.Hex()] = cate
|
|
|
+ }
|
|
|
+
|
|
|
+ // 结构化处理:返回树状结构
|
|
|
+ for _, cate := range cates {
|
|
|
+ if parent, ok := cateMap[cate.Pid]; ok {
|
|
|
+ parent.Children = append(parent.Children, cate)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从映射中获取顶层分类
|
|
|
+ topCates := []*model.Category{}
|
|
|
+ for _, cate := range cateMap {
|
|
|
+ if cate.Pid == "top" {
|
|
|
+ topCates = append(topCates, cate)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return topCates, nil
|
|
|
+
|
|
|
+}
|