announcement_update.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package announcement
  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 checkAnnouncementUpdateParam(req *pb_v1.AnnouncementUpdateRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. case req.Title == "":
  25. return status.Error(10003, "标题不能为空")
  26. case req.Content == "":
  27. return status.Error(10003, "内容不能为空")
  28. }
  29. return nil
  30. }
  31. func AnnouncementUpdate(ctx context.Context, req *pb_v1.AnnouncementUpdateRequest) (reply *pb_v1.AnnouncementUpdateReply, err error) {
  32. reply = &pb_v1.AnnouncementUpdateReply{}
  33. // 捕获各个task中的异常并返回给调用者
  34. defer func() {
  35. if r := recover(); r != nil {
  36. err = fmt.Errorf("%+v", r)
  37. e := &status.Status{}
  38. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  39. logger.Error("err",
  40. zap.String("system_err", err.Error()),
  41. zap.Stack("stacktrace"))
  42. }
  43. }
  44. }()
  45. // 参数检查
  46. err = checkAnnouncementUpdateParam(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. dbname := utils.GetGardenDbName(req.GardenId)
  51. // 获取旧数据
  52. old := dbmodel.NewAnnouncement(dbname)
  53. where := map[string]interface{}{
  54. "id":req.Id,
  55. }
  56. err = old.Find(database.DB(), where)
  57. if err != nil && err != gorm.ErrRecordNotFound {
  58. return nil, errors.DataBaseError
  59. }
  60. if old.ID == 0 {
  61. return nil, errors.ErrRecordNotFound
  62. }
  63. reply.Origin = &pb_v1.AnnouncementUpdateRequest{
  64. GardenId:req.GardenId,
  65. Id:req.Id,
  66. Content:req.Content,
  67. Title:req.Title,
  68. }
  69. // 更新新数据
  70. now := time.Now()
  71. p := dbmodel.NewAnnouncement(dbname)
  72. values := map[string]interface{}{
  73. "title":req.Title,
  74. "content":req.Content,
  75. "updated_at":now,
  76. }
  77. where = map[string]interface{}{
  78. "id":req.Id,
  79. }
  80. db := database.DB().Begin()
  81. err = p.Update(db, where, values)
  82. if err != nil {
  83. db.Rollback()
  84. return nil, errors.DataBaseError
  85. }
  86. inList := oss_utils.GetObjFromText(req.Content)
  87. outList := oss_utils.GetObjFromText(old.Content)
  88. if len(outList) > 0 || len(outList) > 0{
  89. if err := oss_utils.OssObjAdd(inList, outList); err != nil {
  90. db.Rollback()
  91. return nil, err
  92. }
  93. }
  94. db.Commit()
  95. return reply, nil
  96. }