获取可显示的评论内容
(p, listRows int, opt CommentOpt)
| 79 | |
| 80 | // 获取可显示的评论内容 |
| 81 | func (this *Comments) Comments(p, listRows int, opt CommentOpt) (comments []BookCommentsResult, err error) { |
| 82 | sql := `select c.id,c.content,s.score,c.uid,c.status,c.pid,c.doc_id,c.time_create,m.avatar,m.nickname,m.account,b.book_id,b.book_name,b.identify from md_comments c left join md_members m on m.member_id=c.uid left join md_score s on s.uid=c.uid and s.book_id=c.book_id left join md_books b on b.book_id = c.book_id %v order by c.id desc limit %v offset %v` |
| 83 | whereStr := "" |
| 84 | whereSlice := []string{"true"} |
| 85 | if opt.BookId > 0 { |
| 86 | whereSlice = append(whereSlice, "c.book_id = "+strconv.Itoa(opt.BookId)) |
| 87 | } |
| 88 | if opt.WithoutDocComment { // 不加载文档的评论 |
| 89 | whereSlice = append(whereSlice, "c.doc_id = 0") |
| 90 | } else { |
| 91 | if opt.DocId > 0 { |
| 92 | whereSlice = append(whereSlice, "c.doc_id = "+strconv.Itoa(opt.DocId)) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if len(opt.Status) > 0 { |
| 97 | whereSlice = append(whereSlice, "c.status = "+strconv.Itoa(opt.Status[0])) |
| 98 | } |
| 99 | |
| 100 | if len(whereSlice) > 0 { |
| 101 | whereStr = " where " + strings.Join(whereSlice, " and ") |
| 102 | } |
| 103 | |
| 104 | sql = fmt.Sprintf(sql, whereStr, listRows, (p-1)*listRows) |
| 105 | _, err = orm.NewOrm().Raw(sql).QueryRows(&comments) |
| 106 | if len(comments) == 0 { |
| 107 | return |
| 108 | } |
| 109 | |
| 110 | commentMap := make(map[int]BookCommentsResult) |
| 111 | for _, comment := range comments { |
| 112 | commentMap[comment.Id] = comment |
| 113 | } |
| 114 | for idx, comment := range comments { |
| 115 | if val, ok := commentMap[comment.Pid]; ok { |
| 116 | if val.Nickname == "" { |
| 117 | val.Nickname = "匿名" |
| 118 | } |
| 119 | comment.ReplyToUser = val.Nickname |
| 120 | comment.ReplyToContent = val.Content |
| 121 | } |
| 122 | if comment.Nickname == "" { |
| 123 | comment.Nickname = "匿名" |
| 124 | } |
| 125 | comments[idx] = comment |
| 126 | } |
| 127 | return |
| 128 | } |
| 129 | |
| 130 | type CommentCount struct { |
| 131 | Id int |