2025-07-21 14:35:17 +08:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using DMS.Core.Interfaces;
|
|
|
|
|
|
using DMS.Core.Models;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
using DMS.Application.Interfaces.Database;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
using DMS.Application.Interfaces;
|
|
|
|
|
|
|
2025-10-05 17:07:17 +08:00
|
|
|
|
namespace DMS.Application.Services.Database;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// 菜单应用服务,负责处理菜单相关的业务逻辑。
|
2025-10-05 17:07:17 +08:00
|
|
|
|
/// 实现 <see cref="IMenuAppService"/> 接口。
|
2025-07-21 14:35:17 +08:00
|
|
|
|
/// </summary>
|
2025-10-05 17:07:17 +08:00
|
|
|
|
public class MenuAppService : IMenuAppService
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepositoryManager _repoManager;
|
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="repoManager">仓储管理器实例。</param>
|
|
|
|
|
|
/// <param name="mapper">AutoMapper 实例。</param>
|
2025-10-05 17:07:17 +08:00
|
|
|
|
public MenuAppService(IRepositoryManager repoManager, IMapper mapper)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
_repoManager = repoManager;
|
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
|
/// 异步根据ID获取菜单。
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">菜单ID。</param>
|
2025-10-13 20:20:09 +08:00
|
|
|
|
/// <returns>菜单对象。</returns>
|
|
|
|
|
|
public async Task<MenuBean> GetMenuByIdAsync(int id)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
var menu = await _repoManager.Menus.GetByIdAsync(id);
|
2025-10-13 20:20:09 +08:00
|
|
|
|
return _mapper.Map<MenuBean>(menu);
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
|
/// 异步获取所有菜单列表。
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// </summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
|
/// <returns>菜单列表。</returns>
|
|
|
|
|
|
public async Task<List<MenuBean>> GetAllMenusAsync()
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
var menus = await _repoManager.Menus.GetAllAsync();
|
2025-10-13 20:20:09 +08:00
|
|
|
|
return _mapper.Map<List<MenuBean>>(menus);
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步创建一个新菜单(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
|
/// <param name="menu">要创建的菜单。</param>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <returns>新创建菜单的ID。</returns>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果创建菜单时发生错误。</exception>
|
2025-10-13 20:20:09 +08:00
|
|
|
|
public async Task<int> CreateMenuAsync(MenuBean menu)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.Menus.AddAsync(menu);
|
|
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
return menu.Id;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.RollbackAsync();
|
|
|
|
|
|
throw new ApplicationException("创建菜单时发生错误,操作已回滚。", ex);
|
|
|
|
|
|
}
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步更新一个已存在的菜单(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
|
/// <param name="menu">要更新的菜单。</param>
|
2025-10-05 17:07:17 +08:00
|
|
|
|
/// <returns>受影响的行数。</returns>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <exception cref="ApplicationException">如果找不到菜单或更新菜单时发生错误。</exception>
|
2025-10-13 20:20:09 +08:00
|
|
|
|
public async Task<int> UpdateMenuAsync(MenuBean menu)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-10-13 20:20:09 +08:00
|
|
|
|
var dbmenu = await _repoManager.Menus.GetByIdAsync(menu.Id);
|
|
|
|
|
|
if (dbmenu == null)
|
2025-07-21 18:49:49 +08:00
|
|
|
|
{
|
2025-10-13 20:20:09 +08:00
|
|
|
|
throw new ApplicationException($"Menu with ID {menu.Id} not found.");
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
2025-10-13 20:20:09 +08:00
|
|
|
|
_mapper.Map(menu, dbmenu);
|
|
|
|
|
|
int res = await _repoManager.Menus.UpdateAsync(dbmenu);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
2025-10-05 17:07:17 +08:00
|
|
|
|
return res;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.RollbackAsync();
|
|
|
|
|
|
throw new ApplicationException("更新菜单时发生错误,操作已回滚。", ex);
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步删除一个菜单(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">要删除菜单的ID。</param>
|
2025-10-05 17:07:17 +08:00
|
|
|
|
/// <returns>如果删除成功则为 true,否则为 false。</returns>
|
|
|
|
|
|
/// <exception cref="InvalidOperationException">如果删除菜单失败。</exception>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果删除菜单时发生其他错误。</exception>
|
|
|
|
|
|
public async Task<bool> DeleteMenuAsync(int id)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-10-05 17:07:17 +08:00
|
|
|
|
var delRes = await _repoManager.Menus.DeleteByIdAsync(id);
|
|
|
|
|
|
if (delRes == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException($"删除菜单失败:菜单ID:{id},请检查菜单Id是否存在");
|
|
|
|
|
|
}
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
2025-10-05 17:07:17 +08:00
|
|
|
|
return true;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.RollbackAsync();
|
|
|
|
|
|
throw new ApplicationException("删除菜单时发生错误,操作已回滚。", ex);
|
|
|
|
|
|
}
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|