feat: db动态脚本基类修改

This commit is contained in:
Diego
2025-03-01 16:04:40 +08:00
parent d248e047b3
commit caa3ebd600
20 changed files with 231 additions and 102 deletions

View File

@@ -22,7 +22,7 @@
<PackageReference Include="UAParser" Version="3.1.47" />
<PackageReference Include="Rougamo.Fody" Version="5.0.0" />
<PackageReference Include="MailKit" Version="4.10.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.179" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.180" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.*" />

View File

@@ -0,0 +1,43 @@
<Project>
<!--Admin-->
<ItemGroup>
<PackageReference Include="ThingsGateway.Admin.Razor" Version="$(Version)" />
<PackageReference Include="ThingsGateway.Admin.Application" Version="$(Version)" GeneratePathProperty="true"/>
</ItemGroup>
<Target Name="_ResolveCopyAdminLocalNuGetPkgXmls" AfterTargets="ResolveReferences">
<ItemGroup>
<ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->'%(RootDir)%(Directory)%(Filename).xml')" Condition="'%(ReferenceCopyLocalPaths.NuGetPackageId)'=='ThingsGateway.Admin.Application' and Exists('%(RootDir)%(Directory)%(Filename).xml')" />
</ItemGroup>
</Target>
<Target Name="CopyAdminNugetPackages" AfterTargets="Build">
<ItemGroup>
<!-- setting up the variable for convenience -->
<AdminApplicationPackageFiles Include="$(PkgThingsGateway_Admin_Application)\Content\SeedData\Admin\*.*" />
</ItemGroup>
<PropertyGroup>
<AdminApplicationFolder>$(TargetDir)SeedData\Admin\</AdminApplicationFolder>
</PropertyGroup>
<RemoveDir Directories="$(AdminApplicationFolder)" />
<Copy SourceFiles="@(AdminApplicationPackageFiles)" DestinationFolder="$(AdminApplicationFolder)%(RecursiveDir)" />
</Target>
<Target Name="AdminPostPublish" AfterTargets="Publish">
<ItemGroup>
<!-- setting up the variable for convenience -->
<AdminFiles Include="bin\$(Configuration)\$(TargetFramework)\SeedData\**" />
</ItemGroup>
<PropertyGroup>
</PropertyGroup>
<Copy SourceFiles="@(AdminFiles)" DestinationFolder="$(PublishDir)SeedData\%(RecursiveDir)" />
</Target>
<!--Admin-->
</Project>

View File

@@ -6,7 +6,15 @@
"AppSettings": {
"InjectSpecificationDocument": true, // 生产环境是否开启Swagger
"ExternalAssemblies": [ "Plugins" ] // 插件目录
"ExternalAssemblies": [ "Plugins" ], // 插件目录
// nuget动态加载的程序集
"SupportPackageNamePrefixs": [
"ThingsGateway.Admin.Application",
"ThingsGateway.Admin.Razor",
"ThingsGateway.Razor"
]
},
"DynamicApiControllerSettings": {
//"DefaultRoutePrefix": "api", // 默认路由前缀

View File

@@ -23,9 +23,9 @@ public class Program
public static async Task Main(string[] args)
{
await Task.Delay(2000).ConfigureAwait(false);
//当前工作目录设为程序集的基目录
System.IO.Directory.SetCurrentDirectory(AppContext.BaseDirectory);
// 增加中文编码支持
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
@@ -34,6 +34,7 @@ public class Program
Console.Write(Environment.NewLine);
Console.ForegroundColor = ConsoleColor.Yellow;
XTrace.WriteLine(string.Empty);
XTrace.UseConsole();
Console.WriteLine(
"""
@@ -65,6 +66,7 @@ public class Program
}).ConfigureBuilder(builder =>
{
if (!builder.Environment.IsDevelopment())
{
builder.Services.AddResponseCompression(

View File

@@ -27,6 +27,7 @@ using ThingsGateway.Admin.Application;
using ThingsGateway.Admin.Razor;
using ThingsGateway.Extension;
using ThingsGateway.Logging;
using ThingsGateway.NewLife.Caching;
namespace ThingsGateway.AdminServer;
@@ -35,9 +36,9 @@ public class Startup : AppStartup
{
public void ConfigBlazorServer(IServiceCollection services)
{
// 增加中文编码支持网页源码显示汉字
services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
//并发启动/停止host
services.Configure<HostOptions>(options =>
{
@@ -45,6 +46,7 @@ public class Startup : AppStartup
options.ServicesStopConcurrently = true;
});
// 事件总线
services.AddEventBus(options =>
{
@@ -57,6 +59,8 @@ public class Startup : AppStartup
options.AddPersistence<JobPersistence>();
});
// 缓存
services.AddSingleton<ICache, MemoryCache>();
// 允许跨域
services.AddCorsAccessor();
@@ -92,9 +96,9 @@ public class Startup : AppStartup
#if NET8_0_OR_GREATER
services
.AddRazorComponents(options =>
{
options.TemporaryRedirectionUrlValidityDuration = TimeSpan.FromMinutes(10);
})
{
options.TemporaryRedirectionUrlValidityDuration = TimeSpan.FromMinutes(10);
})
.AddInteractiveServerComponents(options =>
{
options.RootComponents.MaxJSRootComponents = 500;
@@ -155,15 +159,6 @@ public class Startup : AppStartup
options.WriteFilter = (logMsg) =>
{
return true;
////如果不是LoggingMonitor日志才格式化
//if (logMsg.LogName != "System.Logging.LoggingMonitor")
//{
// return true;
//}
//else
//{
// return false;
//}
};
options.MessageFormat = (logMsg) =>
@@ -273,10 +268,10 @@ public class Startup : AppStartup
services.AddScoped<BlazorAppContext>(a =>
{
var appContext = new BlazorAppContext(
a.GetService<ISysResourceService>(),
a.GetService<IUserCenterService>(),
a.GetService<ISysUserService>());
appContext.TitleLocalizer = a.GetRequiredService<IStringLocalizer<ThingsGateway.AdminServer.MainLayout>>();
a.GetService<ISysResourceService>(),
a.GetService<IUserCenterService>(),
a.GetService<ISysUserService>());
appContext.TitleLocalizer = a.GetRequiredService<IStringLocalizer<MainLayout>>();
return appContext;
});
@@ -406,11 +401,8 @@ public class Startup : AppStartup
#if NET8_0_OR_GREATER
app.UseAntiforgery();
#endif
app.MapControllers();
}
/// <summary>

View File

@@ -6,7 +6,8 @@
<PropertyGroup>
<TargetFrameworks>net9.0;net8.0;</TargetFrameworks>
</PropertyGroup>
<Import Project="Admin.targets" Condition=" '$(Configuration)' != 'Debug' " />
<PropertyGroup>
<OpenApiGenerateDocuments>false</OpenApiGenerateDocuments>
<SatelliteResourceLanguages>zh-Hans;en-US</SatelliteResourceLanguages>
@@ -24,10 +25,12 @@
<!--<PlatformTarget>x86</PlatformTarget>-->
</PropertyGroup>
<ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<ProjectReference Include="..\ThingsGateway.Admin.Razor\ThingsGateway.Admin.Razor.csproj" />
</ItemGroup>
<ItemGroup>
<Content Remove="Locales\*.json" />
<EmbeddedResource Include="Locales\*.json">

View File

@@ -39,7 +39,7 @@
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="Mapster" Version="7.4.0" />
<PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.5.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.3.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">

View File

@@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BootstrapBlazor.FontAwesome" Version="9.0.2" />
<PackageReference Include="BootstrapBlazor" Version="9.4.1" />
<PackageReference Include="BootstrapBlazor" Version="9.4.2" />
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
</ItemGroup>

View File

@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<PluginVersion>10.0.2.14</PluginVersion>
<ProPluginVersion>10.0.2.14</ProPluginVersion>
<PluginVersion>10.0.2.15</PluginVersion>
<ProPluginVersion>10.0.2.15</ProPluginVersion>
</PropertyGroup>
<PropertyGroup>

View File

@@ -8,10 +8,37 @@
// QQ群605534569
//------------------------------------------------------------------------------
using Microsoft.Extensions.Logging;
using SqlSugar;
namespace ThingsGateway.Plugin.DB;
public interface IDynamicSQL
public abstract class DynamicSQLBase
{
IEnumerable<dynamic> GetList(IEnumerable<object> datas);
Type GetModelType();
public TouchSocket.Core.ILog LogMessage { get; set; }
public virtual IEnumerable<dynamic> GetList(IEnumerable<object> datas)
{
throw new NotSupportedException();
}
public virtual Type GetModelType()
{
throw new NotSupportedException();
}
/// <summary>
/// false=>实现<see cref="GetList(IEnumerable{object})"/>和<see cref="GetModelType"/>
///
/// true=>实现<see cref="DBInsertable(ISqlSugarClient, IEnumerable{object},CancellationToken)"/>
/// </summary>
public virtual bool ManualUpload { get; set; }
/// <summary>
/// 完全自定义上传
/// </summary>
/// <returns></returns>
public virtual Task DBInsertable(ISqlSugarClient db, IEnumerable<object> datas, CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
}

View File

@@ -1,22 +1,24 @@
// ------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://thingsgateway.cn/
// QQ群605534569
// ------------------------------------------------------------------------------
////------------------------------------------------------------------------------
////此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
//// 此代码版权除特别声明外的代码归作者本人Diego所有
//// 源代码使用协议遵循本仓库的开源协议及附加协议
//// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
//// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
//// 使用文档https://thingsgateway.cn/
//// QQ群605534569
//// ------------------------------------------------------------------------------
//using SqlSugar;
//using ThingsGateway;
//using ThingsGateway.Foundation;
//using ThingsGateway.NewLife.Extension;
//using ThingsGateway.Plugin.DB;
//using ThingsGateway.Plugin.SqlDB;
//public class TestHistorySQL : IDynamicSQL
//public class TestHistorySQL : DynamicSQLBase
//{
// public IEnumerable<dynamic> GetList(IEnumerable<object> datas)
// public override IEnumerable<dynamic> GetList(IEnumerable<object> datas)
// {
// var sqlHistoryValues = datas.Cast<SQLHistoryValue>().OrderByDescending(a => a.CollectTime).DistinctBy(a => a.Name);
// List<HistoryModel1> demoDatas = new List<HistoryModel1>();
@@ -31,7 +33,7 @@
// return demoDatas;
// }
// public Type GetModelType()
// public override Type GetModelType()
// {
// return typeof(HistoryModel1);
// }

View File

@@ -1,22 +1,25 @@
// ------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://thingsgateway.cn/
// QQ群605534569
// ------------------------------------------------------------------------------
//// ------------------------------------------------------------------------------
//// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
//// 此代码版权除特别声明外的代码归作者本人Diego所有
//// 源代码使用协议遵循本仓库的开源协议及附加协议
//// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
//// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
//// 使用文档https://thingsgateway.cn/
//// QQ群605534569
//// ------------------------------------------------------------------------------
//using SqlSugar;
//using ThingsGateway;
//using ThingsGateway.Foundation;
//using ThingsGateway.NewLife.Extension;
//using ThingsGateway.Plugin.DB;
//using ThingsGateway.Plugin.SqlDB;
//public class TestRealSQL : IDynamicSQL
//public class TestRealSQL : DynamicSQLBase
//{
// public IEnumerable<dynamic> GetList(IEnumerable<object> datas)
// public override IEnumerable<dynamic> GetList(IEnumerable<object> datas)
// {
// var sqlRealValues = datas.Cast<SQLRealValue>().OrderByDescending(a => a.CollectTime).DistinctBy(a => a.Name);
// List<RealModel1> demoDatas = new List<RealModel1>();
@@ -32,7 +35,7 @@
// return demoDatas;
// }
// public Type GetModelType()
// public override Type GetModelType()
// {
// return typeof(RealModel1);

View File

@@ -153,9 +153,13 @@ public partial class QuestDBProducer : BusinessBaseWithCacheIntervalVariableMode
//必须为间隔上传
if (!_driverPropertys.BigTextScriptHistoryTable.IsNullOrEmpty())
{
var hisModel = CSharpScriptEngineExtension.Do<IDynamicSQL>(_driverPropertys.BigTextScriptHistoryTable);
var type = hisModel.GetModelType();
db.CodeFirst.InitTables(type);
var hisModel = CSharpScriptEngineExtension.Do<DynamicSQLBase>(_driverPropertys.BigTextScriptHistoryTable);
hisModel.LogMessage = LogMessage;
if (!hisModel.ManualUpload)
{
var type = hisModel.GetModelType();
db.CodeFirst.InitTables(type);
}
}
else

View File

@@ -62,12 +62,20 @@ public partial class QuestDBProducer : BusinessBaseWithCacheIntervalVariableMode
db.Ado.CancellationToken = cancellationToken;
if (!_driverPropertys.BigTextScriptHistoryTable.IsNullOrEmpty())
{
var getDeviceModel = CSharpScriptEngineExtension.Do<IDynamicSQL>(_driverPropertys.BigTextScriptHistoryTable);
var result = await db.InsertableByObject(getDeviceModel.GetList(dbInserts)).ExecuteCommandAsync().ConfigureAwait(false);
//var result = await db.Insertable(dbInserts).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);
if (result > 0)
var getDeviceModel = CSharpScriptEngineExtension.Do<DynamicSQLBase>(_driverPropertys.BigTextScriptHistoryTable);
getDeviceModel.LogMessage = LogMessage;
if (getDeviceModel.ManualUpload)
{
LogMessage.Trace($"HistoryTable Data Count{result}");
await getDeviceModel.DBInsertable(db, dbInserts, cancellationToken).ConfigureAwait(false);
}
else
{
var result = await db.InsertableByObject(getDeviceModel.GetList(dbInserts)).ExecuteCommandAsync().ConfigureAwait(false);
//var result = await db.Insertable(dbInserts).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);
if (result > 0)
{
LogMessage.Trace($"HistoryTable Data Count{result}");
}
}
}
else

View File

@@ -173,11 +173,15 @@ public partial class SqlDBProducer : BusinessBaseWithCacheIntervalVariableModel<
//必须为间隔上传
if (!_driverPropertys.BigTextScriptHistoryTable.IsNullOrEmpty())
{
var hisModel = CSharpScriptEngineExtension.Do<IDynamicSQL>(_driverPropertys.BigTextScriptHistoryTable);
if (_driverPropertys.IsHistoryDB)
var hisModel = CSharpScriptEngineExtension.Do<DynamicSQLBase>(_driverPropertys.BigTextScriptHistoryTable);
if (!hisModel.ManualUpload)
{
var type = hisModel.GetModelType();
db.CodeFirst.InitTables(type);
if (_driverPropertys.IsHistoryDB)
{
var type = hisModel.GetModelType();
db.CodeFirst.InitTables(type);
}
}
}
@@ -188,11 +192,14 @@ public partial class SqlDBProducer : BusinessBaseWithCacheIntervalVariableModel<
}
if (!_driverPropertys.BigTextScriptRealTable.IsNullOrEmpty())
{
var realModel = CSharpScriptEngineExtension.Do<IDynamicSQL>(_driverPropertys.BigTextScriptRealTable);
if (_driverPropertys.IsReadDB)
var realModel = CSharpScriptEngineExtension.Do<DynamicSQLBase>(_driverPropertys.BigTextScriptRealTable);
if (!realModel.ManualUpload)
{
var type = realModel.GetModelType();
db.CodeFirst.InitTables(type);
if (_driverPropertys.IsReadDB)
{
var type = realModel.GetModelType();
db.CodeFirst.InitTables(type);
}
}
}
else

View File

@@ -71,12 +71,21 @@ public partial class SqlDBProducer : BusinessBaseWithCacheIntervalVariableModel<
db.Ado.CancellationToken = cancellationToken;
if (!_driverPropertys.BigTextScriptHistoryTable.IsNullOrEmpty())
{
var getDeviceModel = CSharpScriptEngineExtension.Do<IDynamicSQL>(_driverPropertys.BigTextScriptHistoryTable);
var result = await db.InsertableByObject(getDeviceModel.GetList(dbInserts)).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);
//var result = await db.Insertable(dbInserts).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);
if (result > 0)
var getDeviceModel = CSharpScriptEngineExtension.Do<DynamicSQLBase>(_driverPropertys.BigTextScriptHistoryTable);
getDeviceModel.LogMessage = LogMessage;
if (getDeviceModel.ManualUpload)
{
LogMessage.Trace($"HistoryTable Data Count{result}");
await getDeviceModel.DBInsertable(db, dbInserts, cancellationToken).ConfigureAwait(false);
}
else
{
var result = await db.InsertableByObject(getDeviceModel.GetList(dbInserts)).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);
//var result = await db.Insertable(dbInserts).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);
if (result > 0)
{
LogMessage.Trace($"HistoryTable Data Count{result}");
}
}
}
else
@@ -107,29 +116,39 @@ public partial class SqlDBProducer : BusinessBaseWithCacheIntervalVariableModel<
if (!_driverPropertys.BigTextScriptRealTable.IsNullOrEmpty())
{
var getDeviceModel = CSharpScriptEngineExtension.Do<IDynamicSQL>(_driverPropertys.BigTextScriptRealTable);
if (!_initRealData)
var getDeviceModel = CSharpScriptEngineExtension.Do<DynamicSQLBase>(_driverPropertys.BigTextScriptRealTable);
getDeviceModel.LogMessage = LogMessage;
if (getDeviceModel.ManualUpload)
{
if (datas?.Count > 0)
{
var result = db.StorageableByObject(getDeviceModel.GetList(datas)).ExecuteCommand();
if (result > 0)
LogMessage.Trace($"RealTable Data Count{result}");
_initRealData = true;
return OperResult.Success;
}
await getDeviceModel.DBInsertable(db, datas, cancellationToken).ConfigureAwait(false);
return OperResult.Success;
}
else
{
if (datas?.Count > 0)
if (!_initRealData)
{
var result = await db.UpdateableByObject(getDeviceModel.GetList(datas)).ExecuteCommandAsync().ConfigureAwait(false);
if (result > 0)
LogMessage.Trace($"RealTable Data Count{result}");
if (datas?.Count > 0)
{
var result = db.StorageableByObject(getDeviceModel.GetList(datas)).ExecuteCommand();
if (result > 0)
LogMessage.Trace($"RealTable Data Count{result}");
_initRealData = true;
return OperResult.Success;
}
return OperResult.Success;
}
else
{
if (datas?.Count > 0)
{
var result = await db.UpdateableByObject(getDeviceModel.GetList(datas)).ExecuteCommandAsync().ConfigureAwait(false);
if (result > 0)
LogMessage.Trace($"RealTable Data Count{result}");
return OperResult.Success;
}
return OperResult.Success;
}
return OperResult.Success;
}
}
else

View File

@@ -151,9 +151,12 @@ public partial class TDengineDBProducer : BusinessBaseWithCacheIntervalVariableM
//必须为间隔上传
if (!_driverPropertys.BigTextScriptHistoryTable.IsNullOrEmpty())
{
var hisModel = CSharpScriptEngineExtension.Do<IDynamicSQL>(_driverPropertys.BigTextScriptHistoryTable);
var type = hisModel.GetModelType();
db.CodeFirst.InitTables(type);
var hisModel = CSharpScriptEngineExtension.Do<DynamicSQLBase>(_driverPropertys.BigTextScriptHistoryTable);
if (!hisModel.ManualUpload)
{
var type = hisModel.GetModelType();
db.CodeFirst.InitTables(type);
}
}
else

View File

@@ -68,12 +68,20 @@ public partial class TDengineDBProducer : BusinessBaseWithCacheIntervalVariableM
if (!_driverPropertys.BigTextScriptHistoryTable.IsNullOrEmpty())
{
var getDeviceModel = CSharpScriptEngineExtension.Do<IDynamicSQL>(_driverPropertys.BigTextScriptHistoryTable);
var result = await db.InsertableByObject(getDeviceModel.GetList(dbInserts)).ExecuteCommandAsync().ConfigureAwait(false);
//var result = await db.Insertable(dbInserts).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);
if (result > 0)
var getDeviceModel = CSharpScriptEngineExtension.Do<DynamicSQLBase>(_driverPropertys.BigTextScriptHistoryTable);
getDeviceModel.LogMessage = LogMessage;
if (getDeviceModel.ManualUpload)
{
LogMessage.Trace($"HistoryTable Data Count{result}");
await getDeviceModel.DBInsertable(db, dbInserts, cancellationToken).ConfigureAwait(false);
}
else
{
var result = await db.InsertableByObject(getDeviceModel.GetList(dbInserts)).ExecuteCommandAsync().ConfigureAwait(false);
//var result = await db.Insertable(dbInserts).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);
if (result > 0)
{
LogMessage.Trace($"HistoryTable Data Count{result}");
}
}
}
else

View File

@@ -18,7 +18,7 @@
<Import Project="targets\GatewayOther.targets" Condition=" '$(SolutionName)' == 'ThingsGatewayRelease' " />
<!--nuget包解压复制文件上下文动态加载-->
<Import Project="targets\PluginContext.targets" Condition=" '$(SolutionName)' != 'ThingsGatewayPro' OR '$(Configuration)' != 'Debug' " />
<Import Project="targets\PluginContext.targets" Condition=" '$(SolutionName)' != 'ThingsGatewayPro' AND '$(Configuration)' != 'Debug' " />
<!--直接引用-->
<Import Project="targets\PluginDebug.targets" Condition=" '$(SolutionName)' != 'ThingsGatewayPro' AND '$(Configuration)' == 'Debug' " />

View File

@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>10.0.2.14</Version>
<Version>10.0.2.15</Version>
</PropertyGroup>
<ItemGroup>