添加聊天

This commit is contained in:
zpc 2024-07-18 06:03:47 +08:00
parent 8c6d7c975f
commit 1a265b0637
10 changed files with 280 additions and 3 deletions

View File

@ -31,7 +31,7 @@ foreach (var table in tables)
useStringBuilder.Append(className + ".Seed(modelBuilder);");
sb.Append($@"
namespace HZY.Repository.EntityFramework.Admin.Migrations.SeedsDatas.Datas;
namespace MiaoYu.Repository.EntityFramework.Admin.Migrations.SeedsDatas.Datas;
public static class {className}
{{

View File

@ -43,7 +43,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiaoYu.Core.Swagger", "Miao
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiaoYu.WinFormDemo", "MiaoYu.WinFormDemo\MiaoYu.WinFormDemo.csproj", "{75959B17-A901-49E1-A5E7-667349DC4203}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiaoYu.Shared.Admin", "MiaoYu.Shared.Admin\MiaoYu.Shared.Admin.csproj", "{925EF035-4A41-42E4-A3A4-B8E9AE52F6B7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiaoYu.Shared.Admin", "MiaoYu.Shared.Admin\MiaoYu.Shared.Admin.csproj", "{925EF035-4A41-42E4-A3A4-B8E9AE52F6B7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiaoYu.Repository.ChatAI.Admin", "MiaoYu.Repository.ChatAI.Admin\MiaoYu.Repository.ChatAI.Admin.csproj", "{39C765DB-41E7-4BC6-B75E-2A90CFF3A8EF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -115,6 +117,10 @@ Global
{925EF035-4A41-42E4-A3A4-B8E9AE52F6B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{925EF035-4A41-42E4-A3A4-B8E9AE52F6B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{925EF035-4A41-42E4-A3A4-B8E9AE52F6B7}.Release|Any CPU.Build.0 = Release|Any CPU
{39C765DB-41E7-4BC6-B75E-2A90CFF3A8EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39C765DB-41E7-4BC6-B75E-2A90CFF3A8EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39C765DB-41E7-4BC6-B75E-2A90CFF3A8EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39C765DB-41E7-4BC6-B75E-2A90CFF3A8EF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -136,6 +142,7 @@ Global
{452F4EDD-E92B-4673-B5FB-863595996614} = {DB46F54A-9F53-44EC-80F8-9E53F0B871CF}
{75959B17-A901-49E1-A5E7-667349DC4203} = {DC7A7E4A-B4DC-4958-BAA5-2BBB1A153B5F}
{925EF035-4A41-42E4-A3A4-B8E9AE52F6B7} = {ACA91C4A-33F0-4ECA-90D2-BEC0811764EE}
{39C765DB-41E7-4BC6-B75E-2A90CFF3A8EF} = {451BE0BB-26ED-47ED-ABC7-23001D21410C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E3C61955-46C1-4D06-994F-C86A72B2B0E2}

View File

@ -9,7 +9,7 @@
// SqlServer MySql PostgreSqlOracle
"DefaultDatabaseType": "SqlServer",
//EFCore
"IsMonitorEFCore": false,
"IsMonitorEFCore": true,
// - mysql
//"ConnectionString": "Server=localhost; port=3306; Database=hzy_admin_mysql_20230227; uid=root; pwd=123456; Convert Zero Datetime=False"
//// - sqlserver
@ -19,5 +19,9 @@
//"ConnectionString": "User ID=postgres;Password=123456;Host=localhost;Port=5432;Database=hzy_microservices_pgsql_20230227;Pooling=true;TimeZone=Asia/Shanghai",
//// - oracle
//"ConnectionString": "user id=hzy_admin_oracle_20221213;password=123456; data source=//127.0.0.1:1521/orcl;Pooling=true;Min Pool Size=1"
},
"ChatAdminRepositoryOptions": {
"ConnectionString": "Server=192.168.195.2;Database=MiaoYu;User ID=zpc;Password=zpc;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;"
}
}

View File

@ -0,0 +1,39 @@

namespace MiaoYu.Repository.ChatAI.Admin
{
/// <summary>
/// 后台管理系统数据库上下文
/// </summary>
[DbContextConfig($"Repository.*.Entities.*")]
public class ChatAdminDbContext : DbContext, IBaseDbContext
{
/// <summary>
/// 工作单元
/// </summary>
public IUnitOfWork UnitOfWork { get; }
public ChatAdminDbContext(DbContextOptions<ChatAdminDbContext> dbContextOptions) : base(dbContextOptions)
{
UnitOfWork = new UnitOfWorkImpl<ChatAdminDbContext>(this);
}
/// <summary>
/// 模型创建
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var dbContextConfigAttribute = GetType().GetCustomAttribute<DbContextConfigAttribute>()!;
dbContextConfigAttribute!.OnModelCreating(modelBuilder, dbContextConfigAttribute.GetModelTypes(GetType()));
#region
//ModelBuilderExtensions.Seed(modelBuilder);
#endregion
}
}
}

View File

@ -0,0 +1,139 @@

using MiaoYu.Repository.ChatAI.Admin.Models;
namespace MiaoYu.Repository.ChatAI.Admin
{
/// <summary>
/// 程序启动器
/// </summary>
[ImportStartupModule<CoreEntityFrameworkStartup>]
public class ChatAdminRepositoryStartup : StartupModule<ChatAdminRepositoryStartup>
{
/// <summary>
/// 程序启动器
/// </summary>
/// <param name="webApplicationBuilder"></param>
public override void ConfigureServices(WebApplicationBuilder webApplicationBuilder)
{
var configuration = webApplicationBuilder.Configuration;
var services = webApplicationBuilder.Services;
var webHostEnvironment = webApplicationBuilder.Environment;
var repositoriesOptions = configuration
.GetSection(nameof(ChatAdminRepositoryOptions))
.Get<ChatAdminRepositoryOptions>() ?? throw new Exception("配置对象 空 异常!");
var connectionString = repositoriesOptions?.ConnectionString;
connectionString = string.IsNullOrWhiteSpace(connectionString) ?
configuration["ConnectionStrings:" + repositoriesOptions!.DefaultDatabaseType.ToString()] :
connectionString;
services.AddDbContextFactory<ChatAdminDbContext>(optionsBuilder =>
{
switch (repositoriesOptions.DefaultDatabaseType)
{
case DefaultDatabaseType.SqlServer:
optionsBuilder
.UseSqlServer(connectionString, w => w.MinBatchSize(1).MaxBatchSize(1000))
;
break;
case DefaultDatabaseType.MySql:
optionsBuilder
.UseMySql(connectionString, MySqlServerVersion.LatestSupportedServerVersion, w => w.MinBatchSize(1).MaxBatchSize(1000))
;
break;
case DefaultDatabaseType.PostgreSql:
//EnableLegacyTimestampBehavior 启动旧行为,避免时区问题,存储时间报错
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
optionsBuilder
.UseNpgsql(connectionString, w => w.MinBatchSize(1).MaxBatchSize(1000))
;
break;
case DefaultDatabaseType.Oracle:
optionsBuilder
.UseOracle(connectionString, w => w.MinBatchSize(1).MaxBatchSize(1000))
;
break;
default:
break;
}
if (webHostEnvironment.IsDevelopment())
{
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
// sql 日志写入控制台
optionsBuilder.UseLoggerFactory(loggerFactory);
}
// 懒加载代理
//options.UseLazyLoadingProxies();
//添加 EFCore 监控 和 动态表名
optionsBuilder.AddEntityFrameworkMonitor(repositoriesOptions.IsMonitorEFCore);
optionsBuilder.AddInterceptors(new AuditInterceptor());
});
services.AddEntityFrameworkRepositories<ChatAdminDbContext>(repositoriesOptions, (auditOptions) =>
{
// 你的自定义审计字段 ...
//auditOptions.Add(new AuditOptions()
//{
// CreationTimeFieldName = nameof(ICreateEntityV2.CreateTime),
// CreatorUserIdFieldName = "",
// LastModificationTimeFieldName = nameof(IUpdateEntityV2.UpdateTime),
// LastModifierUserIdFieldName = "",
// DeletionTimeFieldName = "UpdateTime",
// DeleterUserIdFieldName = "UpdateBy",
// IsDeletedFieldName = "DelFlag",
//});
}, (freesqlOptions) =>
{
freesqlOptions.FreeSqlAuditAopList?.Add(new FreeSqlAuditAop());
freesqlOptions.FreeSqlAction = (freeSql) =>
{
freeSql.Aop.CurdAfter += (object? sender, FreeSql.Aop.CurdAfterEventArgs curdAfter) =>
{
var stringBuilder = new StringBuilder();
stringBuilder.Append($"\r\n====[FreeSql 开始 耗时: {curdAfter.ElapsedMilliseconds} ms]=========");
stringBuilder.Append($"\r\n{curdAfter.Sql}");
stringBuilder.Append($"\r\n====[FreeSql 结束 线程Id:{Environment.CurrentManagedThreadId}]=========");
LogUtil.Log.Warning(stringBuilder.ToString());
};
};
});
}
/// <summary>
/// Configure
/// </summary>
/// <param name="webApplication"></param>
public override void Configure(WebApplication webApplication)
{
// 使用 DbContext
#region
//if (webApplication.Environment.IsDevelopment())
//{
// // 自动迁移 (如果迁移文件有变动)
// using var scope = webApplication.Services.CreateScope();
// using var adminDbContext = scope.ServiceProvider.GetService<ChatAdminDbContext>();
// if (adminDbContext!.Database.GetPendingMigrations().Count() > 0)
// {
// try
// {
// adminDbContext.Database.Migrate();
// }
// catch (Exception ex)
// {
// LogUtil.Log.Error(ex.Message, ex);
// }
// }
//}
#endregion
}
}
}

View File

@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Rougamo />
</Weavers>

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. -->
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="Rougamo" minOccurs="0" maxOccurs="1" type="xs:anyType" />
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
<xs:annotation>
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="VerifyIgnoreCodes" type="xs:string">
<xs:annotation>
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="GenerateXsd" type="xs:boolean">
<xs:annotation>
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>

View File

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Rougamo.Fody" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MiaoYu.Core.EntityFramework\MiaoYu.Core.EntityFramework.csproj" />
<ProjectReference Include="..\MiaoYu.Core.Logs\MiaoYu.Core.Logs.csproj" />
<ProjectReference Include="..\MiaoYu.Core.Quartz\MiaoYu.Core.Quartz.csproj" />
<ProjectReference Include="..\MiaoYu.Core\MiaoYu.Core.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Entities\" />
<Folder Include="Migrations\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,8 @@
namespace MiaoYu.Repository.ChatAI.Admin.Models;
public class ChatAdminRepositoryOptions : RepositoryOptions
{
}

View File

@ -0,0 +1,26 @@
global using MiaoYu.Core.EntityFramework;
global using MiaoYu.Core.EntityFramework.Interceptors;
global using MiaoYu.Core.Quartz.Models;
global using HZY.Framework.Core.AspNetCore;
global using HZY.Framework.Core.Quartz;
global using HZY.Framework.Repository.EntityFramework;
global using HZY.Framework.Repository.EntityFramework.Attributes;
global using HZY.Framework.Repository.EntityFramework.Models;
global using HZY.Framework.Repository.EntityFramework.Models.Enums;
global using HZY.Framework.Repository.EntityFramework.Models.Standard;
global using HZY.Framework.Repository.EntityFramework.Repositories;
global using HZY.Framework.Repository.EntityFramework.Repositories.Impl;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.EntityFrameworkCore.Infrastructure;
global using Microsoft.Extensions.Caching.Memory;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.Logging;
global using System.ComponentModel.DataAnnotations;
global using System.ComponentModel.DataAnnotations.Schema;
global using System.Reflection;
global using MiaoYu.Core.Logs;
global using System.Text;