// namespace SafeCampus.Application; /// /// /// public class UserService : DbRepository, IUserService { private readonly ISysUserService _sysUserService; private readonly ISysRoleService _sysRoleService; private readonly IImportExportService _importExportService; public UserService(ISysUserService sysUserService, ISysRoleService sysRoleService, IImportExportService importExportService) { _sysUserService = sysUserService; _sysRoleService = sysRoleService; _importExportService = importExportService; } #region 查询 /// public async Task> Page(UserPageInput input) { var query = await GetQuery(input); //分页查询 var pageInfo = await _sysUserService.Page(query); return pageInfo; } /// public async Task> OwnRole(BaseIdInput input) { return await _sysUserService.OwnRole(input);//获取角色 } /// public async Task> Selector(UserSelectorInput input) { //获取数据范围 var dataScope = await _sysUserService.GetLoginUserApiDataScope(); if (dataScope == null) return await _sysUserService.Selector(input);//查询 if (dataScope.Count > 0) { input.OrgIds = dataScope;//赋值机构列表 return await _sysUserService.Selector(input);//查询 } //返回自己 return new SqlSugarPagedList { Total = 1, Pages = 1, HasPrevPages = false, HasNextPages = false, List = new[] { new UserSelectorOutPut { Account = UserManager.UserAccount, Id = UserManager.UserId, Name = UserManager.Name, OrgId = UserManager.OrgId } } }; } /// public async Task> RoleSelector(RoleSelectorInput input) { var sysRoles = new SqlSugarPagedList { Total = 1, Pages = 1, HasPrevPages = false, HasNextPages = false, List = new List() }; //获取数据范围 var dataScope = await _sysUserService.GetLoginUserApiDataScope(); if (dataScope == null) sysRoles = await _sysRoleService.RoleSelector(input);//获取角色选择器列表 else if (dataScope.Count > 0)//如果有机构 { input.OrgIds = dataScope;//将数据范传进去 sysRoles = await _sysRoleService.RoleSelector(input);//获取角色选择器列表 } return sysRoles; } /// public async Task Detail(BaseIdInput input) { var user = await _sysUserService.Detail(input); var errorMessage = "您没有权限查看该人员"; //判断数据范围 await _sysUserService.CheckApiDataScope(user.OrgId, user.CreateUserId, errorMessage); return user; } #endregion 查询 #region 新增 /// public async Task Add(UserAddInput input) { await CheckInput(input, SystemConst.ADD);//检查参数 await _sysUserService.Add(input);//添加 } #endregion 新增 #region 编辑 /// public async Task Edit(UserEditInput input) { await CheckInput(input, SystemConst.EDIT);//检查参数 await _sysUserService.Edit(input);//编辑 } /// public async Task Edits(BatchEditInput input) { //获取数据范围 var dataScope = await _sysUserService.GetLoginUserApiDataScope(); var ids = input.Ids; var sysUsers = await GetListAsync(it => ids.Contains(it.Id), it => new SysUser { OrgId = it.OrgId, CreateUserId = it.CreateUserId });//根据用户ID获取机构id、 sysUsers.ForEach(it => { if (dataScope != null && !dataScope.Contains(it.OrgId) && it.CreateUserId != UserManager.UserId) throw Oops.Bah(ErrorCodeEnum.A0004);//如果不包含机构id并且不是自己创建的 }); await _sysUserService.Edits(input); } /// public async Task DisableUser(BaseIdInput input) { await CheckInput(input.Id, SystemConst.DISABLE);//检查权限 await _sysUserService.DisableUser(input);//禁用 } /// public async Task EnableUser(BaseIdInput input) { await CheckInput(input.Id, SystemConst.ENABLE);//检查权限 await _sysUserService.EnableUser(input);//启用 } /// public async Task GrantRole(UserGrantRoleInput input) { await CheckInput(input.Id, SystemConst.DISABLE);//检查权限 await _sysUserService.GrantRole(input);//授权 } /// public async Task ResetPassword(BaseIdInput input) { await CheckInput(input.Id, SystemConst.RESET_PWD);//检查权限 await _sysUserService.ResetPassword(input);//重置密码 } #endregion 编辑 #region 删除 /// public async Task Delete(BaseIdListInput input) { //获取所有ID var ids = input.Ids; //获取用户下信息 var users = await GetListAsync(it => ids.Contains(it.Id), it => new SysUser { OrgId = it.OrgId, Id = it.Id }); //检查数据范围 var orgIds = users.Select(it => it.OrgId).ToList(); var createUserIds = users.Select(it => it.CreateUserId.GetValueOrDefault()).ToList(); await _sysUserService.CheckApiDataScope(orgIds, createUserIds, "您没有权限删除这些人员"); await _sysUserService.Delete(input);//删除 } #endregion 删除 #region 导入导出 /// public async Task Template() { var templateName = "人员信息.xlsx"; //var result = _importExportService.GenerateLocalTemplate(templateName); var result = await _importExportService.GenerateTemplate(templateName); return result; } /// public async Task Preview(ImportPreviewInput input) { //获取数据范围 var dataScope = await _sysUserService.GetLoginUserApiDataScope(); if (dataScope == null || dataScope.Count > 0) { var importPreview = await _importExportService.GetImportPreview(input.File); importPreview.Data = await CheckImport(importPreview.Data, dataScope);//检查导入数据 return importPreview; } throw Oops.Bah("您无权导入用户"); } /// public async Task Export(UserPageInput input) { var query = await GetQuery(input); var users = await _sysUserService.List(query); var data = users.Adapt>();//转为Dto var result = await _importExportService.Export(data, "人员信息"); return result; } /// public async Task> Import(ImportResultInput input) { //获取数据范围 var dataScope = await _sysUserService.GetLoginUserApiDataScope(); if (dataScope.Count > 0) { var data = await CheckImport(input.Data, dataScope, true);//检查数据格式 var result = _importExportService.GetImportResultPreview(data, out var importData); var sysUsers = importData.Adapt>();//转实体 await _sysUserService.SetUserDefault(sysUsers);//设置用户默认值 await InsertOrBulkCopy(sysUsers);// 数据导入 return result; } throw Oops.Bah("您无权导入用户"); } #endregion 导入导出 #region 方法 /// /// 检查输入参数 /// /// /// 操作类型 private async Task CheckInput(SysUser sysUser, string operate) { var errorMessage = $"您没有权限{operate}该机构下的人员"; if (sysUser.Id != SafeCampusConst.ZERO) { var user = await _sysUserService.GetUserById(sysUser.Id); sysUser.CreateUserId = user.CreateUserId; } //检查数据范围 await _sysUserService.CheckApiDataScope(sysUser.OrgId, sysUser.CreateUserId.GetValueOrDefault(), errorMessage); } /// /// 检查输入参数 /// /// 用户ID /// 操作 private async Task CheckInput(long userId, string operate) { var errorMessage = $"您没有权限{operate}该机构下的人员"; var sysUser = await _sysUserService.GetUserById(userId); //检查数据范围 await _sysUserService.CheckApiDataScope(sysUser.OrgId, sysUser.CreateUserId.GetValueOrDefault(), errorMessage); } /// /// 检查导入数据 /// /// 数据 /// 数据范围ID数组 /// 是否初始化错误 /// public async Task> CheckImport(List data, List dataScope, bool clearError = false) { var errorMessage = "没有权限"; //先经过系统用户检查 var bizUsers = await _sysUserService.CheckImport(data, clearError); bizUsers.ForEach(it => { //如果机构没有错误 if (!it.ErrorInfo.ContainsKey(nameof(it.OrgName))) { //判断是否包含数据范围,如果不包含 if (dataScope != null && !dataScope.Contains(it.OrgId)) { it.ErrorInfo.Add(nameof(it.OrgName), errorMessage); if (!it.ErrorInfo.ContainsKey(nameof(it.PositionName)))//如果机构没错 it.ErrorInfo.Add(nameof(it.PositionName), errorMessage); } } if (it.ErrorInfo.Count > 0) it.HasError = true;//如果错误信息数量大于0则表示有错误 }); bizUsers = bizUsers.OrderByDescending(it => it.HasError).ToList();//排序 return bizUsers; } /// /// 获取查询条件 /// /// /// public async Task GetQuery(UserPageInput input) { //获取数据范围 var dataScope = await _sysUserService.GetLoginUserApiDataScope(); //动态查询条件 var exp = Expressionable.Create(); exp.And(u => u.Account != SysRoleConst.SUPER_ADMIN); exp.AndIF(dataScope is { Count: > 0 }, u => dataScope.Contains(u.OrgId));//用户机构在数据范围内 exp.AndIF(dataScope is { Count: 0 }, u => u.Id == UserManager.UserId);//用户ID等于自己 input.Expression = exp; return input; } #endregion 方法 }