120 lines
4.6 KiB
C#
120 lines
4.6 KiB
C#
using EntityFrameworkCore.Triggered;
|
|
using IP2Region.Net.Abstractions;
|
|
|
|
namespace IRaCIS.Core.Application.Triggers
|
|
{
|
|
public class UserLogTrigger(
|
|
ISearcher _searcher) : IBeforeSaveTrigger<UserLog>
|
|
{
|
|
|
|
|
|
//国家|区域|省份|城市|ISP 缺省的地域信息默认是0
|
|
//0|0|0|内网IP|内网IP
|
|
// 中国|0|湖北省|武汉市|电信
|
|
public async Task BeforeSave(ITriggerContext<UserLog> context, CancellationToken cancellationToken)
|
|
{
|
|
var userLog = context.Entity;
|
|
|
|
if (context.ChangeType == ChangeType.Added)
|
|
{
|
|
|
|
var ipinfo = _searcher.Search(userLog.IP);
|
|
|
|
userLog.IPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3));
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public class UserAddTrigger(IUserInfo _userInfo, IRepository<UserLog> _userLogReposiotry) : IBeforeSaveTrigger<User>
|
|
{
|
|
public async Task BeforeSave(ITriggerContext<User> context, CancellationToken cancellationToken)
|
|
{
|
|
var user = context.Entity;
|
|
|
|
if (context.ChangeType == ChangeType.Added)
|
|
{
|
|
await _userLogReposiotry.AddAsync(new UserLog() { OptType = UserOptType.AddUser, OptUserId = user.Id, LoginUserId = _userInfo.Id, IP = _userInfo.IP });
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public class UserModifyTrigger(IUserInfo _userInfo, IRepository<User> _userReposiotry) : IBeforeSaveTrigger<User>
|
|
{
|
|
public async Task BeforeSave(ITriggerContext<User> context, CancellationToken cancellationToken)
|
|
{
|
|
var user = context.Entity;
|
|
|
|
var beforeUser = context.UnmodifiedEntity;
|
|
|
|
if (context.ChangeType == ChangeType.Modified)
|
|
{
|
|
|
|
if (beforeUser.IsFirstAdd == false && user.IsFirstAdd == true)
|
|
{
|
|
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == user.EMail, u => new User() { IsFirstAdd = true });
|
|
}
|
|
|
|
//只用初始化其中一个
|
|
if (beforeUser.IsFirstAdd == true && user.IsFirstAdd == false)
|
|
{
|
|
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == user.EMail, u => new User() { IsFirstAdd = false });
|
|
}
|
|
|
|
if (beforeUser.Password != user.Password)
|
|
{
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == user.EMail, u => new User() { Password = user.Password });
|
|
}
|
|
|
|
|
|
if (beforeUser.EMail != user.EMail)
|
|
{
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == beforeUser.EMail, u => new User() { EMail = user.EMail });
|
|
}
|
|
|
|
if (beforeUser.CheckCode != user.CheckCode)
|
|
{
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == user.EMail, u => new User() { CheckCode = user.CheckCode });
|
|
}
|
|
|
|
if (beforeUser.FirstName != user.FirstName || beforeUser.LastName != user.LastName)
|
|
{
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == user.EMail, u => new User() { FirstName = user.FirstName, LastName = user.LastName });
|
|
}
|
|
|
|
if (beforeUser.Phone != user.Phone)
|
|
{
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == user.EMail, u => new User() { Phone = user.Phone });
|
|
}
|
|
|
|
if (beforeUser.Sex != user.Sex)
|
|
{
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == user.EMail, u => new User() { Sex = user.Sex });
|
|
}
|
|
|
|
if (beforeUser.IsZhiZhun != user.IsZhiZhun)
|
|
{
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == user.EMail, u => new User() { IsZhiZhun = user.IsZhiZhun, OrganizationName = user.OrganizationName });
|
|
}
|
|
|
|
if (beforeUser.OrganizationName != user.OrganizationName)
|
|
{
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == user.EMail, u => new User() { OrganizationName = user.OrganizationName });
|
|
}
|
|
|
|
if (beforeUser.DepartmentName != user.DepartmentName || beforeUser.PositionName != user.PositionName)
|
|
{
|
|
await _userReposiotry.BatchUpdateNoTrackingAsync(t => t.EMail == user.EMail, u => new User() { DepartmentName = user.DepartmentName, PositionName = user.PositionName });
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|