like.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. actmdl "go-common/app/interface/main/activity/model/like"
  6. l "go-common/app/job/main/activity/model/like"
  7. "go-common/library/log"
  8. )
  9. // AddLike like add data update cache .
  10. func (s *Service) AddLike(c context.Context, addMsg json.RawMessage) (err error) {
  11. var (
  12. likeObj = new(l.Item)
  13. )
  14. if err = json.Unmarshal(addMsg, likeObj); err != nil {
  15. log.Error("AddLike json.Unmarshal(%s) error(%+v)", addMsg, err)
  16. return
  17. }
  18. if err = s.actRPC.LikeUp(c, &actmdl.ArgLikeUp{Lid: likeObj.ID}); err != nil {
  19. log.Error("s.actRPC.LikeUp(%d) error(%+v)", likeObj.ID, err)
  20. return
  21. }
  22. if err = s.actRPC.AddLikeCtimeCache(c, &actmdl.ArgLikeUp{Lid: likeObj.ID}); err != nil {
  23. log.Error("s.actRPC.AddLikeCtimeCache(%d) error(%+v)", likeObj.ID, err)
  24. return
  25. }
  26. log.Info("AddLike success s.actRPC.LikeUp(%d)", likeObj.ID)
  27. return
  28. }
  29. // UpLike update likes data update cahce
  30. func (s *Service) UpLike(c context.Context, newMsg, oldMsg json.RawMessage) (err error) {
  31. var (
  32. likeObj = new(l.Item)
  33. oldObj = new(l.Item)
  34. )
  35. if err = json.Unmarshal(newMsg, likeObj); err != nil {
  36. log.Error("UpLike json.Unmarshal(%s) error(%+v)", newMsg, err)
  37. return
  38. }
  39. if err = json.Unmarshal(oldMsg, oldObj); err != nil {
  40. log.Error("UpLike json.Unmarshal(%s) error(%+v)", oldMsg, err)
  41. return
  42. }
  43. if err = s.actRPC.LikeUp(c, &actmdl.ArgLikeUp{Lid: likeObj.ID}); err != nil {
  44. log.Error("s.actRPC.LikeUp(%d) error(%+v)", likeObj.ID, err)
  45. return
  46. }
  47. if oldObj.State != likeObj.State {
  48. if likeObj.State == 1 {
  49. //add ctime cache
  50. if err = s.actRPC.AddLikeCtimeCache(c, &actmdl.ArgLikeUp{Lid: likeObj.ID}); err != nil {
  51. log.Error("s.actRPC.AddLikeCtimeCache(%d) error(%+v)", likeObj.ID, err)
  52. return
  53. }
  54. } else {
  55. //del ctime cahce
  56. delItem := &actmdl.ArgLikeItem{
  57. ID: likeObj.ID,
  58. Sid: likeObj.Sid,
  59. Type: likeObj.Type,
  60. }
  61. if err = s.actRPC.DelLikeCtimeCache(c, delItem); err != nil {
  62. log.Error("s.actRPC.DelLikeCtimeCache(%v) error(%+v)", likeObj, err)
  63. return
  64. }
  65. }
  66. }
  67. log.Info("UpLike success s.actRPC.LikeUp(%d)", likeObj.ID)
  68. return
  69. }
  70. // DelLike delete like update cache
  71. func (s *Service) DelLike(c context.Context, oldMsg json.RawMessage) (err error) {
  72. var (
  73. likeObj = new(l.Item)
  74. )
  75. if err = json.Unmarshal(oldMsg, likeObj); err != nil {
  76. log.Error("DelLike json.Unmarshal(%s) error(%+v)", oldMsg, err)
  77. return
  78. }
  79. if err = s.actRPC.LikeUp(c, &actmdl.ArgLikeUp{Lid: likeObj.ID}); err != nil {
  80. log.Error("s.actRPC.LikeUp(%d) error(%+v)", likeObj.ID, err)
  81. return
  82. }
  83. //del ctime cahce
  84. delItem := &actmdl.ArgLikeItem{
  85. ID: likeObj.ID,
  86. Sid: likeObj.Sid,
  87. Type: likeObj.Type,
  88. }
  89. if err = s.actRPC.DelLikeCtimeCache(c, delItem); err != nil {
  90. log.Error("s.actRPC.DelLikeCtimeCache(%v) error(%+v)", likeObj, err)
  91. return
  92. }
  93. log.Info("DelLike success s.actRPC.LikeUp(%d)", likeObj.ID)
  94. return
  95. }
  96. // upLikeContent .
  97. func (s *Service) upLikeContent(c context.Context, upMsg json.RawMessage) (err error) {
  98. var (
  99. cont = new(l.Content)
  100. )
  101. if err = json.Unmarshal(upMsg, cont); err != nil {
  102. log.Error("upLikeContent json.Unmarshal(%s) error(%+v)", upMsg, err)
  103. return
  104. }
  105. if err = s.dao.SetLikeContent(c, cont.ID); err != nil {
  106. log.Error("s.dao.SetLikeContent(%d) error(%+v)", cont.ID, err)
  107. }
  108. log.Info("upLikeContent success s.dao.SetLikeContent(%d)", cont.ID)
  109. return
  110. }