using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; 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 MMDAL.CustomCode.PackSet; using MMDAL.DTO.Pack; namespace MMBLL.PackSet { public class PackSetBLL : IPackSetBLL { private readonly IPackSetDAL _PackSetDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public PackSetBLL(IPackSetDAL PackSetDAL, AutoNumber AutoNumber, IValidation Validation) { _PackSetDAL = PackSetDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetPackSet(int PackSetId, LoginDTO LoginDTO) { try { return await _PackSetDAL.GetPackSet(PackSetId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListPackSet(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _PackSetDAL.GetSelectListPackSet(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task SavePackSet(PackSetDTO PackSetDTO, LoginDTO LoginDTO) { if (PackSetDTO == null) throw new ArgumentNullException(nameof(PackSetDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(PackSetDTO.PackSetCode, nameof(PackSetDTO.PackSetCode)); await _Validation.NotEmpty(PackSetDTO.PackSetName, nameof(PackSetDTO.PackSetName)); PackSetDTO.PackSetModifiedById = LoginDTO.UserId; PackSetDTO.PackSetModifiedOn = DateTime.UtcNow; bool isNew = PackSetDTO.PackSetId == 0; if (isNew) { int detailCount = PackSetDTO.PackSetDetailArray?.Count ?? 0; int totalRequired = 1 + detailCount; autoNumberDTO = await _AutoNumber.GetNumberAsync(totalRequired, "PACKSET", LoginDTO); int nextId = autoNumberDTO.StartNumber; PackSetDTO.PackSetId = nextId++; PackSetDTO.PackSetCreatedById = LoginDTO.UserId; PackSetDTO.PackSetCreatedOn = DateTime.UtcNow; if (PackSetDTO.PackSetDetailArray?.Count > 0) { foreach (var detail in PackSetDTO.PackSetDetailArray) { detail.PackSetDetailId = nextId++; detail.PackSetId = PackSetDTO.PackSetId; } } await _PackSetDAL.SavePackSet(PackSetDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessName} {PackSetDTO.PackSetName}"; } else { await _PackSetDAL.UpdatePackSet(PackSetDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessName} {PackSetDTO.PackSetName}"; } } catch { if (autoNumberDTO != null && PackSetDTO.PackSetId > 0) { await _AutoNumber.RollbackAutoNumber("PackSet", autoNumberDTO.StartNumber, LoginDTO); } throw; } } public async Task DeletePackSet(int PackSetId, LoginDTO LoginDTO) { try { int detailsResult = await _PackSetDAL.DeletePackSetDetail(PackSetId, LoginDTO); if (detailsResult <= 0) return ErrorResponse.DeleteNotFoundMessage; int parentResult = await _PackSetDAL.DeletePackSet(PackSetId, LoginDTO); if (parentResult <= 0) return ErrorResponse.DeleteNotFoundMessage; return SuccessResponse.DeleteSuccessMessage; } catch (Exception ex) { throw new Exception($"Error Deleting PackSet ID {PackSetId}", ex); } } } }