repair_class_list.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package repair
  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 checkRepairClassListParam(req *pb_v1.RepairClassListRequest) error {
  18. switch {
  19. case req.GardenId == 0:
  20. return status.Error(10003, "小区不能为空")
  21. }
  22. if req.PageSize == 0 {
  23. req.PageSize = 10
  24. }
  25. if req.Page == 0 {
  26. req.Page = 1
  27. }
  28. return nil
  29. }
  30. func RepairClassList(ctx context.Context, req *pb_v1.RepairClassListRequest) (reply *pb_v1.RepairClassListReply, err error) {
  31. reply = &pb_v1.RepairClassListReply{}
  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 = checkRepairClassListParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. dbname := utils.GetGardenDbName(req.GardenId)
  50. p := dbmodel.NewRepairClass(dbname)
  51. where := map[string]interface{}{
  52. //"garden_id":req.GardenId,
  53. }
  54. if req.Area > 0 {
  55. where["area"] = req.Area
  56. }
  57. reply.Page = req.Page
  58. reply.Total, err = p.Count(database.DB(), where, nil)
  59. if err != nil {
  60. return nil, errors.DataBaseError
  61. }
  62. if reply.Total == 0 {
  63. return reply, nil
  64. }
  65. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  66. if err != nil {
  67. return nil, errors.DataBaseError
  68. }
  69. reply.List = make([]*pb_v1.RepairClassItem, len(list))
  70. for i, v := range list {
  71. item := &pb_v1.RepairClassItem{
  72. Name:v.Name,
  73. Enable:false,
  74. Id:v.ID,
  75. Area:v.Area,
  76. }
  77. if v.Enable == 1 {
  78. item.Enable = true
  79. }
  80. reply.List[i] = item
  81. }
  82. return reply, nil
  83. }