manager_add.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house
  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. "property-garden/errors"
  13. dbmodel "property-garden/model"
  14. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. "strings"
  17. )
  18. func checkBuildingAddManagerParam(req *pb_v1.BuildingAddManagerRequest) error {
  19. switch {
  20. case req.GardenId < 1:
  21. return status.Error(10003, "小区不能为空")
  22. case req.ManagerUid < 1:
  23. return status.Error(10003, "经纪人不能为空")
  24. case req.BuildingId < 1:
  25. return status.Error(10003, "楼栋id不能为空")
  26. }
  27. return nil
  28. }
  29. //
  30. func BuildingAddManager(ctx context.Context, req *pb_v1.BuildingAddManagerRequest) (reply *pb_v1.BuildingAddManagerReply, err error) {
  31. reply = &pb_v1.BuildingAddManagerReply{}
  32. // 捕获各个task中的异常并返回给调用者
  33. defer func() {
  34. if r := recover(); r != nil {
  35. err = fmt.Errorf("%+v", r)
  36. e := &status.Status{}
  37. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  38. logger.Error("err",
  39. zap.String("system_err", err.Error()),
  40. zap.Stack("stacktrace"))
  41. }
  42. }
  43. }()
  44. // 参数检查
  45. err = checkBuildingAddManagerParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. dbname := utils.GetGardenDbName(req.GardenId)
  50. p := &dbmodel.TBuildingManager{
  51. BuildingId: req.BuildingId,
  52. ManagerUid: req.ManagerUid,
  53. //GardenId:req.GardenId,
  54. }
  55. p.SetTable(dbname)
  56. err = p.Insert(database.DB())
  57. if err != nil {
  58. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  59. return nil, status.Error(10003, "该经纪人已存在")
  60. }
  61. return nil, errors.DataBaseError
  62. }
  63. reply.Id = p.ID
  64. return reply, nil
  65. }