query.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package yb_community_question_like_tease
  2. import (
  3. "hongze/hongze_yb/global"
  4. "hongze/hongze_yb/utils"
  5. )
  6. // GetByUserIdAndCommunityQuestionId 根据用户和问答id获取点赞/吐槽记录
  7. func GetByUserIdAndCommunityQuestionId(userId uint64, communityQuestionId uint32) (item *YbCommunityQuestionLikeTease, err error) {
  8. err = global.DEFAULT_MYSQL.
  9. Where("user_id = ? AND community_question_id = ?", userId, communityQuestionId).
  10. First(&item).Error
  11. if err == utils.ErrNoRow {
  12. err = nil
  13. }
  14. return
  15. }
  16. // GetLikeNumByCommunityQuestionId 获取某个问答的点赞数
  17. func GetLikeNumByCommunityQuestionId(communityQuestionId uint32) (num int64, err error) {
  18. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
  19. Where(" community_question_id = ? AND enabled=1 AND op_type = 1 ", communityQuestionId).
  20. Count(&num).Error
  21. return
  22. }
  23. // GetTeaseNumByCommunityQuestionId 获取某个问答的吐槽数
  24. func GetTeaseNumByCommunityQuestionId(communityQuestionId uint32) (num int64, err error) {
  25. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
  26. Where(" community_question_id = ? AND enabled=1 AND op_type = 2 ", communityQuestionId).
  27. Count(&num).Error
  28. return
  29. }
  30. // NumCommentByCommunityQuestionIds 评论统计结构体
  31. type NumCommentByCommunityQuestionIds struct {
  32. CommunityQuestionID uint32 `json:"community_question_id"`
  33. Total int `json:"total"`
  34. }
  35. // GetLikeNumCommentByCommunityQuestionIds 根据问答id列表获取所有问答的点赞数
  36. func GetLikeNumCommentByCommunityQuestionIds(communityQuestionIds []uint32) (items []*NumCommentByCommunityQuestionIds, err error) {
  37. if len(communityQuestionIds) <= 0 {
  38. return
  39. }
  40. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
  41. Select("community_question_id, count(1) total").
  42. Where("community_question_id in (?) AND enabled=1 AND op_type=1", communityQuestionIds).Group("community_question_id").Scan(&items).Error
  43. return
  44. }
  45. // GetTeaseNumCommentByCommunityQuestionIds 根据问答id列表获取所有问答的吐槽数
  46. func GetTeaseNumCommentByCommunityQuestionIds(communityQuestionIds []uint32) (items []*NumCommentByCommunityQuestionIds, err error) {
  47. if len(communityQuestionIds) <= 0 {
  48. return
  49. }
  50. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
  51. Select("community_question_id, count(1) total").
  52. Where("community_question_id in (?) AND enabled=1 AND op_type=2", communityQuestionIds).Group("community_question_id").Scan(&items).Error
  53. return
  54. }
  55. // GetByUserIdAndCommunityQuestionIds 根据用户id和问答id列表获取成功点赞/吐槽记录
  56. func GetByUserIdAndCommunityQuestionIds(userId uint64, communityQuestionIds []uint32) (items []*YbCommunityQuestionLikeTease, err error) {
  57. if len(communityQuestionIds) <= 0 {
  58. return
  59. }
  60. err = global.DEFAULT_MYSQL.
  61. Where("user_id = ? AND enabled=1 AND community_question_id in (?)", userId, communityQuestionIds).Group("community_question_id").
  62. Find(&items).Error
  63. return
  64. }