From db903a131e08b9a35d97f0c4cea2f4a8d59508f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=94=84=E5=A4=B4?= Date: Mon, 9 Sep 2024 09:45:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DB/DB.csproj | 20 +++++++ DB/DbCommandInterceptor.cs | 81 ++++++++++++++++++++++++++ DB/DbFactory.cs | 24 +++++++- DB/MappingHelper.cs | 56 ++++++++++++++++++ DB/SystemEntityTypeBuilder.cs | 74 +++++++++++++++++++++++ RFID电子标签层合裁切设备.sln | 14 +++++ RfidTool/Properties/AssemblyInfo.cs | 35 +++++++++++ RfidTool/RfidSetting.cs | 12 ++++ RfidTool/RfidTool.csproj | 56 ++++++++++++++++++ 9 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 DB/DB.csproj create mode 100644 DB/DbCommandInterceptor.cs create mode 100644 DB/MappingHelper.cs create mode 100644 DB/SystemEntityTypeBuilder.cs create mode 100644 RfidTool/Properties/AssemblyInfo.cs create mode 100644 RfidTool/RfidSetting.cs create mode 100644 RfidTool/RfidTool.csproj diff --git a/DB/DB.csproj b/DB/DB.csproj new file mode 100644 index 0000000..217d205 --- /dev/null +++ b/DB/DB.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + diff --git a/DB/DbCommandInterceptor.cs b/DB/DbCommandInterceptor.cs new file mode 100644 index 0000000..6ce9632 --- /dev/null +++ b/DB/DbCommandInterceptor.cs @@ -0,0 +1,81 @@ +using System.Data; +using System.Diagnostics; +using System.Text; +using Chloe.Infrastructure.Interception; + +namespace DB; + + +public class DbCommandInterceptor : IDbCommandInterceptor +{ + + public void ReaderExecuting(IDbCommand command, DbCommandInterceptionContext interceptionContext) + { + ToDb(command, interceptionContext.Exception); + } + + public void ReaderExecuted(IDbCommand command, DbCommandInterceptionContext interceptionContext) + { + ToDb(command, interceptionContext.Exception); + } + + public void NonQueryExecuting(IDbCommand command, DbCommandInterceptionContext interceptionContext) + { + ToDb(command, interceptionContext.Exception); + } + + public void NonQueryExecuted(IDbCommand command, DbCommandInterceptionContext interceptionContext) + { + ToDb(command, interceptionContext.Exception); + } + + public void ScalarExecuting(IDbCommand command, DbCommandInterceptionContext interceptionContext) + { + ToDb(command, interceptionContext.Exception); + } + + public void ScalarExecuted(IDbCommand command, DbCommandInterceptionContext interceptionContext) + { + ToDb(command, interceptionContext.Exception); + } + + private void ToDb(IDbCommand command, Exception? exception) + { + +#if DEBUG + Trace.WriteLine(command.CommandText); + Trace.WriteLine(AppendDbCommandInfo(command)); +#endif + } + + private string AppendDbCommandInfo(IDbCommand command) + { + var sb = new StringBuilder(); + + foreach (IDbDataParameter param in command.Parameters) + { + if (param == null) + continue; + + object value; + if (param.Value == null || param.Value == DBNull.Value) + { + value = "NULL"; + } + else + { + value = param.Value; + + if (param.DbType == DbType.String || param.DbType == DbType.AnsiString || + param.DbType == DbType.DateTime) + value = "'" + value + "'"; + } + + sb.AppendFormat("{3} {0} {1} = {2};", Enum.GetName(typeof(DbType), param.DbType), param.ParameterName, + value, Enum.GetName(typeof(ParameterDirection), param.Direction)); + sb.AppendLine(); + } + + return sb.ToString(); + } +} \ No newline at end of file diff --git a/DB/DbFactory.cs b/DB/DbFactory.cs index 0513fce..bb36f7f 100644 --- a/DB/DbFactory.cs +++ b/DB/DbFactory.cs @@ -1,6 +1,28 @@ -namespace DB; +using System.Data; +using Chloe; +using Chloe.PostgreSQL; +using Npgsql; +using RfidTool; + +namespace DB; public class DbFactory { + + public static IDbContext GetContext + { + get + { + + PostgreSQLContext context = new PostgreSQLContext(() => + { + IDbConnection conn = new NpgsqlConnection(RfidSetting.Current.Db); //需要自己引入数据库驱动 + + return conn; + }); + context.Options.ConvertToLowercase = false; + return context; + } + } } \ No newline at end of file diff --git a/DB/MappingHelper.cs b/DB/MappingHelper.cs new file mode 100644 index 0000000..94cb81e --- /dev/null +++ b/DB/MappingHelper.cs @@ -0,0 +1,56 @@ +using Chloe.Entity; +using Chloe.Infrastructure.Interception; +using Chloe.Infrastructure; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace DB +{ + public class MappingHelper + { + public static IEntityTypeBuilder[] FindMapsFromAssembly(Assembly assembly) + { + var typeOf_IMap = typeof(IEntityTypeBuilder); + + var mapTypes = assembly.GetTypes().Where(a => a.IsClass && !a.IsAbstract && typeOf_IMap.IsAssignableFrom(a)) + .ToList(); + var maps = mapTypes.Select(a => (IEntityTypeBuilder)Activator.CreateInstance(a)); + + return maps.ToArray(); + } + } + + public static class DbInfo + { + public static void Init() + { + IDbCommandInterceptor interceptor = new DbCommandInterceptor(); + DbInterception.Add(interceptor); + } + + public static void Init(Assembly assembly) + { + Init(); + var findMapsFromAssembly = MappingHelper.FindMapsFromAssembly(assembly); + DbConfiguration.UseTypeBuilders(findMapsFromAssembly); + + } + + public static void Init(List assemblys) + { + List list = new List(); + foreach (var assembly in assemblys) + { + var findMapsFromAssembly = MappingHelper.FindMapsFromAssembly(assembly); + list.AddRange(findMapsFromAssembly); + } + DbConfiguration.UseTypeBuilders(list.ToArray()); + + } + } +} diff --git a/DB/SystemEntityTypeBuilder.cs b/DB/SystemEntityTypeBuilder.cs new file mode 100644 index 0000000..5d3dac6 --- /dev/null +++ b/DB/SystemEntityTypeBuilder.cs @@ -0,0 +1,74 @@ +using Chloe.Annotations; +using Chloe.Entity; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DB +{ + public abstract class SystemEntityTypeBuilder : EntityTypeBuilder where T : BaseChimsDb + { + + protected SystemEntityTypeBuilder(string tableName) + { + this.MapTo(tableName); + this.Property(a => a.ID).IsAutoIncrement(false).IsPrimaryKey(); + this.UpdateIgnore(x => x.CreateUserId); + this.UpdateIgnore(x => x.CreateDate); + this.UpdateIgnore(x => x.CreateUserName); + HasQueryFilter(x => x.IsDelete == 0 && x.IsEnable == 1); + + } + + } + + public class BaseChimsDb + { + + [Column(IsPrimaryKey = true)] + [NonAutoIncrement] + public long ID { get; set; } + + /// + /// + public int IsEnable { get; set; } = 1; + + /// + /// + public int IsDelete { get; set; } = 0; + + + + /// + /// + [UpdateIgnore] + public DateTime CreateDate { get; set; } = DateTime.Now; + + /// + /// + [UpdateIgnore] + public string CreateUserId { get; set; } = ""; + + /// + /// + [UpdateIgnore] + public string CreateUserName { get; set; } = ""; + + /// + /// + public DateTime LastModifyDate { get; set; } = DateTime.Now; + + /// + /// + public string LastModifyUserId { get; set; } = ""; + + /// + /// + public string LastModifyUserName { get; set; } = ""; + + public int SortCode { get; set; } = 0; + } +} diff --git a/RFID电子标签层合裁切设备.sln b/RFID电子标签层合裁切设备.sln index a55ff74..d5b1a0e 100644 --- a/RFID电子标签层合裁切设备.sln +++ b/RFID电子标签层合裁切设备.sln @@ -1,8 +1,22 @@  Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DB", "DB\DB.csproj", "{17786C17-7536-4386-9292-B2B46E2A2ABC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RfidTool", "RfidTool\RfidTool.csproj", "{89746EE2-B3A7-4F88-989C-2AC91AFDE614}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {17786C17-7536-4386-9292-B2B46E2A2ABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {17786C17-7536-4386-9292-B2B46E2A2ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {17786C17-7536-4386-9292-B2B46E2A2ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {17786C17-7536-4386-9292-B2B46E2A2ABC}.Release|Any CPU.Build.0 = Release|Any CPU + {89746EE2-B3A7-4F88-989C-2AC91AFDE614}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {89746EE2-B3A7-4F88-989C-2AC91AFDE614}.Debug|Any CPU.Build.0 = Debug|Any CPU + {89746EE2-B3A7-4F88-989C-2AC91AFDE614}.Release|Any CPU.ActiveCfg = Release|Any CPU + {89746EE2-B3A7-4F88-989C-2AC91AFDE614}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection EndGlobal diff --git a/RfidTool/Properties/AssemblyInfo.cs b/RfidTool/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d133f04 --- /dev/null +++ b/RfidTool/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("RfidTool")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("RfidTool")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("89746EE2-B3A7-4F88-989C-2AC91AFDE614")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/RfidTool/RfidSetting.cs b/RfidTool/RfidSetting.cs new file mode 100644 index 0000000..2c43dbf --- /dev/null +++ b/RfidTool/RfidSetting.cs @@ -0,0 +1,12 @@ +using System.ComponentModel; +using NewLife.Configuration; + +namespace RfidTool +{ + [DisplayName("核心设置")] + [Config("Core")] + public class RfidSetting:Config + { + public string Db { get; set; } = "server=127.0.0.1;database=postgres;uid=postgres;pwd=yangwei"; + } +} \ No newline at end of file diff --git a/RfidTool/RfidTool.csproj b/RfidTool/RfidTool.csproj new file mode 100644 index 0000000..7585878 --- /dev/null +++ b/RfidTool/RfidTool.csproj @@ -0,0 +1,56 @@ + + + + + Debug + AnyCPU + {89746EE2-B3A7-4F88-989C-2AC91AFDE614} + Library + Properties + RfidTool + RfidTool + v4.8 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + \ No newline at end of file