neighbor_comment_update.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "strings"
  15. "time"
  16. "git.getensh.com/common/gopkgs/database"
  17. "git.getensh.com/common/gopkgs/logger"
  18. "go.uber.org/zap"
  19. "google.golang.org/grpc/status"
  20. )
  21. func checkNeighborCommentUpdateParam(req *pb_v1.NeighborCommentUpdateRequest) error {
  22. switch {
  23. case req.GardenId == 0:
  24. return status.Error(10003, "小区不能为空")
  25. case req.Content == "":
  26. return status.Error(10003, "内容不能为空")
  27. case req.Id == 0:
  28. return status.Error(10003, "id 不能为空")
  29. case req.Uid == 0:
  30. return status.Error(10003, "uid 不能为空")
  31. }
  32. return nil
  33. }
  34. func NeighborCommentUpdate(ctx context.Context, req *pb_v1.NeighborCommentUpdateRequest) (reply *pb_v1.NeighborCommentUpdateReply, err error) {
  35. reply = &pb_v1.NeighborCommentUpdateReply{}
  36. // 捕获各个task中的异常并返回给调用者
  37. defer func() {
  38. if r := recover(); r != nil {
  39. err = fmt.Errorf("%+v", r)
  40. e := &status.Status{}
  41. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  42. logger.Error("err",
  43. zap.String("system_err", err.Error()),
  44. zap.Stack("stacktrace"))
  45. }
  46. }
  47. }()
  48. // 参数检查
  49. err = checkNeighborCommentUpdateParam(req)
  50. if err != nil {
  51. return nil, err
  52. }
  53. dbname := utils.GetGardenDbName(req.GardenId)
  54. // 获取旧数据
  55. old := dbmodel.NewNeighborComment(dbname)
  56. where := map[string]interface{}{
  57. "id": req.Id,
  58. }
  59. err = old.Find(database.DB(), where)
  60. if err != nil && err != gorm.ErrRecordNotFound {
  61. return nil, errors.DataBaseError
  62. }
  63. if old.ID == 0 {
  64. return nil, errors.ErrRecordNotFound
  65. }
  66. if old.Uid != req.Uid {
  67. return nil, status.Error(10003, "非本人不能修改")
  68. }
  69. // 更新新数据
  70. now := time.Now()
  71. comment := dbmodel.NewNeighborComment(dbname)
  72. values := map[string]interface{}{
  73. "content": req.Content,
  74. "updated_at": now,
  75. "pics": utils.StringJoin(req.Pics, ";"),
  76. }
  77. where = map[string]interface{}{
  78. "id": req.Id,
  79. }
  80. db := database.DB().Begin()
  81. err = comment.Update(db, where, values)
  82. if err != nil {
  83. db.Rollback()
  84. return nil, errors.DataBaseError
  85. }
  86. if err := oss_utils.OssObjAdd(req.Pics, strings.Split(old.Pics, ";")); err != nil {
  87. db.Rollback()
  88. return nil, err
  89. }
  90. db.Commit()
  91. return reply, nil
  92. }