package service import ( "Medical_OAuth/app/admin/model" "Medical_OAuth/app/admin/service/dto" "Medical_OAuth/common/actions" cDto "Medical_OAuth/common/dto" "Medical_OAuth/common/global" "errors" "gogs.baozhida.cn/zoie/OAuth-core/pkg" "gogs.baozhida.cn/zoie/OAuth-core/pkg/utils" "gogs.baozhida.cn/zoie/OAuth-core/service" "gorm.io/gorm" ) type SysUser struct { service.Service } // GetPage 获取SysUser列表 func (e *SysUser) GetPage(c *dto.SysUserGetPageReq, p *actions.DataPermission, list *[]model.SysUser, count *int64) error { var err error var data model.SysUser var userList []model.SysUser err = e.Orm. Scopes( cDto.MakeCondition(c.GetNeedSearch()), cDto.Paginate(c.GetPageSize(), c.GetPageIndex()), actions.UserPermission(data.TableName(), p), ). Find(&userList).Limit(-1).Offset(-1). Count(count).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.GetFailedErr } for i := 0; i < len(userList); i++ { var dept model.SysDept var role model.SysRole //var post model.SysPost err = e.Orm.First(&dept, userList[i].DeptId).Error userList[i].Dept = dept err = e.Orm.First(&role, userList[i].RoleId).Error userList[i].Role = role //err = e.Orm.First(&post, userList[i].PostId).Error //userList[i].Post = post } *list = userList return nil } func (e *SysUser) GetCount(c *dto.SysUserGetCountReq, count *int64) error { var err error var data model.SysUser err = e.Orm.Model(&data). Scopes( cDto.MakeCondition(c.GetNeedSearch()), ).Count(count).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.GetFailedErr } return nil } // Get 获取SysUser对象 func (e *SysUser) Get(d *dto.SysUserGetReq, p *actions.DataPermission, userModel *model.SysUser) error { var data model.SysUser var dept model.SysDept var role model.SysRole //var post model.SysPost err := e.Orm.Model(&data). Scopes(actions.UserPermission(data.TableName(), p)). First(userModel, d.GetId()).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.GetNotFoundOrNoPermissionErr } return global.GetFailedErr } err = e.Orm.First(&dept, userModel.DeptId).Error userModel.Dept = dept err = e.Orm.First(&role, userModel.RoleId).Error userModel.Role = role //err = e.Orm.First(&post, userModel.PostId).Error //userModel.Post = post return nil } // Insert 创建SysUser对象 func (e *SysUser) Insert(c *dto.SysUserInsertReq) error { var err error var data model.SysUser var i int64 err = e.Orm.Model(&data).Where("username = ?", c.Username).Count(&i).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.CreateFailedErr } if i > 0 { err = errors.New("用户名已存在!") e.Log.Errorf("db error: %s", err) return err } var uuid string for { uuid = utils.GetUUID() var j int64 err = e.Orm.Model(&data).Where("uuid = ?", uuid).Count(&j).Error if err != nil { continue } if j == 0 { break } } c.Generate(&data) data.Uuid = uuid err = e.Orm.Create(&data).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.CreateFailedErr } c.Id = data.Id return nil } // Update 修改SysUser对象 func (e *SysUser) Update(c *dto.SysUserUpdateReq, p *actions.DataPermission) error { var err error var userModel model.SysUser err = e.Orm. Scopes(actions.UserPermission(userModel.TableName(), p)). First(&userModel, c.GetId()).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.UpdateNotFoundOrNoPermissionErr } return global.UpdateFailedErr } c.Generate(&userModel) err = e.Orm.Model(&userModel).Where("id = ?", &userModel.Id). Omit("password", "salt").Updates(&userModel).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.UpdateFailedErr } return nil } // UpdateStatus 更新用户状态 func (e *SysUser) UpdateStatus(c *dto.UpdateSysUserStatusReq, p *actions.DataPermission) error { var userModel model.SysUser err := e.Orm. Scopes(actions.UserPermission(userModel.TableName(), p)). First(&userModel, c.GetId()).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.UpdateNotFoundOrNoPermissionErr } return global.UpdateFailedErr } err = e.Orm.Table(userModel.TableName()).Where("id = ? ", c.Id).Updates(c).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.UpdateFailedErr } return nil } // ResetPwd 重置用户密码 func (e *SysUser) ResetPwd(c *dto.ResetSysUserPwdReq, p *actions.DataPermission) error { var userModel model.SysUser err := e.Orm. Scopes(actions.UserPermission(userModel.TableName(), p)). First(&userModel, c.GetId()).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.UpdateNotFoundOrNoPermissionErr } return global.UpdateFailedErr } c.Generate(&userModel) err = e.Orm.Omit("username", "nick_name", "phone", "role_id", "avatar", "sex").Save(&userModel).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.UpdateFailedErr } return nil } // Remove 删除SysUser func (e *SysUser) Remove(c *dto.SysUserDeleteReq, p *actions.DataPermission) error { var data model.SysUser db := e.Orm.Model(&data). Scopes(actions.UserPermission(data.TableName(), p)). Find(&data, c.GetId()) if err := db.Error; err != nil { e.Log.Errorf("db error: %s", err) return err } if db.RowsAffected == 0 { return global.DeleteNotFoundOrNoPermissionErr } if err := e.Orm.Delete(&data, c.GetId()).Error; err != nil { e.Log.Errorf("db error: %s", err) return err } return nil } // UpdatePwd 修改SysUser对象密码 func (e *SysUser) UpdatePwd(id int, oldPassword, newPassword string) error { var err error if newPassword == "" { return nil } c := &model.SysUser{} err = e.Orm.Model(c). Select("id", "password", "salt"). First(c, id).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.UpdateNotFoundOrNoPermissionErr } return errors.New("密码修改失败") } var ok bool ok, err = pkg.CompareHashAndPassword(c.Password, oldPassword) if err != nil { e.Log.Errorf("CompareHashAndPassword error, %s", err.Error()) return errors.New("密码修改失败") } if !ok { err = errors.New("incorrect Password") e.Log.Warnf("user[%d] %s", id, err.Error()) return err } c.Password = newPassword db := e.Orm.Model(c).Where("id = ?", id). Select("Password", "Salt"). Updates(c) if err = db.Error; err != nil { e.Log.Errorf("db error: %s", err) return errors.New("密码修改失败") } return nil } func (e *SysUser) GetProfile(c *dto.SysUserById, user *model.SysUser) error { err := e.Orm.First(user, c.GetId()).Error if err != nil { return err } var dept model.SysDept var role model.SysRole //var post model.SysPost err = e.Orm.First(&dept, user.DeptId).Error if err != nil { return err } user.Dept = dept err = e.Orm.First(&role, user.RoleId).Error if err != nil { return err } user.Role = role //err = e.Orm.First(&post, user.PostId).Error //if err != nil { // return err //} //user.Post = post return nil } // 公开用户注册 func (e *SysUser) Register(c *dto.SysUserRegisterReq) error { var err error var registerAuth model.SysRegisterAuth err = e.Orm.Where("ur_key = ?", c.URKey).First(®isterAuth).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return errors.New("URKey不存在!") } return global.CreateFailedErr } if registerAuth.Status == 1 { return errors.New("URKey已停用!") } if registerAuth.SMSVerify == 2 { if len(c.Phone) == 0 { return errors.New("手机号码不能为空!") } if len(c.Code) == 0 { return errors.New("短信验证码不能为空!") } code, err := e.Cache.Get(model.GetVerifyCodeCacheKey(c.Phone)) if err != nil || code != c.Code { return errors.New("短信验证码错误") } } var data model.SysUser var i int64 err = e.Orm.Model(&data).Where("username = ?", c.Username).Count(&i).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.CreateFailedErr } if i > 0 { err = errors.New("用户名已存在!") e.Log.Errorf("db error: %s", err) return err } var uuid string for { uuid = utils.GetUUID() var j int64 err = e.Orm.Model(&data).Where("uuid = ?", uuid).Count(&j).Error if err != nil { continue } if j == 0 { break } } c.Generate(&data) data.Uuid = uuid data.RoleId = registerAuth.RoleId data.DeptId = registerAuth.DeptId data.Status = "2" err = e.Orm.Create(&data).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.CreateFailedErr } c.Id = data.Id return nil }