query.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, source int) (list []*YbCommunityQuestionComment, err error) {
  16. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  17. Where("user_id = ? AND community_question_id = ? AND enabled = 1 AND source = ?", userId, communityQuestionID, source).
  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, source int) (total int64, err error) {
  27. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  28. Where("user_id = ? AND community_question_id = ? AND enabled = 1 AND source = ?", userId, communityQuestionID, source).
  29. Count(&total).Error
  30. return
  31. }
  32. // GetHotListByCommunityQuestionID 获取报告的精选留言列表(2022-08-23修改为获取所有留言, 不再精选)
  33. func GetHotListByCommunityQuestionID(communityQuestionID int, offset, limit, source int) (list []*YbCommunityQuestionComment, err error) {
  34. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  35. Where("community_question_id = ? AND enabled = 1 AND type = 1 AND source = ?", communityQuestionID, source).
  36. //Order("is_top desc, hot_top_time desc, community_question_comment_id desc").
  37. Order("create_time DESC").
  38. Offset(offset).
  39. Limit(limit).
  40. Scan(&list).Error
  41. return
  42. }
  43. // GetHotListTotalByCommunityQuestionID 获取精选留言总条数(2022-08-23修改为获取所有留言, 不再精选)
  44. func GetHotListTotalByCommunityQuestionID(communityQuestionID, source int) (total int64, err error) {
  45. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  46. Where("community_question_id = ? AND enabled = 1 AND type = 1 AND source = ?", communityQuestionID, source).
  47. Count(&total).Error
  48. return
  49. }
  50. // GetReplyListByReplyCommentId 获取报告的精选留言的回复列表
  51. func GetReplyListByReplyCommentId(replyCommentIds []uint64) (list []*YbCommunityQuestionComment, err error) {
  52. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  53. Where("enabled = 1 and type = 2 and reply_comment_id in ?", replyCommentIds).
  54. Order("community_question_comment_id asc").
  55. Scan(&list).Error
  56. return
  57. }
  58. type SimpleYbCommunityQuestionComment struct {
  59. CommunityQuestionCommentID uint64 `gorm:"primaryKey;column:community_question_comment_id;type:bigint(20) unsigned;not null" json:"-"`
  60. UserID uint64 `gorm:"column:user_id;type:bigint(20) unsigned;not null;default:0" json:"userId"` // 用户id
  61. Type int8 `gorm:"column:type;type:tinyint(1);not null;default:1" json:"type"` // 留言类型 1-评论 2-回复
  62. Enabled int8 `gorm:"column:enabled;type:tinyint(1);not null;default:1" json:"enabled"` // 是否有效, 0-无效留言 1-有效留言
  63. }
  64. // GetSimpleByCommentId 根据留言ID,查询留言
  65. func GetSimpleByCommentId(commentId uint64) (item *SimpleYbCommunityQuestionComment, err error) {
  66. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  67. Select("community_question_comment_id, user_id, type, enabled").
  68. Where("community_question_comment_id = ?", commentId).First(&item).Error
  69. if err == utils.ErrNoRow {
  70. err = nil
  71. }
  72. return
  73. }
  74. // GetMyLatestComment 获取用户最新的留言
  75. func GetMyLatestComment(userId uint64) (item *YbCommunityQuestionComment, err error) {
  76. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  77. Select("comment_id, user_id, type, enabled, is_show_name").
  78. Where("user_id = ? and type=1", userId).Order("community_question_comment_id desc").First(&item).Error
  79. return
  80. }
  81. // NumCommentByCommunityQuestion 评论统计结构体
  82. type NumCommentByCommunityQuestion struct {
  83. CommunityQuestionID uint32 `json:"community_question_id"`
  84. Total int `json:"total"`
  85. }
  86. // GetNumCommentByCommunityQuestionIds 获取用户留言的汇总数
  87. func GetNumCommentByCommunityQuestionIds(communityQuestionIds []uint32, userId uint64) (item []*NumCommentByCommunityQuestion, err error) {
  88. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  89. Select("community_question_id, count(1) total").
  90. Where("community_question_id in (?) and type=1 and enabled = 1 and (is_hot = 1 or user_id=?) ", communityQuestionIds, userId).Group("community_question_id").Scan(&item).Error
  91. return
  92. }
  93. // GetLastHotListByCommunityQuestionIds 根据问答id列表获取最近精选的留言
  94. func GetLastHotListByCommunityQuestionIds(communityQuestionIds []uint32) (items []*YbCommunityQuestionComment, err error) {
  95. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  96. 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).
  97. Find(&items).Error
  98. return
  99. }
  100. // GetLastMyListByCommunityQuestionIds 根据问答id列表获取我最近的留言
  101. func GetLastMyListByCommunityQuestionIds(userId uint64, communityQuestionIds []uint32) (items []*YbCommunityQuestionComment, err error) {
  102. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  103. 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).
  104. Find(&items).Error
  105. return
  106. }
  107. type UserAvatarRandom struct {
  108. AvatarUrl string
  109. }
  110. func GetUserAvatarRandom() (item *UserAvatarRandom, err error) {
  111. sql := ` select * from user_avatar_random order by rand() limit 1 `
  112. err = global.DEFAULT_MYSQL.Raw(sql).Scan(&item).Error
  113. return
  114. }
  115. // GetHotListByCommunityQuestionIds 根据问答id列表获取精选留言-精选时间倒序(2022-0823不再精选,获取所有评论)
  116. func GetHotListByCommunityQuestionIds(communityQuestionIds []uint32, source int) (items []*YbCommunityQuestionComment, err error) {
  117. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
  118. Where("community_question_id IN (?) AND enabled = 1 AND type = 1 AND source = ?", communityQuestionIds, source).
  119. Order("hot_time DESC").
  120. Find(&items).Error
  121. return
  122. }