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 MMDAL.DTO.MaterialAccount; using MMDAL.Query.MaterialAccount; using Newtonsoft.Json; namespace MMDAL.CustomCode.MaterialAccount { public class MaterialAccountDAL : IMaterialAccountDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public MaterialAccountDAL(IQueryExecutor QueryExecutor, IValidation Validation) { _QueryExecutor = QueryExecutor; _Validation = Validation; } public async Task GetMaterialAccount(int MaterialAccountId, LoginDTO LoginDTO) { try { var sql = MaterialAccountQB.GET_MATERIALACCOUNT; var parameters = new { materialaccountid = MaterialAccountId }; var MaterialAccountDict = new Dictionary(); var result = await _QueryExecutor.QueryMultiMapAsync( LoginDTO, sql, (parent, child) => { if (!MaterialAccountDict.TryGetValue(parent.MaterialAccountId, out var existingParent)) { existingParent = parent; existingParent.MaterialAccountDetailArray = new List(); MaterialAccountDict[parent.MaterialAccountId] = existingParent; } if (child != null && child.MaterialAccountDetailId != 0) { existingParent.MaterialAccountDetailArray.Add(child); } return existingParent; }, parameters, splitOn: "MaterialAccountDetailId" ); var finalResult = MaterialAccountDict.Values.FirstOrDefault(); return JsonConvert.SerializeObject(finalResult); } catch (Exception ex) { throw new Exception($"Failed to retrieve MaterialAccount: {ex.Message}", ex); } } public async Task SaveMaterialAccount(MaterialAccountDTO MaterialAccountDTO, LoginDTO LoginDTO) { try { // SQL Server queries only string MaterialAccountSql = MaterialAccountQB.SAVE_MATERIALACCOUNT; string detailSql = MaterialAccountQB.SAVE_MATERIALACCOUNT_DETAIL; string countSql = MaterialAccountQB.GET_MATERIALACCOUNT_COUNT; // DUPLICATE CHECK int existingCount = await _QueryExecutor.ExecuteScalarAsync(LoginDTO, countSql, new { MaterialAccountDTO.MaterialAccountId }); if (existingCount > 0) { throw new MethodNotAllowedException( "Duplicate MaterialAccount record exists." ); } // TRANSACTION BUILD var statements = new List<(string sql, object param)> { (MaterialAccountSql, MaterialAccountDTO) }; if (MaterialAccountDTO.MaterialAccountDetailArray?.Count > 0) { foreach (var detail in MaterialAccountDTO.MaterialAccountDetailArray) { detail.MaterialAccountId = MaterialAccountDTO.MaterialAccountId; statements.Add((detailSql, detail)); } } await _QueryExecutor.ExecuteInTransactionAsync(LoginDTO, statements); return MaterialAccountDTO.MaterialAccountId; } catch (Exception ex) { throw new Exception( $"{ErrorResponse.SaveErrorMessage}: {ex.Message}", ex); } } public async Task UpdateMaterialAccount(MaterialAccountDTO MaterialAccountDTO, LoginDTO login) { var statements = new List<(string sql, object param)>(); statements.Add((MaterialAccountQB.UPDATE_MATERIALACCOUNT, MaterialAccountDTO)); foreach (var d in MaterialAccountDTO.MaterialAccountDetailArray) { d.MaterialAccountId = MaterialAccountDTO.MaterialAccountId; if (d.MaterialAccountDetailId == 0) statements.Add((MaterialAccountQB.SAVE_MATERIALACCOUNT_DETAIL, d)); else statements.Add((MaterialAccountQB.UPDATE_MATERIALACCOUNT_DETAIL, d)); } await _QueryExecutor.ExecuteInTransactionAsync(login, statements); return MaterialAccountDTO.MaterialAccountId; } public async Task DeleteMaterialAccountDetail(int MaterialAccountId, LoginDTO LoginDTO) { try { return await _QueryExecutor.ExecuteAsync( LoginDTO, MaterialAccountQB.DELETE_MATERIALACCOUNT_DETAIL, new { materialaccountid = MaterialAccountId } ); } catch (Exception ex) { throw new Exception($"Error while deleting MaterialAccount detail for ID {MaterialAccountId}", ex); } } public async Task DeleteMaterialAccount(int MaterialAccountId, LoginDTO LoginDTO) { try { return await _QueryExecutor.ExecuteAsync( LoginDTO, MaterialAccountQB.DELETE_MATERIALACCOUNT, new { materialaccountid = MaterialAccountId } ); } catch (Exception ex) { throw new Exception($"Error while deleting MaterialAccount with ID {MaterialAccountId}", ex); } } public async Task GetSelectListMaterialAccount(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { string SQL = MaterialAccountQB.GET_SELECTLIST_MATERIALACCOUNT; var Result = await _QueryExecutor.QueryAsync(LoginDTO, SQL, null!); string Json = JsonConvert.SerializeObject(Result); return Json; } catch (Exception) { throw; } } } }