mysql.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package block
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/service/main/member/model/block"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/xstr"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _user = `SELECT id,mid,status,ctime,mtime FROM block_user WHERE mid=? LIMIT 1`
  12. _upsertUser = `INSERT INTO block_user (mid,status) VALUES (?,?) ON DUPLICATE KEY UPDATE status=?`
  13. _userStatusList = `SELECT id,mid FROM block_user WHERE status=? AND id >= 0 LIMIT ?`
  14. _userStatusListWithMIDs = `SELECT mid FROM block_user WHERE status=? AND mid IN (%s)`
  15. _userLastHistory = `SELECT id,mid,action,start_time,duration FROM block_history_%d WHERE mid=? ORDER BY id DESC LIMIT 1`
  16. _addAddBlockCount = `INSERT INTO block_user_detail (mid,block_count) VALUES (?,1) ON DUPLICATE KEY UPDATE block_count=block_count+1`
  17. _insertHistory = `INSERT INTO block_history_%d (mid,admin_id,admin_name,source,area,reason,comment,action,start_time,duration,notify) VALUES (?,?,?,?,?,?,?,?,?,?,?)`
  18. _userDetails = `SELECT id,mid,block_count,ctime,mtime FROM block_user_detail WHERE mid IN (%s)`
  19. )
  20. func historyIdx(mid int64) int64 {
  21. return mid % 10
  22. }
  23. // User get block user from db
  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. if err == xsql.ErrNoRows {
  29. err = nil
  30. user = nil
  31. return
  32. }
  33. err = errors.WithStack(err)
  34. return
  35. }
  36. return
  37. }
  38. // UserLastHistory get lastest block history
  39. func (d *Dao) UserLastHistory(c context.Context, mid int64) (his *model.DBHistory, err error) {
  40. var (
  41. sql = fmt.Sprintf(_userLastHistory, historyIdx(mid))
  42. )
  43. row := d.db.QueryRow(c, sql, mid)
  44. his = &model.DBHistory{}
  45. if err = row.Scan(&his.ID, &his.MID, &his.Action, &his.StartTime, &his.Duration); err != nil {
  46. if err == xsql.ErrNoRows {
  47. err = nil
  48. his = nil
  49. return
  50. }
  51. err = errors.WithStack(err)
  52. return
  53. }
  54. return
  55. }
  56. // UserStatusList get user status list
  57. func (d *Dao) UserStatusList(c context.Context, status model.BlockStatus, startID int64, limit int) (maxID int64, mids []int64, err error) {
  58. var (
  59. rows *xsql.Rows
  60. )
  61. if rows, err = d.db.Query(c, _userStatusList, status, startID, limit); err != nil {
  62. err = errors.WithStack(err)
  63. return
  64. }
  65. defer rows.Close()
  66. for rows.Next() {
  67. var (
  68. id, mid int64
  69. )
  70. if err = rows.Scan(&id, &mid); err != nil {
  71. err = errors.WithStack(err)
  72. return
  73. }
  74. if maxID < id {
  75. maxID = id
  76. }
  77. mids = append(mids, mid)
  78. }
  79. if err = rows.Err(); err != nil {
  80. err = errors.WithStack(err)
  81. return
  82. }
  83. return
  84. }
  85. // UserStatusMapWithMIDs get user block status map by mids
  86. func (d *Dao) UserStatusMapWithMIDs(c context.Context, status model.BlockStatus, mids []int64) (midMap map[int64]struct{}, err error) {
  87. var (
  88. rows *xsql.Rows
  89. )
  90. url := fmt.Sprintf(_userStatusListWithMIDs, xstr.JoinInts(mids))
  91. if rows, err = d.db.Query(c, url, status); err != nil {
  92. err = errors.WithStack(err)
  93. return
  94. }
  95. defer rows.Close()
  96. midMap = make(map[int64]struct{})
  97. for rows.Next() {
  98. var (
  99. mid int64
  100. )
  101. if err = rows.Scan(&mid); err != nil {
  102. err = errors.WithStack(err)
  103. return
  104. }
  105. midMap[mid] = struct{}{}
  106. }
  107. if err = rows.Err(); err != nil {
  108. err = errors.WithStack(err)
  109. return
  110. }
  111. return
  112. }
  113. // TxUpdateUser is
  114. func (d *Dao) TxUpdateUser(c context.Context, tx *xsql.Tx, mid int64, status model.BlockStatus) (err error) {
  115. if _, err = tx.Exec(_upsertUser, mid, status, status); err != nil {
  116. err = errors.WithStack(err)
  117. return
  118. }
  119. return
  120. }
  121. // UpdateAddBlockCount is
  122. func (d *Dao) UpdateAddBlockCount(c context.Context, mid int64) (err error) {
  123. if _, err = d.db.Exec(c, _addAddBlockCount, mid); err != nil {
  124. err = errors.WithStack(err)
  125. return
  126. }
  127. return
  128. }
  129. // TxInsertHistory is
  130. func (d *Dao) TxInsertHistory(c context.Context, tx *xsql.Tx, h *model.DBHistory) (err error) {
  131. var (
  132. sql = fmt.Sprintf(_insertHistory, historyIdx(h.MID))
  133. )
  134. 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 {
  135. err = errors.WithStack(err)
  136. return
  137. }
  138. return
  139. }
  140. // UserDetails .
  141. func (d *Dao) UserDetails(c context.Context, mids []int64) (midMap map[int64]*model.DBUserDetail, err error) {
  142. var (
  143. rows *xsql.Rows
  144. )
  145. sql := fmt.Sprintf(_userDetails, xstr.JoinInts(mids))
  146. if rows, err = d.db.Query(c, sql); err != nil {
  147. err = errors.WithStack(err)
  148. return
  149. }
  150. defer rows.Close()
  151. midMap = make(map[int64]*model.DBUserDetail, len(mids))
  152. for rows.Next() {
  153. detail := &model.DBUserDetail{}
  154. if err = rows.Scan(&detail.ID, &detail.MID, &detail.BlockCount, &detail.CTime, &detail.MTime); err != nil {
  155. err = errors.WithStack(err)
  156. return
  157. }
  158. midMap[detail.MID] = detail
  159. }
  160. if err = rows.Err(); err != nil {
  161. err = errors.WithStack(err)
  162. return
  163. }
  164. return
  165. }