order.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/vip/model"
  6. "go-common/library/database/sql"
  7. "github.com/pkg/errors"
  8. )
  9. //OrderCount order count.
  10. func (d *Dao) OrderCount(c context.Context, arg *model.ArgPayOrder) (count int64, err error) {
  11. if arg.Mid == 0 && len(arg.OrderNo) == 0 {
  12. return
  13. }
  14. sqlStr := _vipOrderCountSQL
  15. if arg.Mid > 0 {
  16. sqlStr += fmt.Sprintf(" AND mid = %v", arg.Mid)
  17. }
  18. if len(arg.OrderNo) > 0 {
  19. sqlStr += fmt.Sprintf(" AND order_no = '%v'", arg.OrderNo)
  20. }
  21. if arg.Status > 0 {
  22. sqlStr += fmt.Sprintf(" AND status = %v", arg.Status)
  23. }
  24. var row = d.db.QueryRow(c, sqlStr)
  25. if err = row.Scan(&count); err != nil {
  26. err = errors.WithStack(err)
  27. d.errProm.Incr("row_scan_db")
  28. return
  29. }
  30. return
  31. }
  32. // OrderList order list.
  33. func (d *Dao) OrderList(c context.Context, arg *model.ArgPayOrder) (res []*model.PayOrder, err error) {
  34. if arg.Mid == 0 && len(arg.OrderNo) == 0 {
  35. return
  36. }
  37. sqlStr := _vipOrderListSQL
  38. if arg.Mid > 0 {
  39. sqlStr += fmt.Sprintf(" AND mid = %v", arg.Mid)
  40. }
  41. if len(arg.OrderNo) > 0 {
  42. sqlStr += fmt.Sprintf(" AND order_no = '%v'", arg.OrderNo)
  43. }
  44. if arg.Status > 0 {
  45. sqlStr += fmt.Sprintf(" AND status = %v", arg.Status)
  46. }
  47. if arg.PN < 0 {
  48. arg.PN = _defpn
  49. }
  50. if arg.PS < 0 || arg.PS > 100 {
  51. arg.PS = _defps
  52. }
  53. sqlStr += fmt.Sprintf(" ORDER BY ID DESC LIMIT %v,%v", (arg.PN-1)*arg.PS, arg.PS)
  54. var rows *sql.Rows
  55. if rows, err = d.db.Query(c, sqlStr); err != nil {
  56. err = errors.WithStack(err)
  57. d.errProm.Incr("query_db")
  58. return
  59. }
  60. defer rows.Close()
  61. for rows.Next() {
  62. r := new(model.PayOrder)
  63. if err = rows.Scan(&r.ID, &r.OrderNo, &r.AppID, &r.Platform, &r.OrderType, &r.Mid, &r.ToMid, &r.BuyMonths, &r.Money, &r.RefundAmount, &r.Status, &r.PayType, &r.RechargeBp,
  64. &r.ThirdTradeNo, &r.Ver, &r.PaymentTime, &r.Ctime, &r.Mtime, &r.AppSubID); err != nil {
  65. err = errors.WithStack(err)
  66. d.errProm.Incr("row_scan_db")
  67. res = nil
  68. return
  69. }
  70. res = append(res, r)
  71. }
  72. err = rows.Err()
  73. return
  74. }
  75. // SelOrder sel order by orderNo.
  76. func (d *Dao) SelOrder(c context.Context, orderNo string) (r *model.PayOrder, err error) {
  77. row := d.db.QueryRow(c, _vipOrderSQL, orderNo)
  78. r = new(model.PayOrder)
  79. if err = row.Scan(&r.ID, &r.OrderNo, &r.AppID, &r.Platform, &r.OrderType, &r.Mid, &r.ToMid, &r.BuyMonths, &r.Money, &r.RefundAmount, &r.Status, &r.PayType, &r.RechargeBp,
  80. &r.ThirdTradeNo, &r.Ver, &r.PaymentTime, &r.Ctime, &r.Mtime, &r.AppSubID); err != nil {
  81. if err == sql.ErrNoRows {
  82. err = nil
  83. r = nil
  84. return
  85. }
  86. err = errors.WithStack(err)
  87. d.errProm.Incr("row_scan_db")
  88. }
  89. return
  90. }
  91. //AddPayOrderLog add order log.
  92. func (d *Dao) AddPayOrderLog(c context.Context, arg *model.PayOrderLog) (err error) {
  93. if _, err = d.db.Exec(c, _addOrderLogSQL, arg.OrderNo, arg.RefundID, arg.RefundAmount, arg.Operator, arg.Mid, arg.Status); err != nil {
  94. err = errors.WithStack(err)
  95. d.errProm.Incr("db_exec")
  96. }
  97. return
  98. }