From 0e507245da960f69f70242a884040cd2719f5b33 Mon Sep 17 00:00:00 2001 From: hwh <332078369@qq.com> Date: 星期三, 19 六月 2024 15:50:23 +0800 Subject: [PATCH] unitofwork事务 --- Wms/Utility/UnitOfWork/SqlSugarUnitOfWork.cs | 69 ++++++++++++++++++++++++++++++++++ Wms/WMS.IBLL/IBllTaskServer/IBllTaskSyncServer.cs | 1 Wms/WMS.BLL/BllTaskServer/BllTaskSyncServer.cs | 30 ++++++++------ Wms/Utility/Utility.csproj | 1 Wms/Wms/Startup.cs | 4 ++ 5 files changed, 92 insertions(+), 13 deletions(-) diff --git a/Wms/Utility/UnitOfWork/SqlSugarUnitOfWork.cs b/Wms/Utility/UnitOfWork/SqlSugarUnitOfWork.cs new file mode 100644 index 0000000..5d795ba --- /dev/null +++ b/Wms/Utility/UnitOfWork/SqlSugarUnitOfWork.cs @@ -0,0 +1,69 @@ +锘縰sing Microsoft.AspNetCore.Mvc.Filters; +using SqlSugar; + +namespace Utility +{ + /// <summary> + /// SqlSugar 浜嬪姟鍜屽伐浣滃崟鍏� + /// </summary> + public sealed class SqlSugarUnitOfWork : IUnitOfWork + { + /// <summary> + /// SqlSugar 瀵硅薄 + /// </summary> + private readonly ISqlSugarClient _sqlSugarClient; + + /// <summary> + /// 鏋勯�犲嚱鏁� + /// </summary> + /// <param name="sqlSugarClient"></param> + public SqlSugarUnitOfWork(ISqlSugarClient sqlSugarClient) + { + _sqlSugarClient = sqlSugarClient; + } + + /// <summary> + /// 寮�鍚伐浣滃崟鍏冨鐞� + /// </summary> + /// <param name="context"></param> + /// <param name="unitOfWork"></param> + /// <exception cref="NotImplementedException"></exception> + public void BeginTransaction(FilterContext context, UnitOfWorkAttribute unitOfWork) + { + _sqlSugarClient.AsTenant().BeginTran(); + } + + /// <summary> + /// 鎻愪氦宸ヤ綔鍗曞厓澶勭悊 + /// </summary> + /// <param name="resultContext"></param> + /// <param name="unitOfWork"></param> + /// <exception cref="NotImplementedException"></exception> + public void CommitTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork) + { + _sqlSugarClient.AsTenant().CommitTran(); + } + + /// <summary> + /// 鍥炴粴宸ヤ綔鍗曞厓澶勭悊 + /// </summary> + /// <param name="resultContext"></param> + /// <param name="unitOfWork"></param> + /// <exception cref="NotImplementedException"></exception> + public void RollbackTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork) + { + _sqlSugarClient.AsTenant().RollbackTran(); + } + + /// <summary> + /// 鎵ц瀹屾瘯锛堟棤璁烘垚鍔熷け璐ワ級 + /// </summary> + /// <param name="context"></param> + /// <param name="resultContext"></param> + /// <exception cref="NotImplementedException"></exception> + public void OnCompleted(FilterContext context, FilterContext resultContext) + { + _sqlSugarClient.Dispose(); + } + } +} \ No newline at end of file diff --git a/Wms/Utility/Utility.csproj b/Wms/Utility/Utility.csproj index 49a3a6a..9d18915 100644 --- a/Wms/Utility/Utility.csproj +++ b/Wms/Utility/Utility.csproj @@ -24,6 +24,7 @@ <PackageReference Include="Serilog.Sinks.Async" Version="2.0.0" /> <PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> + <PackageReference Include="SqlSugarCore" Version="5.1.3.43" /> <PackageReference Include="System.Drawing.Common" Version="6.0.0" /> <PackageReference Include="ZXing.Net" Version="0.16.7" /> </ItemGroup> diff --git a/Wms/WMS.BLL/BllTaskServer/BllTaskSyncServer.cs b/Wms/WMS.BLL/BllTaskServer/BllTaskSyncServer.cs index 04fb3d6..1723543 100644 --- a/Wms/WMS.BLL/BllTaskServer/BllTaskSyncServer.cs +++ b/Wms/WMS.BLL/BllTaskServer/BllTaskSyncServer.cs @@ -1,5 +1,6 @@ 锘縰sing Model.InterFaceModel; using Model.ModelDto.BllTaskDto; +using Model.ModelDto.SysDto; using Model.ModelVm.BllTaskVm; using Newtonsoft.Json; using SqlSugar; @@ -7,6 +8,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; +using Utility; using Utility.Tools; using WMS.BLL.LogServer; using WMS.DAL; @@ -27,6 +30,7 @@ public BllTaskSyncServer() : base(Db) { } + /// <summary> /// 鑾峰彇浠诲姟鍒楄〃 /// </summary> @@ -57,7 +61,7 @@ StartLocat = it.StartLocat, EndLocat = it.EndLocat, PalletNo = it.PalletNo, - PalletType=it.PalletType, + PalletType = it.PalletType, FinishDate = it.FinishDate == null ? "" : Convert.ToDateTime(it.FinishDate).ToString("yyyy-MM-dd HH:mm:ss"), WCSName = it.WCSName, CreateTime = it.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"), @@ -120,7 +124,7 @@ entry.WCSName = model.WCSName; //娣诲姞浠诲姟淇℃伅 Db.Insertable(entry).ExecuteCommand(); - + if (model.Type == "0" || model.Type == "2")//鍏ュ簱浠诲姟銆佺Щ搴撲换鍔� { //鐩爣鍌ㄤ綅 @@ -167,7 +171,7 @@ BitPalletMark = "0", IsBale = "0", IsBelt = "0", - CompleteTime=model.FinishDate,//瀹屾垚鏃堕棿 + CompleteTime = model.FinishDate,//瀹屾垚鏃堕棿 CreateTime = _dateNow }; var bindId = Db.Insertable(bind).ExecuteReturnIdentity(); @@ -233,7 +237,7 @@ /// </summary> /// <param name="taskNo"></param> /// <param name="orderNo"></param> - public void HandleTaskSync(string taskNo, string orderNo,string url, int userId) + public void HandleTaskSync(string taskNo, string orderNo, string url, int userId) { try { @@ -465,8 +469,8 @@ IsBelt = bindInfo.IsBelt, Demo = bindInfo.Demo, - OwnerNo= arrivalNotice.CustomerNo,//渚涘簲鍟嗙紪鐮� - OwnerName=arrivalNotice.CustomerName,//渚涘簲鍟嗗悕绉� + OwnerNo = arrivalNotice.CustomerNo,//渚涘簲鍟嗙紪鐮� + OwnerName = arrivalNotice.CustomerName,//渚涘簲鍟嗗悕绉� IsDel = "0", CreateUser = 0, @@ -538,8 +542,8 @@ CreateUser = userId, CreateTime = (DateTime)task.FinishDate, - OwnerNo=arrivalNotice.CustomerNo, - OwnerName=arrivalNotice.CustomerName + OwnerNo = arrivalNotice.CustomerNo, + OwnerName = arrivalNotice.CustomerName }; Db.Insertable(dataStockInfo).ExecuteCommand(); } @@ -603,7 +607,7 @@ exTask.Msg = $"鍏ュ簱鍙e埌=>>{task.EndLocat}鐨勫叆搴撲换鍔�";//鍏抽敭淇℃伅 #endregion break; - + case "1"://鍑哄簱浠诲姟 #region 鍑哄簱浠诲姟澶勭悊閫昏緫 if (task.PalletType == "1")//绌烘墭鍑哄簱 @@ -785,8 +789,8 @@ exTask.OrderType = "1";//鍑哄簱鍗� exTask.Msg = $"{task.StartLocat}鐨勫嚭搴撲换鍔�";//鍏抽敭淇℃伅 break; - #endregion - + #endregion + case "2"://绉诲簱浠诲姟 #region 绉诲簱浠诲姟澶勭悊閫昏緫 //鎵樼洏搴撳瓨鏄庣粏 @@ -936,7 +940,7 @@ Db.CommitTran(); } catch (Exception e) - { + { //鍥炴粴浜嬪姟 Db.RollbackTran(); throw new Exception(e.Message); @@ -961,7 +965,7 @@ { sqlString = $"select SONo from BllExportNotice where Status in ('1','2','3') and IsDel='0' order by CreateTime;"; } - + var modelList = Db.Ado.SqlQuery<string>(sqlString); return modelList; diff --git a/Wms/WMS.IBLL/IBllTaskServer/IBllTaskSyncServer.cs b/Wms/WMS.IBLL/IBllTaskServer/IBllTaskSyncServer.cs index f1ee3cf..128abb7 100644 --- a/Wms/WMS.IBLL/IBllTaskServer/IBllTaskSyncServer.cs +++ b/Wms/WMS.IBLL/IBllTaskServer/IBllTaskSyncServer.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; using WMS.Entity.BllTaskEntity; namespace WMS.IBLL.IBllTaskServer diff --git a/Wms/Wms/Startup.cs b/Wms/Wms/Startup.cs index 9414fe7..cbab3dc 100644 --- a/Wms/Wms/Startup.cs +++ b/Wms/Wms/Startup.cs @@ -20,6 +20,7 @@ using Utility.Extension; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Http; +using SqlSugar; namespace Wms { @@ -121,6 +122,9 @@ //注册serilog services.AddConfigSerilog(); services.AddScoped<ApiResponseActionFilter>(); + services.AddScoped<UnitOfWorkAttribute>(); + services.AddSingleton<ISqlSugarClient>(DataContext.Db); // 单例注册 + services.AddTransient<IUnitOfWork, SqlSugarUnitOfWork>(); // 事务与工作单元注册 } public void ConfigureContainer(ContainerBuilder builder) { -- Gitblit v1.8.0