using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WiDAL.CustomCode.ProductionLine; using WiDAL.DTOs; namespace WiBLL.ProductionLine { public class ProductionLineBLL : IProductionLineBLL { private readonly IProductionLineDAL _dal; private readonly AutoNumber _autoNumber; private readonly IValidation _validation; public ProductionLineBLL(IProductionLineDAL dal, AutoNumber autoNumber, IValidation validation) { _dal = dal; _autoNumber = autoNumber; _validation = validation; } public async Task GetProductionLineById(int ProductionLineId, LoginDTO login, CancellationToken ct) => await _dal.GetProductionLineById(ProductionLineId, login, ct).ConfigureAwait(false); public async Task SaveProductionLine(ProductionLineDTO ProductionLineDTO, LoginDTO login, CancellationToken ct) { if (ProductionLineDTO == null) throw new ArgumentNullException(nameof(ProductionLineDTO)); AutoNumberDTO? autoNumberDTO = null; try { ProductionLineDTO.ProductionLineTenantId = login.ClientId; ProductionLineDTO.ProductionLineModifiedById = login.UserId; bool isNew = ProductionLineDTO.ProductionLineId == 0; if (isNew) { autoNumberDTO = await _autoNumber.GetNumberAsync(1, "PRODUCTIONLINE", login); ProductionLineDTO.ProductionLineId = autoNumberDTO.StartNumber; // ID changes here ProductionLineDTO.ProductionLineCreatedById = login.UserId; } // ✅ pass isNew to DAL await _dal.SaveProductionLine(ProductionLineDTO, isNew, login, ct); return isNew ? string.Format(SuccessResponse.SaveSuccessWithCodeAndName, ProductionLineDTO.ProductionLineCode, ProductionLineDTO.ProductionLineName) : string.Format(SuccessResponse.UpdateSuccessWithCodeAndName, ProductionLineDTO.ProductionLineCode, ProductionLineDTO.ProductionLineName); } catch { if (autoNumberDTO != null && ProductionLineDTO.ProductionLineId > 0) { await _autoNumber.RollbackAutoNumber("PRODUCTIONLINE", ProductionLineDTO.ProductionLineId, login); } throw; } } public async Task GetSelectListProductionLine(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _dal.GetSelectListProductionLine(criteriaDTO, login, ct); public async Task DeleteProductionLine(int ProductionLineId, LoginDTO loginDTO) { try { return await _dal.DeleteProductionLine(ProductionLineId, loginDTO); } catch (Exception) { throw; } } public async Task GetListProductionLine(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _dal.GetListProductionLine(criteriaDTO, login, ct); } }