using BPA.MES.Base.Application.Services;
using BPA.MES.Base.Application.Services.AGVService.Dtos;
using Furion.JsonSerialization;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Diagnostics;

namespace BPA.MES.Base.Application
{
    public class AGVAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 执行操作前后
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            // 获取 HttpContext 和 HttpRequest 对象
            var httpContext = context.HttpContext;
            var httpRequest = httpContext.Request;
            var sw = new Stopwatch();
            sw.Start();
            var actionContext = await next();
            sw.Stop();
            var server = App.GetService<IAGVThirdPartyService>();
            var contentstring = JSON.Serialize(context.ActionArguments["input"]);
            AGVLoadAndUnloadRequest input = JSON.Deserialize<AGVLoadAndUnloadRequest>(contentstring);
            Roller_JobRequest roller_Job = new()
            {
                AgvCode = input.Body.Event.agvCode,
                Complete = true,
                JobId = input.Body.Event.jobId,
                MsgId = input.Body.Event.msgId
            };
            await server.RollerJobExecute(roller_Job);
        }
    }
}
