list.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package charge_conf
  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 checkChargeConfListParam(req *pb_v1.ChargeConfListRequest) 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. if req.PageSize == -1 || req.Page == -1 {
  29. req.Page = -1
  30. req.PageSize = -1
  31. }
  32. return nil
  33. }
  34. func ChargeConfList(ctx context.Context, req *pb_v1.ChargeConfListRequest) (reply *pb_v1.ChargeConfListReply, err error) {
  35. reply = &pb_v1.ChargeConfListReply{}
  36. // 捕获各个task中的异常并返回给调用者
  37. defer func() {
  38. if r := recover(); r != nil {
  39. err = fmt.Errorf("%+v", r)
  40. e := &status.Status{}
  41. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  42. logger.Error("err",
  43. zap.String("system_err", err.Error()),
  44. zap.Stack("stacktrace"))
  45. }
  46. }
  47. }()
  48. // 参数检查
  49. err = checkChargeConfListParam(req)
  50. if err != nil {
  51. return nil, err
  52. }
  53. dbname := utils.GetGardenDbName(req.GardenId)
  54. p := dbmodel.NewChargeConf(dbname)
  55. where := map[string]interface{}{
  56. //"garden_id":req.GardenId,
  57. }
  58. if req.ChargeType > 0 {
  59. where["charge_type"] = req.ChargeType
  60. }
  61. reply.Page = req.Page
  62. reply.Total, err = p.Count(database.DB(), where, nil)
  63. if err != nil {
  64. return nil, errors.DataBaseError
  65. }
  66. if reply.Total == 0 {
  67. return reply, nil
  68. }
  69. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  70. if err != nil {
  71. return nil, errors.DataBaseError
  72. }
  73. reply.List = make([]*pb_v1.ChargeConfItem, len(list))
  74. for i, v := range list {
  75. item := &pb_v1.ChargeConfItem{
  76. GardenId: req.GardenId,
  77. ChargeType: v.ChargeType,
  78. ChargeName: v.ChargeName,
  79. ChargeTimeType: v.ChargeTimeType,
  80. ChargeBasis: v.ChargeBasis,
  81. FixAmount: v.FixAmount,
  82. FixAmountName: v.FixAmountName,
  83. UnitPrice: v.UnitPrice,
  84. Id: v.ID,
  85. ChargeEffectiveTime: v.ChargeEffectiveTime,
  86. BillPeriod: v.BillPeriod,
  87. BillPeriodType: v.BillPeriodType,
  88. LateFeeEnable: false,
  89. LateFeeMaxDays: v.LateFeeMaxDays,
  90. LateFeeDay: v.LateFeeDay,
  91. LateFeePercent: v.LateFeePercent,
  92. }
  93. if v.LateFeeEnable == 1 {
  94. item.LateFeeEnable = true
  95. }
  96. reply.List[i] = item
  97. }
  98. return reply, nil
  99. }