511 lines
22 KiB
C#
511 lines
22 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 Erp.Platform.Entities;
|
||
|
||
namespace Erp.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;
|
||
private readonly IRepository<Meal, Guid> _mealRepository;
|
||
private readonly IRepository<Bank, Guid> _bankRepository;
|
||
private readonly IRepository<Cash, Guid> _cashRepository;
|
||
private readonly IRepository<CurrentAccount, Guid> _currentAccountRepository;
|
||
|
||
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,
|
||
IRepository<Meal, Guid> mealRepository,
|
||
IRepository<Bank, Guid> bankRepository,
|
||
IRepository<Cash, Guid> cashRepository,
|
||
IRepository<CurrentAccount, Guid> currentAccountRepository
|
||
)
|
||
{
|
||
_branchRepository = branchRepository;
|
||
_registrationTypeRepository = registrationTypeRepository;
|
||
_registrationMethodRepository = registrationMethodRepository;
|
||
_classTypeRepository = classTypeRepository;
|
||
_classRepository = classRepository;
|
||
_levelRepository = levelRepository;
|
||
_lessonPeriodRepository = lessonPeriodRepository;
|
||
_scheduleRepository = scheduleRepository;
|
||
_mealRepository = mealRepository;
|
||
_bankRepository = bankRepository;
|
||
_cashRepository = cashRepository;
|
||
_currentAccountRepository = currentAccountRepository;
|
||
}
|
||
|
||
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 = []
|
||
};
|
||
|
||
if (items.RegistrationTypes.Count > 0)
|
||
{
|
||
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(
|
||
$"{{ " +
|
||
$"\"name\": \"{item.Name}\", " +
|
||
$"\"status\": \"{item.Status}\" " +
|
||
$"}}"
|
||
);
|
||
}
|
||
}
|
||
result.Details.Add(registrationTypeLog);
|
||
}
|
||
|
||
if (items.RegistrationMethods.Count > 0)
|
||
{
|
||
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(
|
||
$"{{ " +
|
||
$"\"name\": \"{item.Name}\", " +
|
||
$"\"status\": \"{item.Status}\", " +
|
||
$"\"registrationType\": \"{item.RegistrationTypeName}\" " +
|
||
$"}}"
|
||
);
|
||
}
|
||
}
|
||
}
|
||
result.Details.Add(registrationMethodLog);
|
||
}
|
||
|
||
if (items.ClassTypes.Count > 0)
|
||
{
|
||
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(
|
||
$"{{ " +
|
||
$"\"name\": \"{item.Name}\", " +
|
||
$"\"status\": \"{item.Status}\", " +
|
||
$"\"registrationType\": \"{item.RegistrationTypeName}\", " +
|
||
$"\"minStudentCount\": {item.MinStudentCount}, " +
|
||
$"\"maxStudentCount\": {item.MaxStudentCount} " +
|
||
$"}}"
|
||
);
|
||
}
|
||
}
|
||
}
|
||
result.Details.Add(classTypeLog);
|
||
}
|
||
|
||
if (items.Classes.Count > 0)
|
||
{
|
||
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(
|
||
$"{{ " +
|
||
$"\"name\": \"{item.Name}\", " +
|
||
$"\"status\": \"{item.Status}\", " +
|
||
$"\"classType\": \"{item.ClassTypeName}\" " +
|
||
$"}}"
|
||
);
|
||
}
|
||
}
|
||
}
|
||
result.Details.Add(classLog);
|
||
}
|
||
|
||
if (items.Levels.Count > 0)
|
||
{
|
||
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,
|
||
LevelType = item.LevelType,
|
||
Name = item.Name,
|
||
Order = item.Order,
|
||
LessonCount = item.LessonCount,
|
||
LessonDuration = item.LessonDuration,
|
||
Status = item.Status,
|
||
MonthlyPaymentRate = item.MonthlyPaymentRate
|
||
});
|
||
|
||
levelLog.InsertedCount++;
|
||
levelLog.InsertedItems.Add(
|
||
$"{{ " +
|
||
$"\"name\": \"{item.Name}\", " +
|
||
$"\"status\": \"{item.Status}\", " +
|
||
$"\"classType\": \"{item.ClassTypeName}\", " +
|
||
$"\"levelType\": \"{item.LevelType}\", " +
|
||
$"\"order\": {item.Order}, " +
|
||
$"\"lessonCount\": {item.LessonCount}, " +
|
||
$"\"lessonDuration\": {item.LessonDuration}, " +
|
||
$"\"monthlyPaymentRate\": {item.MonthlyPaymentRate} " +
|
||
$"}}"
|
||
);
|
||
}
|
||
}
|
||
}
|
||
result.Details.Add(levelLog);
|
||
}
|
||
|
||
if (items.LessonPeriods.Count > 0)
|
||
{
|
||
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(
|
||
$"{{ " +
|
||
$"\"name\": \"{item.Name}\", " +
|
||
$"\"day\": \"{item.Day}\", " +
|
||
$"\"lesson1\": \"{item.Lesson1}\", " +
|
||
$"\"lesson2\": \"{item.Lesson2}\", " +
|
||
$"\"lesson3\": \"{item.Lesson3}\", " +
|
||
$"\"lesson4\": \"{item.Lesson4}\" " +
|
||
$"}}"
|
||
);
|
||
}
|
||
}
|
||
result.Details.Add(lessonPeriodLog);
|
||
}
|
||
|
||
if (items.Schedules.Count > 0)
|
||
{
|
||
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(
|
||
$"{{ \"name\": \"{item.Name}\", " +
|
||
$"\"status\": \"{item.Status}\", " +
|
||
$"\"startTime\": \"{item.StartTime:HH\\:mm}\", " +
|
||
$"\"endTime\": \"{item.EndTime:HH\\:mm}\", " +
|
||
$"\"lessonMinute\": {item.LessonMinute}, " +
|
||
$"\"lessonBreakMinute\": {item.LessonBreakMinute}, " +
|
||
$"\"lessonCount\": {item.LessonCount}, " +
|
||
$"\"lunchTime\": \"{item.LunchTime:HH\\:mm}\", " +
|
||
$"\"lunchMinute\": {item.LunchMinute}, " +
|
||
$"\"includeLunch\": {item.IncludeLunch.ToString().ToLower()}, " +
|
||
$"\"monday\": {item.Monday.ToString().ToLower()}, " +
|
||
$"\"tuesday\": {item.Tuesday.ToString().ToLower()}, " +
|
||
$"\"wednesday\": {item.Wednesday.ToString().ToLower()}, " +
|
||
$"\"thursday\": {item.Thursday.ToString().ToLower()}, " +
|
||
$"\"friday\": {item.Friday.ToString().ToLower()}, " +
|
||
$"\"saturday\": {item.Saturday.ToString().ToLower()}, " +
|
||
$"\"sunday\": {item.Sunday.ToString().ToLower()} }}"
|
||
);
|
||
}
|
||
}
|
||
result.Details.Add(scheduleLog);
|
||
}
|
||
|
||
if (items.Meals.Count > 0)
|
||
{
|
||
var mealLog = CreateLog(nameof(Meal));
|
||
foreach (var item in items.Meals)
|
||
{
|
||
var exists = await _mealRepository.AnyAsync(x => x.Date == item.Date && x.BranchId == branchId);
|
||
if (!exists)
|
||
{
|
||
await _mealRepository.InsertAsync(new()
|
||
{
|
||
TenantId = tenantId,
|
||
BranchId = branchId,
|
||
Date = item.Date,
|
||
TotalCalorie = item.TotalCalorie,
|
||
Materials = item.Materials,
|
||
}, autoSave: true);
|
||
|
||
mealLog.InsertedCount++;
|
||
|
||
mealLog.InsertedItems.Add($"{{ \"date\": \"{item.Date:yyyy-MM-dd}\", \"type\": \"{item.Type}\", \"materials\": \"{item.Materials.Replace("|", ", ")}\" }}");
|
||
}
|
||
}
|
||
result.Details.Add(mealLog);
|
||
}
|
||
|
||
if (items.Banks.Count > 0)
|
||
{
|
||
var bankLog = CreateLog(nameof(Bank));
|
||
foreach (var item in items.Banks)
|
||
{
|
||
var exists = await _bankRepository.AnyAsync(x => x.Code == item.Code && x.BranchId == branchId);
|
||
|
||
if (!exists)
|
||
{
|
||
await _bankRepository.InsertAsync(new()
|
||
{
|
||
TenantId = tenantId,
|
||
BranchId = branchId,
|
||
Code = item.Code,
|
||
Name = item.Name,
|
||
Address1 = item.Address1,
|
||
Address2 = item.Address2,
|
||
Country = item.Country,
|
||
City = item.City,
|
||
District = item.District,
|
||
PostalCode = item.PostalCode,
|
||
PhoneNumber = item.PhoneNumber,
|
||
Email = item.Email
|
||
});
|
||
|
||
bankLog.InsertedCount++;
|
||
bankLog.InsertedItems.Add(
|
||
$"{{ " +
|
||
$"\"code\": \"{item.Code}\", " +
|
||
$"\"name\": \"{item.Name}\", " +
|
||
$"}}"
|
||
);
|
||
}
|
||
}
|
||
result.Details.Add(bankLog);
|
||
}
|
||
|
||
if (items.Cashes.Count > 0)
|
||
{
|
||
var cashLog = CreateLog(nameof(Cash));
|
||
foreach (var item in items.Cashes)
|
||
{
|
||
var exists = await _cashRepository.AnyAsync(x => x.Code == item.Code && x.BranchId == branchId);
|
||
|
||
if (!exists)
|
||
{
|
||
await _cashRepository.InsertAsync(new()
|
||
{
|
||
TenantId = tenantId,
|
||
BranchId = branchId,
|
||
Code = item.Code,
|
||
Name = item.Name,
|
||
Description = item.Description,
|
||
Currency = item.Currency,
|
||
Balance = item.Balance,
|
||
IsActive = item.IsActive
|
||
});
|
||
|
||
cashLog.InsertedCount++;
|
||
cashLog.InsertedItems.Add(
|
||
$"{{ " +
|
||
$"\"code\": \"{item.Code}\", " +
|
||
$"\"name\": \"{item.Name}\", " +
|
||
$"}}"
|
||
);
|
||
}
|
||
}
|
||
result.Details.Add(cashLog);
|
||
}
|
||
|
||
if (items.CurrentAccounts.Count > 0)
|
||
{
|
||
var currentAccountLog = CreateLog(nameof(CurrentAccount));
|
||
foreach (var item in items.CurrentAccounts)
|
||
{
|
||
var exists = await _currentAccountRepository.AnyAsync(x => x.Code == item.Code && x.BranchId == branchId);
|
||
|
||
if (!exists)
|
||
{
|
||
await _currentAccountRepository.InsertAsync(new()
|
||
{
|
||
TenantId = tenantId,
|
||
BranchId = branchId,
|
||
Code = item.Code,
|
||
Name = item.Name,
|
||
Description = item.Description,
|
||
TaxNumber = item.TaxNumber,
|
||
TaxOffice = item.TaxOffice,
|
||
CreditLimit = item.CreditLimit,
|
||
Balance = item.Balance,
|
||
Currency = item.Currency,
|
||
Risk = item.Risk,
|
||
IsActive = item.IsActive
|
||
});
|
||
|
||
currentAccountLog.InsertedCount++;
|
||
currentAccountLog.InsertedItems.Add(
|
||
$"{{ " +
|
||
$"\"code\": \"{item.Code}\", " +
|
||
$"\"name\": \"{item.Name}\", " +
|
||
$"}}"
|
||
);
|
||
}
|
||
}
|
||
result.Details.Add(currentAccountLog);
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
|