refactor:完成重构设备的添加,删除,更新。
This commit is contained in:
@@ -29,20 +29,6 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有菜单树结构。
|
||||
/// </summary>
|
||||
/// <returns>包含所有菜单树结构的列表。</returns>
|
||||
public async Task<List<MenuBean>> GetMenuTreesAsync()
|
||||
{
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var dbMenuTree = await _dbContext.GetInstance().Queryable<DbMenu>()
|
||||
.ToTreeAsync(dm => dm.Childrens, dm => dm.ParentId, 0);
|
||||
stopwatch.Stop();
|
||||
_logger.LogInformation($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return _mapper.Map<List<MenuBean>>(dbMenuTree);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID获取单个菜单。
|
||||
@@ -88,64 +74,21 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
|
||||
/// </summary>
|
||||
/// <param name="entity">要删除的菜单实体。</param>
|
||||
/// <returns>受影响的行数。</returns>
|
||||
public async Task<int> DeleteAsync(MenuBean entity) => await base.DeleteAsync(new List<DbMenu> { _mapper.Map<DbMenu>(entity) });
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据菜单ID删除菜单树(包括子菜单)。
|
||||
/// </summary>
|
||||
/// <param name="id">要删除菜单树的根菜单ID。</param>
|
||||
/// <returns>受影响的行数。</returns>
|
||||
public async Task<int> DeleteMenuTreeByIdAsync(int id)
|
||||
public async Task<int> DeleteAsync(MenuBean entity)
|
||||
{
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
int delConut = 0;
|
||||
var childList = await _dbContext.GetInstance().Queryable<DbMenu>()
|
||||
.ToChildListAsync(c => c.ParentId, id);
|
||||
delConut = await _dbContext.GetInstance().Deleteable<DbMenu>(childList)
|
||||
.ExecuteCommandAsync();
|
||||
delConut += await _dbContext.GetInstance().Deleteable<DbMenu>()
|
||||
.Where(m => m.Id == id)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
_logger.LogInformation($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return delConut;
|
||||
var menuToDeleteList =new List<MenuBean>();
|
||||
foreach (var item in entity.Children)
|
||||
{
|
||||
menuToDeleteList.Add(item);
|
||||
foreach (var menu in item.Children)
|
||||
{
|
||||
menuToDeleteList.Add(menu);
|
||||
}
|
||||
}
|
||||
menuToDeleteList.Add(entity);
|
||||
return await base.DeleteAsync( _mapper.Map<List<DbMenu>>(menuToDeleteList) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据菜单类型和目标ID删除菜单树。
|
||||
/// </summary>
|
||||
/// <param name="menuType">菜单类型。</param>
|
||||
/// <param name="targetId">目标ID。</param>
|
||||
/// <returns>受影响的行数。</returns>
|
||||
public async Task<int> DeleteMenuTreeByTargetIdAsync(MenuType menuType, int targetId)
|
||||
{
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var menu = await _dbContext.GetInstance().Queryable<DbMenu>().FirstAsync(m => m.MenuType == menuType && m.TargetId == targetId);
|
||||
if (menu == null) return 0;
|
||||
var childList = await _dbContext.GetInstance().Queryable<DbMenu>()
|
||||
.ToChildListAsync(c => c.ParentId, menu.Id);
|
||||
var delConut = await _dbContext.GetInstance().Deleteable<DbMenu>(childList)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
_logger.LogInformation($"Delete {typeof(DbMenu)},TargetId={targetId},耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return delConut;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据菜单类型和目标ID获取菜单。
|
||||
/// </summary>
|
||||
/// <param name="menuType">菜单类型。</param>
|
||||
/// <param name="targetId">目标ID。</param>
|
||||
/// <returns>对应的菜单实体,如果不存在则为null。</returns>
|
||||
public async Task<MenuBean> GetMenuByTargetIdAsync(MenuType menuType, int targetId)
|
||||
{
|
||||
var dbMenu = await _dbContext.GetInstance().Queryable<DbMenu>().FirstAsync(m => m.MenuType == menuType && m.TargetId == targetId);
|
||||
return _mapper.Map<MenuBean>(dbMenu);
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<MenuBean>> AddAsync(List<MenuBean> entities)
|
||||
@@ -165,7 +108,7 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
|
||||
var dbEntities = _mapper.Map<List<DbMenu>>(entities);
|
||||
return await base.UpdateAsync(dbEntities);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据实体列表批量删除菜单。
|
||||
@@ -174,6 +117,8 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
|
||||
/// <returns>受影响的行数。</returns>
|
||||
public async Task<int> DeleteAsync(List<MenuBean> entities)
|
||||
{
|
||||
var deleteList = entities.SelectMany(c => c.Children).ToList();
|
||||
deleteList.AddRange(entities);
|
||||
var dbEntities = _mapper.Map<List<DbMenu>>(entities);
|
||||
return await base.DeleteAsync(dbEntities);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user