package service import ( "confrontation-training/common" "confrontation-training/constant" "confrontation-training/global" "confrontation-training/models" "gorm.io/gorm" ) type UserService struct { } // FindUserByPhone find by phone func (u *UserService) FindUserByPhone(phone string) (models.User, int64) { var user models.User count := global.Db.Where("phone = ?", phone).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, Phone: user.Phone, } return global.Db.Create(&userCreate) } func (u *UserService) Login(userLogin models.UserLogin) *models.User { var user models.User rows := global.Db.Where(" phone = ? and password = ? and role = ?", userLogin.Phone, userLogin.Password, userLogin.Role).First(&user).RowsAffected if rows == 0 { return nil } else { return &user } } func (u *UserService) FindUserByPhoneAndRole(phone string, role uint8) *models.User { var user models.User rows := global.Db.Where(" phone = ? and role = ?", phone, role).First(&user).RowsAffected if rows == 0 { return nil } else { return &user } } func (u *UserService) RestPassword(phone string) *gorm.DB { user, count := u.FindUserByPhone(phone) if count <= 0 { return nil } userUpdate := models.User{ Password: constant.DefaultPassword, } userUpdate.ID = user.ID result := global.Db.Updates(&userUpdate) return result } 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, "ct_users", query, "", &userList, &[]models.User{}) return userList, rows } func (u *UserService) ChangePassword(id uint, newPassword string) int64 { userUpdate := models.User{ Password: newPassword, } userUpdate.ID = id count := global.Db.Debug().Updates(&userUpdate).RowsAffected return count }