sign_del.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. dbmodel "property-garden/model"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. )
  18. func checkEventSignDelParam(req *pb_v1.EventSignDelRequest) error {
  19. switch {
  20. case req.GardenId < 1:
  21. return status.Error(10003, "小区不能为空")
  22. case req.Id == 0:
  23. return status.Error(10003, "id不能为空")
  24. }
  25. return nil
  26. }
  27. //
  28. func EventSignDel(ctx context.Context, req *pb_v1.EventSignDelRequest) (reply *pb_v1.EventSignDelReply, err error) {
  29. reply = &pb_v1.EventSignDelReply{}
  30. // 捕获各个task中的异常并返回给调用者
  31. defer func() {
  32. if r := recover(); r != nil {
  33. err = fmt.Errorf("%+v", r)
  34. e := &status.Status{}
  35. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  36. logger.Error("err",
  37. zap.String("system_err", err.Error()),
  38. zap.Stack("stacktrace"))
  39. }
  40. }
  41. }()
  42. // 参数检查
  43. err = checkEventSignDelParam(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. dbname := utils.GetGardenDbName(req.GardenId)
  48. p := dbmodel.NewEventSign(dbname)
  49. where := map[string]interface{}{
  50. "id":req.Id,
  51. }
  52. err = p.Find(database.DB(), where)
  53. if err != nil && err != gorm.ErrRecordNotFound {
  54. return nil, errors.DataBaseError
  55. }
  56. if p.ID == 0 {
  57. return nil, errors.ErrRecordNotFound
  58. }
  59. db := database.DB().Begin()
  60. defer func() {
  61. if err != nil {
  62. db.Rollback()
  63. }
  64. }()
  65. err = p.Delete(db, where)
  66. if err != nil {
  67. return nil, errors.DataBaseError
  68. }
  69. event := dbmodel.NewEvent(dbname)
  70. where = map[string]interface{}{
  71. "id":p.EventId,
  72. }
  73. values := map[string]interface{}{
  74. "people_count":gorm.Expr("people_count - ?", p.SignCount),
  75. }
  76. err = event.Update(db, where, values)
  77. if err != nil {
  78. return nil, errors.DataBaseError
  79. }
  80. db.Commit()
  81. return reply, nil
  82. }