/
service_categorylist.go
executable file
·38 lines (35 loc) · 1.07 KB
/
service_categorylist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package site
import (
"github.com/ch3nnn/webstack-go/internal/pkg/core"
"github.com/ch3nnn/webstack-go/internal/repository/mysql"
"github.com/ch3nnn/webstack-go/internal/repository/mysql/category"
"sort"
)
func (s *service) CategoryList(ctx core.Context) (listData []*category.Category, err error) {
qb := category.NewQueryBuilder()
parentIds := qb.GroupByParentId(s.db.GetDbR().WithContext(ctx.RequestContext()))
// 一级分类
qb1 := category.NewQueryBuilder()
categories01, err := qb1.
WhereParentIdIn(parentIds).
WhereParentId(mysql.NotEqualPredicate, 0).
QueryAll(s.db.GetDbR().WithContext(ctx.RequestContext()))
if err != nil {
return nil, err
}
// 二级分类
qb2 := category.NewQueryBuilder()
categories02, err := qb2.
WhereIdNotIn(parentIds).
WhereParentId(mysql.EqualPredicate, 0).
QueryAll(s.db.GetDbR().WithContext(ctx.RequestContext()))
if err != nil {
return nil, err
}
listData = append(categories01, categories02...)
// 按分类升序
sort.Slice(listData, func(i, j int) bool {
return listData[i].Sort < listData[j].Sort
})
return
}