2017-04-21 18:20:35 +08:00
package models
2017-04-22 17:24:17 +08:00
import (
"time"
2017-04-30 22:13:12 +08:00
"strings"
2017-07-07 16:20:55 +08:00
2017-05-02 10:00:21 +08:00
"github.com/astaxie/beego"
2017-07-07 16:20:55 +08:00
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/orm"
"github.com/lifei6671/mindoc/conf"
"fmt"
2017-04-22 17:24:17 +08:00
)
2017-04-21 18:20:35 +08:00
// Book struct .
type Book struct {
2017-07-07 16:20:55 +08:00
BookId int ` orm:"pk;auto;unique;column(book_id)" json:"book_id" `
2017-04-21 18:20:35 +08:00
// BookName 项目名称.
2017-07-07 16:20:55 +08:00
BookName string ` orm:"column(book_name);size(500)" json:"book_name" `
2017-04-21 18:20:35 +08:00
// Identify 项目唯一标识.
2017-07-07 16:20:55 +08:00
Identify string ` orm:"column(identify);size(100);unique" json:"identify" `
2018-01-19 20:10:54 +08:00
//是否是自动发布 0 否/1 是
AutoRelease int ` orm:"column(auto_release);type(int);default(0)" json:"auto_release" `
2017-07-07 16:20:55 +08:00
OrderIndex int ` orm:"column(order_index);type(int);default(0)" json:"order_index" `
2017-04-21 18:20:35 +08:00
// Description 项目描述.
2017-07-07 16:20:55 +08:00
Description string ` orm:"column(description);size(2000)" json:"description" `
Label string ` orm:"column(label);size(500)" json:"label" `
2017-04-21 18:20:35 +08:00
// PrivatelyOwned 项目私有: 0 公开/ 1 私有
2017-07-07 16:20:55 +08:00
PrivatelyOwned int ` orm:"column(privately_owned);type(int);default(0)" json:"privately_owned" `
2017-04-21 18:20:35 +08:00
// 当项目是私有时的访问Token.
2017-07-07 16:20:55 +08:00
PrivateToken string ` orm:"column(private_token);size(500);null" json:"private_token" `
2017-05-01 12:15:55 +08:00
//状态: 0 正常/1 已删除
2017-07-07 16:20:55 +08:00
Status int ` orm:"column(status);type(int);default(0)" json:"status" `
2017-04-26 18:17:38 +08:00
//默认的编辑器.
2017-07-07 16:20:55 +08:00
Editor string ` orm:"column(editor);size(50)" json:"editor" `
2017-04-21 18:20:35 +08:00
// DocCount 包含文档数量.
2017-07-07 16:20:55 +08:00
DocCount int ` orm:"column(doc_count);type(int)" json:"doc_count" `
2017-04-22 17:24:17 +08:00
// CommentStatus 评论设置的状态:open 为允许所有人评论, closed 为不允许评论, group_only 仅允许参与者评论 ,registered_only 仅允许注册者评论.
2017-07-07 16:20:55 +08:00
CommentStatus string ` orm:"column(comment_status);size(20);default(open)" json:"comment_status" `
CommentCount int ` orm:"column(comment_count);type(int)" json:"comment_count" `
2017-04-30 22:13:12 +08:00
//封面地址
2017-07-07 16:20:55 +08:00
Cover string ` orm:"column(cover);size(1000)" json:"cover" `
2017-04-30 22:13:12 +08:00
//主题风格
2017-07-07 16:20:55 +08:00
Theme string ` orm:"column(theme);size(255);default(default)" json:"theme" `
2017-04-21 18:20:35 +08:00
// CreateTime 创建时间 .
2017-07-07 16:20:55 +08:00
CreateTime time . Time ` orm:"type(datetime);column(create_time);auto_now_add" json:"create_time" `
MemberId int ` orm:"column(member_id);size(100)" json:"member_id" `
ModifyTime time . Time ` orm:"type(datetime);column(modify_time);null;auto_now" json:"modify_time" `
Version int64 ` orm:"type(bigint);column(version)" json:"version" `
2017-04-21 18:20:35 +08:00
}
// TableName 获取对应数据库表名.
func ( m * Book ) TableName ( ) string {
return "books"
}
2017-07-07 16:20:55 +08:00
2017-04-21 18:20:35 +08:00
// TableEngine 获取数据使用的引擎.
func ( m * Book ) TableEngine ( ) string {
return "INNODB"
}
2017-04-22 17:24:17 +08:00
func ( m * Book ) TableNameWithPrefix ( ) string {
return conf . GetDatabasePrefix ( ) + m . TableName ( )
}
func NewBook ( ) * Book {
return & Book { }
}
func ( m * Book ) Insert ( ) error {
o := orm . NewOrm ( )
2017-07-07 16:20:55 +08:00
// o.Begin()
2017-04-29 21:28:09 +08:00
2017-07-07 16:20:55 +08:00
_ , err := o . Insert ( m )
2017-04-22 17:24:17 +08:00
2017-04-24 18:25:17 +08:00
if err == nil {
2017-07-07 16:20:55 +08:00
if m . Label != "" {
NewLabel ( ) . InsertOrUpdateMulti ( m . Label )
}
2017-04-24 18:25:17 +08:00
relationship := NewRelationship ( )
relationship . BookId = m . BookId
relationship . RoleId = 0
relationship . MemberId = m . MemberId
err = relationship . Insert ( )
2017-04-29 21:28:09 +08:00
if err != nil {
2017-07-07 16:20:55 +08:00
logs . Error ( "插入项目与用户关联 => " , err )
2017-05-05 11:09:17 +08:00
//o.Rollback()
2017-04-29 21:28:09 +08:00
return err
}
document := NewDocument ( )
document . BookId = m . BookId
document . DocumentName = "空白文档"
document . MemberId = m . MemberId
err = document . InsertOrUpdate ( )
2017-07-07 16:20:55 +08:00
if err != nil {
2017-05-05 11:09:17 +08:00
//o.Rollback()
2017-04-29 21:28:09 +08:00
return err
}
2017-05-05 11:09:17 +08:00
//o.Commit()
2017-05-04 10:35:56 +08:00
return nil
2017-04-24 18:25:17 +08:00
}
2017-05-05 11:09:17 +08:00
//o.Rollback()
2017-04-22 17:24:17 +08:00
return err
}
2017-07-07 16:20:55 +08:00
func ( m * Book ) Find ( id int ) ( * Book , error ) {
2017-04-23 20:33:21 +08:00
if id <= 0 {
2017-07-07 16:20:55 +08:00
return m , ErrInvalidParameter
2017-04-23 20:33:21 +08:00
}
o := orm . NewOrm ( )
2017-07-07 16:20:55 +08:00
err := o . QueryTable ( m . TableNameWithPrefix ( ) ) . Filter ( "book_id" , id ) . One ( m )
2017-04-25 20:05:59 +08:00
2017-07-07 16:20:55 +08:00
return m , err
2017-04-23 20:33:21 +08:00
}
2017-07-07 16:20:55 +08:00
func ( m * Book ) Update ( cols ... string ) error {
2017-04-22 17:24:17 +08:00
o := orm . NewOrm ( )
2017-07-07 16:20:55 +08:00
temp := NewBook ( )
temp . BookId = m . BookId
if err := o . Read ( temp ) ; err != nil {
return err
}
if ( m . Label + temp . Label ) != "" {
go NewLabel ( ) . InsertOrUpdateMulti ( m . Label + "," + temp . Label )
}
_ , err := o . Update ( m , cols ... )
2017-04-22 17:24:17 +08:00
return err
}
2017-04-30 22:13:12 +08:00
//根据指定字段查询结果集.
2017-07-07 16:20:55 +08:00
func ( m * Book ) FindByField ( field string , value interface { } ) ( [ ] * Book , error ) {
2017-04-22 17:24:17 +08:00
o := orm . NewOrm ( )
2017-04-30 22:13:12 +08:00
var books [ ] * Book
2017-07-07 16:20:55 +08:00
_ , err := o . QueryTable ( m . TableNameWithPrefix ( ) ) . Filter ( field , value ) . All ( & books )
2017-04-22 17:24:17 +08:00
2017-07-07 16:20:55 +08:00
return books , err
2017-04-22 17:24:17 +08:00
}
2017-04-30 22:13:12 +08:00
//根据指定字段查询一个结果.
2017-07-07 16:20:55 +08:00
func ( m * Book ) FindByFieldFirst ( field string , value interface { } ) ( * Book , error ) {
2017-04-24 18:25:17 +08:00
o := orm . NewOrm ( )
2017-07-07 16:20:55 +08:00
err := o . QueryTable ( m . TableNameWithPrefix ( ) ) . Filter ( field , value ) . One ( m )
2017-04-24 18:25:17 +08:00
2017-07-07 16:20:55 +08:00
return m , err
2017-04-24 18:25:17 +08:00
}
2017-04-30 22:13:12 +08:00
2017-07-07 16:20:55 +08:00
func ( m * Book ) FindByIdentify ( identify string ) ( * Book , error ) {
2017-05-12 10:45:40 +08:00
o := orm . NewOrm ( )
2017-07-07 16:20:55 +08:00
err := o . QueryTable ( m . TableNameWithPrefix ( ) ) . Filter ( "identify" , identify ) . One ( m )
2017-05-12 10:45:40 +08:00
2017-07-07 16:20:55 +08:00
return m , err
2017-05-12 10:45:40 +08:00
}
2017-04-30 22:13:12 +08:00
//分页查询指定用户的项目
2017-07-07 16:20:55 +08:00
func ( m * Book ) FindToPager ( pageIndex , pageSize , memberId int ) ( books [ ] * BookResult , totalCount int , err error ) {
2017-04-22 17:24:17 +08:00
relationship := NewRelationship ( )
o := orm . NewOrm ( )
2017-07-07 16:20:55 +08:00
sql1 := "SELECT COUNT(book.book_id) AS total_count FROM " + m . TableNameWithPrefix ( ) + " AS book LEFT JOIN " +
relationship . TableNameWithPrefix ( ) + " AS rel ON book.book_id=rel.book_id AND rel.member_id = ? WHERE rel.relationship_id > 0 "
2017-04-22 17:24:17 +08:00
2017-07-07 16:20:55 +08:00
err = o . Raw ( sql1 , memberId ) . QueryRow ( & totalCount )
2017-04-22 17:24:17 +08:00
if err != nil {
return
}
offset := ( pageIndex - 1 ) * pageSize
2017-07-07 16:20:55 +08:00
sql2 := "SELECT book.*,rel.member_id,rel.role_id,m.account as create_name FROM " + m . TableNameWithPrefix ( ) + " AS book" +
" LEFT JOIN " + relationship . TableNameWithPrefix ( ) + " AS rel ON book.book_id=rel.book_id AND rel.member_id = ?" +
" LEFT JOIN " + relationship . TableNameWithPrefix ( ) + " AS rel1 ON book.book_id=rel1.book_id AND rel1.role_id=0" +
" LEFT JOIN " + NewMember ( ) . TableNameWithPrefix ( ) + " AS m ON rel1.member_id=m.member_id " +
" WHERE rel.relationship_id > 0 ORDER BY book.order_index DESC,book.book_id DESC LIMIT " + fmt . Sprintf ( "%d,%d" , offset , pageSize )
_ , err = o . Raw ( sql2 , memberId ) . QueryRows ( & books )
2017-04-24 18:25:17 +08:00
if err != nil {
2017-07-07 16:20:55 +08:00
logs . Error ( "分页查询项目列表 => " , err )
2017-04-24 18:25:17 +08:00
return
}
2017-04-22 17:24:17 +08:00
sql := "SELECT m.account,doc.modify_time FROM md_documents AS doc LEFT JOIN md_members AS m ON doc.modify_at=m.member_id WHERE book_id = ? LIMIT 1 ORDER BY doc.modify_time DESC"
2017-07-07 16:20:55 +08:00
if err == nil && len ( books ) > 0 {
for index , book := range books {
var text struct {
Account string
2017-04-22 17:24:17 +08:00
ModifyTime time . Time
}
2017-07-07 16:20:55 +08:00
err1 := o . Raw ( sql , book . BookId ) . QueryRow ( & text )
2017-04-22 17:24:17 +08:00
if err1 == nil {
books [ index ] . LastModifyText = text . Account + " 于 " + text . ModifyTime . Format ( "2006-01-02 15:04:05" )
}
2017-07-07 16:20:55 +08:00
if book . RoleId == 0 {
2017-04-22 17:24:17 +08:00
book . RoleName = "创始人"
2017-07-07 16:20:55 +08:00
} else if book . RoleId == 1 {
2017-04-22 17:24:17 +08:00
book . RoleName = "管理员"
2017-07-07 16:20:55 +08:00
} else if book . RoleId == 2 {
2017-04-22 17:24:17 +08:00
book . RoleName = "编辑者"
2017-07-07 16:20:55 +08:00
} else if book . RoleId == 3 {
2017-04-22 17:24:17 +08:00
book . RoleName = "观察者"
}
}
}
return
2017-04-23 12:48:46 +08:00
}
2017-04-23 20:33:21 +08:00
// 彻底删除项目.
func ( m * Book ) ThoroughDeleteBook ( id int ) error {
2017-07-07 16:20:55 +08:00
if id <= 0 {
2017-04-23 20:33:21 +08:00
return ErrInvalidParameter
}
o := orm . NewOrm ( )
m . BookId = id
if err := o . Read ( m ) ; err != nil {
return err
}
o . Begin ( )
2017-07-07 16:20:55 +08:00
2017-04-23 20:33:21 +08:00
sql2 := "DELETE FROM " + NewDocument ( ) . TableNameWithPrefix ( ) + " WHERE book_id = ?"
2017-07-07 16:20:55 +08:00
_ , err := o . Raw ( sql2 , m . BookId ) . Exec ( )
2017-04-23 20:33:21 +08:00
if err != nil {
o . Rollback ( )
return err
}
sql3 := "DELETE FROM " + m . TableNameWithPrefix ( ) + " WHERE book_id = ?"
2017-07-07 16:20:55 +08:00
_ , err = o . Raw ( sql3 , m . BookId ) . Exec ( )
2017-04-23 20:33:21 +08:00
if err != nil {
o . Rollback ( )
return err
}
2017-04-24 18:25:17 +08:00
sql4 := "DELETE FROM " + NewRelationship ( ) . TableNameWithPrefix ( ) + " WHERE book_id = ?"
2017-04-23 20:33:21 +08:00
2017-07-07 16:20:55 +08:00
_ , err = o . Raw ( sql4 , m . BookId ) . Exec ( )
2017-04-23 20:33:21 +08:00
if err != nil {
o . Rollback ( )
return err
}
2017-07-07 16:20:55 +08:00
if m . Label != "" {
NewLabel ( ) . InsertOrUpdateMulti ( m . Label )
}
2017-04-23 20:33:21 +08:00
return o . Commit ( )
}
2017-05-13 12:12:37 +08:00
//分页查找系统首页数据.
2017-07-07 16:20:55 +08:00
func ( m * Book ) FindForHomeToPager ( pageIndex , pageSize , member_id int ) ( books [ ] * BookResult , totalCount int , err error ) {
2017-04-30 22:13:12 +08:00
o := orm . NewOrm ( )
offset := ( pageIndex - 1 ) * pageSize
//如果是登录用户
if member_id > 0 {
sql1 := "SELECT COUNT(*) FROM md_books AS book LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id = ? WHERE relationship_id > 0 OR book.privately_owned = 0"
2017-07-07 16:20:55 +08:00
err = o . Raw ( sql1 , member_id ) . QueryRow ( & totalCount )
2017-04-30 22:13:12 +08:00
if err != nil {
return
}
sql2 := ` SELECT book . * , rel1 . * , member . account AS create_name FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel . book_id = book . book_id AND rel . member_id = ?
LEFT JOIN md_relationship AS rel1 ON rel1 . book_id = book . book_id AND rel1 . role_id = 0
LEFT JOIN md_members AS member ON rel1 . member_id = member . member_id
WHERE rel . relationship_id > 0 OR book . privately_owned = 0 ORDER BY order_index DESC , book . book_id DESC LIMIT ? , ? `
2017-04-23 20:33:21 +08:00
2017-07-07 16:20:55 +08:00
_ , err = o . Raw ( sql2 , member_id , offset , pageSize ) . QueryRows ( & books )
2017-04-23 20:33:21 +08:00
2017-04-30 22:13:12 +08:00
return
2017-04-23 20:33:21 +08:00
2017-07-07 16:20:55 +08:00
} else {
count , err1 := o . QueryTable ( m . TableNameWithPrefix ( ) ) . Filter ( "privately_owned" , 0 ) . Count ( )
2017-04-23 20:33:21 +08:00
2017-04-30 22:13:12 +08:00
if err1 != nil {
err = err1
return
}
totalCount = int ( count )
2017-04-23 20:33:21 +08:00
2017-04-30 22:13:12 +08:00
sql := ` SELECT book . * , rel . * , member . account AS create_name FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel . book_id = book . book_id AND rel . role_id = 0
LEFT JOIN md_members AS member ON rel . member_id = member . member_id
WHERE book . privately_owned = 0 ORDER BY order_index DESC , book . book_id DESC LIMIT ? , ? `
2017-07-07 16:20:55 +08:00
_ , err = o . Raw ( sql , offset , pageSize ) . QueryRows ( & books )
2017-04-30 22:13:12 +08:00
return
}
}
2017-07-07 16:20:55 +08:00
//分页全局搜索.
func ( m * Book ) FindForLabelToPager ( keyword string , pageIndex , pageSize , member_id int ) ( books [ ] * BookResult , totalCount int , err error ) {
o := orm . NewOrm ( )
keyword = "%" + keyword + "%"
offset := ( pageIndex - 1 ) * pageSize
//如果是登录用户
if member_id > 0 {
sql1 := "SELECT COUNT(*) FROM md_books AS book LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id = ? WHERE (relationship_id > 0 OR book.privately_owned = 0) AND book.label LIKE ?"
err = o . Raw ( sql1 , member_id , keyword ) . QueryRow ( & totalCount )
if err != nil {
return
}
sql2 := ` SELECT book . * , rel1 . * , member . account AS create_name FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel . book_id = book . book_id AND rel . member_id = ?
LEFT JOIN md_relationship AS rel1 ON rel1 . book_id = book . book_id AND rel1 . role_id = 0
LEFT JOIN md_members AS member ON rel1 . member_id = member . member_id
WHERE ( rel . relationship_id > 0 OR book . privately_owned = 0 ) AND book . label LIKE ? ORDER BY order_index DESC , book . book_id DESC LIMIT ? , ? `
_ , err = o . Raw ( sql2 , member_id , keyword , offset , pageSize ) . QueryRows ( & books )
return
} else {
count , err1 := o . QueryTable ( NewBook ( ) . TableNameWithPrefix ( ) ) . Filter ( "privately_owned" , 0 ) . Filter ( "label__icontains" , keyword ) . Count ( )
if err1 != nil {
err = err1
return
}
totalCount = int ( count )
sql := ` SELECT book . * , rel . * , member . account AS create_name FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel . book_id = book . book_id AND rel . role_id = 0
LEFT JOIN md_members AS member ON rel . member_id = member . member_id
WHERE book . privately_owned = 0 AND book . label LIKE ? ORDER BY order_index DESC , book . book_id DESC LIMIT ? , ? `
_ , err = o . Raw ( sql , keyword , offset , pageSize ) . QueryRows ( & books )
return
}
}
2017-04-30 22:13:12 +08:00
func ( book * Book ) ToBookResult ( ) * BookResult {
m := NewBookResult ( )
2017-07-07 16:20:55 +08:00
m . BookId = book . BookId
m . BookName = book . BookName
m . Identify = book . Identify
m . OrderIndex = book . OrderIndex
m . Description = strings . Replace ( book . Description , "\r\n" , "<br/>" , - 1 )
m . PrivatelyOwned = book . PrivatelyOwned
m . PrivateToken = book . PrivateToken
m . DocCount = book . DocCount
m . CommentStatus = book . CommentStatus
m . CommentCount = book . CommentCount
m . CreateTime = book . CreateTime
m . ModifyTime = book . ModifyTime
m . Cover = book . Cover
m . Label = book . Label
m . Status = book . Status
m . Editor = book . Editor
m . Theme = book . Theme
2018-01-19 20:10:54 +08:00
m . AutoRelease = book . AutoRelease == 1
2017-07-07 16:20:55 +08:00
if book . Theme == "" {
2017-05-01 12:15:55 +08:00
m . Theme = "default"
}
if book . Editor == "" {
m . Editor = "markdown"
}
2017-04-30 22:13:12 +08:00
return m
}
2017-04-23 20:33:21 +08:00
2017-05-02 10:00:21 +08:00
//重置文档数量
2017-07-07 16:20:55 +08:00
func ( m * Book ) ResetDocumentNumber ( book_id int ) {
2017-05-02 10:00:21 +08:00
o := orm . NewOrm ( )
2017-04-23 20:33:21 +08:00
2017-07-07 16:20:55 +08:00
totalCount , err := o . QueryTable ( NewDocument ( ) . TableNameWithPrefix ( ) ) . Filter ( "book_id" , book_id ) . Count ( )
2017-05-02 10:00:21 +08:00
if err == nil {
2017-07-07 16:20:55 +08:00
o . Raw ( "UPDATE md_books SET doc_count = ? WHERE book_id = ?" , int ( totalCount ) , book_id ) . Exec ( )
} else {
2017-05-02 10:00:21 +08:00
beego . Error ( err )
}
}