1
0

DocumentSearchResult.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package models
  2. import (
  3. "time"
  4. "github.com/astaxie/beego/orm"
  5. )
  6. type DocumentSearchResult struct {
  7. DocumentId int `json:"doc_id"`
  8. DocumentName string `json:"doc_name"`
  9. // Identify 文档唯一标识
  10. Identify string `json:"identify"`
  11. Description string `json:"description"`
  12. Author string `json:"author"`
  13. ModifyTime time.Time `json:"modify_time"`
  14. CreateTime time.Time `json:"create_time"`
  15. BookId int `json:"book_id"`
  16. BookName string `json:"book_name"`
  17. BookIdentify string `json:"book_identify"`
  18. }
  19. func NewDocumentSearchResult() *DocumentSearchResult {
  20. return &DocumentSearchResult{}
  21. }
  22. //分页全局搜索.
  23. func (m *DocumentSearchResult) FindToPager(keyword string, page_index, page_size, member_id int) (search_result []*DocumentSearchResult, total_count int, err error) {
  24. o := orm.NewOrm()
  25. offset := (page_index - 1) * page_size
  26. keyword = "%" + keyword + "%"
  27. if member_id <= 0 {
  28. sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc
  29. LEFT JOIN md_books as book ON doc.book_id = book.book_id
  30. WHERE book.privately_owned = 0 AND (doc.document_name LIKE ? OR doc.release LIKE ?) `
  31. sql2 := `SELECT doc.document_id,doc.modify_time,doc.create_time,doc.document_name,doc.identify,doc.release as description,doc.modify_time,book.identify as book_identify,book.book_name,rel.member_id,member.account AS author FROM md_documents AS doc
  32. LEFT JOIN md_books as book ON doc.book_id = book.book_id
  33. LEFT JOIN md_relationship AS rel ON book.book_id = rel.book_id AND rel.role_id = 0
  34. LEFT JOIN md_members as member ON rel.member_id = member.member_id
  35. WHERE book.privately_owned = 0 AND (doc.document_name LIKE ? OR doc.release LIKE ?)
  36. ORDER BY doc.document_id DESC LIMIT ?,? `
  37. err = o.Raw(sql1, keyword, keyword).QueryRow(&total_count)
  38. if err != nil {
  39. return
  40. }
  41. _, err = o.Raw(sql2, keyword, keyword, offset, page_size).QueryRows(&search_result)
  42. if err != nil {
  43. return
  44. }
  45. } else {
  46. sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc
  47. LEFT JOIN md_books as book ON doc.book_id = book.book_id
  48. LEFT JOIN md_relationship AS rel ON doc.book_id = rel.book_id AND rel.role_id = 0
  49. LEFT JOIN md_relationship AS rel1 ON doc.book_id = rel1.book_id AND rel1.member_id = ?
  50. WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0) AND (doc.document_name LIKE ? OR doc.release LIKE ?) `
  51. sql2 := `SELECT doc.document_id,doc.modify_time,doc.create_time,doc.document_name,doc.identify,doc.release as description,doc.modify_time,book.identify as book_identify,book.book_name,rel.member_id,member.account AS author FROM md_documents AS doc
  52. LEFT JOIN md_books as book ON doc.book_id = book.book_id
  53. LEFT JOIN md_relationship AS rel ON book.book_id = rel.book_id AND rel.role_id = 0
  54. LEFT JOIN md_members as member ON rel.member_id = member.member_id
  55. LEFT JOIN md_relationship AS rel1 ON doc.book_id = rel1.book_id AND rel1.member_id = ?
  56. WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0) AND (doc.document_name LIKE ? OR doc.release LIKE ?)
  57. ORDER BY doc.document_id DESC LIMIT ?,? `
  58. err = o.Raw(sql1, member_id, keyword, keyword).QueryRow(&total_count)
  59. if err != nil {
  60. return
  61. }
  62. _, err = o.Raw(sql2, member_id, keyword, keyword, offset, page_size).QueryRows(&search_result)
  63. if err != nil {
  64. return
  65. }
  66. }
  67. return
  68. }
  69. //项目内搜索.
  70. func (m *DocumentSearchResult) SearchDocument(keyword string, book_id int) (docs []*DocumentSearchResult, err error) {
  71. o := orm.NewOrm()
  72. sql := "SELECT * FROM md_documents WHERE book_id = ? AND (document_name LIKE ? OR `release` LIKE ?) "
  73. keyword = "%" + keyword + "%"
  74. _, err = o.Raw(sql, book_id, keyword, keyword).QueryRows(&docs)
  75. return
  76. }