diff --git a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs index 4da2b86f6..6d7e35357 100644 --- a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs +++ b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs @@ -483,7 +483,7 @@ namespace IRaCIS.Core.API.Controllers if(incommand.VisitTaskId!=null && incommand.VisitTaskId !=Guid.Empty) { - await _noneDicomStudyFileRepository.AddAsync(new NoneDicomStudyFile() { FileName = item.FileName, Path = item.FilePath, OriginNoneDicomStudyId=noneDicomStudyId.Value, FileType = item.FileType, FileSize = item.FileFize }); + await _noneDicomStudyFileRepository.AddAsync(new NoneDicomStudyFile() { FileName = item.FileName, Path = item.FilePath, OriginNoneDicomStudyId=noneDicomStudyId.Value, VisitTaskId=incommand.VisitTaskId, FileType = item.FileType, FileSize = item.FileFize }); } else diff --git a/IRaCIS.Core.Application/Service/Management/UserService.cs b/IRaCIS.Core.Application/Service/Management/UserService.cs index 5aed045e7..06fda2da9 100644 --- a/IRaCIS.Core.Application/Service/Management/UserService.cs +++ b/IRaCIS.Core.Application/Service/Management/UserService.cs @@ -760,14 +760,6 @@ namespace IRaCIS.Application.Services return ResponseOutput.NotOk(_localizer["User_Disabled"], new LoginReturnDTO()); } - //超过90天没修改密码 - if (_verifyConfig.CurrentValue.IsNeedChangePassWord && loginUser.LastChangePassWordTime != null && DateTime.Now.AddDays(-_verifyConfig.CurrentValue.ChangePassWordDays) > loginUser.LastChangePassWordTime.Value) - { - loginUser.LoginState = 1; - } - - - //登录成功 清除缓存 await _fusionCache.SetAsync(cacheKey, 0, TimeSpan.FromMinutes(lockoutMinutes)); var ipinfo = _searcher.Search(_userInfo.IP); @@ -781,8 +773,12 @@ namespace IRaCIS.Application.Services { loginUser.LoginState = 2; } + } - + //超过90天没修改密码 + if (_verifyConfig.CurrentValue.IsNeedChangePassWord && loginUser.LastChangePassWordTime != null && DateTime.Now.AddDays(-_verifyConfig.CurrentValue.ChangePassWordDays) > loginUser.LastChangePassWordTime.Value) + { + loginUser.LoginState = 1; } await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = loginUser.Id, OptUserId = loginUser.Id, OptType = UserOptType.Login }, true); diff --git a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs index c8be13bf5..933e3990a 100644 --- a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs +++ b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs @@ -57,13 +57,9 @@ namespace IRaCIS.Core.Infra.EFCore public class IRaCISDBContext : DbContext { - private readonly ILogger _logger; - public IRaCISDBContext(DbContextOptions options, ILogger logger - - ) : base(options) + public IRaCISDBContext(DbContextOptions options) : base(options) { - _logger = logger; } @@ -163,68 +159,6 @@ namespace IRaCIS.Core.Infra.EFCore #endregion - public override async Task SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken()) - { - - try - { - return await base.SaveChangesAsync(cancellationToken); - - } - catch (UniqueConstraintException ex) - { - _logger.LogError(ex.InnerException is null ? ex.Message : ex.InnerException?.Message); - - throw new DBSaveFailedException("该唯一键已经存在于数据库中。"); - - } - catch (TimeoutException ex) - { - _logger.LogError(ex.InnerException is null ? ex.Message : ex.InnerException?.Message); - - throw new DBSaveFailedException("数据库操作已经超时,请稍后重试。"); - - } - catch (CannotInsertNullException ex) - { - _logger.LogError(ex.InnerException is null ? ex.Message : ex.InnerException?.Message); - - throw new DBSaveFailedException("无法在非空列上插入空值。"); - } - catch (MaxLengthExceededException ex) - { - _logger.LogError(ex.InnerException is null ? ex.Message : ex.InnerException?.Message); - - throw new DBSaveFailedException("字符串超过了数据库列的最大长度。"); - } - catch (NumericOverflowException ex) - { - _logger.LogError(ex.InnerException is null ? ex.Message : ex.InnerException?.Message); - throw new DBSaveFailedException("数值超过了数据类型的范围。"); - } - catch (SyntaxErrorException ex) - { - _logger.LogError(ex.InnerException is null ? ex.Message : ex.InnerException?.Message); - - throw new DBSaveFailedException("SQL 查询中存在语法错误。"); - } - catch (ReferenceConstraintException ex) - { - _logger.LogError(ex.InnerException is null ? ex.Message : ex.InnerException?.Message); - throw new DBSaveFailedException("无法进行当前操作,当前数据不符合外键约束。"); - } - catch (DbUpdateConcurrencyException ex) - { - _logger.LogError(ex.InnerException is null ? ex.Message : ex.InnerException?.Message); - - throw new DBSaveFailedException("SQL 事务失败,请检查环境。"); - } - - } - - - - #region Doctor public virtual DbSet Dictionary { get; set; } public virtual DbSet Doctor { get; set; } diff --git a/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs b/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs index 3167bad6a..b1a694174 100644 --- a/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs +++ b/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs @@ -1,11 +1,15 @@ -using IRaCIS.Core.Domain.Models; +using EntityFramework.Exceptions.Common; +using IRaCIS.Core.Domain.Models; using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Infra.EFCore.Common; +using IRaCIS.Core.Infra.EFCore.Interceptor; using Microsoft.AspNetCore.Mvc.Diagnostics; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.Extensions.Logging; using System; +using System.Data; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -13,7 +17,7 @@ using System.Threading.Tasks; namespace IRaCIS.Core.Infra.EFCore; -public class AuditEntityInterceptor(IUserInfo _userInfo) : SaveChangesInterceptor +public class AuditEntityInterceptor(IUserInfo _userInfo, ILogger _logger) : SaveChangesInterceptor { /// @@ -129,4 +133,63 @@ public class AuditEntityInterceptor(IUserInfo _userInfo) : SaveChangesIntercepto } } + + private void LoggerDBContextException(DbContextErrorEventData eventData) + { + var ex = eventData.Exception; + + if (ex is UniqueConstraintException) + { + _logger.LogError("该唯一键已经存在于数据库中:" + ex.InnerException?.Message ?? ex.Message); + } + else if (ex is TimeoutException) + { + _logger.LogError("数据库操作已经超时,请稍后重试:" + ex.InnerException?.Message ?? ex.Message); + } + else if (ex is CannotInsertNullException) + { + _logger.LogError("无法在非空列上插入空值:" + ex.InnerException is null ? ex.Message : ex.InnerException?.Message); + } + else if (ex is MaxLengthExceededException) + { + _logger.LogError("字符串超过了数据库列的最大长度:" + ex.InnerException is null ? ex.Message : ex.InnerException?.Message); + } + else if (ex is NumericOverflowException) + { + _logger.LogError("数值超过了数据类型的范围:" + ex.InnerException is null ? ex.Message : ex.InnerException?.Message); + } + else if (ex is SyntaxErrorException) + { + _logger.LogError("SQL 查询中存在语法错误:" + ex.InnerException is null ? ex.Message : ex.InnerException?.Message); + } + else if (ex is ReferenceConstraintException) + { + _logger.LogError("无法进行当前操作,当前数据不符合外键约束:" + ex.InnerException is null ? ex.Message : ex.InnerException?.Message); + } + else if (ex is DbUpdateConcurrencyException) + { + _logger.LogError("并发更新异常:" + ex.InnerException is null ? ex.Message : ex.InnerException?.Message); + } + else + { + _logger.LogError("SQL 事务失败,请检查环境:" + ex.InnerException is null ? ex.Message : ex.InnerException?.Message); + } + } + + //在保存失败后处理异常并记录日志 + public override Task SaveChangesFailedAsync(DbContextErrorEventData eventData, CancellationToken cancellationToken = default) + { + + LoggerDBContextException(eventData); + + + return base.SaveChangesFailedAsync(eventData, cancellationToken); + } + + public override void SaveChangesFailed(DbContextErrorEventData eventData) + { + LoggerDBContextException(eventData); + + base.SaveChangesFailed(eventData); + } } \ No newline at end of file diff --git a/IRaCIS.Core.Infra.EFCore/Interceptor/DispatchDomainEventsInterceptor.cs b/IRaCIS.Core.Infra.EFCore/Interceptor/DispatchDomainEventsInterceptor.cs index b18186107..f89896e3c 100644 --- a/IRaCIS.Core.Infra.EFCore/Interceptor/DispatchDomainEventsInterceptor.cs +++ b/IRaCIS.Core.Infra.EFCore/Interceptor/DispatchDomainEventsInterceptor.cs @@ -8,6 +8,10 @@ using System.Threading; using System.Threading.Tasks; using IRaCIS.Core.Domain.Models; using MassTransit; +using EntityFramework.Exceptions.Common; +using IRaCIS.Core.Infrastructure; +using System.Data; +using Microsoft.Extensions.Logging; namespace IRaCIS.Core.Infra.EFCore.Interceptor { @@ -57,5 +61,6 @@ namespace IRaCIS.Core.Infra.EFCore.Interceptor await publishEndpoint.Publish(domainEvent); } } + } } diff --git a/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj b/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj index b00d3c836..512ce8727 100644 --- a/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj +++ b/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj @@ -87,4 +87,8 @@ + + + + diff --git a/IRaCIS.Core.Test/Program.cs b/IRaCIS.Core.Test/Program.cs index 533ca55fa..c9771b296 100644 --- a/IRaCIS.Core.Test/Program.cs +++ b/IRaCIS.Core.Test/Program.cs @@ -1,5 +1,6 @@  using Fluid; +using IRaCIS.Core.Infra.EFCore; using IRaCIS.Core.Test.Template; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; @@ -68,7 +69,7 @@ partial class Program // 要生成的表名数组 - var tableNames = new List { "User", "Product", "Order" }; + var tableNames = new List { "Product"/*, "Order"*/ }; try { @@ -79,7 +80,7 @@ partial class Program var fileName=Path.GetFileNameWithoutExtension(templateFilePath); //模板放入具体的文件夹 - var folder = fileName == "Entity" ? "Entity" : fileName.Replace("Entity", ""); + //var folder = fileName == "Entity" ? "Entity" : fileName.Replace("Entity", ""); foreach (var tableName in tableNames) { @@ -101,7 +102,7 @@ partial class Program //Console.WriteLine(template.Render(context)); - string outputFilePath = Path.Combine(outPutTemplateFolderPath, folder, $"{fileName.Replace("Entity", tableName)}.cs"); + string outputFilePath = Path.Combine(outPutTemplateFolderPath, /*folder,*/ $"{fileName.Replace("Entity", tableName)}.cs"); File.WriteAllText(outputFilePath, template.Render(context)); } diff --git a/IRaCIS.Core.Test/Template/YourDbContext.cs b/IRaCIS.Core.Test/YourDbContext.cs similarity index 100% rename from IRaCIS.Core.Test/Template/YourDbContext.cs rename to IRaCIS.Core.Test/YourDbContext.cs