using Autofac;
using Microsoft.AspNetCore.Mvc;
using Rhea.Common;
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Xml.Linq;
using Tiger.IBusiness;
using Tiger.Model;
using Tiger.Model.Entitys.MES.Position;
namespace Tiger.Api.Controllers.MES
{
public partial class MESController : ControllerBase
{
///
/// GetTransaction(ApiAction(Data:NewPositionInput))
///
///
///
[HttpPost]
[Route("api/[controller]/TestNode/GetTransaction")]
public IActionResult TestNode_GetTransaction([FromBody] ApiAction action)
{
ApiAction response;
ITestNode trans = null;
try
{
if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID))
{
trans = iBiz.MES.Context.GetTransDic()[action.ID] as ITestNode;
}
else
{
trans = AutoFacContainer.Instance.Resolve().Init(action.ID, Request.Host.Value, action.Data?.USER_CODE, action.Data?.POST_CODE);
iBiz.MES.Context.NewTransaction(HttpContext, trans);
}
response = action.GetResponse();
}
catch (System.IO.InvalidDataException ex)
{
response = action.GetResponse();
response.IsSuccessed = false;
response.LocaleMsg = new(ex.Message, ex.InnerException.Message.Split('|', System.StringSplitOptions.RemoveEmptyEntries));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
trans?.AddHistory(Request, action);
return Ok(response);
}
///
/// CloseTransaction(ApiAction)
/// 关闭事务
///
///
///
[HttpPost]
[Route("api/[controller]/TestNode/CloseTransaction")]
public IActionResult TestNode_CloseTransaction([FromBody] ApiAction action)
{
ApiAction response;
ITestNode trans = null;
try
{
if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID))
{
trans = iBiz.MES.Context.GetTransDic()[action.ID] as ITestNode;
if (!trans.IsFinished)
{
if (action.IsAsync)
{
response = action.GetResponse(trans.Close());
}
else
{
lock (trans.TransLock) { response = action.GetResponse(trans.Close()); }
}
response.Message = $"岗位[{trans.PostCode}]的测试工序事务[ID:{action.ID}]关闭{(response.IsSuccessed ? "成功" : "失败")}";
}
else
{
response = action.GetResponse($"Transaction Error: 岗位[{trans.PostCode}]的测试工序事务[ID:{action.ID}]已经关闭", false);
}
}
else
{
response = action.GetResponse($"Transaction Error: 岗位的测试工序事务[ID:{action.ID}]已经被关闭", false);
}
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
trans?.AddHistory(Request, action);
return Ok(response);
}
///
/// Reset(ApiAction)
/// 重置工序操作
///
///
///
[HttpPost]
[Route("api/[controller]/TestNode/Reset")]
public async Task TestNode_ResetAsync([FromBody] ApiAction action)
{
ApiAction response;
ITestNode trans = null;
try
{
if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID))
{
trans = iBiz.MES.Context.GetTransDic()[action.ID] as ITestNode;
if (!trans.IsFinished)
{
lock (trans.TransLock) { response = action.GetResponse(trans.Reset()); }
}
else
{
response = action.GetResponse($"Transaction Error: 岗位[{trans.PostCode}]的测试工序事务[ID:{action.ID}]已经关闭", false);
}
}
else
{
response = action.GetResponse($"Transaction Error: 岗位的测试工序事务[ID:{action.ID}]已经被关闭", false);
}
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
trans?.AddHistory(Request, action);
return Ok(response);
}
///
/// SelectWO(ApiAction(Data:{WoInput}))
/// 测试工序:选择工单
///
///
///
[HttpPost]
[Route("api/[controller]/TestNode/SelectWO")]
public async Task TestNode_SelectWOAsync([FromBody] ApiAction action)
{
ApiAction response;
ITestNode trans = null;
try
{
if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID))
{
trans = iBiz.MES.Context.GetTransDic()[action.ID] as ITestNode;
if (!trans.IsFinished)
{
if (action.IsAsync)
{
response = action.GetResponse(await trans.SelectOrder(action.Data));
}
else
{
lock (trans.TransLock) { response = action.GetResponse(trans.SelectOrder(action.Data).Result); }
}
}
else
{
response = action.GetResponse($"Transaction Error: 岗位[{trans.PostCode}]的测试工序事务[ID:{action.ID}]已经关闭", false);
}
}
else
{
response = action.GetResponse($"Transaction Error: 岗位的测试工序事务[ID:{action.ID}]已经被关闭", false);
}
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
trans?.AddHistory(Request, action);
return Ok(response);
}
///
/// Submit(ApiAction(Data:{SubmitInput}))
/// 测试工序:提交操作数据
///
///
///
[HttpPost]
[Route("api/[controller]/TestNode/Submit")]
public async Task TestNode_SubmitAsync([FromBody] ApiAction action)
{
ApiAction response;
ITestNode trans = null;
try
{
if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID))
{
trans = iBiz.MES.Context.GetTransDic()[action.ID] as ITestNode;
if (!trans.IsFinished)
{
if (action.IsAsync)
{
response = action.GetResponse(await trans.Submit(action.Data));
}
else
{
lock (trans.TransLock) { response = action.GetResponse(trans.Submit(action.Data).Result); }
}
}
else
{
response = action.GetResponse($"Transaction Error: 岗位[{trans.PostCode}]的测试工序事务[ID:{action.ID}]已经关闭", false);
}
}
else
{
response = action.GetResponse($"Transaction Error: 岗位的测试工序事务[ID:{action.ID}]已经被关闭", false);
}
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
trans?.AddHistory(Request, action);
return Ok(response);
}
///
/// GetDefects(ApiAction)
/// 测试工序:获取工序不良代码
///
///
///
[HttpPost]
[Route("api/[controller]/TestNode/GetDefects")]
public async Task TestNode_GetDefectsAsync([FromBody] ApiAction action)
{
ApiAction response;
ITestNode trans = null;
try
{
if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID))
{
trans = iBiz.MES.Context.GetTransDic()[action.ID] as ITestNode;
if (!trans.IsFinished)
{
response = action.GetResponse(trans.GetDefects());
}
else
{
response = action.GetResponse($"Transaction Error: 岗位[{trans.PostCode}]的测试工序事务[ID:{action.ID}]已经关闭", false);
}
}
else
{
response = action.GetResponse($"Transaction Error: 岗位的测试工序事务[ID:{action.ID}]已经被关闭", false);
}
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
trans?.AddHistory(Request, action);
return Ok(response);
}
}//endClass
}