list.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package property_package
  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 checkPropertyPackageListParam(req *pb_v1.PropertyPackageListRequest) 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 PropertyPackageList(ctx context.Context, req *pb_v1.PropertyPackageListRequest) (reply *pb_v1.PropertyPackageListReply, err error) {
  31. reply = &pb_v1.PropertyPackageListReply{}
  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 = checkPropertyPackageListParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. dbname := utils.GetGardenDbName(req.GardenId)
  50. p := dbmodel.NewPropertyPackage(dbname)
  51. where := map[string]interface{}{
  52. //"garden_id":req.GardenId,
  53. }
  54. reply.Page = req.Page
  55. reply.Total, err = p.Count(database.DB(), where, nil)
  56. if err != nil {
  57. return nil, errors.DataBaseError
  58. }
  59. if reply.Total == 0 {
  60. return reply, nil
  61. }
  62. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  63. if err != nil {
  64. return nil, errors.DataBaseError
  65. }
  66. reply.List = make([]*pb_v1.PropertyPackageItem, len(list))
  67. for i, v := range list {
  68. item := &pb_v1.PropertyPackageItem{
  69. GiveContent:v.GiveContent,
  70. GiveMonths:v.GiveMonths,
  71. PackageType:v.PackageType,
  72. PayMonths:v.PayMonths,
  73. Enable:false,
  74. Id:v.ID,
  75. }
  76. if v.Enable == 1 {
  77. item.Enable = true
  78. }
  79. reply.List[i] = item
  80. }
  81. return reply, nil
  82. }