using Consul;
|
using FlexJobApi.Core;
|
using Furion.DatabaseAccessor;
|
using Furion.DataValidation;
|
using Furion.FriendlyException;
|
using Mapster;
|
using MediatR;
|
using Microsoft.CodeAnalysis;
|
using Microsoft.EntityFrameworkCore;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Text.RegularExpressions;
|
using System.Threading;
|
using System.Threading.Tasks;
|
|
namespace FlexJobApi.UserServer.Application
|
{
|
/// <summary>
|
/// 保存菜单
|
/// </summary>
|
public class SaveMenuCommandHandler(
|
IRepository<Menu> rep
|
) : IRequestHandler<SaveMenuCommand, Guid>
|
{
|
private readonly IRepository<Menu> rep = rep;
|
|
/// <inheritdoc/>
|
public async Task<Guid> Handle(SaveMenuCommand request, CancellationToken cancellationToken)
|
{
|
return await request.SaveData<Menu, SaveMenuCommand>(
|
q => q.Include(it => it.Children),
|
it =>
|
it.UserType == request.UserType
|
&& it.ClientType == request.ClientType
|
&& it.ParentId == request.ParentId
|
&& it.Code == request.Code
|
&& it.Id != request.Id,
|
(entity) =>
|
{
|
entity.Path = DbUtils.GetTreeDataPath<Menu>(request.ParentId, cancellationToken).Result;
|
if (request.Id.HasValue)
|
{
|
if (entity.UserType != request.UserType) throw Oops.Oh(EnumErrorCodeType.s410, "用户类型");
|
if (entity.ClientType != request.ClientType) throw Oops.Oh(EnumErrorCodeType.s410, "客户端类型");
|
DbUtils.UpdateTreeDataChildrenPath<DictionaryData>(
|
$"{entity.Path}/{entity.Code}/",
|
$"{entity.Path}/{request.Code}/",
|
cancellationToken).Wait();
|
}
|
request.Adapt(entity);
|
SaveChildrens(entity, request);
|
},
|
cancellationToken);
|
}
|
|
/// <summary>
|
/// 保存下级
|
/// </summary>
|
/// <param name="entity"></param>
|
/// <param name="request"></param>
|
/// <returns></returns>
|
private void SaveChildrens(Menu entity, SaveMenuCommand request)
|
{
|
// 获取子集Id
|
var childrenIds = GetRequestChildrenIds(request);
|
// 删除子级
|
entity.Children = entity.Children
|
.Where(it =>
|
it.Type == EnumMenuType.Menu
|
|| it.Type == EnumMenuType.Page
|
|| it.Type == EnumMenuType.Modal
|
|| childrenIds.Contains(it.Id))
|
.ToList();
|
|
// 遍历分组
|
|
foreach (var group in request.Groups)
|
{
|
// 遍历按钮位置
|
foreach (var buttonLocation in group.ButtonLocations)
|
{
|
// 遍历按钮 添加或更新
|
foreach (var button in buttonLocation.Buttons)
|
{
|
var buttonEntity = entity.Children.FirstOrDefault(it => it.Id == button.Id);
|
if (buttonEntity == null)
|
{
|
if (button.Id.HasValue) throw Oops.Oh(EnumErrorCodeType.s404, $"当前分组{group.Group}-位置{buttonLocation.Location}-按钮{button.Code}");
|
buttonEntity = new Menu
|
{
|
UserType = entity.UserType,
|
ClientType = entity.ClientType,
|
Type = EnumMenuType.Button,
|
VisitLevel = entity.VisitLevel,
|
};
|
entity.Children.Add(buttonEntity);
|
}
|
else if (buttonEntity.Type != EnumMenuType.Button) throw Oops.Oh(EnumErrorCodeType.s400, $"当前分组{group.Group}-字段{button.Code}并非一个按钮");
|
buttonEntity.Path = $"{entity.Path}{entity.Code}/";
|
buttonEntity.Group = group.Group;
|
buttonEntity.Location = buttonLocation.Location;
|
button.Adapt(buttonEntity);
|
}
|
}
|
|
// 遍历字段 添加或更新
|
foreach (var field in group.Fields)
|
{
|
var fieldEntity = entity.Children.FirstOrDefault(it => it.Id == field.Id);
|
if (fieldEntity == null)
|
{
|
if (field.Id.HasValue) throw Oops.Oh(EnumErrorCodeType.s404, $"当前分组{group.Group}-字段{field.Code}");
|
fieldEntity = new Menu
|
{
|
UserType = entity.UserType,
|
ClientType = entity.ClientType,
|
Type = EnumMenuType.Field,
|
VisitLevel = entity.VisitLevel,
|
};
|
entity.Children.Add(fieldEntity);
|
}
|
else if (fieldEntity.Type != EnumMenuType.Field) throw Oops.Oh(EnumErrorCodeType.s400, $"当前分组{group.Group}-位置{fieldEntity.Location}-按钮{fieldEntity.Code}并非一个字段");
|
fieldEntity.Path = $"{entity.Path}{entity.Code}/";
|
fieldEntity.Group = group.Group;
|
field.Adapt(fieldEntity);
|
}
|
}
|
CheckRepeatChildrens(entity);
|
}
|
|
/// <summary>
|
/// 校验重复子集
|
/// </summary>
|
/// <param name="entity"></param>
|
private void CheckRepeatChildrens(Menu entity)
|
{
|
var repeats = entity.Children
|
.GroupBy(it =>
|
{
|
return it.Type == EnumMenuType.Button
|
? $"{it.Group}:{it.Type}:{it.Location}:{it.Code}"
|
: $"{it.Group}:{it.Type}:{it.Code}";
|
})
|
.Select(it => new
|
{
|
it.Key,
|
Count = it.Count()
|
})
|
.Where(it => it.Count > 1)
|
.SplitJoin(it => it.Key, ",");
|
if (repeats.IsNotNull()) throw Oops.Oh(EnumErrorCodeType.s406, repeats);
|
}
|
|
/// <summary>
|
/// 获取请求子集Id
|
/// </summary>
|
/// <param name="request"></param>
|
/// <returns></returns>
|
private List<Guid> GetRequestChildrenIds(SaveMenuCommand request)
|
{
|
return request.Groups
|
.Select(g =>
|
g.ButtonLocations.SelectMany(bl => bl.Buttons.Where(b => b.Id.HasValue).Select(b => b.Id!.Value))
|
.Union(
|
g.Fields.Where(f => f.Id.HasValue).Select(f => f.Id!.Value)))
|
.SelectMany(it => it)
|
.ToList();
|
}
|
}
|
}
|