mysql.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "go-common/app/admin/main/block/model"
  7. xsql "go-common/library/database/sql"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _user = `SELECT id,mid,status,ctime,mtime FROM block_user WHERE mid=? LIMIT 1`
  12. _users = `SELECT id,mid,status,ctime,mtime FROM block_user WHERE mid IN (%s)`
  13. _upsertUser = `INSERT INTO block_user (mid,status) VALUES (?,?) ON DUPLICATE KEY UPDATE status=?`
  14. _userDetails = `SELECT id,mid,block_count,ctime,mtime FROM block_user_detail WHERE mid IN (%s)`
  15. _addAddBlockCount = `INSERT INTO block_user_detail (mid,block_count) VALUES (?,1) ON DUPLICATE KEY UPDATE block_count=block_count+1`
  16. _history = `SELECT id,mid,admin_id,admin_name,source,area,reason,comment,action,start_time,duration,notify,ctime,mtime FROM block_history_%d WHERE mid=? LIMIT ?,?`
  17. _historyCount = `SELECT count(*) FROM block_history_%d WHERE mid=? LIMIT 1`
  18. _insertHistory = `INSERT INTO block_history_%d (mid,admin_id,admin_name,source,area,reason,comment,action,start_time,duration,notify) VALUES (?,?,?,?,?,?,?,?,?,?,?)`
  19. )
  20. func historyIdx(mid int64) int64 {
  21. return mid % 10
  22. }
  23. // User .
  24. func (d *Dao) User(c context.Context, mid int64) (user *model.DBUser, err error) {
  25. user = &model.DBUser{}
  26. row := d.db.QueryRow(c, _user, mid)
  27. if err = row.Scan(&user.ID, &user.MID, &user.Status, &user.CTime, &user.MTime); err != nil {
  28. err = nil
  29. user = nil
  30. return
  31. }
  32. return
  33. }
  34. // Users .
  35. func (d *Dao) Users(c context.Context, mids []int64) (users []*model.DBUser, err error) {
  36. if len(mids) == 0 {
  37. return
  38. }
  39. var (
  40. sql = fmt.Sprintf(_users, strings.Join(intsToStrs(mids), ","))
  41. rows *xsql.Rows
  42. )
  43. if rows, err = d.db.Query(c, sql); err != nil {
  44. err = errors.WithStack(err)
  45. return
  46. }
  47. defer rows.Close()
  48. for rows.Next() {
  49. user := &model.DBUser{}
  50. if err = rows.Scan(&user.ID, &user.MID, &user.Status, &user.CTime, &user.MTime); err != nil {
  51. err = errors.WithStack(err)
  52. return
  53. }
  54. users = append(users, user)
  55. }
  56. if err = rows.Err(); err != nil {
  57. err = errors.WithStack(err)
  58. }
  59. return
  60. }
  61. // TxUpdateUser .
  62. func (d *Dao) TxUpdateUser(c context.Context, tx *xsql.Tx, mid int64, status model.BlockStatus) (err error) {
  63. if _, err = tx.Exec(_upsertUser, mid, status, status); err != nil {
  64. err = errors.WithStack(err)
  65. return
  66. }
  67. return
  68. }
  69. // UserDetails .
  70. func (d *Dao) UserDetails(c context.Context, mids []int64) (users []*model.DBUserDetail, err error) {
  71. if len(mids) == 0 {
  72. return
  73. }
  74. var (
  75. sql = fmt.Sprintf(_userDetails, strings.Join(intsToStrs(mids), ","))
  76. rows *xsql.Rows
  77. )
  78. if rows, err = d.db.Query(c, sql); err != nil {
  79. err = errors.WithStack(err)
  80. return
  81. }
  82. defer rows.Close()
  83. for rows.Next() {
  84. user := &model.DBUserDetail{}
  85. if err = rows.Scan(&user.ID, &user.MID, &user.BlockCount, &user.CTime, &user.MTime); err != nil {
  86. err = errors.WithStack(err)
  87. return
  88. }
  89. users = append(users, user)
  90. }
  91. if err = rows.Err(); err != nil {
  92. err = errors.WithStack(err)
  93. }
  94. return
  95. }
  96. // UpdateAddBlockCount .
  97. func (d *Dao) UpdateAddBlockCount(c context.Context, mid int64) (err error) {
  98. if _, err = d.db.Exec(c, _addAddBlockCount, mid); err != nil {
  99. err = errors.WithStack(err)
  100. return
  101. }
  102. return
  103. }
  104. // History 获得mid历史封禁记录
  105. func (d *Dao) History(c context.Context, mid int64, start, limit int) (history []*model.DBHistory, err error) {
  106. var (
  107. rows *xsql.Rows
  108. sql = fmt.Sprintf(_history, historyIdx(mid))
  109. )
  110. if rows, err = d.db.Query(c, sql, mid, start, limit); err != nil {
  111. err = errors.WithStack(err)
  112. return
  113. }
  114. defer rows.Close()
  115. for rows.Next() {
  116. h := &model.DBHistory{}
  117. if err = rows.Scan(&h.ID, &h.MID, &h.AdminID, &h.AdminName, &h.Source, &h.Area, &h.Reason, &h.Comment, &h.Action, &h.StartTime, &h.Duration, &h.Notify, &h.CTime, &h.MTime); err != nil {
  118. err = errors.WithStack(err)
  119. return
  120. }
  121. history = append(history, h)
  122. }
  123. if err = rows.Err(); err != nil {
  124. return
  125. }
  126. return
  127. }
  128. // HistoryCount 获得历史记录总长度
  129. func (d *Dao) HistoryCount(c context.Context, mid int64) (total int, err error) {
  130. var (
  131. row *xsql.Row
  132. sql = fmt.Sprintf(_historyCount, historyIdx(mid))
  133. )
  134. row = d.db.QueryRow(c, sql, mid)
  135. if err = row.Scan(&total); err != nil {
  136. err = errors.WithStack(err)
  137. return
  138. }
  139. return
  140. }
  141. // TxInsertHistory .
  142. func (d *Dao) TxInsertHistory(c context.Context, tx *xsql.Tx, h *model.DBHistory) (err error) {
  143. var (
  144. sql = fmt.Sprintf(_insertHistory, historyIdx(h.MID))
  145. )
  146. 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 {
  147. err = errors.WithStack(err)
  148. return
  149. }
  150. return
  151. }
  152. func intsToStrs(ints []int64) (strs []string) {
  153. for _, i := range ints {
  154. strs = append(strs, fmt.Sprintf("%d", i))
  155. }
  156. return
  157. }