123 lines
3.7 KiB
C#
123 lines
3.7 KiB
C#
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 MenuGroupSeedDto
|
|
{
|
|
public string Name { get; set; }
|
|
}
|
|
|
|
public class MenuSeedDto
|
|
{
|
|
public string ParentCode { get; set; }
|
|
public string Code { get; set; }
|
|
public string DisplayName { get; set; }
|
|
public int Order { get; set; }
|
|
public string Url { get; set; }
|
|
public string Icon { get; set; }
|
|
public string RequiredPermissionName { get; set; }
|
|
public bool IsDisabled { get; set; }
|
|
public string ShortName { get; set; }
|
|
}
|
|
|
|
public class MenuSeederDto
|
|
{
|
|
public List<RouteSeedDto> Routes { get; set; }
|
|
public List<MenuSeedDto> Menus { get; set; }
|
|
public List<MenuGroupSeedDto> MenuGroups { get; set; }
|
|
}
|
|
|
|
public class MenuDataSeeder : IDataSeedContributor, ITransientDependency
|
|
{
|
|
private readonly IRepository<Route, Guid> _routeRepository;
|
|
private readonly IRepository<Menu, Guid> _menuRepository;
|
|
private readonly IRepository<MenuGroup, string> _menuGroupRepository;
|
|
|
|
public MenuDataSeeder(
|
|
IRepository<Route, Guid> routeRepository,
|
|
IRepository<Menu, Guid> menuRepository,
|
|
IRepository<MenuGroup, string> menuGroupRepository
|
|
)
|
|
{
|
|
_routeRepository = routeRepository;
|
|
_menuRepository = menuRepository;
|
|
_menuGroupRepository = menuGroupRepository;
|
|
}
|
|
|
|
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.MenuGroups)
|
|
{
|
|
var exists = await _menuGroupRepository.AnyAsync(x => x.Name == item.Name);
|
|
|
|
if (!exists)
|
|
{
|
|
await _menuGroupRepository.InsertAsync(new MenuGroup(item.Name)
|
|
{
|
|
Name = item.Name
|
|
});
|
|
}
|
|
}
|
|
|
|
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
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|