manager_add.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house_rent
  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 checkHouseRentAddManagerParam(req *pb_v1.HouseRentAddManagerRequest) 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.RentId < 1:
  25. return status.Error(10003, "租房id不能为空")
  26. }
  27. return nil
  28. }
  29. //
  30. func HouseRentAddManager(ctx context.Context, req *pb_v1.HouseRentAddManagerRequest) (reply *pb_v1.HouseRentAddManagerReply, err error) {
  31. reply = &pb_v1.HouseRentAddManagerReply{}
  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 = checkHouseRentAddManagerParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. dbname := utils.GetGardenDbName(req.GardenId)
  50. p := &dbmodel.THouseRentManager{
  51. RentId:req.RentId,
  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. }