neighbor_comment_del.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package neighbor
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. "property-garden/errors"
  14. "property-garden/impl/v1/oss_utils"
  15. dbmodel "property-garden/model"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. "strings"
  19. )
  20. func checkNeighborCommentDelParam(req *pb_v1.NeighborCommentDelRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. case req.Id < 1:
  25. return status.Error(10003, "id 不能为空")
  26. case req.Uid == 0 && !req.Admin:
  27. return status.Error(10003, "uid 不能为空")
  28. }
  29. return nil
  30. }
  31. func getSubCommentIds(id int64, db *gorm.DB, dbname string) ([]int64, error) {
  32. ret := []int64{id}
  33. ids := []int64{id}
  34. p := dbmodel.NewNeighborComment(dbname)
  35. for len(ids) > 0 {
  36. where := map[string]interface{}{
  37. "pid in":ids,
  38. }
  39. list, err := p.List(db, where, nil, -1, -1)
  40. if err != nil {
  41. return nil, errors.DataBaseError
  42. }
  43. ids = make([]int64, len(list))
  44. for i, v := range list {
  45. ids[i] = v.ID
  46. }
  47. ret = append(ret, ids...)
  48. }
  49. return ret, nil
  50. }
  51. func NeighborCommentDel(ctx context.Context, req *pb_v1.NeighborCommentDelRequest) (reply *pb_v1.NeighborCommentDelReply, err error) {
  52. reply = &pb_v1.NeighborCommentDelReply{}
  53. // 捕获各个task中的异常并返回给调用者
  54. defer func() {
  55. if r := recover(); r != nil {
  56. err = fmt.Errorf("%+v", r)
  57. e := &status.Status{}
  58. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  59. logger.Error("err",
  60. zap.String("system_err", err.Error()),
  61. zap.Stack("stacktrace"))
  62. }
  63. }
  64. }()
  65. // 参数检查
  66. err = checkNeighborCommentDelParam(req)
  67. if err != nil {
  68. return nil, err
  69. }
  70. dbname := utils.GetGardenDbName(req.GardenId)
  71. // 获取旧数据
  72. // 获取旧数据
  73. old := dbmodel.NewNeighborComment(dbname)
  74. where := map[string]interface{}{
  75. "id":req.Id,
  76. }
  77. err = old.Find(database.DB(), where)
  78. if err != nil && err != gorm.ErrRecordNotFound {
  79. return nil, errors.DataBaseError
  80. }
  81. if old.ID == 0 {
  82. return nil, errors.ErrRecordNotFound
  83. }
  84. if !req.Admin && old.Uid != req.Uid {
  85. return nil, status.Error(10003, "非本人不能删除")
  86. }
  87. article := dbmodel.NewNeighborArticle(dbname)
  88. comment := dbmodel.NewNeighborComment(dbname)
  89. like := dbmodel.NewNeighborLike(dbname)
  90. db := database.DB().Begin()
  91. // 删除本条评论
  92. if err = old.Delete(db, where); err != nil {
  93. db.Rollback()
  94. return nil, errors.DataBaseError
  95. }
  96. pids, err := getSubCommentIds(req.Id, db, dbname)
  97. if err != nil {
  98. db.Rollback()
  99. return nil, err
  100. }
  101. // 删除子评论
  102. where = map[string]interface{}{
  103. "pid in":pids,
  104. }
  105. if err = comment.Delete(db, where); err != nil {
  106. db.Rollback()
  107. return nil, errors.DataBaseError
  108. }
  109. // 删除评论相关点赞
  110. where = map[string]interface{}{
  111. "article_id":old.ArticleId,
  112. "like_type":2,
  113. "like_id in":pids,
  114. }
  115. if err = like.Delete(db, where); err != nil {
  116. db.Rollback()
  117. return nil, errors.DataBaseError
  118. }
  119. if old.Pid == 0 {
  120. // 减少文章评论数
  121. where = map[string]interface{}{
  122. "id":old.ArticleId,
  123. }
  124. values := map[string]interface{}{
  125. "comment_count":gorm.Expr("comment_count - ?", 1),
  126. }
  127. if err = article.Update(db, where, values); err != nil {
  128. db.Rollback()
  129. return nil, errors.DataBaseError
  130. }
  131. } else {
  132. // 减少父评论的评论数
  133. where = map[string]interface{}{
  134. "id":old.Pid,
  135. }
  136. values := map[string]interface{}{
  137. "sub_comment_count":gorm.Expr("sub_comment_count - ?", 1),
  138. }
  139. if err = comment.Update(db, where, values); err != nil {
  140. db.Rollback()
  141. return nil, errors.DataBaseError
  142. }
  143. }
  144. if err := oss_utils.OssObjAdd(nil, strings.Split(old.Pics, ";")); err != nil {
  145. db.Rollback()
  146. return nil, err
  147. }
  148. db.Commit()
  149. return reply, nil
  150. }