up_account.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package income
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/admin/main/growup/model/income"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. // count
  11. _upAccountCountSQL = "SELECT count(*) FROM up_account WHERE %s is_deleted = ?"
  12. // select
  13. _upAccountSQL = "SELECT mid,total_income,total_unwithdraw_income,total_withdraw_income,withdraw_date_version,last_withdraw_time,mtime FROM up_account WHERE %s is_deleted = ? LIMIT ?,?"
  14. _upAccountByMIDSQL = "SELECT mid,total_income,total_unwithdraw_income,withdraw_date_version,version FROM up_account WHERE mid = ? AND is_deleted = 0"
  15. // update
  16. _breachUpAccountSQL = "UPDATE up_account SET total_income = ?, total_unwithdraw_income = ?, version = ? WHERE mid = ? AND version = ? AND is_deleted = 0"
  17. )
  18. // UpAccountCount get up_account count
  19. func (d *Dao) UpAccountCount(c context.Context, query string, isDeleted int) (total int64, err error) {
  20. err = d.db.QueryRow(c, fmt.Sprintf(_upAccountCountSQL, query), isDeleted).Scan(&total)
  21. if err == sql.ErrNoRows {
  22. err = nil
  23. }
  24. return
  25. }
  26. // ListUpAccount list up account bu query
  27. func (d *Dao) ListUpAccount(c context.Context, query string, isDeleted, from, limit int) (ups []*model.UpAccount, err error) {
  28. ups = make([]*model.UpAccount, 0)
  29. rows, err := d.db.Query(c, fmt.Sprintf(_upAccountSQL, query), isDeleted, from, limit)
  30. if err != nil {
  31. log.Error("ListUpAccount d.db.Query error(%v)", err)
  32. return
  33. }
  34. defer rows.Close()
  35. for rows.Next() {
  36. up := &model.UpAccount{}
  37. err = rows.Scan(&up.MID, &up.TotalIncome, &up.TotalUnwithdrawIncome, &up.TotalWithdrawIncome, &up.WithdrawDateVersion, &up.LastWithdrawTime, &up.MTime)
  38. if err != nil {
  39. log.Error("ListUpAccount rows scan error(%v)", err)
  40. return
  41. }
  42. ups = append(ups, up)
  43. }
  44. err = rows.Err()
  45. return
  46. }
  47. // GetUpAccount get up_account by mid
  48. func (d *Dao) GetUpAccount(c context.Context, mid int64) (up *model.UpAccount, err error) {
  49. up = &model.UpAccount{}
  50. err = d.db.QueryRow(c, _upAccountByMIDSQL, mid).Scan(&up.MID, &up.TotalIncome, &up.TotalUnwithdrawIncome, &up.WithdrawDateVersion, &up.Version)
  51. return
  52. }
  53. // TxBreachUpAccount breach up_account
  54. func (d *Dao) TxBreachUpAccount(tx *sql.Tx, total, unwithdraw, mid, newVersion, oldVersion int64) (rows int64, err error) {
  55. res, err := tx.Exec(_breachUpAccountSQL, total, unwithdraw, newVersion, mid, oldVersion)
  56. if err != nil {
  57. return
  58. }
  59. return res.RowsAffected()
  60. }