sunpengfei
2025-08-11 2b93d154c57c8456a730361d635a0ff88dc8dfe8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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.UserServer.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)
        {
            var parent = await rep.FirstOrDefaultAsync(it => it.Id == request.ParentId);
            if (parent == null) throw Oops.Oh(EnumErrorCodeType.s404, "上级菜单");
            return await request.SaveData<Menu, SaveMenuFieldCommand>(
                null,
                it =>
                    it.ParentId == request.ParentId
                    && it.Type == EnumMenuType.Field
                    && it.Group == request.Group
                    && it.Code == request.Code
                    && it.Id != request.Id,
                (entity) =>
                {
                    if (request.Id.HasValue)
                    {
                        if (entity.ParentId != request.ParentId) throw Oops.Oh(EnumErrorCodeType.s410, "上级Id");
                    }
                    else
                    {
                        entity.Path = $"{parent.Path}{parent.Code}/";
                        entity.Type = EnumMenuType.Field;
                        entity.VisitLevel = parent.VisitLevel;
                    }
                    request.Adapt(entity);
                },
                cancellationToken);
        }
    }
}