mng_user.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package dao
  2. import (
  3. "context"
  4. model "go-common/app/admin/main/macross/model/manager"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. const (
  9. // load cache(get all).
  10. _usersSQL = `SELECT user.id,user.system,user.name,user.rid,role.name,user.ctime,user.mtime FROM user,role WHERE user.rid=role.id`
  11. _userSQL = `SELECT user.id,user.system,user.name,user.rid,role.name,user.ctime,user.mtime FROM user,role WHERE user.rid=role.id AND user.id=?`
  12. // user.
  13. _inUserSQL = `INSERT INTO user (system,name,rid) VALUES(?,?,?)`
  14. _upUserSQL = `UPDATE user SET name=?,rid=? WHERE id=?`
  15. _delUserSQL = `DELETE FROM user WHERE id=?`
  16. )
  17. // Users select all user from db.
  18. func (d *Dao) Users(c context.Context) (res map[string]map[string]*model.User, err error) {
  19. rows, err := d.db.Query(c, _usersSQL)
  20. if err != nil {
  21. log.Error("UserAll d.db.Query(%d) error(%v)", err)
  22. return
  23. }
  24. defer rows.Close()
  25. res = make(map[string]map[string]*model.User)
  26. for rows.Next() {
  27. var (
  28. users map[string]*model.User
  29. ok bool
  30. )
  31. user := &model.User{}
  32. if err = rows.Scan(&user.UserID, &user.System, &user.UserName, &user.RoleID, &user.RoleName, &user.CTime, &user.MTime); err != nil {
  33. log.Error("Users rows.Scan error(%v)", err)
  34. return
  35. }
  36. if users, ok = res[user.System]; !ok {
  37. users = make(map[string]*model.User)
  38. res[user.System] = users
  39. }
  40. users[user.UserName] = user
  41. }
  42. return
  43. }
  44. // User get user.
  45. func (d *Dao) User(c context.Context, userID int64) (re *model.User, err error) {
  46. row := d.db.QueryRow(c, _userSQL, userID)
  47. re = &model.User{}
  48. if err = row.Scan(&re.UserID, &re.System, &re.UserName, &re.RoleID, &re.RoleName, &re.CTime, &re.MTime); err != nil {
  49. if err == sql.ErrNoRows {
  50. err = nil
  51. } else {
  52. log.Error("User d.db.QueryRow(%d) error(%v)", userID, err)
  53. }
  54. }
  55. return
  56. }
  57. // AddUser insert user.
  58. func (d *Dao) AddUser(c context.Context, roleID int64, system, userName string) (rows int64, err error) {
  59. res, err := d.db.Exec(c, _inUserSQL, system, userName, roleID)
  60. if err != nil {
  61. log.Error("AddUser d.db.Exec() error(%v)", err)
  62. return
  63. }
  64. rows, err = res.RowsAffected()
  65. return
  66. }
  67. // UpUser update user.
  68. func (d *Dao) UpUser(c context.Context, userID, roleID int64, userName string) (rows int64, err error) {
  69. res, err := d.db.Exec(c, _upUserSQL, userName, roleID, userID)
  70. if err != nil {
  71. log.Error("UpUser d.db.Exec() error(%v)", err)
  72. return
  73. }
  74. rows, err = res.RowsAffected()
  75. return
  76. }
  77. // DelUser del user.
  78. func (d *Dao) DelUser(c context.Context, userID int64) (rows int64, err error) {
  79. res, err := d.db.Exec(c, _delUserSQL, userID)
  80. if err != nil {
  81. log.Error("DelUser d.db.Exec() error(%v)", err)
  82. return
  83. }
  84. rows, err = res.RowsAffected()
  85. return
  86. }