using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FrameworkDAL.CustomCode.DrillDown; using FrameworkDAL.DTO.DrillDown; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.ResponseStandard; using GB5Shared.Validation; namespace FrameworkBLL.DrillDown { public class DrillDownBLL : IDrillDownBLL { private readonly IDrillDownDAL _DrillDownDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public DrillDownBLL(IDrillDownDAL DrillDownDAL, AutoNumber AutoNumber, IValidation Validation) { _DrillDownDAL = DrillDownDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetDrillDown(int DrillDownId, LoginDTO LoginDTO) { try { return await _DrillDownDAL.GetDrillDown(DrillDownId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveDrillDown(DrillDownDTO DrillDownDTO, LoginDTO loginDTO) { if (DrillDownDTO == null) throw new ArgumentNullException(nameof(DrillDownDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(DrillDownDTO.DrillDownCode, nameof(DrillDownDTO.DrillDownCode)); await _Validation.NotEmpty(DrillDownDTO.DrillDownDescription, nameof(DrillDownDTO.DrillDownDescription)); DrillDownDTO.DrillDownModifiedById = loginDTO.UserId; DrillDownDTO.DrillDownModifiedOn = DateTime.UtcNow; int count = 1; if (DrillDownDTO.DrillDownId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "DRILLDOWN", loginDTO); DrillDownDTO.DrillDownId = autoNumberDTO.StartNumber; DrillDownDTO.DrillDownCreatedById = loginDTO.UserId; DrillDownDTO.DrillDownCreatedOn = DateTime.UtcNow; if (DrillDownDTO.DrillDownDetailArray != null && DrillDownDTO.DrillDownDetailArray.Count > 0) { // Detail IDs get their own AutoNumber reservation — the parent's own // DrillDownId must never be mutated here (it was previously incremented // once per child via `DrillDownDTO.DrillDownId++`, corrupting the parent's // own primary key by +N before it was even saved). var detailAutoNumberDTO = await _AutoNumber.GetAutoNumber( DrillDownDTO.DrillDownDetailArray.Count, "DRILLDOWNDETAIL", loginDTO); int nextDetailId = detailAutoNumberDTO.StartNumber; foreach (var child in DrillDownDTO.DrillDownDetailArray) { child.DrillDownDetailId = nextDetailId++; child.DrillDownId = DrillDownDTO.DrillDownId; } } await _DrillDownDAL.SaveDrillDown(DrillDownDTO, loginDTO); return $"DrillDown saved successfully with DrillDownId {DrillDownDTO.DrillDownId}."; } else { await _DrillDownDAL.UpdateDrillDown(DrillDownDTO, loginDTO); return $"DrillDown updated successfully with DrillDownId {DrillDownDTO.DrillDownId}."; } } catch { if (autoNumberDTO != null && DrillDownDTO.DrillDownId > 0) await _AutoNumber.RollbackAutoNumber("DRILLDOWN", DrillDownDTO.DrillDownId, loginDTO); throw; } } public async Task> DeleteDrillDown(int DrillDownId, LoginDTO LoginDTO) { try { return await _DrillDownDAL.DeleteDrillDown(DrillDownId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListDrillDown(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _DrillDownDAL.GetSelectListDrillDown(FirstNumber, MaxResult, CriteriaDTO, LoginDTO); } catch (Exception ex) { throw new Exception("Error in GetSelectListDrillDown", ex); } } } }