using System; using System.Threading.Tasks; using FrameworkDAL.CustomCode.SysJobShare; using FrameworkDAL.DTO.SysJobShare; using FrameworkBLL.SystemJob; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; namespace FrameworkBLL.SysJobShare { public class SysJobShareBLL : ISysJobShareBLL { private readonly ISysJobShareDAL _SysJobShareDAL; private readonly AutoNumber _AutoNumber; private readonly ISysJobHubNotifier _hub; public SysJobShareBLL( ISysJobShareDAL sysJobShareDAL, AutoNumber autoNumber, ISysJobHubNotifier hub) { _SysJobShareDAL = sysJobShareDAL; _AutoNumber = autoNumber; _hub = hub; } public async Task GetSysJobShare(int SysJobShareId, LoginDTO LoginDTO) { try { return await _SysJobShareDAL.GetSysJobShare(SysJobShareId, LoginDTO); } catch (Exception) { throw; } } public async Task GetListSysJobShare(int SysJobId, LoginDTO LoginDTO) { try { return await _SysJobShareDAL.GetListSysJobShare(SysJobId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveSysJobShare(SysJobShareDTO SysJobShareDTO, LoginDTO LoginDTO) { try { if (SysJobShareDTO.SysJobShareId == 0) { var autoNumber = await _AutoNumber.GetAutoNumber(1, "SYSJOBSHARE", LoginDTO); SysJobShareDTO.SysJobShareId = autoNumber.StartNumber; SysJobShareDTO.SharedById = LoginDTO.UserId; SysJobShareDTO.SharedOn = DateTime.UtcNow; SysJobShareDTO.TenantId = LoginDTO.ClientId; int rows = await _SysJobShareDAL.SaveSysJobShare(SysJobShareDTO, LoginDTO); if (rows == 0) return "Share could not be created: you must own the job, and the user must not already have access."; await _hub.NotifyJobSharedAsync( SysJobShareDTO.SharedWithUserId, SysJobShareDTO.SysJobId, LoginDTO.UserName); return SuccessResponse.SaveSuccess; } else { SysJobShareDTO.SharedById = LoginDTO.UserId; SysJobShareDTO.TenantId = LoginDTO.ClientId; await _SysJobShareDAL.UpdateSysJobShare(SysJobShareDTO, LoginDTO); return SuccessResponse.UpdateSuccess; } } catch (Exception) { throw; } } public async Task> DeleteSysJobShare(int SysJobShareId, LoginDTO LoginDTO) { try { return await _SysJobShareDAL.DeleteSysJobShare(SysJobShareId, LoginDTO); } catch (Exception) { throw; } } } }