announcement_del.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "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. )
  19. func checkAnnouncementDelParam(req *pb_v1.AnnouncementDelRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case req.Id < 1:
  24. return status.Error(10003, "id 不能为空")
  25. }
  26. return nil
  27. }
  28. func AnnouncementDel(ctx context.Context, req *pb_v1.AnnouncementDelRequest) (reply *pb_v1.AnnouncementDelReply, err error) {
  29. reply = &pb_v1.AnnouncementDelReply{}
  30. // 捕获各个task中的异常并返回给调用者
  31. defer func() {
  32. if r := recover(); r != nil {
  33. err = fmt.Errorf("%+v", r)
  34. e := &status.Status{}
  35. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  36. logger.Error("err",
  37. zap.String("system_err", err.Error()),
  38. zap.Stack("stacktrace"))
  39. }
  40. }
  41. }()
  42. // 参数检查
  43. err = checkAnnouncementDelParam(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. dbname := utils.GetGardenDbName(req.GardenId)
  48. // 获取旧数据
  49. // 获取旧数据
  50. old := dbmodel.NewAnnouncement(dbname)
  51. where := map[string]interface{}{
  52. "id": req.Id,
  53. }
  54. err = old.Find(database.DB(), where)
  55. if err != nil && err != gorm.ErrRecordNotFound {
  56. return nil, errors.DataBaseError
  57. }
  58. if old.ID == 0 {
  59. return nil, errors.ErrRecordNotFound
  60. }
  61. reply.Origin = &pb_v1.AnnouncementUpdateRequest{
  62. GardenId: req.GardenId,
  63. Id: req.Id,
  64. Content: old.Content,
  65. Title: old.Title,
  66. }
  67. // 删除数据
  68. p := dbmodel.NewAnnouncement(dbname)
  69. db := database.DB().Begin()
  70. err = p.Delete(db, where)
  71. if err != nil {
  72. db.Rollback()
  73. return nil, errors.DataBaseError
  74. }
  75. ar := dbmodel.NewAnnouncementRead(dbname)
  76. where = map[string]interface{}{
  77. "announcement_id": req.Id,
  78. }
  79. err = ar.Delete(db, where)
  80. if err != nil {
  81. db.Rollback()
  82. return nil, errors.DataBaseError
  83. }
  84. outList := oss_utils.GetObjFromText(old.Content)
  85. if len(outList) > 0 {
  86. if err := oss_utils.OssObjAdd(nil, outList); err != nil {
  87. db.Rollback()
  88. return nil, err
  89. }
  90. }
  91. db.Commit()
  92. return reply, nil
  93. }