1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package service
- import (
- "AIT/common"
- "AIT/constant"
- "AIT/global"
- "AIT/models"
- "gorm.io/gorm"
- )
- type UserService struct {
- }
- // FindUserByUsername find by username
- func (u *UserService) FindUserByUsername(userName string) (models.User, int64) {
- var user models.User
- count := global.Db.Where("user_name = ?", userName).First(&user).RowsAffected
- return user, count
- }
- // CreateUser create user
- func (u *UserService) CreateUser(user models.UserRegister) *gorm.DB {
- userCreate := models.User{
- UserName: user.UserName,
- Password: user.Password,
- Role: user.Role,
- }
- userCreate.Id = common.GenerateUUID()
- return global.Db.Create(&userCreate)
- }
- func (u *UserService) Login(userLogin models.UserLogin) *models.User {
- var user models.User
- rows := global.Db.Where(" user_name = ? and password = ? and role = ?", userLogin.Username, userLogin.Password, userLogin.Role).First(&user).RowsAffected
- if rows == 0 {
- return nil
- } else {
- return &user
- }
- }
- func (u *UserService) FindUserByUsernameAndRole(username string, role uint8) *models.User {
- var user models.User
- rows := global.Db.Where(" user_name = ? and role = ?", username, role).First(&user).RowsAffected
- if rows == 0 {
- return nil
- } else {
- return &user
- }
- }
- func (u *UserService) RestPassword(username string) *gorm.DB {
- user, count := u.FindUserByUsername(username)
- if count <= 0 {
- return nil
- }
- userUpdate := models.User{
- Password: constant.DefaultUserPassword,
- }
- userUpdate.Id = user.Id
- if user.Id != "" {
- result := global.Db.Debug().Updates(&userUpdate)
- return result
- } else {
- return nil
- }
- }
- func (u *UserService) GetUserList(param models.UserListParam) ([]models.User, int64) {
- query := models.User{
- Role: param.Role,
- }
- if len(param.UserName) > 0 {
- query.UserName = param.UserName
- }
- page := models.Page{
- PageNum: param.PageNum,
- PageSize: param.PageSize,
- }
- userList := make([]models.User, 0)
- rows := common.RestPage(page, "user", query, "", &userList, &[]models.User{})
- return userList, rows
- }
- func (u *UserService) ChangePassword(id string, newPassword string) int64 {
- userUpdate := models.User{
- Password: newPassword,
- }
- userUpdate.Id = id
- count := global.Db.Debug().Updates(&userUpdate).RowsAffected
- return count
- }
|