update.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/oss_utils"
  11. dbmodel "property-garden/model"
  12. "property-garden/parser"
  13. pb_v1 "property-garden/pb/v1"
  14. "property-garden/utils"
  15. "strings"
  16. "time"
  17. "git.getensh.com/common/gopkgs/database"
  18. "git.getensh.com/common/gopkgs/logger"
  19. "go.uber.org/zap"
  20. "google.golang.org/grpc/status"
  21. )
  22. func checkVoteUpdateParam(req *pb_v1.VoteUpdateRequest) error {
  23. switch {
  24. case req.Id == 0:
  25. return status.Error(10003, "id不能为空")
  26. case req.GardenId == 0:
  27. return status.Error(10003, "小区不能为空")
  28. case req.Title == "":
  29. return status.Error(10003, "标题不能为空")
  30. case req.Start < 1 || req.End < 1:
  31. return status.Error(10003, "开始和结束时间不能为空")
  32. case req.Start >= req.End:
  33. return status.Error(10003, "开始时间不能晚于结束时间")
  34. case req.End <= time.Now().Unix():
  35. return status.Error(10003, "结束时间不能早于当前时间")
  36. case len(req.Topics) == 0:
  37. return status.Error(10003, "题目不能为空")
  38. }
  39. pic := []string{}
  40. for _, v := range req.Pics {
  41. if oss_utils.IsFix(v) {
  42. continue
  43. }
  44. pic = append(pic, v)
  45. }
  46. req.Pics = pic
  47. flagM := map[string]bool{}
  48. for _, v := range req.Topics {
  49. if v.TopicType != TopicTypeMultiChoice && v.TopicType != TopicTypeSingleChoice {
  50. continue
  51. }
  52. for _, c := range v.ChoiceItems {
  53. if c.Flag == "" || c.Text == "" {
  54. return status.Error(10003, "选择题的选项和标签不能为空")
  55. }
  56. if flagM[c.Flag] {
  57. return status.Error(10003, "选择题的标签有重复")
  58. }
  59. flagM[c.Flag] = true
  60. }
  61. }
  62. return nil
  63. }
  64. func VoteUpdate(ctx context.Context, req *pb_v1.VoteUpdateRequest) (reply *pb_v1.VoteUpdateReply, err error) {
  65. reply = &pb_v1.VoteUpdateReply{}
  66. // 捕获各个task中的异常并返回给调用者
  67. defer func() {
  68. if r := recover(); r != nil {
  69. err = fmt.Errorf("%+v", r)
  70. e := &status.Status{}
  71. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  72. logger.Error("err",
  73. zap.String("system_err", err.Error()),
  74. zap.Stack("stacktrace"))
  75. }
  76. }
  77. }()
  78. // 参数检查
  79. err = checkVoteUpdateParam(req)
  80. if err != nil {
  81. return nil, err
  82. }
  83. dbname := utils.GetGardenDbName(req.GardenId)
  84. // 获取旧数据
  85. old := dbmodel.NewVote(dbname)
  86. where := map[string]interface{}{
  87. "id": req.Id,
  88. }
  89. err = old.Find(database.DB(), where)
  90. if err != nil && err != gorm.ErrRecordNotFound {
  91. return nil, errors.DataBaseError
  92. }
  93. if old.ID == 0 {
  94. return nil, errors.ErrRecordNotFound
  95. }
  96. // todo 投票中不能修改
  97. collection := parser.Session.DB(dbmodel.VoteMgoDb).C(dbmodel.VoteCollection(req.GardenId, req.Id))
  98. count, err := collection.Count()
  99. if err != nil {
  100. return nil, errors.DataBaseError
  101. }
  102. if count > 0 {
  103. return nil, status.Error(10003, "已经开始投票,不能修改")
  104. }
  105. for i, _ := range req.Topics {
  106. req.Topics[i].Number = int64(i + 1)
  107. }
  108. // 更新新数据
  109. now := time.Now()
  110. vote := dbmodel.NewVote(dbname)
  111. topics, _ := json.Marshal(req.Topics)
  112. values := map[string]interface{}{
  113. "title": req.Title,
  114. "start": req.Start,
  115. "end": req.End,
  116. "topics": string(topics),
  117. "updated_at": now,
  118. }
  119. values["pics"] = ""
  120. if len(req.Pics) > 0 {
  121. values["pics"] = utils.StringJoin(req.Pics, ";")
  122. }
  123. where = map[string]interface{}{
  124. "id": req.Id,
  125. }
  126. db := database.DB().Begin()
  127. err = vote.Update(db, where, values)
  128. if err != nil {
  129. db.Rollback()
  130. return nil, errors.DataBaseError
  131. }
  132. if err := oss_utils.OssObjAdd(req.Pics, strings.Split(old.Pics, ";")); err != nil {
  133. db.Rollback()
  134. return nil, err
  135. }
  136. db.Commit()
  137. return reply, nil
  138. }