From 9e085dbbdca971232bd61d32833fb01027a9b154 Mon Sep 17 00:00:00 2001 From: nxiaoxiao <3247747442@qq.com> Date: Sun, 18 Aug 2024 12:11:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=88=A0=E9=99=A4=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/LayerData.cs | 34 +++++++++++-- GlobalArea.cs | 11 ++++ Instructs/AddElementInstruct.cs | 12 ++--- Instructs/DeleteElementInstuct.cs | 83 +++++++++++++++++++++++++++++-- 4 files changed, 125 insertions(+), 15 deletions(-) diff --git a/Common/LayerData.cs b/Common/LayerData.cs index 5d6e93b..801c5f2 100644 --- a/Common/LayerData.cs +++ b/Common/LayerData.cs @@ -30,17 +30,17 @@ namespace OMS.NET.Common Phase = layer.Phase; } - public void AppendElement(long itemId, string ItemType) + public void AppendElement(long elementId, string elementType) { if (Type == "order") { - GlobalArea.Log.Error($"order图层不能添加{ItemType}元素"); + GlobalArea.Log.Error($"order图层不能添加{elementType}元素"); return; } - if (Members![itemId.ToString()] == null) + if (Members![elementId.ToString()] == null) { - Members![itemId.ToString()] = ItemType; - Structure!.AsArray().Add(itemId); + Members![elementId.ToString()] = elementType; + Structure!.AsArray().Add(elementId); HasChange = true; //上传图层到数据库 MapLayer mapLayer = ConvertToMapLayer(); @@ -48,6 +48,30 @@ namespace OMS.NET.Common } } + public void DeleteElement(long elementId) + { + if (Type == "order") + { + GlobalArea.Log.Error($"order图层不能移除Element元素"); + return; + } + Members!.AsObject().Remove(elementId.ToString()); + //移除structure中的值 + for (int i = 0; i < Structure!.AsArray().Count; i++) + { + JsonNode node = Structure!.AsArray().ElementAt(i)!; + if (node.GetValueKind() == JsonValueKind.Number && + node.GetValue() == elementId) + { + Structure!.AsArray().RemoveAt(i); + break; + } + } + //上传图层到数据库 + MapLayer mapLayer = ConvertToMapLayer(); + MapLayer.Update(mapLayer); + } + public MapLayer ConvertToMapLayer() { return new MapLayer() diff --git a/GlobalArea.cs b/GlobalArea.cs index 0e82699..bb34c2a 100644 --- a/GlobalArea.cs +++ b/GlobalArea.cs @@ -329,6 +329,17 @@ namespace OMS.NET } } + /// + /// 移除活动数据,调用前必需确保对象已存在 + /// + public static void RemoveActiveDataElement(long elementId) + { + lock (_ActiveDataListLock) + { + _ActiveDataList.Remove(GetActiveDataElement(elementId)!); + } + } + /// /// 从数据库中构建layerid 和 templateid的对应关系 /// diff --git a/Instructs/AddElementInstruct.cs b/Instructs/AddElementInstruct.cs index 0daaae0..6ecf988 100644 --- a/Instructs/AddElementInstruct.cs +++ b/Instructs/AddElementInstruct.cs @@ -92,16 +92,14 @@ namespace OMS.NET.Instructs Class = "upload", Conveyor = conveyor, Time = time, - Data = new Dictionary() - { - {"vid",id}, - {"rid",mapData.Id} - }, - + Data = new + { + vid = id, + rid = mapData.Id + } }); } - } else { diff --git a/Instructs/DeleteElementInstuct.cs b/Instructs/DeleteElementInstuct.cs index 6ad2cf6..9042573 100644 --- a/Instructs/DeleteElementInstuct.cs +++ b/Instructs/DeleteElementInstuct.cs @@ -1,4 +1,6 @@ using System.Text.Json; +using OMS.NET.Common; +using OMS.NET.DbClass; namespace OMS.NET.Instructs { @@ -6,6 +8,7 @@ namespace OMS.NET.Instructs { public DeleteElementInstruct() { + Type = "broadcast"; Class = "deleteElement"; } @@ -15,17 +18,91 @@ namespace OMS.NET.Instructs { if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出 if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 + string conveyor = GlobalArea.GetLoginEmailByID(wsid); try { - string conveyor = GlobalArea.GetLoginEmailByID(wsid); //解析id和tmpId long id = Data.GetProperty("id").GetInt64(); string tmpId = Data.GetProperty("tmpId").GetString(); //需要先实现activeData相关 + MapData mapData = MapData.Get(id) ?? throw new Exception($"Element{id}不存在"); + mapData.Phase = 2; + int row = MapData.Update(mapData); + if (row == -1) throw new Exception("数据库修改失败"); + if (GlobalArea.IsElementExit(id)) + { + GlobalArea.RemoveActiveDataElement(id); + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsBroadcast = true, + Type = "broadcast", + Class = "pickSelectEndElements", + Conveyor = conveyor, + Time = GlobalArea.GetCurrentTime(), + Data = new + { + id = id + } + }); + } + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsResponse = true, + Type = "send_correct", + Class = "delete", + Conveyor = conveyor, + Time = GlobalArea.GetCurrentTime(), + Data = new + { + rid = id + } + }); + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsBroadcast = true, + Type = "broadcast", + Class = "deleteElement", + Conveyor = conveyor, + Time = GlobalArea.GetCurrentTime(), + Data = new + { + id = id + } + }); + LayerData layer = GlobalArea.GetLayerDataByTemplateId(tmpId)!; + layer.DeleteElement(id); + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsBroadcast = true, + Type = "broadcast", + Class = "updateLayerData", + Conveyor = conveyor, + Time = GlobalArea.GetCurrentTime(), + Data = new + { + id = layer.LayerId, + members = layer.Members!.ToString(), + structure = layer.Structure!.ToString(), + } + }); } - catch + catch (Exception ex) { - + GlobalArea.Log.Warn($"处理{Class}广播指令出错:" + ex.Message); + //ResponseOrBroadcastInstructs.Clear(); + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsResponse = true, + Type = "send_error", + Class = "delete", + Conveyor = conveyor, + Time = GlobalArea.GetCurrentTime(), + Data = new + { + source = Data, + message = ex.Message + } + }); } }); }