order.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "fmt"
  6. "go-common/app/service/main/vip/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/xstr"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _vipOrderListSQL = "SELECT id,order_no,app_id,platform,order_type,mid,to_mid,buy_months,money,refund_amount,status,pay_type,recharge_bp,third_trade_no,ver,payment_time,ctime,mtime,app_sub_id FROM vip_pay_order WHERE mid = ? AND status = ? ORDER BY id DESC LIMIT ?,?;"
  13. _vipOrderCountSQL = "SELECT COUNT(1) FROM vip_pay_order WHERE mid = ? AND status = ? ORDER BY id desc;"
  14. _getOrderInfoSQL = "SELECT id,order_no, app_id,platform,order_type,mid,to_mid,buy_months,money,refund_amount,status,pay_type,recharge_bp,third_trade_no,payment_time,ver,ctime,mtime,app_sub_id FROM `vip_pay_order` WHERE `order_no` = ?;"
  15. _discountSQL = "SELECT mid,discount_id,status FROM vip_user_discount_history where mid=? AND discount_id = ? AND status = 1 "
  16. _allPriceMapping = "SELECT id,month_id,month_type,money,selected,first_discount_money,discount_money,start_time,end_time,remark,operator,mtime FROM vip_month_price WHERE month_id = ? AND month_type = ? LIMIT 1;"
  17. _insertPayOrder = "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,app_sub_id,coupon_money,pid,user_ip)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
  18. _allMonthOrderSQL = "SELECT id,month,month_type,operator,status,deleted,mtime FROM vip_month where deleted = 0 AND status = 1 ORDER BY month "
  19. _updateOrderStatusSQL = "UPDATE vip_pay_order SET status = ?,pay_type = ?,third_trade_no = ? WHERE order_no = ?;"
  20. _updatePayOrderSQL = "UPDATE vip_pay_order SET pay_type=?,third_trade_no=?,recharge_bp=?,ver=? WHERE id = ? AND ver=? "
  21. _updateIosPayOrderSQL = "UPDATE vip_pay_order SET pay_type=?,third_trade_no=?,recharge_bp=?,ver=?,order_type=?,status=? WHERE id = ? AND ver=? "
  22. _updatePayOrderStatusSQL = "UPDATE vip_pay_order SET status = ? WHERE id = ? AND ver=?;"
  23. _addOrderLogSQL = "INSERT INTO vip_pay_order_log(order_no,refund_id,refund_amount,mid,status) VALUES(?,?,?,?,?);"
  24. _payOrderLastSQL = "SELECT id,order_no,app_id,platform,order_type,mid,to_mid,buy_months,money,refund_amount,status,pay_type,recharge_bp,third_trade_no,payment_time,ver,ctime,mtime,app_sub_id FROM vip_pay_order WHERE mid = %v AND status = %v AND order_type IN (%v) ORDER BY id DESC LIMIT 1 "
  25. _updatePayOrderRefundAmountSQL = "UPDATE vip_pay_order SET refund_amount=?,ver=? WHERE id=? AND ver=?"
  26. _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,pid,user_ip from vip_pay_order WHERE order_no = ?;"
  27. _selOrderLogSQL = "SELECT order_no,refund_id,mid,status FROM vip_pay_order_log WHERE order_no = ? AND refund_id = ? AND status = ?"
  28. )
  29. // BeginTran begin tran.
  30. func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
  31. tx, err = d.db.Begin(c)
  32. return
  33. }
  34. //OrderCount order count.
  35. func (d *Dao) OrderCount(c context.Context, mid int64, status int8) (count int64, err error) {
  36. var row *sql.Row
  37. if row = d.db.QueryRow(c, _vipOrderCountSQL, mid, status); err != nil {
  38. err = errors.WithStack(err)
  39. d.errProm.Incr("query_db")
  40. return
  41. }
  42. if err = row.Scan(&count); err != nil {
  43. err = errors.WithStack(err)
  44. d.errProm.Incr("row_scan_db")
  45. return
  46. }
  47. return
  48. }
  49. // OrderList order list.
  50. func (d *Dao) OrderList(c context.Context, mid int64, status int8, pn, ps int) (res []*model.PayOrder, err error) {
  51. var rows *sql.Rows
  52. if rows, err = d.db.Query(c, _vipOrderListSQL, mid, status, (pn-1)*ps, ps); err != nil {
  53. err = errors.WithStack(err)
  54. d.errProm.Incr("query_db")
  55. return
  56. }
  57. defer rows.Close()
  58. for rows.Next() {
  59. r := new(model.PayOrder)
  60. 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,
  61. &r.ThirdTradeNo, &r.Ver, &r.PaymentTime, &r.Ctime, &r.Mtime, &r.AppSubID); err != nil {
  62. err = errors.WithStack(err)
  63. d.errProm.Incr("row_scan_db")
  64. res = nil
  65. return
  66. }
  67. res = append(res, r)
  68. }
  69. err = rows.Err()
  70. return
  71. }
  72. //OrderInfo select order by order no.
  73. func (d *Dao) OrderInfo(c context.Context, orderNo string) (r *model.OrderInfo, err error) {
  74. var row = d.db.QueryRow(c, _getOrderInfoSQL, orderNo)
  75. r = new(model.OrderInfo)
  76. 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,
  77. &r.RechargeBP, &r.ThirdTradeNo, &r.PaymentTime, &r.Ver, &r.Ctime, &r.Mtime, &r.AppSubID); err != nil {
  78. if err == sql.ErrNoRows {
  79. r = nil
  80. err = nil
  81. } else {
  82. err = errors.WithStack(err)
  83. d.errProm.Incr("row_scan_db")
  84. }
  85. }
  86. return
  87. }
  88. // DiscountSQL discount sql.
  89. func (d *Dao) DiscountSQL(c context.Context, mid int64, discountID int64) (dh *model.VipUserDiscountHistory, err error) {
  90. row := d.db.QueryRow(c, _discountSQL, mid, discountID)
  91. dh = new(model.VipUserDiscountHistory)
  92. if err = row.Scan(&dh.Mid, &dh.DiscountID, &dh.Status); err != nil {
  93. if err == sql.ErrNoRows {
  94. dh = nil
  95. err = nil
  96. } else {
  97. err = errors.WithStack(err)
  98. d.errProm.Incr("row_scan_db")
  99. }
  100. }
  101. return
  102. }
  103. //PriceMapping all price mapping.
  104. func (d *Dao) PriceMapping(c context.Context, monthID int64, platform int8) (r *model.PriceMapping, err error) {
  105. row := d.db.QueryRow(c, _allPriceMapping, monthID, platform)
  106. r = new(model.PriceMapping)
  107. if err = row.Scan(&r.ID, &r.MonthID, &r.MonthType, &r.Money, &r.Selected, &r.FirstDiscountMoney, &r.DiscountMoney,
  108. &r.StartTime, &r.EndTime, &r.Remark, &r.Operator, &r.Mtime); err != nil {
  109. if err == sql.ErrNoRows {
  110. err = nil
  111. r = nil
  112. } else {
  113. err = errors.WithStack(err)
  114. d.errProm.Incr("row_scan_db")
  115. }
  116. }
  117. return
  118. }
  119. // TxAddOrder tx add order.
  120. func (d *Dao) TxAddOrder(tx *sql.Tx, p *model.PayOrder) (id int64, err error) {
  121. var result xsql.Result
  122. if result, err = tx.Exec(_insertPayOrder, &p.OrderNo, &p.AppID, &p.Platform, &p.OrderType, &p.Mid, &p.ToMid,
  123. &p.BuyMonths, &p.Money, &p.Status, &p.PayType, &p.RechargeBp, &p.ThirdTradeNo, &p.AppSubID, &p.CouponMoney, &p.PID, &p.UserIP); err != nil {
  124. err = errors.WithStack(err)
  125. d.errProm.Incr("exec_db")
  126. return
  127. }
  128. if id, err = result.LastInsertId(); err != nil {
  129. err = errors.WithStack(err)
  130. }
  131. return
  132. }
  133. //AllMonthByOrder order by month.
  134. func (d *Dao) AllMonthByOrder(c context.Context, orderStr string) (res []*model.Month, err error) {
  135. var rows *sql.Rows
  136. if rows, err = d.db.Query(c, _allMonthOrderSQL+orderStr); err != nil {
  137. err = errors.WithStack(err)
  138. d.errProm.Incr("query_db")
  139. return
  140. }
  141. defer rows.Close()
  142. for rows.Next() {
  143. r := new(model.Month)
  144. if err = rows.Scan(&r.ID, &r.Month, &r.MonthType, &r.Operator, &r.Status, &r.Deleted, &r.Mtime); err != nil {
  145. err = errors.WithStack(err)
  146. d.errProm.Incr("row_scan_db")
  147. res = nil
  148. return
  149. }
  150. res = append(res, r)
  151. }
  152. err = rows.Err()
  153. return
  154. }
  155. // TxUpdateOrderStatus update order status.
  156. func (d *Dao) TxUpdateOrderStatus(c context.Context, tx *sql.Tx, status int8, payType string, thirdTradeNO string, orderNO string) (err error) {
  157. if _, err = tx.Exec(_updateOrderStatusSQL, status, payType, thirdTradeNO, orderNO); err != nil {
  158. err = errors.WithStack(err)
  159. d.errProm.Incr("exec_db")
  160. return
  161. }
  162. return
  163. }
  164. //TxUpdatePayOrder .
  165. func (d *Dao) TxUpdatePayOrder(tx *sql.Tx, o *model.OrderInfo, ver int64) (err error) {
  166. if _, err = tx.Exec(_updatePayOrderSQL, o.PayType, o.ThirdTradeNo, o.RechargeBP, o.Ver, o.ID, ver); err != nil {
  167. err = errors.WithStack(err)
  168. return
  169. }
  170. return
  171. }
  172. //TxUpdateIosPayOrder .
  173. func (d *Dao) TxUpdateIosPayOrder(tx *sql.Tx, o *model.OrderInfo, ver int64) (err error) {
  174. if _, err = tx.Exec(_updateIosPayOrderSQL, o.PayType, o.ThirdTradeNo, o.RechargeBP, o.Ver, o.OrderType, o.Status, o.ID, ver); err != nil {
  175. err = errors.WithStack(err)
  176. }
  177. return
  178. }
  179. //TxUpdatePayOrderStatus .
  180. func (d *Dao) TxUpdatePayOrderStatus(tx *sql.Tx, status int8, id int64, ver int64) (a int64, err error) {
  181. var result xsql.Result
  182. if result, err = tx.Exec(_updatePayOrderStatusSQL, status, id, ver); err != nil {
  183. err = errors.WithStack(err)
  184. return
  185. }
  186. if a, err = result.RowsAffected(); err != nil {
  187. err = errors.WithStack(err)
  188. return
  189. }
  190. return
  191. }
  192. //TxAddOrderLog .
  193. func (d *Dao) TxAddOrderLog(tx *sql.Tx, arg *model.VipPayOrderLog) (err error) {
  194. if _, err = tx.Exec(_addOrderLogSQL, arg.OrderNo, arg.RefundID, arg.RefundAmount, arg.Mid, arg.Status); err != nil {
  195. err = errors.WithStack(err)
  196. return
  197. }
  198. return
  199. }
  200. //PayOrderLast .
  201. func (d *Dao) PayOrderLast(c context.Context, mid int64, status int8, orderTypes ...int64) (r *model.PayOrder, err error) {
  202. row := d.db.QueryRow(c, fmt.Sprintf(_payOrderLastSQL, mid, status, xstr.JoinInts(orderTypes)))
  203. r = new(model.PayOrder)
  204. 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,
  205. &r.RechargeBp, &r.ThirdTradeNo, &r.PaymentTime, &r.Ver, &r.Ctime, &r.Mtime, &r.AppSubID); err != nil {
  206. if err == sql.ErrNoRows {
  207. r = nil
  208. err = nil
  209. } else {
  210. err = errors.WithStack(err)
  211. d.errProm.Incr("row_scan_db")
  212. }
  213. }
  214. return
  215. }
  216. //SelOldPayOrder sel old payorder
  217. func (d *Dao) SelOldPayOrder(c context.Context, orderNo string) (r *model.VipPayOrderOld, err error) {
  218. var row = d.olddb.QueryRow(c, _selOldPayOrder, orderNo)
  219. r = new(model.VipPayOrderOld)
  220. if err = row.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.PID, &r.UserIP); err != nil {
  221. if err == sql.ErrNoRows {
  222. r = nil
  223. err = nil
  224. } else {
  225. err = errors.WithStack(err)
  226. d.errProm.Incr("row_scan_db")
  227. }
  228. }
  229. return
  230. }
  231. // SelPayOrderLog sel pay order log.
  232. func (d *Dao) SelPayOrderLog(c context.Context, orderNo, refundID string, status int8) (res *model.VipPayOrderLog, err error) {
  233. row := d.db.QueryRow(c, _selOrderLogSQL, orderNo, refundID, status)
  234. res = new(model.VipPayOrderLog)
  235. if err = row.Scan(&res.OrderNo, &res.RefundID, &res.Mid, &res.Status); err != nil {
  236. if err == sql.ErrNoRows {
  237. res = nil
  238. err = nil
  239. return
  240. }
  241. err = errors.WithStack(err)
  242. d.errProm.Incr("row_scan_db")
  243. }
  244. return
  245. }
  246. //TxUpdatePayOrderRefundAmount update payorder refund amount
  247. func (d *Dao) TxUpdatePayOrderRefundAmount(tx *sql.Tx, id int64, refundAmount float64, ver, oldVer int64) (err error) {
  248. if _, err = tx.Exec(_updatePayOrderRefundAmountSQL, refundAmount, ver, id, oldVer); err != nil {
  249. err = errors.WithStack(err)
  250. d.errProm.Incr("tx_db")
  251. }
  252. return
  253. }