221 lines
8.5 KiB
C#
221 lines
8.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using Kurs.Platform.Entities;
|
|
using Kurs.Platform.Seeds;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Volo.Abp.Data;
|
|
using Volo.Abp.DependencyInjection;
|
|
using Volo.Abp.Domain.Repositories;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Kurs.Languages.Entities;
|
|
using Kurs.Settings.Entities;
|
|
|
|
namespace Kurs.Platform.Data.Seeds;
|
|
|
|
public class PlatformBranchDataSeeder : ITransientDependency
|
|
{
|
|
private readonly IRepository<Language, Guid> _languages;
|
|
private readonly IRepository<LanguageKey, Guid> _languageKey;
|
|
private readonly IRepository<LanguageText, Guid> _languagesText;
|
|
private readonly IRepository<DataSource, Guid> _dataSources;
|
|
private readonly IRepository<SettingDefinition, Guid> _settings;
|
|
private readonly IRepository<RegistrationType, Guid> _registrationTypeRepository;
|
|
private readonly IRepository<RegistrationMethod, Guid> _registrationMethodRepository;
|
|
private readonly IRepository<ClassType, Guid> _classTypeRepository;
|
|
private readonly IRepository<Class, Guid> _classRepository;
|
|
private readonly IRepository<Level, Guid> _levelRepository;
|
|
private readonly IRepository<LessonPeriod, Guid> _lessonPeriodRepository;
|
|
private readonly IRepository<Schedule, Guid> _scheduleRepository;
|
|
|
|
public PlatformBranchDataSeeder(
|
|
IRepository<Language, Guid> languages,
|
|
IRepository<LanguageKey, Guid> languageKey,
|
|
IRepository<LanguageText, Guid> languagesText,
|
|
IRepository<DataSource, Guid> dataSource,
|
|
IRepository<SettingDefinition, Guid> settings,
|
|
IRepository<Schedule, Guid> scheduleRepository,
|
|
IRepository<LessonPeriod, Guid> lessonPeriodRepository,
|
|
IRepository<RegistrationType, Guid> registrationTypeRepository,
|
|
IRepository<RegistrationMethod, Guid> registrationMethodRepository,
|
|
IRepository<ClassType, Guid> classTypeRepository,
|
|
IRepository<Class, Guid> classRepository,
|
|
IRepository<Level, Guid> levelRepository
|
|
)
|
|
{
|
|
_languages = languages;
|
|
_languageKey = languageKey;
|
|
_languagesText = languagesText;
|
|
_dataSources = dataSource;
|
|
_settings = settings;
|
|
_registrationTypeRepository = registrationTypeRepository;
|
|
_registrationMethodRepository = registrationMethodRepository;
|
|
_classTypeRepository = classTypeRepository;
|
|
_classRepository = classRepository;
|
|
_levelRepository = levelRepository;
|
|
_lessonPeriodRepository = lessonPeriodRepository;
|
|
_scheduleRepository = scheduleRepository;
|
|
}
|
|
|
|
public async Task SeedAsync(DataSeedContext context)
|
|
{
|
|
var settings = await _settings.GetListAsync();
|
|
var dataSources = await _dataSources.GetListAsync();
|
|
var languages = await _languages.GetListAsync();
|
|
var keys = await _languageKey.GetListAsync();
|
|
var texts = await _languagesText.GetListAsync();
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile(Path.Combine("Seeds", "BranchData.json"))
|
|
.AddJsonFile(Path.Combine("Seeds", $"BranchData.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? ""}.json"), true)
|
|
.Build();
|
|
var items = configuration.Get<BranchSeederDto>();
|
|
|
|
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
|
|
|
foreach (var item in items.RegistrationTypes)
|
|
{
|
|
var exists = await _registrationTypeRepository.AnyAsync(x => x.Name == item.Name);
|
|
|
|
if (!exists)
|
|
{
|
|
await _registrationTypeRepository.InsertAsync(new RegistrationType
|
|
{
|
|
Name = item.Name,
|
|
Status = item.Status
|
|
}, autoSave: true);
|
|
}
|
|
}
|
|
|
|
foreach (var item in items.RegistrationMethods)
|
|
{
|
|
var exists = await _registrationMethodRepository.AnyAsync(x => x.Name == item.Name);
|
|
|
|
if (!exists)
|
|
{
|
|
var registrationType = await _registrationTypeRepository.FirstOrDefaultAsync(x => x.Name == item.RegistrationTypeName);
|
|
if (registrationType != null)
|
|
{
|
|
await _registrationMethodRepository.InsertAsync(new RegistrationMethod
|
|
{
|
|
RegistrationTypeId = registrationType.Id,
|
|
Name = item.Name,
|
|
Status = item.Status
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var item in items.ClassTypes)
|
|
{
|
|
var exists = await _classTypeRepository.AnyAsync(x => x.Name == item.Name);
|
|
|
|
if (!exists)
|
|
{
|
|
var registrationType = await _registrationTypeRepository.FirstOrDefaultAsync(x => x.Name == item.RegistrationTypeName);
|
|
if (registrationType != null)
|
|
{
|
|
await _classTypeRepository.InsertAsync(new ClassType
|
|
{
|
|
RegistrationTypeId = registrationType.Id,
|
|
Name = item.Name,
|
|
MinStudentCount = item.MinStudentCount,
|
|
MaxStudentCount = item.MaxStudentCount,
|
|
Status = item.Status
|
|
}, autoSave: true);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var item in items.Classes)
|
|
{
|
|
var exists = await _classRepository.AnyAsync(x => x.Name == item.Name);
|
|
|
|
if (!exists)
|
|
{
|
|
var classType = await _classTypeRepository.FirstOrDefaultAsync(x => x.Name == item.ClassTypeName);
|
|
if (classType != null)
|
|
{
|
|
await _classRepository.InsertAsync(new()
|
|
{
|
|
ClassTypeId = classType.Id,
|
|
Name = item.Name,
|
|
Status = item.Status,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var item in items.Levels)
|
|
{
|
|
var exists = await _levelRepository.AnyAsync(x => x.Name == item.Name);
|
|
|
|
if (!exists)
|
|
{
|
|
var classType = await _classTypeRepository.FirstOrDefaultAsync(x => x.Name == item.ClassTypeName);
|
|
if (classType != null)
|
|
{
|
|
await _levelRepository.InsertAsync(new Level
|
|
{
|
|
ClassTypeId = classType.Id,
|
|
Name = item.Name,
|
|
LevelType = item.LevelType,
|
|
LessonCount = item.LessonCount,
|
|
Status = item.Status,
|
|
LessonDuration = item.LessonDuration,
|
|
MonthlyPaymentRate = item.MonthlyPaymentRate
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var item in items.LessonPeriods)
|
|
{
|
|
var exists = await _lessonPeriodRepository.AnyAsync(x => x.Name == item.Name);
|
|
|
|
if (!exists)
|
|
{
|
|
await _lessonPeriodRepository.InsertAsync(new()
|
|
{
|
|
Name = item.Name,
|
|
Day = item.Day,
|
|
Lesson1 = item.Lesson1,
|
|
Lesson2 = item.Lesson2,
|
|
Lesson3 = item.Lesson3,
|
|
Lesson4 = item.Lesson4,
|
|
});
|
|
}
|
|
}
|
|
|
|
foreach (var item in items.Schedules)
|
|
{
|
|
var exists = await _scheduleRepository.AnyAsync(x => x.Name == item.Name);
|
|
|
|
if (!exists)
|
|
{
|
|
await _scheduleRepository.InsertAsync(new()
|
|
{
|
|
Name = item.Name,
|
|
Status = item.Status,
|
|
StartTime = item.StartTime,
|
|
EndTime = item.EndTime,
|
|
LessonMinute = item.LessonMinute,
|
|
LessonBreakMinute = item.LessonBreakMinute,
|
|
LessonCount = item.LessonCount,
|
|
LunchTime = item.LunchTime,
|
|
LunchMinute = item.LunchMinute,
|
|
IncludeLunch = item.IncludeLunch,
|
|
Monday = item.Monday,
|
|
Tuesday = item.Tuesday,
|
|
Wednesday = item.Wednesday,
|
|
Thursday = item.Thursday,
|
|
Friday = item.Friday,
|
|
Saturday = item.Saturday,
|
|
Sunday = item.Sunday,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|