using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WiDAL.DTOs; using WiDAL.Query.Wi; namespace WiDAL.CustomCode.ProductionLine { public class ProductionLineDAL : IProductionLineDAL { private readonly IQueryExecutor _qe; private readonly IValidation _Validation; public ProductionLineDAL(IQueryExecutor qe, IValidation validation) { _qe = qe; _Validation = validation; } public async Task GetProductionLineById(int ProductionLineId, LoginDTO login, CancellationToken ct) { var ProductionLines = (await _qe.QueryAsync(login, ProductionLineQB.GET_BY_ID, new { productionLineId = ProductionLineId, TenantId = login.ClientId }, cancellationToken: ct)).ToList(); return JsonConvert.SerializeObject(ProductionLines); } public async Task SaveProductionLine(ProductionLineDTO dto, bool isNew, LoginDTO login, CancellationToken ct) { try { string sql = isNew ? ProductionLineQB.SAVE_PRODUCTIONLINE : ProductionLineQB.UPDATE_PRODUCTIONLINE; var rows = await _qe.ExecuteAsync(login, sql, dto); // 🔥 important check if (rows == 0) { throw new Exception(isNew ? "Insert failed!" : "Update failed! No matching record."); } return dto.ProductionLineId; } catch (Exception ex) { string error = await _Validation.HandleException(ex, isNew ? ErrorResponse.SaveErrorMessage : ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task GetSelectListProductionLine(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync( login, ProductionLineQB.GET_SELECTLIST_PRODUCTIONLINE); return JsonConvert.SerializeObject(result); } public async Task DeleteProductionLine(int ProductionLineId, LoginDTO loginDTO) { try { string sql = ProductionLineQB.DELETE_PRODUCTIONLINE; var parameter = new { productionLineId = ProductionLineId }; var result = await _qe.ExecuteAsync(loginDTO, sql, parameter); if (result > 0) return SuccessResponse.DeleteSuccessMessage; else return ErrorResponse.DeleteNotFoundMessage; } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage); throw new Exception(error); } } public async Task GetListProductionLine(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync( login, ProductionLineQB.GET_LIST_PRODUCTIONLINE); return JsonConvert.SerializeObject(result); } public async Task> GetLinesForTenant(LoginDTO login, CancellationToken ct) { return await _qe.QueryAsync(login, ProductionLineQB.GET_LINES_FOR_TENANT, new { TenantId = login.ClientId }, cancellationToken: ct); } } }