using BPA.SAAS.Manage.Application.System.Dtos;
using BPA.SAAS.Manage.Application.System.Interface;
using BPA.SAAS.Manage.Comm.Const;
using BPA.SAAS.Manage.Core.Org;
using BPA.SAAS.Manage.Core.System;
using Furion.LinqBuilder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BPA.SAAS.Manage.Application.System.Services
{
public class MenuService : IMenuService, ITransient
{
private readonly ISqlSugarClient _db;
public MenuService(ISqlSugarClient db)
{
_db = db;
}
///
/// 分页查询
///
///
///
public async Task> GetMenuTreePage(MenuTreeSearchDtoInput input)
{
if (App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value == null)
return null;
var AllMenus =await _db.Queryable().Where(t => t.IsDeleted == 0).OrderBy(t => t.Sort).ToListAsync();
var query = AllMenus.Where(t => t.Pid == "" || t.Pid == null);
if (!input.Name.IsNullOrEmpty())
query = query.Where(t => t.Name.Contains(input.Name));
if (!input.Code.IsNullOrEmpty())
query = query.Where(t => t.Code.Contains(input.Code));
var data = query.Select(t => new MenuTreeDtoOutput
{
Id = t.Id,
Name = t.Name,
CreateAt = t.CreateAt,
Code = t.Code,
Icon = t.Icon,
Router = t.Router,
Pid = t.Pid,
Sort = t.Sort,
Component = t.Component,
Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeDtoOutput
{
Id = x.Id,
Name = x.Name,
CreateAt = x.CreateAt,
Code = x.Code,
Pid = x.Pid,
Icon = x.Icon,
Router = x.Router,
Sort = x.Sort,
Component = x.Component,
Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeDtoOutput
{
Id = y.Id.ToString(),
Name = y.Name,
CreateAt = y.CreateAt,
Code = y.Code,
Icon = y.Icon,
Pid = y.Pid.ToString(),
Router = y.Router,
Sort = y.Sort,
Component = y.Component
}).ToList()
}).ToList()
}).ToList();
return data;
}
///
/// 新增菜单
///
///
///
public async Task Add(MenuDtoInput input)
{
var menu = input.Adapt();
menu.Id= Guid.NewGuid().ToString();
menu.CreateAt= DateTime.Now;
menu.CreateBy= App.User?.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
return await _db.Insertable(menu).ExecuteCommandAsync()> 0;
}
///
/// 编辑菜单
///
///
///
public async Task Update(MenuDtoInput input)
{
var menu = input.Adapt();
menu.CreateAt = DateTime.Now;
menu.CreateBy = App.User?.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
return await _db.Updateable(menu).ExecuteCommandAsync()>0;
}
///
/// 删除菜单
///
///
///
public async Task Delete(List input)
{
try
{
_db.Ado.BeginTran();
// 查询数据库中是否存在未删除的活动信息
var resEntitites = _db.Queryable().In(input).ToList();
resEntitites.ForEach(x =>
{
x.IsDeleted = 1;
});
var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
_db.Ado.CommitTran();
return res>0;
}
catch (Exception)
{
_db.Ado.RollbackTran();
return false;
}
}
///
/// 查询菜单树
///
///
///
public async Task> MenuTree(string GetType)
{
if (!string.IsNullOrWhiteSpace(GetType) && GetType.ToLower() == "user")
{
var userId = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
var GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
if (string.IsNullOrWhiteSpace(GroupId) || string.IsNullOrWhiteSpace(userId))
{
return null;
}
List dataRoles =await _db.Queryable().Where(a => a.GroupId == GroupId).ToListAsync();
if (dataRoles.Count() <= 0)
{
return null;
}
var sysRoleId = dataRoles.Select(a => a.Id).ToArray();
var menus = _db.Queryable().Where(t => sysRoleId.Contains(t.SysRoleId))
.Select(x => x.SysMenuId).ToArray();
var AllMenus = _db.Queryable().Where(t => t.IsDeleted == 0 && menus.Contains(t.Id)).OrderBy(t => t.Sort).ToList();
var data = AllMenus.Where(t => t.Pid == null || t.Pid == "").Select(t => new MenuTreeOutput
{
Key = t.Id,
Title = t.Name,
value = t.Name,
Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
{
Key = x.Id,
Title = x.Name,
value = x.Name,
Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
{
Key = y.Id,
Title = y.Name,
value = y.Name,
}).ToList()
}).ToList()
}).ToList();
return data;
}
else
{
var AllMenus = _db.Queryable().Where(t => t.IsDeleted == 0).OrderBy(t => t.Sort).ToList();
var data = AllMenus.Where(t => t.Pid == null || t.Pid == "").Select(t => new MenuTreeOutput
{
Key = t.Id,
Title = t.Name,
value = t.Id,
Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
{
Key = x.Id,
Title = x.Name,
value = x.Id,
Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
{
Key = y.Id,
Title = y.Name,
value = y.Name,
}).ToList()
}).ToList()
}).ToList();
return data;
}
}
}
}