user.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/videoup-task/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/xstr"
  8. )
  9. const (
  10. _usernameRoleSQL = `SELECT u.id, u.username, coalesce(r.role,0) role FROM user u LEFT JOIN auth_role r ON u.id = r.uid WHERE u.id IN (%s)`
  11. _usernameDepartmentSQL = `SELECT u.id, u.username, coalesce(d.name,'') department FROM user u LEFT JOIN user_department d ON u.department_id = d.id WHERE u.id IN (%s)`
  12. _usernameSQL = `SELECT id,username FROM user WHERE id IN (%s)`
  13. )
  14. //GetUsernameAndRole batch get username & role
  15. func (d *Dao) GetUsernameAndRole(ctx context.Context, uids []int64) (list map[int64]*model.UserRole, err error) {
  16. var (
  17. rows *sql.Rows
  18. )
  19. list = map[int64]*model.UserRole{}
  20. uidStr := xstr.JoinInts(uids)
  21. if rows, err = d.mngDB.Query(ctx, fmt.Sprintf(_usernameRoleSQL, uidStr)); err != nil {
  22. PromeErr("mngdb: query", "GetUsernameAndRole d.mngDB.Query error(%v) uids(%s)", err, uidStr)
  23. return
  24. }
  25. defer rows.Close()
  26. for rows.Next() {
  27. u := new(model.UserRole)
  28. if err = rows.Scan(&u.UID, &u.Name, &u.Role); err != nil {
  29. PromeErr("mngdb: scan", "GetUsernameAndRole rows.Scan error(%v) uids(%s)", err, uidStr)
  30. return
  31. }
  32. list[u.UID] = u
  33. }
  34. return
  35. }
  36. //GetUsernameAndDepartment batch get username & department
  37. func (d *Dao) GetUsernameAndDepartment(ctx context.Context, uids []int64) (list map[int64]*model.UserDepart, err error) {
  38. var (
  39. rows *sql.Rows
  40. )
  41. list = map[int64]*model.UserDepart{}
  42. uidStr := xstr.JoinInts(uids)
  43. if rows, err = d.mngDB.Query(ctx, fmt.Sprintf(_usernameDepartmentSQL, uidStr)); err != nil {
  44. PromeErr("mngdb: query", "GetUsernameAndDepartment d.mngDB.Query error(%v) uids(%s)", err, uidStr)
  45. return
  46. }
  47. defer rows.Close()
  48. for rows.Next() {
  49. u := new(model.UserDepart)
  50. if err = rows.Scan(&u.UID, &u.Name, &u.Department); err != nil {
  51. PromeErr("mngdb: scan", "GetUsernameAndDepartment rows.Scan error(%v) uids(%s)", err, uidStr)
  52. return
  53. }
  54. list[u.UID] = u
  55. }
  56. return
  57. }
  58. //GetUsername get username
  59. func (d *Dao) GetUsername(ctx context.Context, uids []int64) (list map[int64]string, err error) {
  60. var (
  61. rows *sql.Rows
  62. uid int64
  63. name string
  64. )
  65. list = map[int64]string{}
  66. uidStr := xstr.JoinInts(uids)
  67. if rows, err = d.mngDB.Query(ctx, fmt.Sprintf(_usernameSQL, uidStr)); err != nil {
  68. PromeErr("mngdb: query", "GetUsername d.mngDB.Query error(%v) uids(%s)", err, uidStr)
  69. return
  70. }
  71. defer rows.Close()
  72. for rows.Next() {
  73. if err = rows.Scan(&uid, &name); err != nil {
  74. PromeErr("mngdb: scan", "GetUsername rows.Scan error(%v) uids(%s)", err, uidStr)
  75. return
  76. }
  77. list[uid] = name
  78. }
  79. return
  80. }