实现麻烦的调整元素顺序指令

dev
nxiaoxiao 1 year ago
parent 13b64ed549
commit 2666922c4e

@ -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<long>() == elementToFind)
{
return i;
}
}
return -1; // 未找到目标元素,返回 -1
}
public static void RemoveAll(this JsonArray jsonArray, Func<JsonNode?, bool> predicate)
{
var itemsToRemove = jsonArray.Where(predicate).ToList();
foreach (var item in itemsToRemove)
{
jsonArray.Remove(item);
}
}
}
}

@ -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<long>() == 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()

@ -67,5 +67,42 @@ namespace OMS.NET.Common
{
return JsonSerializer.Deserialize<Point>(json, options) ?? throw new Exception("转化point类型失败:" + json);
}
//==================================================================================
/// <summary>
/// 麻烦的键值操作
/// </summary>
public static void DetailsTransform(JsonArray details, JsonArray detailsRule)
{
var originalKeys = details.Select(d => d!["key"]?.GetValue<string>())
.Where(k => k != null).ToList();
//好像可以直接拷贝一个新的detail模板然后继承原有属性即可顺序可直接继承模板不用再调整
JsonArray newDetails = new();
foreach (var rule in detailsRule)
{
var key = rule!["name"]?.GetValue<string>()!;
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;
}
}
}

@ -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<LayerData> 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<bool>() == 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
/// <summary>

@ -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<dynamic> 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);
}
});
}
}
}
Loading…
Cancel
Save