Forráskód Böngészése

Merge branch 'feature/snapshot' into dev

qinchaoyue 4 hónapja
szülő
commit
1ae2d94780

+ 19 - 1
src/Hotline.Application.Tests/Application/SnapshotApplicationTest.cs

@@ -43,8 +43,9 @@ public class SnapshotApplicationTest : TestBase
     private readonly IGuiderSystemService _guiderSystemService;
     private readonly ISystemSettingCacheManager _systemSettingCacheManager;
     private readonly ICommunityInfoRepository _communityInfoRepository;
+    private readonly IIndustryLogRepository _industryLogRepository;
 
-    public SnapshotApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, ISnapshotApplication snapshotApplication, IIdentityAppService identityAppService, IRepository<RedPackRecord> redPackRecordRepository, IIndustryApplication industryApplication, IIndustryRepository industryRepository, IFileRepository fileRepository, OrderServiceMock orderServiceMock, IOrderRepository orderRepository, IOrderSnapshotRepository orderSnapshotRepository, IThirdIdentiyService thirdService, IThirdAccountRepository thirdAccount, ISessionContext sessionContext, IGuiderSystemService guiderSystemService, ISystemSettingCacheManager systemSettingCacheManager, ICommunityInfoRepository communityInfoRepository) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdService, thirdAccount)
+    public SnapshotApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, ISnapshotApplication snapshotApplication, IIdentityAppService identityAppService, IRepository<RedPackRecord> redPackRecordRepository, IIndustryApplication industryApplication, IIndustryRepository industryRepository, IFileRepository fileRepository, OrderServiceMock orderServiceMock, IOrderRepository orderRepository, IOrderSnapshotRepository orderSnapshotRepository, IThirdIdentiyService thirdService, IThirdAccountRepository thirdAccount, ISessionContext sessionContext, IGuiderSystemService guiderSystemService, ISystemSettingCacheManager systemSettingCacheManager, ICommunityInfoRepository communityInfoRepository, IIndustryLogRepository industryLogRepository) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdService, thirdAccount)
     {
         _snapshotApplication = snapshotApplication;
         _identityAppService = identityAppService;
@@ -60,6 +61,7 @@ public class SnapshotApplicationTest : TestBase
         _guiderSystemService = guiderSystemService;
         _systemSettingCacheManager = systemSettingCacheManager;
         _communityInfoRepository = communityInfoRepository;
+        _industryLogRepository = industryLogRepository;
     }
 
     [Fact]
@@ -285,6 +287,9 @@ public class SnapshotApplicationTest : TestBase
     {
         await _snapshotApplication.AddVolunteerAsync(new AddVolunteerInDto { Name = _sessionContext.UserName, PhoneNumber = _sessionContext.Phone }, CancellationToken.None);
         var inDto = _fixture.Create<AddVolunteerReportInDto>();
+        inDto.JobType = "电焊";
+        inDto.PhoneNumber = "13999989" + DateTime.Now.ToString("ss");
+        inDto.Name = "单元测试" + DateTime.Now.ToString("ss");
         foreach (var item in inDto.Files)
         {
             item.FileName = DateTime.Now.ToShortTimeString() + "文件.doc";
@@ -435,6 +440,19 @@ public class SnapshotApplicationTest : TestBase
         var order = _orderServiceMock.CreateSnapshotOrder()
             .GetCreateResult();
         await _snapshotApplication.PostOrderGuiderSystemAsync(order.Id, CancellationToken.None);
+        await _orderSnapshotRepository.Updateable()
+            .SetColumns(m => m.IndustryName, "修改行业名称")
+            .Where(m => m.Id == order.Id)
+            .ExecuteCommandAsync();
+        var industryLog = await _industryLogRepository.Queryable()
+            .Where(m => m.OrderId == order.Id)
+            .FirstAsync();
+        industryLog.ShouldNotBeNull();
+        industryLog.IndustryName.ShouldBe("修改行业名称");
+        await _orderSnapshotRepository.Updateable()
+            .SetColumns(m => m.IndustryName, industryLog.OldIndustryName)
+            .Where(m => m.Id == order.Id)
+            .ExecuteCommandAsync();
         var orderSnapshot = await _orderSnapshotRepository.GetAsync(order.Id);
         var replyDto = new GuiderSystemInDto 
         {

+ 18 - 0
src/Hotline.Repository.SqlSugar/Snapshot/IndustryLogRepository.cs

@@ -0,0 +1,18 @@
+using Hotline.Repository.SqlSugar.DataPermissions;
+using Hotline.Snapshot;
+using Hotline.Snapshot.Interfaces;
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Dependency;
+
+namespace Hotline.Repository.SqlSugar.Snapshot;
+public class IndustryLogRepository : BaseRepository<IndustryLog>, IIndustryLogRepository,  IScopeDependency
+{
+    public IndustryLogRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder) : base(uow, dataPermissionFilterBuilder)
+    {
+    }
+}

+ 45 - 0
src/Hotline/Orders/DatabaseEventHandler/OrderSnapshotEventHandler.cs

@@ -0,0 +1,45 @@
+using Hotline.Snapshot;
+using Hotline.Snapshot.Interfaces;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Repository.Events;
+
+namespace Hotline.Orders.DatabaseEventHandler;
+public class OrderSnapshotEventHandler : IUpdateDatabaseEvent<OrderSnapshot>
+{
+    private readonly IIndustryLogRepository _industryLogRepository;
+
+    public OrderSnapshotEventHandler(IIndustryLogRepository industryLogRepository)
+    {
+        _industryLogRepository = industryLogRepository;
+    }
+
+    public void OnInserted(OrderSnapshot entity)
+    {
+    }
+
+    public void OnUpdated(OrderSnapshot entity)
+    {
+    }
+
+    public void OnUpdating(object oldValue, string propertyName, OrderSnapshot entity)
+    {
+        if (oldValue == null) return;
+        if (propertyName != "IndustryName") return;
+        if (oldValue is String industryName)
+        {
+            if (industryName == entity.IndustryName) return;
+            var entityLog = new IndustryLog
+            {
+                IndustryName = oldValue.ToString(),
+                OldIndustryName = industryName,
+                CreationTime = DateTime.Now,
+                OrderId = entity.Id,
+            };
+            _industryLogRepository.AddAsync(entityLog).GetAwaiter().GetResult();
+        }
+    }
+}

+ 7 - 2
src/Hotline/Orders/DatabaseEventHandler/OrderVisitDetailEventHandler.cs

@@ -25,16 +25,21 @@ public class OrderVisitDetailEventHandler : IUpdateDatabaseEvent<OrderVisitDetai
         _systemLogRepository = systemLogRepository;
     }
 
-    public void OnInsert(OrderVisitDetail entity)
+    public void OnInserted(OrderVisitDetail entity)
     {
         OrderUpdate(entity);
     }
 
-    public void OnUpdate(OrderVisitDetail entity)
+    public void OnUpdated(OrderVisitDetail entity)
     {
         OrderUpdate(entity);
     }
 
+    public void OnUpdating(object oldValue, string propertyName, OrderVisitDetail entity)
+    {
+
+    }
+
     private void OrderUpdate(OrderVisitDetail visit)
     {
         var name = "回填Order回访字段";

+ 22 - 0
src/Hotline/Snapshot/IndustryLog.cs

@@ -0,0 +1,22 @@
+using System.ComponentModel;
+using XF.Domain.Repository;
+
+namespace Hotline.Snapshot;
+[Description("行业修改日志")]
+public class IndustryLog : FullStateEntity
+{
+    /// <summary>
+    /// OrderId
+    /// </summary>
+    public string OrderId { get; set; }
+
+    /// <summary>
+    /// 行业名称
+    /// </summary>
+    public string OldIndustryName { get; set; }
+
+    /// <summary>
+    /// 行业名称
+    /// </summary>
+    public string IndustryName { get; set; }
+}

+ 11 - 0
src/Hotline/Snapshot/Interfaces/IndustryLogRepository.cs

@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Repository;
+
+namespace Hotline.Snapshot.Interfaces;
+public interface IIndustryLogRepository : IRepository<IndustryLog>
+{
+}

+ 19 - 2
src/XF.Domain.Repository/Events/DatabaseEventDispatcher.cs

@@ -25,10 +25,27 @@ public class DatabaseEventDispatcher
         switch (operationType)
         {
             case DataFilterType.UpdateByObject:
-                targetInterface?.GetMethod("OnUpdate")?.Invoke(handler,[entity]);
+                targetInterface?.GetMethod("OnUpdated")?.Invoke(handler,[entity]);
                 break;
             case DataFilterType.InsertByObject:
-                targetInterface?.GetMethod("OnInsert")?.Invoke(handler, [entity]);
+                targetInterface?.GetMethod("OnInserted")?.Invoke(handler, [entity]);
+                break;
+        }
+    }
+
+    public void Dispatch<TEntity>(object oldValue, string propertyName, TEntity entity, DataFilterType operationType) where TEntity : new()
+    {
+        var targetInterface = typeof(IUpdateDatabaseEvent<>).MakeGenericType(entity.GetType());
+        var handler = _serviceProvider.GetService(targetInterface);
+        if (handler == null) return;
+
+        switch (operationType)
+        {
+            case DataFilterType.UpdateByObject:
+                targetInterface?.GetMethod("OnUpdating")?.Invoke(handler, [oldValue, propertyName, entity]);
+                break;
+            case DataFilterType.InsertByObject:
+                targetInterface?.GetMethod("OnInserted")?.Invoke(handler, [oldValue, propertyName, entity]);
                 break;
         }
     }

+ 25 - 2
src/XF.Domain.Repository/Events/IUpdateDatabaseEvent.cs

@@ -8,6 +8,29 @@ namespace XF.Domain.Repository.Events;
 
 public interface IUpdateDatabaseEvent<TEntity> where TEntity : new()
 {
-    void OnUpdate(TEntity entity);
-    void OnInsert(TEntity entity);
+    /// <summary>
+    /// 数据库更新完成后
+    /// </summary>
+    /// <param name="entity"></param>
+    void OnUpdated(TEntity entity);
+
+    /// <summary>
+    /// 数据库插入完成后
+    /// </summary>
+    /// <param name="entity"></param>
+    void OnInserted(TEntity entity);
+
+    /// <summary>
+    /// 数据库更新之前
+    /// </summary>
+    /// <param name="oldValue">旧的值</param>
+    /// <param name="propertyName">被更新的字段名称</param>
+    /// <param name="entity"></param>
+    void OnUpdating(object oldValue, string propertyName, TEntity entity);
+
+    ///// <summary>
+    ///// 数据库插入之前
+    ///// </summary>
+    ///// <param name="entity"></param>
+    //void OnInserting(TEntity entity);
 }