up_account.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package income
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/library/log"
  7. model "go-common/app/job/main/growup/model/income"
  8. )
  9. const (
  10. // select
  11. _upAccountsSQL = "SELECT id,mid,has_sign_contract,state,total_income,total_unwithdraw_income,total_withdraw_income,last_withdraw_time,version,allowance_state,nick_name,withdraw_date_version,is_deleted FROM up_account WHERE id > ? ORDER BY id LIMIT ?"
  12. _upAccountSQL = "SELECT total_income,total_unwithdraw_income,version,withdraw_date_version FROM up_account WHERE mid=?"
  13. // for batch insert
  14. _insertUpAccountSQL = "INSERT INTO up_account(mid,has_sign_contract,total_income,total_unwithdraw_income,withdraw_date_version,version) VALUES %s ON DUPLICATE KEY UPDATE total_income=VALUES(total_income),total_unwithdraw_income=VALUES(total_unwithdraw_income),version=VALUES(version)"
  15. _updateUpAccountSQL = "UPDATE up_account SET total_income=?,total_unwithdraw_income=?,version=? WHERE mid=? AND version=?"
  16. )
  17. // UpAccounts batch read up account
  18. func (d *Dao) UpAccounts(c context.Context, id int64, limit int64) (m map[int64]*model.UpAccount, last int64, err error) {
  19. rows, err := d.db.Query(c, _upAccountsSQL, id, limit)
  20. if err != nil {
  21. log.Error("d.db.UpAccounts error(%v)", err)
  22. return
  23. }
  24. m = make(map[int64]*model.UpAccount)
  25. defer rows.Close()
  26. for rows.Next() {
  27. ua := &model.UpAccount{}
  28. err = rows.Scan(&last, &ua.MID, &ua.HasSignContract, &ua.State, &ua.TotalIncome, &ua.TotalUnwithdrawIncome, &ua.TotalWithdrawIncome, &ua.LastWithdrawTime, &ua.Version, &ua.AllowanceState, &ua.Nickname, &ua.WithdrawDateVersion, &ua.IsDeleted)
  29. if err != nil {
  30. log.Error("rows scan error(%v)", err)
  31. return
  32. }
  33. m[ua.MID] = ua
  34. }
  35. return
  36. }
  37. // UpAccount get up account by mid
  38. func (d *Dao) UpAccount(c context.Context, mid int64) (a *model.UpAccount, err error) {
  39. row := d.db.QueryRow(c, _upAccountSQL, mid)
  40. a = &model.UpAccount{}
  41. if err = row.Scan(&a.TotalIncome, &a.TotalUnwithdrawIncome, &a.Version, &a.WithdrawDateVersion); err != nil {
  42. if err == sql.ErrNoRows {
  43. err = nil
  44. } else {
  45. log.Error("dao.UpAccount error(%v)", err)
  46. }
  47. }
  48. return
  49. }
  50. // InsertUpAccount batch insert up account
  51. func (d *Dao) InsertUpAccount(c context.Context, values string) (rows int64, err error) {
  52. res, err := d.db.Exec(c, fmt.Sprintf(_insertUpAccountSQL, values))
  53. if err != nil {
  54. log.Error("d.db.Exec InsertUpAccount error(%v)", err)
  55. return
  56. }
  57. return res.RowsAffected()
  58. }
  59. // UpdateUpAccount update up account by mid and version instead batch update
  60. func (d *Dao) UpdateUpAccount(c context.Context, mid, ver, totalIncome, totalUnwithdrawIncome int64) (rows int64, err error) {
  61. res, err := d.db.Exec(c, _updateUpAccountSQL, totalIncome, totalUnwithdrawIncome, ver+1, mid, ver)
  62. if err != nil {
  63. log.Error("d.db.Exec UpdateUpAccount error(%v)", err)
  64. return
  65. }
  66. return res.RowsAffected()
  67. }