add.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "property-garden/errors"
  9. dbmodel "property-garden/model"
  10. pb_v1 "property-garden/pb/v1"
  11. "property-garden/utils"
  12. "strings"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkParkAddParam(req *pb_v1.ParkAddRequest) error {
  20. switch {
  21. case req.GardenId < 1:
  22. return status.Error(10003, "小区不能为空")
  23. case req.ParkName == "":
  24. return status.Error(10003, "停车场名称不能为空")
  25. case req.ParkNumber == "":
  26. return status.Error(10003, "停车场编号不能为空")
  27. case req.ParkType < 1:
  28. return status.Error(10003, "停车场类型不能为空")
  29. }
  30. return nil
  31. }
  32. //
  33. func ParkAdd(ctx context.Context, req *pb_v1.ParkAddRequest) (reply *pb_v1.ParkAddReply, err error) {
  34. reply = &pb_v1.ParkAddReply{}
  35. // 捕获各个task中的异常并返回给调用者
  36. defer func() {
  37. if r := recover(); r != nil {
  38. err = fmt.Errorf("%+v", r)
  39. e := &status.Status{}
  40. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  41. logger.Error("err",
  42. zap.String("system_err", err.Error()),
  43. zap.Stack("stacktrace"))
  44. }
  45. }
  46. }()
  47. // 参数检查
  48. err = checkParkAddParam(req)
  49. if err != nil {
  50. return nil, err
  51. }
  52. dbname := utils.GetGardenDbName(req.GardenId)
  53. now := time.Now()
  54. p := &dbmodel.TPark{
  55. ParkType:req.ParkType,
  56. ParkNumber:req.ParkNumber,
  57. ParkName:req.ParkName,
  58. Comment:req.Comment,
  59. UpdatedAt:now,
  60. CreatedAt:now,
  61. //GardenId:req.GardenId,
  62. }
  63. p.SetTable(dbname)
  64. err = p.Insert(database.DB())
  65. if err != nil {
  66. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  67. return nil, status.Error(10003, "该停车场已存在")
  68. }
  69. return nil, errors.DataBaseError
  70. }
  71. reply.Id = p.ID
  72. return reply, nil
  73. }