diff --git a/DbClass/SqlHelper.cs b/DbClass/SqlHelper.cs index 964d745..69cf645 100644 --- a/DbClass/SqlHelper.cs +++ b/DbClass/SqlHelper.cs @@ -1,7 +1,8 @@ using System.Reflection; using MySql.Data.MySqlClient; using OMS.NET.Common; -namespace OMS.NET.DbClass{ +namespace OMS.NET.DbClass +{ [AttributeUsage(AttributeTargets.Class, Inherited = false)] public class TableAttribute : Attribute { @@ -27,13 +28,20 @@ namespace OMS.NET.DbClass{ } } - public class SqlHelper{ + public class SqlHelper + { public static string GetTableName(Type type) { var attr = type.GetCustomAttribute(); return attr?.TableName ?? throw new Exception($"{type.Name} 没有指定数据表"); } + public static bool IsIdentity(Type type) + { + var primaryKey = GetPrimaryKeyProperties(type); + return primaryKey.GetCustomAttribute()!.IsIdentity; + } + public static IEnumerable GetProperties(Type type) { return type.GetProperties() diff --git a/DbClass/SqlTable.cs b/DbClass/SqlTable.cs index dca910b..81ee941 100644 --- a/DbClass/SqlTable.cs +++ b/DbClass/SqlTable.cs @@ -16,12 +16,15 @@ namespace OMS.NET.DbClass Console.WriteLine("===========this is a test area================="); } + /// + /// 将对象本身插入到数据库中 + /// public virtual void Insert() { var type = GetType(); var insertSql = SqlHelper.GetInsertSql(GetType()); using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName); - connection.Open(); // 确保在执行命令之前打开连接 + connection.Open(); //确保在执行命令之前打开连接 using var command = new MySqlCommand(insertSql, connection); AddParameters(command); Console.WriteLine($"SQL: {command.CommandText}"); @@ -38,7 +41,7 @@ namespace OMS.NET.DbClass var type = GetType(); var updateSql = SqlHelper.GetUpdateSql(GetType()); using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName); - connection.Open(); // 确保在执行命令之前打开连接 + connection.Open(); using var command = new MySqlCommand(updateSql, connection); AddParameters(command); command.ExecuteNonQuery(); @@ -47,25 +50,29 @@ namespace OMS.NET.DbClass public virtual void Delete() { - var type = GetType(); var deleteSql = SqlHelper.GetDeleteSql(GetType()); using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName); - connection.Open(); // 确保在执行命令之前打开连接 + connection.Open(); using var command = new MySqlCommand(deleteSql, connection); AddParameters(command); command.ExecuteNonQuery(); - Log.Info($"{SqlHelper.GetTableName(type)}数据表删除({string.Join(',', ShowDetail())})"); + Log.Info($"{SqlHelper.GetTableName(GetType())}数据表删除({string.Join(',', ShowDetail())})"); } private void AddParameters(MySqlCommand command) { + //bool IsIdentity = SqlHelper.IsIdentity(GetType()); foreach (var property in SqlHelper.GetProperties(GetType())) { + if (property.GetCustomAttribute()!.IsIdentity) + { + continue; + } command.Parameters.AddWithValue("@" + property.Name, property.GetValue(this)); } } - private IEnumerable ShowDetail() + public IEnumerable ShowDetail() { return SqlHelper.GetProperties(GetType()).Select(p => $"{p.Name}={p.GetValue(this)}"); } diff --git a/Instructs/GetMapDataInstruct.cs b/Instructs/GetMapDataInstruct.cs index 9904449..8b73a6d 100644 --- a/Instructs/GetMapDataInstruct.cs +++ b/Instructs/GetMapDataInstruct.cs @@ -6,7 +6,7 @@ namespace OMS.NET.Instructs { public GetMapDataInstruct() { - this.Type = "get_mapData"; + Type = "get_mapData"; } public override Task Handler(string wsid) @@ -25,7 +25,7 @@ namespace OMS.NET.Instructs { public SendMapDataInstruct() { - this.Type = "send_mapData"; + Type = "send_mapData"; } } } \ No newline at end of file diff --git a/Instructs/GetMapLayerInstruct.cs b/Instructs/GetMapLayerInstruct.cs index 1916a7f..164063e 100644 --- a/Instructs/GetMapLayerInstruct.cs +++ b/Instructs/GetMapLayerInstruct.cs @@ -6,7 +6,7 @@ namespace OMS.NET.Instructs { public GetMapLayerInstruct() { - this.Type = "get_mapLayer"; + Type = "get_mapLayer"; } public override Task Handler(string wsid) @@ -25,7 +25,7 @@ namespace OMS.NET.Instructs { public SendMapLayerInstruct() { - this.Type = "send_mapLayer"; + Type = "send_mapLayer"; } } } \ No newline at end of file diff --git a/Instructs/GetPublickeyInstruct.cs b/Instructs/GetPublickeyInstruct.cs index 1d49a79..54b39c6 100644 --- a/Instructs/GetPublickeyInstruct.cs +++ b/Instructs/GetPublickeyInstruct.cs @@ -6,14 +6,14 @@ namespace OMS.NET.Instructs { public GetPublickeyInstruct() { - this.Type = "get_publickey"; + Type = "get_publickey"; } public override Task Handler(string wsid) { return Task.Run(() => { - this.ResponseOrBroadcastInstructs.Add(new PublickeyInstruct() + ResponseOrBroadcastInstructs.Add(new PublickeyInstruct() { Data = GlobalArea.GetRsaPublickKey(), IsResponse = true @@ -27,7 +27,7 @@ namespace OMS.NET.Instructs { public PublickeyInstruct() { - this.Type = "publickey"; + Type = "publickey"; } } diff --git a/Instructs/GetUserDataInstruct.cs b/Instructs/GetUserDataInstruct.cs index 85ae581..b7f2fa7 100644 --- a/Instructs/GetUserDataInstruct.cs +++ b/Instructs/GetUserDataInstruct.cs @@ -6,7 +6,7 @@ namespace OMS.NET.Instructs { public GetUserDataInstruct() { - this.Type = "get_userData"; + Type = "get_userData"; } public override Task Handler(string wsid) @@ -25,7 +25,7 @@ namespace OMS.NET.Instructs { public SendUserDataInstruct() { - this.Type = "send_userData"; + Type = "send_userData"; } } } \ No newline at end of file diff --git a/Instructs/Instruct.cs b/Instructs/Instruct.cs index 7ee3722..bf9b908 100644 --- a/Instructs/Instruct.cs +++ b/Instructs/Instruct.cs @@ -112,7 +112,7 @@ namespace OMS.NET.Instructs public Instruct() { - this.Type = ""; + Type = ""; } /// @@ -130,7 +130,7 @@ namespace OMS.NET.Instructs stopWatch.Start(); await Handler(wsid); stopWatch.Stop(); - Log.Debug($"处理{this.GetType()}耗时:{stopWatch.ElapsedMilliseconds}ms"); + Log.Debug($"处理{GetType()}耗时:{stopWatch.ElapsedMilliseconds}ms"); } public string ToJsonString() diff --git a/Instructs/LoginInstruct.cs b/Instructs/LoginInstruct.cs index 58cf9ae..5b4a991 100644 --- a/Instructs/LoginInstruct.cs +++ b/Instructs/LoginInstruct.cs @@ -11,17 +11,17 @@ namespace OMS.NET.Instructs { public LoginInstruct() { - this.Type = "login"; + Type = "login"; } public override Task Handler(string wsid) { return Task.Run(() => { - if (this.Data?.GetType() == typeof(JsonElement)) + if (Data?.GetType() == typeof(JsonElement)) { - string email = this.Data.GetProperty("email").GetString(); - string password = this.Data.GetProperty("password").GetString(); + string email = Data.GetProperty("email").GetString(); + string password = Data.GetProperty("password").GetString(); if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { Log.Warn("登录信息不能为空!"); @@ -36,7 +36,7 @@ namespace OMS.NET.Instructs { public LoginStatusInstuct() { - this.Type = "loginStatus"; + Type = "loginStatus"; } } } \ No newline at end of file diff --git a/Instructs/TestInstuct.cs b/Instructs/TestInstuct.cs index d1e4c80..8369ebb 100644 --- a/Instructs/TestInstuct.cs +++ b/Instructs/TestInstuct.cs @@ -1,4 +1,7 @@ +using System.Text.Json; using OMS.NET.Common; +using OMS.NET.DbClass; +using ZstdSharp.Unsafe; namespace OMS.NET.Instructs { @@ -9,7 +12,7 @@ namespace OMS.NET.Instructs { public TestInstruct() { - this.Type = "test"; + Type = "test"; } public override Task Handler(string wsid) @@ -17,8 +20,47 @@ namespace OMS.NET.Instructs return Task.Run(() => { //GlobalArea.UserConnects.Where(x => x.ID == wsid).First().UserEmail = "xxx@xx.com";//login - Log.Info("当前客户端连接数:" + GlobalArea.ConnectClientsCount); - Log.Info("当前登录用户数:" + GlobalArea.LoginUserCount); + // Log.Info("当前客户端连接数:" + GlobalArea.ConnectClientsCount); + // Log.Info("当前登录用户数:" + GlobalArea.LoginUserCount); + //测试数据表连接 + if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 + try + { + int op = Data.GetInt32(); + Map map = new() + { + Id = "map1", + Name = "test", + Description = "haksjhdkasd" + }; + switch (op) + { + case 0: + map.Test(); + break; + case 1: + map.Insert(); + Log.Debug($"插入测试成功,新的id为{map.Id}"); + break; + case 2: + Log.Debug(string.Join(',', map.ShowDetail())); + break; + case 3: + map.Description = "随便写一个有意义的字符串"; + map.Update(); + break; + case 4: + map.Delete(); + Log.Debug($"删除测试成功"); + break; + default: + throw new Exception("do nothing"); + } + } + catch (Exception ex) + { + Log.Warn($"处理{Type}指令出错:" + ex.Message); + } }); } } diff --git a/MapServer.cs b/MapServer.cs index 4cb9b47..b26a8e5 100644 --- a/MapServer.cs +++ b/MapServer.cs @@ -11,11 +11,11 @@ namespace OMS.NET private IPEndPoint iPEndPoint = new(IPAddress.Any, 0); protected override async void OnMessage(MessageEventArgs e) { - Log.Debug(this.ID + " " + this.Context.UserEndPoint.ToString() + ":" + e.Data); + Log.Debug(ID + " " + Context.UserEndPoint.ToString() + ":" + e.Data); Instruct? instruct = Instruct.JsonStringParse(e.Data); if (instruct != null) { - await instruct.HandlerAndMeasure(this.ID); + await instruct.HandlerAndMeasure(ID); if (instruct.ResponseOrBroadcastInstructs.Count > 0) { instruct.ResponseOrBroadcastInstructs.ForEach(res => @@ -28,7 +28,7 @@ namespace OMS.NET if (res.IsBroadcast) { string str = res.ToJsonString(); - foreach (IWebSocketSession session in this.Sessions.Sessions) + foreach (IWebSocketSession session in Sessions.Sessions) { //看起来只有登录后的连接才能收到广播,这里添加下过滤 if (GlobalArea.LoginCheckByID(session.ID)) @@ -44,23 +44,23 @@ namespace OMS.NET protected override void OnOpen() { - this.iPEndPoint = this.Context.UserEndPoint; - GlobalArea.AddUserConnect(this.ID, this.iPEndPoint); - // Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Open"); + iPEndPoint = Context.UserEndPoint; + GlobalArea.AddUserConnect(ID, iPEndPoint); + Log.Info(ID + " " + iPEndPoint.ToString() + " Conection Open"); // Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}"); } protected override void OnClose(CloseEventArgs e) { - GlobalArea.RemoveUserConnectByID(this.ID); - // Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Close" + e.Reason); + GlobalArea.RemoveUserConnectByID(ID); + Log.Info(this.ID + " " + this.iPEndPoint.ToString() + " Conection Close" + e.Reason); // Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}"); } protected override void OnError(ErrorEventArgs e) { - GlobalArea.RemoveUserConnectByID(this.ID); - // Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Error Close" + e.Message); + GlobalArea.RemoveUserConnectByID(ID); + Log.Info(this.ID + " " + this.iPEndPoint.ToString() + " Conection Error Close" + e.Message); // Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}"); } }