306 lines
9.7 KiB
C#
306 lines
9.7 KiB
C#
using _0_Framework.Application;
|
|
using Company.Domain.LawAgg;
|
|
using CompanyManagment.App.Contracts.Law;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CompanyManagment.Application;
|
|
|
|
public class LawApplication : ILawApplication
|
|
{
|
|
private readonly ILawRepository _lawRepository;
|
|
|
|
public LawApplication(ILawRepository lawRepository)
|
|
{
|
|
_lawRepository = lawRepository;
|
|
}
|
|
|
|
public OperationResult Create(CreateLaw command)
|
|
{
|
|
var operation = new OperationResult();
|
|
|
|
// قبل از ایجاد قانون جدید، همه قوانین قدیمی از این نوع را غیرفعال میکنیم
|
|
var existingLaws = _lawRepository.Get().Where(x => x.Type == command.Type).ToList();
|
|
if (existingLaws.Any())
|
|
{
|
|
foreach (var existingLaw in existingLaws)
|
|
{
|
|
existingLaw.Deactivate();
|
|
}
|
|
_lawRepository.SaveChanges();
|
|
}
|
|
|
|
var law = new Law(command.Title, command.Type, command.Notifications, command.HeadTitle);
|
|
if (command.Items == null || command.Items.Count == 0)
|
|
{
|
|
return operation.Failed("باید حداقل یک بند برای قانون باید ثبت شود");
|
|
}
|
|
|
|
var orderNumber = 1;
|
|
foreach (var item in command.Items)
|
|
{
|
|
law.AddItem(item.Header, item.Details, orderNumber);
|
|
orderNumber++;
|
|
}
|
|
|
|
_lawRepository.Create(law);
|
|
_lawRepository.SaveChanges();
|
|
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public OperationResult Edit(EditLaw command)
|
|
{
|
|
var operation = new OperationResult();
|
|
var law = _lawRepository.Get(command.Id);
|
|
|
|
if (law == null)
|
|
return operation.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
if (command.Items == null || command.Items.Count == 0)
|
|
{
|
|
return operation.Failed("باید حداقل یک بند برای قانون باید ثبت شود");
|
|
}
|
|
|
|
// Create new items list for the new version
|
|
var orderNumber = 1;
|
|
var lawItems = command.Items.Select(x =>
|
|
{
|
|
var res = new LawItem(x.Header, x.Details, orderNumber);
|
|
orderNumber++;
|
|
return res;
|
|
}).ToList();
|
|
|
|
// Mark the current version as old version
|
|
law.SetAsOldVersion();
|
|
|
|
// Create a new version based on the old one
|
|
var newVersion = law.CreateNewVersion(command.Title, command.Notifications, command.HeadTitle, lawItems);
|
|
|
|
// Save the new version
|
|
_lawRepository.Create(newVersion);
|
|
_lawRepository.SaveChanges();
|
|
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public OperationResult UpsertLaw(EditLaw command)
|
|
{
|
|
var operation = new OperationResult();
|
|
|
|
// Validate items
|
|
if (command.Items == null || command.Items.Count == 0)
|
|
{
|
|
return operation.Failed("باید حداقل یک بند برای قانون باید ثبت شود");
|
|
}
|
|
|
|
// Check if active law exists by type
|
|
var existingLaw = _lawRepository.Get().FirstOrDefault(x => x.Type == command.Type && x.IsActive);
|
|
|
|
if (existingLaw == null)
|
|
{
|
|
// If law doesn't exist, create a new one
|
|
var law = new Law(command.Title, command.Type, command.Notifications, command.HeadTitle);
|
|
|
|
var orderNumber = 1;
|
|
foreach (var item in command.Items)
|
|
{
|
|
law.AddItem(item.Header, item.Details, orderNumber);
|
|
orderNumber++;
|
|
}
|
|
|
|
_lawRepository.Create(law);
|
|
_lawRepository.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
// Mark the current version as old version
|
|
existingLaw.SetAsOldVersion();
|
|
|
|
// Create new items list for the new version
|
|
var orderNumber = 1;
|
|
var lawItems = command.Items.Select(x =>
|
|
{
|
|
var res = new LawItem(x.Header, x.Details, orderNumber);
|
|
orderNumber++;
|
|
return res;
|
|
}).ToList();
|
|
|
|
// Create a new version based on the old one
|
|
var newVersion = existingLaw.CreateNewVersion(command.Title, command.Notifications, command.HeadTitle, lawItems);
|
|
|
|
// Save the new version
|
|
_lawRepository.Create(newVersion);
|
|
_lawRepository.SaveChanges();
|
|
}
|
|
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public OperationResult Activate(long id)
|
|
{
|
|
var operation = new OperationResult();
|
|
var law = _lawRepository.Get(id);
|
|
|
|
if (law == null)
|
|
return operation.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
// غیرفعال کردن همه نسخههای قبلی از این نوع قانون
|
|
var otherLaws = _lawRepository.Get().Where(x => x.Type == law.Type && x.id != law.id).ToList();
|
|
foreach (var otherLaw in otherLaws)
|
|
{
|
|
otherLaw.Deactivate();
|
|
}
|
|
|
|
// فعال کردن این نسخه به عنوان آخرین نسخه
|
|
law.Activate();
|
|
_lawRepository.SaveChanges();
|
|
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public OperationResult Deactivate(long id)
|
|
{
|
|
var operation = new OperationResult();
|
|
var law = _lawRepository.Get(id);
|
|
|
|
if (law == null)
|
|
return operation.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
law.Deactivate();
|
|
_lawRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public OperationResult ActivateByType(LawType type)
|
|
{
|
|
var operation = new OperationResult();
|
|
|
|
// غیرفعال کردن همه نسخههای قبلی از این نوع قانون
|
|
var existingLaws = _lawRepository.Get().Where(x => x.Type == type).ToList();
|
|
|
|
if (!existingLaws.Any())
|
|
{
|
|
// If law doesn't exist, create a new active one with default values
|
|
var newLaw = new Law(GetDefaultTitleForLawType(type), type, new List<string>(), "");
|
|
newLaw.Activate();
|
|
_lawRepository.Create(newLaw);
|
|
_lawRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
// فعال کردن آخرین نسخه (با بالاترین شماره نسخه)
|
|
var latestVersion = existingLaws.OrderByDescending(x => x.Version).First();
|
|
|
|
// غیرفعال کردن سایر نسخهها
|
|
foreach (var law in existingLaws.Where(x => x.id != latestVersion.id))
|
|
{
|
|
law.Deactivate();
|
|
}
|
|
|
|
// فعال کردن آخرین نسخه
|
|
latestVersion.Activate();
|
|
_lawRepository.SaveChanges();
|
|
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public OperationResult DeactivateByType(LawType type)
|
|
{
|
|
var operation = new OperationResult();
|
|
var laws = _lawRepository.Get().Where(x => x.Type == type).ToList();
|
|
|
|
if (!laws.Any())
|
|
return operation.Failed("قانون مورد نظر یافت نشد");
|
|
|
|
foreach (var law in laws)
|
|
{
|
|
law.Deactivate();
|
|
}
|
|
|
|
_lawRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public EditLaw GetDetails(long id)
|
|
{
|
|
var law = _lawRepository.Get(id);
|
|
return new EditLaw
|
|
{
|
|
Id = law.id,
|
|
Title = law.Title,
|
|
Type = law.Type,
|
|
HeadTitle = law.HeadTitle,
|
|
Notifications = law.Notifications,
|
|
Items = law.Items.OrderBy(x => x.OrderNumber).Select(x => new LawItemViewModel
|
|
{
|
|
Header = x.Header,
|
|
Details = x.Details
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public async Task<List<LawViewModel>> GetList(LawSearchModel searchModel)
|
|
{
|
|
// Get all laws from database, including version information
|
|
return await _lawRepository.GetList(searchModel);
|
|
}
|
|
|
|
private string GetDefaultTitleForLawType(LawType lawType)
|
|
{
|
|
return lawType switch
|
|
{
|
|
LawType.Register => "قوانین ثبت نام",
|
|
_ => $"قوانین {lawType}"
|
|
};
|
|
}
|
|
|
|
public async Task<LawViewModel> GetLawWithItems(long id)
|
|
{
|
|
var law = await _lawRepository.GetWithItems(id);
|
|
if (law == null)
|
|
return null;
|
|
|
|
return new LawViewModel
|
|
{
|
|
Id = law.id,
|
|
Title = law.Title,
|
|
IsActive = law.IsActive,
|
|
CreatedAt = law.CreationDate,
|
|
Type = law.Type,
|
|
HeadTitle = law.HeadTitle,
|
|
Notifications = law.Notifications,
|
|
Version = law.Version,
|
|
Items = law.Items.OrderBy(x=>x.OrderNumber).Select(x => new LawItemViewModel
|
|
{
|
|
Header = x.Header,
|
|
Details = x.Details,
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public async Task<LawViewModel> GetLawByType(LawType type)
|
|
{
|
|
// Only get the active (latest) version of the law
|
|
var lawViewModel = await _lawRepository.GetByType(type);
|
|
|
|
// If no law exists for this type, return a default empty law
|
|
if (lawViewModel == null)
|
|
{
|
|
return new LawViewModel
|
|
{
|
|
Id = 0,
|
|
Title = GetDefaultTitleForLawType(type),
|
|
IsActive = false,
|
|
CreatedAt = DateTime.Now,
|
|
Type = type,
|
|
Version = 1,
|
|
Items = new List<LawItemViewModel>()
|
|
};
|
|
}
|
|
|
|
return lawViewModel;
|
|
}
|
|
} |