-
Notifications
You must be signed in to change notification settings - Fork 1
/
category.go
73 lines (68 loc) · 2.05 KB
/
category.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package articlelogic
import (
"strconv"
"github.com/admpub/webx/application/dbschema"
"github.com/admpub/webx/application/model/official"
modelArticle "github.com/admpub/webx/application/model/official/article"
"github.com/webx-top/db"
"github.com/webx-top/echo"
)
func GetCategories(c echo.Context, limit int, categoryType ...string) ([]*dbschema.OfficialCommonCategory, error) {
return GetCategoriesWithLevel(c, 0, limit, categoryType...)
}
func GetCategoriesWithLevel(c echo.Context, level int, limit int, categoryType ...string) ([]*dbschema.OfficialCommonCategory, error) {
var ctype string
if len(categoryType) > 0 {
ctype = categoryType[0]
}
if len(ctype) == 0 {
ctype = modelArticle.GroupName
}
cacheKey := `article.getCategories.` + ctype
cates, ok := c.Internal().Get(cacheKey).([]*dbschema.OfficialCommonCategory)
if ok {
return cates, nil
}
cond := db.NewCompounds()
cond.AddKV(`type`, ctype)
cond.AddKV(`level`, level)
cond.AddKV(`disabled`, `N`)
cateM := official.NewCategory(c)
_, err := cateM.List(nil, func(r db.Result) db.Result {
return r.OrderBy(`sort`, `id`)
}, 0, limit, cond.And())
if err != nil {
return nil, err
}
cates = cateM.Objects()
c.Internal().Set(cacheKey, cates)
return cates, nil
}
func GetSubCategories(c echo.Context, parentId int, limit int, categoryType ...string) ([]*dbschema.OfficialCommonCategory, error) {
var ctype string
if len(categoryType) > 0 {
ctype = categoryType[0]
}
if len(ctype) == 0 {
ctype = modelArticle.GroupName
}
cacheKey := `article.getSubCategories.` + ctype + `.` + strconv.Itoa(parentId)
cates, ok := c.Internal().Get(cacheKey).([]*dbschema.OfficialCommonCategory)
if ok {
return cates, nil
}
cond := db.NewCompounds()
cond.AddKV(`type`, ctype)
cond.AddKV(`parent_id`, parentId)
cond.AddKV(`disabled`, `N`)
cateM := official.NewCategory(c)
_, err := cateM.List(nil, func(r db.Result) db.Result {
return r.OrderBy(`sort`, `id`)
}, 0, limit, cond.And())
if err != nil {
return nil, err
}
cates = cateM.Objects()
c.Internal().Set(cacheKey, cates)
return cates, nil
}