using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FrameworkDAL.CustomCode.DocumentSet; using FrameworkDAL.CustomCode.WorkFlow; using FrameworkDAL.DTO.DocumentSet; using FrameworkDAL.Query.DocumentSet; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.PubSub; using GB5Shared.GenerateAutoNumber; using GB5Shared.PubSub.OutBox; using GB5Shared.ResponseStandard; using GB5Shared.Validation; using GB5Shared.WorkFlow; using static GB5Shared.GB5Constant.Constant; namespace FrameworkBLL.DocumentSet { public class DocumentSetBLL : IDocumentSetBLL { private readonly IDocumentSetDAL _DocumentSetDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly IOutBox _outBox; public DocumentSetBLL(IDocumentSetDAL DocumentSetDAL, AutoNumber AutoNumber, IValidation Validation, IOutBox outBox ) { _DocumentSetDAL = DocumentSetDAL; _AutoNumber = AutoNumber; _Validation = Validation; _outBox = outBox; } public async Task GetDocumentSet(int DocumentSetId, LoginDTO LoginDTO) { try { return await _DocumentSetDAL.GetDocumentSet(DocumentSetId, LoginDTO); } catch (Exception) { throw; } } /// /// Retrieves a paginated list of DocumentSet records based on specified filter criteria. /// Can optionally return the total count of records instead of the list. /// /// The starting index of records to fetch (for pagination). /// The maximum number of records to fetch. /// Filter criteria for selecting DocumentSet records. /// The current user's login details. /// If true, returns the total count of matching records instead of the list. /// A JSON string representing either the list of DocumentSet records or the total count. public async Task GetSelectListDocumentSet(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO loginDTO, bool isCount = false) { try { return await _DocumentSetDAL.GetSelectListDocumentSet(firstNumber, maxResult, criteriaDTO, loginDTO, isCount); } catch (Exception) { throw; } } public async Task SaveDocumentSet(DocumentSetDTO DocumentSetDTO, LoginDTO loginDTO) { ArgumentNullException.ThrowIfNull(DocumentSetDTO); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(DocumentSetDTO.DocumentSetCode, nameof(DocumentSetDTO.DocumentSetCode)); await _Validation.NotEmpty(DocumentSetDTO.DocumentSetName, nameof(DocumentSetDTO.DocumentSetName)); DocumentSetDTO.DocumentSetModifiedById = loginDTO.UserId; DocumentSetDTO.DocumentSetModifiedOn = DateTime.UtcNow; const int count = 1; if (DocumentSetDTO.DocumentSetId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "DOCUMENTSET", loginDTO); DocumentSetDTO.DocumentSetId = autoNumberDTO.StartNumber; DocumentSetDTO.DocumentSetCreatedById = loginDTO.UserId; DocumentSetDTO.DocumentSetCreatedOn = DateTime.UtcNow; if (DocumentSetDTO.DocumentSetDetailArray != null) { foreach (var child in DocumentSetDTO.DocumentSetDetailArray) { child.DocumentSetDetailId = DocumentSetDTO.DocumentSetId++; child.DocumentSetId = DocumentSetDTO.DocumentSetId; } } await _DocumentSetDAL.SaveDocumentSet(DocumentSetDTO, loginDTO); //await _outBox.PublishEventAsync(new OutboxDTO //{ // ObjectTypeId = EntityConstant.OBJECTDOCUMENTSET, // ObjectId = DocumentSetDTO.DocumentSetId, // EventTypeId = EventTypeConstant.SAVEDOCUMENTSET, // Version = 1, // Payload = DocumentSetDTO //}, loginDTO); return $"DocumentSet saved successfully with DocumentSetId {DocumentSetDTO.DocumentSetId}."; } else { await _DocumentSetDAL.UpdateDocumentSet(DocumentSetDTO, loginDTO); //await _outBox.PublishEventAsync(new OutboxDTO //{ // ObjectTypeId = EntityConstant.OBJECTDOCUMENTSET, // ObjectId = DocumentSetDTO.DocumentSetId, // EventTypeId = EventTypeConstant.SAVEDOCUMENTSET, // Payload = DocumentSetDTO //}, loginDTO); return $"DocumentSet updated successfully with DocumentSetId {DocumentSetDTO.DocumentSetId}."; } } catch { if (autoNumberDTO != null && DocumentSetDTO.DocumentSetId > 0) await _AutoNumber.RollbackAutoNumber("DOCUMENTSET", DocumentSetDTO.DocumentSetId, loginDTO); throw; } } public async Task> DeleteDocumentSet(int DocumentSetId, LoginDTO LoginDTO) { try { var result = await _DocumentSetDAL.DeleteDocumentSet(DocumentSetId, LoginDTO); if (result.IsSuccess) { //await _outBox.PublishEventAsync(new OutboxDTO //{ // ObjectTypeId = EntityConstant.OBJECTDOCUMENTSET, // ObjectId = DocumentSetId, // EventTypeId = EventTypeConstant.SAVEACTIONEVENTTYPEID, //wrong // Version = 0, // Payload = new { DocumentSetId = DocumentSetId } //}, LoginDTO); } return result; } catch (Exception) { throw; } } } }