associate_order.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "go-common/app/service/main/vip/model"
  6. "go-common/library/database/sql"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _countOrderByAppidSQL = "SELECT COUNT(1) FROM vip_pay_order WHERE mid = ? AND app_id = ? AND status=2;"
  11. _countAssociateGrantSQL = "SELECT COUNT(1) FROM vip_order_associate_grant WHERE mid = ? AND app_id = ?;"
  12. _countAssociateByOrderNoSQL = "SELECT COUNT(1) FROM vip_order_associate_grant WHERE out_trade_no = ? AND app_id = ?;"
  13. _countAssociateByMidAndMonthsSQL = "SELECT COUNT(1) FROM vip_order_associate_grant WHERE mid = ? AND app_id = ? AND months = ?;"
  14. _insertAssociateGrantOrderSQL = "INSERT INTO vip_order_associate_grant(app_id,mid,months,out_open_id,out_trade_no,state,ctime)VALUES(?,?,?,?,?,?,?);"
  15. _updateAssociateGrantStateSQL = "UPDATE vip_order_associate_grant SET state = ? WHERE out_trade_no = ? AND app_id = ?"
  16. _associateGrantCountInfoSQL = "SELECT id,app_id,mid,months,current_count,ctime,mtime FROM vip_order_associate_grant_count WHERE mid = ? AND app_id = ? AND months = ?;"
  17. _addAssociateGrantCountSQL = "INSERT IGNORE INTO vip_order_associate_grant_count(app_id,mid,months,current_count)VALUES(?,?,?,?);"
  18. _updateAssociateGrantCountSQL = "UPDATE vip_order_associate_grant_count SET current_count = current_count + 1 WHERE mid = ? AND months = ? AND app_id = ? AND current_count = ?;"
  19. )
  20. // CountAssociateOrder associate order count.
  21. func (d *Dao) CountAssociateOrder(c context.Context, mid int64, appID int64) (count int64, err error) {
  22. if err = d.olddb.QueryRow(c, _countOrderByAppidSQL, mid, appID).Scan(&count); err != nil {
  23. err = errors.Wrapf(err, "dao associate count orders(%d,%d)", mid, appID)
  24. }
  25. return
  26. }
  27. // CountAssociateGrants associate grant count.
  28. func (d *Dao) CountAssociateGrants(c context.Context, mid int64, appID int64) (count int64, err error) {
  29. if err = d.olddb.QueryRow(c, _countAssociateGrantSQL, mid, appID).Scan(&count); err != nil {
  30. err = errors.Wrapf(err, "dao associate count grant(%d,%d)", mid, appID)
  31. }
  32. return
  33. }
  34. // CountGrantOrderByOutTradeNo grant order by out_trade_no count.
  35. func (d *Dao) CountGrantOrderByOutTradeNo(c context.Context, outTradeNo string, appID int64) (count int64, err error) {
  36. if err = d.olddb.QueryRow(c, _countAssociateByOrderNoSQL, outTradeNo, appID).Scan(&count); err != nil {
  37. err = errors.Wrapf(err, "dao associate count grant by outno (%s,%d)", outTradeNo, appID)
  38. }
  39. return
  40. }
  41. // CountAssociateByMidAndMonths count grant order by mid and months .
  42. func (d *Dao) CountAssociateByMidAndMonths(c context.Context, mid int64, appID int64, months int32) (count int64, err error) {
  43. if err = d.olddb.QueryRow(c, _countAssociateByMidAndMonthsSQL, mid, appID, months).Scan(&count); err != nil {
  44. err = errors.Wrapf(err, "dao associate count grant by mid and months (%d,%d,%d)", mid, appID, months)
  45. }
  46. return
  47. }
  48. // TxInsertAssociateGrantOrder insert associate grant order.
  49. func (d *Dao) TxInsertAssociateGrantOrder(tx *sql.Tx, oa *model.VipOrderAssociateGrant) (aff int64, err error) {
  50. var res xsql.Result
  51. if res, err = tx.Exec(_insertAssociateGrantOrderSQL, oa.AppID, oa.Mid, oa.Months, oa.OutOpenID, oa.OutTradeNO, oa.State, oa.Ctime); err != nil {
  52. err = errors.Wrapf(err, "dao insert grant(%+v)", oa)
  53. return
  54. }
  55. return res.RowsAffected()
  56. }
  57. // TxUpdateAssociateGrantState update associate grant state.
  58. func (d *Dao) TxUpdateAssociateGrantState(tx *sql.Tx, oa *model.VipOrderAssociateGrant) (aff int64, err error) {
  59. var res xsql.Result
  60. if res, err = tx.Exec(_updateAssociateGrantStateSQL, oa.State, oa.OutTradeNO, oa.AppID); err != nil {
  61. err = errors.Wrapf(err, "dao update grant(%+v)", oa)
  62. return
  63. }
  64. return res.RowsAffected()
  65. }
  66. // AssociateGrantCountInfo associate grant count info.
  67. func (d *Dao) AssociateGrantCountInfo(c context.Context, mid int64, appID int64, months int32) (res *model.VipAssociateGrantCount, err error) {
  68. res = new(model.VipAssociateGrantCount)
  69. if err = d.olddb.QueryRow(c, _associateGrantCountInfoSQL, mid, appID, months).
  70. Scan(&res.ID, &res.AppID, &res.Mid, &res.Months, &res.CurrentCount, &res.Ctime, &res.Mtime); err != nil {
  71. if err == sql.ErrNoRows {
  72. res = nil
  73. err = nil
  74. return
  75. }
  76. err = errors.Wrapf(err, "dao associate openinfo openid(%d,%d,%d)", mid, appID, months)
  77. }
  78. return
  79. }
  80. // AddAssociateGrantCount add associate grant count.
  81. func (d *Dao) AddAssociateGrantCount(c context.Context, arg *model.VipAssociateGrantCount) (err error) {
  82. if _, err = d.olddb.Exec(c, _addAssociateGrantCountSQL, arg.AppID, arg.Mid, arg.Months, arg.CurrentCount); err != nil {
  83. err = errors.Wrapf(err, "dao update bind state(%+v)", arg)
  84. }
  85. return
  86. }
  87. // UpdateAssociateGrantCount update associate grant count.
  88. func (d *Dao) UpdateAssociateGrantCount(c context.Context, arg *model.VipAssociateGrantCount) (err error) {
  89. if _, err = d.olddb.Exec(c, _updateAssociateGrantCountSQL, arg.Mid, arg.Months, arg.AppID, arg.CurrentCount); err != nil {
  90. err = errors.Wrapf(err, "dao update associate grant count(%+v)", arg)
  91. }
  92. return
  93. }