清空评论
(uid int)
| 135 | |
| 136 | // 清空评论 |
| 137 | func (this *Comments) ClearComments(uid int) { |
| 138 | var ( |
| 139 | comments []CommentCount |
| 140 | cid []interface{} |
| 141 | bookId []interface{} |
| 142 | bookIdMap = make(map[int]int) |
| 143 | ) |
| 144 | o := orm.NewOrm() |
| 145 | sql := "select count(id) cnt,id,book_id from md_comments where uid = ? group by book_id" |
| 146 | o.Raw(sql, uid).QueryRows(&comments) |
| 147 | fmt.Printf("%+v", comments) |
| 148 | if len(comments) > 0 { |
| 149 | for _, comment := range comments { |
| 150 | cid = append(cid, comment.Id) |
| 151 | bookId = append(bookId, comment.BookId) |
| 152 | bookIdMap[comment.BookId] = comment.Cnt |
| 153 | } |
| 154 | var err error |
| 155 | o.Begin() |
| 156 | defer func() { |
| 157 | if err != nil { |
| 158 | beego.Error(err) |
| 159 | o.Rollback() |
| 160 | } else { |
| 161 | o.Commit() |
| 162 | } |
| 163 | }() |
| 164 | _, err = o.QueryTable(this).Filter("book_id__in", bookId...).Delete() |
| 165 | if err != nil { |
| 166 | return |
| 167 | } |
| 168 | sqlUpdate := "update md_books set cnt_comment = cnt_comment - ? where cnt_comment> ? and book_id = ?" |
| 169 | for bid, cnt := range bookIdMap { |
| 170 | if _, err = o.Raw(sqlUpdate, cnt, cnt-1, bid).Exec(); err != nil { |
| 171 | return |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // 删除评论 |
| 178 | func (this *Comments) DeleteComment(id int) { |