list.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. "property-garden/errors"
  13. dbmodel "property-garden/model"
  14. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. )
  17. //
  18. func ParkList(ctx context.Context, req *pb_v1.ParkListRequest) (reply *pb_v1.ParkListReply, err error) {
  19. reply = &pb_v1.ParkListReply{}
  20. // 捕获各个task中的异常并返回给调用者
  21. defer func() {
  22. if r := recover(); r != nil {
  23. err = fmt.Errorf("%+v", r)
  24. e := &status.Status{}
  25. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  26. logger.Error("err",
  27. zap.String("system_err", err.Error()),
  28. zap.Stack("stacktrace"))
  29. }
  30. }
  31. }()
  32. if req.GardenId < 1 {
  33. return nil, errors.ParamsError
  34. }
  35. dbname := utils.GetGardenDbName(req.GardenId)
  36. p := dbmodel.NewPark(dbname)
  37. where := map[string]interface{}{
  38. //"garden_id":req.GardenId,
  39. }
  40. list, err := p.List(database.DB(), where, nil, -1, -1)
  41. if err != nil {
  42. return nil, errors.DataBaseError
  43. }
  44. reply.List = make([]*pb_v1.ParkItem, len(list))
  45. for i, v := range list {
  46. reply.List[i] = &pb_v1.ParkItem{
  47. Id:v.ID,
  48. ParkName:v.ParkName,
  49. ParkNumber:v.ParkNumber,
  50. ParkType:v.ParkType,
  51. SpaceCount:v.SpaceCount,
  52. Comment:v.Comment,
  53. GardenId:req.GardenId,
  54. CreatedAt:v.CreatedAt.Format("2006-01-02 15:04:05"),
  55. }
  56. }
  57. return reply, nil
  58. }