add.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package vote
  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. "property-garden/parser"
  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 checkVoteAddParam(req *pb_v1.VoteAddRequest) 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.Start < 1 || req.End < 1:
  27. return status.Error(10003, "开始和结束时间不能为空")
  28. case req.Start >= req.End:
  29. return status.Error(10003, "开始时间不能晚于结束时间")
  30. case req.End <= time.Now().Unix():
  31. return status.Error(10003, "结束时间不能早于当前时间")
  32. case len(req.Topics) == 0:
  33. return status.Error(10003, "题目不能为空")
  34. }
  35. pic := []string{}
  36. for _, v := range req.Pics {
  37. if oss_utils.IsFix(v) {
  38. continue
  39. }
  40. pic = append(pic, v)
  41. }
  42. req.Pics = pic
  43. flagM := map[string]bool{}
  44. for _, v := range req.Topics {
  45. if v.TopicType != TopicTypeMultiChoice && v.TopicType != TopicTypeSingleChoice {
  46. continue
  47. }
  48. for _, c := range v.ChoiceItems {
  49. if c.Flag == "" || c.Text == "" {
  50. return status.Error(10003, "选择题的选项和标签不能为空")
  51. }
  52. if flagM[c.Flag] {
  53. return status.Error(10003, "选择题的标签有重复")
  54. }
  55. flagM[c.Flag] = true
  56. }
  57. flagM = map[string]bool{}
  58. }
  59. return nil
  60. }
  61. func VoteAdd(ctx context.Context, req *pb_v1.VoteAddRequest) (reply *pb_v1.VoteAddReply, err error) {
  62. reply = &pb_v1.VoteAddReply{}
  63. // 捕获各个task中的异常并返回给调用者
  64. defer func() {
  65. if r := recover(); r != nil {
  66. err = fmt.Errorf("%+v", r)
  67. e := &status.Status{}
  68. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  69. logger.Error("err",
  70. zap.String("system_err", err.Error()),
  71. zap.Stack("stacktrace"))
  72. }
  73. }
  74. }()
  75. // 参数检查
  76. err = checkVoteAddParam(req)
  77. if err != nil {
  78. return nil, err
  79. }
  80. for i, _ := range req.Topics {
  81. req.Topics[i].Number = int64(i + 1)
  82. }
  83. now := time.Now()
  84. dbname := utils.GetGardenDbName(req.GardenId)
  85. topics, _ := json.Marshal(req.Topics)
  86. p := dbmodel.TVote{
  87. Start: req.Start,
  88. End: req.End,
  89. Title: req.Title,
  90. Topics: string(topics),
  91. UpdatedAt: now,
  92. CreatedAt: now,
  93. }
  94. if len(req.Pics) > 0 {
  95. p.Pics = utils.StringJoin(req.Pics, ";")
  96. }
  97. p.SetTable(dbname)
  98. db := database.DB().Begin()
  99. err = p.Insert(db)
  100. if err != nil {
  101. db.Rollback()
  102. return nil, errors.DataBaseError
  103. }
  104. if err := oss_utils.OssObjAdd(req.Pics, nil); err != nil {
  105. db.Rollback()
  106. return nil, err
  107. }
  108. db.Commit()
  109. collection := parser.Session.DB(dbmodel.VoteMgoDb).C(dbmodel.VoteCollection(req.GardenId, p.ID))
  110. collection.EnsureIndexKey("uid")
  111. return reply, nil
  112. }