using System; using System.Threading.Tasks; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.EntityHandler; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using GB5Shared.Resource.GB5Solution; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using MMDAL.CustomCode.ItemStock; using MMDAL.DTO.ItemStock; using static GB5Shared.GB5Constant.Constant; namespace MMBLL.ItemStock { public class ItemStockBLL : IItemStockBLL { private readonly IItemStockDAL _ItemStockDAL; private readonly AutoNumber _AutoNumber; private readonly IQueryExecutor _QueryExecutor; private readonly BaseEntityAppService _baseEntityAppService; public ItemStockBLL(IItemStockDAL ItemStockDAL, AutoNumber AutoNumber, IQueryExecutor IQueryExecutor, BaseEntityAppService baseEntityAppService) { _ItemStockDAL = ItemStockDAL; _AutoNumber = AutoNumber; _QueryExecutor = IQueryExecutor; _baseEntityAppService = baseEntityAppService; } /// /// Retrieves a single ItemStock record by its unique identifier. /// /// The ID of the ItemStock to retrieve. /// The current user's login details. /// A JSON string representing the ItemStock details. public async Task GetItemStock(int ItemStockId, LoginDTO LoginDTO) { try { return await _ItemStockDAL.GetItemStock(ItemStockId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListItemStock(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _ItemStockDAL.GetSelectListItemStock(FirstNumber, MaxResult, CriteriaDTO, LoginDTO); } catch (Exception ex) { throw new Exception("Error in GetSelectListItemStock", ex); } } public async Task SaveItemStock(ItemStockDTO ItemStockDTO, LoginDTO LoginDTO) { try { if (ItemStockDTO == null) throw new ArgumentNullException(nameof(ItemStockDTO)); bool isNew = ItemStockDTO.ItemStockId == 0; // Pre-Transaction : Auto Number var ItemStockAutoNumber = await _AutoNumber.GetAutoNumber( 1, AUTONUMBERCONSTANT.ITEMSTOCK, LoginDTO); var Trans = await _QueryExecutor.BeginTransactionAsync(LoginDTO); try { await _baseEntityAppService.ExecuteSaveAsync( EntityConstant.ITEMSTOCK, EventTypeConstant.SAVEITEMSTOCKEVENTTYPEID, ItemStockDTO, LoginDTO, async tx => { if (isNew) { ItemStockDTO.ItemStockId = ItemStockAutoNumber.StartNumber; } int Result; if (isNew) { Result = await _ItemStockDAL.SaveItemStock(ItemStockDTO, LoginDTO, tx); } else { Result = await _ItemStockDAL.UpdateItemStock(ItemStockDTO, LoginDTO, tx); } return Result; }, null, -1, -1, Trans, isNew); await _QueryExecutor.CommitAsync(Trans); if (isNew) { return SuccessResponse.SaveSuccess; } else { return SuccessResponse.UpdateSuccess; } } catch (Exception) { await _QueryExecutor.RollbackAsync(Trans); throw; } } catch (Exception) { throw; } } public async Task DeleteItemStock(int ItemStockId, LoginDTO LoginDTO) { try { return await _ItemStockDAL.DeleteItemStock(ItemStockId, LoginDTO); } catch (Exception) { throw; } } } }