charge_order_list.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. dbmodel "property-garden/model"
  14. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. "strings"
  17. "time"
  18. )
  19. func checkChargeOrderListParam(req *pb_v1.ChargeOrderListRequest) 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 ChargeOrderList(ctx context.Context, req *pb_v1.ChargeOrderListRequest) (reply *pb_v1.ChargeOrderListReply, err error) {
  33. reply = &pb_v1.ChargeOrderListReply{}
  34. // 捕获各个task中的异常并返回给调用者
  35. defer func() {
  36. if r := recover(); r != nil {
  37. err = fmt.Errorf("%+v", r)
  38. e := &status.Status{}
  39. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  40. logger.Error("err",
  41. zap.String("system_err", err.Error()),
  42. zap.Stack("stacktrace"))
  43. }
  44. }
  45. }()
  46. // 参数检查
  47. err = checkChargeOrderListParam(req)
  48. if err != nil {
  49. return nil, err
  50. }
  51. dbname := utils.GetGardenDbName(req.GardenId)
  52. order := dbmodel.NewChargeOrder(dbname)
  53. where := map[string]interface{}{}
  54. where["deleted"] = 0
  55. if req.PayType > 0 {
  56. where["pay_type"] = req.PayType
  57. }
  58. if req.ObjName != "" {
  59. where["obj_name"] = req.ObjName
  60. }
  61. if req.PayStatus > 0 {
  62. where["status"] = req.PayStatus
  63. }
  64. if req.Uid > 0 {
  65. where["uid"] = req.Uid
  66. }
  67. // 账单数量
  68. reply.Total, err = order.Count(database.DB(), where, nil)
  69. if err != nil {
  70. return nil, errors.DataBaseError
  71. }
  72. if reply.Total == 0 {
  73. return reply, nil
  74. }
  75. reply.Page = req.Page
  76. // 账单列表
  77. list, err := order.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  78. if err != nil {
  79. return nil, errors.DataBaseError
  80. }
  81. reply.List = make([]*pb_v1.ChargeOrderItem, len(list))
  82. for i, v := range list {
  83. item := &pb_v1.ChargeOrderItem{
  84. TransactionId:v.TransactionId,
  85. PayingDesc:v.PayingDesc,
  86. ShouldPayAmount:v.Amount,
  87. PayAmount:v.PayAmount,
  88. Comment:v.Comment,
  89. PayTime:time.Unix(v.PayTime, 0).Format("2006-01-02 15:04:05"),
  90. PayType:v.PayType,
  91. ObjName:v.ObjName,
  92. OrderId:v.OrderId,
  93. Status:int32(v.Status),
  94. Desc:v.PayingDesc,
  95. }
  96. billCount := len(strings.Split(v.BillIds, ","))
  97. if billCount == 0 && v.OnlinePay == 1 {
  98. billCount = 1
  99. }
  100. item.BillCount = int64(billCount)
  101. reply.List[i] = item
  102. }
  103. return reply, nil
  104. }