using FMDAL.CustomCode.AssetState; using FMDAL.DTO.AssetState; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.Login; using GB5Shared.EventLogPublish; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Telemetry; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using static GB5Shared.GB5Constant.Constant; namespace FMBLL.AssetState { public class AssetStateBLL : IAssetStateBLL { private readonly IAssetStateDAL _dal; private readonly IQueryExecutor _queryExecutor; private readonly AutoNumber _autoNumber; private readonly KeyInvalidate _keyInvalidate; private readonly EventLogPublish _eventLog; private readonly ILogger _logger; public AssetStateBLL( IAssetStateDAL dal, IQueryExecutor queryExecutor, AutoNumber autoNumber, KeyInvalidate keyInvalidate, EventLogPublish eventLog, ILogger logger) { _dal = dal; _queryExecutor = queryExecutor; _autoNumber = autoNumber; _keyInvalidate = keyInvalidate; _eventLog = eventLog; _logger = logger; } public async Task GetAssetState(int assetStateId, LoginDTO login, CancellationToken ct) => await _dal.GetAssetState(assetStateId, login, ct).ConfigureAwait(false); public async Task> GetAssetStateListByType(int assetTypeId, LoginDTO login, CancellationToken ct) => await _dal.GetAssetStateListByType(assetTypeId, login, ct).ConfigureAwait(false); public async Task SaveAssetState(AssetStateDTO dto, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("validate-assetstate", new { dto.AssetStateId, dto.AssetTypeId }); if (string.IsNullOrWhiteSpace(dto.AssetStateCode)) throw new ArgumentException("StateCode is required"); if (string.IsNullOrWhiteSpace(dto.AssetStateName)) throw new ArgumentException("StateName is required"); if (dto.AssetTypeId <= 0) throw new ArgumentException("AssetTypeId is required"); bool isNew = dto.AssetStateId == 0; dto.TenantId = login.ClientId; dto.OrganizationUnitId = dto.OrganizationUnitId > 0 ? dto.OrganizationUnitId : login.WorkOUId; dto.AssetStateModifiedById = login.UserId; dto.AssetStateModifiedOn = DateTime.UtcNow; if (isNew) { dto.AssetStateCreatedById = login.UserId; dto.AssetStateCreatedOn = DateTime.UtcNow; } GB5Trace.Step("save-assetstate", new { dto.AssetStateId, isNew }); await using var tx = await _queryExecutor.BeginTransactionAsync(login).ConfigureAwait(false); try { if (isNew) { var auto = await _autoNumber.GetNumberAsync(1, AUTONUMBERCONSTANT.ASSETSTATE, login).ConfigureAwait(false); dto.AssetStateId = auto.StartNumber; await _dal.SaveAssetState(dto, login, tx, ct).ConfigureAwait(false); } else { await _dal.UpdateAssetState(dto, login, tx, ct).ConfigureAwait(false); } await tx.CommitAsync(ct).ConfigureAwait(false); } catch { await tx.RollbackAsync(ct).ConfigureAwait(false); throw; } var cacheKey = new CacheKeyGeneration().KeyGeneration(dto.AssetStateId, EntityConstant.OBJECTASSETSTATE, CacheKeyLevel.CLIENT_LEVEL, login); await _keyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); var eventTypeId = isNew ? EventTypeConstant.SAVEASSETSTATEEVENTTYPEID : EventTypeConstant.UPDATEASSETSTATEEVENTTYPEID; GB5Trace.Step("event-publish", new { EventTypeId = eventTypeId }); await _eventLog.PublishEventLogAsync( isNew ? "Asset State Created" : "Asset State Updated", dto, eventTypeId, dto.AssetStateId, login, ct: ct).ConfigureAwait(false); return isNew ? $"{SuccessResponse.SaveSuccessMessage} {dto.AssetStateId}" : SuccessResponse.UpdateSuccess; } catch (Exception ex) { GB5Trace.MarkFailed("save-assetstate-failed", ex); _logger.LogError(ex, "SaveAssetState failed for AssetStateId {Id}", dto.AssetStateId); throw; } } public async Task DeleteAssetState(int assetStateId, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("delete-assetstate", new { assetStateId }); await using var tx = await _queryExecutor.BeginTransactionAsync(login).ConfigureAwait(false); try { await _dal.DeleteAssetState(assetStateId, login.UserId, login, tx, ct).ConfigureAwait(false); await tx.CommitAsync(ct).ConfigureAwait(false); } catch { await tx.RollbackAsync(ct).ConfigureAwait(false); throw; } var cacheKey = new CacheKeyGeneration().KeyGeneration(assetStateId, EntityConstant.OBJECTASSETSTATE, CacheKeyLevel.CLIENT_LEVEL, login); await _keyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); GB5Trace.Step("event-publish", new { EventTypeId = EventTypeConstant.DELETEASSETSTATEEVENTTYPEID }); await _eventLog.PublishEventLogAsync( "Asset State Deleted", new { AssetStateId = assetStateId }, EventTypeConstant.DELETEASSETSTATEEVENTTYPEID, assetStateId, login, ct: ct).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } catch (Exception ex) { GB5Trace.MarkFailed("delete-assetstate-failed", ex); _logger.LogError(ex, "DeleteAssetState failed for AssetStateId {Id}", assetStateId); throw; } } } }