using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; using KmsDAL.CustomCode.KmSpace; using KmsDAL.DTO.KmSpace; namespace KmsBLL.KmSpace { public class KmSpaceBLL : IKmSpaceBLL { private readonly IKmSpaceDAL _dal; private readonly AutoNumber _autoNumber; private readonly IValidation _validation; public KmSpaceBLL(IKmSpaceDAL dal, AutoNumber autoNumber, IValidation validation) { _dal = dal; _autoNumber = autoNumber; _validation = validation; } public async Task GetKmSpace(int spaceId, LoginDTO login, CancellationToken ct) => await _dal.GetKmSpace(spaceId, login, ct); public async Task GetSelectListKmSpace(LoginDTO login, CancellationToken ct) => await _dal.GetSelectListKmSpace(login, ct); public async Task SaveKmSpace(KmSpaceDTO dto, LoginDTO login, CancellationToken ct) { await _validation.NotEmpty(dto.SpaceCode, nameof(dto.SpaceCode)); await _validation.NotEmpty(dto.SpaceName, nameof(dto.SpaceName)); var duplicateCount = await _dal.GetKmSpaceCodeCount(dto.SpaceCode, dto.SpaceId, login, ct); if (duplicateCount > 0) throw new InvalidOperationException($"Space code '{dto.SpaceCode}' already exists."); dto.TenantId = login.ClientId; bool isNew = dto.SpaceId == 0; if (isNew) { var autoNum = await _autoNumber.GetNumberAsync(1, "KMSPACE", login); dto.SpaceId = autoNum.StartNumber; dto.CreatedById = login.UserId; dto.ModifiedById = login.UserId; await _dal.SaveKmSpace(dto, login, ct); return $"{SuccessResponse.SaveSuccessMessage} {dto.SpaceId}"; } else { dto.ModifiedById = login.UserId; await _dal.UpdateKmSpace(dto, login, ct); return $"{SuccessResponse.UpdateSuccessMessage} {dto.SpaceId}"; } } public async Task DeleteKmSpace(int spaceId, LoginDTO login, CancellationToken ct) { await _dal.DeleteKmSpace(spaceId, login, ct); return SuccessResponse.DeleteSuccessMessage; } } }