mysql.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/block/model"
  6. xsql "go-common/library/database/sql"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _upsertUser = `INSERT INTO block_user (mid,status) VALUES (?,?) ON DUPLICATE KEY UPDATE status=?`
  11. _userStatusList = `SELECT id,mid FROM block_user WHERE status=? AND id>? LIMIT ?`
  12. _userExtra = `SELECT id,mid,credit_answer_flag,action_time FROM block_extra WHERE mid=? ORDER BY id DESC LIMIT 1`
  13. _userLastHistory = `SELECT id,mid,source,area,action,start_time,duration FROM block_history_%d WHERE mid=? ORDER BY id DESC LIMIT 1`
  14. _upsertAddBlockCount = `INSERT INTO block_user_detail (mid,block_count) VALUES (?,1) ON DUPLICATE KEY UPDATE block_count=block_count+1`
  15. _upsertExtra = `INSERT INTO block_extra (mid,credit_answer_flag,action_time) VALUES (?,?,?) ON DUPLICATE KEY UPDATE credit_answer_flag=? , action_time=?`
  16. _insertHistory = `INSERT INTO block_history_%d (mid,admin_id,admin_name,source,area,reason,comment,action,start_time,duration,notify) VALUES (?,?,?,?,?,?,?,?,?,?,?)`
  17. )
  18. func historyIdx(mid int64) int64 {
  19. return mid % 10
  20. }
  21. func (d *Dao) UserStatusList(c context.Context, status model.BlockStatus, startID int64, limit int) (maxID int64, mids []int64, err error) {
  22. var (
  23. rows *xsql.Rows
  24. )
  25. if rows, err = d.db.Query(c, _userStatusList, status, startID, limit); err != nil {
  26. err = errors.WithStack(err)
  27. return
  28. }
  29. defer rows.Close()
  30. for rows.Next() {
  31. var (
  32. id, mid int64
  33. )
  34. if err = rows.Scan(&id, &mid); err != nil {
  35. err = errors.WithStack(err)
  36. return
  37. }
  38. if maxID < id {
  39. maxID = id
  40. }
  41. mids = append(mids, mid)
  42. }
  43. if err = rows.Err(); err != nil {
  44. err = errors.WithStack(err)
  45. return
  46. }
  47. return
  48. }
  49. func (d *Dao) UserLastHistory(c context.Context, mid int64) (his *model.DBHistory, err error) {
  50. var (
  51. sql = fmt.Sprintf(_userLastHistory, historyIdx(mid))
  52. )
  53. row := d.db.QueryRow(c, sql, mid)
  54. his = &model.DBHistory{}
  55. if err = row.Scan(&his.ID, &his.MID, &his.Source, &his.Area, &his.Action, &his.StartTime, &his.Duration); err != nil {
  56. if err == xsql.ErrNoRows {
  57. err = nil
  58. his = nil
  59. return
  60. }
  61. err = errors.WithStack(err)
  62. return
  63. }
  64. return
  65. }
  66. func (d *Dao) UserExtra(c context.Context, mid int64) (ex *model.DBExtra, err error) {
  67. row := d.db.QueryRow(c, _userExtra, mid)
  68. ex = &model.DBExtra{}
  69. if err = row.Scan(&ex.ID, &ex.MID, &ex.CreditAnswerFlag, &ex.ActionTime); err != nil {
  70. if err == xsql.ErrNoRows {
  71. err = nil
  72. ex = nil
  73. return
  74. }
  75. err = errors.WithStack(err)
  76. return
  77. }
  78. return
  79. }
  80. func (d *Dao) TxUpsertUser(c context.Context, tx *xsql.Tx, mid int64, status model.BlockStatus) (err error) {
  81. if _, err = tx.Exec(_upsertUser, mid, status, status); err != nil {
  82. err = errors.WithStack(err)
  83. return
  84. }
  85. return
  86. }
  87. func (d *Dao) InsertExtra(c context.Context, ex *model.DBExtra) (err error) {
  88. if _, err = d.db.Exec(c, _upsertExtra, ex.MID, ex.CreditAnswerFlag, ex.ActionTime, ex.CreditAnswerFlag, ex.ActionTime); err != nil {
  89. err = errors.WithStack(err)
  90. return
  91. }
  92. return
  93. }
  94. func (d *Dao) TxUpsertExtra(c context.Context, tx *xsql.Tx, ex *model.DBExtra) (err error) {
  95. if _, err = tx.Exec(_upsertExtra, ex.MID, ex.CreditAnswerFlag, ex.ActionTime, ex.CreditAnswerFlag, ex.ActionTime); err != nil {
  96. err = errors.WithStack(err)
  97. return
  98. }
  99. return
  100. }
  101. func (d *Dao) TxInsertHistory(c context.Context, tx *xsql.Tx, h *model.DBHistory) (err error) {
  102. var (
  103. sql = fmt.Sprintf(_insertHistory, historyIdx(h.MID))
  104. )
  105. if _, err = tx.Exec(sql, h.MID, h.AdminID, h.AdminName, h.Source, h.Area, h.Reason, h.Comment, h.Action, h.StartTime, h.Duration, h.Notify); err != nil {
  106. err = errors.WithStack(err)
  107. return
  108. }
  109. return
  110. }
  111. func (d *Dao) UpsertAddBlockCount(c context.Context, mid int64) (err error) {
  112. if _, err = d.db.Exec(c, _upsertAddBlockCount, mid); err != nil {
  113. err = errors.WithStack(err)
  114. return
  115. }
  116. return
  117. }