using EnablementDAL.CustomCode.ShowSpace; 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.ShowSpace; public class ShowSpaceBLL : IShowSpaceBLL { private const byte SUBSCRIPTION_OPTIONAL = 2; private const byte AUTOSUBSCRIBE_MANUALONLY = 3; private readonly IShowSpaceDAL _dal; private readonly AutoNumber _autoNumber; private readonly EventLogPublish _eventLog; private readonly ILogger? _logger; public ShowSpaceBLL( IShowSpaceDAL dal, AutoNumber autoNumber, EventLogPublish eventLog, ILogger? logger = null) { _dal = dal; _autoNumber = autoNumber; _eventLog = eventLog; _logger = logger; } public async Task> GetShowSpaceAsync(int spaceId, LoginDTO login, CancellationToken ct) { if (spaceId <= 0) throw new ArgumentException("SpaceId must be greater than zero."); var dto = await _dal.GetShowSpaceAsync(spaceId, login, ct).ConfigureAwait(false); if (dto is null) throw new NotFoundException($"ShowSpace {spaceId} not found."); return OkResponse(dto); } public async Task> GetShowSpaceListAsync( 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.GetShowSpaceListAsync(pageNo, pageSize, login, ct).ConfigureAwait(false); int total = await _dal.GetShowSpaceListCountAsync(login, ct).ConfigureAwait(false); return OkResponse(new { Items = items, TotalCount = total }); } public async Task> GetSelectListShowSpaceAsync(LoginDTO login, CancellationToken ct) { var items = await _dal.GetSelectListShowSpaceAsync(login, ct).ConfigureAwait(false); return OkResponse(items); } public async Task> SaveShowSpaceAsync(ShowSpaceDTO dto, LoginDTO login, CancellationToken ct) { if (string.IsNullOrWhiteSpace(dto.SpaceCode)) throw new ArgumentException("SpaceCode must not be empty."); if (string.IsNullOrWhiteSpace(dto.SpaceName)) throw new ArgumentException("SpaceName 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.SpaceId <= 0; if (isNew) { var an = await _autoNumber.GetNumberAsync(1, "SHOWSPACE", login).ConfigureAwait(false); dto.SpaceId = an.StartNumber; dto.CreatedById = login.UserId; dto.SourceType = login.SourceType; await _dal.InsertShowSpaceAsync(dto, login, ct).ConfigureAwait(false); _ = _eventLog.PublishEventLogAsync("ShowSpace Created", dto, 1, dto.SpaceId, login, ct: ct); } else { await _dal.UpdateShowSpaceAsync(dto, login, ct).ConfigureAwait(false); _ = _eventLog.PublishEventLogAsync("ShowSpace Updated", dto, 1, dto.SpaceId, login, ct: ct); } _logger?.LogInformation("ShowSpace saved SpaceId={SpaceId} SpaceCode={SpaceCode}", dto.SpaceId, dto.SpaceCode); return OkResponse(isNew ? $"{SuccessResponse.SaveSuccessMessage} {dto.SpaceId}" : SuccessResponse.UpdateSuccess); } public async Task> DeleteShowSpaceAsync(int spaceId, LoginDTO login, CancellationToken ct) { if (spaceId <= 0) throw new ArgumentException("SpaceId must be greater than zero."); await _dal.DeleteShowSpaceAsync(spaceId, login, ct).ConfigureAwait(false); _ = _eventLog.PublishEventLogAsync("ShowSpace Deleted", new { SpaceId = spaceId }, 1, spaceId, login, ct: ct); return OkResponse(SuccessResponse.DeleteSuccessMessage); } private static ResponseStandardDTO OkResponse(object body) => new() { Body = body, Status = FrameworkEnumDTO.ResponseStatus.Ok }; }