实现项目内搜索功能

This commit is contained in:
Minho
2017-05-15 14:59:23 +08:00
parent 08c4d52116
commit eba2fdc4ac
5 changed files with 150 additions and 27 deletions

View File

@@ -53,6 +53,7 @@ func NewDocument() *Document {
}
}
//根据文档ID查询指定文档.
func (m *Document) Find(id int) (*Document,error) {
if id <= 0 {
return m,ErrInvalidParameter
@@ -150,7 +151,8 @@ func (m *Document) ReleaseContent(book_id int) {
}
}
func (m *Document) FindListByBookId(book_id int) (docs []*Document,err error) {
//根据项目ID查询文档列表.
func (m *Document) FindListByBookId(book_id int) (docs []*Document,err error) {
o := orm.NewOrm()
_,err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id",book_id).All(&docs)
@@ -162,3 +164,6 @@ func (m *Document) FindListByBookId(book_id int) (docs []*Document,err error) {

View File

@@ -7,29 +7,29 @@ import (
)
type DocumentSearchResult struct {
DocumentId int `json:"doc_id"`
DocumentName string `json:"doc_name"`
DocumentId int `json:"doc_id"`
DocumentName string `json:"doc_name"`
// Identify 文档唯一标识
Identify string `json:"identify"`
Description string `json:"description"`
Author string `json:"author"`
ModifyTime time.Time `json:"modify_time"`
CreateTime time.Time `json:"create_time"`
BookId int `json:"book_id"`
BookName string `json:"book_name"`
BookIdentify string `json:"book_identify"`
Identify string `json:"identify"`
Description string `json:"description"`
Author string `json:"author"`
ModifyTime time.Time `json:"modify_time"`
CreateTime time.Time `json:"create_time"`
BookId int `json:"book_id"`
BookName string `json:"book_name"`
BookIdentify string `json:"book_identify"`
}
func NewDocumentSearchResult() *DocumentSearchResult {
return &DocumentSearchResult{}
}
func (m *DocumentSearchResult) FindToPager(keyword string,page_index,page_size,member_id int) (search_result []*DocumentSearchResult,total_count int,err error) {
//分页全局搜索.
func (m *DocumentSearchResult) FindToPager(keyword string, page_index, page_size, member_id int) (search_result []*DocumentSearchResult, total_count int, err error) {
o := orm.NewOrm()
offset := (page_index - 1) * page_size
keyword = "%"+keyword+"%"
keyword = "%" + keyword + "%"
if member_id <= 0 {
sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc
@@ -43,15 +43,15 @@ WHERE book.privately_owned = 0 AND (doc.document_name LIKE ? OR doc.release LIKE
WHERE book.privately_owned = 0 AND (doc.document_name LIKE ? OR doc.release LIKE ?)
ORDER BY doc.document_id DESC LIMIT ?,? `
err = o.Raw(sql1,keyword,keyword).QueryRow(&total_count)
if err != nil{
return
}
_,err = o.Raw(sql2,keyword,keyword,offset,page_size).QueryRows(&search_result)
err = o.Raw(sql1, keyword, keyword).QueryRow(&total_count)
if err != nil {
return
}
}else{
_, err = o.Raw(sql2, keyword, keyword, offset, page_size).QueryRows(&search_result)
if err != nil {
return
}
} else {
sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc
LEFT JOIN md_books as book ON doc.book_id = book.book_id
LEFT JOIN md_relationship AS rel ON doc.book_id = rel.book_id AND role_id = 0
@@ -66,12 +66,11 @@ WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0) AND (doc.document_
WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0) AND (doc.document_name LIKE ? OR doc.release LIKE ?)
ORDER BY doc.document_id DESC LIMIT ?,? `
err = o.Raw(sql1,member_id,keyword,keyword).QueryRow(&total_count)
if err != nil{
err = o.Raw(sql1, member_id, keyword, keyword).QueryRow(&total_count)
if err != nil {
return
}
_,err = o.Raw(sql2,member_id,keyword,keyword,offset,page_size).QueryRows(&search_result)
_, err = o.Raw(sql2, member_id, keyword, keyword, offset, page_size).QueryRows(&search_result)
if err != nil {
return
}
@@ -79,3 +78,14 @@ WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0) AND (doc.document_
return
}
//项目内搜索.
func (m *DocumentSearchResult) SearchDocument(keyword string, book_id int) (docs []*DocumentSearchResult, err error) {
o := orm.NewOrm()
sql := "SELECT * FROM md_documents WHERE book_id = ? AND (document_name LIKE ? OR `release` LIKE ?) "
keyword = "%" + keyword + "%"
_, err = o.Raw(sql, book_id, keyword, keyword).QueryRows(&docs)
return
}