action.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/reply/model"
  6. thumbup "go-common/app/service/main/thumbup/api"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. // ActionCount return action exact count.
  11. func (s *Service) ActionCount(c context.Context, rpID, oid, adminID int64, typ int32) (like, hate int32, err error) {
  12. rp, err := s.dao.Reply(c, oid, rpID)
  13. if err != nil {
  14. return
  15. }
  16. if rp == nil {
  17. err = ecode.ReplyNotExist
  18. return
  19. }
  20. like = rp.Like
  21. hate = rp.Hate
  22. return
  23. }
  24. // UpActionLike update action like.
  25. func (s *Service) UpActionLike(c context.Context, rpID, oid, adminID int64, typ, count int32, remark string) (err error) {
  26. rp, err := s.dao.Reply(c, oid, rpID)
  27. if err != nil {
  28. return
  29. }
  30. if rp == nil {
  31. err = ecode.ReplyNotExist
  32. return
  33. }
  34. if _, err = s.thumbupClient.UpdateCount(c, &thumbup.UpdateCountReq{
  35. Business: "reply",
  36. OriginID: rp.Oid,
  37. MessageID: rpID,
  38. LikeChange: int64(count),
  39. Operator: fmt.Sprintf("%d", adminID),
  40. }); err != nil {
  41. log.Error("s.thumbupClient.UpdateCount (%d,%d,%d) failed!err:=%v", oid, rpID, int64(count), err)
  42. return
  43. }
  44. rp.Like += count
  45. if rp.Like < 0 {
  46. rp.Like = 0
  47. }
  48. if err = s.addReplyIndex(c, rp); err != nil {
  49. log.Error("s.addReplyIndex(%d,%d,%d) error(%v)", rp.ID, rp.Oid, rp.Type, err)
  50. }
  51. if err = s.dao.DelReplyCache(c, rp.ID); err != nil {
  52. log.Error("s.dao.DeleteReplyCache(%d) error(%v)", rp.ID, err)
  53. }
  54. s.cache.Do(c, func(ctx context.Context) {
  55. s.pubSearchReply(ctx, map[int64]*model.Reply{rp.ID: rp}, rp.State)
  56. })
  57. return
  58. }
  59. // UpActionHate update action hate.
  60. func (s *Service) UpActionHate(c context.Context, rpID, oid, adminID int64, typ, count int32, remark string) (err error) {
  61. rp, err := s.dao.Reply(c, oid, rpID)
  62. if err != nil {
  63. return
  64. }
  65. if rp == nil {
  66. err = ecode.ReplyNotExist
  67. return
  68. }
  69. if _, err = s.thumbupClient.UpdateCount(c, &thumbup.UpdateCountReq{
  70. Business: "reply",
  71. OriginID: rp.Oid,
  72. MessageID: rpID,
  73. DislikeChange: int64(count),
  74. Operator: fmt.Sprintf("%d", adminID),
  75. }); err != nil {
  76. log.Error("s.thumbupClient.UpdateCount (%d,%d,%d) failed!err:=%v", oid, rpID, int64(count), err)
  77. return
  78. }
  79. rp.Hate += count
  80. if rp.Hate < 0 {
  81. rp.Hate = 0
  82. }
  83. if err = s.addReplyIndex(c, rp); err != nil {
  84. log.Error("s.addReplyIndex(%d,%d,%d) error(%v)", rp.ID, rp.Oid, rp.Type, err)
  85. }
  86. if err = s.dao.DelReplyCache(c, rp.ID); err != nil {
  87. log.Error("s.dao.DeleteReplyCache(%d) error(%v)", rp.ID, err)
  88. }
  89. s.cache.Do(c, func(ctx context.Context) {
  90. s.pubSearchReply(ctx, map[int64]*model.Reply{rp.ID: rp}, rp.State)
  91. })
  92. return
  93. }