2017-07-07 16:20:55 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
2021-03-26 11:34:02 +08:00
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-24 00:28:13 +08:00
|
|
|
"github.com/beego/beego/v2/core/logs"
|
2021-03-23 15:09:17 +08:00
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
2017-07-07 16:20:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Label struct {
|
2022-10-28 12:33:36 +08:00
|
|
|
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"`
|
2017-07-07 16:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableName 获取对应数据库表名.
|
|
|
|
func (m *Label) TableName() string {
|
|
|
|
return "label"
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
2017-07-07 16:20:55 +08:00
|
|
|
// TableEngine 获取数据使用的引擎.
|
|
|
|
func (m *Label) TableEngine() string {
|
|
|
|
return "INNODB"
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (m *Label) TableNameWithPrefix() string {
|
|
|
|
return conf.GetDatabasePrefix() + m.TableName()
|
2017-07-07 16:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLabel() *Label {
|
|
|
|
return &Label{}
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (m *Label) FindFirst(field string, value interface{}) (*Label, error) {
|
2017-07-07 16:20:55 +08:00
|
|
|
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()
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("label_name", labelName).One(m)
|
2017-07-07 16:20:55 +08:00
|
|
|
if err != nil && err != orm.ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
count, _ := o.QueryTable(NewBook().TableNameWithPrefix()).Filter("label__icontains", labelName).Count()
|
2017-07-07 16:20:55 +08:00
|
|
|
m.BookNumber = int(count)
|
|
|
|
m.LabelName = labelName
|
|
|
|
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
err = nil
|
|
|
|
m.LabelName = labelName
|
2018-01-26 17:17:38 +08:00
|
|
|
_, err = o.Insert(m)
|
|
|
|
} else {
|
|
|
|
_, err = o.Update(m)
|
2017-07-07 16:20:55 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//批量插入或更新标签.
|
2018-01-26 17:17:38 +08:00
|
|
|
func (m *Label) InsertOrUpdateMulti(labels string) {
|
|
|
|
if labels != "" {
|
2017-07-07 16:20:55 +08:00
|
|
|
labelArray := strings.Split(labels, ",")
|
|
|
|
|
|
|
|
for _, label := range labelArray {
|
|
|
|
if label != "" {
|
|
|
|
NewLabel().InsertOrUpdate(label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 21:55:50 +08:00
|
|
|
|
2018-02-28 15:47:00 +08:00
|
|
|
//删除标签
|
|
|
|
func (m *Label) Delete() error {
|
|
|
|
o := orm.NewOrm()
|
2021-03-23 21:55:50 +08:00
|
|
|
_, err := o.Raw("DELETE FROM "+m.TableNameWithPrefix()+" WHERE label_id= ?", m.LabelId).Exec()
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-07 16:20:55 +08:00
|
|
|
|
|
|
|
//分页查找标签.
|
2018-01-26 17:17:38 +08:00
|
|
|
func (m *Label) FindToPager(pageIndex, pageSize int) (labels []*Label, totalCount int, err error) {
|
2017-07-07 16:20:55 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
count, err := o.QueryTable(m.TableNameWithPrefix()).Count()
|
2017-07-07 16:20:55 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
totalCount = int(count)
|
|
|
|
|
|
|
|
offset := (pageIndex - 1) * pageSize
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
_, err = o.QueryTable(m.TableNameWithPrefix()).OrderBy("-book_number").Offset(offset).Limit(pageSize).All(&labels)
|
2017-07-07 16:20:55 +08:00
|
|
|
|
2018-08-07 17:19:56 +08:00
|
|
|
if err == orm.ErrNoRows {
|
2021-03-23 21:55:50 +08:00
|
|
|
logs.Info("没有查询到标签 ->", err)
|
2018-08-07 17:19:56 +08:00
|
|
|
err = nil
|
|
|
|
return
|
|
|
|
}
|
2017-07-07 16:20:55 +08:00
|
|
|
return
|
|
|
|
}
|