mysql.go 4.8 KB

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