Browse Source

事务bug修复

Murphy 2 years ago
parent
commit
be4759f25a

+ 181 - 17
MicroServices/Business/Business.Application/ResourceExamineManagement/ResourceExamineAppService.cs

@@ -44,6 +44,7 @@ using System.Transactions;
 using Spire.Pdf.General.Render.Decode.Jpeg2000.j2k.codestream;
 using Volo.Abp.ObjectMapping;
 using SixLabors.ImageSharp;
+using static System.Formats.Asn1.AsnWriter;
 
 namespace Business.ResourceExamineManagement
 {
@@ -343,6 +344,7 @@ namespace Business.ResourceExamineManagement
         /// 节假日
         /// </summary>
         public List<HolidayMaster> holidays = new List<HolidayMaster>();
+        private readonly IUnitOfWorkManager _unitOfWorkManager;
         #endregion
 
         #region 构造函数
@@ -430,7 +432,8 @@ namespace Business.ResourceExamineManagement
             ProductExamineAppService productExamineAppService,
             ISqlRepository<rf_serialnumber> rf_serialnumber,
             ISqlRepository<LocationDetail> locationDetail,
-            ISqlRepository<ItemPackMaster> itemPackMaster
+            ISqlRepository<ItemPackMaster> itemPackMaster,
+            IUnitOfWorkManager unitOfWorkManager
             )
         {
             _mes_technique = mes_technique;
@@ -509,6 +512,7 @@ namespace Business.ResourceExamineManagement
             _productExamineAppService = productExamineAppService;
             _locationDetail = locationDetail;
             _itemPackMaster = itemPackMaster;
+            _unitOfWorkManager = unitOfWorkManager;
         }
         #endregion
 
@@ -684,8 +688,7 @@ namespace Business.ResourceExamineManagement
                 //同步工单
                 CreateWorkOrdDates(moderlist, allRoutings, workOrds, workOrdRoutings, workOrdDetails);
             }
-
-            using (TransactionScope scope = new TransactionScope())
+            using (var unitOfWork = _unitOfWorkManager.Begin(false,true))
             {
                 try
                 {
@@ -743,15 +746,83 @@ namespace Business.ResourceExamineManagement
                         var poOccupies = ObjectMapper.Map<List<mo_srm_po_occupy>, List<srm_po_occupy>>(_CalcBomViewAppService.srm_Po_OccupiesInsert);
                         _businessDbContext.BulkInsert(poOccupies);
                     }
-                    scope.Complete();
+                    await unitOfWork.CompleteAsync();
                 }
                 catch (Exception e)
                 {
-                    scope.Dispose();
+                    unitOfWork.Dispose();
                     new NLogHelper("ResourceExamineAppService").WriteLog("ReceiveResult", "资源检查工单相关数据更新失败:" + e.Message, _currentTenant.Id.ToString());
                     return e.Message;
-                }
+                };
             }
+
+            //using (TransactionScope scope = new TransactionScope())
+            //{
+            //    try
+            //    {
+            //        await _mysql_crm_seorderentry.UpdateManyAsync(sentrys);
+            //        //批量保存 后期考虑子工单
+            //        if (moderlist.Any())
+            //        {
+            //            await _mes_morder.InsertMany(_CalcBomViewAppService.mordersInsertList);
+            //            _businessDbContext.BulkInsert(moderlist);
+            //            if (workOrds.Any())
+            //            {
+            //                _workOrdMaster.Insert(workOrds);
+            //            }
+            //            if (workOrdRoutings.Any())
+            //            {
+            //                _workOrdRouting.Insert(workOrdRoutings);
+            //            }
+            //            if (workOrdDetails.Any())
+            //            {
+            //                _workOrdDetail.Insert(workOrdDetails);
+            //            }
+            //        }
+            //        if (_CalcBomViewAppService.mooccupyAllInsertList.Any())
+            //        {
+            //            _CalcBomViewAppService.mooccupyAllInsertList.ForEach(s => { s.tenant_id = param.tenantId; s.factory_id = param.factoryId; });
+            //            await _mes_mooccupy.InsertMany(_CalcBomViewAppService.mooccupyAllInsertList);
+            //            var moOccupy = ObjectMapper.Map<List<mo_mes_mooccupy>, List<mes_mooccupy>>(_CalcBomViewAppService.mooccupyAllInsertList);
+            //            _businessDbContext.BulkInsert(moOccupy);
+            //            rtn.mooccupyAllList = _CalcBomViewAppService.mooccupyAllInsertList;
+            //        }
+            //        if (_CalcBomViewAppService.moentriesInsertList.Any())
+            //        {
+            //            _CalcBomViewAppService.moentriesInsertList.ForEach(s => { s.tenant_id = param.tenantId; s.factory_id = param.factoryId; });
+            //            await _mes_moentry.InsertMany(_CalcBomViewAppService.moentriesInsertList);
+            //            var mesmoentrys = ObjectMapper.Map<List<mo_mes_moentry>, List<mes_moentry>>(_CalcBomViewAppService.moentriesInsertList);
+            //            _businessDbContext.BulkInsert(mesmoentrys);
+            //        }
+            //        if (_CalcBomViewAppService.orderList.Any())
+            //        {
+            //            _CalcBomViewAppService.orderList.ForEach(s => { s.tenant_id = param.tenantId; s.factory_id = param.factoryId; });
+            //            await _mes_oorder.InsertMany(_CalcBomViewAppService.orderList);
+            //            var ooders = ObjectMapper.Map<List<mo_mes_oorder>, List<mes_oorder>>(_CalcBomViewAppService.orderList);
+            //            _businessDbContext.BulkInsert(ooders);
+            //            rtn.order_list = _CalcBomViewAppService.orderList;
+            //        }
+            //        if (sklist.Any())
+            //        {
+            //            sklist.ForEach(s => { s.id = help.NextId(); s.tenant_id = param.tenantId; s.factory_id = param.factoryId; });
+            //            await _ic_item_stockoccupy.InsertMany(sklist);
+            //        }
+            //        if (_CalcBomViewAppService.srm_Po_OccupiesInsert.Any())
+            //        {
+            //            _CalcBomViewAppService.srm_Po_OccupiesInsert.ForEach(s => { s.tenant_id = param.tenantId; s.factory_id = param.factoryId; });
+            //            await _srm_po_occupy.InsertMany(_CalcBomViewAppService.srm_Po_OccupiesInsert);
+            //            var poOccupies = ObjectMapper.Map<List<mo_srm_po_occupy>, List<srm_po_occupy>>(_CalcBomViewAppService.srm_Po_OccupiesInsert);
+            //            _businessDbContext.BulkInsert(poOccupies);
+            //        }
+            //        scope.Complete();
+            //    }
+            //    catch (Exception e)
+            //    {
+            //        scope.Dispose();
+            //        new NLogHelper("ResourceExamineAppService").WriteLog("ReceiveResult", "资源检查工单相关数据更新失败:" + e.Message, _currentTenant.Id.ToString());
+            //        return e.Message;
+            //    }
+            //}
             List<mo_srm_pr_main> prmainlist = new List<mo_srm_pr_main>();
             if (_CalcBomViewAppService.SRMPRDtoList.Any())
             {
@@ -776,7 +847,7 @@ namespace Business.ResourceExamineManagement
                 PoActionListDto poaction = new PoActionListDto();
 
                 AutoCreatePOFromPR(prmainlist, bangid, icitemlist, itemsupplierList, poaction);
-                using (TransactionScope scope = new TransactionScope())
+                using (var unitOfWork = _unitOfWorkManager.Begin(false,true))
                 {
                     try
                     {
@@ -804,7 +875,7 @@ namespace Business.ResourceExamineManagement
                             _businessDbContext.BulkInsert(poOccupies);
                         }
 
-                        if(poaction.poMasterList.Any())
+                        if (poaction.poMasterList.Any())
                         {
                             _purOrdMaster.Insert(poaction.poMasterList);
                             //快开平台用自增列RecId关联,所以需要插入后再查给明细表赋相应的值
@@ -816,15 +887,64 @@ namespace Business.ResourceExamineManagement
                             });
                             _purOrdDetail.Insert(poaction.poDetailList);
                         }
-                        scope.Complete();
+                        await unitOfWork.CompleteAsync();
                     }
                     catch (Exception e)
                     {
-                        scope.Dispose();
+                        unitOfWork.Dispose();
                         new NLogHelper("ResourceExamineAppService").WriteLog("ReceiveResult", "资源检查PR/PO相关数据更新失败:" + e.Message, _currentTenant.Id.ToString());
                         return e.Message;
                     }
                 }
+                //using (TransactionScope scope = new TransactionScope())
+                //{
+                //    try
+                //    {
+                //        await _srm_pr_main.InsertMany(prmainlist);
+                //        _businessDbContext.BulkInsert(pr_mainlist);
+                //        if (poaction.mopoMain.Any())
+                //        {
+                //            await _srm_po_main.InsertMany(poaction.mopoMain);
+                //        }
+                //        if (poaction.mopolist.Any())
+                //        {
+                //            await _srm_po_list.InsertMany(poaction.mopolist);
+                //        }
+                //        if (poaction.poMain.Any())
+                //        {
+                //            _businessDbContext.BulkInsert(poaction.poMain);
+                //        }
+                //        if (poaction.polist.Any())
+                //        {
+                //            _businessDbContext.BulkInsert(poaction.polist);
+                //        }
+                //        if (poaction.poOccupiesList.Any())
+                //        {
+                //            var poOccupies = ObjectMapper.Map<List<mo_srm_po_occupy>, List<srm_po_occupy>>(poaction.poOccupiesList);
+                //            _businessDbContext.BulkInsert(poOccupies);
+                //        }
+
+                //        if(poaction.poMasterList.Any())
+                //        {
+                //            _purOrdMaster.Insert(poaction.poMasterList);
+                //            //快开平台用自增列RecId关联,所以需要插入后再查给明细表赋相应的值
+                //            List<string> nbrs = poaction.poMasterList.Select(a => a.PurOrd).ToList();
+                //            var nbrList = _purOrdMaster.Select(a => a.Domain == "1001" && nbrs.Contains(a.PurOrd));
+                //            poaction.poDetailList.ForEach(c =>
+                //            {
+                //                c.PurOrdRecID = nbrList.Where(a => a.PurOrd == c.PurOrd).First().RecID;
+                //            });
+                //            _purOrdDetail.Insert(poaction.poDetailList);
+                //        }
+                //        scope.Complete();
+                //    }
+                //    catch (Exception e)
+                //    {
+                //        scope.Dispose();
+                //        new NLogHelper("ResourceExamineAppService").WriteLog("ReceiveResult", "资源检查PR/PO相关数据更新失败:" + e.Message, _currentTenant.Id.ToString());
+                //        return e.Message;
+                //    }
+                //}
             }
 
             rtn.examines = examines;
@@ -923,7 +1043,7 @@ namespace Business.ResourceExamineManagement
                     }
                 });
             }
-            using (TransactionScope scope = new TransactionScope())
+            using (var unitOfWork = _unitOfWorkManager.Begin(false,true))
             {
                 try
                 {
@@ -955,15 +1075,57 @@ namespace Business.ResourceExamineManagement
                     {
                         _businessBangDbContext.BulkInsert(purchaseoccupyList);
                     }
-                    scope.Complete();
+                   await unitOfWork.CompleteAsync();
                 }
                 catch (Exception e)
                 {
                     new NLogHelper("ResourceExamineAppService").WriteLog("ReceiveResult", "资源检查计算结果相关数据更新失败:" + e.Message, _currentTenant.Id.ToString());
-                    scope.Dispose();
+                    unitOfWork.Dispose();
                     return e.Message;
                 }
             }
+        
+            //using (TransactionScope scope = new TransactionScope())
+            //{
+            //    try
+            //    {
+            //        if (examineList.Any())
+            //        {
+            //            _businessBangDbContext.BulkInsert(examineList);
+            //        }
+            //        if (bomExamineList.Any())
+            //        {
+            //            _businessBangDbContext.BulkInsert(bomExamineList.OrderBy(s => s.num_order).ToList());
+            //        }
+            //        if (mooccupyList.Any())
+            //        {
+            //            _businessBangDbContext.BulkInsert(mooccupyList);
+            //        }
+            //        if (moorderList.Any())
+            //        {
+            //            _businessBangDbContext.BulkInsert(moorderList);
+            //        }
+            //        if (ooderList.Any())
+            //        {
+            //            _businessBangDbContext.BulkInsert(ooderList);
+            //        }
+            //        if (purchaseList.Any())
+            //        {
+            //            _businessBangDbContext.BulkInsert(purchaseList);
+            //        }
+            //        if (purchaseoccupyList.Any())
+            //        {
+            //            _businessBangDbContext.BulkInsert(purchaseoccupyList);
+            //        }
+            //        scope.Complete();
+            //    }
+            //    catch (Exception e)
+            //    {
+            //        new NLogHelper("ResourceExamineAppService").WriteLog("ReceiveResult", "资源检查计算结果相关数据更新失败:" + e.Message, _currentTenant.Id.ToString());
+            //        scope.Dispose();
+            //        return e.Message;
+            //    }
+            //}
 
             //清空快照数据
             await ClearSnapShot(bangid);
@@ -1571,9 +1733,10 @@ namespace Business.ResourceExamineManagement
                     UpdateTime = a.update_time,
                     Line = a.polist_row,
                     ItemNum = a.ItemNum,
-                    QtyOrded = a.qty,
-                    QtyReceived = a.rqty,
+                    QtyOrded = a.qty.GetValueOrDefault(),
+                    QtyReceived = a.rqty.GetValueOrDefault(),
                     PurCost = a.price,
+                    StdCost = a.price / (1 + a.rate),
                     TaxRate = a.rate,
                     DueDate = a.rarrdate
                 });
@@ -1860,9 +2023,10 @@ namespace Business.ResourceExamineManagement
                     UpdateTime = a.update_time,
                     Line = a.polist_row,
                     ItemNum = a.ItemNum,
-                    QtyOrded = a.qty,
-                    QtyReceived = a.rqty,
+                    QtyOrded = a.qty.GetValueOrDefault(),
+                    QtyReceived = a.rqty.GetValueOrDefault(),
                     PurCost = a.price,
+                    StdCost= a.price/(1+ a.rate),
                     TaxRate = a.rate,
                     DueDate = a.rarrdate
                 });

+ 1 - 1
MicroServices/Business/Business.Host/appsettings.json

@@ -18,7 +18,7 @@
     "DOPBang": "Server=123.60.181.157;Database=dopbiz;uid=sa;pwd=5heng)uN;Trusted_Connection=false;TrustServerCertificate=True",
     "DOPExt": "Server=123.60.181.157;Database=dopbiz;uid=sa;pwd=5heng)uN;Trusted_Connection=false;TrustServerCertificate=True",
 
-    "MongoDB": "mongodb://zzydop:h1Q$us3r@123.60.180.165/",
+    "MongoDB": "mongodb://zzydop:h1Q$us3r@123.60.181.157/",
     "DBName": "dopbase"
   },
   "Redis": {