client_user.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package client
  2. import (
  3. "errors"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/client"
  6. clientReq "github.com/flipped-aurora/gin-vue-admin/server/model/client/request"
  7. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  8. "github.com/google/uuid"
  9. "gorm.io/gorm"
  10. )
  11. type ClientUserService struct {
  12. }
  13. func (clientUserService *ClientUserService) Login(loginInfo *clientReq.Login) (clientUser client.ClientUser, err error) {
  14. err = global.GVA_DB.Where("username = ?", loginInfo.Username).First(&clientUser).Error
  15. if err != nil {
  16. return clientUser, errors.New("用户不存在")
  17. }
  18. if !utils.BcryptCheck(loginInfo.Password, clientUser.Password) {
  19. return clientUser, errors.New("密码错误")
  20. }
  21. return
  22. }
  23. // CreateClientUser 创建客户端用户记录
  24. // Author [piexlmax](https://github.com/piexlmax)
  25. func (clientUserService *ClientUserService) CreateClientUser(clientUser *client.ClientUser) (err error) {
  26. ferr := global.GVA_DB.Where("username = ?", clientUser.Username).First(&client.ClientUser{}).Error
  27. if ferr == nil {
  28. return errors.New("用户名已注册")
  29. }
  30. if clientUser.Nickname == "" {
  31. clientUser.Nickname = clientUser.Username
  32. }
  33. clientUser.Password = utils.BcryptHash(clientUser.Password)
  34. clientUser.UUID = uuid.New()
  35. err = global.GVA_DB.Create(clientUser).Error
  36. return err
  37. }
  38. func (clientUserService *ClientUserService) ResetPassword(ID uint, password string) (err error) {
  39. err = global.GVA_DB.Model(&client.ClientUser{}).Where("id = ?", ID).Update("password", utils.BcryptHash(password)).Error
  40. return err
  41. }
  42. // DeleteClientUser 删除客户端用户记录
  43. // Author [piexlmax](https://github.com/piexlmax)
  44. func (clientUserService *ClientUserService) DeleteClientUser(ID string, userID uint) (err error) {
  45. err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  46. if err := tx.Model(&client.ClientUser{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
  47. return err
  48. }
  49. if err = tx.Delete(&client.ClientUser{}, "id = ?", ID).Error; err != nil {
  50. return err
  51. }
  52. return nil
  53. })
  54. return err
  55. }
  56. // DeleteClientUserByIds 批量删除客户端用户记录
  57. // Author [piexlmax](https://github.com/piexlmax)
  58. func (clientUserService *ClientUserService) DeleteClientUserByIds(IDs []string, deleted_by uint) (err error) {
  59. err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  60. if err := tx.Model(&client.ClientUser{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
  61. return err
  62. }
  63. if err := tx.Where("id in ?", IDs).Delete(&client.ClientUser{}).Error; err != nil {
  64. return err
  65. }
  66. return nil
  67. })
  68. return err
  69. }
  70. // UpdateClientUser 更新客户端用户记录
  71. // Author [piexlmax](https://github.com/piexlmax)
  72. func (clientUserService *ClientUserService) UpdateClientUser(clientUser client.ClientUser) (err error) {
  73. err = global.GVA_DB.Updates(&clientUser).Error
  74. return err
  75. }
  76. // GetClientUser 根据ID获取客户端用户记录
  77. // Author [piexlmax](https://github.com/piexlmax)
  78. func (clientUserService *ClientUserService) GetClientUser(ID string) (clientUser client.ClientUser, err error) {
  79. err = global.GVA_DB.Omit("password").Where("id = ?", ID).First(&clientUser).Error
  80. return
  81. }
  82. // GetClientUserInfoList 分页获取客户端用户记录
  83. // Author [piexlmax](https://github.com/piexlmax)
  84. func (clientUserService *ClientUserService) GetClientUserInfoList(info clientReq.ClientUserSearch) (list []client.ClientUser, total int64, err error) {
  85. limit := info.PageSize
  86. offset := info.PageSize * (info.Page - 1)
  87. // 创建db
  88. db := global.GVA_DB.Model(&client.ClientUser{})
  89. var clientUsers []client.ClientUser
  90. // 如果有条件搜索 下方会自动创建搜索语句
  91. if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
  92. db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
  93. }
  94. if info.Username != "" {
  95. db = db.Where("username = ?", info.Username)
  96. }
  97. if info.Nickname != "" {
  98. db = db.Where("nickname LIKE ?", "%"+info.Nickname+"%")
  99. }
  100. err = db.Count(&total).Error
  101. if err != nil {
  102. return
  103. }
  104. if limit != 0 {
  105. db = db.Limit(limit).Offset(offset)
  106. }
  107. err = db.Find(&clientUsers).Error
  108. return clientUsers, total, err
  109. }