2018-08-13 19:05:49 +08:00
|
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"errors"
|
2018-08-13 19:05:49 +08:00
|
|
|
|
"time"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
|
|
|
|
|
"github.com/astaxie/beego/logs"
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
2018-08-13 19:05:49 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Template struct {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
TemplateId int `orm:"column(template_id);pk;auto;unique;" json:"template_id"`
|
|
|
|
|
TemplateName string `orm:"column(template_name);size(500);" json:"template_name"`
|
|
|
|
|
MemberId int `orm:"column(member_id);index" json:"member_id"`
|
|
|
|
|
BookId int `orm:"column(book_id);index" json:"book_id"`
|
|
|
|
|
BookName string `orm:"-" json:"book_name"`
|
2018-08-13 19:05:49 +08:00
|
|
|
|
//是否是全局模板:0 否/1 是; 全局模板在所有项目中都可以使用;否则只能在创建模板的项目中使用
|
2021-03-30 14:24:14 +08:00
|
|
|
|
IsGlobal int `orm:"column(is_global);default(0)" json:"is_global"`
|
|
|
|
|
TemplateContent string `orm:"column(template_content);type(text);null" json:"template_content"`
|
|
|
|
|
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
|
|
|
|
|
CreateName string `orm:"-" json:"create_name"`
|
|
|
|
|
ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
|
|
|
|
|
ModifyAt int `orm:"column(modify_at);type(int)" json:"-"`
|
|
|
|
|
ModifyName string `orm:"-" json:"modify_name"`
|
|
|
|
|
Version int64 `orm:"type(bigint);column(version)" json:"version"`
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName 获取对应数据库表名.
|
|
|
|
|
func (m *Template) TableName() string {
|
|
|
|
|
return "templates"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableEngine 获取数据使用的引擎.
|
|
|
|
|
func (m *Template) TableEngine() string {
|
|
|
|
|
return "INNODB"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Template) TableNameWithPrefix() string {
|
|
|
|
|
return conf.GetDatabasePrefix() + m.TableName()
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 14:24:14 +08:00
|
|
|
|
func NewTemplate() *Template {
|
2018-08-13 19:05:49 +08:00
|
|
|
|
return &Template{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//查询指定ID的模板
|
2021-03-30 14:24:14 +08:00
|
|
|
|
func (t *Template) Find(templateId int) (*Template, error) {
|
2018-08-13 19:05:49 +08:00
|
|
|
|
if templateId <= 0 {
|
|
|
|
|
return t, ErrInvalidParameter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2021-03-30 14:24:14 +08:00
|
|
|
|
err := o.QueryTable(t.TableNameWithPrefix()).Filter("template_id", templateId).One(t)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("查询模板时失败 ->%s", err)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
return t, err
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//查询属于指定项目的模板.
|
2021-03-30 14:24:14 +08:00
|
|
|
|
func (t *Template) FindByBookId(bookId int) ([]*Template, error) {
|
2018-08-13 19:05:49 +08:00
|
|
|
|
if bookId <= 0 {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
return nil, ErrInvalidParameter
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
|
|
var templateList []*Template
|
|
|
|
|
|
2021-03-30 14:24:14 +08:00
|
|
|
|
_, err := o.QueryTable(t.TableNameWithPrefix()).Filter("book_id", bookId).OrderBy("-template_id").All(&templateList)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("查询模板列表失败 ->", err)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
return templateList, err
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//查询指定项目所有可用模板列表.
|
2021-03-30 14:24:14 +08:00
|
|
|
|
func (t *Template) FindAllByBookId(bookId int) ([]*Template, error) {
|
2018-08-13 19:05:49 +08:00
|
|
|
|
if bookId <= 0 {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
return nil, ErrInvalidParameter
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
|
|
cond := orm.NewCondition()
|
|
|
|
|
|
2021-03-30 14:24:14 +08:00
|
|
|
|
cond1 := cond.And("book_id", bookId).Or("is_global", 1)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
|
|
|
|
|
qs := o.QueryTable(t.TableNameWithPrefix())
|
|
|
|
|
|
|
|
|
|
var templateList []*Template
|
|
|
|
|
|
2021-03-30 14:24:14 +08:00
|
|
|
|
_, err := qs.SetCond(cond1).OrderBy("-template_id").All(&templateList)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("查询模板列表失败 ->", err)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
return templateList, err
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
|
2018-08-13 19:05:49 +08:00
|
|
|
|
//删除一个模板
|
2021-03-30 14:24:14 +08:00
|
|
|
|
func (t *Template) Delete(templateId int, memberId int) error {
|
2018-08-13 19:05:49 +08:00
|
|
|
|
if templateId <= 0 {
|
|
|
|
|
return ErrInvalidParameter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2021-03-30 14:24:14 +08:00
|
|
|
|
qs := o.QueryTable(t.TableNameWithPrefix()).Filter("template_id", templateId)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
|
|
|
|
|
if memberId > 0 {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
qs = qs.Filter("member_id", memberId)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
_, err := qs.Delete()
|
2018-08-13 19:05:49 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("删除模板失败 ->", err)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//添加或更新模板
|
|
|
|
|
func (t *Template) Save(cols ...string) (err error) {
|
|
|
|
|
|
|
|
|
|
if t.BookId <= 0 {
|
|
|
|
|
return ErrInvalidParameter
|
|
|
|
|
}
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2021-03-30 14:24:14 +08:00
|
|
|
|
if !o.QueryTable(NewBook().TableNameWithPrefix()).Filter("book_id", t.BookId).Exist() {
|
2018-08-13 19:05:49 +08:00
|
|
|
|
return errors.New("项目不存在")
|
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
if !o.QueryTable(NewMember().TableNameWithPrefix()).Filter("member_id", t.MemberId).Filter("status", 0).Exist() {
|
2018-08-13 19:05:49 +08:00
|
|
|
|
return errors.New("用户已被禁用")
|
|
|
|
|
}
|
2018-08-14 15:57:52 +08:00
|
|
|
|
t.Version = time.Now().Unix()
|
|
|
|
|
|
2018-08-13 19:05:49 +08:00
|
|
|
|
if t.TemplateId > 0 {
|
|
|
|
|
t.ModifyTime = time.Now()
|
2021-03-30 14:24:14 +08:00
|
|
|
|
_, err = o.Update(t, cols...)
|
|
|
|
|
} else {
|
2018-08-13 19:05:49 +08:00
|
|
|
|
t.CreateTime = time.Now()
|
2021-03-30 14:24:14 +08:00
|
|
|
|
_, err = o.Insert(t)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-14 15:57:52 +08:00
|
|
|
|
//预加载一些数据
|
|
|
|
|
func (t *Template) Preload() *Template {
|
|
|
|
|
if t != nil {
|
|
|
|
|
if t.MemberId > 0 {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
m, err := NewMember().Find(t.MemberId, "account", "real_name")
|
2018-08-14 15:57:52 +08:00
|
|
|
|
if err == nil {
|
|
|
|
|
if m.RealName != "" {
|
|
|
|
|
t.CreateName = m.RealName
|
2021-03-30 14:24:14 +08:00
|
|
|
|
} else {
|
2018-08-14 15:57:52 +08:00
|
|
|
|
t.CreateName = m.Account
|
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
} else {
|
|
|
|
|
logs.Error("加载模板所有者失败 ->", err)
|
2018-08-14 15:57:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if t.ModifyAt > 0 {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
if m, err := NewMember().Find(t.ModifyAt, "account", "real_name"); err == nil {
|
2018-08-14 15:57:52 +08:00
|
|
|
|
if m.RealName != "" {
|
|
|
|
|
t.ModifyName = m.RealName
|
2021-03-30 14:24:14 +08:00
|
|
|
|
} else {
|
2018-08-14 15:57:52 +08:00
|
|
|
|
t.ModifyName = m.Account
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if t.BookId > 0 {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
if b, err := NewBook().Find(t.BookId, "book_name"); err == nil {
|
2018-08-14 15:57:52 +08:00
|
|
|
|
t.BookName = b.BookName
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return t
|
|
|
|
|
}
|