manager_list.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. dbmodel "property-garden/model"
  13. "property-garden/pb"
  14. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. )
  17. func checkHouseRentManagerListParam(req *pb_v1.HouseRentManagerListRequest) error {
  18. switch {
  19. case req.GardenId < 1:
  20. return status.Error(10003, "小区不能为空")
  21. case req.RentId < 1:
  22. return status.Error(10003, "租房id不能为空")
  23. }
  24. return nil
  25. }
  26. func getManagerInfos(ids []int64, gardenId int64) (map[int64]pb_v1.UserItem, error) {
  27. ret := map[int64]pb_v1.UserItem{}
  28. if len(ids) == 0 {
  29. return ret, nil
  30. }
  31. mreq := pb_v1.UserListRequest{
  32. GardenId:gardenId,
  33. Ids:ids,
  34. PageSize:-1,
  35. Page:-1,
  36. }
  37. mreply, err := pb.System.UserList(context.Background(), &mreq)
  38. if err != nil {
  39. return ret, err
  40. }
  41. for _, v := range mreply.List {
  42. ret[v.Id] = *v
  43. }
  44. return ret, nil
  45. }
  46. //
  47. func HouseRentManagerList(ctx context.Context, req *pb_v1.HouseRentManagerListRequest) (reply *pb_v1.HouseRentManagerListReply, err error) {
  48. reply = &pb_v1.HouseRentManagerListReply{}
  49. // 捕获各个task中的异常并返回给调用者
  50. defer func() {
  51. if r := recover(); r != nil {
  52. err = fmt.Errorf("%+v", r)
  53. e := &status.Status{}
  54. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  55. logger.Error("err",
  56. zap.String("system_err", err.Error()),
  57. zap.Stack("stacktrace"))
  58. }
  59. }
  60. }()
  61. // 参数检查
  62. err = checkHouseRentManagerListParam(req)
  63. if err != nil {
  64. return nil, err
  65. }
  66. dbname := utils.GetGardenDbName(req.GardenId)
  67. p := dbmodel.NewHouseRentManager(dbname)
  68. where := map[string]interface{}{
  69. "rent_id":req.RentId,
  70. }
  71. list, err := p.List(database.DB(), where, nil, -1, -1)
  72. uids := []int64{}
  73. for _, v := range list {
  74. uids = append(uids, v.ManagerUid)
  75. }
  76. managerInfos, err := getManagerInfos(uids, req.GardenId)
  77. if err != nil {
  78. return nil, err
  79. }
  80. reply.List = make([]*pb_v1.HouseRentManagerItem, len(list))
  81. for i, v := range list {
  82. item := &pb_v1.HouseRentManagerItem{
  83. Id:v.ID,
  84. ManagerUid:v.ManagerUid,
  85. Name:managerInfos[v.ManagerUid].RealName,
  86. UserName:managerInfos[v.ManagerUid].UserName,
  87. Phone:managerInfos[v.ManagerUid].Phone,
  88. OpenimId:managerInfos[v.ManagerUid].OpenimId,
  89. }
  90. reply.List[i] = item
  91. }
  92. return reply, nil
  93. }