mysql.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/service/main/ugcpay/model"
  6. xsql "go-common/library/database/sql"
  7. )
  8. var (
  9. _selectOrderUser = `SELECT id,order_id,mid,biz,platform,oid,otype,fee,real_fee,currency,pay_id,pay_reason,pay_time,state,ctime,mtime,refund_time,version FROM order_user WHERE order_id=? LIMIT 1`
  10. _insertOrderUser = "INSERT INTO order_user (order_id,mid,biz,platform,oid,otype,fee,real_fee,currency,pay_id,pay_reason,pay_time,state,refund_time,version) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
  11. _updateOrderUser = "UPDATE order_user SET order_id=?,mid=?,biz=?,platform=?,oid=?,otype=?,fee=?,real_fee=?,currency=?,pay_id=?,pay_reason=?,pay_time=?,state=?,refund_time=?,version=version+1 WHERE order_id=? AND version=?"
  12. _insertLogOrderUser = "INSERT INTO log_order_user (order_id,from_state,to_state,`desc`) VALUES (?,?,?,?)"
  13. _selectAsset = "SELECT id,mid,oid,otype,currency,price,state,ctime,mtime FROM asset WHERE oid=? AND otype=? AND currency=? LIMIT 1"
  14. _upsertAsset = "INSERT INTO asset (mid,oid,otype,currency,price,state) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE mid=?,price=?,state=?"
  15. _selectAssetRelation = "SELECT id,oid,otype,mid,state,ctime,mtime FROM asset_relation WHERE oid=? AND otype=? AND mid=? LIMIT 1"
  16. _upsertAssetRelation = "INSERT INTO asset_relation (oid,otype,mid,state) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE state=?"
  17. _selectBillUserDaily = "SELECT id,mid,biz,currency,`in`,`out`,ver,ctime,mtime,version FROM bill_user_daily WHERE mid=? AND biz=? AND currency=? AND ver=? LIMIT 1"
  18. _selectBillUserDailyListByMonthVer = "SELECT id,mid,biz,currency,`in`,`out`,ver,ctime,mtime,version FROM bill_user_daily WHERE mid=? AND biz=? AND currency=? AND month_ver=?"
  19. _selectAggrIncomeUser = "SELECT id,mid,currency,pay_success,pay_error,total_in,total_out,ctime,mtime FROM aggr_income_user WHERE mid=? AND currency=? LIMIT 1"
  20. _selectAggrIncomeAssetList = "SELECT id,mid,currency,ver,oid,otype,pay_success,pay_error,total_in,total_out,ctime,mtime FROM aggr_income_user_asset WHERE mid=? AND currency=? AND ver=? ORDER BY oid DESC LIMIT ?"
  21. _selectAggrIncomeAsset = "SELECT id,mid,currency,ver,oid,otype,pay_success,pay_error,total_in,total_out,ctime,mtime FROM aggr_income_user_asset WHERE mid=? AND currency=? AND ver=? AND oid=? AND otype=? LIMIT 1"
  22. _orderRechargeShell = "SELECT id,mid,order_id,biz,amount,pay_msg,state,`ver`,ctime,mtime FROM order_recharge_shell WHERE order_id=?"
  23. _updateOrderRechargeShell = "UPDATE order_recharge_shell SET mid=?,order_id=?,biz=?,amount=?,pay_msg=?,state=?,`ver`=? WHERE order_id=?"
  24. _insertOrderRechargeShellLog = "INSERT INTO log_order_recharge_shell (order_id,from_state,to_state,`desc`,bill_user_monthly_id) VALUES (?,?,?,?,?)"
  25. )
  26. // BeginTran begin transcation.
  27. func (d *Dao) BeginTran(c context.Context) (tx *xsql.Tx, err error) {
  28. return d.db.Begin(c)
  29. }
  30. // RawOrderUser get user order
  31. func (d *Dao) RawOrderUser(ctx context.Context, id string) (data *model.Order, err error) {
  32. data = &model.Order{}
  33. row := d.db.Master().QueryRow(ctx, _selectOrderUser, id)
  34. if err = row.Scan(&data.ID, &data.OrderID, &data.MID, &data.Biz, &data.Platform, &data.OID, &data.OType, &data.Fee, &data.RealFee, &data.Currency, &data.PayID, &data.PayReason, &data.PayTime, &data.State, &data.CTime, &data.MTime, &data.RefundTime, &data.Version); err != nil {
  35. if err == xsql.ErrNoRows {
  36. err = nil
  37. data = nil
  38. return
  39. }
  40. return
  41. }
  42. return
  43. }
  44. // InsertOrderUser is.
  45. func (d *Dao) InsertOrderUser(ctx context.Context, data *model.Order) (id int64, err error) {
  46. var (
  47. res sql.Result
  48. )
  49. if res, err = d.db.Exec(ctx, _insertOrderUser, data.OrderID, data.MID, data.Biz, data.Platform, data.OID, data.OType, data.Fee, data.RealFee, data.Currency, data.PayID, data.PayReason, data.PayTime, data.State, data.RefundTime, data.Version); err != nil {
  50. return
  51. }
  52. if id, err = res.LastInsertId(); err != nil {
  53. return
  54. }
  55. return
  56. }
  57. // TXUpdateOrderUser .
  58. func (d *Dao) TXUpdateOrderUser(ctx context.Context, tx *xsql.Tx, data *model.Order) (affected int64, err error) {
  59. res, err := tx.Exec(_updateOrderUser, data.OrderID, data.MID, data.Biz, data.Platform, data.OID, data.OType, data.Fee, data.RealFee, data.Currency, data.PayID, data.PayReason, data.PayTime, data.State, data.RefundTime, data.OrderID, data.Version)
  60. if err != nil {
  61. return
  62. }
  63. affected, err = res.RowsAffected()
  64. return
  65. }
  66. // TXInsertOrderUserLog .
  67. func (d *Dao) TXInsertOrderUserLog(ctx context.Context, tx *xsql.Tx, data *model.LogOrder) (id int64, err error) {
  68. var (
  69. res sql.Result
  70. )
  71. if res, err = tx.Exec(_insertLogOrderUser, data.OrderID, data.FromState, data.ToState, data.Desc); err != nil {
  72. return
  73. }
  74. if id, err = res.LastInsertId(); err != nil {
  75. return
  76. }
  77. return
  78. }
  79. // RawAsset is
  80. func (d *Dao) RawAsset(ctx context.Context, oid int64, otype string, currency string) (data *model.Asset, err error) {
  81. data = &model.Asset{}
  82. row := d.db.Master().QueryRow(ctx, _selectAsset, oid, otype, currency)
  83. if err = row.Scan(&data.ID, &data.MID, &data.OID, &data.OType, &data.Currency, &data.Price, &data.State, &data.CTime, &data.MTime); err != nil {
  84. if err == xsql.ErrNoRows {
  85. err = nil
  86. data = nil
  87. return
  88. }
  89. return
  90. }
  91. return
  92. }
  93. // UpsertAsset is
  94. func (d *Dao) UpsertAsset(ctx context.Context, data *model.Asset) (err error) {
  95. if _, err = d.db.Exec(ctx, _upsertAsset, data.MID, data.OID, data.OType, data.Currency, data.Price, data.State, data.MID, data.Price, data.State); err != nil {
  96. return
  97. }
  98. return
  99. }
  100. // RawAssetRelation is
  101. func (d *Dao) RawAssetRelation(ctx context.Context, mid int64, oid int64, otype string) (data *model.AssetRelation, err error) {
  102. data = &model.AssetRelation{}
  103. row := d.db.Master().QueryRow(ctx, _selectAssetRelation, oid, otype, mid)
  104. if err = row.Scan(&data.ID, &data.OID, &data.OType, &data.MID, &data.State, &data.CTime, &data.MTime); err != nil {
  105. if err == xsql.ErrNoRows {
  106. err = nil
  107. data = nil
  108. return
  109. }
  110. return
  111. }
  112. return
  113. }
  114. // UpsertAssetRelation is
  115. func (d *Dao) UpsertAssetRelation(ctx context.Context, data *model.AssetRelation) (rows int64, err error) {
  116. var (
  117. result sql.Result
  118. )
  119. if result, err = d.db.Exec(ctx, _upsertAssetRelation, data.OID, data.OType, data.MID, data.State, data.State); err != nil {
  120. return
  121. }
  122. if rows, err = result.RowsAffected(); err != nil {
  123. return
  124. }
  125. return
  126. }
  127. // TXUpsertAssetRelation is
  128. func (d *Dao) TXUpsertAssetRelation(ctx context.Context, tx *xsql.Tx, data *model.AssetRelation) (rows int64, err error) {
  129. var (
  130. result sql.Result
  131. )
  132. if result, err = tx.Exec(_upsertAssetRelation, data.OID, data.OType, data.MID, data.State, data.State); err != nil {
  133. return
  134. }
  135. if rows, err = result.RowsAffected(); err != nil {
  136. return
  137. }
  138. return
  139. }
  140. // RawAggrIncomeUser is.
  141. func (d *Dao) RawAggrIncomeUser(ctx context.Context, mid int64, currency string) (data *model.AggrIncomeUser, err error) {
  142. data = &model.AggrIncomeUser{}
  143. row := d.db.QueryRow(ctx, _selectAggrIncomeUser, mid, currency)
  144. if err = row.Scan(&data.ID, &data.MID, &data.Currency, &data.PaySuccess, &data.PayError, &data.TotalIn, &data.TotalOut, &data.CTime, &data.MTime); err != nil {
  145. if err == xsql.ErrNoRows {
  146. err = nil
  147. data = nil
  148. return
  149. }
  150. return
  151. }
  152. return
  153. }
  154. // RawAggrIncomeUserAssetList is.
  155. func (d *Dao) RawAggrIncomeUserAssetList(ctx context.Context, mid int64, currency string, ver int64, limit int) (data []*model.AggrIncomeUserAsset, err error) {
  156. var (
  157. rows *xsql.Rows
  158. )
  159. if rows, err = d.db.Query(ctx, _selectAggrIncomeAssetList, mid, currency, ver, limit); err != nil {
  160. return
  161. }
  162. defer rows.Close()
  163. for rows.Next() {
  164. var (
  165. d = &model.AggrIncomeUserAsset{}
  166. )
  167. if err = rows.Scan(&d.ID, &d.MID, &d.Currency, &d.Ver, &d.OID, &d.OType, &d.PaySuccess, &d.PayError, &d.TotalIn, &d.TotalOut, &d.CTime, &d.MTime); err != nil {
  168. return
  169. }
  170. data = append(data, d)
  171. }
  172. if err = rows.Err(); err != nil {
  173. return
  174. }
  175. return
  176. }
  177. // RawAggrIncomeUserAsset .
  178. func (d *Dao) RawAggrIncomeUserAsset(ctx context.Context, mid int64, currency string, oid int64, otype string, ver int64) (data *model.AggrIncomeUserAsset, err error) {
  179. data = &model.AggrIncomeUserAsset{}
  180. row := d.db.QueryRow(ctx, _selectAggrIncomeAsset, mid, currency, ver, oid, otype)
  181. if err = row.Scan(&data.ID, &data.MID, &data.Currency, &data.Ver, &data.OID, &data.OType, &data.PaySuccess, &data.PayError, &data.TotalIn, &data.TotalOut, &data.CTime, &data.MTime); err != nil {
  182. if err == xsql.ErrNoRows {
  183. err = nil
  184. data = nil
  185. return
  186. }
  187. return
  188. }
  189. return
  190. }
  191. // RawBillUserDaily is.
  192. func (d *Dao) RawBillUserDaily(ctx context.Context, mid int64, biz string, currency string, ver int64) (data *model.Bill, err error) {
  193. data = &model.Bill{}
  194. row := d.db.QueryRow(ctx, _selectBillUserDaily, mid, biz, currency, ver)
  195. if err = row.Scan(&data.ID, &data.MID, &data.Biz, &data.Currency, &data.In, &data.Out, &data.Ver, &data.CTime, &data.MTime, &data.Version); err != nil {
  196. if err == xsql.ErrNoRows {
  197. err = nil
  198. data = nil
  199. return
  200. }
  201. return
  202. }
  203. return
  204. }
  205. // RawBillUserDailyByMonthVer .
  206. func (d *Dao) RawBillUserDailyByMonthVer(ctx context.Context, mid int64, biz string, currency string, monthVer int64) (datas []*model.Bill, err error) {
  207. rows, err := d.db.Query(ctx, _selectBillUserDailyListByMonthVer, mid, biz, currency, monthVer)
  208. if err != nil {
  209. return
  210. }
  211. defer rows.Close()
  212. for rows.Next() {
  213. data := &model.Bill{}
  214. if err = rows.Scan(&data.ID, &data.MID, &data.Biz, &data.Currency, &data.In, &data.Out, &data.Ver, &data.CTime, &data.MTime, &data.Version); err != nil {
  215. return
  216. }
  217. datas = append(datas, data)
  218. }
  219. err = rows.Err()
  220. return
  221. }
  222. // RawAccountUser is.
  223. // func (d *Dao) RawAccountUser(ctx context.Context, mid int64, biz string, currency string) (data *model.AccountUser, err error) {
  224. // data = &model.AccountUser{}
  225. // row := d.db.QueryRow(ctx, _selectAccountUser, mid, biz, currency)
  226. // if err = row.Scan(&data.ID, &data.Biz, &data.MID, &data.Currency, &data.Balance, &data.Ver, &data.State, &data.CTime, &data.MTime); err != nil {
  227. // if err == xsql.ErrNoRows {
  228. // err = nil
  229. // data = nil
  230. // return
  231. // }
  232. // return
  233. // }
  234. // return
  235. // }
  236. // RawOrderRechargeShell .
  237. func (d *Dao) RawOrderRechargeShell(ctx context.Context, orderID string) (data *model.OrderRechargeShell, err error) {
  238. data = &model.OrderRechargeShell{}
  239. row := d.db.QueryRow(ctx, _orderRechargeShell, orderID)
  240. if err = row.Scan(&data.ID, &data.MID, &data.OrderID, &data.Biz, &data.Amount, &data.PayMSG, &data.State, &data.Ver, &data.CTime, &data.MTime); err != nil {
  241. if err == xsql.ErrNoRows {
  242. err = nil
  243. data = nil
  244. return
  245. }
  246. return
  247. }
  248. return
  249. }
  250. // TXUpdateOrderRechargeShell .
  251. func (d *Dao) TXUpdateOrderRechargeShell(ctx context.Context, tx *xsql.Tx, data *model.OrderRechargeShell) (err error) {
  252. if _, err = tx.Exec(_updateOrderRechargeShell, data.MID, data.OrderID, data.Biz, data.Amount, data.PayMSG, data.State, data.Ver, data.OrderID); err != nil {
  253. return
  254. }
  255. return
  256. }
  257. // TXInsertOrderRechargeShellLog .
  258. func (d *Dao) TXInsertOrderRechargeShellLog(ctx context.Context, tx *xsql.Tx, order *model.OrderRechargeShellLog) (id int64, err error) {
  259. result, err := tx.Exec(_insertOrderRechargeShellLog, order.OrderID, order.FromState, order.ToState, order.Desc, order.BillUserMonthlyID)
  260. if err != nil {
  261. return
  262. }
  263. id, err = result.LastInsertId()
  264. return
  265. }