using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using AccountsDAL.CustomCode.CostCenterType; using AccountsDAL.DTO.CostCenterType; using AccountsDAL.Query.AccountSchedule; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.HybridCache; using GB5Shared.DTO.Framework.Logging; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; using static GB5Shared.GB5Constant.Constant; namespace AccountsBLL.CostCenterType { public class CostCenterTypeBLL : ICostCenterTypeBLL { private readonly ICostCenterTypeDAL _CostCenterTypeDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly KeyInvalidate _KeyInvalidate; public CostCenterTypeBLL(ICostCenterTypeDAL ICostCenterTypeDAL, AutoNumber AutoNumber, IValidation validation, KeyInvalidate keyInvalidate) { _CostCenterTypeDAL = ICostCenterTypeDAL; _AutoNumber = AutoNumber; _Validation = validation; _KeyInvalidate = keyInvalidate; } public async Task GetCostCenterType(int CostCenterTypeId, LoginDTO LoginDTO) { try { return await _CostCenterTypeDAL.GetCostCenterType(CostCenterTypeId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListCostCenterType(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _CostCenterTypeDAL.GetSelectListCostCenterType(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task GetCostCenterTypeList(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _CostCenterTypeDAL.GetCostCenterTypeList(CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task SaveCostCenterType(CostCenterTypeDTO CostCenterTypeDTO, LoginDTO LoginDTO) { if (CostCenterTypeDTO == null) throw new ArgumentNullException(nameof(CostCenterTypeDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(CostCenterTypeDTO.CostCenterTypeCode, nameof(CostCenterTypeDTO.CostCenterTypeCode)); await _Validation.NotEmpty(CostCenterTypeDTO.CostCenterTypeName, nameof(CostCenterTypeDTO.CostCenterTypeName)); CostCenterTypeDTO.CostCenterTypeModifiedById = LoginDTO.UserId; CostCenterTypeDTO.CostCenterTypeModifiedOn = DateTime.UtcNow; int initialId = CostCenterTypeDTO.CostCenterTypeId; int count = 1 + initialId; if (initialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "COSTCENTERTYPE", LoginDTO); CostCenterTypeDTO.CostCenterTypeId = AutoNumberDTO.StartNumber; CostCenterTypeDTO.CostCenterTypeCreatedById = LoginDTO.UserId; CostCenterTypeDTO.CostCenterTypeCreatedOn = DateTime.UtcNow; await _CostCenterTypeDAL.SaveCostCenterType(CostCenterTypeDTO, LoginDTO); var cacheKey = CacheKeyGenerator.KeyGeneration( CostCenterTypeDTO.CostCenterTypeId, // ✅ int parameter from method signature EntityConstant.OBJECTCOSTCENTERTYPE, CacheKeyLevel.CLIENT_LEVEL, string.Empty, LoginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); return $"{SuccessResponse.SaveSuccessMessage} {CostCenterTypeDTO.CostCenterTypeId}"; } else { await _CostCenterTypeDAL.UpdateCostCenterType(CostCenterTypeDTO, LoginDTO); var cacheKey = CacheKeyGenerator.KeyGeneration( CostCenterTypeDTO.CostCenterTypeId, EntityConstant.OBJECTCOSTCENTERTYPE, CacheKeyLevel.CLIENT_LEVEL, string.Empty, LoginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); return $"{SuccessResponse.UpdateSuccessMessage} {CostCenterTypeDTO.CostCenterTypeId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && CostCenterTypeDTO.CostCenterTypeId > 0) await _AutoNumber.RollbackAutoNumber("COSTCENTERTYPE", CostCenterTypeDTO.CostCenterTypeId, LoginDTO); throw; } } public async Task DeleteCostCenterType(int CostCenterTypeId, LoginDTO LoginDTO) { var result = await _CostCenterTypeDAL .DeleteCostCenterType(CostCenterTypeId, LoginDTO); var cacheKey = CacheKeyGenerator.KeyGeneration( CostCenterTypeId, EntityConstant.OBJECTCOSTCENTERTYPE, CacheKeyLevel.CLIENT_LEVEL, string.Empty, LoginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); return result; } } }