96 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			96 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
| 
								 | 
							
								using langguanApi.Extensions;
							 | 
						|||
| 
								 | 
							
								using langguanApi.Extensions.AutoDI;
							 | 
						|||
| 
								 | 
							
								using langguanApi.Model;
							 | 
						|||
| 
								 | 
							
								using langguanApi.Model.Entity;
							 | 
						|||
| 
								 | 
							
								using Mapster;
							 | 
						|||
| 
								 | 
							
								using NPOI.SS.Formula.Functions;
							 | 
						|||
| 
								 | 
							
								using System.Linq.Expressions;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								namespace langguanApi.Service
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
								    [ServiceInjection(InjectionType.Transient)]
							 | 
						|||
| 
								 | 
							
								    public class RoleService : BaseService<Role>
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        private readonly RoleMenuServie _roleMenuServie;
							 | 
						|||
| 
								 | 
							
								        public RoleService(IConfiguration config, RoleMenuServie roleMenuServie) : base(config, nameof(Role))
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            _roleMenuServie = roleMenuServie;
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 新增角色
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        /// <param name="role"></param>
							 | 
						|||
| 
								 | 
							
								        /// <returns></returns>
							 | 
						|||
| 
								 | 
							
								        public async Task<ApiResult> Add(AddRoleDto role)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            if (await base.Exist(filter => filter.RoleName == role.RoleName))
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                return new ApiResult() { code = 1, msg = "角色名称已存在" };
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            var entity = role.Adapt<Role>();
							 | 
						|||
| 
								 | 
							
								            await base.CreateAsync(entity);
							 | 
						|||
| 
								 | 
							
								            return new ApiResult() { code = 0 };
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 获取角色及菜单
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        /// <param name="roleId"></param>
							 | 
						|||
| 
								 | 
							
								        /// <returns></returns>
							 | 
						|||
| 
								 | 
							
								        public async Task<ApiResult> GetRoleAndMenu(string roleId)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            var role = await base.GetAsync(roleId);
							 | 
						|||
| 
								 | 
							
								            if (role != null)
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                var menus = await _roleMenuServie.GetByRoleId(roleId);
							 | 
						|||
| 
								 | 
							
								                return new ApiResult() { code = 0, data = new { role, menus } };
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            return new ApiResult() { code = 0 };
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 删除角色
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        /// <param name="ids"></param>
							 | 
						|||
| 
								 | 
							
								        /// <returns></returns>
							 | 
						|||
| 
								 | 
							
								        public async Task<ApiResult> Remove(IEnumerable<string> ids)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            if (ids.Any())
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                foreach (var item in ids)
							 | 
						|||
| 
								 | 
							
								                {
							 | 
						|||
| 
								 | 
							
								                    await base.RemoveAsync(item);
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            return new ApiResult() { code = 0 };
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 更新角色
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        /// <param name="role"></param>
							 | 
						|||
| 
								 | 
							
								        /// <returns></returns>
							 | 
						|||
| 
								 | 
							
								        public async Task<ApiResult> update(UpdateRoleDto role)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            var entity = role.Adapt<Role>();
							 | 
						|||
| 
								 | 
							
								            await base.UpdateAsync(entity.Id, entity);
							 | 
						|||
| 
								 | 
							
								            return new ApiResult() { code = 0 };
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 分页取数据
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        /// <param name="input"></param>
							 | 
						|||
| 
								 | 
							
								        /// <returns></returns>
							 | 
						|||
| 
								 | 
							
								        public async Task<object> GetPage(reqpage input)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            Expression<Func<Role, bool>> exp = filter => filter.IsDelete == false;
							 | 
						|||
| 
								 | 
							
								            if (!string.IsNullOrEmpty(input.key))
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                exp = exp.And(filter => filter.RoleName.Contains(input.key));
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            return await base.GetPager(new ReqPaing()
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                pageSize = input.pageSize,
							 | 
						|||
| 
								 | 
							
								                current = input.current
							 | 
						|||
| 
								 | 
							
								            }, exp);
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 |