using System.Text; using System.Text.Json; using OMS.NET.Common; using OMS.NET.DbClass; namespace OMS.NET.Instructs { public class AddElementBroadcastInstruct : BroadcastInstuct { public AddElementBroadcastInstruct() { Type = "broadcast"; } public override Task Handler(string wsid) { return Task.Run(() => { if (GlobalArea.LoginCheckByID(wsid)) { //从Data中解析数据,收到的是json文本类型 if (Data?.GetType() == typeof(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 BroadcastInstuct() { IsBroadcast = true, Class = this.Class, Conveyor = conveyor, Time = time, Data = mapData, }); ResponseOrBroadcastInstructs.Add(new BroadcastInstuct() { IsBroadcast = true, Class = "updateLayerData", Conveyor = conveyor, Time = time, Data = new { id = layer.LayerId, members = layer.Members!.ToString(), structure = layer.Structure!.ToString(), } }); ResponseOrBroadcastInstructs.Add(new SendErrorInstuct() { IsResponse = true, Class = "upload", Conveyor = conveyor, Time = time, Data = new Dictionary() { {"vid",id}, {"rid",mapData.Id} }, }); } } else { throw new Exception("未找到 tmpId"); } } catch (Exception ex) { Console.WriteLine(ex.Message); ResponseOrBroadcastInstructs.Add(new SendErrorInstuct() { IsResponse = true, Class = "upload", Conveyor = GlobalArea.GetLoginEmailByID(wsid), Time = GlobalArea.GetCurrentTime(), Data = new { source = Data, message = ex.Message, }, }); } } } }); } } }