mysql.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package block
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/job/main/member/model/block"
  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. // UserStatusList is.
  22. func (d *Dao) UserStatusList(c context.Context, status model.BlockStatus, startID int64, limit int) (maxID int64, mids []int64, err error) {
  23. var (
  24. rows *xsql.Rows
  25. )
  26. if rows, err = d.db.Query(c, _userStatusList, status, startID, limit); err != nil {
  27. err = errors.WithStack(err)
  28. return
  29. }
  30. defer rows.Close()
  31. for rows.Next() {
  32. var (
  33. id, mid int64
  34. )
  35. if err = rows.Scan(&id, &mid); err != nil {
  36. err = errors.WithStack(err)
  37. return
  38. }
  39. if maxID < id {
  40. maxID = id
  41. }
  42. mids = append(mids, mid)
  43. }
  44. if err = rows.Err(); err != nil {
  45. err = errors.WithStack(err)
  46. return
  47. }
  48. return
  49. }
  50. // UserLastHistory is.
  51. func (d *Dao) UserLastHistory(c context.Context, mid int64) (his *model.DBHistory, err error) {
  52. var (
  53. sql = fmt.Sprintf(_userLastHistory, historyIdx(mid))
  54. )
  55. row := d.db.QueryRow(c, sql, mid)
  56. his = &model.DBHistory{}
  57. if err = row.Scan(&his.ID, &his.MID, &his.Source, &his.Area, &his.Action, &his.StartTime, &his.Duration); err != nil {
  58. if err == xsql.ErrNoRows {
  59. err = nil
  60. his = nil
  61. return
  62. }
  63. err = errors.WithStack(err)
  64. return
  65. }
  66. return
  67. }
  68. // UserExtra is.
  69. func (d *Dao) UserExtra(c context.Context, mid int64) (ex *model.DBExtra, err error) {
  70. row := d.db.QueryRow(c, _userExtra, mid)
  71. ex = &model.DBExtra{}
  72. if err = row.Scan(&ex.ID, &ex.MID, &ex.CreditAnswerFlag, &ex.ActionTime); err != nil {
  73. if err == xsql.ErrNoRows {
  74. err = nil
  75. ex = nil
  76. return
  77. }
  78. err = errors.WithStack(err)
  79. return
  80. }
  81. return
  82. }
  83. // TxUpsertUser is.
  84. func (d *Dao) TxUpsertUser(c context.Context, tx *xsql.Tx, mid int64, status model.BlockStatus) (count int64, err error) {
  85. rows, err := tx.Exec(_upsertUser, mid, status, status)
  86. if err != nil {
  87. err = errors.WithStack(err)
  88. return
  89. }
  90. return rows.RowsAffected()
  91. }
  92. // InsertExtra is.
  93. func (d *Dao) InsertExtra(c context.Context, ex *model.DBExtra) (err error) {
  94. if _, err = d.db.Exec(c, _upsertExtra, ex.MID, ex.CreditAnswerFlag, ex.ActionTime, ex.CreditAnswerFlag, ex.ActionTime); err != nil {
  95. err = errors.WithStack(err)
  96. return
  97. }
  98. return
  99. }
  100. // TxUpsertExtra is.
  101. func (d *Dao) TxUpsertExtra(c context.Context, tx *xsql.Tx, ex *model.DBExtra) (err error) {
  102. if _, err = tx.Exec(_upsertExtra, ex.MID, ex.CreditAnswerFlag, ex.ActionTime, ex.CreditAnswerFlag, ex.ActionTime); err != nil {
  103. err = errors.WithStack(err)
  104. return
  105. }
  106. return
  107. }
  108. //TxInsertHistory is.
  109. func (d *Dao) TxInsertHistory(c context.Context, tx *xsql.Tx, h *model.DBHistory) (err error) {
  110. var (
  111. sql = fmt.Sprintf(_insertHistory, historyIdx(h.MID))
  112. )
  113. 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 {
  114. err = errors.WithStack(err)
  115. return
  116. }
  117. return
  118. }
  119. //UpsertAddBlockCount is.
  120. func (d *Dao) UpsertAddBlockCount(c context.Context, mid int64) (err error) {
  121. if _, err = d.db.Exec(c, _upsertAddBlockCount, mid); err != nil {
  122. err = errors.WithStack(err)
  123. return
  124. }
  125. return
  126. }