sozsoft-platform/api/src/Sozsoft.Platform.DbMigrator/Seeds/MenuDataSeeder.cs

89 lines
2.6 KiB
C#
Raw Normal View History

2026-03-04 13:47:25 +00:00
using System;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic;
using Sozsoft.Platform.Entities;
using Microsoft.Extensions.Configuration;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
namespace Sozsoft.Platform.Data.Seeds;
public class RouteSeedDto
{
public string Key { get; set; }
public string Path { get; set; }
public string ComponentPath { get; set; }
public string RouteType { get; set; }
public string[] Authority { get; set; }
}
public class MenuSeederDto
{
public List<RouteSeedDto> Routes { get; set; }
public List<MenuSeedDto> Menus { get; set; }
}
public class MenuDataSeeder : IDataSeedContributor, ITransientDependency
{
private readonly IRepository<Route, Guid> _routeRepository;
private readonly IRepository<Menu, Guid> _menuRepository;
public MenuDataSeeder(
IRepository<Route, Guid> routeRepository,
IRepository<Menu, Guid> menuRepository
)
{
_routeRepository = routeRepository;
_menuRepository = menuRepository;
}
public async Task SeedAsync(DataSeedContext context)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(Path.Combine("Seeds", "MenusData.json"))
.Build();
var items = configuration.Get<MenuSeederDto>();
foreach (var item in items.Routes)
{
var exists = await _routeRepository.AnyAsync(x => x.Key == item.Key);
if (!exists)
{
await _routeRepository.InsertAsync(new Route(
item.Key,
item.Path,
item.ComponentPath,
item.RouteType,
item.Authority ?? []
));
}
}
foreach (var item in items.Menus)
{
var exists = await _menuRepository.AnyAsync(x => x.Code == item.Code);
if (!exists)
{
await _menuRepository.InsertAsync(new Menu
{
ParentCode = string.IsNullOrWhiteSpace(item.ParentCode) ? null : item.ParentCode,
Code = item.Code,
DisplayName = item.DisplayName,
Order = item.Order,
Url = item.Url,
Icon = item.Icon,
RequiredPermissionName = item.RequiredPermissionName,
IsDisabled = item.IsDisabled,
ShortName = item.ShortName
});
}
}
}
}