sys_user.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package dao
  2. import (
  3. "errors"
  4. "github.com/gofrs/uuid/v5"
  5. "gorm.io/gorm"
  6. "server/global"
  7. )
  8. type SysUser struct {
  9. global.GVA_MODEL
  10. UUID uuid.UUID `json:"uuid" gorm:"index;comment:用户UUID"` // 用户UUID
  11. Username string `json:"userName" gorm:"index;comment:用户登录名"` // 用户登录名
  12. Password string `json:"-" gorm:"comment:用户登录密码"` // 用户登录密码
  13. NickName string `json:"nickName" gorm:"default:系统用户;comment:用户昵称"` // 用户昵称
  14. SideMode string `json:"sideMode" gorm:"default:dark;comment:用户侧边主题"` // 用户侧边主题
  15. HeaderImg string `json:"headerImg" gorm:"default:https://qmplusimg.henrongyi.top/gva_header.jpg;comment:用户头像"` // 用户头像
  16. BaseColor string `json:"baseColor" gorm:"default:#fff;comment:基础颜色"` // 基础颜色
  17. ActiveColor string `json:"activeColor" gorm:"default:#1890ff;comment:活跃颜色"` // 活跃颜色
  18. AuthorityId uint `json:"authorityId" gorm:"default:888;comment:用户角色ID"` // 用户角色ID
  19. Authority SysAuthority `json:"authority" gorm:"foreignKey:AuthorityId;references:AuthorityId;comment:用户角色"`
  20. Phone string `json:"phone" gorm:"comment:用户手机号"` // 用户手机号
  21. Email string `json:"email" gorm:"comment:用户邮箱"` // 用户邮箱
  22. Enable int `json:"enable" gorm:"default:1;comment:用户是否被冻结 1正常 2冻结"` //用户是否被冻结 1正常 2冻结
  23. DepartmentId int `json:"departmentId" gorm:"comment:部门id"`
  24. }
  25. func (SysUser) TableName() string {
  26. return "sys_users"
  27. }
  28. // TODO:查询
  29. // TODO:新增
  30. // TODO:修改
  31. // TODO:删除
  32. // TODO: 用户查询
  33. // QueryUserByUserName 按用户名查询用户
  34. func QueryUserByUserName(userName string) (SysUser, error) {
  35. var user SysUser
  36. err := global.GVA_DB.Where("username =?", userName).First(&user).Error
  37. return user, err
  38. }
  39. // QueryUserByUserId 按用户Id查询用户
  40. func QueryUserByUserId(id uint) (SysUser, error) {
  41. var user SysUser
  42. err := global.GVA_DB.Where("id =?", id).First(&user).Error
  43. return user, err
  44. }
  45. // QueryUserAndAuthorityByUserName 按用户名查询用户和角色
  46. func QueryUserAndAuthorityByUserName(userName string) (user SysUser, err error) {
  47. err = global.GVA_DB.Where("username = ?", userName).Preload("Authority").First(&user).Error
  48. return
  49. }
  50. // QueryUserInfoList 条件查询用户信息列表
  51. func QueryUserInfoList(limit, offset int) (userList []SysUser, total int64, err error) {
  52. db := global.GVA_DB.Model(&SysUser{})
  53. err = db.Count(&total).Error
  54. if err != nil {
  55. return
  56. }
  57. err = db.Limit(limit).Offset(offset).Preload("Authority").Find(&userList).Error
  58. return
  59. }
  60. // QueryUserAuthority 查询用户角色
  61. func QueryUserAuthority(id, authorityId uint) (authority SysAuthority, err error) {
  62. err = global.GVA_DB.Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityId).First(&authority).Error
  63. return
  64. }
  65. // GetUserInfoByUUID 获取用户信息 按UUID
  66. func GetUserInfoByUUID(uuid uuid.UUID) (user SysUser, err error) {
  67. err = global.GVA_DB.Preload("Authority").First(&user, "uuid = ?", uuid).Error
  68. return user, err
  69. }
  70. // QueryAllUsers 查询所有用户
  71. func QueryAllUsers() (users []SysUser, err error) {
  72. err = global.GVA_DB.Model(&SysUser{}).Find(&users).Error
  73. return users, err
  74. }
  75. // TODO: 用户新增
  76. // CreateUser 创建用户
  77. func (u SysUser) CreateUser() error {
  78. return global.GVA_DB.Create(&u).Error
  79. }
  80. // TODO: 用户修改
  81. // UpdateUser 修改用户
  82. func (u SysUser) UpdateUser() error {
  83. return global.GVA_DB.Save(&u).Error
  84. }
  85. // UpdateUserAuthority 修改用户权限
  86. func UpdateUserAuthority(id, authorityId uint) error {
  87. return global.GVA_DB.Model(&SysUser{}).Where("id = ?", id).Update("authority_id", authorityId).Error
  88. }
  89. // SetUserAuthorities 设置用户权限
  90. func SetUserAuthorities(id uint, authorityId uint) (err error) {
  91. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  92. var user SysUser
  93. TxErr := tx.Where("id = ?", id).First(&user).Error
  94. if TxErr != nil {
  95. global.GVA_LOG.Debug(TxErr.Error())
  96. return errors.New("查询用户数据失败")
  97. }
  98. TxErr = tx.Delete(&[]SysUserAuthority{}, "sys_user_id = ?", id).Error
  99. if TxErr != nil {
  100. return TxErr
  101. }
  102. useAuthority := SysUserAuthority{
  103. SysUserId: id, SysAuthorityAuthorityId: authorityId,
  104. }
  105. TxErr = tx.Create(&useAuthority).Error
  106. if TxErr != nil {
  107. return TxErr
  108. }
  109. TxErr = tx.Model(&user).Update("authority_id", authorityId).Error
  110. if TxErr != nil {
  111. return TxErr
  112. }
  113. // 返回 nil 提交事务
  114. return nil
  115. })
  116. }
  117. // SetUserInfo 修改用户信息
  118. func (u SysUser) SetUserInfo() error {
  119. return global.GVA_DB.Model(&SysUser{}).Updates(&u).Error
  120. }
  121. // SetUserInfoById 修改用户信息 按id
  122. func (u SysUser) SetUserInfoById() error {
  123. return global.GVA_DB.Model(&SysUser{}).
  124. Where("id=?", u.ID).
  125. Updates(&u).Error
  126. }
  127. // UpdateUserDep 修改用户部门
  128. func UpdateUserDep(ids []int, depId int) error {
  129. return global.GVA_DB.Model(&SysUser{}).
  130. Where("id in (?)", ids).
  131. Update("department_id", depId).Error
  132. }
  133. // TODO: 用户删除
  134. // DeleteUser 删除用户
  135. func DeleteUser(id int) (err error) {
  136. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  137. if err := tx.Where("id = ?", id).Delete(&SysUser{}).Error; err != nil {
  138. return err
  139. }
  140. if err := tx.Delete(&[]SysUserAuthority{}, "sys_user_id = ?", id).Error; err != nil {
  141. return err
  142. }
  143. return nil
  144. })
  145. }