MCPcopy Index your code
hub / github.com/TruthHun/BookStack / FindToPager

Method FindToPager

models/book.go:233–303  ·  view source on GitHub ↗

分页查询指定用户的书籍 按照最新的进行排序

(pageIndex, pageSize, memberId int, wd string, PrivatelyOwned ...int)

Source from the content-addressed store, hash-verified

231//分页查询指定用户的书籍
232//按照最新的进行排序
233func (m *Book) FindToPager(pageIndex, pageSize, memberId int, wd string, PrivatelyOwned ...int) (books []*BookResult, totalCount int, err error) {
234 var args = []interface{}{memberId}
235 relationship := NewRelationship()
236 o := orm.NewOrm()
237 sqlCount := "SELECT COUNT(book.book_id) AS total_count FROM " + m.TableNameWithPrefix() + " AS book LEFT JOIN " +
238 relationship.TableNameWithPrefix() + " AS rel ON book.book_id=rel.book_id AND rel.member_id = ? WHERE rel.relationship_id > 0 "
239 if len(PrivatelyOwned) > 0 {
240 sqlCount = sqlCount + " and book.privately_owned=" + strconv.Itoa(PrivatelyOwned[0])
241 }
242
243 if wd = strings.TrimSpace(wd); wd != "" {
244 wd = "%" + wd + "%"
245 sqlCount = sqlCount + " and (book.book_name like ? or book.description like ?)"
246 args = append(args, wd, wd)
247 }
248
249 err = o.Raw(sqlCount, args...).QueryRow(&totalCount)
250 if err != nil {
251 beego.Error(err)
252 return
253 }
254
255 offset := (pageIndex - 1) * pageSize
256 sqlQuery := "SELECT book.*,rel.member_id,rel.role_id,m.account as create_name FROM " + m.TableNameWithPrefix() + " AS book" +
257 " LEFT JOIN " + relationship.TableNameWithPrefix() + " AS rel ON book.book_id=rel.book_id AND rel.member_id = ? " +
258 " LEFT JOIN " + NewMember().TableNameWithPrefix() + " AS m ON rel.member_id=m.member_id " +
259 " WHERE rel.relationship_id > 0 %v ORDER BY book.book_id DESC LIMIT " + fmt.Sprintf("%d,%d", offset, pageSize)
260
261 cond := []string{}
262 if wd != "" { // 不需要处理 wd 和 args,因为在上面已处理过
263 cond = append(cond, " and (book.book_name like ? or book.description like ?)")
264 }
265
266 if len(PrivatelyOwned) > 0 {
267 cond = append(cond, " and book.privately_owned="+strconv.Itoa(PrivatelyOwned[0]))
268 }
269 sqlQuery = fmt.Sprintf(sqlQuery, strings.Join(cond, " "))
270
271 _, err = o.Raw(sqlQuery, args...).QueryRows(&books)
272 if err != nil {
273 beego.Error("分页查询书籍列表 => ", err, sqlQuery)
274 return
275 }
276
277 if err == nil && len(books) > 0 {
278 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 = ? ORDER BY doc.modify_time DESC LIMIT 1 "
279
280 for index, book := range books {
281 var text struct {
282 Account string
283 ModifyTime time.Time
284 }
285
286 err = o.Raw(sql, book.BookId).QueryRow(&text)
287 if err == nil {
288 books[index].LastModifyText = text.Account + " 于 " + text.ModifyTime.Format("2006-01-02 15:04:05")
289 }
290

Callers 9

IndexMethod · 0.45
HistoryMethod · 0.45
IndexMethod · 0.45
UsersMethod · 0.45
TagsMethod · 0.45
BooksMethod · 0.45
AttachListMethod · 0.45
ListMethod · 0.45
UserReleaseBookMethod · 0.45

Calls 5

TableNameWithPrefixMethod · 0.95
TableNameWithPrefixMethod · 0.95
NewRelationshipFunction · 0.85
NewMemberFunction · 0.85
appendFunction · 0.50

Tested by

no test coverage detected