using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using CRMDAL.DTO.SLA; using CRMDAL.Query.SLA; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GB5Exception; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Newtonsoft.Json; namespace CRMDAL.CustomCode.SLA { public class SLADAL : ISLADAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public SLADAL(IQueryExecutor QueryExecutor, IValidation Validation) { _QueryExecutor = QueryExecutor; _Validation = Validation; } public async Task GetSLA(int SLAId, LoginDTO LoginDTO) { try { var sql = SLAQB.GET_SLA; var parameters = new { SLAid = SLAId }; var SLADict = new Dictionary(); var result = await _QueryExecutor.QueryMultiMapAsync( LoginDTO, sql, (parent, child) => { if (!SLADict.TryGetValue(parent.SLAId, out var existingParent)) { existingParent = parent; existingParent.SLADetailArray = new List(); SLADict[parent.SLAId] = existingParent; } if (child != null && child.SLADetailId != 0) { existingParent.SLADetailArray.Add(child); } return existingParent; }, parameters, splitOn: "SLADETAILID" ); var finalResult = SLADict.Values.FirstOrDefault(); return JsonConvert.SerializeObject(finalResult); } catch (Exception ex) { throw new Exception($"Failed to retrieve SLA: {ex.Message}", ex); } } public async Task SaveSLA(SLADTO SLADTO, LoginDTO LoginDTO) { try { // SQL Server queries only string SLASql = SLAQB.SAVE_SLA; string detailSql = SLAQB.SAVE_SLA_DETAIL; string countSql = SLAQB.GET_SLA_COUNT; // DUPLICATE CHECK int existingCount = await _QueryExecutor.ExecuteScalarAsync(LoginDTO, countSql, new { SLADTO.SLAId }); if (existingCount > 0) { throw new MethodNotAllowedException( "Duplicate SLA record exists." ); } // TRANSACTION BUILD var statements = new List<(string sql, object param)> { (SLASql, SLADTO) }; if (SLADTO.SLADetailArray?.Count > 0) { foreach (var detail in SLADTO.SLADetailArray) { detail.SLAId = SLADTO.SLAId; statements.Add((detailSql, detail)); } } await _QueryExecutor.ExecuteInTransactionAsync(LoginDTO, statements); return SLADTO.SLAId; } catch (Exception ex) { throw new Exception( $"{ErrorResponse.SaveErrorMessage}: {ex.Message}", ex); } } public async Task UpdateSLA(SLADTO SLADTO, LoginDTO login) { var statements = new List<(string sql, object param)>(); statements.Add((SLAQB.UPDATE_SLA, SLADTO)); foreach (var d in SLADTO.SLADetailArray) { d.SLAId = SLADTO.SLAId; if (d.SLADetailId == 0) statements.Add((SLAQB.SAVE_SLA_DETAIL, d)); else statements.Add((SLAQB.UPDATE_SLA_DETAIL, d)); } await _QueryExecutor.ExecuteInTransactionAsync(login, statements); return SLADTO.SLAId; } public async Task DeleteSLADetail(int SLAId, LoginDTO LoginDTO) { try { return await _QueryExecutor.ExecuteAsync( LoginDTO, SLAQB.DELETE_SLA_DETAIL, new { SLAid = SLAId } ); } catch (Exception ex) { throw new Exception($"Error while deleting SLA Detail for ID {SLAId}", ex); } } public async Task DeleteSLA(int SLAId, LoginDTO LoginDTO) { try { return await _QueryExecutor.ExecuteAsync( LoginDTO, SLAQB.DELETE_SLA, new { SLAid = SLAId } ); } catch (Exception ex) { throw new Exception($"Error while deleting SLA with ID {SLAId}", ex); } } public async Task GetSelectListSLA(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { string SQL = SLAQB.GET_SELECTLIST_SLA; var Result = await _QueryExecutor.QueryAsync(LoginDTO, SQL, null!); string Json = JsonConvert.SerializeObject(Result); return Json; } catch (Exception) { throw; } } } }