book.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package models
  2. import (
  3. "time"
  4. "github.com/astaxie/beego/orm"
  5. "github.com/lifei6671/godoc/conf"
  6. "github.com/astaxie/beego/logs"
  7. "strings"
  8. "github.com/astaxie/beego"
  9. )
  10. // Book struct .
  11. type Book struct {
  12. BookId int `orm:"pk;auto;unique;column(book_id)" json:"book_id"`
  13. // BookName 项目名称.
  14. BookName string `orm:"column(book_name);size(500)" json:"book_name"`
  15. // Identify 项目唯一标识.
  16. Identify string `orm:"column(identify);size(100);unique" json:"identify"`
  17. OrderIndex int `orm:"column(order_index);type(int);default(0)" json:"order_index"`
  18. // Description 项目描述.
  19. Description string `orm:"column(description);size(2000)" json:"description"`
  20. Label string `orm:"column(label);size(500)" json:"label"`
  21. // PrivatelyOwned 项目私有: 0 公开/ 1 私有
  22. PrivatelyOwned int `orm:"column(privately_owned);type(int);default(0)" json:"privately_owned"`
  23. // 当项目是私有时的访问Token.
  24. PrivateToken string `orm:"column(private_token);size(500);null" json:"private_token"`
  25. //状态:0 正常/1 已删除
  26. Status int `orm:"column(status);type(int);default(0)" json:"status"`
  27. //默认的编辑器.
  28. Editor string `orm:"column(editor);size(50)" json:"editor"`
  29. // DocCount 包含文档数量.
  30. DocCount int `orm:"column(doc_count);type(int)" json:"doc_count"`
  31. // CommentStatus 评论设置的状态:open 为允许所有人评论,closed 为不允许评论, group_only 仅允许参与者评论 ,registered_only 仅允许注册者评论.
  32. CommentStatus string `orm:"column(comment_status);size(20);default(open)" json:"comment_status"`
  33. CommentCount int `orm:"column(comment_count);type(int)" json:"comment_count"`
  34. //封面地址
  35. Cover string `orm:"column(cover);size(1000)" json:"cover"`
  36. //主题风格
  37. Theme string `orm:"columen(theme);size(255);default(default)" json:"theme"`
  38. // CreateTime 创建时间 .
  39. CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add" json:"create_time"`
  40. MemberId int `orm:"column(member_id);size(100)" json:"member_id"`
  41. ModifyTime time.Time `orm:"type(datetime);column(modify_time);null;auto_now" json:"modify_time"`
  42. Version int64 `orm:"type(bigint);column(version)" json:"version"`
  43. }
  44. // TableName 获取对应数据库表名.
  45. func (m *Book) TableName() string {
  46. return "books"
  47. }
  48. // TableEngine 获取数据使用的引擎.
  49. func (m *Book) TableEngine() string {
  50. return "INNODB"
  51. }
  52. func (m *Book) TableNameWithPrefix() string {
  53. return conf.GetDatabasePrefix() + m.TableName()
  54. }
  55. func NewBook() *Book {
  56. return &Book{}
  57. }
  58. func (m *Book) Insert() error {
  59. o := orm.NewOrm()
  60. o.Begin()
  61. _,err := o.Insert(m)
  62. if err == nil {
  63. relationship := NewRelationship()
  64. relationship.BookId = m.BookId
  65. relationship.RoleId = 0
  66. relationship.MemberId = m.MemberId
  67. err = relationship.Insert()
  68. if err != nil {
  69. logs.Error("插入项目与用户关联 => ",err)
  70. o.Rollback()
  71. return err
  72. }
  73. document := NewDocument()
  74. document.BookId = m.BookId
  75. document.DocumentName = "空白文档"
  76. document.MemberId = m.MemberId
  77. err = document.InsertOrUpdate()
  78. if err != nil{
  79. o.Rollback()
  80. return err
  81. }
  82. o.Commit()
  83. }
  84. o.Rollback()
  85. return err
  86. }
  87. func (m *Book) Find(id int) (*Book,error) {
  88. if id <= 0 {
  89. return m,ErrInvalidParameter
  90. }
  91. o := orm.NewOrm()
  92. err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id",id).One(m)
  93. return m,err
  94. }
  95. func (m *Book) Update(cols... string) error {
  96. o := orm.NewOrm()
  97. _,err := o.Update(m,cols...)
  98. return err
  99. }
  100. //根据指定字段查询结果集.
  101. func (m *Book) FindByField(field string,value interface{}) ([]*Book,error) {
  102. o := orm.NewOrm()
  103. var books []*Book
  104. _,err := o.QueryTable(m.TableNameWithPrefix()).Filter(field,value).All(&books)
  105. return books,err
  106. }
  107. //根据指定字段查询一个结果.
  108. func (m *Book) FindByFieldFirst(field string,value interface{})(*Book,error) {
  109. o := orm.NewOrm()
  110. err := o.QueryTable(m.TableNameWithPrefix()).Filter(field,value).One(m)
  111. return m,err
  112. }
  113. //分页查询指定用户的项目
  114. func (m *Book) FindToPager(pageIndex, pageSize ,memberId int) (books []*BookResult,totalCount int,err error){
  115. relationship := NewRelationship()
  116. o := orm.NewOrm()
  117. qb, _ := orm.NewQueryBuilder("mysql")
  118. qb.Select("COUNT(book.book_id) AS total_count").
  119. From(m.TableNameWithPrefix() + " AS book").
  120. LeftJoin(relationship.TableNameWithPrefix() + " AS rel").
  121. On("book.book_id=rel.book_id").
  122. Where("rel.member_id=?")
  123. err = o.Raw(qb.String(),memberId).QueryRow(&totalCount)
  124. if err != nil {
  125. return
  126. }
  127. offset := (pageIndex - 1) * pageSize
  128. qb2,_ := orm.NewQueryBuilder("mysql")
  129. qb2.Select("book.*,rel.member_id","rel.role_id","m.account as create_name").
  130. From(m.TableNameWithPrefix() + " AS book").
  131. LeftJoin(relationship.TableNameWithPrefix() + " AS rel").
  132. On("book.book_id=rel.book_id").
  133. LeftJoin(NewMember().TableNameWithPrefix() + " AS m").On("rel.member_id=m.member_id AND rel.role_id=0").
  134. Where("rel.member_id=?").
  135. OrderBy("book.order_index DESC ","book.book_id").Desc().
  136. Limit(pageSize).
  137. Offset(offset)
  138. //logs.Info("",qb2.String())
  139. _,err = o.Raw(qb2.String(),memberId).QueryRows(&books)
  140. if err != nil {
  141. logs.Error("分页查询项目列表 => ",err)
  142. return
  143. }
  144. 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"
  145. if err == nil && len(books) > 0{
  146. for index,book := range books {
  147. var text struct{
  148. Account string
  149. ModifyTime time.Time
  150. }
  151. err1 := o.Raw(sql,book.BookId).QueryRow(&text)
  152. if err1 == nil {
  153. books[index].LastModifyText = text.Account + " 于 " + text.ModifyTime.Format("2006-01-02 15:04:05")
  154. }
  155. if book.RoleId == 0{
  156. book.RoleName = "创始人"
  157. }else if book.RoleId == 1 {
  158. book.RoleName = "管理员"
  159. }else if book.RoleId == 2 {
  160. book.RoleName = "编辑者"
  161. }else if book.RoleId == 3 {
  162. book.RoleName = "观察者"
  163. }
  164. }
  165. }
  166. return
  167. }
  168. // 彻底删除项目.
  169. func (m *Book) ThoroughDeleteBook(id int) error {
  170. if id <= 0{
  171. return ErrInvalidParameter
  172. }
  173. o := orm.NewOrm()
  174. m.BookId = id
  175. if err := o.Read(m); err != nil {
  176. return err
  177. }
  178. o.Begin()
  179. sql1 := "DELETE FROM " + NewComment().TableNameWithPrefix() + " WHERE book_id = ?"
  180. _,err := o.Raw(sql1,m.BookId).Exec()
  181. if err != nil {
  182. o.Rollback()
  183. return err
  184. }
  185. sql2 := "DELETE FROM " + NewDocument().TableNameWithPrefix() + " WHERE book_id = ?"
  186. _,err = o.Raw(sql2,m.BookId).Exec()
  187. if err != nil {
  188. o.Rollback()
  189. return err
  190. }
  191. sql3 := "DELETE FROM " + m.TableNameWithPrefix() + " WHERE book_id = ?"
  192. _,err = o.Raw(sql3,m.BookId).Exec()
  193. if err != nil {
  194. o.Rollback()
  195. return err
  196. }
  197. sql4 := "DELETE FROM " + NewRelationship().TableNameWithPrefix() + " WHERE book_id = ?"
  198. _,err = o.Raw(sql4,m.BookId).Exec()
  199. if err != nil {
  200. o.Rollback()
  201. return err
  202. }
  203. return o.Commit()
  204. }
  205. func (m *Book) FindForHomeToPager(pageIndex, pageSize ,member_id int) (books []*BookResult,totalCount int,err error) {
  206. o := orm.NewOrm()
  207. offset := (pageIndex - 1) * pageSize
  208. //如果是登录用户
  209. if member_id > 0 {
  210. 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"
  211. err = o.Raw(sql1,member_id).QueryRow(&totalCount)
  212. if err != nil {
  213. return
  214. }
  215. sql2 := `SELECT book.*,rel1.*,member.account AS create_name FROM md_books AS book
  216. LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id = ?
  217. LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0
  218. LEFT JOIN md_members AS member ON rel1.member_id = member.member_id
  219. WHERE rel.relationship_id > 0 OR book.privately_owned = 0 ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?`
  220. _,err = o.Raw(sql2,member_id,offset,pageSize).QueryRows(&books)
  221. return
  222. }else{
  223. count,err1 := o.QueryTable(m.TableNameWithPrefix()).Filter("privately_owned",0).Count()
  224. if err1 != nil {
  225. err = err1
  226. return
  227. }
  228. totalCount = int(count)
  229. sql := `SELECT book.*,rel.*,member.account AS create_name FROM md_books AS book
  230. LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0
  231. LEFT JOIN md_members AS member ON rel.member_id = member.member_id
  232. WHERE book.privately_owned = 0 ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?`
  233. _,err = o.Raw(sql,offset,pageSize).QueryRows(&books)
  234. return
  235. }
  236. }
  237. func (book *Book) ToBookResult() *BookResult {
  238. m := NewBookResult()
  239. m.BookId = book.BookId
  240. m.BookName = book.BookName
  241. m.Identify = book.Identify
  242. m.OrderIndex = book.OrderIndex
  243. m.Description = strings.Replace(book.Description, "\r\n", "<br/>", -1)
  244. m.PrivatelyOwned = book.PrivatelyOwned
  245. m.PrivateToken = book.PrivateToken
  246. m.DocCount = book.DocCount
  247. m.CommentStatus = book.CommentStatus
  248. m.CommentCount = book.CommentCount
  249. m.CreateTime = book.CreateTime
  250. m.ModifyTime = book.ModifyTime
  251. m.Cover = book.Cover
  252. m.Label = book.Label
  253. m.Status = book.Status
  254. m.Editor = book.Editor
  255. m.Theme = book.Theme
  256. if book.Theme == ""{
  257. m.Theme = "default"
  258. }
  259. if book.Editor == "" {
  260. m.Editor = "markdown"
  261. }
  262. return m
  263. }
  264. //重置文档数量
  265. func (m *Book) ResetDocumentNumber(book_id int) {
  266. o := orm.NewOrm()
  267. totalCount,err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("book_id",book_id).Count()
  268. if err == nil {
  269. o.Raw("UPDATE md_books SET doc_count = ? WHERE book_id = ?",int(totalCount),book_id).Exec()
  270. }else{
  271. beego.Error(err)
  272. }
  273. }