sys_user.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. TunnelIds []int `json:"tunnelIds" gorm:"-"`
  21. Tunnels []Tunnel `json:"tunnels" gorm:"many2many:user_tunnel"`
  22. Phone string `json:"phone" gorm:"comment:用户手机号"` // 用户手机号
  23. Email string `json:"email" gorm:"comment:用户邮箱"` // 用户邮箱
  24. Enable int `json:"enable" gorm:"default:1;comment:用户是否被冻结 1正常 2冻结"` //用户是否被冻结 1正常 2冻结
  25. }
  26. func (SysUser) TableName() string {
  27. return "sys_users"
  28. }
  29. // TODO: 用户查询
  30. // QueryUserByUserName 按用户名查询用户
  31. func QueryUserByUserName(userName string) (SysUser, error) {
  32. var user SysUser
  33. err := global.GVA_DB.Where("username =?", userName).First(&user).Error
  34. return user, err
  35. }
  36. // QueryUserByUserId 按用户Id查询用户
  37. func QueryUserByUserId(id uint) (SysUser, error) {
  38. var user SysUser
  39. err := global.GVA_DB.Where("id =?", id).First(&user).Error
  40. return user, err
  41. }
  42. // QueryUserAndAuthorityByUserName 按用户名查询用户和角色
  43. func QueryUserAndAuthorityByUserName(userName string) (user SysUser, err error) {
  44. err = global.GVA_DB.Where("username = ?", userName).Preload("Authority").Preload("Tunnels").First(&user).Error
  45. return
  46. }
  47. // QueryUserInfoList 条件查询用户信息列表
  48. func QueryUserInfoList(limit, offset int) (userList []SysUser, total int64, err error) {
  49. db := global.GVA_DB.Model(&SysUser{})
  50. err = db.Count(&total).Error
  51. if err != nil {
  52. return
  53. }
  54. err = db.Limit(limit).Offset(offset).Preload("Authority").Preload("Tunnels").Find(&userList).Error
  55. return
  56. }
  57. func CreateUserTunnels(user SysUser, ids []int) error {
  58. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  59. var tunnels []Tunnel
  60. err := tx.Model(&Tunnel{}).Where("id in (?)", ids).Find(&tunnels).Error
  61. if err != nil {
  62. return err
  63. }
  64. err = tx.Model(&user).Debug().Association("Tunnels").Replace(tunnels)
  65. if err != nil {
  66. return err
  67. }
  68. return nil
  69. })
  70. }
  71. // QueryUserAuthority 查询用户角色
  72. func QueryUserAuthority(id, authorityId uint) (authority SysAuthority, err error) {
  73. err = global.GVA_DB.Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityId).First(&authority).Error
  74. return
  75. }
  76. // GetUserInfoByUUID 获取用户信息 按UUID
  77. func GetUserInfoByUUID(uuid uuid.UUID) (user SysUser, err error) {
  78. err = global.GVA_DB.Preload("Authority").First(&user, "uuid = ?", uuid).Error
  79. return user, err
  80. }
  81. // TODO: 用户新增
  82. // CreateUser 创建用户
  83. func (u SysUser) CreateUser() error {
  84. return global.GVA_DB.Create(&u).Error
  85. }
  86. // TODO: 用户修改
  87. // UpdateUser 修改用户
  88. func (u SysUser) UpdateUser() error {
  89. return global.GVA_DB.Save(&u).Error
  90. }
  91. // UpdateUserAuthority 修改用户权限
  92. func UpdateUserAuthority(id, authorityId uint) error {
  93. return global.GVA_DB.Model(&SysUser{}).Where("id = ?", id).Update("authority_id", authorityId).Error
  94. }
  95. // SetUserAuthorities 设置用户权限
  96. func SetUserAuthorities(id uint, authorityId uint) (err error) {
  97. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  98. var user SysUser
  99. TxErr := tx.Where("id = ?", id).First(&user).Error
  100. if TxErr != nil {
  101. global.GVA_LOG.Debug(TxErr.Error())
  102. return errors.New("查询用户数据失败")
  103. }
  104. TxErr = tx.Delete(&[]SysUserAuthority{}, "sys_user_id = ?", id).Error
  105. if TxErr != nil {
  106. return TxErr
  107. }
  108. useAuthority := SysUserAuthority{
  109. SysUserId: id, SysAuthorityAuthorityId: authorityId,
  110. }
  111. TxErr = tx.Create(&useAuthority).Error
  112. if TxErr != nil {
  113. return TxErr
  114. }
  115. TxErr = tx.Model(&user).Update("authority_id", authorityId).Error
  116. if TxErr != nil {
  117. return TxErr
  118. }
  119. // 返回 nil 提交事务
  120. return nil
  121. })
  122. }
  123. // SetUserInfo 修改用户信息
  124. func (u SysUser) SetUserInfo() error {
  125. return global.GVA_DB.Model(&SysUser{}).Updates(&u).Error
  126. }
  127. // SetUserInfoById 修改用户信息 按id
  128. func (u SysUser) SetUserInfoById() error {
  129. return global.GVA_DB.Model(&SysUser{}).
  130. Where("id=?", u.ID).
  131. Updates(&u).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. }