charge_bill_obj_list.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. )
  18. func checkChargeBillObjListParam(req *pb_v1.ChargeBillObjListRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. }
  23. if req.Page == 0 {
  24. req.Page = 1
  25. }
  26. if req.PageSize == 0 {
  27. req.PageSize = 10
  28. }
  29. return nil
  30. }
  31. type ChargeBillObjItem struct {
  32. ObjName string
  33. ObjId int64
  34. ObjType int32
  35. BillCount int64
  36. ShouldPayAmount int64
  37. }
  38. func GetObjLateFee(objId int64, objType int32, chargeInfoM map[int64]dbmodel.TChargeConf, dbname string) (int64, error) {
  39. p := dbmodel.NewChargeBill(dbname)
  40. where := map[string]interface{}{
  41. "obj_id": objId,
  42. "obj_type": objType,
  43. }
  44. page := 1
  45. ret := int64(0)
  46. for {
  47. list, err := p.List(database.DB(), where, nil, page, 10, "")
  48. if err != nil {
  49. return 0, errors.DataBaseError
  50. }
  51. if len(list) == 0 {
  52. return ret, nil
  53. }
  54. for _, v := range list {
  55. chargeInfo := chargeInfoM[v.ChargeId]
  56. lfee, _ := charge_utils.GetLateFee(v.Amount, v.ChargeEnd, &chargeInfo)
  57. ret += lfee
  58. }
  59. if len(list) < 10 {
  60. break
  61. }
  62. }
  63. return ret, nil
  64. }
  65. func ChargeBillObjList(ctx context.Context, req *pb_v1.ChargeBillObjListRequest) (reply *pb_v1.ChargeBillObjListReply, err error) {
  66. reply = &pb_v1.ChargeBillObjListReply{}
  67. // 捕获各个task中的异常并返回给调用者
  68. defer func() {
  69. if r := recover(); r != nil {
  70. err = fmt.Errorf("%+v", r)
  71. e := &status.Status{}
  72. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  73. logger.Error("err",
  74. zap.String("system_err", err.Error()),
  75. zap.Stack("stacktrace"))
  76. }
  77. }
  78. }()
  79. // 参数检查
  80. err = checkChargeBillObjListParam(req)
  81. if err != nil {
  82. return nil, err
  83. }
  84. dbname := utils.GetGardenDbName(req.GardenId)
  85. bill := dbmodel.NewChargeBill(dbname)
  86. result := []ChargeBillObjItem{}
  87. group := "obj_type, obj_id"
  88. selectStr := "obj_id, obj_type, obj_name, sum(amount) as should_pay_amount, sum(1) as bill_count"
  89. where := map[string]interface{}{}
  90. reply.Total, err = bill.CountGroup(database.DB(), where, nil, group)
  91. if err != nil {
  92. return nil, errors.DataBaseError
  93. }
  94. reply.Page = req.Page
  95. if reply.Total == 0 {
  96. return reply, nil
  97. }
  98. err = bill.SelectGroup(database.DB(), where, nil,
  99. selectStr, group, int(req.Page), int(req.PageSize), &result)
  100. if err != nil {
  101. return nil, errors.DataBaseError
  102. }
  103. chargeInfoM, err := charge_utils.GetAllChargeInfos(dbname)
  104. if err != nil {
  105. return nil, err
  106. }
  107. reply.List = make([]*pb_v1.ChargeBillObjItem, len(result))
  108. for i, v := range result {
  109. item := &pb_v1.ChargeBillObjItem{
  110. ObjName: v.ObjName,
  111. ObjId: v.ObjId,
  112. ObjType: v.ObjType,
  113. BillCount: v.BillCount,
  114. ShouldPayAmount: v.ShouldPayAmount,
  115. }
  116. lateFee, err := GetObjLateFee(v.ObjId, v.ObjType, chargeInfoM, dbname)
  117. if err != nil {
  118. return nil, err
  119. }
  120. item.ShouldPayAmount += lateFee
  121. reply.List[i] = item
  122. }
  123. return reply, nil
  124. }