using FrameworkDAL.CustomCode.SystemJob; using FrameworkDAL.DTO.SystemJob; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using Newtonsoft.Json; using System; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace FrameworkBLL.SystemJob { public class SystemJobBLL : ISystemJobBLL { private readonly ISystemJobDAL _SystemJobDAL; private readonly AutoNumber _AutoNumber; public SystemJobBLL(ISystemJobDAL ISystemJobDAL, AutoNumber autoNumber) { _SystemJobDAL = ISystemJobDAL; _AutoNumber = autoNumber; } public async Task CheckJobEligibility(int WebServiceId, LoginDTO LoginDTO) { try { return await _SystemJobDAL.CheckJobEligibility(WebServiceId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSystemJob(int SysJobId, LoginDTO LoginDTO) { try { return await _SystemJobDAL.GetSystemJob(SysJobId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListSystemJob(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _SystemJobDAL.GetSelectListSystemJob(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task SaveSystemJob(SystemJobDTO systemJobDTO, LoginDTO loginDTO) { try { ValidateDeliveryChannel(systemJobDTO); if (systemJobDTO.SysJobId == 0) { systemJobDTO.SubmittedById = loginDTO.UserId; systemJobDTO.SubmittedOn = DateTime.UtcNow; systemJobDTO.SysJobCreatedById = loginDTO.UserId; systemJobDTO.SysJobCreatedOn = DateTime.UtcNow; systemJobDTO.SysJobModifiedById = loginDTO.UserId; systemJobDTO.SysJobModifiedOn = DateTime.UtcNow; systemJobDTO.SysJobTenantId = loginDTO.ClientId; systemJobDTO.LastUpdate = DateTime.UtcNow; // Save and get generated identity systemJobDTO.SysJobId = await _SystemJobDAL.SaveSystemJob(systemJobDTO, loginDTO); return SuccessResponse.SaveSuccess; } else { systemJobDTO.SysJobModifiedById = loginDTO.UserId; systemJobDTO.SysJobModifiedOn = DateTime.UtcNow; systemJobDTO.SysJobTenantId = loginDTO.ClientId; await _SystemJobDAL.UpdateSystemJob(systemJobDTO, loginDTO); return SuccessResponse.UpdateSuccess; } } catch { throw; } } private static readonly Regex EmailPattern = new(@"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.Compiled); private static readonly Regex PhonePattern = new(@"^\+?[0-9]{7,15}$", RegexOptions.Compiled); private static void ValidateDeliveryChannel(SystemJobDTO systemJobDTO) { if (!systemJobDTO.DeliveryChannel.HasValue) return; var destination = systemJobDTO.DeliveryDestination?.Trim(); switch (systemJobDTO.DeliveryChannel.Value) { case SysJobDeliveryChannel.AppChat: // In-app notification — no destination needed. return; case SysJobDeliveryChannel.Email: if (string.IsNullOrWhiteSpace(destination) || !EmailPattern.IsMatch(destination)) throw new ArgumentException("DeliveryDestination must be a valid email address when DeliveryChannel is Email."); return; case SysJobDeliveryChannel.WhatsApp: case SysJobDeliveryChannel.SMS: if (string.IsNullOrWhiteSpace(destination) || !PhonePattern.IsMatch(destination)) throw new ArgumentException("DeliveryDestination must be a valid phone number when DeliveryChannel is WhatsApp or SMS."); return; default: throw new ArgumentException($"Unsupported DeliveryChannel: {systemJobDTO.DeliveryChannel.Value}."); } } public async Task> DeleteSystemJob(int SysJobId, LoginDTO LoginDTO) { try { return await _SystemJobDAL.DeleteSystemJob(SysJobId, LoginDTO); } catch (Exception) { throw; } } public async Task GetListSystemJob(int? RunStatus, string SysJobType, string QueueName, DateTime? DateFrom, DateTime? DateTo, string SearchText, int FirstNumber, int MaxResult, LoginDTO LoginDTO) { try { return await _SystemJobDAL.GetListSystemJob(RunStatus, SysJobType, QueueName, DateFrom, DateTo, SearchText, FirstNumber, MaxResult, LoginDTO); } catch (Exception) { throw; } } public async Task GetListCountSystemJob(int? RunStatus, string SysJobType, string QueueName, DateTime? DateFrom, DateTime? DateTo, string SearchText, LoginDTO LoginDTO) { try { return await _SystemJobDAL.GetListCountSystemJob(RunStatus, SysJobType, QueueName, DateFrom, DateTo, SearchText, LoginDTO); } catch (Exception) { throw; } } public async Task GetJobSummary(DateTime? DateFrom, DateTime? DateTo, LoginDTO LoginDTO) { try { return await _SystemJobDAL.GetJobSummary(DateFrom, DateTo, LoginDTO); } catch (Exception) { throw; } } public async Task> CancelSystemJob(int SysJobId, LoginDTO LoginDTO) { try { return await _SystemJobDAL.CancelSystemJob(SysJobId, LoginDTO); } catch (Exception) { throw; } } public async Task CloneSystemJob(int SysJobId, LoginDTO LoginDTO) { try { string json = await _SystemJobDAL.GetSystemJob(SysJobId, LoginDTO); var list = JsonConvert.DeserializeObject>(json); if (list == null || list.Count == 0) return "Source job not found."; var source = list[0]; source.SysJobId = 0; source.RunStatus = 0; source.RetryCount = 0; source.ResultLocation = null; source.ErrorMessage = null; source.SubmittedOn = DateTime.UtcNow; source.StartTime = DateTime.UtcNow; source.EndTime = DateTime.UtcNow; return await SaveSystemJob(source, LoginDTO); } catch (Exception) { throw; } } public async Task GetJobResultPath(int SysJobId, LoginDTO LoginDTO) { try { string json = await _SystemJobDAL.GetSystemJob(SysJobId, LoginDTO); var list = JsonConvert.DeserializeObject>(json); if (list == null || list.Count == 0) return null; return list[0].ResultLocation; } catch (Exception) { throw; } } } }