building_list.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "property-garden/errors"
  9. dbmodel "property-garden/model"
  10. pb_v1 "property-garden/pb/v1"
  11. "property-garden/utils"
  12. "git.getensh.com/common/gopkgs/database"
  13. "git.getensh.com/common/gopkgs/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. )
  17. func checkBuildingListParam(req *pb_v1.BuildingListRequest) error {
  18. if req.Page == 0 {
  19. req.Page = 1
  20. }
  21. if req.PageSize == 0 {
  22. req.PageSize = 10
  23. }
  24. if req.GardenId < 1 {
  25. return errors.DataBaseError
  26. }
  27. return nil
  28. }
  29. //
  30. func BuildingList(ctx context.Context, req *pb_v1.BuildingListRequest) (reply *pb_v1.BuildingListReply, err error) {
  31. reply = &pb_v1.BuildingListReply{}
  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. err = checkBuildingListParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. dbname := utils.GetGardenDbName(req.GardenId)
  49. p := dbmodel.NewBuildingUnit(dbname)
  50. where := map[string]interface{}{
  51. //"garden_id":req.GardenId,
  52. }
  53. if req.BuildingNumber != "" {
  54. where["building_number"] = req.BuildingNumber
  55. }
  56. reply.Page = req.Page
  57. reply.Total, err = p.BuildingCount(database.DB(), where, nil)
  58. if err != nil {
  59. return nil, errors.DataBaseError
  60. }
  61. if reply.Total == 0 {
  62. return reply, nil
  63. }
  64. list, err := p.BuildingList(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  65. if err != nil {
  66. return nil, errors.DataBaseError
  67. }
  68. reply.List = make([]*pb_v1.BuildingItem, len(list))
  69. for i, v := range list {
  70. reply.List[i]=&pb_v1.BuildingItem{
  71. Id:v.ID,
  72. BuildingNumber:v.BuildingNumber,
  73. BuildingUsedArea:v.BuildingUsedArea,
  74. BuildingArea:v.BuildingArea,
  75. BuildingName:v.BuildingName,
  76. UnitCount:v.UnitCount,
  77. Comment:v.Comment,
  78. }
  79. }
  80. return reply, nil
  81. }