forked from mindoc-org/mindoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabelModel.go
110 lines (87 loc) · 2.5 KB
/
LabelModel.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
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
package models
import (
"strings"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/logs"
"github.com/mindoc-org/mindoc/conf"
)
type Label struct {
LabelId int `orm:"column(label_id);pk;auto;unique;description(项目标签id)" json:"label_id"`
LabelName string `orm:"column(label_name);size(50);unique;description(项目标签名称)" json:"label_name"`
BookNumber int `orm:"column(book_number);description(包涵项目数量)" json:"book_number"`
}
// TableName 获取对应数据库表名.
func (m *Label) TableName() string {
return "label"
}
// TableEngine 获取数据使用的引擎.
func (m *Label) TableEngine() string {
return "INNODB"
}
func (m *Label) TableNameWithPrefix() string {
return conf.GetDatabasePrefix() + m.TableName()
}
func NewLabel() *Label {
return &Label{}
}
func (m *Label) FindFirst(field string, value interface{}) (*Label, error) {
o := orm.NewOrm()
err := o.QueryTable(m.TableNameWithPrefix()).Filter(field, value).One(m)
return m, err
}
//插入或更新标签.
func (m *Label) InsertOrUpdate(labelName string) error {
o := orm.NewOrm()
err := o.QueryTable(m.TableNameWithPrefix()).Filter("label_name", labelName).One(m)
if err != nil && err != orm.ErrNoRows {
return err
}
count, _ := o.QueryTable(NewBook().TableNameWithPrefix()).Filter("label__icontains", labelName).Count()
m.BookNumber = int(count)
m.LabelName = labelName
if err == orm.ErrNoRows {
err = nil
m.LabelName = labelName
_, err = o.Insert(m)
} else {
_, err = o.Update(m)
}
return err
}
//批量插入或更新标签.
func (m *Label) InsertOrUpdateMulti(labels string) {
if labels != "" {
labelArray := strings.Split(labels, ",")
for _, label := range labelArray {
if label != "" {
NewLabel().InsertOrUpdate(label)
}
}
}
}
//删除标签
func (m *Label) Delete() error {
o := orm.NewOrm()
_, err := o.Raw("DELETE FROM "+m.TableNameWithPrefix()+" WHERE label_id= ?", m.LabelId).Exec()
if err != nil {
return err
}
return nil
}
//分页查找标签.
func (m *Label) FindToPager(pageIndex, pageSize int) (labels []*Label, totalCount int, err error) {
o := orm.NewOrm()
count, err := o.QueryTable(m.TableNameWithPrefix()).Count()
if err != nil {
return
}
totalCount = int(count)
offset := (pageIndex - 1) * pageSize
_, err = o.QueryTable(m.TableNameWithPrefix()).OrderBy("-book_number").Offset(offset).Limit(pageSize).All(&labels)
if err == orm.ErrNoRows {
logs.Info("没有查询到标签 ->", err)
err = nil
return
}
return
}