neighbor_comment_add.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/oss_utils"
  11. dbmodel "property-garden/model"
  12. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "time"
  15. "git.getensh.com/common/gopkgs/database"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "go.uber.org/zap"
  18. "google.golang.org/grpc/status"
  19. )
  20. func checkNeighborCommentAddParam(req *pb_v1.NeighborCommentAddRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. case req.Pid == 0 && req.ArticleId == 0:
  25. return status.Error(10003, "父id和文章id不能同时为空")
  26. case req.Pid > 0 && req.ArticleId > 0:
  27. return status.Error(10003, "父id和文章id智能选其一")
  28. case req.Content == "":
  29. return status.Error(10003, "内容不能为空")
  30. case req.Uid == 0:
  31. return status.Error(10003, "uid 不能为空")
  32. }
  33. return nil
  34. }
  35. func articleCommentAdd(req *pb_v1.NeighborCommentAddRequest, db *gorm.DB, dbname string) (err error) {
  36. article := dbmodel.NewNeighborArticle(dbname)
  37. where := map[string]interface{}{
  38. "id": req.ArticleId,
  39. }
  40. err = article.Find(db, where)
  41. if err != nil && err != gorm.ErrRecordNotFound {
  42. return errors.DataBaseError
  43. }
  44. if article.ID == 0 {
  45. return errors.ErrRecordNotFound
  46. }
  47. now := time.Now()
  48. p := dbmodel.TNeighborComment{
  49. ArticleId: req.ArticleId,
  50. ClassId: article.ClassId,
  51. Pid: 0,
  52. Content: req.Content,
  53. Pics: utils.StringJoin(req.Pics, ","),
  54. Uid: req.Uid,
  55. SubCommentCount: 0,
  56. CreatedAt: now,
  57. UpdatedAt: now,
  58. Likes: 0,
  59. UserIcon: req.UserIcon,
  60. NickName: req.NickName,
  61. }
  62. p.SetTable(dbname)
  63. err = p.Insert(db)
  64. if err != nil {
  65. return errors.DataBaseError
  66. }
  67. values := map[string]interface{}{
  68. "comment_count": gorm.Expr("comment_count + ?", 1),
  69. }
  70. err = article.Update(db, where, values)
  71. if err != nil {
  72. return errors.DataBaseError
  73. }
  74. return nil
  75. }
  76. func subCommentAdd(req *pb_v1.NeighborCommentAddRequest, db *gorm.DB, dbname string) (err error) {
  77. comment := dbmodel.NewNeighborComment(dbname)
  78. where := map[string]interface{}{
  79. "id": req.Pid,
  80. }
  81. err = comment.Find(db, where)
  82. if err != nil && err != gorm.ErrRecordNotFound {
  83. return errors.DataBaseError
  84. }
  85. if comment.ID == 0 {
  86. return errors.ErrRecordNotFound
  87. }
  88. now := time.Now()
  89. p := dbmodel.TNeighborComment{
  90. ArticleId: comment.ArticleId,
  91. ClassId: comment.ClassId,
  92. Pid: req.Pid,
  93. Content: req.Content,
  94. Pics: utils.StringJoin(req.Pics, ";"),
  95. Uid: req.Uid,
  96. SubCommentCount: 0,
  97. CreatedAt: now,
  98. UpdatedAt: now,
  99. Likes: 0,
  100. UserIcon: req.UserIcon,
  101. }
  102. p.SetTable(dbname)
  103. err = p.Insert(db)
  104. if err != nil {
  105. return errors.DataBaseError
  106. }
  107. values := map[string]interface{}{
  108. "sub_comment_count": gorm.Expr("sub_comment_count + ?", 1),
  109. }
  110. err = comment.Update(db, where, values)
  111. if err != nil {
  112. return errors.DataBaseError
  113. }
  114. return nil
  115. }
  116. func NeighborCommentAdd(ctx context.Context, req *pb_v1.NeighborCommentAddRequest) (reply *pb_v1.NeighborCommentAddReply, err error) {
  117. reply = &pb_v1.NeighborCommentAddReply{}
  118. // 捕获各个task中的异常并返回给调用者
  119. defer func() {
  120. if r := recover(); r != nil {
  121. err = fmt.Errorf("%+v", r)
  122. e := &status.Status{}
  123. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  124. logger.Error("err",
  125. zap.String("system_err", err.Error()),
  126. zap.Stack("stacktrace"))
  127. }
  128. }
  129. }()
  130. // 参数检查
  131. err = checkNeighborCommentAddParam(req)
  132. if err != nil {
  133. return nil, err
  134. }
  135. dbname := utils.GetGardenDbName(req.GardenId)
  136. db := database.DB().Begin()
  137. if req.Pid > 0 {
  138. err = subCommentAdd(req, db, dbname)
  139. } else {
  140. err = articleCommentAdd(req, db, dbname)
  141. }
  142. if err != nil {
  143. db.Rollback()
  144. return nil, err
  145. }
  146. if err := oss_utils.OssObjAdd(req.Pics, nil); err != nil {
  147. db.Rollback()
  148. return nil, err
  149. }
  150. db.Commit()
  151. return reply, nil
  152. }