charge_list.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package accounting
  4. import (
  5. "context"
  6. "gd_management/apis"
  7. "gd_management/common.in/utils"
  8. "gd_management/consts"
  9. "gd_management/errors"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/astaxie/beego/orm"
  13. "go.uber.org/zap"
  14. )
  15. func ChargeList(ctx context.Context, req *apis.ChargeListReq, reply *apis.ChargeListReply) error {
  16. if req.PageNumber > 0 {
  17. req.PageNumber -= 1
  18. } else {
  19. req.PageNumber = 0
  20. }
  21. o := orm.NewOrm()
  22. sql := "select t1.* ,t2.merchant_name from t_gd_bill as t1 left join t_gd_merchants as t2 on t1.merchant_id=t2.id where t1.bill_type=1"
  23. where := ""
  24. if req.MerchantId != 0 {
  25. where = fmt.Sprintf("%s and t1.merchant_id=%d", where, req.MerchantId)
  26. }
  27. if req.StartTime != "" {
  28. if len(req.StartTime) > consts.DayLen {
  29. req.StartTime = req.StartTime[0:consts.DayLen]
  30. }
  31. where = fmt.Sprintf("%s and t1.bill_create_time>='%s'", where, req.StartTime)
  32. }
  33. if req.EndTime != "" {
  34. if len(req.EndTime) > consts.DayLen {
  35. req.EndTime = req.EndTime[0:consts.DayLen]
  36. }
  37. where = fmt.Sprintf("%s and t1.bill_create_time<='%s'", where, req.EndTime)
  38. }
  39. sql = sql + where
  40. sql = fmt.Sprintf("%s order by t1.id desc", sql)
  41. sql = sql + " LIMIT ? OFFSET ?"
  42. countSql := `select count(1) from t_gd_bill as t1 where t1.bill_type=1` + where
  43. err := o.Raw(countSql).QueryRow(&reply.Total)
  44. if err != nil {
  45. l.Error("func",
  46. zap.String("call", "ChargeList"),
  47. zap.String("args", utils.MarshalJsonString(req)),
  48. zap.String("error", err.Error()))
  49. return errors.DataBaseError
  50. }
  51. if reply.Total == 0 {
  52. reply.PageSize = PAGESIZE
  53. return nil
  54. }
  55. _, err = o.Raw(sql, PAGESIZE, req.PageNumber*PAGESIZE).QueryRows(&reply.List)
  56. if err != nil {
  57. l.Error("func",
  58. zap.String("call", "ChargeList"),
  59. zap.String("args", utils.MarshalJsonString(req)),
  60. zap.String("error", err.Error()))
  61. return errors.DataBaseError
  62. }
  63. for index, _ := range reply.List {
  64. if reply.List[index].Remark != "" {
  65. json.Unmarshal([]byte(reply.List[index].Remark), &reply.List[index].RemarkList)
  66. }
  67. }
  68. reply.PageSize = PAGESIZE
  69. reply.PageNumber = req.PageNumber + 1
  70. return nil
  71. }