using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GB5Exception; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using MarketingDAL.DTO.LeadSource; using MarketingDAL.Query.LeadSource; using Newtonsoft.Json; namespace MarketingDAL.CustomCode.LeadSource { public class LeadSourceDAL : ILeadSourceDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public LeadSourceDAL(IQueryExecutor QueryExecutor, IValidation Validation) { _QueryExecutor = QueryExecutor; _Validation = Validation; } public async Task GetLeadSource(int LeadSourceId, LoginDTO LoginDTO) { try { var sql = LeadSourceQB.GET_LEADSOURCE; var parameters = new { leadsourceid = LeadSourceId }; var LeadSourceDict = new Dictionary(); var result = await _QueryExecutor.QueryMultiMapAsync( LoginDTO, sql, (parent, child) => { if (!LeadSourceDict.TryGetValue(parent.LeadSourceId, out var existingParent)) { existingParent = parent; existingParent.LeadSourceDetailArray = new List(); LeadSourceDict[parent.LeadSourceId] = existingParent; } if (child != null && child.LeadSourceDetailId != 0) { existingParent.LeadSourceDetailArray.Add(child); } return existingParent; }, parameters, splitOn: "LeadSourceDETAILID" ); var finalResult = LeadSourceDict.Values.FirstOrDefault(); return JsonConvert.SerializeObject(finalResult); } catch (Exception ex) { throw new Exception($"Failed to retrieve LeadSource: {ex.Message}", ex); } } public async Task GetSelectListLeadSource(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { string SQL = LeadSourceQB.GET_SELECTLIST_LEADSOURCE; var Result = await _QueryExecutor.QueryAsync(LoginDTO, SQL, null!); string Json = JsonConvert.SerializeObject(Result); return Json; } catch (Exception) { throw; } } public async Task SaveLeadSource(LeadSourceDTO LeadSourceDTO, LoginDTO LoginDTO) { try { // SQL Server queries only string LeadSourceSql = LeadSourceQB.SAVE_LEADSOURCE; string detailSql = LeadSourceQB.SAVE_LEADSOURCE_DETAIL; string countSql = LeadSourceQB.GET_LEADSOURCE_COUNT; // DUPLICATE CHECK int existingCount = await _QueryExecutor.ExecuteScalarAsync(LoginDTO, countSql, new { LeadSourceDTO.LeadSourceId }); if (existingCount > 0) { throw new MethodNotAllowedException( "Duplicate LeadSource record exists." ); } // TRANSACTION BUILD var statements = new List<(string sql, object param)> { (LeadSourceSql, LeadSourceDTO) }; if (LeadSourceDTO.LeadSourceDetailArray?.Count > 0) { foreach (var detail in LeadSourceDTO.LeadSourceDetailArray) { detail.LeadSourceId = LeadSourceDTO.LeadSourceId; statements.Add((detailSql, detail)); } } await _QueryExecutor.ExecuteInTransactionAsync(LoginDTO, statements); return LeadSourceDTO.LeadSourceId; } catch (Exception ex) { throw new Exception( $"{ErrorResponse.SaveErrorMessage}: {ex.Message}", ex); } } public async Task UpdateLeadSource(LeadSourceDTO LeadSourceDTO, LoginDTO login) { var statements = new List<(string sql, object param)>(); statements.Add((LeadSourceQB.UPDATE_LEADSOURCE, LeadSourceDTO)); foreach (var d in LeadSourceDTO.LeadSourceDetailArray) { d.LeadSourceId = LeadSourceDTO.LeadSourceId; if (d.LeadSourceDetailId == 0) statements.Add((LeadSourceQB.SAVE_LEADSOURCE_DETAIL, d)); else statements.Add((LeadSourceQB.UPDATE_LEADSOURCE_DETAIL, d)); } await _QueryExecutor.ExecuteInTransactionAsync(login, statements); return LeadSourceDTO.LeadSourceId; } public async Task DeleteLeadSourceDetail(int LeadSourceId, LoginDTO LoginDTO) { try { return await _QueryExecutor.ExecuteAsync( LoginDTO, LeadSourceQB.DELETE_LEADSOURCE_DETAIL, new { leadsourceid = LeadSourceId } ); } catch (Exception ex) { throw new Exception($"Error while deleting LeadSource detail for ID {LeadSourceId}", ex); } } public async Task DeleteLeadSource(int LeadSourceId, LoginDTO LoginDTO) { try { return await _QueryExecutor.ExecuteAsync( LoginDTO, LeadSourceQB.DELETE_LEADSOURCE, new { leadsourceid = LeadSourceId } ); } catch (Exception ex) { throw new Exception($"Error while deleting LeadSource with ID {LeadSourceId}", ex); } } } }