You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OMS.NET/Common/LayerData.cs

145 lines
4.7 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using OMS.NET.DbClass;
namespace OMS.NET.Common
{
public class LayerData
{
public string? TemplateId { get; set; }
public long LayerId { get; set; }
public bool HasChange { get; set; } = false;
public string Type { get; set; }
public JsonNode? Members { get; set; }
public JsonNode? Structure { get; set; }
public int Phase { get; set; } = 1;
public LayerData(long layerId, string type)
{
LayerId = layerId;
Type = type;
}
public LayerData(string? templateId, MapLayer layer)
{
TemplateId = templateId;
LayerId = layer.Id;
Type = layer.Type;
Members = (layer.Type == "order") ? JsonNode.Parse(layer.Members) : JsonNode.Parse(Util.Base64ToJson(layer.Members));
Structure = string.IsNullOrEmpty(layer.Structure) ? null : JsonNode.Parse(Util.Base64ToJson(layer.Structure));
Phase = layer.Phase;
}
public void AppendElement(long elementId, string elementType)
{
if (Type == "order")
{
GlobalArea.Log.Error($"order图层不能添加{elementType}元素");
return;
}
if (Members![elementId.ToString()] == null)
{
Members![elementId.ToString()] = elementType;
Structure!.AsArray().Add(elementId);
HasChange = true;
UpdateToDb();
}
}
public void DeleteElement(long elementId)
{
if (Type == "order")
{
GlobalArea.Log.Error($"order图层不能移除Element元素");
return;
}
Members!.AsObject().Remove(elementId.ToString());
//移除structure中的值
int index = Structure!.AsArray().FindElementIndex(elementId);
if (index != -1)
{
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 void AdjustOrderLayer(long layerAId, long layerBId, string type)
{
if (Type == "order")
{
JsonArray array = Members!.AsArray();
int indexA = array.FindElementIndex(layerAId);
int indexB = array.FindElementIndex(layerBId);
if (type == "up")
{
array.RemoveAt(indexA);
array.Insert(indexB, layerAId);
}
if (type == "down")
{
array.RemoveAt(indexA);
indexB = array.FindElementIndex(layerBId);
array.Insert(indexB + 1, layerAId);
}
UpdateToDb();
}
}
public MapLayer ConvertToMapLayer()
{
return new MapLayer()
{
Id = LayerId,
Type = Type,
Members = (Type == "order") ? Members!.ToString() : Util.JsonToBase64(Members!.ToString()),
Structure = (Structure == null) ? "" : Util.JsonToBase64(Structure!.ToString()),
Phase = Phase
};
}
}
}