using EnablementDAL.CustomCode.ShowChannel; using EnablementDAL.DTO; using GB5Shared.DTO.Framework.Enum; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.EventLogPublish; using GB5Shared.GB5Exception; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using Microsoft.Extensions.Logging; namespace EnablementBLL.ShowChannel; public class ShowChannelBLL : IShowChannelBLL { private const byte SUBSCRIPTION_OPTIONAL = 2; private const byte AUTOSUBSCRIBE_MANUALONLY = 3; private readonly IShowChannelDAL _dal; private readonly AutoNumber _autoNumber; private readonly EventLogPublish _eventLog; private readonly ILogger? _logger; public ShowChannelBLL( IShowChannelDAL dal, AutoNumber autoNumber, EventLogPublish eventLog, ILogger? logger = null) { _dal = dal; _autoNumber = autoNumber; _eventLog = eventLog; _logger = logger; } // ------------------------------------------------------------------------- // MSHOWCHANNEL — Admin CRUD // ------------------------------------------------------------------------- public async Task> GetShowChannelAsync(int channelId, LoginDTO login, CancellationToken ct) { if (channelId <= 0) throw new ArgumentException("ChannelId must be greater than zero."); var dto = await _dal.GetShowChannelAsync(channelId, login, ct).ConfigureAwait(false); if (dto is null) throw new NotFoundException($"ShowChannel {channelId} not found."); return OkResponse(dto); } public async Task> GetShowChannelListAsync( int pageNo, int pageSize, LoginDTO login, CancellationToken ct) { if (pageNo < 1) throw new ArgumentException("PageNo must be >= 1."); if (pageSize < 1) throw new ArgumentException("PageSize must be >= 1."); var items = await _dal.GetShowChannelListAsync(pageNo, pageSize, login, ct).ConfigureAwait(false); int total = await _dal.GetShowChannelListCountAsync(login, ct).ConfigureAwait(false); return OkResponse(new { Items = items, TotalCount = total }); } public async Task> GetSelectListShowChannelAsync(LoginDTO login, CancellationToken ct) { var items = await _dal.GetSelectListShowChannelAsync(login, ct).ConfigureAwait(false); return OkResponse(items); } public async Task> SaveShowChannelAsync(ShowChannelDTO dto, LoginDTO login, CancellationToken ct) { if (string.IsNullOrWhiteSpace(dto.ChannelCode)) throw new ArgumentException("ChannelCode must not be empty."); if (string.IsNullOrWhiteSpace(dto.ChannelName)) throw new ArgumentException("ChannelName must not be empty."); if (dto.SubscriptionType > SUBSCRIPTION_OPTIONAL) throw new ArgumentException("SubscriptionType must be in range 0-2."); if (dto.AutoSubscribeRule > AUTOSUBSCRIBE_MANUALONLY) throw new ArgumentException("AutoSubscribeRule must be in range 0-3."); dto.TenantId = login.ClientId; dto.ModifiedById = login.UserId; bool isNew = dto.ChannelId <= 0; if (isNew) { var an = await _autoNumber.GetNumberAsync(1, "SHOWCHANNEL", login).ConfigureAwait(false); dto.ChannelId = an.StartNumber; dto.CreatedById = login.UserId; dto.SourceType = login.SourceType; await _dal.InsertShowChannelAsync(dto, login, ct).ConfigureAwait(false); _ = _eventLog.PublishEventLogAsync("ShowChannel Created", dto, 1, dto.ChannelId, login, ct: ct); } else { await _dal.UpdateShowChannelAsync(dto, login, ct).ConfigureAwait(false); _ = _eventLog.PublishEventLogAsync("ShowChannel Updated", dto, 1, dto.ChannelId, login, ct: ct); } _logger?.LogInformation("ShowChannel saved ChannelId={ChannelId} ChannelCode={ChannelCode}", dto.ChannelId, dto.ChannelCode); return OkResponse(isNew ? $"{SuccessResponse.SaveSuccessMessage} {dto.ChannelId}" : SuccessResponse.UpdateSuccess); } public async Task> DeleteShowChannelAsync(int channelId, LoginDTO login, CancellationToken ct) { if (channelId <= 0) throw new ArgumentException("ChannelId must be greater than zero."); await _dal.DeleteShowChannelAsync(channelId, login, ct).ConfigureAwait(false); _ = _eventLog.PublishEventLogAsync("ShowChannel Deleted", new { ChannelId = channelId }, 1, channelId, login, ct: ct); return OkResponse(SuccessResponse.DeleteSuccessMessage); } // ------------------------------------------------------------------------- // TSHOWCHANNEL — Show-to-Channel assignment (many-to-many) // ------------------------------------------------------------------------- public async Task> AssignChannelToShowAsync(ShowChannelAssignmentDTO dto, LoginDTO login, CancellationToken ct) { if (dto.ShowId <= 0) throw new ArgumentException("ShowId must be greater than zero."); if (dto.ChannelId <= 0) throw new ArgumentException("ChannelId must be greater than zero."); var an = await _autoNumber.GetNumberAsync(1, "SHOWCHANNELASSIGNMENT", login).ConfigureAwait(false); dto.ShowChannelId = an.StartNumber; dto.CreatedById = login.UserId; dto.ModifiedById = login.UserId; dto.SourceType = login.SourceType; dto.TenantId = login.ClientId; await _dal.AssignChannelToShowAsync(dto, login, ct).ConfigureAwait(false); _ = _eventLog.PublishEventLogAsync("ShowChannel Assigned", dto, 1, dto.ShowChannelId, login, ct: ct); _logger?.LogInformation("ShowChannel assigned ShowId={ShowId} ChannelId={ChannelId}", dto.ShowId, dto.ChannelId); return OkResponse($"{SuccessResponse.SaveSuccessMessage} {dto.ShowChannelId}"); } public async Task> UnassignChannelFromShowAsync(ShowChannelAssignmentDTO dto, LoginDTO login, CancellationToken ct) { if (dto.ShowId <= 0) throw new ArgumentException("ShowId must be greater than zero."); if (dto.ChannelId <= 0) throw new ArgumentException("ChannelId must be greater than zero."); await _dal.UnassignChannelFromShowAsync(dto.ShowId, dto.ChannelId, login, ct).ConfigureAwait(false); _ = _eventLog.PublishEventLogAsync("ShowChannel Unassigned", dto, 1, dto.ShowId, login, ct: ct); return OkResponse(SuccessResponse.DeleteSuccessMessage); } public async Task> GetChannelsForShowAsync(int showId, LoginDTO login, CancellationToken ct) { if (showId <= 0) throw new ArgumentException("ShowId must be greater than zero."); var items = await _dal.GetChannelsForShowAsync(showId, login, ct).ConfigureAwait(false); return OkResponse(items); } private static ResponseStandardDTO OkResponse(object body) => new() { Body = body, Status = FrameworkEnumDTO.ResponseStatus.Ok }; }