yb_community_question_like_roast.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package yb_community_question
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/models/tables/yb_community_question_like_roast"
  7. "time"
  8. )
  9. // SetLikeOrRoast 用户对问答进行(取消)点赞或(取消)吐槽
  10. func SetLikeOrRoast(userId uint64, communityQuestionId uint32, opType, enable, sourceAgent int8) (ybCommunityQuestionLikeRoast *yb_community_question_like_roast.YbCommunityQuestionLikeRoast, likeNum, roastNum int64, err error, errMsg string) {
  11. //user.UserID
  12. defer func() {
  13. if err != nil {
  14. global.LOG.Critical(fmt.Sprintf("SetLike: userId=%d, err:%s, errMsg:%s", userId, err.Error(), errMsg))
  15. }
  16. }()
  17. if sourceAgent == 0 {
  18. err = errors.New("请输入来源")
  19. return
  20. }
  21. if communityQuestionId <= 0 {
  22. err = errors.New("请输入问答ID")
  23. return
  24. }
  25. // 判断是否有点赞权限(不需要权限校验,先保留,有需要后续再说)
  26. //err, errMsg = services.CheckSimpleCompanyProduct(user)
  27. //if err != nil {
  28. // return
  29. //}
  30. //查询用户对问答的点赞/吐槽状态
  31. ybCommunityQuestionLikeRoast, err = yb_community_question_like_roast.GetByUserIdAndCommunityQuestionId(userId, communityQuestionId)
  32. if err != nil {
  33. errMsg = "查询点赞/吐槽记录出错"
  34. return
  35. }
  36. //如果未点赞则新增点赞记录
  37. now := time.Now()
  38. if ybCommunityQuestionLikeRoast.CommunityQuestionLikeRoastID <= 0 {
  39. ybCommunityQuestionLikeRoast = &yb_community_question_like_roast.YbCommunityQuestionLikeRoast{
  40. //CommunityQuestionLikeRoastID: 0,
  41. UserID: userId,
  42. OpType: opType,
  43. Enabled: 1,
  44. CreateTime: now,
  45. ModifyTime: now,
  46. CommunityQuestionID: communityQuestionId,
  47. SourceAgent: sourceAgent,
  48. }
  49. err = ybCommunityQuestionLikeRoast.Create()
  50. if err != nil {
  51. errMsg = err.Error()
  52. err = errors.New("新增点赞/吐槽记录出错")
  53. return
  54. }
  55. } else {
  56. ybCommunityQuestionLikeRoast.OpType = opType
  57. ybCommunityQuestionLikeRoast.Enabled = enable
  58. ybCommunityQuestionLikeRoast.ModifyTime = now
  59. updates := []string{"op_type", "enabled", "modify_time"}
  60. err = ybCommunityQuestionLikeRoast.Update(updates)
  61. if err != nil {
  62. errMsg = "更新点赞记录出错"
  63. return
  64. }
  65. }
  66. //查询总的点赞数
  67. likeNum, err = yb_community_question_like_roast.GetLikeNumByCommunityQuestionId(communityQuestionId)
  68. if err != nil {
  69. errMsg = err.Error()
  70. err = errors.New("查询点赞数出错")
  71. }
  72. roastNum, err = yb_community_question_like_roast.GetRoastNumByCommunityQuestionId(communityQuestionId)
  73. if err != nil {
  74. errMsg = err.Error()
  75. err = errors.New("查询吐槽数出错")
  76. }
  77. return
  78. }