From 2666922c4e2790b31408126c171ae5ca663cd178 Mon Sep 17 00:00:00 2001 From: nxiaoxiao <3247747442@qq.com> Date: Sun, 18 Aug 2024 19:17:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=BA=BB=E7=83=A6=E7=9A=84?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=85=83=E7=B4=A0=E9=A1=BA=E5=BA=8F=E6=8C=87?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/JsonArrayExtensions.cs | 31 +++++++++++ Common/LayerData.cs | 57 ++++++++++++++++---- Common/Utils.cs | 37 +++++++++++++ GlobalArea.cs | 60 +++++++++++++++++++++ Instructs/AdjustElementOrderInstuct.cs | 75 ++++++++++++++++++++++++++ 5 files changed, 249 insertions(+), 11 deletions(-) create mode 100644 Common/JsonArrayExtensions.cs create mode 100644 Instructs/AdjustElementOrderInstuct.cs diff --git a/Common/JsonArrayExtensions.cs b/Common/JsonArrayExtensions.cs new file mode 100644 index 0000000..a2c47b9 --- /dev/null +++ b/Common/JsonArrayExtensions.cs @@ -0,0 +1,31 @@ +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace OMS.NET.Common +{ + public static class Extensions + { + public static int FindElementIndex(this JsonArray jsonArray, long elementToFind) + { + for (int i = 0; i < jsonArray.Count; i++) + { + JsonNode node = jsonArray.ElementAt(i)!; + if (node.GetValueKind() == JsonValueKind.Number && + node.GetValue() == elementToFind) + { + return i; + } + } + return -1; // 未找到目标元素,返回 -1 + } + + public static void RemoveAll(this JsonArray jsonArray, Func predicate) + { + var itemsToRemove = jsonArray.Where(predicate).ToList(); + foreach (var item in itemsToRemove) + { + jsonArray.Remove(item); + } + } + } +} \ No newline at end of file diff --git a/Common/LayerData.cs b/Common/LayerData.cs index 801c5f2..264328d 100644 --- a/Common/LayerData.cs +++ b/Common/LayerData.cs @@ -42,9 +42,7 @@ namespace OMS.NET.Common Members![elementId.ToString()] = elementType; Structure!.AsArray().Add(elementId); HasChange = true; - //上传图层到数据库 - MapLayer mapLayer = ConvertToMapLayer(); - MapLayer.Update(mapLayer); + UpdateToDb(); } } @@ -57,21 +55,58 @@ namespace OMS.NET.Common } Members!.AsObject().Remove(elementId.ToString()); //移除structure中的值 - for (int i = 0; i < Structure!.AsArray().Count; i++) + int index = Structure!.AsArray().FindElementIndex(elementId); + if (index != -1) { - JsonNode node = Structure!.AsArray().ElementAt(i)!; - if (node.GetValueKind() == JsonValueKind.Number && - node.GetValue() == elementId) - { - Structure!.AsArray().RemoveAt(i); - break; - } + Structure!.AsArray().RemoveAt(index); } + UpdateToDb(); + } + + private void UpdateToDb() + { //上传图层到数据库 MapLayer mapLayer = ConvertToMapLayer(); MapLayer.Update(mapLayer); } + public void AdjustABOrder(long a, long b, string method) + { + JsonArray arr = Structure!.AsArray(); + int indexA = arr.FindElementIndex(a); + int indexB = arr.FindElementIndex(b); + switch (method) + { + case "up": + { + if (indexA > indexB) + { + arr.RemoveAt(indexA); + arr.Insert(indexB, a); + HasChange = true; + UpdateToDb(); + } + break; + } + case "down": + { + if (indexA < indexB) + { + arr.RemoveAt(indexA); + indexB = arr.FindElementIndex(b); + arr.Insert(indexB + 1, a); + HasChange = true; + UpdateToDb(); + } + break; + } + case "join"://donothing + break; + default: + throw new Exception($"LayerData.AdjustABOrder不支持的method类型:{method}"); + } + } + public MapLayer ConvertToMapLayer() { return new MapLayer() diff --git a/Common/Utils.cs b/Common/Utils.cs index 3d114c9..4614e28 100644 --- a/Common/Utils.cs +++ b/Common/Utils.cs @@ -67,5 +67,42 @@ namespace OMS.NET.Common { return JsonSerializer.Deserialize(json, options) ?? throw new Exception("转化point类型失败:" + json); } + + //================================================================================== + /// + /// 麻烦的键值操作 + /// + public static void DetailsTransform(JsonArray details, JsonArray detailsRule) + { + var originalKeys = details.Select(d => d!["key"]?.GetValue()) + .Where(k => k != null).ToList(); + //好像可以直接拷贝一个新的detail模板,然后继承原有属性即可,顺序可直接继承模板,不用再调整 + JsonArray newDetails = new(); + foreach (var rule in detailsRule) + { + var key = rule!["name"]?.GetValue()!; + var value = rule["default"]; + JsonValueKind newType = rule["default"]!.GetValueKind(); + if (originalKeys.Contains(key)) + { + int index = originalKeys.IndexOf(key); + JsonValueKind oldType = details.ElementAt(index)!["value"]!.GetValueKind(); + //如果模板类型会有变化,原设计中对应的value需要调整下格式 + //这里直接放弃旧值,转化来转化去,太麻烦且没必要 + if (oldType == newType) + { + value = details.ElementAt(index)!["value"]; + } + + } + var newDetail = new JsonObject + { + ["key"] = key, + ["value"] = value + }; + newDetails.Add(newDetail); + } + details = newDetails; + } } } \ No newline at end of file diff --git a/GlobalArea.cs b/GlobalArea.cs index bb34c2a..d223391 100644 --- a/GlobalArea.cs +++ b/GlobalArea.cs @@ -2,8 +2,10 @@ using System.Net; using System.Security.Cryptography; using System.Text; using System.Text.Json; +using System.Text.Json.Nodes; using OMS.NET.Common; using OMS.NET.DbClass; +using OMS.NET.Instructs; namespace OMS.NET { @@ -377,6 +379,64 @@ namespace OMS.NET } } } + + // public static LayerData? GetLayerDataByLayerId(long layerId) + // { + // lock (_LayerDataListLock) + // { + // try + // { + // return _LayerDataList.Where(x => x.LayerId == layerId).First(); + // } + // catch + // { + // Log.Warn("通过图层id未获取到图层数据"); + // return null; + // } + // } + // } + + public static dynamic AdjustElementOrder(long elementAId, long elementBId, string templateA, string templateB, string method) + { + lock (_LayerDataListLock) + { + dynamic result = new object(); + List layers = new(); + result.layers = layers; + LayerData layerA = GetLayerDataByTemplateId(templateA)!; + LayerData layerB = GetLayerDataByTemplateId(templateB)!; + if (layerA.LayerId == layerB.LayerId) + { + layerA.AdjustABOrder(elementAId, elementBId, method); + layers.Add(layerA); + } + else + { + MapData mapDataA = MapData.Get(elementAId)!; + JsonNode bTemplateData = layerB.Structure!.AsArray().ElementAt(1)!["template"]!; + if (bTemplateData["typeRule"]![mapDataA.Type]!.GetValue() == true) + { + JsonArray aDetail = JsonObject.Parse(Util.Base64ToJson(mapDataA.Details!))!.AsArray(); + JsonNode aCustom = JsonObject.Parse(Util.Base64ToJson(mapDataA.Custom!))!; + Util.DetailsTransform(aDetail, bTemplateData.AsArray()); + aCustom["tmpId"] = templateB; + mapDataA.Details = Util.JsonToBase64(aDetail.ToString()); + mapDataA.Custom = Util.JsonToBase64(aCustom.ToString()); + if (MapData.Update(mapDataA) == -1) throw new Exception("数据库修改失败"); + result.element = mapDataA; + } + layerA.DeleteElement(elementAId); + layerB.AppendElement(elementAId, mapDataA.Type); + layerB.AdjustABOrder(elementAId, elementBId, method);//丢在一起,再调整顺序 + layers.Add(layerA); + layers.Add(layerB); + } + return result; + } + } + + + #endregion /// diff --git a/Instructs/AdjustElementOrderInstuct.cs b/Instructs/AdjustElementOrderInstuct.cs new file mode 100644 index 0000000..11f1016 --- /dev/null +++ b/Instructs/AdjustElementOrderInstuct.cs @@ -0,0 +1,75 @@ +using System.Text.Json; +using OMS.NET.Common; +using OMS.NET.DbClass; + +namespace OMS.NET.Instructs +{ + public class AdjustElementOrderInstuct : Instruct + { + public AdjustElementOrderInstuct() + { + Type = "broadcast"; + Class = "adjustElementOrder"; + } + + public override Task Handler(string wsid) + { + return Task.Run(() => + { + if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出 + if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 + string conveyor = GlobalArea.GetLoginEmailByID(wsid); + try + { + long elementA = Data.GetProperty("elementA").GetInt64(); + long elementB = Data.GetProperty("elementB").GetInt64(); + string templateA = Data.GetProperty("templateA").GetString(); + string templateB = Data.GetProperty("templateB").GetString(); + string method = Data.GetProperty("method").GetString(); + dynamic result = GlobalArea.AdjustElementOrder(elementA, elementB, templateA, templateB, method); + List list = new(); + foreach (LayerData layerData in result.layers) + { + list.Add(new + { + id = layerData.LayerId, + members = layerData.Members, + structure = layerData.Structure, + }); + } + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsBroadcast = true, + Type = "broadcast", + Class = "batchUpdateLayerData", + Conveyor = conveyor, + Time = GlobalArea.GetCurrentTime(), + Data = list + }); + if (result.element != null && result.element.GetType() != typeof(MapData)) + { + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsBroadcast = true, + Type = "broadcast", + Class = "updateElement", + Conveyor = conveyor, + Time = GlobalArea.GetCurrentTime(), + Data = new + { + id = result.element.ID, + type = result.element.Type, + details = result.element.Details, + custom = result.element.Custom + } + }); + } + } + catch (Exception ex) + { + GlobalArea.Log.Warn($"处理{Class}广播指令出错:" + ex.Message); + } + }); + } + } +} \ No newline at end of file