announcement_add.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "property-garden/errors"
  9. "property-garden/impl/v1/oss_utils"
  10. dbmodel "property-garden/model"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkAnnouncementAddParam(req *pb_v1.AnnouncementAddRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case req.Title == "":
  24. return status.Error(10003, "标题不能为空")
  25. case req.Content == "":
  26. return status.Error(10003, "内容不能为空")
  27. }
  28. return nil
  29. }
  30. func AnnouncementAdd(ctx context.Context, req *pb_v1.AnnouncementAddRequest) (reply *pb_v1.AnnouncementAddReply, err error) {
  31. reply = &pb_v1.AnnouncementAddReply{}
  32. // 捕获各个task中的异常并返回给调用者
  33. defer func() {
  34. if r := recover(); r != nil {
  35. err = fmt.Errorf("%+v", r)
  36. e := &status.Status{}
  37. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  38. logger.Error("err",
  39. zap.String("system_err", err.Error()),
  40. zap.Stack("stacktrace"))
  41. }
  42. }
  43. }()
  44. // 参数检查
  45. err = checkAnnouncementAddParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. now := time.Now()
  50. dbname := utils.GetGardenDbName(req.GardenId)
  51. p := dbmodel.TAnnouncement{
  52. Content:req.Content,
  53. Title:req.Title,
  54. UpdatedAt:now,
  55. CreatedAt:now,
  56. }
  57. p.SetTable(dbname)
  58. db := database.DB().Begin()
  59. err = p.Insert(db)
  60. if err != nil {
  61. db.Rollback()
  62. return nil, errors.DataBaseError
  63. }
  64. inList := oss_utils.GetObjFromText(req.Content)
  65. if len(inList) > 0 {
  66. if err := oss_utils.OssObjAdd(inList, nil); err != nil {
  67. db.Rollback()
  68. return nil, err
  69. }
  70. }
  71. db.Commit()
  72. reply.Id = p.ID
  73. return reply, nil
  74. }