Merge branch 'framework' of https://gitee.com/ccnetcore/Yi into framework

This commit is contained in:
橙子
2023-02-16 23:07:49 +08:00
50 changed files with 920 additions and 221 deletions

View File

@@ -43,7 +43,11 @@ namespace Yi.Framework.Core.Sqlsugar.Repositories
{
return await _DbQueryable.Where(whereExpression).OrderByIF(orderBy is not null, orderBy + " " + orderByType.ToString().ToLower()).ToPageListAsync(page.PageNum, page.PageSize);
}
public async Task<List<T>> GetPageListAsync(List<IConditionalModel> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await _DbQueryable.Where(whereExpression).OrderByIF(orderBy is not null, orderBy + " " + orderByType.ToString().ToLower()).ToPageListAsync(page.PageNum, page.PageSize);
}
public async Task<bool> UpdateIgnoreNullAsync(T updateObj)
{

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Core.Enums;
namespace Yi.Framework.Core.Attributes
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
public class QueryParameterAttribute:Attribute
{
public QueryParameterAttribute()
{
}
public QueryParameterAttribute(QueryOperatorEnum queryOperatorEnum)
{
QueryOperator = queryOperatorEnum;
}
public QueryOperatorEnum QueryOperator { get; set; }
/// <summary>
/// 验证值为0时是否作为查询条件
/// true-作为查询条件 false-不作为查询条件
/// </summary>
public bool VerifyIsZero { get; set; } = false;
/// <summary>
///
/// </summary>
public string ColumnName { get; set; }
public ColumnTypeEnum ColumnType { get; set; }
}
public enum ColumnTypeEnum
{
datetime,
@bool
}
}

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Core.Enums
{
public enum QueryOperatorEnum
{
/// <summary>
/// 相等
/// </summary>
Equal,
/// <summary>
/// 匹配
/// </summary>
Like,
/// <summary>
/// 大于
/// </summary>
GreaterThan,
/// <summary>
/// 大于或等于
/// </summary>
GreaterThanOrEqual,
/// <summary>
/// 小于
/// </summary>
LessThan,
/// <summary>
/// 小于或等于
/// </summary>
LessThanOrEqual,
/// <summary>
/// 等于集合
/// </summary>
In,
/// <summary>
/// 不等于集合
/// </summary>
NotIn,
/// <summary>
/// 左边匹配
/// </summary>
LikeLeft,
/// <summary>
/// 右边匹配
/// </summary>
LikeRight,
/// <summary>
/// 不相等
/// </summary>
NoEqual,
/// <summary>
/// 为空或空
/// </summary>
IsNullOrEmpty,
/// <summary>
/// 不为空
/// </summary>
IsNot,
/// <summary>
/// 不匹配
/// </summary>
NoLike,
/// <summary>
/// 时间段 值用 "|" 隔开
/// </summary>
DateRange
}
}

View File

@@ -8,6 +8,6 @@ namespace Yi.Framework.Data.Entities
{
public interface IState
{
public bool? State { get; set; }
public bool State { get; set; }
}
}

View File

@@ -1,4 +1,5 @@
using System;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -14,5 +15,7 @@ namespace Yi.Framework.Ddd.Dtos
string? SortBy { get; set; }
OrderByEnum SortType { get; set; }
List<IConditionalModel> Conditions { get; set; }
}
}

View File

@@ -1,4 +1,5 @@
using System;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -13,5 +14,6 @@ namespace Yi.Framework.Ddd.Dtos
public int PageSize { get; set; } = int.MaxValue;
public string? SortBy { get; set; }
public OrderByEnum SortType { get; set; } = OrderByEnum.Desc;
public List<IConditionalModel> Conditions { get; set; }
}
}

View File

@@ -32,7 +32,8 @@ namespace Yi.Framework.Ddd.Repositories
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, Expression<Func<T, object>>? orderByExpression = null, OrderByEnum orderByType = OrderByEnum.Asc);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc);
Task<List<T>> GetPageListAsync(List<IConditionalModel> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc);
//插入
Task<bool> InsertAsync(T insertObj);

View File

@@ -6,6 +6,7 @@ using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Core.Attributes;
using Yi.Framework.Core.Helper;
using Yi.Framework.Core.Model;
using Yi.Framework.Ddd.Dtos;
@@ -84,38 +85,97 @@ where TEntityDto : IEntityDto<TKey>
/// <returns></returns>
public virtual async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
{
var totalCount = await _repository.CountAsync(_ => true);
var totalCount = -1;
var entities = new List<TEntity>();
var entityDtos = new List<TGetListOutputDto>();
if (totalCount > 0)
bool isPageList = true;
//这里还可以追加如果是审计日志,继续拼接条件即可
if (input is IPageTimeResultRequestDto timeInput)
{
//这里还可以追加如果是审计日志,继续拼接条件即可
if (input is IPageTimeResultRequestDto timeInput)
if (timeInput.StartTime is not null)
{
if (timeInput.StartTime is not null)
{
timeInput.EndTime = timeInput.EndTime ?? DateTime.Now;
}
timeInput.EndTime = timeInput.EndTime ?? DateTime.Now;
}
if (input is IPagedAndSortedResultRequestDto sortInput)
{
entities = await _repository.GetPageListAsync(_ => true, sortInput,sortInput.SortBy, sortInput.SortType);
}
else
{
entities = await _repository.GetListAsync();
}
entityDtos = await MapToGetListOutputDtosAsync(entities);
}
if (input is IPagedAndSortedResultRequestDto sortInput)
{
var dependsOnbuild = sortInput.GetType().GetCustomAttributes(typeof(QueryParameterAttribute), false).FirstOrDefault() as QueryParameterAttribute;
if (dependsOnbuild is null)
{
entities = await _repository.GetPageListAsync(_ => true, sortInput, sortInput.SortBy, sortInput.SortType);
}
else
{
sortInput.Conditions = new List<IConditionalModel>();
System.Reflection.PropertyInfo[] properties = sortInput.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo item in properties)
{
var query = item.GetCustomAttributes(typeof(QueryParameterAttribute), false).FirstOrDefault() as QueryParameterAttribute;
if (query is not null)
{
object value = item.GetValue(sortInput, null);
if (value is not null)
{
if (value.ToString() == "0")
{
if (query.VerifyIsZero)
{
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = (ConditionalType)(int)query.QueryOperator });
}
}
else {
switch (query.ColumnType)
{
case ColumnTypeEnum.datetime:
if (!string.IsNullOrEmpty(query.ColumnName))
{
DateTime dt = DateTime.Now;
DateTime.TryParse(value.ToString(), out dt);
sortInput.Conditions.Add(new ConditionalModel { FieldValue = dt.ToString("yyyy-MM-dd HH:mm:ss"), FieldName = query.ColumnName, ConditionalType = (ConditionalType)(int)query.QueryOperator });
}
else
{
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = (ConditionalType)(int)query.QueryOperator });
}
break;
case ColumnTypeEnum.@bool:
string _Value = "";
if ((bool)value)
{
_Value = "1";
}
else {
_Value = "0";
}
sortInput.Conditions.Add(new ConditionalModel { FieldValue = _Value, FieldName = item.Name, ConditionalType = (ConditionalType)(int)query.QueryOperator });
break;
default:
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = (ConditionalType)(int)query.QueryOperator });
break;
}
}
}
}
}
entities = await _repository.GetPageListAsync(sortInput.Conditions, sortInput, sortInput.SortBy, sortInput.SortType);
}
}
else
{
isPageList = false;
entities = await _repository.GetListAsync();
}
entityDtos = await MapToGetListOutputDtosAsync(entities);
//如果是分页查询,还需要统计数量
if (isPageList)
{
totalCount = await _repository.CountAsync(_ => true);
}
return new PagedResultDto<TGetListOutputDto>(
totalCount,
entityDtos

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.RBAC.Application.Contracts.Identity.Dtos.Account
{
public class RestPasswordDto
{
public string Password = string.Empty;
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.RBAC.Application.Contracts.Identity.Dtos.Account
{
public class UpdatePasswordDto
{
public string NewPassword { get; set; } = string.Empty;
public string OldPassword { get; set; } = string.Empty;
}
}

View File

@@ -3,16 +3,28 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Core.Enums;
using Yi.Framework.Ddd.Dtos;
namespace Yi.RBAC.Application.Contracts.Identity.Dtos
{
[QueryParameter]
public class DeptGetListInputVo : PagedAllResultRequestDto
{
[QueryParameter(QueryOperatorEnum.Equal)]
public long Id { get; set; }
[QueryParameter(QueryOperatorEnum.Equal, ColumnType = ColumnTypeEnum.@bool)]
public bool? State { get; set; }
[QueryParameter(QueryOperatorEnum.Like)]
public string? DeptName { get; set; }
public string? DeptCode { get; set; }
[QueryParameter(QueryOperatorEnum.Equal)]
public string? DeptCode { get; set; }
[QueryParameter(QueryOperatorEnum.Equal)]
public string? Leader { get; set; }
[QueryParameter(QueryOperatorEnum.GreaterThanOrEqual, ColumnName = "CreationTime",ColumnType =ColumnTypeEnum.datetime)]
public DateTime? StartTime { get; set; }
[QueryParameter(QueryOperatorEnum.LessThanOrEqual, ColumnName = "CreationTime", ColumnType = ColumnTypeEnum.datetime)]
public DateTime? EndTime { get; set; }
}
}

View File

@@ -10,13 +10,12 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
public class DeptGetOutputDto : IEntityDto<long>
{
public long Id { get; set; }
public DateTime CreationTime { get; set; } = DateTime.Now;
public long? CreatorId { get; set; }
public bool State { get; set; }
public string DeptName { get; set; }=string.Empty;
public string DeptCode { get; set; } = string.Empty;
public string? Leader { get; set; }
public long ParentId { get; set; }
public string? Remark { get; set; }
public long? deptId { get; set; }
}
}

View File

@@ -11,7 +11,6 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
{
public long Id { get; set; }
public DateTime CreationTime { get; set; } = DateTime.Now;
public long? CreatorId { get; set; }
public bool State { get; set; }
public string PostCode { get; set; }=string.Empty;
public string PostName { get; set; } = string.Empty;

View File

@@ -12,13 +12,16 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
/// </summary>
public class RoleCreateInputVo
{
public long Id { get; set; }
public DateTime CreationTime { get; set; } = DateTime.Now;
public long? CreatorId { get; set; }
public string? RoleName { get; set; }
public string? RoleCode { get; set; }
public string? Remark { get; set; }
public DataScopeEnum DataScope { get; set; } = DataScopeEnum.ALL;
public bool State { get; set; }
public bool State { get; set; } = true;
public int OrderNum { get; set; }
public List<long> DeptIds { get; set; }
public List<long> MenuIds { get; set; }
}
}

View File

@@ -18,5 +18,7 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
public string? Remark { get; set; }
public DataScopeEnum DataScope { get; set; } = DataScopeEnum.ALL;
public bool State { get; set; }
public int OrderNum { get; set; }
}
}

View File

@@ -18,5 +18,7 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
public string? Remark { get; set; }
public DataScopeEnum DataScope { get; set; } = DataScopeEnum.ALL;
public bool State { get; set; }
public int OrderNum { get; set; }
}
}

View File

@@ -9,13 +9,16 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
{
public class RoleUpdateInputVo
{
public long Id { get; set; }
public DateTime CreationTime { get; set; } = DateTime.Now;
public long? CreatorId { get; set; }
public string? RoleName { get; set; }
public string? RoleCode { get; set; }
public string? Remark { get; set; }
public DataScopeEnum DataScope { get; set; } = DataScopeEnum.ALL;
public bool State { get; set; }
public int OrderNum { get; set; }
public List<long> DeptIds { get; set; }
public List<long> MenuIds { get; set; }
}
}

View File

@@ -27,5 +27,6 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
public List<long>? RoleIds { get; set; }
public List<long>? PostIds { get; set; }
public long? DeptId { get; set; }
public bool State { get; set; } = true;
}
}

View File

@@ -14,8 +14,6 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
public string? Name { get; set; }
public int? Age { get; set; }
public string UserName { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string Salt { get; set; } = string.Empty;
public string? Icon { get; set; }
public string? Nick { get; set; }
public string? Email { get; set; }
@@ -25,10 +23,15 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
public string? Introduction { get; set; }
public string? Remark { get; set; }
public SexEnum Sex { get; set; } = SexEnum.Unknown;
public long? DeptId { get; set; }
public DateTime CreationTime { get; set; } = DateTime.Now;
public long? CreatorId { get; set; }
public bool State { get; set; }
public DateTime CreationTime { get; set; }
public long DeptId { get; set; }
public DeptGetOutputDto Dept { get; set; }
public List<PostGetListOutputDto> Posts { get; set; }
public List<RoleGetListOutputDto> Roles { get; set; }
}
}

View File

@@ -1,3 +1,4 @@
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -9,12 +10,12 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
{
public class UserUpdateInputVo
{
public long Id { get; set; }
public string? Name { get; set; }
public int? Age { get; set; }
public string UserName { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string Salt { get; set; } = string.Empty;
public string? UserName { get; set; }
[AdaptIgnore]
public string? Password { get; set; }
public string? Icon { get; set; }
public string? Nick { get; set; }
public string? Email { get; set; }
@@ -23,11 +24,11 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos
public long? Phone { get; set; }
public string? Introduction { get; set; }
public string? Remark { get; set; }
public SexEnum Sex { get; set; } = SexEnum.Unknown;
public SexEnum? Sex { get; set; }
public long? DeptId { get; set; }
public DateTime CreationTime { get; set; } = DateTime.Now;
public long? CreatorId { get; set; }
public List<long>? PostIds { get; set; }
public bool State { get; set; }
public List<long>? RoleIds { get; set; }
public bool? State { get; set; }
}
}

View File

@@ -58,6 +58,21 @@
</summary>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.AccountService.UpdatePasswordAsync(Yi.RBAC.Application.Contracts.Identity.Dtos.Account.UpdatePasswordDto)">
<summary>
更新密码
</summary>
<param name="input"></param>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.AccountService.RestPasswordAsync(System.Int64,Yi.RBAC.Application.Contracts.Identity.Dtos.Account.RestPasswordDto)">
<summary>
重置密码
</summary>
<param name="userId"></param>
<param name="input"></param>
<returns></returns>
</member>
<member name="T:Yi.RBAC.Application.Identity.DeptService">
<summary>
Dept服务实现
@@ -69,18 +84,18 @@
</summary>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.DeptService.GetListAsync(Yi.RBAC.Application.Contracts.Identity.Dtos.DeptGetListInputVo)">
<summary>
多查
</summary>
<param name="input"></param>
<returns></returns>
</member>
<member name="T:Yi.RBAC.Application.Identity.MenuService">
<summary>
Menu服务实现
</summary>
</member>
<member name="M:Yi.RBAC.Application.Identity.MenuService.GetListRoleIdAsync(System.Int64)">
<summary>
查询当前角色的菜单
</summary>
<param name="roleId"></param>
<returns></returns>
</member>
<member name="T:Yi.RBAC.Application.Identity.PostService">
<summary>
Post服务实现
@@ -91,6 +106,13 @@
Role服务实现
</summary>
</member>
<member name="M:Yi.RBAC.Application.Identity.RoleService.CreateAsync(Yi.RBAC.Application.Contracts.Identity.Dtos.RoleCreateInputVo)">
<summary>
添加角色
</summary>
<param name="input"></param>
<returns></returns>
</member>
<member name="T:Yi.RBAC.Application.Identity.UserService">
<summary>
User服务实现
@@ -111,5 +133,20 @@
<returns></returns>
<exception cref="T:Yi.Framework.Core.Exceptions.UserFriendlyException"></exception>
</member>
<member name="M:Yi.RBAC.Application.Identity.UserService.GetAsync(System.Int64)">
<summary>
单查
</summary>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.UserService.UpdateAsync(System.Int64,Yi.RBAC.Application.Contracts.Identity.Dtos.UserUpdateInputVo)">
<summary>
更新用户
</summary>
<param name="id"></param>
<param name="input"></param>
<returns></returns>
</member>
</members>
</doc>

View File

@@ -140,5 +140,37 @@ namespace Yi.RBAC.Application.Identity
var imgbyte = _securityCode.GetEnDigitalCodeByte(code);
return new CaptchaImageDto { Img = imgbyte, Uuid = code };
}
/// <summary>
/// 更新密码
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<bool> UpdatePasswordAsync(UpdatePasswordDto input)
{
if (input.OldPassword.Equals(input.NewPassword))
{
throw new UserFriendlyException("无效更新!输入的数据,新密码不能与老密码相同");
}
await _accountManager.UpdatePasswordAsync(_currentUser.Id, input.NewPassword, input.OldPassword);
return true;
}
/// <summary>
/// 重置密码
/// </summary>
/// <param name="userId"></param>
/// <param name="input"></param>
/// <returns></returns>
[HttpPut]
public async Task<bool> RestPasswordAsync(long userId, RestPasswordDto input)
{
if (!string.IsNullOrEmpty(input.Password))
{
throw new UserFriendlyException("重置密码不能为空!");
}
await _accountManager.RestPasswordAsync(userId, input.Password);
return true;
}
}
}

View File

@@ -28,24 +28,24 @@ namespace Yi.RBAC.Application.Identity
return await MapToGetListOutputDtosAsync(entities);
}
/// <summary>
/// 多查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<PagedResultDto<DeptGetListOutputDto>> GetListAsync(DeptGetListInputVo input)
{
RefAsync<int> total = 0;
var entities = await _DbQueryable
.WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
.WhereIF(input.State is not null, u => u.State == input.State)
.OrderBy(u => u.OrderNum, OrderByType.Asc)
.ToPageListAsync(input.PageNum, input.PageSize, total);
return new PagedResultDto<DeptGetListOutputDto>
{
Items = await MapToGetListOutputDtosAsync(entities),
Total = total
};
}
///// <summary>
///// 多查
///// </summary>
///// <param name="input"></param>
///// <returns></returns>
//public override async Task<PagedResultDto<DeptGetListOutputDto>> GetListAsync(DeptGetListInputVo input)
//{
// RefAsync<int> total = 0;
// var entities = await _DbQueryable
// .WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
// .WhereIF(input.State is not null, u => u.State == input.State)
// .OrderBy(u => u.OrderNum, OrderByType.Asc)
// .ToPageListAsync(input.PageNum, input.PageSize, total);
// return new PagedResultDto<DeptGetListOutputDto>
// {
// Items = await MapToGetListOutputDtosAsync(entities),
// Total = total
// };
//}
}
}

View File

@@ -3,6 +3,7 @@ using NET.AutoWebApi.Setting;
using Yi.RBAC.Application.Contracts.Identity.Dtos;
using Yi.RBAC.Domain.Identity.Entities;
using Yi.Framework.Ddd.Services;
using SqlSugar;
namespace Yi.RBAC.Application.Identity
{
@@ -13,5 +14,16 @@ namespace Yi.RBAC.Application.Identity
public class MenuService : CrudAppService<MenuEntity, MenuGetOutputDto, MenuGetListOutputDto, long, MenuGetListInputVo, MenuCreateInputVo, MenuUpdateInputVo>,
IMenuService, IAutoApiService
{
/// <summary>
/// 查询当前角色的菜单
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public async Task<List<MenuGetListOutputDto>> GetListRoleIdAsync(long roleId)
{
var entities= await _DbQueryable.Where(m => SqlFunc.Subqueryable<RoleMenuEntity>().Where(rm => rm.RoleId == roleId && rm.MenuId == m.Id).Any()).ToListAsync();
return await MapToGetListOutputDtosAsync(entities);
}
}
}

View File

@@ -3,6 +3,9 @@ using NET.AutoWebApi.Setting;
using Yi.RBAC.Application.Contracts.Identity.Dtos;
using Yi.RBAC.Domain.Identity.Entities;
using Yi.Framework.Ddd.Services;
using Yi.RBAC.Domain.Identity;
using Microsoft.AspNetCore.Identity;
using Yi.Framework.Uow;
namespace Yi.RBAC.Application.Identity
{
@@ -13,5 +16,30 @@ namespace Yi.RBAC.Application.Identity
public class RoleService : CrudAppService<RoleEntity, RoleGetOutputDto, RoleGetListOutputDto, long, RoleGetListInputVo, RoleCreateInputVo, RoleUpdateInputVo>,
IRoleService, IAutoApiService
{
[Autowired]
private RoleManager _roleManager { get; set; }
[Autowired]
private IUnitOfWorkManager _unitOfWorkManager { get; set; }
/// <summary>
/// 添加角色
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<RoleGetOutputDto> CreateAsync(RoleCreateInputVo input)
{
RoleGetOutputDto outputDto;
using (var uow = _unitOfWorkManager.CreateContext())
{
var entity = await MapToEntityAsync(input);
await _repository.InsertAsync(entity);
outputDto = await MapToGetOutputDtoAsync(entity);
await _roleManager.GiveRoleSetMenuAsync(new List<long> { entity.Id }, input.MenuIds);
uow.Commit();
}
return outputDto;
}
}
}

View File

@@ -8,6 +8,8 @@ using Yi.RBAC.Domain.Identity;
using Yi.Framework.Uow;
using Yi.Framework.Ddd.Dtos;
using Yi.RBAC.Domain.Identity.Repositories;
using SqlSugar;
using Mapster;
namespace Yi.RBAC.Application.Identity
{
@@ -36,7 +38,7 @@ namespace Yi.RBAC.Application.Identity
{
var entity = await MapToEntityAsync(input);
int total = 0;
RefAsync<int> total = 0;
var entities = await _DbQueryable.WhereIF(!string.IsNullOrEmpty(input.UserName), x => x.UserName.Contains(input.UserName!)).
WhereIF(input.Phone is not null, x => x.Phone.ToString()!.Contains(input.Phone.ToString()!)).
@@ -80,5 +82,47 @@ namespace Yi.RBAC.Application.Identity
return result;
}
}
/// <summary>
/// 单查
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override async Task<UserGetOutputDto> GetAsync(long id)
{
//使用导航树形查询
var entity = await _DbQueryable.Includes(u => u.Roles).Includes(u => u.Posts).Includes(u => u.Dept).InSingleAsync(id);
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 更新用户
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
public async override Task<UserGetOutputDto> UpdateAsync(long id, UserUpdateInputVo input)
{
if (await _repository.IsAnyAsync(u => input.UserName!.Equals(u.UserName) && !id.Equals(u.Id)))
{
throw new UserFriendlyException("用户已经在,更新失败");
}
var entity = await _repository.GetByIdAsync(id);
//更新密码,特殊处理
if (input.Password is not null)
{
entity.Password = input.Password;
entity.BuildPassword();
}
await MapToEntityAsync(input, entity);
using (var uow = _unitOfWorkManager.CreateContext())
{
var res1 = await _repository.UpdateAsync(entity);
await _userManager.GiveUserSetRoleAsync(new List<long> { id }, input.RoleIds);
await _userManager.GiveUserSetPostAsync(new List<long> { id }, input.PostIds);
uow.Commit();
}
return await MapToGetOutputDtoAsync(entity);
}
}
}

View File

@@ -0,0 +1,138 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Data.DataSeeds;
using Yi.Framework.Ddd.Repositories;
using Yi.RBAC.Domain.Dictionary.Entities;
using Yi.RBAC.Domain.Identity.Entities;
namespace Yi.RBAC.Domain.DataSeeds
{
[AppService(typeof(IDataSeed))]
public class DeptDataSeed : AbstractDataSeed<DeptEntity>
{
public DeptDataSeed(IRepository<DeptEntity> repository) : base(repository)
{
}
public override List<DeptEntity> GetSeedData()
{
var entities =new List<DeptEntity>();
DeptEntity chengziDept = new DeptEntity()
{
Id = SnowflakeHelper.NextId,
DeptName = "橙子科技",
DeptCode = "Yi",
OrderNum = 100,
IsDeleted = false,
ParentId = 0,
Leader = "橙子",
Remark = "如名所指"
};
entities.Add(chengziDept);
DeptEntity shenzhenDept = new DeptEntity()
{
Id = SnowflakeHelper.NextId,
DeptName = "深圳总公司",
OrderNum = 100,
IsDeleted = false,
ParentId = chengziDept.Id
};
entities.Add(shenzhenDept);
DeptEntity jiangxiDept = new DeptEntity()
{
Id = SnowflakeHelper.NextId,
DeptName = "江西总公司",
OrderNum = 100,
IsDeleted = false,
ParentId = chengziDept.Id
};
entities.Add(jiangxiDept);
DeptEntity szDept1 = new DeptEntity()
{
Id = SnowflakeHelper.NextId,
DeptName = "研发部门",
OrderNum = 100,
IsDeleted = false,
ParentId = shenzhenDept.Id
};
entities.Add(szDept1);
DeptEntity szDept2 = new DeptEntity()
{
Id = SnowflakeHelper.NextId,
DeptName = "市场部门",
OrderNum = 100,
IsDeleted = false,
ParentId = shenzhenDept.Id
};
entities.Add(szDept2);
DeptEntity szDept3 = new DeptEntity()
{
Id = SnowflakeHelper.NextId,
DeptName = "测试部门",
OrderNum = 100,
IsDeleted = false,
ParentId = shenzhenDept.Id
};
entities.Add(szDept3);
DeptEntity szDept4 = new DeptEntity()
{
Id = SnowflakeHelper.NextId,
DeptName = "财务部门",
OrderNum = 100,
IsDeleted = false,
ParentId = shenzhenDept.Id
};
entities.Add(szDept4);
DeptEntity szDept5 = new DeptEntity()
{
Id = SnowflakeHelper.NextId,
DeptName = "运维部门",
OrderNum = 100,
IsDeleted = false,
ParentId = shenzhenDept.Id
};
entities.Add(szDept5);
DeptEntity jxDept1 = new DeptEntity()
{
Id = SnowflakeHelper.NextId,
DeptName = "市场部门",
OrderNum = 100,
IsDeleted = false,
ParentId = jiangxiDept.Id
};
entities.Add(jxDept1);
DeptEntity jxDept2 = new DeptEntity()
{
Id = SnowflakeHelper.NextId,
DeptName = "财务部门",
OrderNum = 100,
IsDeleted = false,
ParentId = jiangxiDept.Id
};
entities.Add(jxDept2);
return entities;
}
}
}

View File

@@ -0,0 +1,69 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Data.DataSeeds;
using Yi.Framework.Ddd.Repositories;
using Yi.RBAC.Domain.Identity.Entities;
namespace Yi.RBAC.Domain.DataSeeds
{
[AppService(typeof(IDataSeed))]
public class PostDataSeed : AbstractDataSeed<PostEntity>
{
public PostDataSeed(IRepository<PostEntity> repository) : base(repository)
{
}
public override List<PostEntity> GetSeedData()
{
var entites=new List<PostEntity>();
PostEntity Post1 = new PostEntity()
{
Id = SnowflakeHelper.NextId,
PostName = "董事长",
PostCode = "ceo",
OrderNum = 100,
IsDeleted = false
};
entites.Add(Post1);
PostEntity Post2 = new PostEntity()
{
Id = SnowflakeHelper.NextId,
PostName = "项目经理",
PostCode = "se",
OrderNum = 100,
IsDeleted = false
};
entites.Add(Post2);
PostEntity Post3 = new PostEntity()
{
Id = SnowflakeHelper.NextId,
PostName = "人力资源",
PostCode = "hr",
OrderNum = 100,
IsDeleted = false
};
entites.Add(Post3);
PostEntity Post4 = new PostEntity()
{
Id = SnowflakeHelper.NextId,
PostName = "普通员工",
PostCode = "user",
OrderNum = 100,
IsDeleted = false
};
entites.Add(Post4);
return entites;
}
}
}

View File

@@ -0,0 +1,51 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Data.DataSeeds;
using Yi.Framework.Ddd.Repositories;
using Yi.RBAC.Domain.Identity.Entities;
using Yi.RBAC.Domain.Shared.Identity.EnumClasses;
namespace Yi.RBAC.Domain.DataSeeds
{
[AppService(typeof(IDataSeed))]
public class RoleDataSeed : AbstractDataSeed<RoleEntity>
{
public RoleDataSeed(IRepository<RoleEntity> repository) : base(repository)
{
}
public override List<RoleEntity> GetSeedData()
{
var entities = new List<RoleEntity>();
RoleEntity role1 = new RoleEntity()
{
Id = SnowflakeHelper.NextId,
RoleName = "管理员",
RoleCode = "admin",
DataScope = DataScopeEnum.ALL,
OrderNum = 999,
Remark = "管理员",
IsDeleted = false
};
entities.Add(role1);
RoleEntity role2 = new RoleEntity()
{
Id = SnowflakeHelper.NextId,
RoleName = "测试角色",
RoleCode = "test",
DataScope = DataScopeEnum.ALL,
OrderNum = 1,
Remark = "测试用的角色",
IsDeleted = false
};
entities.Add(role2);
return entities;
}
}
}

View File

@@ -33,7 +33,7 @@ namespace Yi.RBAC.Domain.Dictionary.Entities
/// <summary>
/// 状态
/// </summary>
public bool? State { get; set; } = true;
public bool State { get; set; } = true;
/// <summary>
/// 描述

View File

@@ -669,6 +669,14 @@
<param name="userId"></param>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Domain.Identity.RoleManager.GiveRoleSetMenuAsync(System.Collections.Generic.List{System.Int64},System.Collections.Generic.List{System.Int64})">
<summary>
给角色设置菜单
</summary>
<param name="roleIds"></param>
<param name="menuIds"></param>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Domain.Identity.UserManager.GiveUserSetRoleAsync(System.Collections.Generic.List{System.Int64},System.Collections.Generic.List{System.Int64})">
<summary>
给用户设置角色

View File

@@ -1,4 +1,5 @@
using System;
using Mapster;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@@ -103,6 +104,30 @@ namespace Yi.RBAC.Domain.Identity
return claims;
}
public async Task UpdatePasswordAsync(long userId, string newPassword, string oldPassword)
{
var user = await _repository.GetByIdAsync(userId);
if (!user.JudgePassword(oldPassword))
{
throw new UserFriendlyException("无效更新!新密码不能与老密码相同");
}
user.Password = newPassword;
user.BuildPassword();
await _repository.UpdateAsync(user);
}
public async Task<bool> RestPasswordAsync(long userId, string password)
{
var user = await _repository.GetByIdAsync(userId);
user.Id = userId;
user.Password = password;
user.BuildPassword();
return await _repository.UpdateAsync(user);
}
}
}

View File

@@ -54,7 +54,7 @@ namespace Yi.RBAC.Domain.Identity.Entities
/// <summary>
/// 状态
/// </summary>
public bool? State { get; set; }
public bool State { get; set; } = true;
/// <summary>
/// 部门名称

View File

@@ -56,7 +56,7 @@ namespace Yi.RBAC.Domain.Identity.Entities
/// <summary>
/// 状态
/// </summary>
public bool? State { get; set; }
public bool State { get; set; }
/// <summary>
/// 菜单名

View File

@@ -55,7 +55,7 @@ namespace Yi.RBAC.Domain.Identity.Entities
/// <summary>
/// 状态
/// </summary>
public bool? State { get; set; }
public bool State { get; set; }=true;
/// <summary>
/// 岗位编码

View File

@@ -79,7 +79,7 @@ namespace Yi.RBAC.Domain.Identity.Entities
/// <summary>
/// 状态
/// </summary>
public bool? State { get; set; }
public bool State { get; set; }=true;
[Navigate(typeof(RoleMenuEntity), nameof(RoleMenuEntity.RoleId), nameof(RoleMenuEntity.MenuId))]

View File

@@ -133,7 +133,7 @@ namespace Yi.RBAC.Domain.Identity.Entities
/// <summary>
/// 状态
/// </summary>
public bool? State { get; set; } = true;
public bool State { get; set; } = true;
/// <summary>

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Ddd.Repositories;
using Yi.RBAC.Domain.Identity.Entities;
namespace Yi.RBAC.Domain.Identity
{
[AppService]
public class RoleManager
{
private IRepository<RoleEntity> _repository;
private IRepository<RoleMenuEntity> _roleMenuRepository;
public RoleManager(IRepository<RoleEntity> repository, IRepository<RoleMenuEntity> roleMenuRepository)
{
_repository = repository;
_roleMenuRepository = roleMenuRepository;
}
/// <summary>
/// 给角色设置菜单
/// </summary>
/// <param name="roleIds"></param>
/// <param name="menuIds"></param>
/// <returns></returns>
public async Task GiveRoleSetMenuAsync(List<long> roleIds, List<long> menuIds)
{
//这个是需要事务的在service中进行工作单元
await _roleMenuRepository.DeleteAsync(u => roleIds.Contains(u.RoleId));
//遍历用户
foreach (var roleId in roleIds)
{
//添加新的关系
List<RoleMenuEntity> roleMenuEntity = new();
foreach (var menu in menuIds)
{
roleMenuEntity.Add(new RoleMenuEntity() {Id=SnowflakeHelper.NextId, RoleId = roleId, MenuId = menu });
}
//一次性批量添加
await _roleMenuRepository.InsertRangeAsync(roleMenuEntity);
}
}
}
}