charge_bill_list.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 checkChargeBillListParam(req *pb_v1.ChargeBillListRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case req.BindId == 0:
  24. return status.Error(10003, "关系id不能为空")
  25. }
  26. if req.Page == 0 {
  27. req.Page = 1
  28. }
  29. if req.PageSize == 0 {
  30. req.PageSize = 10
  31. }
  32. return nil
  33. }
  34. func GetBindChargeBillTotalFee(bindId int64, chargeInfoM map[int64]dbmodel.TChargeConf, dbname string) (int64, error) {
  35. p := dbmodel.NewChargeBill(dbname)
  36. where := map[string]interface{}{
  37. "charge_bind_id": bindId,
  38. }
  39. page := 1
  40. ret := int64(0)
  41. for {
  42. list, err := p.List(database.DB(), where, nil, page, 10, "")
  43. if err != nil {
  44. return 0, errors.DataBaseError
  45. }
  46. if len(list) == 0 {
  47. return ret, nil
  48. }
  49. for _, v := range list {
  50. chargeInfo := chargeInfoM[v.ChargeId]
  51. ret += v.Amount
  52. lfee, _ := charge_utils.GetLateFee(v.Amount, v.ChargeEnd, &chargeInfo)
  53. ret += lfee
  54. }
  55. if len(list) < 10 {
  56. break
  57. }
  58. }
  59. return ret, nil
  60. }
  61. func ChargeBillList(ctx context.Context, req *pb_v1.ChargeBillListRequest) (reply *pb_v1.ChargeBillListReply, err error) {
  62. reply = &pb_v1.ChargeBillListReply{}
  63. // 捕获各个task中的异常并返回给调用者
  64. defer func() {
  65. if r := recover(); r != nil {
  66. err = fmt.Errorf("%+v", r)
  67. e := &status.Status{}
  68. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  69. logger.Error("err",
  70. zap.String("system_err", err.Error()),
  71. zap.Stack("stacktrace"))
  72. }
  73. }
  74. }()
  75. // 参数检查
  76. err = checkChargeBillListParam(req)
  77. if err != nil {
  78. return nil, err
  79. }
  80. dbname := utils.GetGardenDbName(req.GardenId)
  81. chargeInfoM, err := charge_utils.GetAllChargeInfos(dbname)
  82. if err != nil {
  83. return nil, err
  84. }
  85. bill := dbmodel.NewChargeBill(dbname)
  86. where := map[string]interface{}{
  87. "charge_bind_id": req.BindId,
  88. }
  89. if req.Status > 0 {
  90. where["status"] = req.Status
  91. }
  92. // 账单数量
  93. reply.Total, err = bill.Count(database.DB(), where, nil)
  94. if err != nil {
  95. return nil, errors.DataBaseError
  96. }
  97. if reply.Total == 0 {
  98. return reply, nil
  99. }
  100. // 总的代缴费用包含滞纳金
  101. reply.ShouldPayAmount, err = GetBindChargeBillTotalFee(req.BindId, chargeInfoM, dbname)
  102. if err != nil {
  103. return nil, err
  104. }
  105. reply.Page = req.Page
  106. // 账单列表
  107. list, err := bill.List(database.DB(), where, nil, int(req.Page), int(req.PageSize), "")
  108. if err != nil {
  109. return nil, errors.DataBaseError
  110. }
  111. reply.List = make([]*pb_v1.ChargeBillItem, len(list))
  112. chargeIds := []int64{}
  113. for _, v := range list {
  114. chargeIds = append(chargeIds, v.ChargeId)
  115. }
  116. for i, v := range list {
  117. chargeInfo := chargeInfoM[v.ChargeId]
  118. chargeDesc := dbmodel.ChargeDesc{}
  119. json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc)
  120. latefee, lateDays := charge_utils.GetLateFee(v.Amount, v.ChargeEnd, &chargeInfo)
  121. item := &pb_v1.ChargeBillItem{
  122. Start: time.Unix(v.ChargeStart, 0).Format("2006-01-02"),
  123. End: time.Unix(v.ChargeEnd, 0).Format("2006-01-02"),
  124. ChargeType: v.ChargeType,
  125. ChargeName: v.ChargeName,
  126. Amount: v.Amount,
  127. LastUsed: chargeDesc.PowerLastUsed,
  128. CurrentUsed: chargeDesc.PowerCurrentUsed,
  129. LateFee: latefee,
  130. LateDays: lateDays,
  131. BillId: v.ID,
  132. ChargeDesc: charge_utils.GetChargeDesc(chargeDesc),
  133. }
  134. if i == 0 {
  135. reply.ChargeName = v.ChargeName
  136. reply.ChargeType = v.ChargeType
  137. }
  138. reply.List[i] = item
  139. }
  140. return reply, nil
  141. }