using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using PayRollDAL.CustomeCode.NOCList; using PayRollDAL.DTO.NOCList; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace PayRollBLL.NOCList { public class NOCListBLL : INOCListBLL { private readonly INOCListDAL _NOCListDAL; private readonly AutoNumber _AutoNumber; private readonly IQueryExecutor _QueryExecutor; public NOCListBLL( INOCListDAL nocListDAL, AutoNumber autoNumber, IQueryExecutor queryExecutor) { _NOCListDAL = nocListDAL; _AutoNumber = autoNumber; _QueryExecutor = queryExecutor; } // ── Get single record ───────────────────────────────────────────── public async Task GetNOCList(int NOCListId, LoginDTO LoginDTO) { return await _NOCListDAL.GetNOCList(NOCListId, LoginDTO).ConfigureAwait(false); } // ── Save (insert or update) ─────────────────────────────────────── // GB4 parity: NOCListBLL.SaveNOCList — autonumber block sized for // header + all detail rows (GenericDal.GetNumber(Count,"NOCLIST",...)). // Transaction shared across the autonumber-consuming insert so a // rollback reverts both the counter and the entity rows. public async Task SaveNOCList(NOCListDTO NOCListDTO, LoginDTO LoginDTO) { if (NOCListDTO == null) throw new ArgumentNullException(nameof(NOCListDTO)); bool isNew = NOCListDTO.NOCListId == 0; AutoNumberDTO? autoNumberDTO = null; var trans = await _QueryExecutor.BeginTransactionAsync(LoginDTO).ConfigureAwait(false); try { if (isNew) { int count = 1 + (NOCListDTO.NOCListDetailArray?.Count ?? 0); autoNumberDTO = await _AutoNumber.GetNumberAsync(count, "NOCLIST", LoginDTO) .ConfigureAwait(false); NOCListDTO.NOCListId = autoNumberDTO.StartNumber; NOCListDTO.NOCListCreatedById = LoginDTO.UserId; NOCListDTO.NOCListCreatedOn = DateTime.UtcNow; if (NOCListDTO.NOCListDetailArray != null) { int tmpCount = 1; foreach (var detail in NOCListDTO.NOCListDetailArray) { detail.NOCListDetailId = autoNumberDTO.StartNumber + tmpCount; tmpCount++; } } } NOCListDTO.NOCListModifiedById = LoginDTO.UserId; NOCListDTO.NOCListModifiedOn = DateTime.UtcNow; int rows = isNew ? await _NOCListDAL.SaveNOCList(NOCListDTO, LoginDTO, trans).ConfigureAwait(false) : await _NOCListDAL.UpdateNOCList(NOCListDTO, LoginDTO, trans).ConfigureAwait(false); await _QueryExecutor.CommitAsync(trans).ConfigureAwait(false); if (rows <= 0) return isNew ? "Failed to save NOC list." : "Failed to update NOC list."; return isNew ? $"Details Saved Successfully With Id :{NOCListDTO.NOCListId}" : $"Details Updated Successfully With Id :{NOCListDTO.NOCListId}"; } catch (Exception) { await _QueryExecutor.RollbackAsync(trans).ConfigureAwait(false); if (autoNumberDTO != null) await _AutoNumber.RollbackAutoNumber("NOCLIST", autoNumberDTO.StartNumber, LoginDTO) .ConfigureAwait(false); throw; } } // ── Delete ──────────────────────────────────────────────────────── public async Task DeleteNOCList(int NOCListId, LoginDTO LoginDTO) { return await _NOCListDAL.DeleteNOCList(NOCListId, LoginDTO).ConfigureAwait(false); } // ── Picklist ────────────────────────────────────────────────────── public async Task GetSelectListNOCList(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { return await _NOCListDAL .GetSelectListNOCList(FirstNumber, MaxResult, CriteriaDTO, LoginDTO) .ConfigureAwait(false); } } }