document_search_result.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. func (m *DocumentSearchResult) FindToPager(keyword string,page_index,page_size,member_id int) (search_result []*DocumentSearchResult,total_count int,err error) {
  23. o := orm.NewOrm()
  24. offset := (page_index - 1) * page_size
  25. if member_id <= 0 {
  26. sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc
  27. LEFT JOIN md_books as book ON doc.book_id = book.book_id
  28. WHERE book.privately_owned = 0 AND (doc.document_name LIKE '%?%' OR doc.release LIKE '%?%') `
  29. 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
  30. LEFT JOIN md_books as book ON doc.book_id = book.book_id
  31. LEFT JOIN md_relationship AS rel ON book.book_id = rel.book_id AND role_id = 0
  32. LEFT JOIN md_members as member ON rel.member_id = member.member_id
  33. WHERE book.privately_owned = 0 AND (doc.document_name LIKE '%?%' OR doc.release LIKE '%?%')
  34. ORDER BY doc.document_id DESC LIMIT ?,? `
  35. err = o.Raw(sql1,keyword,keyword).QueryRow(&total_count)
  36. if err != nil{
  37. return
  38. }
  39. _,err = o.Raw(sql2,keyword,keyword,offset,page_size).QueryRows(&search_result)
  40. if err != nil {
  41. return
  42. }
  43. }else{
  44. sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc
  45. LEFT JOIN md_books as book ON doc.book_id = book.book_id
  46. LEFT JOIN md_relationship AS rel ON doc.book_id = rel.book_id AND role_id = 0
  47. WHERE (book.privately_owned = 0 OR rel.relationship_id > 0) AND (doc.document_name LIKE ? OR doc.release LIKE ?) `
  48. 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
  49. LEFT JOIN md_books as book ON doc.book_id = book.book_id
  50. LEFT JOIN md_relationship AS rel ON book.book_id = rel.book_id AND role_id = 0
  51. LEFT JOIN md_members as member ON rel.member_id = member.member_id
  52. WHERE (book.privately_owned = 0 OR rel.relationship_id > 0) AND (doc.document_name LIKE ? OR doc.release LIKE ?)
  53. ORDER BY doc.document_id DESC LIMIT ?,? `
  54. keyword = "%"+keyword+"%"
  55. err = o.Raw(sql1,keyword,keyword).QueryRow(&total_count)
  56. if err != nil{
  57. return
  58. }
  59. _,err = o.Raw(sql2,keyword,keyword,offset,page_size).QueryRows(&search_result)
  60. if err != nil {
  61. return
  62. }
  63. }
  64. return
  65. }