From 9de6a3c8135faead8023b21888a75ba7a6c68e65 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Fri, 29 Aug 2025 14:52:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.API/_ServiceExtensions/EFSetup.cs | 2 + .../Management/DTO/UserLogViewModel.cs | 35 -------- .../AfterSaveTrigger/UserLogAfterTrigger.cs | 84 +++++++++++++++++++ IRaCIS.Core.Domain/Management/UserLog.cs | 46 ++++++++++ 4 files changed, 132 insertions(+), 35 deletions(-) create mode 100644 IRaCIS.Core.Application/Triggers/AfterSaveTrigger/UserLogAfterTrigger.cs diff --git a/IRaCIS.Core.API/_ServiceExtensions/EFSetup.cs b/IRaCIS.Core.API/_ServiceExtensions/EFSetup.cs index acbdea6db..f21ef5404 100644 --- a/IRaCIS.Core.API/_ServiceExtensions/EFSetup.cs +++ b/IRaCIS.Core.API/_ServiceExtensions/EFSetup.cs @@ -84,6 +84,8 @@ namespace IRaCIS.Core.API triggerOptions.AddTrigger(); + triggerOptions.AddTrigger(); + diff --git a/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs b/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs index 7f6742ce3..7b46ab67b 100644 --- a/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs +++ b/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs @@ -38,41 +38,6 @@ namespace IRaCIS.Core.Application.ViewModel public UserLogJsonObj UserObj => JsonObj.IsNotNullOrEmpty() ? JsonConvert.DeserializeObject(JsonObj) : new UserLogJsonObj(); } - public class UserLogJsonObj - { - public string FullName => LastName + " / " + FirstName; - - public string UserCode { get; set; } - public string UserName { get; set; } - public string EMail { get; set; } - - public string FirstName { get; set; } - - public string LastName { get; set; } - - public string Phone { get; set; } - - public int? Sex { get; set; } - - public UserStateEnum Status { get; set; } = UserStateEnum.Enable; - - public string OrganizationName { get; set; } - public string PositionName { get; set; } - - public string DepartmentName { get; set; } - - public List UserRoleList { get; set; } - - } - - public class UserRoleLogObj - { - public string UserTypeShortName { get; set; } - - public UserTypeEnum UserTypeEnum { get; set; } - - public bool IsUserRoleDisabled { get; set; } - } public class SetIsIgnoreUncommonlyInDto diff --git a/IRaCIS.Core.Application/Triggers/AfterSaveTrigger/UserLogAfterTrigger.cs b/IRaCIS.Core.Application/Triggers/AfterSaveTrigger/UserLogAfterTrigger.cs new file mode 100644 index 000000000..cb4c62261 --- /dev/null +++ b/IRaCIS.Core.Application/Triggers/AfterSaveTrigger/UserLogAfterTrigger.cs @@ -0,0 +1,84 @@ +using AutoMapper.QueryableExtensions; +using EntityFrameworkCore.Triggered; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Application.Triggers.AfterSaveTrigger +{ + /// + /// 账户日志 记录账户每次操作的信息 + /// + /// + public class UserLogAfterTrigger(IRepository _identityUserRepository) : IAfterSaveTrigger + { + public async Task AfterSave(ITriggerContext context, CancellationToken cancellationToken) + { + var userlog = context.Entity; + + if (context.ChangeType == ChangeType.Added) + { + + if (userlog.TargetIdentityUserId != null) + { + + //判断是否需要记录详情 + + if (userlog.OptType == UserOptType.AddUser || userlog.OptType == UserOptType.UpdateUser || userlog.OptType == UserOptType.UpdateUserRole + || userlog.OptType == UserOptType.AccountEnable || userlog.OptType == UserOptType.AccountLocked) + { + var obj = await _identityUserRepository.Where(t => t.Id == userlog.TargetIdentityUserId).Select(t => new UserLogJsonObj() + { + DepartmentName = t.DepartmentName, + EMail = t.EMail, + FirstName = t.FirstName, + LastName = t.LastName, + OrganizationName = t.OrganizationName, + Phone = t.Phone, + PositionName = t.PositionName, + Sex = t.Sex, + Status = t.Status, + UserCode = t.UserCode, + UserName = t.UserName, + UserRoleList = t.UserRoleList.Select(t => new UserRoleLogObj() + { + IsUserRoleDisabled = t.IsUserRoleDisabled, + UserTypeEnum = t.UserTypeEnum, + UserTypeShortName = t.UserTypeRole.UserTypeShortName + }).ToList(), + + HospitalGroupList = t.IdentityUserHospitalGroupList.Select(t => new HospitalGroupLogObj() + { + HospitalGroupId = t.HospitalGroupId, + Code = t.HospitalGroup.Code, + Name = t.HospitalGroup.Name, + }).ToList(), + + }).FirstOrDefaultAsync(); + + + + userlog.JsonObj = obj.ToJsonStr(); + } + + + + } + + if (userlog.ActionIdentityUserId != null && userlog.ActionUserName.IsNullOrWhiteSpace()) + { + userlog.ActionUserName = await _identityUserRepository.Where(t => t.Id == userlog.ActionIdentityUserId).Select(t => t.UserName).FirstOrDefaultAsync(); + } + + await _identityUserRepository.SaveChangesAsync(); + + + } + } + } + + + +} diff --git a/IRaCIS.Core.Domain/Management/UserLog.cs b/IRaCIS.Core.Domain/Management/UserLog.cs index 1cb847702..0b56839b6 100644 --- a/IRaCIS.Core.Domain/Management/UserLog.cs +++ b/IRaCIS.Core.Domain/Management/UserLog.cs @@ -64,3 +64,49 @@ public class UserLog : BaseAddAuditEntity } +public class UserLogJsonObj +{ + public string FullName => LastName + " / " + FirstName; + + public string UserCode { get; set; } + public string UserName { get; set; } + public string EMail { get; set; } + + public string FirstName { get; set; } + + public string LastName { get; set; } + + public string Phone { get; set; } + + public int? Sex { get; set; } + + public UserStateEnum Status { get; set; } = UserStateEnum.Enable; + + public string OrganizationName { get; set; } + public string PositionName { get; set; } + + public string DepartmentName { get; set; } + + public List UserRoleList { get; set; } + + public List HospitalGroupList { get; set; } + +} + + +public class UserRoleLogObj +{ + public string UserTypeShortName { get; set; } + + public UserTypeEnum UserTypeEnum { get; set; } + + public bool IsUserRoleDisabled { get; set; } +} + +public class HospitalGroupLogObj +{ + public Guid HospitalGroupId { get; set; } + public string Name { get; set; } + + public string Code { get; set; } +} \ No newline at end of file