using Tiger.Model; using SqlSugar; using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Rhea.Common; using System.Net; using System.Linq; using Newtonsoft.Json; using Tiger.IBusiness; using Microsoft.AspNetCore.Http; using Tiger.Model.Entitys.WMS.DTOS; using static Tiger.Model.BIZ_MES_WO; using Tiger.Model.Entitys.MES.Position; namespace Tiger.Business { public partial class Biz { /// /// 工艺路线 /// public partial class Route : IRoute { /// /// 获取工艺路线 /// /// /// public async Task>> GetRoute(string routerCode) { var res = new ApiAction>(); List list = new List(); try { list = await Db.Queryable() .WhereIF(!string.IsNullOrEmpty(routerCode), x => x.ROT_CODE.Equals(routerCode)) .OrderBy(x => x.CREATE_TIME).ToListAsync(); } catch (Exception ex) { res.CatchExceptionWithLog(ex, "查询异常"); } res.Data = list; return res; } /// /// 获取工艺路线图形数据 /// /// /// public async Task> GetRouteData(string routerId) { var res = new ApiAction(); RouteData rotData = new RouteData(); try { if (string.IsNullOrEmpty(routerId)) { res.IsSuccessed = false; res.LocaleMsg = new($"传入的工艺路线ID为空,不能查找工艺路线!"); return res; } //工艺路线 var route = await Db.Queryable() .WhereIF(!string.IsNullOrEmpty(routerId), x => x.ID.Equals(routerId) || x.ROT_CODE == routerId) .OrderBy(x => x.CREATE_TIME).FirstAsync(); rotData.route = route; if (route == null) { res.IsSuccessed = false; res.LocaleMsg = new($"不能查找到工艺路线!"); return res; } //节点 var nodes = await Db.Queryable() .WhereIF(!string.IsNullOrEmpty(route.ID), x => x.ROT_ID.Equals(route.ID)) .ToListAsync(); foreach (var node in nodes) { node.node = new() { id = node.ID, type = node.GPH_TYPE, x = node.GPH_X, y = node.GPH_Y, properties = node.GPH_PROP, text = new() { x = node.GPH_X, y = node.GPH_Y, value = node.GPH_TEXT } }; } rotData.nodes = nodes; var acts = await Db.Queryable() .WhereIF(!string.IsNullOrEmpty(route.ID), x => x.ROT_ID.Equals(route.ID)) .ToListAsync(); foreach (var act in acts) { act.node = new() { id = act.ID, type = act.GPH_TYPE, x = act.GPH_X, y = act.GPH_Y, properties = act.GPH_PROP, text = new() { x = act.GPH_X, y = act.GPH_Y, value = act.GPH_TEXT } }; } rotData.acts = acts; //边 var edges = await Db.Queryable() .WhereIF(!string.IsNullOrEmpty(route.ID), x => x.ROT_ID.Equals(route.ID)) .ToListAsync(); foreach (var edge in edges) { edge.edge = new() { id = edge.ID, type = edge.GPH_TYPE, sourceNodeId = edge.SRC_NODE, targetNodeId = edge.TGT_NODE, properties = edge.GPH_PROP, startPoint = new() { x = edge.GPH_SRC_X, y = edge.GPH_SRC_Y, }, endPoint = new() { x = edge.GPH_TGT_X, y = edge.GPH_TGT_Y, }, pointsList = new() { new(){ x = edge.GPH_SRC_X, y = edge.GPH_SRC_Y, }, new(){ x = edge.GPH_TGT_X, y = edge.GPH_TGT_Y, } } }; } rotData.edges = edges; } catch (Exception ex) { res.CatchExceptionWithLog(ex, "查询异常"); } res.Data = rotData; return res; } /// /// 保存边数据 /// /// /// public async Task SaveEdges(List routeEdge) { var result = new ApiAction(); try { var db = Db; var dbTran = db.UseTran(() => { var y = db.Storageable(routeEdge) .WhereColumns(t => new { t.ID, t.GHOST_ROW }) .ToStorage(); y.AsInsertable.ExecuteCommand(); y.AsUpdateable.ExecuteCommand(); }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"保存边数据异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "保存边数据异常"); } return await Task.FromResult(result); } /// /// 保存节点数据 /// /// /// public async Task SaveNodes(List routeNode) { var result = new ApiAction(); try { var db = Db; var dbTran = db.UseTran(() => { var y = db.Storageable(routeNode) .WhereColumns(t => new { t.ID, t.GHOST_ROW }) .ToStorage(); y.AsInsertable.ExecuteCommand(); y.AsUpdateable.ExecuteCommand(); }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"保存节点数据异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "保存节点数据异常"); } return await Task.FromResult(result); } /// /// 保存行为 /// /// /// public async Task SaveActs(List routeAct) { var result = new ApiAction(); try { var db = Db; var dbTran = db.UseTran(() => { var y = db.Storageable(routeAct) .WhereColumns(t => new { t.ID, t.GHOST_ROW }) .ToStorage(); y.AsInsertable.ExecuteCommand(); y.AsUpdateable.ExecuteCommand(); }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"保存行为数据异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "保存行为数据异常"); } return await Task.FromResult(result); } /// /// 保存 /// /// /// public async Task SaveRoute(MES_ROUTE route) { var result = new ApiAction(); try { var _route = Db.Queryable().Where(x => x.ROT_CODE == route.ROT_CODE).First(); if (_route != null && Db.Queryable().Where(x => x.ROT_ID == _route.ID).Any()) { result.IsSuccessed = false; result.LocaleMsg = new($"工艺路线已经有设计记录,不能保存!"); return result; } var db = Db; var dbTran = db.UseTran(() => { var y = db.Storageable(route) .WhereColumns(t => new { t.ROT_CODE, t.GHOST_ROW }) .ToStorage(); y.AsInsertable.ExecuteCommand(); y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand(); }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.Message = $"保存工艺路线异常"; } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "保存工艺路线异常"); } return await Task.FromResult(result); } /// /// 保存工艺路线图形数据 /// /// /// public async Task SaveRouteData(RouteData routeData) { var result = new ApiAction(); try { var db = Db; var dbTran = db.UseTran(() => { if (routeData.route != null) { var y = db.Storageable(routeData.route) .WhereColumns(t => new { t.ROT_CODE, t.GHOST_ROW }) .ToStorage(); y.AsInsertable.ExecuteCommand(); y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand(); } db.Deleteable(false).Where(x => x.ROT_ID == routeData.route.ID).ExecuteCommand(); db.Deleteable(false).Where(x => x.ROT_ID == routeData.route.ID).ExecuteCommand(); db.Deleteable(false).Where(x => x.ROT_ID == routeData.route.ID).ExecuteCommand(); if (routeData.nodes != null) { db.Insertable(routeData.nodes).ExecuteCommand(); } if (routeData.edges != null) { db.Insertable(routeData.edges).ExecuteCommand(); } if (routeData.acts != null) { db.Insertable(routeData.acts).ExecuteCommand(); } }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"保存工艺路线图形数据异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "保存工艺路线图形数据异常"); } return await Task.FromResult(result); } /// /// 删除工艺路线 /// /// /// public async Task DeleteRoute(string routeId) { var result = new ApiAction(); try { //查询是否已经有工单在用或者有绑定产品 var db = Db; var dbTran = db.UseTran(() => { db.Deleteable(false).Where(x => x.ROT_ID == routeId).ExecuteCommand(); db.Deleteable(false).Where(x => x.ROT_ID == routeId).ExecuteCommand(); db.Deleteable(false).Where(x => x.ROT_ID == routeId).ExecuteCommand(); db.Deleteable().Where(x => x.ID == routeId).ExecuteCommand(); }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"删除工艺路线异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "删除工艺路线异常"); } return await Task.FromResult(result); } /// /// 保存节点岗位资源 /// /// /// public async Task SaveRouteNodePost(List nodePost) { var result = new ApiAction(); try { var db = Db; var dbTran = db.UseTran(() => { var y = db.Storageable(nodePost) .ToStorage(); y.AsInsertable.ExecuteCommand(); y.AsUpdateable.ExecuteCommand(); }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"保存节点岗位资源数据异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "保存节点岗位资源数据异常"); } return await Task.FromResult(result); } /// /// 删除产品绑定的工艺路线 /// /// /// /// public async Task DeleteProdRoute(DelProdRotInput input) { var result = new ApiAction(); try { var expable = Expressionable.Create(); var expableOper = Expressionable.Create(); if (!string.IsNullOrEmpty(input.rotId)) { expable.And(x => x.ROT_ID == input.rotId); expableOper.And(x => x.ROT_ID == input.rotId); } else if (!string.IsNullOrEmpty(input.rotCode)) { var item = await Db.Queryable().Where(x => x.ROT_CODE == input.rotCode).FirstAsync(); if (item != null) { expable.And(x => x.ROT_ID == item.ID); expableOper.And(x => x.ROT_ID == item.ID); } } if (!string.IsNullOrEmpty(input.prodCode)) { expable.And(x => x.PROD_CODE == input.prodCode); expableOper.And(x => x.PROD_CODE == input.prodCode); } var exp = expable.ToExpression(); var expOper = expableOper.ToExpression(); var db = Db; var dbTran = db.UseTran(() => { db.Deleteable().Where(exp).ExecuteCommand(); db.Deleteable().Where(expOper).ExecuteCommand(); }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"删除工艺路线异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "删除工艺路线异常"); } return result; } /// /// 删除工单绑定的工艺路线 /// /// /// /// public async Task DeleteWoRoute(DelWoRotInput input) { var result = new ApiAction(); try { var bizMesWo = await Db.Queryable().Where(x => x.ORDER_NO == input.wo).FirstAsync(); if (bizMesWo != null && bizMesWo.STATUS > (int)STATUSs.Imported && bizMesWo.STATUS < (int)STATUSs.Closed) { result.IsSuccessed = false; result.LocaleMsg = new($"工单状态为[{EnumHelper.GetDesc(EnumHelper.GetEnum(bizMesWo.STATUS))}],不能删除工艺路线"); return result; } var expableNode = Expressionable.Create(); var expableEdge = Expressionable.Create(); var expableAct = Expressionable.Create(); var expable = Expressionable.Create(); var expableOper = Expressionable.Create(); var _rotId = input.rotId; if (!string.IsNullOrEmpty(input.rotId)) { expable.And(x => x.ROT_ID == input.rotId); expableOper.And(x => x.ROT_ID == input.rotId); expableNode.And(x => x.ROT_ID == input.rotId); expableEdge.And(x => x.ROT_ID == input.rotId); expableAct.And(x => x.ROT_ID == input.rotId); } else if (!string.IsNullOrEmpty(input.rotCode)) { var item = await Db.Queryable().Where(x => x.ROT_CODE == input.rotCode).FirstAsync(); if (item != null) { _rotId = item.ID; expable.And(x => x.ROT_ID == item.ID); expableOper.And(x => x.ROT_ID == item.ID); expableNode.And(x => x.ROT_ID == item.ID); expableEdge.And(x => x.ROT_ID == item.ID); expableAct.And(x => x.ROT_ID == item.ID); } } //如果除了删除的当前工艺路线不存在其他绑定的工艺路线,则更新状态 if (bizMesWo != null && !Db.Queryable().Where(x => x.WORK_ORDER == input.wo && x.ROT_ID != _rotId).Any()) { bizMesWo.ROUTE_STATUS = (int)ROUTE_STATUSs.WaitSet; bizMesWo.STATUS = (int)STATUSs.Init; bizMesWo.ROUTE_CODE = ""; } if (!string.IsNullOrEmpty(input.wo)) { expable.And(x => x.WORK_ORDER == input.wo); expableOper.And(x => x.WORK_ORDER == input.wo); expableNode.And(x => x.WORK_ORDER == input.wo); expableEdge.And(x => x.WORK_ORDER == input.wo); expableAct.And(x => x.WORK_ORDER == input.wo); } var exp = expable.ToExpression(); var expOper = expableOper.ToExpression(); var expNode = expableNode.ToExpression(); var expEdge = expableEdge.ToExpression(); var expAct = expableAct.ToExpression(); var db = Db; var dbTran = db.UseTran(() => { db.Deleteable().Where(exp).ExecuteCommand(); db.Deleteable().Where(expOper).ExecuteCommand(); db.Deleteable().Where(expNode).ExecuteCommand(); db.Deleteable().Where(expEdge).ExecuteCommand(); db.Deleteable().Where(expAct).ExecuteCommand(); db.Updateable(bizMesWo).ExecuteCommand(); }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"删除工艺路线异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "删除工艺路线异常"); } return result; } #region 工单工艺路线相关 /// /// 获取工单工艺路线图形数据 /// /// /// public async Task> GetWoRouteData(string workorder) { var res = new ApiAction(); WoRouteData rotData = new WoRouteData(); try { if (string.IsNullOrEmpty(workorder)) { res.IsSuccessed = false; res.LocaleMsg = new($"传入的工艺路线ID为空,不能查找工艺路线!"); return res; } //工艺路线 var route = await Db.Queryable((q, s) => new JoinQueryInfos(JoinType.Inner, q.ROUTE_CODE == s.ROT_CODE)) .Where((q, s) => q.ID.Equals(workorder) || q.ORDER_NO == workorder).Select((q, s) => new MES_ROUTE { ID = s.ID, ROT_CODE = s.ROT_CODE, ROT_NAME = s.ROT_NAME, ROT_TYPE = s.ROT_TYPE, ROT_VER = s.ROT_VER, IS_ACTIVE = s.IS_ACTIVE, REMARK = s.REMARK, WORK_ORDER = q.ORDER_NO, PROD_CODE = q.ITEM_CODE, CUST_CODE = q.CUST_CODE, }).FirstAsync(); rotData.route = route; if (route == null) { res.IsSuccessed = false; res.LocaleMsg = new($"不能查找到工单工艺路线!"); return res; } //节点 var nodes = await Db.Queryable() .WhereIF(!string.IsNullOrEmpty(route.ID), x => x.WORK_ORDER.Equals(route.WORK_ORDER)) .ToListAsync(); foreach (var node in nodes) { node.node = new() { id = node.ID, type = node.GPH_TYPE, x = node.GPH_X, y = node.GPH_Y, properties = node.GPH_PROP, text = new() { x = node.GPH_X, y = node.GPH_Y, value = node.GPH_TEXT } }; } rotData.nodes = nodes; var acts = await Db.Queryable() .WhereIF(!string.IsNullOrEmpty(route.ID), x => x.WORK_ORDER.Equals(route.WORK_ORDER)) .ToListAsync(); foreach (var act in acts) { act.node = new() { id = act.ID, type = act.GPH_TYPE, x = act.GPH_X, y = act.GPH_Y, properties = act.GPH_PROP, text = new() { x = act.GPH_X, y = act.GPH_Y, value = act.GPH_TEXT } }; } rotData.acts = acts; //边 var edges = await Db.Queryable() .WhereIF(!string.IsNullOrEmpty(route.ID), x => x.WORK_ORDER.Equals(route.WORK_ORDER)) .ToListAsync(); foreach (var edge in edges) { edge.edge = new() { id = edge.ID, type = edge.GPH_TYPE, sourceNodeId = edge.SRC_NODE, targetNodeId = edge.TGT_NODE, properties = edge.GPH_PROP, startPoint = new() { x = edge.GPH_SRC_X, y = edge.GPH_SRC_Y, }, endPoint = new() { x = edge.GPH_TGT_X, y = edge.GPH_TGT_Y, }, pointsList = new() { new(){ x = edge.GPH_SRC_X, y = edge.GPH_SRC_Y, }, new(){ x = edge.GPH_TGT_X, y = edge.GPH_TGT_Y, } } }; } rotData.edges = edges; } catch (Exception ex) { res.CatchExceptionWithLog(ex, "查询异常"); } res.Data = rotData; return res; } /// /// 保存工单工艺路线图形数据 /// /// /// public async Task SaveWoRouteData(WoRouteData routeData) { var result = new ApiAction(); try { var db = Db; var dbTran = db.UseTran(() => { //if (routeData.route != null) //{ // var y = db.Storageable(routeData.route) // .WhereColumns(t => new { t.ROT_CODE, t.GHOST_ROW }) // .ToStorage(); // y.AsInsertable.ExecuteCommand(); // y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand(); //} db.Deleteable(false).Where(x => x.WORK_ORDER == routeData.route.WORK_ORDER).ExecuteCommand(); db.Deleteable(false).Where(x => x.WORK_ORDER == routeData.route.WORK_ORDER).ExecuteCommand(); db.Deleteable(false).Where(x => x.WORK_ORDER == routeData.route.WORK_ORDER).ExecuteCommand(); if (routeData.nodes != null) { db.Insertable(routeData.nodes).ExecuteCommand(); } if (routeData.edges != null) { db.Insertable(routeData.edges).ExecuteCommand(); } if (routeData.acts != null) { db.Insertable(routeData.acts).ExecuteCommand(); } }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"保存工单工艺路线图形数据异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "保存工单工艺路线图形数据异常"); } return await Task.FromResult(result); } /// /// 保存工单工艺节点岗位资源 /// /// /// public async Task SaveWoRouteNodePost(List nodePost) { var result = new ApiAction(); try { var db = Db; var dbTran = db.UseTran(() => { var y = db.Storageable(nodePost) .ToStorage(); y.AsInsertable.ExecuteCommand(); y.AsUpdateable.ExecuteCommand(); }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"保存工单工艺节点岗位资源数据异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "保存工单工艺节点岗位资源数据异常"); } return await Task.FromResult(result); } #endregion } //endClass Route } //endClass Biz }