using FlexJobApi.Core;
|
using Furion.DatabaseAccessor;
|
using Furion.FriendlyException;
|
using Mapster;
|
using MediatR;
|
using Microsoft.EntityFrameworkCore;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace FlexJobApi.User.Application
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public class SaveMenuFieldCommandHandler(
|
IRepository<Menu> rep
|
) : IRequestHandler<SaveMenuFieldCommand, Guid>
|
{
|
private readonly IRepository<Menu> rep = rep;
|
|
/// <inheritdoc/>
|
public async Task<Guid> Handle(SaveMenuFieldCommand request, CancellationToken cancellationToken)
|
{
|
if (request.Id.HasValue)
|
{
|
var entity = await rep.FirstOrDefaultAsync(it => it.Id == it.Id);
|
if (entity.ParentId != request.ParentId) throw Oops.Oh(EnumErrorCodeType.s410, "上级Id");
|
request.Adapt(entity);
|
if (await CheckExist(entity)) throw Oops.Oh(EnumErrorCodeType.s405, "菜单编号");
|
await rep.UpdateAsync(entity);
|
return entity.Id;
|
}
|
else
|
{
|
var entity = new Menu();
|
request.Adapt(entity);
|
if (await CheckExist(entity)) throw Oops.Oh(EnumErrorCodeType.s405, "菜单编号");
|
await rep.InsertAsync(entity);
|
return entity.Id;
|
}
|
}
|
|
/// <summary>
|
/// 校验菜单是否重复
|
/// </summary>
|
/// <param name="entity"></param>
|
/// <returns></returns>
|
private async Task<bool> CheckExist(Menu entity)
|
{
|
return await rep.AsQueryable().AsNoTracking()
|
.AnyAsync(it =>
|
it.ParentId == entity.ParentId
|
&& it.Group == entity.Group
|
&& it.Code == entity.Code
|
&& it.Id != entity.Id);
|
}
|
|
}
|
}
|