order.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "go-common/app/job/main/vip/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. _insertPayOrder = "INSERT IGNORE INTO vip_pay_order(order_no,app_id,platform,order_type,mid,to_mid,buy_months,money,status,pay_type,recharge_bp,third_trade_no,payment_time,ver,app_sub_id,coupon_money) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
  14. _selPayOrder = "SELECT order_no,app_id,platform,order_type,mid,to_mid,buy_months,money,status,pay_type,third_trade_no,payment_time,ver,app_sub_id FROM vip_pay_order WHERE id>? AND id <=?"
  15. _selPayOrderByMidSQL = "SELECT order_no,app_id,platform,order_type,mid,to_mid,buy_months,money,status,pay_type,third_trade_no,payment_time,ver,app_sub_id FROM vip_pay_order WHERE mid=? AND order_type=? AND status=? ORDER BY ID DESC LIMIT 1"
  16. _selOrderByMidSQL = "SELECT order_no,app_id,platform,order_type,mid,to_mid,buy_months,money,status,pay_type,third_trade_no,payment_time,ver,app_sub_id FROM vip_pay_order WHERE order_no=?"
  17. _selPayOrderLogByMidSQL = "SELECT order_no,status,mid FROM vip_pay_order_log WHERE mid=? AND status=? ORDER BY ID DESC LIMIT 1"
  18. _selOldPayOrder = "SELECT order_no,app_id,order_type,mid,IFNULL(buy_months,0),money,IFNULL(pay_type,4),IFNULL(status,1),ver,IFNULL(platform,3),mtime,app_sub_id,bmid,coupon_money from vip_pay_order WHERE id>? AND id<=?"
  19. _selOldRechargeOrder = "SELECT pay_order_no,third_trade_no,recharge_bp FROM vip_recharge_order WHERE pay_order_no IN "
  20. _updatePayOrderStatusSQL = "UPDATE vip_pay_order SET "
  21. _updateRechageOrderSQL = "UPDATE vip_pay_order SET recharge_bp = ?,third_trade_no = ? where order_no = ?"
  22. _insertPayOrderLog = "INSERT INTO vip_pay_order_log(order_no,mid,status) VALUES(?,?,?)"
  23. _batchAddPayOrder = "INSERT INTO vip_pay_order(order_no,app_id,platform,order_type,mid,to_mid,buy_months,money,status,pay_type,recharge_bp,third_trade_no,payment_time,ver,app_sub_id) VALUES"
  24. _selOrderMaxIDSQL = "SELECT IFNULL(MAX(id),0) FROM vip_pay_order"
  25. _selOldOrderMaxIDSQL = "SELECT IFNULL(MAX(id),0) FROM vip_pay_order"
  26. )
  27. //SelPayOrderByMid sel payorder by mid
  28. func (d *Dao) SelPayOrderByMid(c context.Context, mid int64, orderType, status int8) (r *model.VipPayOrder, err error) {
  29. row := d.db.QueryRow(c, _selPayOrderByMidSQL, mid, orderType, status)
  30. r = new(model.VipPayOrder)
  31. if err = row.Scan(&r.OrderNo, &r.AppID, &r.Platform, &r.OrderType, &r.Mid, &r.ToMid, &r.BuyMonths, &r.Money, &r.Status, &r.PayType, &r.ThirdTradeNo, &r.PaymentTime, &r.Ver, &r.AppSubID); err != nil {
  32. if err == sql.ErrNoRows {
  33. err = nil
  34. r = nil
  35. return
  36. }
  37. err = errors.WithStack(err)
  38. d.errProm.Incr("db_scan")
  39. return
  40. }
  41. return
  42. }
  43. //SelOrderByOrderNo sel payorder by orderNo
  44. func (d *Dao) SelOrderByOrderNo(c context.Context, orderNo string) (r *model.VipPayOrder, err error) {
  45. row := d.db.QueryRow(c, _selOrderByMidSQL, orderNo)
  46. r = new(model.VipPayOrder)
  47. if err = row.Scan(&r.OrderNo, &r.AppID, &r.Platform, &r.OrderType, &r.Mid, &r.ToMid, &r.BuyMonths, &r.Money, &r.Status, &r.PayType, &r.ThirdTradeNo, &r.PaymentTime, &r.Ver, &r.AppSubID); err != nil {
  48. if err == sql.ErrNoRows {
  49. err = nil
  50. r = nil
  51. return
  52. }
  53. err = errors.WithStack(err)
  54. d.errProm.Incr("db_scan")
  55. return
  56. }
  57. return
  58. }
  59. //SelPayOrderLog sel pay order log.
  60. func (d *Dao) SelPayOrderLog(c context.Context, mid int64, status int8) (r *model.VipPayOrderLog, err error) {
  61. row := d.db.QueryRow(c, _selPayOrderLogByMidSQL, mid, status)
  62. r = new(model.VipPayOrderLog)
  63. if err = row.Scan(&r.OrderNo, &r.Status, &r.Mid); err != nil {
  64. if err == sql.ErrNoRows {
  65. err = nil
  66. r = nil
  67. return
  68. }
  69. err = errors.WithStack(err)
  70. d.errProm.Incr("db_scan")
  71. return
  72. }
  73. return
  74. }
  75. //AddPayOrder add payorder
  76. func (d *Dao) AddPayOrder(c context.Context, r *model.VipPayOrder) (a int64, err error) {
  77. var result sql.Result
  78. if result, err = d.db.Exec(c, _insertPayOrder, &r.OrderNo, &r.AppID, &r.Platform, &r.OrderType, &r.Mid, &r.ToMid, &r.BuyMonths, &r.Money, &r.Status, &r.PayType, &r.RechargeBp, &r.ThirdTradeNo, &r.PaymentTime, &r.Ver, &r.AppSubID, &r.CouponMoney); err != nil {
  79. log.Error("AddPayOrder d.db.exec(%v) error(%v)", r, err)
  80. return
  81. }
  82. if a, err = result.RowsAffected(); err != nil {
  83. log.Error("AddPayOrder result.RowsAffected() error(%v)", err)
  84. return
  85. }
  86. return
  87. }
  88. //SelOrderMaxID sel order maxID
  89. func (d *Dao) SelOrderMaxID(c context.Context) (maxID int, err error) {
  90. row := d.db.QueryRow(c, _selOrderMaxIDSQL)
  91. if err = row.Scan(&maxID); err != nil {
  92. err = errors.WithStack(err)
  93. d.errProm.Incr("db_scan")
  94. return
  95. }
  96. return
  97. }
  98. //SelOldOrderMaxID sel old order maxID
  99. func (d *Dao) SelOldOrderMaxID(c context.Context) (maxID int, err error) {
  100. row := d.oldDb.QueryRow(c, _selOldOrderMaxIDSQL)
  101. if err = row.Scan(&maxID); err != nil {
  102. err = errors.WithStack(err)
  103. d.errProm.Incr("db_scan")
  104. return
  105. }
  106. return
  107. }
  108. //BatchAddPayOrder batch add pay order
  109. func (d *Dao) BatchAddPayOrder(c context.Context, res []*model.VipPayOrder) (err error) {
  110. var values []string
  111. if len(res) == 0 {
  112. return
  113. }
  114. for _, v := range res {
  115. value := fmt.Sprintf("('%v','%v','%v','%v','%v','%v','%v','%v','%v','%v','%v','%v','%v','%v','%v','%v')", v.OrderNo, v.AppID, v.Platform, v.OrderType, v.Mid, v.ToMid, v.BuyMonths, v.Money, v.Status, v.PayType, v.RechargeBp, v.ThirdTradeNo, v.PaymentTime.Time().Format("2006-01-02 15:04:05"), v.Ver, v.AppSubID, v.CouponMoney)
  116. values = append(values, value)
  117. }
  118. valuesStr := strings.Join(values, ",")
  119. dupStr := " ON DUPLICATE KEY UPDATE app_id = VALUES(app_id),platform=VALUES(platform),order_type=VALUES(order_type)," +
  120. "mid=VALUES(mid),to_mid=VALUES(to_mid),buy_months=VALUES(buy_months),money=VALUES(money),status=VALUES(status),pay_type=VALUES(pay_type),recharge_bp=VALUES(recharge_bp)," +
  121. "third_trade_no=VALUES(third_trade_no) ,payment_time=VALUES(payment_time) ,ver=VALUES(ver),app_sub_id=VALUES(app_sub_id),coupon_money=VALUES(coupon_money) "
  122. if _, err = d.db.Exec(c, _batchAddPayOrder+valuesStr+dupStr); err != nil {
  123. err = errors.WithStack(err)
  124. return
  125. }
  126. return
  127. }
  128. //UpdatePayOrderStatus update payorder status
  129. func (d *Dao) UpdatePayOrderStatus(c context.Context, r *model.VipPayOrder) (a int64, err error) {
  130. var result sql.Result
  131. sqlStr := _updatePayOrderStatusSQL
  132. if r.PayType != 0 {
  133. sqlStr += fmt.Sprintf(" pay_type=%v ,payment_time='%v' ,", r.PayType, r.PaymentTime.Time().Format("2006-01-02 15:04:05"))
  134. }
  135. sqlStr += fmt.Sprintf(" ctime='%v',mtime='%v',ver=%v,status=%v,order_type=%v,coupon_money=%v WHERE order_no='%v' ", r.Ctime.Time().Format("2006-01-02 15:04:05"), r.Mtime.Time().Format("2006-01-02 15:04:05"), r.Ver, r.Status, r.OrderType, r.CouponMoney, r.OrderNo)
  136. if result, err = d.db.Exec(c, sqlStr); err != nil {
  137. err = errors.WithStack(err)
  138. return
  139. }
  140. if a, err = result.RowsAffected(); err != nil {
  141. err = errors.WithStack(err)
  142. return
  143. }
  144. return
  145. }
  146. //UpdateRechargeOrder update recharge order info
  147. func (d *Dao) UpdateRechargeOrder(c context.Context, r *model.VipPayOrder) (a int64, err error) {
  148. var result sql.Result
  149. if result, err = d.db.Exec(c, _updateRechageOrderSQL, &r.RechargeBp, &r.ThirdTradeNo, &r.OrderNo); err != nil {
  150. err = errors.WithStack(err)
  151. return
  152. }
  153. if a, err = result.RowsAffected(); err != nil {
  154. err = errors.WithStack(err)
  155. return
  156. }
  157. return
  158. }
  159. //AddPayOrderLog add payorderlog
  160. func (d *Dao) AddPayOrderLog(c context.Context, r *model.VipPayOrderLog) (a int64, err error) {
  161. var result sql.Result
  162. if result, err = d.db.Exec(c, _insertPayOrderLog, &r.OrderNo, &r.Mid, &r.Status); err != nil {
  163. err = errors.WithStack(err)
  164. return
  165. }
  166. if a, err = result.RowsAffected(); err != nil {
  167. err = errors.WithStack(err)
  168. return
  169. }
  170. return
  171. }
  172. //SelPayOrder sel payorder
  173. func (d *Dao) SelPayOrder(c context.Context, sID, eID int) (res []*model.VipPayOrder, err error) {
  174. var rows *xsql.Rows
  175. if rows, err = d.db.Query(c, _selPayOrder, sID, eID); err != nil {
  176. log.Error("SelPayOrder d.db.query(sID:%v,eID:%v) error(%v)", sID, eID, err)
  177. return
  178. }
  179. defer rows.Close()
  180. for rows.Next() {
  181. r := new(model.VipPayOrder)
  182. if err = rows.Scan(&r.OrderNo, &r.AppID, &r.Platform, &r.OrderType, &r.Mid, &r.ToMid, &r.BuyMonths, &r.Money, &r.Status, &r.PayType, &r.ThirdTradeNo, &r.PaymentTime, &r.Ver, &r.AppSubID); err != nil {
  183. log.Error("SelPayOrder rows.scan() error(%v)", err)
  184. res = nil
  185. return
  186. }
  187. res = append(res, r)
  188. }
  189. err = rows.Err()
  190. return
  191. }
  192. //SelOldPayOrder sel old payorder
  193. func (d *Dao) SelOldPayOrder(c context.Context, sID, eID int) (res []*model.VipPayOrderOld, err error) {
  194. var rows *xsql.Rows
  195. if rows, err = d.oldDb.Query(c, _selOldPayOrder, sID, eID); err != nil {
  196. log.Error("SelOldPayOrder d.db.query(sID:%v,eID:%v) error(%v)", sID, eID, err)
  197. return
  198. }
  199. defer rows.Close()
  200. for rows.Next() {
  201. r := new(model.VipPayOrderOld)
  202. if err = rows.Scan(&r.OrderNo, &r.AppID, &r.OrderType, &r.Mid, &r.BuyMonths, &r.Money, &r.PayType, &r.Status, &r.Ver, &r.Platform, &r.PaymentTime, &r.AppSubID, &r.Bmid, &r.CouponMoney); err != nil {
  203. log.Error("SelOldPayOrder rows.scan() error(%v)", err)
  204. res = nil
  205. return
  206. }
  207. res = append(res, r)
  208. }
  209. err = rows.Err()
  210. return
  211. }
  212. //SelOldRechargeOrder sel old rechargeOrder
  213. func (d *Dao) SelOldRechargeOrder(c context.Context, orderNos []string) (res []*model.VipRechargeOrder, err error) {
  214. var inStr = strings.Join(orderNos, "','")
  215. inStr = "('" + inStr + "')"
  216. var rows *xsql.Rows
  217. if rows, err = d.oldDb.Query(c, _selOldRechargeOrder+inStr); err != nil {
  218. log.Error("SelOldRechargeOrder d.db.query(%v) error(%v)", orderNos, err)
  219. return
  220. }
  221. defer rows.Close()
  222. for rows.Next() {
  223. r := new(model.VipRechargeOrder)
  224. if err = rows.Scan(&r.PayOrderNo, &r.ThirdTradeNo, &r.RechargeBp); err != nil {
  225. log.Error("SelOldRechargeOrder rows.scan(%v) error(%v)", orderNos, err)
  226. res = nil
  227. return
  228. }
  229. res = append(res, r)
  230. }
  231. err = rows.Err()
  232. return
  233. }