update.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "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. "strings"
  19. "time"
  20. )
  21. func checkEventUpdateParam(req *pb_v1.EventUpdateRequest) error {
  22. switch {
  23. case req.GardenId < 1:
  24. return status.Error(10003, "小区不能为空")
  25. case req.Title == "":
  26. return status.Error(10003, "标题不能为空")
  27. case req.Content == "":
  28. return status.Error(10003, "内容不能为空")
  29. case req.Start < 1:
  30. return status.Error(10003, "活动开始时间不能为空")
  31. case req.End < 1:
  32. return status.Error(10003, "活动结束时间不能为空")
  33. case req.End < req.Start:
  34. return status.Error(10003, "结束时间不能晚于开始时间")
  35. case req.SignUpEnd < 1:
  36. return status.Error(10003, "报名截止时间不能为空")
  37. case req.SignUpEnd > req.End:
  38. return status.Error(10003, "报名截止时间不能晚于活动结束时间")
  39. case req.PeopleLimit < 1:
  40. return status.Error(10003, "人数限制不能为空")
  41. case req.Addr == "":
  42. return status.Error(10003, "活动地址不能为空")
  43. case req.Id < 1:
  44. return status.Error(10003, "id不能为空")
  45. }
  46. pic := []string{}
  47. for _, v := range req.Pic {
  48. if oss_utils.IsFix(v) {
  49. continue
  50. }
  51. pic = append(pic, v)
  52. }
  53. req.Pic = pic
  54. return nil
  55. }
  56. //
  57. func EventUpdate(ctx context.Context, req *pb_v1.EventUpdateRequest) (reply *pb_v1.EventUpdateReply, err error) {
  58. reply = &pb_v1.EventUpdateReply{}
  59. // 捕获各个task中的异常并返回给调用者
  60. defer func() {
  61. if r := recover(); r != nil {
  62. err = fmt.Errorf("%+v", r)
  63. e := &status.Status{}
  64. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  65. logger.Error("err",
  66. zap.String("system_err", err.Error()),
  67. zap.Stack("stacktrace"))
  68. }
  69. }
  70. }()
  71. // 参数检查
  72. err = checkEventUpdateParam(req)
  73. if err != nil {
  74. return nil, err
  75. }
  76. dbname := utils.GetGardenDbName(req.GardenId)
  77. now := time.Now()
  78. p := dbmodel.NewEvent(dbname)
  79. where := map[string]interface{}{
  80. "id": req.Id,
  81. //"garden_id":req.GardenId,
  82. }
  83. old := dbmodel.NewEvent(dbname)
  84. err = old.Find(database.DB(), where)
  85. if err != nil {
  86. if err != gorm.ErrRecordNotFound {
  87. return nil, errors.DataBaseError
  88. }
  89. }
  90. values := map[string]interface{}{
  91. "title": req.Title,
  92. "content": req.Content,
  93. "start": req.Start,
  94. "end": req.End,
  95. "sign_up_end": req.SignUpEnd,
  96. "people_limit": req.PeopleLimit,
  97. "addr": req.Addr,
  98. "fee": req.Fee,
  99. "updated_at": now,
  100. }
  101. values["pic"] = ""
  102. if len(req.Pic) > 0 {
  103. values["pic"] = utils.StringJoin(req.Pic, ";")
  104. }
  105. db := database.DB().Begin()
  106. err = p.Update(db, where, values)
  107. if err != nil {
  108. db.Rollback()
  109. return nil, errors.DataBaseError
  110. }
  111. inList := oss_utils.GetObjFromText(req.Content)
  112. outList := oss_utils.GetObjFromText(old.Content)
  113. pics := strings.Split(old.Pic, ";")
  114. if len(pics) > 0 {
  115. outList = append(outList, pics...)
  116. }
  117. if len(req.Pic) > 0 {
  118. inList = append(inList, req.Pic...)
  119. }
  120. if len(outList) > 0 || len(outList) > 0 {
  121. if err := oss_utils.OssObjAdd(inList, outList); err != nil {
  122. db.Rollback()
  123. return nil, err
  124. }
  125. }
  126. db.Commit()
  127. return reply, nil
  128. }