charge_payed_bill_list.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 checkChargePayedBillListParam(req *pb_v1.ChargePayedBillListRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. }
  24. if req.Page == 0 {
  25. req.Page = 1
  26. }
  27. if req.PageSize == 0 {
  28. req.PageSize = 10
  29. }
  30. return nil
  31. }
  32. func getMonthStartAndEnd(data int64) (int64, int64) {
  33. t := time.Unix(data, 0)
  34. start := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local)
  35. t = t.AddDate(0, 1, 0)
  36. end := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local)
  37. return start.Unix(), end.Unix()
  38. }
  39. func ChargePayedBillList(ctx context.Context, req *pb_v1.ChargePayedBillListRequest) (reply *pb_v1.ChargePayedBillListReply, err error) {
  40. reply = &pb_v1.ChargePayedBillListReply{}
  41. // 捕获各个task中的异常并返回给调用者
  42. defer func() {
  43. if r := recover(); r != nil {
  44. err = fmt.Errorf("%+v", r)
  45. e := &status.Status{}
  46. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  47. logger.Error("err",
  48. zap.String("system_err", err.Error()),
  49. zap.Stack("stacktrace"))
  50. }
  51. }
  52. }()
  53. // 参数检查
  54. err = checkChargePayedBillListParam(req)
  55. if err != nil {
  56. return nil, err
  57. }
  58. dbname := utils.GetGardenDbName(req.GardenId)
  59. bill := dbmodel.NewChargeBillPayed(dbname)
  60. where := map[string]interface{}{}
  61. if req.ChargeType > 0 {
  62. where["charge_type"] = req.ChargeType
  63. }
  64. if req.ObjName != "" {
  65. where["obj_name"] = req.ObjName
  66. }
  67. if req.ChargeName != "" {
  68. where["charge_name"] = req.ChargeName
  69. }
  70. if req.PayTime > 0 {
  71. start, end := getMonthStartAndEnd(req.PayTime)
  72. where["pay_time >="] = start
  73. where["pay_time <"] = end
  74. }
  75. if req.HouseId > 0 {
  76. where["house_id"] = req.HouseId
  77. }
  78. if req.ObjType > 0 {
  79. where["obj_type"] = req.ObjType
  80. }
  81. // 账单数量
  82. reply.Total, err = bill.Count(database.DB(), where, nil)
  83. if err != nil {
  84. return nil, errors.DataBaseError
  85. }
  86. if reply.Total == 0 {
  87. return reply, nil
  88. }
  89. reply.Page = req.Page
  90. // 账单列表
  91. list, err := bill.List(database.DB(), where, nil, int(req.Page), int(req.PageSize), "pay_time desc")
  92. if err != nil {
  93. return nil, errors.DataBaseError
  94. }
  95. reply.List = make([]*pb_v1.ChargePayedBillItem, len(list))
  96. for i, v := range list {
  97. chargeDesc := dbmodel.ChargeDesc{}
  98. json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc)
  99. item := &pb_v1.ChargePayedBillItem{
  100. Start: time.Unix(v.ChargeStart, 0).Format("2006-01-02"),
  101. End: time.Unix(v.ChargeEnd, 0).Format("2006-01-02"),
  102. ChargeType: v.ChargeType,
  103. ChargeName: v.ChargeName,
  104. Amount: v.Amount,
  105. LastUsed: chargeDesc.PowerLastUsed,
  106. CurrentUsed: chargeDesc.PowerCurrentUsed,
  107. LateFee: v.LateFee,
  108. BillId: v.ID,
  109. ChargeDesc: charge_utils.GetChargeDesc(chargeDesc),
  110. PayTime: time.Unix(v.PayTime, 0).Format("2006-01-02 15:04:05"),
  111. PayType: v.PayType,
  112. ObjName: v.ObjName,
  113. ObjType: v.ObjType,
  114. }
  115. reply.List[i] = item
  116. }
  117. return reply, nil
  118. }