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/Instructs/AddElementInstruct.cs

130 lines
5.7 KiB
C#

using System.Text;
using System.Text.Json;
using OMS.NET.Common;
using OMS.NET.DbClass;
namespace OMS.NET.Instructs
{
public class AddElementInstruct : Instruct
{
public AddElementInstruct()
{
Type = "broadcast";
}
public override Task Handler(string wsid)
{
return Task.Run(() =>
{
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出
if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查
try
{
long id = Data.GetProperty("id").GetInt64();
string type = Data.GetProperty("type").GetString();
string points = Data.GetProperty("points").GetString();
string point = Data.GetProperty("point").GetString();
string? color = Data.GetProperty("color").GetString();
int? width = Data.GetProperty("width").GetInt32();
string? childRelations = Data.GetProperty("childRelations").GetString();
string? fatherRelations = Data.GetProperty("fatherRelations").GetString();
string? childNodes = Data.GetProperty("childNodes").GetString();
string? fatherNode = Data.GetProperty("fatherNode").GetString();
string? details = Data.GetProperty("details").GetString();
string? custom = Data.GetProperty("custom").GetString();
//validations todo
//假设所有数据均合法
MapData mapData = new()
{
Type = type,
Points = Util.JsonToBase64(points),
Point = Util.JsonToBase64(point),
Color = color,
Width = width,
ChildRelations = childRelations,
FatherRelations = fatherRelations,
ChildNodes = childNodes,
FatherNode = fatherNode,
Details = Util.JsonToBase64(details),
Custom = Util.JsonToBase64(custom)
};
//保存点到数据库
MapData.Add(mapData);
//获取图层id
if (Data.GetProperty("custom").TryGetProperty("tmpId", out JsonElement tmpIdNode))
{
string tmpId = tmpIdNode.GetString() ?? throw new Exception("tmpId不能为空");
LayerData? layer = GlobalArea.GetLayerDataByTemplateId(tmpId);
if (layer != null && mapData.Id != -1)
{
layer.AppendElement(mapData.Id, mapData.Type);//包括数据库操作
//准备回复广播
string conveyor = GlobalArea.GetLoginEmailByID(wsid);
string time = GlobalArea.GetCurrentTime();
ResponseOrBroadcastInstructs.Add(new Instruct()
{
IsBroadcast = true,
Type = "broadcast",
Class = Class,
Conveyor = conveyor,
Time = time,
Data = mapData,
});
ResponseOrBroadcastInstructs.Add(new Instruct()
{
IsBroadcast = true,
Type = "broadcast",
Class = "updateLayerData",
Conveyor = conveyor,
Time = time,
Data = new
{
id = layer.LayerId,
members = layer.Members!.ToString(),
structure = layer.Structure!.ToString(),
}
});
ResponseOrBroadcastInstructs.Add(new Instruct()
{
IsResponse = true,
Type = "send_correct",
Class = "upload",
Conveyor = conveyor,
Time = time,
Data = new
{
vid = id,
rid = mapData.Id
}
});
}
}
else
{
throw new Exception("未找到 tmpId");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ResponseOrBroadcastInstructs.Add(new Instruct()
{
IsResponse = true,
Type = "send_error",
Class = "upload",
Conveyor = GlobalArea.GetLoginEmailByID(wsid),
Time = GlobalArea.GetCurrentTime(),
Data = new
{
source = Data,
message = ex.Message
}
});
}
});
}
}
}