fold.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/app/job/main/reply/model/reply"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. func (s *Service) folderHanlder(ctx context.Context, msg *consumerMsg) {
  11. var d struct {
  12. Op string `json:"op"`
  13. Oid int64 `json:"oid"`
  14. Tp int8 `json:"tp"`
  15. Root int64 `json:"root"`
  16. }
  17. if err := json.Unmarshal([]byte(msg.Data), &d); err != nil {
  18. log.Error("json.Unmarshal() error(%v)", err)
  19. return
  20. }
  21. switch d.Op {
  22. case "re_idx":
  23. s.recoverFolderIdx(ctx, d.Oid, d.Tp, d.Root)
  24. // case "marker":
  25. // 删除折叠评论后需要查询是否需要取消标记
  26. // s.marker.Do(ctx, func(ctx context.Context) {
  27. // s.handleHasFoldedMark(ctx, d.Oid, d.Tp, d.Root)
  28. // })
  29. default:
  30. return
  31. }
  32. }
  33. // handleFolded ...
  34. func (s *Service) handleFolded(ctx context.Context, rp *reply.Reply) {
  35. sub, root, err := s.handleHasFoldedMark(ctx, rp.Oid, rp.Type, rp.Root)
  36. if err != nil {
  37. return
  38. }
  39. if sub != nil {
  40. s.dao.Mc.DeleteSub(ctx, sub.Oid, sub.Type)
  41. }
  42. if root != nil {
  43. s.dao.Mc.DeleteReply(ctx, root.RpID)
  44. }
  45. s.remFoldedCache(ctx, rp)
  46. }
  47. func (s *Service) recoverFolderIdx(ctx context.Context, oid int64, tp int8, root int64) {
  48. rps, err := s.dao.Reply.FoldedReplies(ctx, oid, tp, root)
  49. if err != nil || len(rps) == 0 {
  50. return
  51. }
  52. // 折叠根评论
  53. if root == 0 {
  54. s.dao.Redis.AddFolderBatch(ctx, reply.FolderKindSub, oid, rps)
  55. } else {
  56. s.dao.Redis.AddFolderBatch(ctx, reply.FolderKindRoot, root, rps)
  57. }
  58. }
  59. func (s *Service) handleHasFoldedMark(ctx context.Context, oid int64, tp int8, root int64) (sub *reply.Subject, reply *reply.Reply, err error) {
  60. var (
  61. tx *sql.Tx
  62. count int
  63. )
  64. if tx, err = s.dao.BeginTran(ctx); err != nil {
  65. return
  66. }
  67. // 锁subject表
  68. if sub, err = s.dao.Subject.GetForUpdate(tx, oid, tp); err != nil {
  69. tx.Rollback()
  70. return
  71. }
  72. if count, err = s.dao.Reply.TxCountFoldedReplies(tx, oid, tp, root); err != nil || count > 0 {
  73. tx.Rollback()
  74. return
  75. }
  76. // 折叠根评论
  77. if root == 0 {
  78. if !sub.HasFolded() {
  79. tx.Rollback()
  80. return
  81. }
  82. sub.UnmarkHasFolded()
  83. if _, err = s.dao.Subject.TxUpAttr(tx, oid, tp, sub.Attr, time.Now()); err != nil {
  84. tx.Rollback()
  85. return
  86. }
  87. } else {
  88. if reply, err = s.dao.Reply.GetForUpdate(tx, oid, root); err != nil {
  89. tx.Rollback()
  90. return
  91. }
  92. if !reply.HasFolded() {
  93. tx.Rollback()
  94. return
  95. }
  96. reply.UnmarkHasFolded()
  97. if _, err = s.dao.Reply.TxUpAttr(tx, oid, root, reply.Attr, time.Now()); err != nil {
  98. tx.Rollback()
  99. return
  100. }
  101. }
  102. if err = tx.Commit(); err != nil {
  103. return
  104. }
  105. return
  106. }
  107. // remFoldedCache ...
  108. func (s *Service) remFoldedCache(ctx context.Context, rp *reply.Reply) {
  109. if rp.IsRoot() {
  110. s.dao.Redis.RemFolder(ctx, reply.FolderKindSub, rp.Oid, rp.RpID)
  111. } else {
  112. s.dao.Redis.RemFolder(ctx, reply.FolderKindRoot, rp.Root, rp.RpID)
  113. }
  114. }