sign_add.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "gorm.io/gorm"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "strings"
  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 checkEventSignAddParam(req *pb_v1.EventSignAddRequest) error {
  21. switch {
  22. case req.GardenId < 1:
  23. return status.Error(10003, "小区不能为空")
  24. case req.Name == "":
  25. return status.Error(10003, "姓名不能为空")
  26. case req.Phone == "":
  27. return status.Error(10003, "电话不能为空")
  28. case req.Count < 1:
  29. return status.Error(10003, "报名人数不能为空")
  30. case req.HouseholdUid < 1:
  31. return status.Error(10003, "住户id不能为空")
  32. case req.EventId < 1:
  33. return status.Error(10003, "活动不能为空")
  34. }
  35. return nil
  36. }
  37. //
  38. func EventSignAdd(ctx context.Context, req *pb_v1.EventSignAddRequest) (reply *pb_v1.EventSignAddReply, err error) {
  39. reply = &pb_v1.EventSignAddReply{}
  40. // 捕获各个task中的异常并返回给调用者
  41. defer func() {
  42. if r := recover(); r != nil {
  43. err = fmt.Errorf("%+v", r)
  44. e := &status.Status{}
  45. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  46. logger.Error("err",
  47. zap.String("system_err", err.Error()),
  48. zap.Stack("stacktrace"))
  49. }
  50. }
  51. }()
  52. // 参数检查
  53. err = checkEventSignAddParam(req)
  54. if err != nil {
  55. return nil, err
  56. }
  57. dbname := utils.GetGardenDbName(req.GardenId)
  58. now := time.Now()
  59. // 乐观锁方案,先直接更新和插入,最后查询是否超过人数限制
  60. p := dbmodel.NewEvent(dbname)
  61. where := map[string]interface{}{
  62. "id":req.EventId,
  63. }
  64. values := map[string]interface{}{
  65. "people_count":gorm.Expr("people_count + ?", req.Count),
  66. }
  67. db := database.DB().Begin()
  68. defer func() {
  69. if err != nil {
  70. db.Rollback()
  71. }
  72. }()
  73. err = p.FindForUpdate(db, where)
  74. if err != nil {
  75. if err != gorm.ErrRecordNotFound {
  76. return nil, errors.DataBaseError
  77. }
  78. }
  79. if p.ID == 0 {
  80. return nil, errors.ErrRecordNotFound
  81. }
  82. if p.PeopleCount + req.Count > p.PeopleLimit {
  83. return nil, status.Error(10003, "人数已达上限")
  84. }
  85. // 更新活动报名总人数
  86. err = p.Update(db, where, values)
  87. if err != nil {
  88. return nil, errors.DataBaseError
  89. }
  90. // 插入报名记录
  91. sign := dbmodel.TEventSign{
  92. EventId:req.EventId,
  93. HouseholdUid:req.HouseholdUid,
  94. SignCount:req.Count,
  95. Name:req.Name,
  96. Phone:req.Phone,
  97. Comment:req.Comment,
  98. CreatedAt:now,
  99. UpdatedAt:now,
  100. }
  101. sign.SetTable(dbname)
  102. err = sign.Insert(db)
  103. if err != nil {
  104. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  105. return nil, status.Error(10003, "不能重复报名")
  106. }
  107. return nil, errors.DataBaseError
  108. }
  109. db.Commit()
  110. return reply, nil
  111. }