From ae6bbf378b56b3a583527c546f8050b9813c1d1b Mon Sep 17 00:00:00 2001 From: 南骏 池 <chiffly@163.com> Date: 星期四, 13 三月 2025 14:38:54 +0800 Subject: [PATCH] 1.生产入库数据源优化 --- service/Warehouse/MesInvItemOutsManager.cs | 532 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 512 insertions(+), 20 deletions(-) diff --git a/service/Warehouse/MesInvItemOutsManager.cs b/service/Warehouse/MesInvItemOutsManager.cs index dae6a83..413e251 100644 --- a/service/Warehouse/MesInvItemOutsManager.cs +++ b/service/Warehouse/MesInvItemOutsManager.cs @@ -2,6 +2,7 @@ using NewPdaSqlServer.Dto.service; using NewPdaSqlServer.entity; using NewPdaSqlServer.util; +using Newtonsoft.Json; using SqlSugar; namespace NewPdaSqlServer.service.Warehouse; @@ -144,7 +145,7 @@ //杩斿洖ID var command = Db.Insertable(mesInvItemOuts) - .ExecuteCommand(); + .IgnoreColumns(true).ExecuteCommand(); if (command <= 0) throw new Exception("鎻掑叆閿欒"); @@ -161,7 +162,7 @@ }); var insertable = Db.Insertable(dto.SaveItems) - .ExecuteCommand(); + .IgnoreColumns(true).ExecuteCommand(); if (insertable < 0) throw new Exception("鎻掑叆閿欒"); @@ -316,9 +317,7 @@ var nflag = mesInvItemOuts.Nflag ?? 0; - if (nflag != 0) return true; - - return false; + return nflag != 0; } public bool Audit(WarehouseQuery query) @@ -337,14 +336,12 @@ { var isValid = Guid.TryParse(entity.id, out parsedGuid); if (!isValid) - throw new ApplicationException("GUID杞崲閿欒"); + throw new ApplicationException("GUID杞崲閿欒"); } if (!UtilityHelper.CheckGuid(parsedGuid)) - { - throw new ApplicationException("鏇存柊閿欒锛屾暟鎹槸涓嶅悎娉曠殑"); - } - + throw new ApplicationException("鏇存柊閿欒锛屾暟鎹槸涓嶅悎娉曠殑"); + var userName = entity.userName ?? "system"; return Db.Updateable<MesInvItemOuts>() .SetColumns(s => s.Status == entity.status) @@ -378,16 +375,87 @@ { var message = MesToErpParam(query); - // var executeReturnIdentity = - // Db.Insertable(message).ExecuteReturnIdentity(); - // if (executeReturnIdentity > 0) - // { - // message.Id = executeReturnIdentity; - // message.Pid = executeReturnIdentity; - // return message; - // } + var newGuid = Guid.NewGuid(); + message.Guid = newGuid; + message.Pid = newGuid; + var executeReturnIdentity = + Db.Insertable(message).IgnoreColumns(true).ExecuteCommand(); + if (executeReturnIdentity > 0) return message; throw new Exception("鑾峰彇鏁版嵁澶辫触"); + } + + public List<MesInvItemOuts> GetItemOutNo() + { + var data5 = Db.Queryable<MesInvItemOuts>().Where(it => + it.BillTypeId == 200 && it.TransactionNo == 203 && + it.Status == 1) + .ToPageList(1, 20); + + var ItemOutNos = data5.FindAll(a => a.Nflag == null || a.Nflag == 0) + .ToList(); + + return ItemOutNos; + } + + public OutItemDto GetSumItem(WarehouseQuery query) + { + var mesInvItemOuts = base.GetSingle(it => it.ItemOutNo == query.billNo); + if (mesInvItemOuts == null) throw new Exception("閲囪喘閫�璐у崟涓嶅瓨鍦�"); + + + var dto = new OutItemDto(); + //dto.SumItem = GetItems(query); + + var mesInvItemStocks = Db.Queryable<MesInvItemStocks>() + .Where(a => a.ItemBarcode == query.barcode).Single(); + + if (mesInvItemStocks == null) return dto; + + var mesItems = Db.Queryable<MesItems>() + .Where(s => s.Id == mesInvItemStocks.ItemId).Single(); + + dto.ItemNo = mesItems.ItemNo; + dto.Quantity = mesInvItemStocks.Quantity; + + return dto; + } + + public List<MesInvItemOutCDetails> GetScanBarcode(WarehouseQuery query) + { + //string杞琯uid + var parsedGuid = Guid.Empty; + if (string.IsNullOrEmpty(query.id)) return []; + + var isValid = Guid.TryParse(query.id, out parsedGuid); + if (!isValid) + throw new ApplicationException("GUID杞崲閿欒"); + + var mesInvItemOutCDetailsList = Db + .Queryable<MesInvItemOutCDetails, MesInvItemBarcodes, MesItems, + MesDepots>( + (b, bar, c, d) => new + JoinQueryInfos( + JoinType.Left, b.ItemBarcode == bar.ItemBarcode, + JoinType.Left, b.ItemId == c.Id, + JoinType.Left, b.DepotId == d.DepotId + )) + .Where((b, bar, c, d) => b.ItemOutId == parsedGuid) + .Select((b, bar, c, d) => new MesInvItemOutCDetails + { + Guid = b.Guid, + Quantity = b.Quantity, + DepotSectionCode = b.DepotSectionCode, + DepotCode = b.DepotCode, + ItemNo = b.ItemNo, + ItemBarcode = b.ItemBarcode, + ItemName = c.ItemName, + ItemModel = c.ItemModel, + DepotName = d.DepotName, + Unit = bar.Unit + }).ToList(); + + return mesInvItemOutCDetailsList; } public MessageCenter MesToErpParam(WarehouseQuery query) @@ -397,12 +465,12 @@ var tableName = "MES_INV_ITEM_OUTS_" + query.Type; if ("A".Equals(query.Type)) { - //erpParameters = GetErpParameters(query.billNo); + erpParameters = GetErpParameters(query.id); title = "閲囪喘閫�璐у崟" + query.billNo + "瀹℃牳"; } else if ("B".Equals(query.Type)) { - //erpParameters = GetDeApprovePam(query.id); + erpParameters = GetDeApprovePam(query.id); title = "閲囪喘閫�璐у崟" + query.billNo + "鍙嶅鏍�"; } @@ -426,4 +494,428 @@ }; return message; } + + private string GetErpParameters(string? id) + { + var guid = Guid.Empty; + if (string.IsNullOrEmpty(id)) return ""; + + var isValid = Guid.TryParse(id, out guid); + if (!isValid) + throw new ApplicationException("GUID杞崲閿欒"); + + var materials = Db + .Queryable<MesInvItemOuts, MesInvItemOutItems, + MesItems, MesUnit, MesInvItemArnDetail, MesDepots>( + (a, b, c, d, e, f) => new JoinQueryInfos( + JoinType.Left, a.Guid == b.ItemOutId, + JoinType.Left, b.ItemId == c.Id, + JoinType.Left, d.Id.ToString() == c.ItemUnit, + JoinType.Left, + e.CbillNo == b.WorkNo && e.ItemId == b.ItemId, + JoinType.Left, f.DepotId == a.DepotId + )).Where((a, b, c, d, e, f) => b.Guid == guid) + .Select<Material>((a, b, c, d, e, f) => new Material + { + FMaterialId = c.ItemNo, + FRMREALQTY = b.Quantity.ToString(), + FStockId = f.DepotCode, + FUnitID = d.Fnumber, + FLot = b.WorkNo, + F_MES_ENTRYID = b.Guid, + FsrcEntryId = e.Guid.ToString() + }).ToList(); + + + var mm = Db.Queryable<MesInvItemOuts>() + .Where(a => a.Guid == guid).First(); + + var jsonEntries = materials.Select(d => new + { + d.FMaterialId, + d.FRMREALQTY, + d.FStockId, + d.FUnitID, + d.FLot, + F_MES_ENTRYID = d.F_MES_ENTRYID.ToString(), + d.FsrcEntryId + }).ToList(); + var fdate = DateTime.Now.ToString("yyyy-MM-dd"); + var jsonString = JsonConvert.SerializeObject(jsonEntries); + var encodedUrl = "taskname=CGTL&mesid=" + guid + + "&optype=create&datajson={\"F_MES_ID\":\"" + guid + + "\",\"FDate\":\"" + fdate + + "\",\"FSRCBillTypeId\":\"" + "閲囪喘鍏ュ簱鍗�" + + "\",\"FDESCRIPTION\":\"" + mm.Remark + + "\",\"FMRMODE\":\"" + mm.Fmrmode + + "\",\"cgtlentry\":" + jsonString + + "}"; + return encodedUrl; + } + + private string GetDeApprovePam(string? id) + { + var encodedUrl = "taskname=CGTL&mesid=" + id + + "&optype=delete&datajson={}"; + + return encodedUrl; + } + + public bool ScanCode(WarehouseQuery query) + { + decimal billTypeId = 200; + decimal transactionNo = 203; + + var count = Db.Queryable<MesInvItemOutCDetails>() + .Where(a => + a.ItemBarcode == query.barcode && a.TaskNo == query.billNo) + .Count(); + + if (count > 0) throw new Exception("鏉$爜閲嶅鎵弿锛岃鏍稿锛�"); + + //楠岃瘉鏉$爜 + var mesInvItemStocks = Db.Queryable<MesInvItemStocks>() + .Where(a => a.ItemBarcode == query.barcode + && a.Quantity > 0).Single(); + + if (mesInvItemStocks == null) + throw new Exception(query.barcode + "搴撳瓨涓棤姝ゆ潯鐮侊紝璇锋牳瀵癸紒"); + + //楠岃瘉鍑哄簱鍗昺es_inv_item_outs + var mesInvItemOuts = Db.Queryable<MesInvItemOuts>() + .Where(d => d.ItemOutNo == query.billNo + && d.BillTypeId == billTypeId && + d.TransactionNo == transactionNo).Single(); + + //鍑哄簱鍗曠殑鏍¢獙 + if (mesInvItemOuts == null) + throw new Exception("鍑哄簱鍗� " + query.billNo + " 涓嶅瓨鍦紝璇风‘璁わ紒"); + + //鏈鏍哥殑涓嶅厑璁哥户缁� + if (mesInvItemOuts.Status == null || mesInvItemOuts.Status == 0) + throw new Exception("鍑哄簱鍗� " + query.billNo + "鏈鏍�"); + + //宸叉帹閫佺殑涓嶅厑璁哥户缁� + if (mesInvItemOuts.Nflag == 1) + throw new Exception("鍑哄簱鍗� " + query.billNo + "宸茬粡鎺ㄩ�侊紝鏃犳硶閲嶅鎺ㄩ��"); + + var depotCodeOut = mesInvItemOuts.DepotCode ?? "0"; + var depotsCodeStock = mesInvItemStocks.DepotsCode ?? "0"; + + if (!depotCodeOut.Equals(depotsCodeStock)) + { + // 鍒涘缓閿欒娑堟伅 + var errorMessage = + $"002[鏉$爜浠撳簱{depotsCodeStock}涓庣敵璇蜂粨搴搟depotCodeOut}涓嶄竴鑷达紝璇锋牳瀵癸紒"; + + throw new Exception(errorMessage); + } + + var StocksNum = mesInvItemStocks.Quantity; + + var sql = string.Format( + @"select isnull(SUM(S.QUANTITY),0) - isnull(SUM(S.TL_QTY),0) + from MES_INV_ITEM_OUT_ITEMS S + LEFT JOIN mes_inv_item_outs d + ON S.ITEM_OUT_ID = D.GUID + where d.item_out_no = '{0}' + and d.bill_type_id = {1} + and d.transaction_no = {2} + AND S.item_ID = '{3}' + and isnull(d.status, 0) = 1 + and isnull(d.nflag, 0) = 0", query.billNo, billTypeId, + transactionNo, + mesInvItemStocks.ItemId); + + var cqty = Db.Ado.SqlQuerySingle<decimal>(sql); + + if (cqty == null) + throw new Exception("鐗╂枡" + mesInvItemStocks.ItemNo + + "鐨勮緟鍔╁睘鎬т笌鏉$爜涓嶄竴鑷达紒璇锋鏌�"); + + if (cqty < StocksNum) throw new Exception("璇风‘璁ゅ彂鏂欐暟閲�"); + + //璋冪敤瀛樺偍杩囩▼ + var prcRfPdaBarcodeCgth = PrcRfPdaBarcodeCgth(query); + if (!prcRfPdaBarcodeCgth) throw new Exception("鎵ц澶辫触"); + + // 鍙橀噺鐢ㄤ簬淇濆瓨鏌ヨ缁撴灉 + decimal? C_SQ_QTY = 0; + decimal? C_OK_QTY = 0; + var C_COUNT = 0; + + sql = string.Format(@" + SELECT SUM(C.QUANTITY) AS SQ_QTY, SUM(D.QUANTITY_OK) AS OK_QTY + FROM MES_INV_ITEM_OUT_ITEMS C + LEFT JOIN (SELECT ITEM_OUT_ID, ITEM_ID, PBILL_NO, RK_NO, SUM(QUANTITY) AS QUANTITY_OK + FROM MES_INV_ITEM_OUT_C_DETAILS + GROUP BY ITEM_OUT_ID, ITEM_ID, PBILL_NO, RK_NO) D + ON D.ITEM_OUT_ID = C.ITEM_OUT_ID + AND D.ITEM_ID = C.ITEM_ID + AND isnull(C.PBILL_NO, '0') = isnull(D.PBILL_NO, '0') + WHERE C.ITEM_OUT_ID = '{0}'", + mesInvItemOuts.Guid); + + var queryResult = Db.Ado.SqlQuerySingle<dynamic>(sql); + + C_SQ_QTY = queryResult?.SQ_QTY ?? 0; + C_OK_QTY = queryResult?.OK_QTY ?? 0; + + // 鍒ゆ柇閫昏緫 + if (C_SQ_QTY < C_OK_QTY) throw new Exception("鎵弿鏁伴噺瓒呰繃鐢宠鏁伴噺,璇烽噸鏂版壂鎻忥紒"); + + return C_SQ_QTY == C_OK_QTY; + } + + private bool PrcRfPdaBarcodeCgth(WarehouseQuery query) + { + var c_user = query.userName; + var p_bill_no = query.billNo; + var p_item_barcode = query.barcode; + var p_bill_type_id = 200; + var p_transaction_no = 203; + int c_num; + decimal C_STOCKNUM, C_SQTY, C_OKQTY, C_CQTY; + + var commit = UseTransaction(db => + { + if (string.IsNullOrEmpty(p_bill_no)) throw new Exception("璇烽�夊彇鍗曟嵁鍙凤紒"); + + // Retrieve mes_inv_item_outs + var c_mes_inv_item_outs = db.Queryable<MesInvItemOuts>() + .Where(d => d.ItemOutNo == p_bill_no) + .First(); + + if (c_mes_inv_item_outs == null) + throw new Exception("璇烽�夋嫨鎴栨壂鎻忔纭殑閲囪喘閫�璐у崟锛�"); + + if (c_mes_inv_item_outs.Status == 0) + throw new Exception("閲囪喘閫�璐х敵璇峰崟鏈鏍革紒"); + + if (c_mes_inv_item_outs.Nflag == 1) + throw new Exception("閲囪喘閫�璐у崟宸插畬缁撴帹閫丒RP,涓嶈兘鎿嶄綔锛�"); + + // Check for duplicate barcode scan + c_num = db.Queryable<MesInvItemOutCDetails>() + .Where(b => + b.ItemBarcode == p_item_barcode && + b.ItemOutId == c_mes_inv_item_outs.Guid) + .Count(); + + if (c_num > 0) throw new Exception("姝ゆ潯鐮佸凡閲囪喘閫�璐ф壂鐮佸畬鎴愶紒"); + + // Query item stock info + var c_mes_inv_item_stocks = db.Queryable<MesInvItemStocks>() + .Where(t => t.ItemBarcode == p_item_barcode && t.Quantity > 0) + .First(); + + if (c_mes_inv_item_stocks == null) + throw new Exception($"搴撳瓨涓棤姝ゆ潯鐮侊紝璇锋牳瀵癸紒{p_item_barcode}"); + + // Validate depot codes + if (c_mes_inv_item_outs.DepotCode != + c_mes_inv_item_stocks.DepotsCode) + throw new Exception( + $"姝ゆ潯鐮佸睘浜庝粨搴� {c_mes_inv_item_stocks.DepotsCode} 涓庨噰璐��璐х敵璇蜂粨搴� {c_mes_inv_item_outs.DepotCode} 涓嶄竴鑷达紝璇锋牳瀵癸紒"); + + C_STOCKNUM = c_mes_inv_item_stocks.Quantity.Value; + + // Calculate the total and completed quantities for the item + var quantities = db.Queryable<MesInvItemOutItems, MesInvItemOuts>( + (s, d) => new object[] + { + JoinType.Left, s.ItemOutId == d.Guid + }) + .Where((s, d) => + d.ItemOutNo == p_bill_no && + s.ItemId == c_mes_inv_item_stocks.ItemId + && d.Status == 1 + && (d.Nflag == 0 || d.Nflag == null)) + .Select((s, d) => new + { + TotalQuantity = SqlFunc.AggregateSum(s.Quantity), + CompletedQuantity = SqlFunc.AggregateSum(s.TlQty) + }) + .First(); + + if (quantities == null) + throw new Exception("閲囪喘閫�鏂欏崟 '" + p_bill_no + "'鏈鏍�"); + + C_SQTY = quantities.TotalQuantity ?? 0; + C_OKQTY = quantities.CompletedQuantity ?? 0; + C_CQTY = C_SQTY - C_OKQTY; + + if (C_CQTY < C_STOCKNUM) return 1; + + // Handle barcode reduction + var p_number2 = C_STOCKNUM; + var itemOutItems = db.Queryable<MesInvItemOutItems>() + .Where(s => + s.ItemOutId == c_mes_inv_item_outs.Guid && + s.ItemId == c_mes_inv_item_stocks.ItemId) + .ToList(); + + itemOutItems = itemOutItems + .Where(s => s.Quantity - (s.TlQty ?? 0) > 0).ToList(); + + foreach (var CMI in itemOutItems) + { + if (p_number2 == 0) + break; + + var c_mes_inv_item_arn_detail = db + .Queryable<MesInvItemArnDetail>() + .Where(t => + t.CbillNo == CMI.PbillNo + && t.Ebeln == CMI.WorkNo && + //t.WorkLine == CMI.WorkLine && + t.ItemId == CMI.ItemId) + .First(); + + if (c_mes_inv_item_arn_detail == null) + throw new Exception("鏈壘鍒伴噰璐��鏂欐潵婧愬崟鎹�"); + + if (CMI.Quantity - CMI.TlQty <= p_number2) + { + db.Updateable<MesInvItemOutItems>() + .SetColumns(it => new MesInvItemOutItems + { TlQty = (int)it.Quantity, Status = 1 }) + .Where(it => it.Guid == CMI.Guid) + .ExecuteCommand(); + + p_number2 -= CMI.Quantity ?? 0 - CMI.TlQty ?? 0; + + db.Insertable(new MesInvItemOutCDetails + { + ItemOutId = c_mes_inv_item_outs.Guid, + ItemBarcode = p_item_barcode, + ItemNo = c_mes_inv_item_stocks.ItemNo, + LotNo = c_mes_inv_item_stocks.LotNo, + Quantity = CMI.Quantity - CMI.TlQty, + ForceOutFlag = 0, + CreateBy = c_user, + CreateDate = DateTime.Now, + LastupdateBy = c_user, + LastupdateDate = DateTime.Now, + DepotCode = c_mes_inv_item_stocks.DepotsCode, + DepotSectionCode = + c_mes_inv_item_stocks.DepotSectionsCode, + WorkNo = CMI.WorkNo, + WorkLine = CMI.WorkLine, + SuppNo = c_mes_inv_item_outs.SuppNo, + PbillNo = CMI.PbillNo, + ItemId = c_mes_inv_item_stocks.ItemId, + EbelnK3id = c_mes_inv_item_stocks.EbelnK3id, + LineK3id = c_mes_inv_item_stocks.LineK3id, + DepotId = c_mes_inv_item_outs.DepotId + }).IgnoreColumns(true).ExecuteCommand(); + + db.Insertable(new MesInvBusiness2 + { + Status = 1, + BillTypeId = p_bill_type_id, + TransactionCode = p_transaction_no.ToString(), + BusinessType = 1, + ItemBarcode = p_item_barcode, + ItemNo = c_mes_inv_item_stocks.ItemNo, + LotNo = c_mes_inv_item_stocks.LotNo, + EpFlag = true, + Quantity = CMI.Quantity - CMI.TlQty, + FromInvDepotsCode = c_mes_inv_item_stocks.DepotsCode, + FromInvDepotSectionsCode = + c_mes_inv_item_stocks.DepotSectionsCode, + ToInvDepotsCode = null, + ToInvDepotSectionsCode = null, + Description = "閲囪喘閫�璐�", + CreateBy = c_user, + CreateDate = DateTime.Now, + LastupdateBy = c_user, + LastupdateDate = DateTime.Now, + TaskNo = CMI.WorkNo, + ItemId = c_mes_inv_item_stocks.ItemId, + EbelnK3id = c_mes_inv_item_stocks.EbelnK3id, + LineK3id = c_mes_inv_item_stocks.LineK3id + }).IgnoreColumns(true).ExecuteCommand(); + + if (p_number2 == 0) + db.Deleteable<MesInvItemStocks>() + .Where(s => s.Guid == c_mes_inv_item_outs.Guid) + .ExecuteCommand(); + } + else + { + var mesInvItemOutItems = db.Queryable<MesInvItemOutItems>() + .Where(s => s.Guid == CMI.Guid).First(); + + db.Updateable<MesInvItemOutItems>() + .SetColumns(i => + i.TlQty == p_number2 + + (mesInvItemOutItems.TlQty ?? 0)) + .Where(i => i.Guid == CMI.Guid) + .ExecuteCommand(); + + db.Insertable(new MesInvItemOutCDetails + { + ItemOutId = c_mes_inv_item_outs.Guid, + ItemBarcode = p_item_barcode, + ItemNo = c_mes_inv_item_stocks.ItemNo, + LotNo = c_mes_inv_item_stocks.LotNo, + Quantity = p_number2, + ForceOutFlag = 0, + CreateBy = c_user, + CreateDate = DateTime.Now, + LastupdateBy = c_user, + LastupdateDate = DateTime.Now, + DepotCode = c_mes_inv_item_stocks.DepotsCode, + DepotSectionCode = + c_mes_inv_item_stocks.DepotSectionsCode, + WorkNo = CMI.WorkNo, + WorkLine = CMI.WorkLine, + SuppNo = c_mes_inv_item_outs.SuppNo, + ItemId = c_mes_inv_item_stocks.ItemId, + EbelnK3id = c_mes_inv_item_stocks.EbelnK3id, + LineK3id = c_mes_inv_item_stocks.LineK3id, + DepotId = c_mes_inv_item_outs.DepotId + }).IgnoreColumns(true).ExecuteCommand(); + + db.Insertable(new MesInvBusiness2 + { + Status = 1, + BillTypeId = p_bill_type_id, + TransactionCode = p_transaction_no.ToString(), + BusinessType = 1, + ItemBarcode = p_item_barcode, + ItemNo = c_mes_inv_item_stocks.ItemNo, + LotNo = c_mes_inv_item_stocks.LotNo, + EpFlag = true, + Quantity = p_number2, + FromInvDepotsCode = c_mes_inv_item_stocks.DepotsCode, + FromInvDepotSectionsCode = + c_mes_inv_item_stocks.DepotSectionsCode, + Description = "閲囪喘閫�璐�", + CreateBy = c_user, + CreateDate = DateTime.Now, + LastupdateBy = c_user, + LastupdateDate = DateTime.Now, + TaskNo = CMI.PbillNo, + BillNo = p_bill_no, + WorkNo = CMI.WorkNo, + WorkLine = CMI.WorkLine, + SuppNo = c_mes_inv_item_outs.SuppNo, + SuppId = c_mes_inv_item_outs.SuppId.ToString(), + ItemId = c_mes_inv_item_stocks.ItemId, + EbelnK3id = c_mes_inv_item_stocks.EbelnK3id, + LineK3id = c_mes_inv_item_stocks.LineK3id + }).IgnoreColumns(true).ExecuteCommand(); + p_number2 = 0; + } + } + + if (p_number2 > 0) throw new Exception("鐗╂枡鏁伴噺灏嗚秴閲囪喘閫�鏂欑敵璇峰崟鏁伴噺,鏃犳硶鍑哄簱"); + + return 1; + }); + + return commit > 0; + } } \ No newline at end of file -- Gitblit v1.9.3