using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Routing; using Syc.Basic.Web.Dtos.Menu; using Syc.Basic.Web.WMS; //using Syc.Basic.Web.Contracts; using Syc.Basic.Web.WMS.Dtos.Menu; using Syc.Basic.Web.WMS.Entitys; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; namespace Syc.Basic.Web.Service { /// /// 菜单服务 /// public class MenuService : ApiService { private readonly IRepository menuRepository; private readonly IRepository roleRepository; public MenuService(IRepository menuRepository, IRepository roleRepository) { this.menuRepository = menuRepository; this.roleRepository = roleRepository; } /// /// 获取菜单列表 /// /// public async Task GetListAsync() { var list = await menuRepository.GetListAsync(); var result = ObjectMapper.Map(list, new List()); return new { Data = result, total = result.Count, }; } /// /// 删除菜单 /// /// public async Task Del(DelMenuInput input) { var menu = await menuRepository.FirstOrDefaultAsync(e => e.Id == input.Id); if (menu is null) throw Oops.Oh("菜单不存在或已被删除"); await menuRepository.DeleteAsync(menu); } /// /// 添加 /// /// /// public async Task Add(UpdateMenuInput input) { if (await menuRepository.AnyAsync(e => e.Name == input.name.Trim())) throw Oops.Oh($"{input.name}已存在"); if (!input.permissionCode.IsNullOrWhiteSpace()) { if (await menuRepository.AnyAsync(e => e.PermissionCode == input.permissionCode.Trim())) throw Oops.Oh("权限标识已存在,请勿重复定义"); } var admins = await roleRepository.GetListAsync(e => e.Code == ConstPool.AdministratorRoleCode); var menu = ObjectMapper.Map(input, new Menu()); menu.Roles.AddRange(admins); await menuRepository.InsertAsync(menu); } /// /// 编辑 /// /// /// public async Task Edit(UpdateMenuInput input) { if (await menuRepository.AnyAsync(e => e.Name == input.name.Trim() && e.Id != input.id)) throw Oops.Oh($"{input.name}已存在"); //var menu = await menuRepository.FirstOrDefaultAsync(e => e.Id == input.id); var newMenu = ObjectMapper.Map(input, new Menu()); await menuRepository.UpdateAsync(newMenu); } } }