charge_unpay_list.go 3.9 KB

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