using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using PayRollDAL.CustomeCode.CasteCategory; using PayRollDAL.DTO.CasteCategory; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace PayRollBLL.CasteCategory { public class CasteCategoryBLL : ICasteCategoryBLL { private readonly ICasteCategoryDAL _CasteCategoryDAL; private readonly AutoNumber _AutoNumber; private readonly IQueryExecutor _QueryExecutor; public CasteCategoryBLL( ICasteCategoryDAL casteCategoryDAL, AutoNumber autoNumber, IQueryExecutor queryExecutor) { _CasteCategoryDAL = casteCategoryDAL; _AutoNumber = autoNumber; _QueryExecutor = queryExecutor; } // ── Get single record ───────────────────────────────────────────── public async Task GetCasteCategory(int CasteCategoryId, LoginDTO LoginDTO) { return await _CasteCategoryDAL.GetCasteCategory(CasteCategoryId, LoginDTO).ConfigureAwait(false); } // ── Save (insert or update) ─────────────────────────────────────── public async Task SaveCasteCategory(CasteCategoryDTO CasteCategoryDTO, LoginDTO LoginDTO) { if (CasteCategoryDTO == null) throw new ArgumentNullException(nameof(CasteCategoryDTO)); bool isNew = CasteCategoryDTO.CasteCategoryId == 0; AutoNumberDTO? autoNumberDTO = null; var trans = await _QueryExecutor.BeginTransactionAsync(LoginDTO).ConfigureAwait(false); try { if (isNew) { int count = 1 + (CasteCategoryDTO.CasteCategoryDetailArray?.Count ?? 0); autoNumberDTO = await _AutoNumber.GetNumberAsync(count, "CASTECATEGORY", LoginDTO) .ConfigureAwait(false); CasteCategoryDTO.CasteCategoryId = autoNumberDTO.StartNumber; CasteCategoryDTO.CasteCategoryCreatedById = LoginDTO.UserId; CasteCategoryDTO.CasteCategoryCreatedOn = DateTime.UtcNow; if (CasteCategoryDTO.CasteCategoryDetailArray != null) { int tmpCount = 1; foreach (var detail in CasteCategoryDTO.CasteCategoryDetailArray) { detail.CasteCategoryDetailId = autoNumberDTO.StartNumber + tmpCount; tmpCount++; } } } CasteCategoryDTO.CasteCategoryModifiedById = LoginDTO.UserId; CasteCategoryDTO.CasteCategoryModifiedOn = DateTime.UtcNow; int rows = isNew ? await _CasteCategoryDAL.SaveCasteCategory(CasteCategoryDTO, LoginDTO, trans).ConfigureAwait(false) : await _CasteCategoryDAL.UpdateCasteCategory(CasteCategoryDTO, LoginDTO, trans).ConfigureAwait(false); await _QueryExecutor.CommitAsync(trans).ConfigureAwait(false); if (rows <= 0) return isNew ? "Failed to save caste category." : "Failed to update caste category."; return isNew ? $"Details Saved Successfully With Id :{CasteCategoryDTO.CasteCategoryId}" : $"Details Updated Successfully With Id :{CasteCategoryDTO.CasteCategoryId}"; } catch (Exception) { await _QueryExecutor.RollbackAsync(trans).ConfigureAwait(false); if (autoNumberDTO != null) await _AutoNumber.RollbackAutoNumber("CASTECATEGORY", autoNumberDTO.StartNumber, LoginDTO) .ConfigureAwait(false); throw; } } // ── Delete ──────────────────────────────────────────────────────── public async Task DeleteCasteCategory(int CasteCategoryId, LoginDTO LoginDTO) { return await _CasteCategoryDAL.DeleteCasteCategory(CasteCategoryId, LoginDTO).ConfigureAwait(false); } // ── Picklist ────────────────────────────────────────────────────── public async Task GetSelectListCasteCategory(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { return await _CasteCategoryDAL .GetSelectListCasteCategory(FirstNumber, MaxResult, CriteriaDTO, LoginDTO) .ConfigureAwait(false); } } }