//
namespace SafeCampus.System;
///
///
///
public class ButtonService : DbRepository, IButtonService
{
private readonly ILogger _logger;
private readonly IResourceService _resourceService;
private readonly IRelationService _relationService;
public ButtonService(ILogger logger, IResourceService resourceService, IRelationService relationService)
{
_logger = logger;
_resourceService = resourceService;
_relationService = relationService;
}
///
public async Task> Page(ButtonPageInput input)
{
var query = Context.Queryable().Where(it => it.ParentId == input.ParentId && it.Category == CateGoryConst.RESOURCE_BUTTON)
.WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Title.Contains(input.SearchKey) || it.Path.Contains(input.SearchKey))//根据关键字查询
.OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}").OrderBy(it => it.SortCode);//排序
var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
return pageInfo;
}
///
public async Task Add(ButtonAddInput input)
{
await CheckInput(input);//检查参数
var sysResource = input.Adapt();//实体转换
if (await InsertAsync(sysResource))//插入数据
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//刷新缓存
}
///
public async Task> AddBatch(ButtonAddInput input)
{
var sysResources = new List();//按钮列表
var codeList = new List { "Add", "Edit", "Delete", "BatchDelete", "Import", "Export", "BatchEdit" };//code后缀
var titleList = new List { "新增", "编辑", "删除", "批量删除", "导入", "导出", "批量编辑" };//title前缀
var idList = new List();//Id列表
for (var i = 0; i < codeList.Count; i++)
{
var id = CommonUtils.GetSingleId();
sysResources.Add(new SysResource
{
Id = id,
Title = titleList[i] + input.Title,//标题等于前缀输入的值
Code = input.Code + codeList[i],//code等于输入的值加后缀
ParentId = input.ParentId,
SortCode = i + 1
});
idList.Add(id);
}
//遍历列表
foreach (var sysResource in sysResources)
{
await CheckInput(sysResource);//检查按钮参数
}
//添加到数据库
if (await InsertRangeAsync(sysResources))//插入数据
{
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//刷新缓存
return sysResources.Select(it => it.Id).ToList();
}
return new List();
}
///
public async Task Edit(ButtonEditInput input)
{
await CheckInput(input);//检查参数
var sysResource = input.Adapt();//实体转换
//事务
var result = await Tenant.UseTranAsync(async () =>
{
await UpdateAsync(sysResource);//更新按钮
});
if (result.IsSuccess)//如果成功了
{
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//资源表按钮刷新缓存
}
else
{
//写日志
_logger.LogError(result.ErrorMessage, result.ErrorException);
throw Oops.Oh(ErrorCodeEnum.A0002);
}
}
///
public async Task Delete(BaseIdListInput input)
{
//获取所有ID
var ids = input.Ids;
//获取所有按钮集合
var buttonList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_BUTTON);
#region 处理关系表角色资源信息
//获取所有菜单集合
var menuList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_MENU);
//获取按钮的父菜单id集合
var parentIds = buttonList.Where(it => ids.Contains(it.Id)).Select(it => it.ParentId.Value.ToString()).ToList();
//获取关系表分类为SYS_ROLE_HAS_RESOURCE数据
var roleResources = await _relationService.GetRelationByCategory(CateGoryConst.RELATION_SYS_ROLE_HAS_RESOURCE);
//获取相关关系表数据
var relationList = roleResources.Where(it => parentIds.Contains(it.TargetId))//目标ID是父ID中
.Where(it => it.ExtJson != null).ToList();//扩展信息不为空
//遍历关系表
relationList.ForEach(it =>
{
var relationRoleResuorce = it.ExtJson.ToJsonEntity();//拓展信息转实体
var buttonInfo = relationRoleResuorce.ButtonInfo;//获取按钮信息
if (buttonInfo.Count > 0)
{
// 使用 LINQ 查询找出交集的补集(即不同元素)
var diffArr = buttonInfo.Except(ids).Union(ids.Except(buttonInfo)).ToList();
relationRoleResuorce.ButtonInfo = diffArr;//重新赋值按钮信息
it.ExtJson = relationRoleResuorce.ToJson();//重新赋值拓展信息
}
});
#endregion 处理关系表角色资源信息
//事务
var result = await Tenant.UseTranAsync(async () =>
{
await DeleteByIdsAsync(ids.Cast