282 lines
12 KiB
C#
282 lines
12 KiB
C#
|
|
using System;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Microsoft.Extensions.Configuration;
|
|||
|
|
using System.Text.Json;
|
|||
|
|
using Volo.Abp.Domain.Services;
|
|||
|
|
using Volo.Abp.Domain.Repositories;
|
|||
|
|
using Kurs.Platform.Entities;
|
|||
|
|
|
|||
|
|
namespace Kurs.Platform.Branchs;
|
|||
|
|
|
|||
|
|
public class BranchSeedManager : DomainService
|
|||
|
|
{
|
|||
|
|
private readonly IRepository<Branch, Guid> _branchRepository;
|
|||
|
|
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 BranchSeedManager(
|
|||
|
|
IRepository<Branch, Guid> branchRepository,
|
|||
|
|
IRepository<RegistrationType, Guid> registrationTypeRepository,
|
|||
|
|
IRepository<RegistrationMethod, Guid> registrationMethodRepository,
|
|||
|
|
IRepository<ClassType, Guid> classTypeRepository,
|
|||
|
|
IRepository<Class, Guid> classRepository,
|
|||
|
|
IRepository<Level, Guid> levelRepository,
|
|||
|
|
IRepository<LessonPeriod, Guid> lessonPeriodRepository,
|
|||
|
|
IRepository<Schedule, Guid> scheduleRepository)
|
|||
|
|
{
|
|||
|
|
_branchRepository = branchRepository;
|
|||
|
|
_registrationTypeRepository = registrationTypeRepository;
|
|||
|
|
_registrationMethodRepository = registrationMethodRepository;
|
|||
|
|
_classTypeRepository = classTypeRepository;
|
|||
|
|
_classRepository = classRepository;
|
|||
|
|
_levelRepository = levelRepository;
|
|||
|
|
_lessonPeriodRepository = lessonPeriodRepository;
|
|||
|
|
_scheduleRepository = scheduleRepository;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<BranchSeedResult> SeedRecordsAsync(Guid? tenantId, Guid branchId)
|
|||
|
|
{
|
|||
|
|
var result = new BranchSeedResult();
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var assemblyPath = Path.GetDirectoryName(typeof(BranchSeedManager).Assembly.Location)!;
|
|||
|
|
var basePath = Path.Combine(assemblyPath, "Branch", "Seeds");
|
|||
|
|
|
|||
|
|
var configuration = new ConfigurationBuilder()
|
|||
|
|
.SetBasePath(basePath)
|
|||
|
|
.AddJsonFile("BranchData.json")
|
|||
|
|
.AddJsonFile($"BranchData.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? ""}.json", optional: true)
|
|||
|
|
.Build();
|
|||
|
|
|
|||
|
|
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
|||
|
|
var items = configuration.Get<BranchSeederDto>();
|
|||
|
|
|
|||
|
|
if (items == null)
|
|||
|
|
{
|
|||
|
|
result.Success = false;
|
|||
|
|
result.Message = "BranchData.json okunamadı veya format hatalı.";
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Yardımcı fonksiyon: kolay log ekleme
|
|||
|
|
BranchSeedDetail CreateLog(string entity) =>
|
|||
|
|
new()
|
|||
|
|
{
|
|||
|
|
EntityName = entity,
|
|||
|
|
InsertedCount = 0,
|
|||
|
|
InsertedItems = []
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
|
|||
|
|
var registrationTypeLog = CreateLog(nameof(RegistrationType));
|
|||
|
|
foreach (var item in items.RegistrationTypes)
|
|||
|
|
{
|
|||
|
|
var exists = await _registrationTypeRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
|
|||
|
|
|
|||
|
|
if (!exists)
|
|||
|
|
{
|
|||
|
|
await _registrationTypeRepository.InsertAsync(new RegistrationType
|
|||
|
|
{
|
|||
|
|
TenantId = tenantId,
|
|||
|
|
BranchId = branchId,
|
|||
|
|
Name = item.Name,
|
|||
|
|
Status = item.Status
|
|||
|
|
}, autoSave: true);
|
|||
|
|
|
|||
|
|
registrationTypeLog.InsertedCount++;
|
|||
|
|
registrationTypeLog.InsertedItems.Add(item.Name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
result.Details.Add(registrationTypeLog);
|
|||
|
|
|
|||
|
|
var registrationMethodLog = CreateLog(nameof(RegistrationMethod));
|
|||
|
|
foreach (var item in items.RegistrationMethods)
|
|||
|
|
{
|
|||
|
|
var exists = await _registrationMethodRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
|
|||
|
|
|
|||
|
|
if (!exists)
|
|||
|
|
{
|
|||
|
|
var registrationType = await _registrationTypeRepository.FirstOrDefaultAsync(x => x.Name == item.RegistrationTypeName);
|
|||
|
|
if (registrationType != null)
|
|||
|
|
{
|
|||
|
|
await _registrationMethodRepository.InsertAsync(new RegistrationMethod
|
|||
|
|
{
|
|||
|
|
TenantId = tenantId,
|
|||
|
|
BranchId = branchId,
|
|||
|
|
RegistrationTypeId = registrationType.Id,
|
|||
|
|
Name = item.Name,
|
|||
|
|
Status = item.Status
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
registrationMethodLog.InsertedCount++;
|
|||
|
|
registrationMethodLog.InsertedItems.Add(item.Name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
result.Details.Add(registrationMethodLog);
|
|||
|
|
|
|||
|
|
var classTypeLog = CreateLog(nameof(ClassType));
|
|||
|
|
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 && x.BranchId == branchId);
|
|||
|
|
if (registrationType != null)
|
|||
|
|
{
|
|||
|
|
await _classTypeRepository.InsertAsync(new ClassType
|
|||
|
|
{
|
|||
|
|
TenantId = tenantId,
|
|||
|
|
BranchId = branchId,
|
|||
|
|
RegistrationTypeId = registrationType.Id,
|
|||
|
|
Name = item.Name,
|
|||
|
|
MinStudentCount = item.MinStudentCount,
|
|||
|
|
MaxStudentCount = item.MaxStudentCount,
|
|||
|
|
Status = item.Status
|
|||
|
|
}, autoSave: true);
|
|||
|
|
|
|||
|
|
classTypeLog.InsertedCount++;
|
|||
|
|
classTypeLog.InsertedItems.Add(item.Name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
result.Details.Add(classTypeLog);
|
|||
|
|
|
|||
|
|
var classLog = CreateLog(nameof(Class));
|
|||
|
|
foreach (var item in items.Classes)
|
|||
|
|
{
|
|||
|
|
var exists = await _classRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
|
|||
|
|
|
|||
|
|
if (!exists)
|
|||
|
|
{
|
|||
|
|
var classType = await _classTypeRepository.FirstOrDefaultAsync(x => x.Name == item.ClassTypeName);
|
|||
|
|
if (classType != null)
|
|||
|
|
{
|
|||
|
|
await _classRepository.InsertAsync(new()
|
|||
|
|
{
|
|||
|
|
TenantId = tenantId,
|
|||
|
|
BranchId = branchId,
|
|||
|
|
ClassTypeId = classType.Id,
|
|||
|
|
Name = item.Name,
|
|||
|
|
Status = item.Status,
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
classLog.InsertedCount++;
|
|||
|
|
classLog.InsertedItems.Add(item.Name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
result.Details.Add(classLog);
|
|||
|
|
|
|||
|
|
var levelLog = CreateLog(nameof(Level));
|
|||
|
|
foreach (var item in items.Levels)
|
|||
|
|
{
|
|||
|
|
var exists = await _levelRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
|
|||
|
|
|
|||
|
|
if (!exists)
|
|||
|
|
{
|
|||
|
|
var classType = await _classTypeRepository.FirstOrDefaultAsync(x => x.Name == item.ClassTypeName);
|
|||
|
|
if (classType != null)
|
|||
|
|
{
|
|||
|
|
await _levelRepository.InsertAsync(new Level
|
|||
|
|
{
|
|||
|
|
TenantId = tenantId,
|
|||
|
|
BranchId = branchId,
|
|||
|
|
ClassTypeId = classType.Id,
|
|||
|
|
Name = item.Name,
|
|||
|
|
LevelType = item.LevelType,
|
|||
|
|
LessonCount = item.LessonCount,
|
|||
|
|
Status = item.Status,
|
|||
|
|
LessonDuration = item.LessonDuration,
|
|||
|
|
MonthlyPaymentRate = item.MonthlyPaymentRate
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
levelLog.InsertedCount++;
|
|||
|
|
levelLog.InsertedItems.Add(item.Name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
result.Details.Add(levelLog);
|
|||
|
|
|
|||
|
|
var lessonPeriodLog = CreateLog(nameof(LessonPeriod));
|
|||
|
|
foreach (var item in items.LessonPeriods)
|
|||
|
|
{
|
|||
|
|
var exists = await _lessonPeriodRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
|
|||
|
|
|
|||
|
|
if (!exists)
|
|||
|
|
{
|
|||
|
|
await _lessonPeriodRepository.InsertAsync(new()
|
|||
|
|
{
|
|||
|
|
TenantId = tenantId,
|
|||
|
|
BranchId = branchId,
|
|||
|
|
Name = item.Name,
|
|||
|
|
Day = item.Day,
|
|||
|
|
Lesson1 = item.Lesson1,
|
|||
|
|
Lesson2 = item.Lesson2,
|
|||
|
|
Lesson3 = item.Lesson3,
|
|||
|
|
Lesson4 = item.Lesson4,
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
lessonPeriodLog.InsertedCount++;
|
|||
|
|
lessonPeriodLog.InsertedItems.Add(item.Name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
result.Details.Add(lessonPeriodLog);
|
|||
|
|
|
|||
|
|
var scheduleLog = CreateLog(nameof(Schedule));
|
|||
|
|
foreach (var item in items.Schedules)
|
|||
|
|
{
|
|||
|
|
var exists = await _scheduleRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
|
|||
|
|
|
|||
|
|
if (!exists)
|
|||
|
|
{
|
|||
|
|
await _scheduleRepository.InsertAsync(new()
|
|||
|
|
{
|
|||
|
|
TenantId = tenantId,
|
|||
|
|
BranchId = branchId,
|
|||
|
|
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,
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
scheduleLog.InsertedCount++;
|
|||
|
|
scheduleLog.InsertedItems.Add(item.Name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
result.Details.Add(scheduleLog);
|
|||
|
|
|
|||
|
|
result.Success = true;
|
|||
|
|
result.Message = $"Seed işlemi başarıyla tamamlandı. Toplam {result.TotalInsertedCount} kayıt eklendi.";
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
result.Success = false;
|
|||
|
|
result.Message = $"Hata: {ex.Message}";
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|