withdraw.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/interface/main/growup/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. // get up_account withdraw count
  14. _upAccountCount = "SELECT count(1) FROM up_account where is_deleted = 0 AND has_sign_contract = 1 AND total_unwithdraw_income > 0 AND withdraw_date_version != ?"
  15. // query up_account by date
  16. _queryUpAccountByDate = "SELECT mid, total_unwithdraw_income, withdraw_date_version FROM up_account WHERE is_deleted = 0 AND has_sign_contract = 1 AND total_unwithdraw_income > 0 AND withdraw_date_version != ? LIMIT ?,?"
  17. // query up_account version by mid
  18. _queryUpAccountVersion = "SELECT version FROM up_account WHERE is_deleted = 0 AND mid = ?"
  19. // update up_account withdraw
  20. _updateUpAccountWithdraw = "UPDATE up_account up SET up.total_unwithdraw_income = up.total_unwithdraw_income - %d, up.total_withdraw_income = up.total_withdraw_income + %d, up.last_withdraw_time = ? WHERE up.mid = ? AND up.total_unwithdraw_income > 0 AND is_deleted = 0"
  21. // update up_account unwithdraw income
  22. _updateUpAccountUnwithdrawIncome = "UPDATE up_account up SET up.total_unwithdraw_income = up.total_income - up.total_withdraw_income - up.exchange_income, up.withdraw_date_version = ?, up.version = up.version + 1 WHERE up.is_deleted = 0 AND up.mid = ? AND up.version = ?"
  23. // update up_account exchange and unwithdraw income
  24. _updateUpAccountExchangeIncome = "UPDATE up_account SET total_unwithdraw_income = total_unwithdraw_income - %d, exchange_income = exchange_income + %d, version = version + 1 WHERE is_deleted = 0 AND mid = ? AND version = ?"
  25. // query up_income_withdraw by mid
  26. _queryUpWithdrawByMID = "SELECT withdraw_income, date_version, state, ctime FROM up_income_withdraw WHERE mid = ? AND is_deleted = 0"
  27. // query up_income_withdraw by mids and date_version
  28. _queryUpWithdrawByMIDs = "SELECT id, mid, withdraw_income, date_version, state, ctime FROM up_income_withdraw WHERE is_deleted = 0 AND mid in (%s) AND date_version = ?"
  29. // query up_income_withdraw by id
  30. _queryUpWithdrawByID = "SELECT id, mid, withdraw_income, date_version, state, ctime FROM up_income_withdraw WHERE is_deleted = 0 AND id = ?"
  31. // query up_income_withdraw max date_version by mid
  32. _queryMaxUpWithdrawDateVersion = "SELECT MAX(date_version) FROM up_income_withdraw where is_deleted = 0 AND mid = ?"
  33. // insert record into up_income_withdraw
  34. _insertUpWithdrawRecord = "INSERT INTO up_income_withdraw(mid, withdraw_income, date_version, state) VALUES(?,?,?,?)"
  35. // update up_income_withdraw
  36. _updateUpWithdrawState = "UPDATE up_income_withdraw up SET up.state = ? WHERE up.id = ? AND is_deleted = 0"
  37. )
  38. // GetUpAccountCount get up account withdraw count
  39. func (d *Dao) GetUpAccountCount(c context.Context, dateVersion string) (count int, err error) {
  40. row := d.db.QueryRow(c, _upAccountCount, dateVersion)
  41. if err = row.Scan(&count); err != nil {
  42. if err == sql.ErrNoRows {
  43. err = nil
  44. count = 0
  45. } else {
  46. log.Error("db.QueryRow(%s) error(%v)", _upAccountCount, err)
  47. }
  48. }
  49. return
  50. }
  51. // QueryUpAccountByDate query up_account by date
  52. func (d *Dao) QueryUpAccountByDate(c context.Context, dateVersion string, from, limit int) (upAccounts []*model.UpAccount, err error) {
  53. upAccounts = make([]*model.UpAccount, 0)
  54. rows, err := d.db.Query(c, _queryUpAccountByDate, dateVersion, from, limit)
  55. if err != nil {
  56. log.Error("d.db.Query(%s) error(%v)", _queryUpAccountByDate, err)
  57. return
  58. }
  59. defer rows.Close()
  60. for rows.Next() {
  61. up := &model.UpAccount{}
  62. err = rows.Scan(&up.MID, &up.TotalUnwithdrawIncome, &up.WithdrawDateVersion)
  63. if err != nil {
  64. log.Error("rows.Scan error(%v)", err)
  65. return
  66. }
  67. upAccounts = append(upAccounts, up)
  68. }
  69. err = rows.Err()
  70. return
  71. }
  72. // QueryUpWithdrawByMID query up_income_withdraw by mid
  73. func (d *Dao) QueryUpWithdrawByMID(c context.Context, mid int64) (upWithdraws []*model.UpIncomeWithdraw, err error) {
  74. upWithdraws = make([]*model.UpIncomeWithdraw, 0)
  75. rows, err := d.db.Query(c, _queryUpWithdrawByMID, mid)
  76. if err != nil {
  77. log.Error("d.db.Query(%s) error(%v)", _queryUpWithdrawByMID, err)
  78. return
  79. }
  80. defer rows.Close()
  81. for rows.Next() {
  82. upWithdraw := &model.UpIncomeWithdraw{}
  83. err = rows.Scan(&upWithdraw.WithdrawIncome, &upWithdraw.DateVersion, &upWithdraw.State, &upWithdraw.CTime)
  84. if err != nil {
  85. log.Error("rows.Scan error(%v)", err)
  86. return
  87. }
  88. upWithdraws = append(upWithdraws, upWithdraw)
  89. }
  90. err = rows.Err()
  91. return
  92. }
  93. // QueryUpWithdrawByMids query up_income_withdraw by mids
  94. func (d *Dao) QueryUpWithdrawByMids(c context.Context, mids []int64, dateVersion string) (upWithdraws map[int64]*model.UpIncomeWithdraw, err error) {
  95. upWithdraws = make(map[int64]*model.UpIncomeWithdraw)
  96. rows, err := d.db.Query(c, fmt.Sprintf(_queryUpWithdrawByMIDs, xstr.JoinInts(mids)), dateVersion)
  97. if err != nil {
  98. log.Error("d.db.Query(%s) error(%v)", _queryUpWithdrawByMIDs, err)
  99. return
  100. }
  101. defer rows.Close()
  102. for rows.Next() {
  103. upWithdraw := &model.UpIncomeWithdraw{}
  104. err = rows.Scan(&upWithdraw.ID, &upWithdraw.MID, &upWithdraw.WithdrawIncome, &upWithdraw.DateVersion, &upWithdraw.State, &upWithdraw.CTime)
  105. if err != nil {
  106. log.Error("rows.Scan error(%v)", err)
  107. return
  108. }
  109. upWithdraws[upWithdraw.MID] = upWithdraw
  110. }
  111. err = rows.Err()
  112. return
  113. }
  114. // InsertUpWithdrawRecord insert record into up_income_withdraw
  115. func (d *Dao) InsertUpWithdrawRecord(c context.Context, upWithdraw *model.UpIncomeWithdraw) (result int64, err error) {
  116. res, err := d.db.Exec(c, _insertUpWithdrawRecord, upWithdraw.MID, upWithdraw.WithdrawIncome, upWithdraw.DateVersion, upWithdraw.State)
  117. if err != nil {
  118. log.Error("d.db.Exec(%s) error(%v)", _insertUpWithdrawRecord, err)
  119. return
  120. }
  121. return res.RowsAffected()
  122. }
  123. // QueryUpWithdrawByID get up_income_withdraw by id
  124. func (d *Dao) QueryUpWithdrawByID(c context.Context, id int64) (upWithdraw *model.UpIncomeWithdraw, err error) {
  125. upWithdraw = &model.UpIncomeWithdraw{}
  126. row := d.db.QueryRow(c, _queryUpWithdrawByID, id)
  127. err = row.Scan(&upWithdraw.ID, &upWithdraw.MID, &upWithdraw.WithdrawIncome, &upWithdraw.DateVersion, &upWithdraw.State, &upWithdraw.CTime)
  128. return
  129. }
  130. // TxUpdateUpWithdrawState update up_income_withdraw state
  131. func (d *Dao) TxUpdateUpWithdrawState(tx *xsql.Tx, id int64, state int) (result int64, err error) {
  132. res, err := tx.Exec(_updateUpWithdrawState, state, id)
  133. if err != nil {
  134. log.Error("d.db.Exec(%s) error(%v)", _updateUpWithdrawState)
  135. return
  136. }
  137. return res.RowsAffected()
  138. }
  139. // TxUpdateUpAccountWithdraw update up_account withdraw
  140. func (d *Dao) TxUpdateUpAccountWithdraw(tx *xsql.Tx, mid, thirdCoin int64) (result int64, err error) {
  141. res, err := tx.Exec(fmt.Sprintf(_updateUpAccountWithdraw, thirdCoin, thirdCoin), time.Now(), mid)
  142. if err != nil {
  143. log.Error("d.db.Exec(%s) error(%v)", _updateUpAccountWithdraw)
  144. return
  145. }
  146. return res.RowsAffected()
  147. }
  148. // TxQueryMaxUpWithdrawDateVersion query max date_version from up_income_withdraw by mid
  149. func (d *Dao) TxQueryMaxUpWithdrawDateVersion(tx *xsql.Tx, mid int64) (dateVersion string, err error) {
  150. row := tx.QueryRow(_queryMaxUpWithdrawDateVersion, mid)
  151. if err = row.Scan(&dateVersion); err != nil {
  152. if err == sql.ErrNoRows {
  153. err = nil
  154. dateVersion = ""
  155. } else {
  156. log.Error("db.QueryRow(%s) error(%v)", _queryMaxUpWithdrawDateVersion, err)
  157. }
  158. }
  159. return
  160. }
  161. // TxQueryUpAccountVersion query up_account version
  162. func (d *Dao) TxQueryUpAccountVersion(tx *xsql.Tx, mid int64) (version int64, err error) {
  163. row := tx.QueryRow(_queryUpAccountVersion, mid)
  164. err = row.Scan(&version)
  165. return
  166. }
  167. // TxUpdateUpAccountUnwithdrawIncome update up_account unwithdraw and version
  168. func (d *Dao) TxUpdateUpAccountUnwithdrawIncome(tx *xsql.Tx, mid int64, dateVersion string, version int64) (result int64, err error) {
  169. res, err := tx.Exec(_updateUpAccountUnwithdrawIncome, dateVersion, mid, version)
  170. if err != nil {
  171. log.Error("d.db.Exec(%s) error(%v)", _updateUpAccountUnwithdrawIncome)
  172. return
  173. }
  174. return res.RowsAffected()
  175. }
  176. // TxUpdateUpAccountExchangeIncome update up_account unwithdraw and exchange_income
  177. func (d *Dao) TxUpdateUpAccountExchangeIncome(tx *xsql.Tx, mid, income, version int64) (result int64, err error) {
  178. res, err := tx.Exec(fmt.Sprintf(_updateUpAccountExchangeIncome, income, income), mid, version)
  179. if err != nil {
  180. log.Error("d.db.Exec(%s) error(%v)", _updateUpAccountExchangeIncome, err)
  181. return
  182. }
  183. return res.RowsAffected()
  184. }