charge_list.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "strings"
  18. "time"
  19. )
  20. func checkChargeListParam(req *pb_v1.ChargeListRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. case req.ObjId == 0:
  25. return status.Error(10003, "对象不能为空")
  26. case req.ObjType == 0:
  27. return status.Error(10003, "对象类型不能为空")
  28. }
  29. if req.Page == 0 {
  30. req.Page = 1
  31. }
  32. if req.PageSize == 0 {
  33. req.PageSize = 10
  34. }
  35. return nil
  36. }
  37. func ChargeList(ctx context.Context, req *pb_v1.ChargeListRequest) (reply *pb_v1.ChargeListReply, err error) {
  38. reply = &pb_v1.ChargeListReply{}
  39. // 捕获各个task中的异常并返回给调用者
  40. defer func() {
  41. if r := recover(); r != nil {
  42. err = fmt.Errorf("%+v", r)
  43. e := &status.Status{}
  44. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  45. logger.Error("err",
  46. zap.String("system_err", err.Error()),
  47. zap.Stack("stacktrace"))
  48. }
  49. }
  50. }()
  51. // 参数检查
  52. err = checkChargeListParam(req)
  53. if err != nil {
  54. return nil, err
  55. }
  56. dbname := utils.GetGardenDbName(req.GardenId)
  57. bind := dbmodel.NewChargeBind(dbname)
  58. where := map[string]interface{}{
  59. "obj_id": req.ObjId,
  60. "obj_type": req.ObjType,
  61. }
  62. if req.ChargeType > 0 {
  63. where["charge_type"] = req.ChargeType
  64. }
  65. reply.Total, err = bind.Count(database.DB(), where, nil)
  66. if err != nil {
  67. return nil, errors.DataBaseError
  68. }
  69. reply.Page = req.Page
  70. if reply.Total == 0 {
  71. return reply, nil
  72. }
  73. list, err := bind.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  74. if err != nil {
  75. return nil, errors.DataBaseError
  76. }
  77. reply.List = make([]*pb_v1.ChargeItem, len(list))
  78. chargeIds := make([]int64, len(list))
  79. for i, v := range list {
  80. chargeIds[i] = v.ChargeId
  81. }
  82. chargeM, err := charge_utils.GetChargeInfos(chargeIds, dbname)
  83. if err != nil {
  84. return nil, err
  85. }
  86. bill := dbmodel.NewChargeBill(dbname)
  87. type Bcount struct {
  88. Count int64 `json:"count"`
  89. BindId int64 `json:"bind_id"`
  90. }
  91. blist := []Bcount{}
  92. err = bill.SelectGroup(database.DB(), where, nil, "sum(1) as count, charge_bind_id as bind_id", "charge_bind_id", -1, -1, &blist)
  93. if err != nil {
  94. return nil, errors.DataBaseError
  95. }
  96. bm := map[int64]bool{}
  97. for _, v := range blist {
  98. if v.Count > 0 {
  99. bm[v.BindId] = true
  100. }
  101. }
  102. for i, v := range list {
  103. chargeInfo := chargeM[v.ChargeId]
  104. item := &pb_v1.ChargeItem{
  105. BindId: v.ID,
  106. ChargeType: v.ChargeType,
  107. ChargeName: chargeInfo.ChargeName,
  108. ChargeTimeType: chargeInfo.ChargeTimeType,
  109. CreatedAt: v.CreatedAt.Format("2006-01-02"),
  110. Start: "-",
  111. End: "-",
  112. Status: "未开始",
  113. HasBill: bm[v.ID],
  114. }
  115. if v.Start > 0 {
  116. item.Start = time.Unix(v.Start, 0).Format("2006-01-02")
  117. }
  118. if v.End > 0 {
  119. item.End = time.Unix(v.End, 0).Format("2006-01-02")
  120. }
  121. if v.Start <= time.Now().Unix() {
  122. item.Status = "缴费中"
  123. }
  124. array := []string{}
  125. if chargeInfo.ChargeBasis == charge_utils.ChargeBasisSpaceArea ||
  126. chargeInfo.ChargeBasis == charge_utils.ChargeBasisArea ||
  127. chargeInfo.ChargeBasis == charge_utils.ChargeBasisUsedArea {
  128. array = append(array, fmt.Sprintf("面积:%f", v.ObjArea))
  129. }
  130. if chargeInfo.ChargeBasis == charge_utils.ChargeBasisFix {
  131. array = append(array, fmt.Sprintf("固定费用:%.2f", float64(chargeInfo.FixAmount)/100.00))
  132. } else if chargeInfo.FixAmount > 0 {
  133. array = append(array, fmt.Sprintf("附加费用(%s):%.2f", chargeInfo.FixAmountName, float64(chargeInfo.FixAmount)/100.00))
  134. }
  135. if chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf {
  136. array = append(array, fmt.Sprintf("自定义费用:%.2f", float64(v.CustomFee)/100.00))
  137. }
  138. if chargeInfo.UnitPrice > 0 {
  139. array = append(array, fmt.Sprintf("单价:%.2f", float64(chargeInfo.UnitPrice)/100.00))
  140. }
  141. item.Desc = strings.Join(array, "/")
  142. reply.List[i] = item
  143. }
  144. return reply, nil
  145. }