using System.Text.Json; using OMS.NET.Common; using OMS.NET.DbClass; namespace OMS.NET.Instructs { public class UpdateElementInstuct : Instruct { public UpdateElementInstuct() { Type = "broadcast"; Class = "updateElement"; } 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 id = Data.GetProperty("id").GetInt64(); string updateId = Data.GetProperty("updateId").GetString(); if (Data.TryGetProperty("changes", out JsonElement changes)) { MapData mapData = MapData.Get(id) ?? throw new Exception($"数据库中未能找到id为{id}的元素"); string color = changes.GetProperty("color").GetString()!; if (changes.TryGetProperty("width", out JsonElement widthElement)) { int width = widthElement.GetInt32(); mapData.Width = width; } if (changes.TryGetProperty("details", out JsonElement detailsElement)) { string details = detailsElement.GetString()!; mapData.Details = Util.JsonToBase64(details); } if (changes.TryGetProperty("custom", out JsonElement customElement)) { string custom = customElement.GetString()!; mapData.Custom = Util.JsonToBase64(custom); } if (MapData.Update(mapData) == -1) throw new Exception("数据库修改失败"); ResponseOrBroadcastInstructs.Add(new Instruct() { IsResponse = true, Type = "send_correct", Class = "updateElement", Conveyor = conveyor, Time = GlobalArea.GetCurrentTime(), Data = new { vid = updateId, rid = id } }); ResponseOrBroadcastInstructs.Add(new Instruct() { IsBroadcast = true, Type = "broadcast", Class = "updateElement", Conveyor = conveyor, Time = GlobalArea.GetCurrentTime(), Data = mapData }); } } catch (Exception ex) { GlobalArea.Log.Warn($"处理{Class}广播指令出错:" + ex.Message); ResponseOrBroadcastInstructs.Add(new Instruct() { IsResponse = true, Type = "send_error", Class = "updateElement", Conveyor = conveyor, Time = GlobalArea.GetCurrentTime(), Data = new { source = Data, message = ex.Message } }); } }); } } }