charge_obj_bill_list.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package charge
  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. "property-garden/impl/v1/charge_utils"
  14. dbmodel "property-garden/model"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. "time"
  18. )
  19. func checkChargeObjBillListParam(req *pb_v1.ChargeObjBillListRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case !req.All && req.ObjId == 0:
  24. //return status.Error(10003, "对象id不能为空")
  25. case !req.All && req.ObjType == 0:
  26. //return status.Error(10003, "对象类型不能为空")
  27. }
  28. if req.Page == 0 {
  29. req.Page = 1
  30. }
  31. if req.PageSize == 0 {
  32. req.PageSize = 10
  33. }
  34. return nil
  35. }
  36. func ChargeObjBillList(ctx context.Context, req *pb_v1.ChargeObjBillListRequest) (reply *pb_v1.ChargeObjBillListReply, err error) {
  37. reply = &pb_v1.ChargeObjBillListReply{}
  38. // 捕获各个task中的异常并返回给调用者
  39. defer func() {
  40. if r := recover(); r != nil {
  41. err = fmt.Errorf("%+v", r)
  42. e := &status.Status{}
  43. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  44. logger.Error("err",
  45. zap.String("system_err", err.Error()),
  46. zap.Stack("stacktrace"))
  47. }
  48. }
  49. }()
  50. // 参数检查
  51. err = checkChargeObjBillListParam(req)
  52. if err != nil {
  53. return nil, err
  54. }
  55. dbname := utils.GetGardenDbName(req.GardenId)
  56. chargeInfoM, err := charge_utils.GetAllChargeInfos(dbname)
  57. if err != nil {
  58. return nil, err
  59. }
  60. bill := dbmodel.NewChargeBill(dbname)
  61. where := map[string]interface{}{}
  62. if req.ObjType > 0 {
  63. where["obj_type"] = req.ObjType
  64. }
  65. if req.ObjId > 0 {
  66. where["obj_id"] = req.ObjId
  67. }
  68. if req.ChargeType > 0 {
  69. where["charge_type"] = req.ChargeType
  70. }
  71. if req.ObjName != "" {
  72. where["obj_name"] = req.ObjName
  73. }
  74. if req.ChargeName != "" {
  75. where["charge_name"] = req.ChargeName
  76. }
  77. // 账单数量
  78. reply.Total, err = bill.Count(database.DB(), where, nil)
  79. if err != nil {
  80. return nil, errors.DataBaseError
  81. }
  82. if reply.Total == 0 {
  83. return reply, nil
  84. }
  85. reply.Page = req.Page
  86. // 账单列表
  87. list, err := bill.List(database.DB(), where, nil, int(req.Page), int(req.PageSize), "")
  88. if err != nil {
  89. return nil, errors.DataBaseError
  90. }
  91. reply.List = make([]*pb_v1.ChargeBillItem, len(list))
  92. for i, v := range list {
  93. chargeInfo := chargeInfoM[v.ChargeId]
  94. chargeDesc := dbmodel.ChargeDesc{}
  95. json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc)
  96. lfee, ldays := charge_utils.GetLateFee(v.Amount, v.ChargeEnd, &chargeInfo)
  97. item := &pb_v1.ChargeBillItem{
  98. Start: time.Unix(v.ChargeStart, 0).Format("2006-01-02"),
  99. End: time.Unix(v.ChargeEnd, 0).Format("2006-01-02"),
  100. ChargeType: v.ChargeType,
  101. ChargeName: v.ChargeName,
  102. Amount: v.Amount,
  103. LastUsed: chargeDesc.PowerLastUsed,
  104. CurrentUsed: chargeDesc.PowerCurrentUsed,
  105. LateFee: lfee,
  106. LateDays: ldays,
  107. BillId: v.ID,
  108. ChargeDesc: charge_utils.GetChargeDesc(chargeDesc),
  109. ObjId: v.ObjId,
  110. ObjType: v.ObjType,
  111. ObjName: v.ObjName,
  112. }
  113. reply.ShouldPayAmount += item.Amount
  114. reply.ShouldPayAmount += item.LateFee
  115. reply.List[i] = item
  116. }
  117. return reply, nil
  118. }