diff --git a/langguanApi/Controllers/MeauController.cs b/langguanApi/Controllers/MeauController.cs new file mode 100644 index 0000000..39d5710 --- /dev/null +++ b/langguanApi/Controllers/MeauController.cs @@ -0,0 +1,43 @@ +using langguanApi.Model; +using langguanApi.Model.Dto.SystemConfigurationDto; +using langguanApi.Service; +using Microsoft.AspNetCore.Mvc; + +namespace langguanApi.Controllers +{ + /// + /// 菜单控制器 + /// + [Route("api/[controller]")] + [ApiController] + public class MeauController : ControllerBase + { + private MeauService _meuaService; + public MeauController(MeauService meauService) + { + _meuaService = meauService; + } + /// + /// 获取菜单列表 + /// + /// + /// + [HttpGet("GetMenuList")] + public async Task GetMenuList([FromQuery] reqpage input) + { + var result = await _meuaService.GetMeauList(input); + return Ok(result); + } + /// + /// 新增菜单 + /// + /// + /// + [HttpPost("AddMeau")] + public async Task AddMeau([FromBody] MeauDto input) + { + var result = await _meuaService.AddMeau(input); + return Ok(result); + } + } +} diff --git a/langguanApi/Controllers/RoleController.cs b/langguanApi/Controllers/RoleController.cs new file mode 100644 index 0000000..8feda65 --- /dev/null +++ b/langguanApi/Controllers/RoleController.cs @@ -0,0 +1,31 @@ +using langguanApi.Model.Dto.SystemConfigurationDto; +using langguanApi.Service; +using Microsoft.AspNetCore.Mvc; + +namespace langguanApi.Controllers +{ + /// + /// 角色 + /// + [Route("api/[controller]")] + [ApiController] + public class RoleController : ControllerBase + { + private RoleService _roleService; + public RoleController(RoleService roleService) + { + _roleService = roleService; + } + /// + ///新增角色 + /// + /// + /// + [HttpPost("AddRole")] + public async Task AddRole([FromBody] RoleDto input) + { + var result = await _roleService.AddRole(input); + return Ok(result); + } + } +} diff --git a/langguanApi/Controllers/UserController.cs b/langguanApi/Controllers/UserController.cs index 2ea1c88..e771242 100644 --- a/langguanApi/Controllers/UserController.cs +++ b/langguanApi/Controllers/UserController.cs @@ -1,5 +1,6 @@ using langguanApi.Model; using langguanApi.Model.Dto; +using langguanApi.Model.Dto.SystemConfigurationDto; using langguanApi.Service; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -79,6 +80,30 @@ namespace langguanApi.Controllers #region 用户管理相关接口 + /// + ///新增用户 + /// + /// + /// + [HttpPost("AddUser")] + public async Task AddUser([FromBody] UserDto input) + { + var result = await _userService.AddUser(input); + return Ok(result); + } + //获取用户列表 + /// + /// 获取用户列表 + /// + /// + /// + [HttpGet("GetUserList")] + public async Task GetUserList([FromQuery] UserQueryDto input) + { + var result = await _userService.GetUserList(input); + return Ok(result); + } + #endregion } diff --git a/langguanApi/Model/Dto/SystemConfiguration/UserDto.cs b/langguanApi/Model/Dto/SystemConfiguration/UserDto.cs deleted file mode 100644 index 152ab6a..0000000 --- a/langguanApi/Model/Dto/SystemConfiguration/UserDto.cs +++ /dev/null @@ -1,57 +0,0 @@ -namespace langguanApi.Model.Dto.SystemConfiguration -{ - /// - /// 用于用户管理的Dto - /// - public class UserDto - { - /// - /// 用户名 - /// - public string Username { get; set; } - /// - /// 角色id - /// - public int roleId { get; set; } - /// - /// 邮箱 - /// - public string Email { get; set; } - /// - /// 学历 - /// - public string Education { get; set; } - /// - /// 手机号 - /// - public int Tel { get; set; } - /// - /// 性别 0表示男 1表示女 - /// - public byte Sex { get; set; } - /// - /// 毕业院校 - /// - public string University { get; set; } - /// - /// 出生日期 - /// - public string Brithday { get; set; } - /// - /// 籍贯 - /// - public string Native { get; set; } - /// - /// 居住地 - /// - public string Address { get; set; } - /// - /// 是否管理员 0否 1是 - /// - public byte IsAdmin { get; set; } - /// - /// 是否删除 0否 1是 - /// - public byte IsDel { get; set; } - } -} diff --git a/langguanApi/Model/Dto/SystemConfigurationDto/MeauDto.cs b/langguanApi/Model/Dto/SystemConfigurationDto/MeauDto.cs new file mode 100644 index 0000000..1f950d0 --- /dev/null +++ b/langguanApi/Model/Dto/SystemConfigurationDto/MeauDto.cs @@ -0,0 +1,30 @@ +namespace langguanApi.Model.Dto.SystemConfigurationDto +{ + public class MeauDto + { + /// + /// 父菜单ID 默认首页为1级,有组织为2级,无组织为2级,下面的子菜单为3级 + /// + public string ParentId { get; set; } + /// + /// 菜单编码 + /// + public string MenuCode { get; set; } + /// + /// 菜单名称 + /// + public string MenuName { get; set; } + /// + /// 可空,菜单跳转路径 + /// + public string Url { get; set; } + /// + /// 排序 + /// + public int Sort { get; set; } + /// + /// 是否激活 + /// + public bool IsActive { get; set; } + } +} diff --git a/langguanApi/Model/Dto/SystemConfigurationDto/RoleDto.cs b/langguanApi/Model/Dto/SystemConfigurationDto/RoleDto.cs new file mode 100644 index 0000000..ba7dd96 --- /dev/null +++ b/langguanApi/Model/Dto/SystemConfigurationDto/RoleDto.cs @@ -0,0 +1,25 @@ +namespace langguanApi.Model.Dto.SystemConfigurationDto +{ + /// + /// 角色DTO + /// + public class RoleDto + { + /// + /// 角色名称 + /// + public string RoleName { get; set; } + /// + /// 多个菜单用,隔开 + /// + public string Meaus { get; set; } + /// + /// 是否管理员 + /// + public bool IsAdmin { get; set; } + /// + /// 角色描述 + /// + public string Description { get; set; } + } +} diff --git a/langguanApi/Model/Dto/SystemConfigurationDto/UserDto.cs b/langguanApi/Model/Dto/SystemConfigurationDto/UserDto.cs new file mode 100644 index 0000000..8e9e631 --- /dev/null +++ b/langguanApi/Model/Dto/SystemConfigurationDto/UserDto.cs @@ -0,0 +1,148 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace langguanApi.Model.Dto.SystemConfigurationDto +{ + /// + /// 用于用户管理的Dto + /// + public class UserDto + { + /// + ///用户ID + /// + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + /// + /// 用户名 + /// + public string Username { get; set; } + /// + /// 角色id + /// + public string roleId { get; set; } + /// + /// 邮箱 + /// + public string Email { get; set; } + /// + /// 学历 + /// + public string Education { get; set; } + /// + /// 手机号 + /// + public string Tel { get; set; } + /// + /// 性别 0表示男 1表示女 + /// + public byte Sex { get; set; } + /// + /// 毕业院校 + /// + public string University { get; set; } + /// + /// 出生日期 + /// + public string BrithdayDateTime { get; set; } + public DateTime? Brithday { get; set; } + + /// + /// 入职日期 + /// + public string HiredateDateTime { get; set; } = DateTime.Now.ToString(); + public DateTime? Hiredate { get; set; } + /// + /// 籍贯 + /// + public string Native { get; set; } + /// + /// 居住地 + /// + public string Address { get; set; } + /// + /// 是否管理员 0否 1是 + /// + public byte IsAdmin { get; set; } + /// + /// 是否删除 0否 1是 + /// + public byte IsDel { get; set; } + //是否启用 0表示未启用 1表示启用 + public byte IsEnable { get; set; } + } + /// + ///根据用户条件查询 + /// + public class UserQueryDto + { + /// + /// 用户名 + /// + public string Username { get; set; } = ""; + /// + /// 手机号 + /// + public string Tel { get; set; } = ""; + //入职开始时间 + public string HiredateStart { get; set; } = ""; + //入职结束时间 + public string HiredateEnd { get; set; } = ""; + } + + /// + /// 用户列表展示 + /// + public class UserListDto + { + /// + /// 用户名 + /// + public string Username { get; set; } + /// + /// 学历 + /// + public string Education { get; set; } + /// + /// 邮箱 + /// + public string Email { get; set; } + /// + /// 角色名称 + /// + public string roleName{ get; set; } + /// + /// 性别 + /// + public string Sex { get; set; } + /// + /// 毕业院校 + /// + public string University { get; set; } + /// + /// 联系方式 + /// + public string Tel { get; set; } + /// + /// 出生日期 + /// + public string Brithday { get; set; } + /// + /// 籍贯 + /// + public string Native { get; set; } + /// + /// 居住地 + /// + public string Address { get; set; } + /// + /// 入职日期 + /// + public string Hiredate { get; set; } + /// + /// 是否管理员 0否 1是 + /// + public string IsAdmin { get; set; } + } +} diff --git a/langguanApi/Model/Entity/UserEntity.cs b/langguanApi/Model/Entity/UserEntity.cs index 8225ad8..4eb8056 100644 --- a/langguanApi/Model/Entity/UserEntity.cs +++ b/langguanApi/Model/Entity/UserEntity.cs @@ -1,4 +1,7 @@ -namespace langguanApi.Model.Entity +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace langguanApi.Model.Entity { /// /// 用户实体 @@ -16,7 +19,8 @@ /// /// 角色id /// - public int roleId { get; set; } + [BsonRepresentation(BsonType.ObjectId)] + public string roleId { get; set; } /// /// 邮箱 /// @@ -28,7 +32,7 @@ /// /// 手机号 /// - public int Tel { get; set; } + public string Tel { get; set; } /// /// 性别 0表示男 1表示女 /// @@ -40,7 +44,7 @@ /// /// 出生日期 /// - public string Brithday { get; set; } + public DateTime? Brithday { get; set; } /// /// 籍贯 /// @@ -50,8 +54,16 @@ /// public string Address { get; set; } /// + /// 入职日期 + /// + public DateTime? Hiredate { get; set; } + /// /// 是否管理员 0否 1是 /// public byte IsAdmin { get; set; } + /// + /// 是否启用 0禁用 1启用 + /// + public byte IsEnable { get; set; } } } diff --git a/langguanApi/Model/ReqPaing.cs b/langguanApi/Model/ReqPaing.cs index 40d145a..d662f43 100644 --- a/langguanApi/Model/ReqPaing.cs +++ b/langguanApi/Model/ReqPaing.cs @@ -1,4 +1,5 @@ -namespace langguanApi.Model + +namespace langguanApi.Model { public class ReqPaing { diff --git a/langguanApi/Model/SystemConfigurationEntity/Meau.cs b/langguanApi/Model/SystemConfigurationEntity/Meau.cs new file mode 100644 index 0000000..192ba51 --- /dev/null +++ b/langguanApi/Model/SystemConfigurationEntity/Meau.cs @@ -0,0 +1,32 @@ +namespace langguanApi.Model.SystemConfigurationEntity +{ + //菜单实体类 + public class Meau : BaseModel + { + /// + /// 父菜单ID 默认首页为1级,有组织为2级,无组织为2级,下面的子菜单为3级 + /// + public string ParentId { get; set; } + /// + /// 菜单编码 + /// + public string MenuCode { get; set; } + /// + /// 菜单名称 + /// + public string MenuName { get; set; } + /// + /// 可空,菜单跳转路径 + /// + public string Url { get; set; } + /// + /// 排序 + /// + public int Sort { get; set; } + /// + /// 是否激活 + /// + public bool IsActive { get; set; } + + } +} diff --git a/langguanApi/Model/SystemConfigurationEntity/UserRole.cs b/langguanApi/Model/SystemConfigurationEntity/UserRole.cs new file mode 100644 index 0000000..dc186be --- /dev/null +++ b/langguanApi/Model/SystemConfigurationEntity/UserRole.cs @@ -0,0 +1,25 @@ +namespace langguanApi.Model.SystemConfigurationEntity +{ + /// + /// 角色实体类 + /// + public class UserRole : BaseModel + { + /// + /// 角色名称 + /// + public string RoleName { get; set; } + /// + /// 多个菜单用,隔开 + /// + public string Meaus { get; set; } + /// + /// 是否管理员 + /// + public bool IsAdmin { get; set; } + /// + /// 角色描述 + /// + public string Description { get; set; } + } +} diff --git a/langguanApi/Service/MeauService.cs b/langguanApi/Service/MeauService.cs new file mode 100644 index 0000000..eed9241 --- /dev/null +++ b/langguanApi/Service/MeauService.cs @@ -0,0 +1,73 @@ +using langguanApi.Extensions.AutoDI; +using langguanApi.Model; +using langguanApi.Model.Dto.SystemConfigurationDto; +using langguanApi.Model.SystemConfigurationEntity; +using Mapster; +using System.Linq.Expressions; + +namespace langguanApi.Service +{ + /// + /// 菜单服务 + /// + [ServiceInjection(InjectionType.Transient)] + public class MeauService : BaseService + { + public MeauService(IConfiguration config) : base(config, nameof(Meau)) + { + } + + /// + /// 获取菜单列表 + /// + /// + /// + public async Task GetMeauList(reqpage input) + { + Expression> exp = filter =>filter.IsDelete == false; + if (!string.IsNullOrEmpty(input.key)) + { + exp=filter => filter.MenuName.Contains(input.key); + } + return await base.GetPager(new ReqPaing() + { + pageSize = input.pageSize, + current = input.current + }, exp); + } + + + /// + /// 新加 + /// + /// + /// + public async Task AddMeau(MeauDto input) + { + if (await Exist(input)) + { + return new ApiResult { code = 1, msg = $"{input.MenuName}的菜单已存在" }; + } + var entity = input.Adapt(); + if (entity != null) + { + await base.CreateAsync(entity); + return new ApiResult { code = 0, msg = "" }; + } + return new ApiResult { code = 1, msg = "菜单添加失败" }; ; + } + /// + /// 是否存在 + /// + /// + /// + public async Task Exist(MeauDto input) + { + var entity = input.Adapt(); + Expression> exp = filter => filter.MenuName == entity.MenuName; + return await base.Exist(exp); + } + + + } +} diff --git a/langguanApi/Service/RoleService.cs b/langguanApi/Service/RoleService.cs new file mode 100644 index 0000000..d1d8a70 --- /dev/null +++ b/langguanApi/Service/RoleService.cs @@ -0,0 +1,63 @@ +using langguanApi.Extensions.AutoDI; +using langguanApi.Model.Dto.SystemConfigurationDto; +using langguanApi.Model; +using langguanApi.Model.Entity; +using langguanApi.Model.SystemConfigurationEntity; +using StackExchange.Redis; +using Mapster; +using System.Linq.Expressions; + +namespace langguanApi.Service +{ + [ServiceInjection(InjectionType.Transient)] + public class RoleService : BaseService + { + public RoleService(IConfiguration config) : base(config, nameof(Role)) + { + } + /// + /// 用户是否存在 + /// + /// + /// + public async Task Exist(RoleDto input) + { + var entity = input.Adapt(); + Expression> exp = filter => filter.RoleName == entity.RoleName; + return await base.Exist(exp); + } + + /// + /// 新加角色 + /// + /// + /// + public async Task AddRole(RoleDto input) + { + if (await Exist(input)) + { + return new ApiResult { code = 1, msg = $"{input.RoleName}的角色已存在" }; + } + var entity = input.Adapt(); + if (entity != null) + { + await base.CreateAsync(entity); + return new ApiResult { code = 0, msg = "" }; + } + return new ApiResult { code = 1, msg = "菜单添加失败" }; ; + } + /// + /// 根据Id,获取多个校色信息 + /// + /// + /// + public async Task> GetRoleListByIds(IEnumerable ids) + { + Expression> exp = filter => ids.Contains(filter.Id) && filter.IsDelete == false; + var list = (await base.GetListWithExp(exp)).ToList(); + return list; + } + + + } +} diff --git a/langguanApi/Service/UserService.cs b/langguanApi/Service/UserService.cs index e4468a8..2f72644 100644 --- a/langguanApi/Service/UserService.cs +++ b/langguanApi/Service/UserService.cs @@ -2,8 +2,13 @@ using langguanApi.Extensions.AutoDI; using langguanApi.Model; using langguanApi.Model.Dto; +using langguanApi.Model.Dto.SystemConfigurationDto; using langguanApi.Model.Entity; using Mapster; +using Microsoft.AspNetCore.Mvc.Filters; +using MongoDB.Bson.IO; +using MongoDB.Driver.Linq; +using Org.BouncyCastle.Asn1.Ocsp; using System.Linq.Expressions; namespace langguanApi.Service @@ -11,8 +16,12 @@ namespace langguanApi.Service [ServiceInjection(InjectionType.Transient)] public class UserService : BaseService { - public UserService(IConfiguration config) : base(config, nameof(UserEntity).Replace("Entity", "")) + private ILogger _logger; + private RoleService _roleService; + public UserService(IConfiguration config, ILogger logger, RoleService roleService) : base(config, nameof(UserEntity).Replace("Entity", "")) { + _logger = logger; + _roleService = roleService; } /// /// 登录 @@ -86,5 +95,152 @@ namespace langguanApi.Service { await base.RemoveAsync(id); } + + #region 用户管理 + /// + /// 用户是否存在 + /// + /// + /// + public async Task Exist(UserDto input) + { + var entity = input.Adapt(); + Expression> exp = filter => filter.Id == entity.Id; + return await base.Exist(exp); + } + /// + ///新增用户 + /// + /// 新增用户dto + /// + public async Task AddUser(UserDto input) + { + try + { + #region 校验参数 + if (string.IsNullOrEmpty(input.Username)) + { + return new ApiResult { code = 1, msg = "用户名非空" }; + } + if (string.IsNullOrEmpty(input.Tel)) + { + return new ApiResult { code = 1, msg = "手机号非空" }; + } + if (string.IsNullOrEmpty(input.Email)) + { + return new ApiResult { code = 1, msg = "邮箱非空" }; + } + if (string.IsNullOrEmpty(input.roleId)) + { + return new ApiResult { code = 1, msg = "角色不能为空" }; + } + #endregion + #region 组织数据 + + if (!string.IsNullOrEmpty(input.BrithdayDateTime)) + { + input.Brithday = Convert.ToDateTime(input.BrithdayDateTime); + } + if (!string.IsNullOrEmpty(input.HiredateDateTime)) + { + input.Hiredate = Convert.ToDateTime(input.HiredateDateTime); + } + input.IsEnable = 1; + var entity = input.Adapt(); + #endregion + #region 保存数据 + if (entity != null && string.IsNullOrEmpty(entity.Id)) + { + await base.CreateAsync(entity); + } + + #endregion + } + catch (Exception ex) + { + _logger.LogError($"新增用户出现异常,请求参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," + + $"请求接口:'api/User/AddUser'," + + $"异常信息:{ex.Message}," + + $"异常位置:{ex.StackTrace}" + ); + return new ApiResult { code = 1, msg = "保存用户信息失败" }; + } + finally + { + _logger.LogInformation($"新增用户参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}"); + } + return new ApiResult { code = 0, msg = "保存用户信息成功" }; + } + /// + /// + /// + /// + /// + public async Task GetUserList(UserQueryDto input) + { + try + { + #region 组织查询条件 + Expression> exp = filter => filter.IsDelete == false; + if (!string.IsNullOrEmpty(input.Username)) + { + exp = filter => filter.Username.Contains(input.Username); + } + if (!string.IsNullOrEmpty(input.Tel)) + { + exp = filter => filter.Tel.Contains(input.Tel); + } + if (!string.IsNullOrEmpty(input.HiredateStart)) + { + exp = filter => filter.Hiredate.Value >= DateTime.Parse(input.HiredateStart); + } + if (!string.IsNullOrEmpty(input.HiredateEnd)) + { + exp = filter => filter.Hiredate.Value <= DateTime.Parse(input.HiredateEnd); + } + #endregion + + #region 获取数据 + var result = await base.GetListWithExp(exp);//获取人员信息 + var roleList = await _roleService.GetRoleListByIds(result.Select(s => s.roleId).ToList()); + #endregion + #region 组装返回数据 + List list = new List(); + foreach (var item in result) + { + list.Add(new UserListDto + { + Username = item.Username, + Education = item.Education, + Tel = item.Tel, + Email = item.Email, + roleName = roleList.FirstOrDefault(s => s.Id == item.roleId)?.RoleName, + Sex = item.Sex == 0 ? "男" : "女", + Address = item.Address, + IsAdmin = item.IsAdmin == 0 ? "否" : "是", + Native = item.Native, + University = item.University, + Hiredate = item.Hiredate.HasValue == true ? item.Hiredate.Value.ToString("yyyy-MM-dd") : "", + Brithday = item.Brithday.HasValue == true ? item.Brithday.Value.ToString("yyyy-MM-dd") : "", + }); + } + #endregion + return new ApiResult { code = 0, data = list }; + } + catch (Exception ex) + { + _logger.LogError($"获取用户列表出现异常,请求参数:userQuery:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," + + $"请求接口:'api/User/GetUserList'," + + $"异常信息:{ex.Message}," + + $"异常位置:{ex.StackTrace}" + ); + return new ApiResult { code = 1, msg = "获取用户列表失败" }; + } + finally + { + _logger.LogInformation($"获取用户列表参数:userQuery:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}"); + } + } + #endregion } }