like.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package yb
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type Like struct {
  7. LikeId uint64 `orm:"column(like_id);pk" description:"消息序号"`
  8. UserId uint64 `orm:"column(user_id)" description:"用户id"`
  9. Enabled int8 `orm:"column(enabled)" description:"状态. 0-已取消赞 1-有效赞"`
  10. CreateTime time.Time `orm:"column(create_time)" description:"创建时间"`
  11. ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
  12. ReportId int `orm:"column(report_id)" description:"报告ID"`
  13. ReportChapterId int `orm:"column(report_chapter_id)" description:"报告章节ID"`
  14. SourceAgent int `orm:"column(source_agent)" description:"留言入口来源,1:小程序,2:小程序pc,4:web pc"`
  15. }
  16. type StatReportLikeItem struct {
  17. ReportId int `description:"报告ID"`
  18. ReportChapterId int `description:"报告章节ID"`
  19. LikeNum int `description:"点赞数"`
  20. ModifyTime time.Time `description:"最新点赞时间"`
  21. }
  22. // GetLikeReportIds 根据条件查询点赞相关的 report
  23. func GetLikeReportIds(orderType string) (list []*StatReportLikeItem, err error) {
  24. o := orm.NewOrm()
  25. sql := `SELECT
  26. count( * ) AS like_num,
  27. report_id,
  28. report_chapter_id,
  29. MAX(modify_time) as modify_time
  30. FROM
  31. yb_like
  32. WHERE
  33. enabled = 1
  34. GROUP BY
  35. report_id,
  36. report_chapter_id
  37. ORDER BY
  38. like_num`
  39. if orderType != "" {
  40. sql += ` `+orderType
  41. }else{
  42. sql += ` desc`
  43. }
  44. _, err = o.Raw(sql).QueryRows(&list)
  45. return
  46. }
  47. // GetLikeListByReportId 根据报告ID,查询点赞列表
  48. func GetLikeListByReportId(reportId, reportChapterId, startSize, pageSize int)(list []*Like, err error) {
  49. o := orm.NewOrm()
  50. sql := `SELECT *
  51. FROM
  52. yb_like
  53. WHERE report_id=? and report_chapter_id=? and enabled=1
  54. ORDER BY modify_time DESC , like_id DESC LIMIT ?,?`
  55. _, err = o.Raw(sql, reportId, reportChapterId, startSize, pageSize).QueryRows(&list)
  56. return
  57. }
  58. // GetLikeListTotalByReportId 根据报告ID,查询点赞列表总数
  59. func GetLikeListTotalByReportId(reportId, reportChapterId int)(total int64, err error) {
  60. o := orm.NewOrm()
  61. sql := `SELECT count(*)
  62. FROM
  63. yb_like
  64. WHERE report_id=? and report_chapter_id=? and enabled=1`
  65. err = o.Raw(sql, reportId, reportChapterId).QueryRow(&total)
  66. return
  67. }