fold.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/reply/model"
  6. "go-common/library/database/sql"
  7. )
  8. const (
  9. _foldedReplies = "SELECT id,oid,type,mid,root,parent,dialog,count,rcount,`like`,floor,state,attr,ctime,mtime FROM reply_%d WHERE oid=? AND type=? AND root=? AND state=12"
  10. _countFoldedReplies = "SELECT COUNT(*) FROM reply_%d WHERE oid=? AND type=? AND root=? AND state=12"
  11. )
  12. // TxCountFoldedReplies ...
  13. func (d *Dao) TxCountFoldedReplies(tx *sql.Tx, oid int64, tp int32, root int64) (count int, err error) {
  14. if err = tx.QueryRow(fmt.Sprintf(_countFoldedReplies, hit(oid)), oid, tp, root).Scan(&count); err != nil {
  15. if err == sql.ErrNoRows {
  16. err = nil
  17. }
  18. return
  19. }
  20. return
  21. }
  22. // FoldedReplies ...
  23. func (d *Dao) FoldedReplies(ctx context.Context, oid int64, tp int32, root int64) (rps []*model.Reply, err error) {
  24. rows, err := d.dbSlave.Query(ctx, fmt.Sprintf(_foldedReplies, hit(oid)), oid, tp, root)
  25. if err != nil {
  26. return
  27. }
  28. defer rows.Close()
  29. for rows.Next() {
  30. r := new(model.Reply)
  31. if err = rows.Scan(&r.ID, &r.Oid, &r.Type, &r.Mid, &r.Root, &r.Parent, &r.Dialog, &r.Count, &r.RCount, &r.Like, &r.Floor, &r.State, &r.Attr, &r.CTime, &r.MTime); err != nil {
  32. return
  33. }
  34. rps = append(rps, r)
  35. }
  36. if err = rows.Err(); err != nil {
  37. return
  38. }
  39. return
  40. }
  41. // RemRdsByFold ...
  42. func (d *Dao) RemRdsByFold(ctx context.Context, roots []int64, childMap map[int64][]int64, sub *model.Subject, rpMap map[int64]*model.Reply) {
  43. var (
  44. keyMap = make(map[string][]int64)
  45. )
  46. // 评论列表缓存
  47. keyMap[keyMainIdx(sub.Oid, sub.Type, model.SortByFloor)] = roots
  48. keyMap[keyMainIdx(sub.Oid, sub.Type, model.SortByCount)] = roots
  49. keyMap[keyMainIdx(sub.Oid, sub.Type, model.SortByLike)] = roots
  50. for root, children := range childMap {
  51. // 评论详情页缓存
  52. keyMap[keyRootIdx(root)] = children
  53. for _, child := range children {
  54. // 对话列表的缓存
  55. if rp, ok := rpMap[child]; ok && rp.Dialog != 0 {
  56. keyMap[keyDialogIdx(rp.Dialog)] = append(keyMap[keyDialogIdx(rp.Dialog)], rp.ID)
  57. }
  58. }
  59. }
  60. d.RemReplyFromRedis(ctx, keyMap)
  61. }
  62. // AddRdsByFold ...
  63. func (d *Dao) AddRdsByFold(ctx context.Context, roots []int64, childMap map[int64][]int64, sub *model.Subject, rpMap map[int64]*model.Reply) {
  64. var (
  65. ok bool
  66. err error
  67. keyMapping = make(map[string][]*model.Reply)
  68. )
  69. if ok, err = d.ExpireFolder(ctx, model.FolderKindSub, sub.Oid); err != nil {
  70. return
  71. }
  72. if ok {
  73. key := keyFolderIdx(model.FolderKindSub, sub.Oid)
  74. for _, root := range roots {
  75. if rp, ok := rpMap[root]; ok {
  76. keyMapping[key] = append(keyMapping[key], rp)
  77. }
  78. }
  79. }
  80. // 这里不回源
  81. for root, children := range childMap {
  82. if ok, err = d.ExpireFolder(ctx, model.FolderKindRoot, root); err != nil {
  83. return
  84. }
  85. if ok {
  86. key := keyFolderIdx(model.FolderKindRoot, root)
  87. for _, child := range children {
  88. if rp, ok := rpMap[child]; ok {
  89. keyMapping[key] = append(keyMapping[key], rp)
  90. }
  91. }
  92. }
  93. }
  94. d.AddFolder(ctx, keyMapping)
  95. }