add.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package event
  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 checkEventAddParam(req *pb_v1.EventAddRequest) error {
  20. switch {
  21. case req.GardenId < 1:
  22. return status.Error(10003, "小区不能为空")
  23. case req.Title == "":
  24. return status.Error(10003, "标题不能为空")
  25. case req.Content == "":
  26. return status.Error(10003, "内容不能为空")
  27. case req.Start < 1:
  28. return status.Error(10003, "活动开始时间不能为空")
  29. case req.End < 1:
  30. return status.Error(10003, "活动结束时间不能为空")
  31. case req.End < req.Start:
  32. return status.Error(10003, "结束时间不能晚于开始时间")
  33. case req.SignUpEnd < 1:
  34. return status.Error(10003, "报名截止时间不能为空")
  35. case req.SignUpEnd > req.End:
  36. return status.Error(10003, "报名截止时间不能晚于活动结束时间")
  37. case req.PeopleLimit < 1:
  38. return status.Error(10003, "人数限制不能为空")
  39. case req.Addr == "":
  40. return status.Error(10003, "活动地址不能为空")
  41. }
  42. pic := []string{}
  43. for _, v := range req.Pic {
  44. if oss_utils.IsFix(v) {
  45. continue
  46. }
  47. pic = append(pic, v)
  48. }
  49. req.Pic = pic
  50. return nil
  51. }
  52. //
  53. func EventAdd(ctx context.Context, req *pb_v1.EventAddRequest) (reply *pb_v1.EventAddReply, err error) {
  54. reply = &pb_v1.EventAddReply{}
  55. // 捕获各个task中的异常并返回给调用者
  56. defer func() {
  57. if r := recover(); r != nil {
  58. err = fmt.Errorf("%+v", r)
  59. e := &status.Status{}
  60. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  61. logger.Error("err",
  62. zap.String("system_err", err.Error()),
  63. zap.Stack("stacktrace"))
  64. }
  65. }
  66. }()
  67. // 参数检查
  68. err = checkEventAddParam(req)
  69. if err != nil {
  70. return nil, err
  71. }
  72. dbname := utils.GetGardenDbName(req.GardenId)
  73. now := time.Now()
  74. p := &dbmodel.TEvent{
  75. Title: req.Title,
  76. Content: req.Content,
  77. Start: req.Start,
  78. End: req.End,
  79. SignUpEnd: req.SignUpEnd,
  80. PeopleLimit: req.PeopleLimit,
  81. Addr: req.Addr,
  82. Fee: req.Fee,
  83. UpdatedAt: now,
  84. CreatedAt: now,
  85. //GardenId:req.GardenId,
  86. }
  87. if len(req.Pic) > 0 {
  88. p.Pic = utils.StringJoin(req.Pic, ";")
  89. }
  90. p.SetTable(dbname)
  91. db := database.DB().Begin()
  92. err = p.Insert(db)
  93. if err != nil {
  94. db.Rollback()
  95. return nil, errors.DataBaseError
  96. }
  97. inList := oss_utils.GetObjFromText(req.Content)
  98. if len(req.Pic) > 0 {
  99. inList = append(inList, req.Pic...)
  100. }
  101. if len(inList) > 0 {
  102. if err := oss_utils.OssObjAdd(inList, nil); err != nil {
  103. db.Rollback()
  104. return nil, err
  105. }
  106. }
  107. db.Commit()
  108. reply.Id = p.ID
  109. return reply, nil
  110. }