mysql.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "go-common/app/job/main/passport/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _insertLoginLog = "INSERT INTO aso_login_log%d(`mid`, `timestamp`, `loginip`, `type`, `server`) VALUES %s"
  12. _queryTelBindLog = "SELECT id, mid, tel, timestamp FROM aso_telephone_bind_log where id = ?"
  13. _queryEmailBindLog = "SELECT id, mid, email, timestamp FROM aso_email_bind_log where id = ?"
  14. _batchGetPwdLog = "select id, timestamp, mid, ip, old_pwd, old_salt, new_pwd, new_salt from aso_pwd_log where id < ? order by id desc limit 1000"
  15. _getPwdLog = "select id, timestamp, mid, ip, old_pwd, old_salt, new_pwd, new_salt from aso_pwd_log where id = ?"
  16. )
  17. // AddLoginLog insert service to db.
  18. func (d *Dao) AddLoginLog(vs []*model.LoginLog) (err error) {
  19. if len(vs) == 0 {
  20. return
  21. }
  22. var args = make([]string, 0, len(vs))
  23. for _, v := range vs {
  24. args = append(args, fmt.Sprintf(`(%d,%d,%d,%d,'%s')`, v.Mid, v.Timestamp, v.LoginIP, v.Type, v.Server))
  25. }
  26. if len(args) == 0 {
  27. return
  28. }
  29. s := fmt.Sprintf(_insertLoginLog, vs[0].Mid%10, strings.Join(args, ","))
  30. if _, err = d.logDB.Exec(context.Background(), s); err != nil {
  31. log.Error("d.logDB.Exec(%s) error(%v)", s, err)
  32. }
  33. return
  34. }
  35. // QueryTelBindLog query from id
  36. func (d *Dao) QueryTelBindLog(id int64) (res *model.TelBindLog, err error) {
  37. if id <= 0 {
  38. return
  39. }
  40. res = new(model.TelBindLog)
  41. row := d.asoDB.QueryRow(context.Background(), _queryTelBindLog, id)
  42. if err = row.Scan(&res.ID, &res.Mid, &res.Tel, &res.Timestamp); err != nil {
  43. if err == sql.ErrNoRows {
  44. err = nil
  45. return
  46. }
  47. log.Error("QueryTelBindLog err(%+v)", err)
  48. return
  49. }
  50. return
  51. }
  52. // QueryEmailBindLog query from id
  53. func (d *Dao) QueryEmailBindLog(id int64) (res *model.EmailBindLog, err error) {
  54. if id <= 0 {
  55. return
  56. }
  57. res = new(model.EmailBindLog)
  58. row := d.asoDB.QueryRow(context.Background(), _queryEmailBindLog, id)
  59. if err = row.Scan(&res.ID, &res.Mid, &res.Email, &res.Timestamp); err != nil {
  60. if err == sql.ErrNoRows {
  61. err = nil
  62. return
  63. }
  64. log.Error("QueryEmailBindLog err(%+v)", err)
  65. return
  66. }
  67. return
  68. }
  69. // BatchGetPwdLog batch get pwd log
  70. func (d *Dao) BatchGetPwdLog(c context.Context, id int64) (res []*model.PwdLog, err error) {
  71. var rows *sql.Rows
  72. if rows, err = d.asoDB.Query(c, _batchGetPwdLog, id); err != nil {
  73. log.Error("batch get pwd log, dao.db.Query(%s) error(%v)", _batchGetPwdLog, err)
  74. return
  75. }
  76. defer rows.Close()
  77. for rows.Next() {
  78. pwd := new(model.PwdLog)
  79. if err = rows.Scan(&pwd.ID, &pwd.Timestamp, &pwd.Mid, &pwd.IP, &pwd.OldPwd, &pwd.OldSalt, &pwd.NewPwd, &pwd.NewSalt); err != nil {
  80. log.Error("row.Scan() error(%v)", err)
  81. return
  82. }
  83. res = append(res, pwd)
  84. }
  85. return
  86. }
  87. // GetPwdLog get pwd log
  88. func (d *Dao) GetPwdLog(c context.Context, id int64) (res *model.PwdLog, err error) {
  89. res = new(model.PwdLog)
  90. row := d.asoDB.QueryRow(c, _getPwdLog, id)
  91. if err = row.Scan(&res.ID, &res.Timestamp, &res.Mid, &res.IP, &res.OldPwd, &res.OldSalt, &res.NewPwd, &res.NewSalt); err != nil {
  92. if err == sql.ErrNoRows {
  93. err = nil
  94. return
  95. }
  96. log.Error("row.Scan() error(%v)", err)
  97. return
  98. }
  99. return
  100. }