using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Google.Api; using MaintenanceDAL.CustomCode.KeyComponent; using MaintenanceDAL.DTO.KeyComponent; using Newtonsoft.Json; using Org.BouncyCastle.Crypto; namespace MaintenanceBLL.KeyComponent { public class KeyComponentBLL: IKeyComponentBLL { private readonly IKeyComponentDAL _KeyComponentDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public KeyComponentBLL(IKeyComponentDAL KeyComponentDAL, AutoNumber AutoNumber, IValidation Validation) { _KeyComponentDAL = KeyComponentDAL; _AutoNumber = AutoNumber; _Validation = Validation; } /// /// Retrieves a KeyComponent by its ID along with its associated details. /// /// /// /// public async TaskGetKeyComponent(int KeyComponentId, LoginDTO LoginDTO) { try { return await _KeyComponentDAL.GetKeyComponent(KeyComponentId, LoginDTO); } catch (Exception) { throw; } } /// /// Saves or updates a KeyComponent along with its associated KeyComponentDetail records, handling auto-numbering and error rollback as needed. /// /// /// /// /// public async Task SaveKeyComponent(KeyComponentDTO KeyComponentDTO, LoginDTO LoginDTO) { if (KeyComponentDTO == null) throw new ArgumentNullException(nameof(KeyComponentDTO)); AutoNumberDTO? autoNumberDTO = null; try { KeyComponentDTO.KeyComponentModifiedById = LoginDTO.UserId; KeyComponentDTO.KeyComponentModifiedOn = DateTime.UtcNow; bool isNew = KeyComponentDTO.KeyComponentId == 0; if (isNew) { int detailCount = KeyComponentDTO.KeyComponentDetailArray?.Count ?? 0; int totalRequired = 1 + detailCount; autoNumberDTO = await _AutoNumber.GetNumberAsync(totalRequired, "KEYCOMPONENT", LoginDTO); int nextId = autoNumberDTO.StartNumber; KeyComponentDTO.KeyComponentId = nextId++; KeyComponentDTO.KeyComponentCreatedById = LoginDTO.UserId; KeyComponentDTO.KeyComponentCreatedOn = DateTime.UtcNow; if (KeyComponentDTO.KeyComponentDetailArray?.Count > 0) { foreach (var detail in KeyComponentDTO.KeyComponentDetailArray) { detail.KeyComponentDetailId = nextId++; detail.KeyComponentId = KeyComponentDTO.KeyComponentId; } } await _KeyComponentDAL.SaveKeyComponent(KeyComponentDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {KeyComponentDTO.KeyComponentId}"; } else { await _KeyComponentDAL.UpdateKeyComponent(KeyComponentDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {KeyComponentDTO.KeyComponentId}"; } } catch { if (autoNumberDTO != null && KeyComponentDTO.KeyComponentId > 0) { await _AutoNumber.RollbackAutoNumber("KEYCOMPONENT", autoNumberDTO.StartNumber, LoginDTO); } throw; } } /// /// Delete KeyComponent Id /// /// /// /// /// public async Task DeleteKeyComponent(int KeyComponentId, LoginDTO LoginDTO) { try { var detailsResult = await _KeyComponentDAL.DeleteKeyComponentDetail(KeyComponentId, LoginDTO); if (detailsResult <= 0) return $"{ErrorResponse.DeleteNotFoundMessage}"; var parentResult = await _KeyComponentDAL.DeleteKeyComponent(KeyComponentId, LoginDTO); if (parentResult <= 0) return $"{ErrorResponse.DeleteNotFoundMessage}"; return $"{SuccessResponse.DeleteSuccessMessage}"; } catch (Exception ex) { throw new Exception($"Error Deleting KeyComponent ID {KeyComponentId}", ex); } } public async Task GetSelectListKeyComponent(CriteriaDTO criteriaDTO, LoginDTO LoginDTO) { try { var data = await _KeyComponentDAL.GetSelectListKeyComponent(criteriaDTO, LoginDTO); return JsonConvert.SerializeObject(data); } catch (Exception) { throw; } } } }