qinchaoyue 4 mesiacov pred
rodič
commit
b04e13ff76

+ 2 - 0
src/Hotline.Api/StartupExtensions.cs

@@ -45,6 +45,7 @@ using Hotline.Application.Snapshot;
 using Hotline.Orders;
 using XF.Domain.Repository.Events;
 using Hotline.Orders.DatabaseEventHandler;
+using Hotline.Snapshot;
 
 
 namespace Hotline.Api;
@@ -197,6 +198,7 @@ internal static class StartupExtensions
 
         //services.AddScoped(typeof(IUpdateDatabase<>), typeof(UpdateDatabase<>));
         services.AddScoped<IUpdateDatabaseEvent<OrderVisitDetail>, OrderVisitDetailEventHandler>();
+        // services.AddScoped<IUpdateDatabaseEvent<OrderSnapshot>, OrderSnapshotEventHandler>();
 
         //sqlsugar
         services.AddSqlSugar(configuration);

+ 4 - 5
src/Hotline.Application.Tests/Application/SnapshotApplicationTest.cs

@@ -440,10 +440,9 @@ 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 orderSnapshot = await _orderSnapshotRepository.GetAsync(order.Id);
+        orderSnapshot.IndustryName = "修改行业名称";
+        await _orderSnapshotRepository.UpdateAsync(orderSnapshot);
         var industryLog = await _industryLogRepository.Queryable()
             .Where(m => m.OrderId == order.Id)
             .FirstAsync();
@@ -453,7 +452,7 @@ public class SnapshotApplicationTest : TestBase
             .SetColumns(m => m.IndustryName, industryLog.OldIndustryName)
             .Where(m => m.Id == order.Id)
             .ExecuteCommandAsync();
-        var orderSnapshot = await _orderSnapshotRepository.GetAsync(order.Id);
+        orderSnapshot = await _orderSnapshotRepository.GetAsync(order.Id);
         var replyDto = new GuiderSystemInDto 
         {
             ReplyCode = order.No,

+ 3 - 0
src/Hotline.Repository.SqlSugar/Extensions/SqlSugarStartupExtensions.cs

@@ -285,6 +285,9 @@ namespace Hotline.Repository.SqlSugar.Extensions
 
             db.Aop.DataExecuting = (oldValue, entityInfo) =>
             {
+                services.BuildServiceProvider()
+                   .GetService<DatabaseEventDispatcher>()?
+                   .Dispatch(oldValue, entityInfo.PropertyName, entityInfo.EntityValue, entityInfo.OperationType);
                 //inset生效
                 if (entityInfo.PropertyName == "CreationTime" && entityInfo.OperationType == DataFilterType.InsertByObject)
                 {

+ 15 - 8
src/Hotline/Orders/DatabaseEventHandler/OrderSnapshotEventHandler.cs

@@ -5,16 +5,19 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using XF.Domain.Dependency;
 using XF.Domain.Repository.Events;
 
 namespace Hotline.Orders.DatabaseEventHandler;
-public class OrderSnapshotEventHandler : IUpdateDatabaseEvent<OrderSnapshot>
+public class OrderSnapshotEventHandler : IUpdateDatabaseEvent<OrderSnapshot>, IScopeDependency
 {
     private readonly IIndustryLogRepository _industryLogRepository;
+    private readonly IOrderSnapshotRepository _orderSnapshotRepository;
 
-    public OrderSnapshotEventHandler(IIndustryLogRepository industryLogRepository)
+    public OrderSnapshotEventHandler(IIndustryLogRepository industryLogRepository, IOrderSnapshotRepository orderSnapshotRepository)
     {
         _industryLogRepository = industryLogRepository;
+        _orderSnapshotRepository = orderSnapshotRepository;
     }
 
     public void OnInserted(OrderSnapshot entity)
@@ -25,17 +28,21 @@ public class OrderSnapshotEventHandler : IUpdateDatabaseEvent<OrderSnapshot>
     {
     }
 
-    public void OnUpdating(object oldValue, string propertyName, OrderSnapshot entity)
+    public void OnUpdating(object propertyValue, string propertyName, OrderSnapshot entity)
     {
-        if (oldValue == null) return;
+        if (propertyValue == null) return;
         if (propertyName != "IndustryName") return;
-        if (oldValue is String industryName)
+        if (propertyValue is String industryName)
         {
-            if (industryName == entity.IndustryName) return;
+            var industry = _orderSnapshotRepository.Queryable()
+                .Where(m => m.Id == entity.Id)
+                .Select(m => m.IndustryName)
+                .First();
+            if (industryName == industry) return;
             var entityLog = new IndustryLog
             {
-                IndustryName = oldValue.ToString(),
-                OldIndustryName = industryName,
+                IndustryName = propertyValue.ToString(),
+                OldIndustryName = industry,
                 CreationTime = DateTime.Now,
                 OrderId = entity.Id,
             };

+ 3 - 3
src/XF.Domain.Repository/Events/DatabaseEventDispatcher.cs

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

+ 4 - 3
src/XF.Domain.Repository/Events/IUpdateDatabaseEvent.cs

@@ -22,11 +22,12 @@ public interface IUpdateDatabaseEvent<TEntity> where TEntity : new()
 
     /// <summary>
     /// 数据库更新之前
+    /// 实体中的每个字段都会触发一次
     /// </summary>
-    /// <param name="oldValue">旧的值</param>
-    /// <param name="propertyName">被更新的字段名称</param>
+    /// <param name="propertyValue">属性值</param>
+    /// <param name="propertyName">属性名称</param>
     /// <param name="entity"></param>
-    void OnUpdating(object oldValue, string propertyName, TEntity entity);
+    void OnUpdating(object propertyValue, string propertyName, TEntity entity);
 
     ///// <summary>
     ///// 数据库插入之前