query.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package yb_community_question_comment
  2. import (
  3. "hongze/hongze_yb/global"
  4. "hongze/hongze_yb/utils"
  5. )
  6. // GetByCommunityQuestionCommentId 根据留言ID,查询留言
  7. func GetByCommunityQuestionCommentId(communityQuestionCommentId uint64) (item *YbCommunityQuestionComment, err error) {
  8. err = global.DEFAULT_MYSQL.Where("community_question_comment_id = ? ", communityQuestionCommentId).First(&item).Error
  9. if err == utils.ErrNoRow {
  10. err = nil
  11. }
  12. return
  13. }
  14. // GetListByUserIdCommunityQuestionID 获取用户的留言列表
  15. func GetListByUserIdCommunityQuestionID(userId uint64, communityQuestionID int) (list []*YbCommunityQuestionComment, err error) {
  16. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  17. Where("user_id = ? AND community_question_id = ? AND enabled = 1 ", userId, communityQuestionID).
  18. Order("create_time desc, community_question_comment_id desc").
  19. Scan(&list).Error
  20. if err == utils.ErrNoRow {
  21. err = nil
  22. }
  23. return
  24. }
  25. // GetListTotalByUserIdCommunityQuestionID 获取用户的留言总条数
  26. func GetListTotalByUserIdCommunityQuestionID(userId uint64, communityQuestionID int) (total int64, err error) {
  27. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  28. Where("user_id = ? AND community_question_id = ? AND enabled = 1 ", userId, communityQuestionID).
  29. Count(&total).Error
  30. return
  31. }
  32. // GetHotListByCommunityQuestionID 获取报告的精选留言列表
  33. func GetHotListByCommunityQuestionID(communityQuestionID int, offset, limit int) (list []*YbCommunityQuestionComment, err error) {
  34. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  35. Where("community_question_id = ? AND enabled = 1 AND is_hot = 1 AND type = 1", communityQuestionID).
  36. Order("is_top desc, hot_top_time desc, community_question_comment_id desc").
  37. Offset(offset).
  38. Limit(limit).
  39. Scan(&list).Error
  40. return
  41. }
  42. // GetHotListTotalByCommunityQuestionID 获取精选留言总条数
  43. func GetHotListTotalByCommunityQuestionID(communityQuestionID int) (total int64, err error) {
  44. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  45. Where("community_question_id = ? AND enabled = 1 AND is_hot = 1 AND type = 1", communityQuestionID).
  46. Count(&total).Error
  47. return
  48. }
  49. // GetReplyListByReplyCommentId 获取报告的精选留言的回复列表
  50. func GetReplyListByReplyCommentId(replyCommentIds []uint64) (list []*YbCommunityQuestionComment, err error) {
  51. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  52. Where("enabled = 1 and type = 2 and reply_comment_id in ?", replyCommentIds).
  53. Order("community_question_comment_id asc").
  54. Scan(&list).Error
  55. return
  56. }
  57. type SimpleYbCommunityQuestionComment struct {
  58. CommunityQuestionCommentID uint64 `gorm:"primaryKey;column:community_question_comment_id;type:bigint(20) unsigned;not null" json:"-"`
  59. UserID uint64 `gorm:"column:user_id;type:bigint(20) unsigned;not null;default:0" json:"userId"` // 用户id
  60. Type int8 `gorm:"column:type;type:tinyint(1);not null;default:1" json:"type"` // 留言类型 1-评论 2-回复
  61. Enabled int8 `gorm:"column:enabled;type:tinyint(1);not null;default:1" json:"enabled"` // 是否有效, 0-无效留言 1-有效留言
  62. }
  63. // GetSimpleByCommentId 根据留言ID,查询留言
  64. func GetSimpleByCommentId(commentId uint64) (item *SimpleYbCommunityQuestionComment, err error) {
  65. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  66. Select("community_question_comment_id, user_id, type, enabled").
  67. Where("community_question_comment_id = ?", commentId).First(&item).Error
  68. if err == utils.ErrNoRow {
  69. err = nil
  70. }
  71. return
  72. }
  73. // GetMyLatestComment 获取用户最新的留言
  74. func GetMyLatestComment(userId uint64) (item *YbCommunityQuestionComment, err error) {
  75. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  76. Select("comment_id, user_id, type, enabled, is_show_name").
  77. Where("user_id = ? and type=1", userId).Order("community_question_comment_id desc").First(&item).Error
  78. return
  79. }
  80. // NumCommentByCommunityQuestion 评论统计结构体
  81. type NumCommentByCommunityQuestion struct {
  82. CommunityQuestionID uint32 `json:"community_question_id"`
  83. Total int `json:"total"`
  84. }
  85. // GetNumCommentByCommunityQuestionIds 获取用户最新的留言
  86. func GetNumCommentByCommunityQuestionIds(communityQuestionIds []uint32) (item []*NumCommentByCommunityQuestion, err error) {
  87. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  88. Select("community_question_id, count(1) total").
  89. Where("community_question_id in (?) and type=1 and enabled = 1 ", communityQuestionIds).Scan(&item).Error
  90. return
  91. }
  92. // GetLastHotListByCommunityQuestionIds 根据问答id列表获取最近精选的留言
  93. func GetLastHotListByCommunityQuestionIds(communityQuestionIds []uint32) (items []*YbCommunityQuestionComment, err error) {
  94. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  95. Where("community_question_comment_id in (SELECT MAX(community_question_comment_id) AS community_question_comment_id FROM yb_community_question_comment WHERE community_question_id IN (?) AND enabled = 1 AND is_hot = 1 AND type = 1 GROUP BY community_question_id)", communityQuestionIds).
  96. Find(&items).Error
  97. return
  98. }
  99. // GetLastMyListByCommunityQuestionIds 根据问答id列表获取我最近的留言
  100. func GetLastMyListByCommunityQuestionIds(userId uint64, communityQuestionIds []uint32) (items []*YbCommunityQuestionComment, err error) {
  101. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  102. Where("community_question_comment_id in (SELECT MAX(community_question_comment_id) AS community_question_comment_id FROM yb_community_question_comment WHERE user_id=? AND community_question_id IN (?) AND enabled = 1 AND type = 1 GROUP BY community_question_id)", userId, communityQuestionIds).
  103. Find(&items).Error
  104. return
  105. }