using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using WiBLL.WiExecution; using static GB5Shared.GB5Constant.Constant; namespace WiSL.EndPoints.WiExecution { public class ResolveForWorkstation : BaseEndpoint> { private readonly IWiExecutionBLL _bll; public ResolveForWorkstation(IWiExecutionBLL bll) { _bll = bll; } public override void Configure() { Get("/Execution/ResolveForWorkstation"); AllowAnonymous(); } public record Parameters( [property: FromHeader] string Login, [property: QueryParam] int WorkStationId, [property: QueryParam] int TriggerEvent, [property: QueryParam] int? WorkCenterId, [property: QueryParam] int? RoutingId, [property: QueryParam] int? RoutingOperationId, [property: QueryParam] int? ModelId, [property: QueryParam] int? ItemGroupId ); // WiAssignment rows change infrequently (an admin editing SOP-to-workstation // mappings), so a short TTL is the right tradeoff here rather than exact-key // invalidation: the resolve match spans wildcard (-1) dimensions across many // concrete WorkStationId values, and SaveAssignment/DeleteAssignment has no way // to know which cached (WorkStationId, TriggerEvent) keys a given wildcard-bearing // row actually affects — Dapr's state store only supports single-key delete, not // prefix/pattern clear. See ResolveForEntity for the same reasoning. // The 5 dimension params are part of the cache key too — a different product/routing // context resolving against the same workstation must never share a cached result. protected override string? GetCacheKey(Parameters req, LoginDTO login) => $"Wi:ResolveWorkstation:{login.ClientId}:{req.WorkStationId}:{req.TriggerEvent}" + $":{req.WorkCenterId ?? -1}:{req.RoutingId ?? -1}:{req.RoutingOperationId ?? -1}" + $":{req.ModelId ?? -1}:{req.ItemGroupId ?? -1}"; protected override int GetTtlSeconds() => TtlForLevel(CacheKeyLevel.CLIENT_LEVEL); protected override async Task> ExecuteAsync( Parameters req, LoginDTO login, CancellationToken ct) { try { var result = await _bll.ResolveForWorkstation( req.WorkStationId, (WiDAL.DTOs.TriggerEvent)req.TriggerEvent, login, ct, req.WorkCenterId ?? -1, req.RoutingId ?? -1, req.RoutingOperationId ?? -1, req.ModelId ?? -1, req.ItemGroupId ?? -1); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(result, CacheKeyLevel.CLIENT_LEVEL, login); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.CLIENT_LEVEL, login, ex.Message, 500); } } } }