like.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package like
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/models/request/like"
  7. "hongze/hongze_yb/models/response"
  8. "hongze/hongze_yb/models/tables/yb_like"
  9. "hongze/hongze_yb/services"
  10. "hongze/hongze_yb/services/user"
  11. "time"
  12. )
  13. // 设置点赞
  14. func SetLike(user user.UserInfo, req like.ReqLike) (resp response.RespLike, err error) {
  15. var errMsg string
  16. defer func() {
  17. if err != nil {
  18. global.LOG.Critical(fmt.Sprintf("SetLike: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  19. }
  20. }()
  21. reportId := req.ReportId
  22. reportChapterId := req.ReportChapterId
  23. if req.SourceAgent == 0 {
  24. err = errors.New("请输入点赞来源")
  25. return
  26. }
  27. if req.ReportChapterId <=0 && req.ReportId <=0 && req.OldReportId <=0 && req.OldReportChapterId <=0{
  28. err = errors.New("请输入报告ID")
  29. return
  30. }
  31. if (req.ReportChapterId > 0 && req.ReportId <=0 ) || (req.OldReportChapterId > 0 && req.OldReportId <= 0){
  32. err = errors.New("请输入报告ID")
  33. return
  34. }
  35. // 判断是否有点赞权限
  36. err, errMsg = services.CheckSimpleCompanyProduct(user)
  37. if err != nil {
  38. return
  39. }
  40. //处理老报告, 转化成新报告ID
  41. if req.ReportChapterId <=0 && req.ReportId <=0 && (req.OldReportId >0 || req.OldReportChapterId > 0) {
  42. reportId, reportChapterId, err, errMsg = services.GetReportIdReportChapterIdByOldReportId(uint64(req.OldReportId), uint64(req.OldReportChapterId))
  43. if err != nil {
  44. return
  45. }
  46. } else {
  47. // 判断报告ID是否正确
  48. err, errMsg = services.CheckReportExistByReportIdReportChapterId(reportId, reportChapterId)
  49. if err != nil {
  50. return
  51. }
  52. }
  53. //查询用户对研报的点赞状态
  54. var likeItem *yb_like.YbLike
  55. if reportId > 0 {
  56. likeItem, err = yb_like.GetLikeByUserIdAndReportId(user.UserID, reportId, reportChapterId)
  57. } else {
  58. likeItem, err = yb_like.GetLikeByUserIdAndOldReportId(user.UserID, req.OldReportId, req.OldReportChapterId)
  59. }
  60. if err != nil {
  61. errMsg = err.Error()
  62. err = errors.New("查询用户点赞记录出错")
  63. return
  64. }
  65. //如果未点赞则新增点赞记录
  66. now := time.Now()
  67. if likeItem.LikeId <= 0 {
  68. likeItem = &yb_like.YbLike{
  69. ReportId: reportId,
  70. ReportChapterId: reportChapterId,
  71. UserId: user.UserID,
  72. Enabled: 1,
  73. CreateTime: now,
  74. ModifyTime: now,
  75. SourceAgent: req.SourceAgent,
  76. OldReportId: req.OldReportId,
  77. OldReportChapterId: req.OldReportChapterId,
  78. }
  79. err = likeItem.Create()
  80. if err != nil {
  81. errMsg = err.Error()
  82. err = errors.New("新增点赞记录出错")
  83. return
  84. }
  85. } else {
  86. //如果已点赞则更新成取消点赞
  87. if likeItem.Enabled == 1 {
  88. updates := []string{"enabled", "modify_time"}
  89. likeItem.Enabled = 0
  90. likeItem.ModifyTime = now
  91. err = likeItem.Update(updates)
  92. if err != nil {
  93. errMsg = err.Error()
  94. err = errors.New("更新点赞记录出错")
  95. return
  96. }
  97. } else {
  98. //如果取消点赞则更新成已点赞
  99. updates := []string{"enabled", "modify_time"}
  100. likeItem.Enabled = 1
  101. likeItem.ModifyTime = now
  102. err = likeItem.Update(updates)
  103. if err != nil {
  104. errMsg = err.Error()
  105. err = errors.New("更新点赞记录出错")
  106. return
  107. }
  108. }
  109. }
  110. //查询总的点赞数
  111. var likeNum int64
  112. if reportId > 0 {
  113. likeNum, err = yb_like.GetLikeNumByReportId(reportId, reportChapterId)
  114. }else{
  115. likeNum, err = yb_like.GetLikeNumByOldReportId(req.OldReportId, req.OldReportChapterId)
  116. }
  117. if err != nil {
  118. errMsg = err.Error()
  119. err = errors.New("查询点赞数出错")
  120. return
  121. }
  122. resp.LikeEnabled = likeItem.Enabled
  123. resp.LikeNum = likeNum
  124. return
  125. }