获取收藏列表,查询书籍信息
(uid, p, listRows int, cid int, order ...string)
| 85 | |
| 86 | //获取收藏列表,查询书籍信息 |
| 87 | func (this *Star) List(uid, p, listRows int, cid int, order ...string) (cnt int64, books []StarResult, err error) { |
| 88 | //根据用户id查询用户的收藏,先从收藏表中查询book_id |
| 89 | o := orm.NewOrm() |
| 90 | var ( |
| 91 | count DataCount |
| 92 | args = []interface{}{uid} |
| 93 | ) |
| 94 | sqlCount := `select count(s.bid) cnt from md_books b left join md_star s on s.bid=b.book_id where s.uid=? and b.privately_owned=0` |
| 95 | if cid > 0 { |
| 96 | args = append(args, cid) |
| 97 | sqlCount = `select count(s.bid) cnt from md_books b left join md_star s on s.bid=b.book_id left join md_book_category bc on bc.book_id = s.bid where s.uid=? and bc.category_id = ? and b.privately_owned=0` |
| 98 | } |
| 99 | o.Raw(sqlCount, args...).QueryRow(&count) |
| 100 | //这里先暂时每次都统计一次用户的收藏数量。合理的做法是在用户表字段中增加一个收藏计数 |
| 101 | orderBy := "s.last_read desc" |
| 102 | if len(order) > 0 && order[0] == "new" { |
| 103 | orderBy = "s.id desc" |
| 104 | } |
| 105 | if cnt = count.Cnt; cnt > 0 { |
| 106 | sql := `select b.*,m.nickname from md_books b left join md_star s on s.bid=b.book_id left join md_members m on m.member_id=b.member_id where s.uid=? and b.privately_owned=0 order by %v limit %v offset %v` |
| 107 | if cid > 0 { |
| 108 | sql = `select b.*,m.nickname from md_books b left join md_star s on s.bid=b.book_id left join md_members m on m.member_id=b.member_id left join md_book_category bc on bc.book_id = s.bid where s.uid=? and bc.category_id = ? and b.privately_owned=0 order by %v limit %v offset %v` |
| 109 | } |
| 110 | sql = fmt.Sprintf(sql, orderBy, listRows, (p-1)*listRows) |
| 111 | _, err = o.Raw(sql, args...).QueryRows(&books) |
| 112 | } |
| 113 | return |
| 114 | } |
| 115 | |
| 116 | func (this *Star) SetLastReadTime(uid, bid int) { |
| 117 | orm.NewOrm().QueryTable(this).Filter("uid", uid).Filter("bid", bid).Update(orm.Params{"last_read": time.Now().Unix()}) |