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.Api.iBiz; 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]/CollectNode/GetTransaction")] public IActionResult CollectNode_GetTransaction([FromBody] ApiAction action) { ApiAction response; ICollectNode trans = null; try { if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID)) { trans = iBiz.MES.Context.GetTransDic()[action.ID] as ICollectNode; } 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]/CollectNode/CloseTransaction")] public IActionResult CollectNode_CloseTransaction([FromBody] ApiAction action) { ApiAction response; ICollectNode trans = null; try { if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID)) { trans = iBiz.MES.Context.GetTransDic()[action.ID] as ICollectNode; 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]/CollectNode/Reset")] public async Task CollectNode_ResetAsync([FromBody] ApiAction action) { ApiAction response; ICollectNode trans = null; try { if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID)) { trans = iBiz.MES.Context.GetTransDic()[action.ID] as ICollectNode; 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]/CollectNode/SelectWO")] public async Task CollectNode_SelectWOAsync([FromBody] ApiAction action) { ApiAction response; ICollectNode trans = null; try { if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID)) { trans = iBiz.MES.Context.GetTransDic()[action.ID] as ICollectNode; 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]/CollectNode/Submit")] public async Task CollectNode_SubmitAsync([FromBody] ApiAction action) { ApiAction response; ICollectNode trans = null; try { if (iBiz.MES.Context.GetTransDic().ContainsKey(action.ID)) { trans = iBiz.MES.Context.GetTransDic()[action.ID] as ICollectNode; 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); } } }