forked from TruthHun/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategory.go
More file actions
131 lines (113 loc) · 3.74 KB
/
category.go
File metadata and controls
131 lines (113 loc) · 3.74 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package models
import (
"github.com/astaxie/beego"
"strings"
"github.com/TruthHun/BookStack/models/store"
"github.com/TruthHun/BookStack/utils"
"errors"
"github.com/astaxie/beego/orm"
)
var tableCategory = "md_category"
// 分类
type Category struct {
Id int `json:"id"` //自增主键
Pid int `json:"pid"` //分类id
Title string `orm:"size(30);unique" json:"title,omitempty"` //分类名称
Intro string `json:"intro,omitempty"` //介绍
Icon string `json:"icon,omitempty"` //分类icon
Cnt int `json:"cnt,omitempty"` //分类下的文档项目统计
Sort int `json:"sort,omitempty"` //排序
Status bool `json:"status,omitempty"` //分类状态,true表示显示,否则表示隐藏
//PrintBookCount int `orm:"default(0)" json:"print_book_count"`
//WikiCount int `orm:"default(0)" json:"wiki_count"`
//ArticleCount int `orm:"default(0)" json:"article_count"`
}
func NewCategory() *Category {
return &Category{}
}
//新增分类
func (this *Category) AddCates(pid int, cates string) (err error) {
slice := strings.Split(cates, "\n")
if len(slice) == 0 {
return
}
o := orm.NewOrm()
for _, item := range slice {
if item = strings.TrimSpace(item); item != "" {
var cate = Category{
Pid: pid,
Title: item,
Status: true,
}
if o.Read(&cate, "title"); cate.Id == 0 {
_, err = orm.NewOrm().Insert(&cate)
}
}
}
return
}
//删除分类(如果分类下的文档项目不为0,则不允许删除)
func (this *Category) Del(id int) (err error) {
var cate = Category{Id: id}
o := orm.NewOrm()
if err = o.Read(&cate); cate.Cnt > 0 { //当前分类下文档项目数量不为0,不允许删除
return errors.New("删除失败,当前分类下的问下项目不为0,不允许删除")
}
if _, err = o.Delete(&cate, "id"); err != nil {
return
}
_, err = o.QueryTable(tableCategory).Filter("pid", id).Delete()
if err != nil { //删除分类图标
return
}
switch utils.StoreType {
case utils.StoreOss:
store.ModelStoreOss.DelFromOss(cate.Icon)
case utils.StoreLocal:
store.ModelStoreLocal.DelFiles(cate.Icon)
}
return
}
//查询所有分类
//@param pid -1表示不限(即查询全部),否则表示查询指定pid的分类
//@param status -1表示不限状态(即查询所有状态的分类),0表示关闭状态,1表示启用状态
func (this *Category) GetCates(pid int, status int) (cates []Category, err error) {
qs := orm.NewOrm().QueryTable(tableCategory)
if pid > -1 {
qs = qs.Filter("pid", pid)
}
if status == 0 || status == 1 {
qs = qs.Filter("status", status)
}
_, err = qs.OrderBy("-status", "sort", "title").All(&cates)
return
}
//根据字段更新内容
func (this *Category) UpdateByField(id int, field, val string) (err error) {
_, err = orm.NewOrm().QueryTable(tableCategory).Filter("id", id).Update(orm.Params{field: val})
return
}
//查询单个分类
func (this *Category) Find(id int) (cate Category) {
cate.Id = id
orm.NewOrm().Read(&cate)
return cate
}
// 用户收藏了的书籍的分类
func (m *Category) CategoryOfUserCollection(uid int, forAPI ...bool) (cates []Category) {
order := " ORDER BY c.sort asc,c.title asc "
if len(forAPI) > 0 && forAPI[0] {
order = " ORDER BY c.title asc "
}
sql := `
SELECT c.id,c.pid,c.title
FROM md_book_category bc
LEFT JOIN md_star s ON s.bid = bc.book_id
LEFT JOIN md_category c ON c.id = bc.category_id
WHERE s.uid = ?
GROUP BY c.id ` + order
if _, err := orm.NewOrm().Raw(sql, uid).QueryRows(&cates); err != nil {
beego.Error(err.Error())
}
return
}