query.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, source int8) (item *YbCommunityQuestionLikeTease, err error) {
  8. err = global.DEFAULT_MYSQL.
  9. Where("user_id = ? AND community_question_id = ? AND source = ?", userId, communityQuestionId, source).
  10. First(&item).Error
  11. if err == utils.ErrNoRow {
  12. err = nil
  13. }
  14. return
  15. }
  16. // GetLikeNumByCommunityQuestionId 获取某个问答的点赞数
  17. func GetLikeNumByCommunityQuestionId(communityQuestionId uint32, source int) (num int64, err error) {
  18. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
  19. Where(" community_question_id = ? AND enabled=1 AND op_type = 1 AND source = ?", communityQuestionId, source).
  20. Count(&num).Error
  21. return
  22. }
  23. // GetTeaseNumByCommunityQuestionId 获取某个问答的吐槽数
  24. func GetTeaseNumByCommunityQuestionId(communityQuestionId uint32, source int) (num int64, err error) {
  25. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
  26. Where(" community_question_id = ? AND enabled=1 AND op_type = 2 AND source = ?", communityQuestionId, source).
  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, source int) (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 AND source = ?", communityQuestionIds, source).
  43. Group("community_question_id").Scan(&items).Error
  44. return
  45. }
  46. // GetTeaseNumCommentByCommunityQuestionIds 根据问答id列表获取所有问答的吐槽数
  47. func GetTeaseNumCommentByCommunityQuestionIds(communityQuestionIds []uint32, source int) (items []*NumCommentByCommunityQuestionIds, err error) {
  48. if len(communityQuestionIds) <= 0 {
  49. return
  50. }
  51. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeTease{}).
  52. Select("community_question_id, count(1) total").
  53. Where("community_question_id in (?) AND enabled=1 AND op_type=2 AND source = ?", communityQuestionIds, source).
  54. Group("community_question_id").Scan(&items).Error
  55. return
  56. }
  57. // GetByUserIdAndCommunityQuestionIds 根据用户id和问答id列表获取成功点赞/吐槽记录
  58. func GetByUserIdAndCommunityQuestionIds(userId uint64, communityQuestionIds []uint32, source int) (items []*YbCommunityQuestionLikeTease, err error) {
  59. if len(communityQuestionIds) <= 0 {
  60. return
  61. }
  62. err = global.DEFAULT_MYSQL.
  63. Where("user_id = ? AND enabled=1 AND source = ? AND community_question_id in (?)", userId, source, communityQuestionIds).Group("community_question_id").
  64. Find(&items).Error
  65. return
  66. }