update.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package park
  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. "strings"
  18. "time"
  19. )
  20. func checkParkUpdateParam(req *pb_v1.ParkUpdateRequest) error {
  21. switch {
  22. case req.GardenId < 1:
  23. return status.Error(10003, "小区不能为空")
  24. case req.ParkName == "":
  25. return status.Error(10003, "停车场名称不能为空")
  26. case req.ParkNumber == "":
  27. return status.Error(10003, "停车场编号不能为空")
  28. case req.ParkType < 1:
  29. return status.Error(10003, "停车场类型不能为空")
  30. case req.Id < 1:
  31. return status.Error(10003, "id 不能为空")
  32. }
  33. return nil
  34. }
  35. //
  36. func ParkUpdate(ctx context.Context, req *pb_v1.ParkUpdateRequest) (reply *pb_v1.ParkUpdateReply, err error) {
  37. reply = &pb_v1.ParkUpdateReply{}
  38. // 捕获各个task中的异常并返回给调用者
  39. defer func() {
  40. if r := recover(); r != nil {
  41. err = fmt.Errorf("%+v", r)
  42. e := &status.Status{}
  43. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  44. logger.Error("err",
  45. zap.String("system_err", err.Error()),
  46. zap.Stack("stacktrace"))
  47. }
  48. }
  49. }()
  50. // 参数检查
  51. err = checkParkUpdateParam(req)
  52. if err != nil {
  53. return nil, err
  54. }
  55. dbname := utils.GetGardenDbName(req.GardenId)
  56. now := time.Now()
  57. p := dbmodel.NewPark(dbname)
  58. where := map[string]interface{}{
  59. "id":req.Id,
  60. //"garden_id":req.GardenId,
  61. }
  62. values := map[string]interface{}{
  63. "park_name":req.ParkName,
  64. "park_number":req.ParkNumber,
  65. "park_type":req.ParkType,
  66. "comment":req.Comment,
  67. "updated_at":now,
  68. }
  69. old := dbmodel.NewPark(dbname)
  70. err = old.Find(database.DB(), where)
  71. if err != nil && err != gorm.ErrRecordNotFound{
  72. return nil, errors.DataBaseError
  73. }
  74. if old.ID == 0 {
  75. return nil, errors.ErrRecordNotFound
  76. }
  77. if req.ParkNumber != old.ParkNumber {
  78. // todo
  79. //isBind, err := isParkBindCharge(req.Id, dbname)
  80. //if err != nil {
  81. // return nil, err
  82. //}
  83. //if isBind {
  84. // return nil, status.Error(10003, "该区域有车位已绑定费用,请先解除绑定")
  85. //}
  86. }
  87. reply.Origin = &pb_v1.ParkUpdateRequest{
  88. Id:req.Id,
  89. ParkType:old.ParkType,
  90. ParkNumber:old.ParkNumber,
  91. ParkName:old.ParkName,
  92. Comment:old.Comment,
  93. GardenId:req.GardenId,
  94. }
  95. err = p.Update(database.DB(), where, values)
  96. if err != nil {
  97. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  98. return nil, status.Error(10003, "该停车场已存在")
  99. }
  100. return nil, errors.DataBaseError
  101. }
  102. return reply, nil
  103. }