using MaintenanceDAL.DTO.TypeComponent; using MaintenanceDAL.Query.TypeComponent; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace MaintenanceDAL.CustomCode.TypeComponent { public class TypeComponentDAL : ITypeComponentDAL { private readonly IQueryExecutor _qe; public TypeComponentDAL(IQueryExecutor qe) => _qe = qe; public async Task GetTypeComponent(int typeComponentId, LoginDTO login, CancellationToken ct) { var dict = new Dictionary(); await _qe.QueryMultiMapAsync( login, TypeComponentQB.GET_TYPECOMPONENT, (parent, child) => { if (!dict.TryGetValue(parent.TypeComponentId, out var existing)) { existing = parent; existing.TypeComponentDetailArray = new List(); dict[parent.TypeComponentId] = existing; } if (child != null && child.TypeComponentDetailId != 0) existing.TypeComponentDetailArray.Add(child); return existing; }, new { TypeComponentId = typeComponentId, TenantId = login.ClientId }, splitOn: "TypeComponentDetailId" ).ConfigureAwait(false); return dict.Values.FirstOrDefault(); } public async Task> GetTypeComponentListByAssetType(int assetTypeId, LoginDTO login, CancellationToken ct) { return await _qe.QueryAsync(login, TypeComponentQB.GET_TYPECOMPONENT_LIST_BY_ASSETTYPE, new { AssetTypeId = assetTypeId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); } public async Task SaveTypeComponent(TypeComponentDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) { dto.TenantId = login.ClientId; dto.TypeComponentCreatedOn = DateTime.UtcNow; dto.TypeComponentCreatedById = login.UserId; dto.TypeComponentModifiedOn = DateTime.UtcNow; dto.TypeComponentModifiedById = login.UserId; await _qe.ExecuteAsync(login, TypeComponentQB.SAVE_TYPECOMPONENT, dto, tx, cancellationToken: ct).ConfigureAwait(false); } public async Task UpdateTypeComponent(TypeComponentDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) { dto.TypeComponentModifiedOn = DateTime.UtcNow; dto.TypeComponentModifiedById = login.UserId; await _qe.ExecuteAsync(login, TypeComponentQB.UPDATE_TYPECOMPONENT, dto, tx, cancellationToken: ct).ConfigureAwait(false); } public async Task DeleteTypeComponent(int typeComponentId, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync(login, TypeComponentQB.DELETE_TYPECOMPONENT, new { TypeComponentId = typeComponentId, TenantId = login.ClientId }, tx, cancellationToken: ct).ConfigureAwait(false); } public async Task SaveTypeComponentDetail(TypeComponentDetailDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync(login, TypeComponentQB.SAVE_TYPECOMPONENTDETAIL, dto, tx, cancellationToken: ct).ConfigureAwait(false); } public async Task DeleteTypeComponentDetailByComponent(int componentId, LoginDTO login, DbTransaction tx, CancellationToken ct) { await _qe.ExecuteAsync(login, TypeComponentQB.DELETE_TYPECOMPONENTDETAIL_BY_COMPONENT, new { ComponentId = componentId }, tx, cancellationToken: ct).ConfigureAwait(false); } } }