admin.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/main/reply/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/xstr"
  13. )
  14. const (
  15. _adminNameURL = "http://manager.bilibili.co/x/admin/manager/users/unames"
  16. _selAdminLogByRpIDSQL = "SELECT id,oid,type,rpid,adminid,result,remark,isnew,isreport,state,ctime,mtime FROM reply_admin_log WHERE rpid=? and isnew=1"
  17. _selAdminLogsByRpIDSQL = "SELECT id,oid,type,rpid,adminid,result,remark,isnew,isreport,state,ctime,mtime FROM reply_admin_log WHERE rpid=? order by ctime desc"
  18. _upAdminLogSQL = "UPDATE reply_admin_log SET isnew=0,mtime=? WHERE rpID IN(%s) AND isnew=1"
  19. _addAdminLogSQL = "INSERT INTO reply_admin_log (oid,type,rpid,adminid,result,remark,isnew,isreport,state,ctime,mtime) VALUES %s"
  20. _addAdminLogFormat = `(%d,%d,%d,%d,'%s','%s',%d,%d,%d,'%s','%s')`
  21. _logTimeLayout = "2006-01-02 15:04:05"
  22. )
  23. // AddAdminLog add admin log to mysql.
  24. func (d *Dao) AddAdminLog(c context.Context, oids, rpIDs []int64, adminID int64, typ, isNew, isReport, state int32, result, remark string, now time.Time) (rows int64, err error) {
  25. var vals []string
  26. for i := range oids {
  27. vals = append(vals, fmt.Sprintf(_addAdminLogFormat, oids[i], typ, rpIDs[i], adminID, result, remark, isNew, isReport, state, now.Format(_logTimeLayout), now.Format(_logTimeLayout)))
  28. }
  29. insertSQL := strings.Join(vals, ",")
  30. res, err := d.db.Exec(c, fmt.Sprintf(_addAdminLogSQL, insertSQL))
  31. if err != nil {
  32. return
  33. }
  34. return res.RowsAffected()
  35. }
  36. // UpAdminNotNew update admin log to not new.
  37. func (d *Dao) UpAdminNotNew(c context.Context, rpID []int64, now time.Time) (rows int64, err error) {
  38. res, err := d.db.Exec(c, fmt.Sprintf(_upAdminLogSQL, xstr.JoinInts(rpID)), now)
  39. if err != nil {
  40. return
  41. }
  42. return res.RowsAffected()
  43. }
  44. // AdminLog return admin log by rpid
  45. func (d *Dao) AdminLog(c context.Context, rpID int64) (r *model.AdminLog, err error) {
  46. row := d.db.QueryRow(c, _selAdminLogByRpIDSQL, rpID)
  47. r = new(model.AdminLog)
  48. if err = row.Scan(&r.ID, &r.Oid, &r.Type, &r.ReplyID, &r.AdminID, &r.Result, &r.Remark, &r.IsNew, &r.IsReport, &r.State, &r.CTime, &r.MTime); err != nil {
  49. return
  50. }
  51. return
  52. }
  53. // AdminLogsByRpID return operation log list.
  54. func (d *Dao) AdminLogsByRpID(c context.Context, rpID int64) (res []*model.AdminLog, err error) {
  55. rows, err := d.db.Query(c, _selAdminLogsByRpIDSQL, rpID)
  56. if err != nil {
  57. log.Error("query error(%v)", err)
  58. return
  59. }
  60. defer rows.Close()
  61. for rows.Next() {
  62. r := new(model.AdminLog)
  63. if err = rows.Scan(&r.ID, &r.Oid, &r.Type, &r.ReplyID, &r.AdminID, &r.Result, &r.Remark, &r.IsNew, &r.IsReport, &r.State, &r.CTime, &r.MTime); err != nil {
  64. return
  65. }
  66. res = append(res, r)
  67. }
  68. err = rows.Err()
  69. return
  70. }
  71. // AdminName get admin name by id.
  72. func (d *Dao) AdminName(c context.Context, admins map[int64]string) (err error) {
  73. if len(admins) == 0 {
  74. return
  75. }
  76. var res struct {
  77. Code int `json:"code"`
  78. Data map[string]string `json:"data"`
  79. }
  80. var uids []int64
  81. for key := range admins {
  82. uids = append(uids, key)
  83. }
  84. params := url.Values{}
  85. params.Set("uids", xstr.JoinInts(uids))
  86. if err = d.httpClient.Get(c, _adminNameURL, "", params, &res); err != nil {
  87. log.Error("ReportLog httpClient.Get(%s) error(%v) res(%v)", _adminNameURL, err, res)
  88. return
  89. }
  90. if ec := ecode.Int(res.Code); !ecode.OK.Equal(ec) {
  91. log.Error("ReportLog not ok(%s) error(%v) res(%v)", _adminNameURL, ec, res)
  92. err = ec
  93. return
  94. }
  95. for k, v := range res.Data {
  96. id, err := strconv.ParseInt(k, 10, 64)
  97. if err == nil {
  98. admins[id] = v
  99. } else {
  100. log.Error("ReportLog(%s) strconv error(%v) res(%v)", _adminNameURL, err, res)
  101. }
  102. }
  103. return
  104. }