From 7883316c61d4f8fa70fe6cde0c0f53e9e3d8d1d0 Mon Sep 17 00:00:00 2001
From: 南骏 池 <chiffly@163.com>
Date: 星期四, 15 五月 2025 14:22:40 +0800
Subject: [PATCH] 1.OA->MES审批接口开发。 2.OA->MES退回接口开发。 3.MES->OA由同步调整为异步。

---
 service/Wom/WwGdManager.cs | 1041 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 1,018 insertions(+), 23 deletions(-)

diff --git a/service/Wom/WwGdManager.cs b/service/Wom/WwGdManager.cs
index 7a64858..09de0e9 100644
--- a/service/Wom/WwGdManager.cs
+++ b/service/Wom/WwGdManager.cs
@@ -1,5 +1,6 @@
 锘縰sing System.Data;
 using System.Data.SqlClient;
+using System.Threading.Tasks.Dataflow;
 using Masuit.Tools;
 using NewPdaSqlServer.DB;
 using NewPdaSqlServer.Dto.service;
@@ -58,7 +59,9 @@
                         daa001 = query.daa001,
                         barcodeNum = barcodeNum,
                         splitNum = splitNum,
-                        barcode = query.barcode
+                        barcode = query.barcode,
+                        strMsg = _strMsg,
+                        result = _intSum
                     };
 
                     return dto;
@@ -147,7 +150,7 @@
         var wwgd = Db.Queryable<WwGd, MesItems>((a, i) =>
                 new JoinQueryInfos(JoinType.Left,
                     a.Daa003 == i.ItemId))
-            .Where((a, i) => a.Daa001 == query.daa001)
+            .Where((a, i) => a.Daa001 == query.daa001 && (a.PcSh ?? 0) == 1)
             .Select((a, i) => new
             {
                 a.Daa001, a.RwdGuid
@@ -162,8 +165,7 @@
                         JoinType.Left,
                         a.Id == b.Pid,
                         JoinType.Inner,
-                        c.Id ==
-                        b.Dab003,
+                        c.Id == b.Dab003,
                         JoinType.Inner, b.Erpid.ToString() == d.ErpId
                     ))
             .Where((a, b, c, d) =>
@@ -218,11 +220,500 @@
         return dto;
     }
 
+    #region 濮斿閫�鏂�
+
+    /// <summary>
+    ///     濮斿閫�鏂欐壂鎻忔潯鐮�(鐢熶骇閫�鏂欑殑閫昏緫锛屽彧鏄皟鏁翠簡鍑犱釜瀛楁)
+    /// </summary>
+    /// <param name="query">浠撳簱鏌ヨ鍙傛暟</param>
+    /// <returns>澶勭悊缁撴灉</returns>
+    public WarehouseQuery WwtlScanBarcode(WarehouseQuery query)
+    {
+        var p_item_barcode = query.barcode; // 鐗╂枡鏉$爜
+        var p_bill_no = query.billNo; // 鍗曟嵁鍙�
+        var p_section_code = query.DepotCode; // 搴撲綅缂栫爜
+        var c_user = query.userName; // 鐢ㄦ埛鍚�
+
+        var p_bill_type_id = 100; // 鍗曟嵁绫诲瀷ID
+        var p_transction_no = 104; // 浜ゆ槗缂栧彿
+
+        // 楠岃瘉搴撲綅鏉$爜
+        if (p_section_code.IsNullOrEmpty()) throw new Exception("璇锋壂搴撲綅鏉$爜锛�");
+
+        // 鑾峰彇搴撲綅淇℃伅
+        var c_depot_code = Db.Queryable<MesDepotSections, MesDepots>((a, b) =>
+                new JoinQueryInfos(JoinType.Inner, a.DepotGuid == b.Guid))
+            .Where((a, b) => a.DepotSectionCode == p_section_code)
+            .Select((a, b) => b.DepotId).First();
+
+        if (!c_depot_code.HasValue)
+            throw new Exception("搴撲綅缂栫爜" + p_section_code + " 涓嶅瓨鍦紝璇风‘璁わ紒");
+
+        // 鑾峰彇搴撲綅鍒嗗尯淇℃伅
+        var mesDepotSections = Db.Queryable<MesDepotSections>()
+            .Where(a => a.DepotSectionCode == p_section_code).First();
+
+        if (mesDepotSections == null)
+            throw new Exception("搴撲綅缂栫爜" + p_section_code + " 涓嶅瓨鍦紝璇风‘璁わ紒");
+
+        // 鑾峰彇搴撲綅鍩虹淇℃伅
+        var c_mes_depots = Db.Queryable<MesDepots>()
+            .Where(b => b.Guid == mesDepotSections.DepotGuid).First();
+
+        if (c_mes_depots == null)
+            throw new Exception("搴撲綅缂栫爜" + p_section_code + " 涓嶅瓨鍦紝璇风‘璁わ紒");
+
+        // 妫�鏌ユ潯鐮佹槸鍚﹀凡鍏ュ簱
+        var c_num = Db.Queryable<MesInvItemIns, MesInvItemInCDetails>((a, b) =>
+                new JoinQueryInfos(JoinType.Inner, a.Guid == b.ItemInId))
+            .Where((a, b) => b.ItemBarcode == p_item_barcode
+                             && a.BillTypeId == p_bill_type_id &&
+                             a.TransctionNo == p_transction_no.ToString())
+            .Count();
+
+        if (c_num > 0) throw new Exception("姝ゆ潯鐮佸凡鎵叆搴擄紝鍕块噸澶嶆壂鎻忥紒");
+
+        c_num = Db.Queryable<MesInvItemStocks>()
+            .Where(t => t.ItemBarcode == p_item_barcode).Count();
+
+        if (c_num > 0) throw new Exception("姝ゆ潯鐮佸凡鎵叆搴擄紝鍕块噸澶嶆壂鎻忥紒");
+
+        // 鑾峰彇鏉$爜淇℃伅
+        var c_mes_inv_item_barcodes = Db.Queryable<MesInvItemBarcodes>()
+            .Where(t => t.ItemBarcode == p_item_barcode).First();
+
+        if (c_mes_inv_item_barcodes == null)
+            throw new Exception("姝ゆ潯鐮佷笉灞炰簬璇ラ��鏂欏崟锛岃鏍稿锛�");
+
+       
+
+        // if (c_mes_inv_item_barcodes.Memo != "鐢熶骇閫�鏂�")
+        //     throw new Exception("姝ゆ潯鐮佷笉鏄敓浜ч��鏂欐潯鐮侊紝涓嶅彲浣跨敤鐢熶骇閫�鏂欐ā鍧楋紒");
+
+        // 鑾峰彇閫�鏂欏崟淇℃伅
+        var C_MES_ITEM_TBL = Db.Queryable<MesItemTbl>()
+            .Where(a => a.BillNo == c_mes_inv_item_barcodes.BillNo
+                        && (a.Tbl013 ?? 0) == 1).First();
+
+        if (C_MES_ITEM_TBL == null) throw new Exception("鐢宠鍗曞凡鎾ゅ洖,鏃犳硶鎵爜!");
+
+        if (C_MES_ITEM_TBL.Tbl020 == 1) throw new Exception("鎵爜瀹屾垚,鐢宠鍗曞凡瀹岀粨!");
+
+        // 妫�鏌ュ伐鍗曚俊鎭�
+        var wwgd = Db.Queryable<WwGd>()
+            .Where(a => a.Id.ToString() == C_MES_ITEM_TBL.Tbl002)
+            .First();
+
+        if (wwgd == null) throw new Exception("鐢宠鍗曞搴旂殑宸ュ崟涓嶅瓨鍦ㄦ垨宸插垹闄�,鏃犳硶鎵爜!");
+
+        // 妫�鏌ュ伐鍗曚俊鎭�
+        var WWRWD = Db.Queryable<ProductionOrder>()
+            .Where(a => a.OrderNo == wwgd.Daa014)
+            .First();
+
+        // 鑾峰彇閫�鏂欏崟鏄庣粏
+        var C_MES_ITEM_TBL_DETAIL = Db.Queryable<MesItemTblDetail>()
+            .Where(a => a.Tlid == c_mes_inv_item_barcodes.AboutGuid)
+            .First();
+
+        if (C_MES_ITEM_TBL_DETAIL == null)
+            throw new Exception("鏉$爜涓嶅睘浜庤鐢宠鍗曟槑缁�,鏃犳硶鎵爜!");
+
+        //鍒ゆ柇璐т富淇℃伅
+        var CABerpid = Db.Queryable<WwGdDetail>().Where(womdab => womdab.Id.ToString() == C_MES_ITEM_TBL_DETAIL.Tld013).Select(womdab => womdab.Erpid).First();
+
+        var WWCAB = Db.Queryable<ProductionOrderSub>().Where(womcab => womcab.ErpId == CABerpid.Value.ToString()).First();
+
+
+        if ( String.IsNullOrEmpty(WWCAB.Owner)) throw new Exception("鐢ㄦ枡娓呭崟璐т富淇℃伅涓嶅瓨鍦�,鏃犳硶鎵爜锛岃鑱旂郴绠$悊鍛�!");
+
+        //var owner_type = "";
+        //if (Db.Queryable<SysOrganization>().Any(x => x.Fid == ownerId))
+        //{
+        //    owner_type = "BD_OwnerOrg";
+        //}
+        //else
+        //{
+        //    // 绗簩灞傚垽鏂細妫�鏌� MES_CUSTOMER
+        //    if (Db.Queryable<MesCustomer>().Any(x => x.Id == Convert.ToInt32(ownerId)))
+        //    {
+        //        owner_type = "BD_Customer";
+        //    }
+        //    else
+        //    {
+        //        // 绗笁灞傚垽鏂細妫�鏌� MES_SUPPLIER
+        //        if (Db.Queryable<MesSupplier>().Any(x => x.Id == Convert.ToInt32(ownerId)))
+        //        {
+        //            owner_type = "BD_Supplier";
+        //        }
+        //        else
+        //        {
+        //            throw new Exception("鍏ュ簱澶辫触,鐢ㄦ枡娓呭崟璐т富淇℃伅瀛樺湪闂锛岃鑱旂郴绠$悊鍛樿В鍐筹紒");
+        //        }
+        //    }
+        //}
+
+        var c_quantity = c_mes_inv_item_barcodes.Quantity;
+
+        var c_bill_no = "";
+        var c_id = Guid.Empty;
+
+        // 浣跨敤浜嬪姟澶勭悊鏁版嵁鏇存柊
+        UseTransaction(db =>
+        {
+            // 鏌ヨ鍏ュ簱鍗�
+            var mesInvItemIns = db.Queryable<MesInvItemIns>()
+                .Where(d =>
+                    d.Status == 0 && d.TransctionNo ==
+                                  p_transction_no.ToString()
+                                  && d.TaskNo == c_mes_inv_item_barcodes.BillNo
+                                  && d.DepotsId == c_depot_code
+                                  && d.InsDate.Value.ToString("yyyyMMdd") ==
+                                  DateTime.Now.ToString("yyyyMMdd")).First();
+
+            var totalResult = 0;
+
+            // 濡傛灉鍏ュ簱鍗曚笉瀛樺湪鍒欏垱寤烘柊鐨勫叆搴撳崟
+            if (mesInvItemIns == null)
+            {
+                c_bill_no = BillNo.GetBillNo("WWTL(濮斿閫�鏂�)");
+
+                c_id = Guid.NewGuid();
+
+                totalResult += db.Insertable(new MesInvItemIns
+                {
+                    Guid = c_id,
+                    BillNo = c_bill_no,
+                    BillTypeId = p_bill_type_id,
+                    InsDate = DateTime.Now,
+                    DepotsId = c_depot_code,
+                    UserNoBack = c_user,
+                    Reason = C_MES_ITEM_TBL.Tbl005,
+                    Remark = C_MES_ITEM_TBL.Tbl006,
+                    //InsDate = DateTime.Now,
+                    DepotsCode = c_mes_depots.DepotCode,
+                    TaskNo = c_mes_inv_item_barcodes.BillNo,
+                    //DepotsId = c_depot_code,
+                    TransctionNo = p_transction_no.ToString(),
+                    CreateBy = c_user,
+                    CreateDate = DateTime.Now,
+                    LastupdateBy = c_user,
+                    LastupdateDate = DateTime.Now,
+                    CbillNo = wwgd.Daa001,
+                    InType = "濮斿閫�鏂�",
+                    ReceiveOrgId = c_mes_depots.FSubsidiary,
+                    Fstatus = 0,
+                    Status = 0,
+                    WorkNo = WWRWD.ErpProductionOrderNo
+                }).IgnoreColumns(true).ExecuteCommand();
+            }
+            else
+            {
+                c_id = mesInvItemIns.Guid;
+                c_bill_no = mesInvItemIns.BillNo;
+            }
+
+            // 妫�鏌ユ槸鍚︿负鍚堝苟鎵撳嵃鏉$爜
+            var hbdy = c_mes_inv_item_barcodes.Hbdy ?? 0;
+            if (hbdy == 1) throw new Exception("涓嶆敮鎸佸悎骞舵墦鍗扮殑鏉$爜:" + p_item_barcode);
+
+
+            // 妫�鏌ユ槸鍚﹀瓨鍦ㄤ簬 MES_INV_ITEM_IN_C_ITEMS 琛�
+            var existingCount = db.Queryable<MesInvItemInCItems>()
+            .Where(it =>
+                it.ItemInId == c_id &&
+                                it.ItemId == C_MES_ITEM_TBL_DETAIL.Tld009 &&
+                                it.DepotId == c_depot_code.ToString())
+                .Count();
+
+            if (existingCount == 0)
+                // 涓嶅瓨鍦ㄦ椂鎻掑叆鏂拌褰�
+                db.Insertable(new MesInvItemInCItems
+                {
+                    ItemInId = c_id,
+                    Quantity = c_mes_inv_item_barcodes.Quantity,
+                    CreateBy = c_user,
+                    CreateDate = DateTime.Now,
+                    ItemNo = c_mes_inv_item_barcodes.ItemNo,
+                    //DepotCode = mesDepost.DepotCode,
+                    ItemSname = c_mes_inv_item_barcodes.ItemSname,
+                    Unit = c_mes_inv_item_barcodes.Unit,
+                    Ebeln = c_mes_inv_item_barcodes.WorkNo,
+                    BillNo = c_bill_no,
+                    WorkNo = c_mes_inv_item_barcodes.WorkNo,
+                    EbelnLineNo = c_mes_inv_item_barcodes.WorkLine,
+                    CbillNo = c_mes_inv_item_barcodes.BillNo,
+                    WorkLine = c_mes_inv_item_barcodes.WorkLine,
+                    SuppId = c_mes_inv_item_barcodes.SuppId,
+                    SuppNo = c_mes_inv_item_barcodes.SuppNo,
+                    Remark = c_mes_inv_item_barcodes.Memo,
+                    EbelnK3id = c_mes_inv_item_barcodes.EbelnK3id,
+                    LineK3id = c_mes_inv_item_barcodes.LineK3id,
+                    ItemId = c_mes_inv_item_barcodes.ItemId,
+                    DepotCode = c_mes_depots.DepotCode,
+                    DepotId = c_depot_code.ToString(),
+                    itemDabid = c_mes_inv_item_barcodes.AboutGuid.ToString()
+                }).IgnoreColumns(true).ExecuteCommand();
+            else
+                // 瀛樺湪鏃舵洿鏂版暟閲�
+                db.Updateable<MesInvItemInCItems>()
+                    .SetColumns(it => new MesInvItemInCItems
+                    {
+                        Quantity = SqlFunc.IsNull(it.Quantity, 0) + c_mes_inv_item_barcodes.Quantity // 纭繚 Quantity 涓嶄负 null
+                    })
+                .Where(it =>
+                       it.ItemInId == c_id &&
+                                it.ItemId == C_MES_ITEM_TBL_DETAIL.Tld009 &&
+                                it.DepotId == c_depot_code.ToString())
+                    //.IgnoreColumns(true) // 淇濈暀 IgnoreColumns
+                    .ExecuteCommand();
+
+            // 鎻掑叆鍏ュ簱鍗曟槑缁�
+            totalResult += db.Insertable(new MesInvItemInCDetails
+            {
+                Guid = Guid.NewGuid(),
+                ItemInId = c_id,
+                BillNo = c_bill_no,
+                ItemBarcode = p_item_barcode,
+                Quantity = c_quantity,
+                BarcodeFlag = true,
+                EpFlag = true,
+                WorkType = 1,
+                ItemId = c_mes_inv_item_barcodes.ItemId,
+                ItemNo = c_mes_inv_item_barcodes.ItemNo,
+                LotNo = c_mes_inv_item_barcodes.LotNo,
+                SuppId = c_mes_inv_item_barcodes.SuppId,
+                SuppNo = c_mes_inv_item_barcodes.SuppNo,
+                DepotId = c_mes_depots.DepotId,
+                DepotCode = c_mes_depots.DepotCode,
+                DepotSectionCode = p_section_code,
+                ItemSname = c_mes_inv_item_barcodes.ItemSname,
+                Unit = c_mes_inv_item_barcodes.Unit,
+                CreateBy = c_user,
+                CreateDate = DateTime.Now,
+                LastupdateBy = c_user,
+                LastupdateDate = DateTime.Now,
+                Remark = c_mes_inv_item_barcodes.Memo,
+                Ebeln = c_mes_inv_item_barcodes.Mblnr,
+                EbelnLineNo = c_mes_inv_item_barcodes.Zeile,
+                WorkNo = c_mes_inv_item_barcodes.WorkNo,
+                WorkLine = c_mes_inv_item_barcodes.WorkLine,
+                CbillNo = c_mes_inv_item_barcodes.BillNo,
+                UrgentFlag = c_mes_inv_item_barcodes.UrgentFlag,
+                BoardStyle = c_mes_inv_item_barcodes.BoardStyle,
+                TaskNo = c_mes_inv_item_barcodes.TaskNo,
+                RbillNo = C_MES_ITEM_TBL.Tbl002,
+                ReceiveOrgId = c_mes_depots.FSubsidiary,
+                EbelnK3id = c_mes_inv_item_barcodes.EbelnK3id,
+                LineK3id = c_mes_inv_item_barcodes.LineK3id,
+                Ischeck = true,
+            }).IgnoreColumns(true).ExecuteCommand();
+
+            // 鎻掑叆涓氬姟璁板綍
+            totalResult += db.Insertable(new MesInvBusiness2
+            {
+                Guid = Guid.NewGuid(),
+                Status = 1,
+                BillTypeId = p_bill_type_id,
+                TransactionCode = p_transction_no.ToString(),
+                BusinessType = 1,
+                ItemBarcode = p_item_barcode,
+                ItemNo = c_mes_inv_item_barcodes.ItemNo,
+                LotNo = c_mes_inv_item_barcodes.LotNo,
+                EpFlag = true,
+                Quantity = c_mes_inv_item_barcodes.Quantity,
+                ToInvDepotsCode = c_mes_depots.DepotCode,
+                InvDepotId = c_depot_code,
+                ToInvDepotSectionsCode = p_section_code,
+                Description = "濮斿閫�鏂�",
+                CreateBy = c_user,
+                CreateDate = DateTime.Now,
+                LastupdateBy = c_user,
+                LastupdateDate = DateTime.Now,
+                TaskNo = c_mes_inv_item_barcodes.TaskNo,
+                BillNo = c_bill_no,
+                WorkNo = c_mes_inv_item_barcodes.WorkNo,
+                WorkLine = c_mes_inv_item_barcodes.WorkLine,
+                SuppId = c_mes_inv_item_barcodes.SuppId,
+                SuppNo = c_mes_inv_item_barcodes.SuppNo,
+                ItemId = c_mes_inv_item_barcodes.ItemId
+            }).IgnoreColumns(true).ExecuteCommand();
+
+            // 鎻掑叆搴撳瓨璁板綍
+            totalResult += db.Insertable(new MesInvItemStocks
+            {
+                Guid = Guid.NewGuid(),
+                TaskNo = c_mes_inv_item_barcodes.TaskNo,
+                ItemBarcode = p_item_barcode,
+                ItemNo = c_mes_inv_item_barcodes.ItemNo,
+                LotNo = c_mes_inv_item_barcodes.LotNo,
+                Quantity = c_mes_inv_item_barcodes.Quantity,
+                //EpFlag = c_mes_inv_item_barcodes.EpFlag.Value
+                //    ? (byte)1
+                //    : (byte)0,
+                DepotId = c_mes_depots.DepotId,
+                DepotsCode = c_mes_depots.DepotCode,
+                DepotSectionsCode = p_section_code,
+                CheckDate = c_mes_inv_item_barcodes.CreateDate,
+                IndepDate = DateTime.Now,
+                BoardStyle = c_mes_inv_item_barcodes.BoardStyle,
+                WorkNo = c_mes_inv_item_barcodes.WorkNo,
+                WorkLine = c_mes_inv_item_barcodes.WorkLine,
+                SuppNo = c_mes_inv_item_barcodes.SuppNo,
+                ItemId = c_mes_inv_item_barcodes.ItemId,
+                BillNo = c_mes_inv_item_barcodes.BillNo,
+                //DepotId = Convert.ToInt32(c_depot_code),
+                OwnerId = WWCAB.Owner,
+                OwnerType = WWCAB.OwnerType,
+                StockOrgId = c_mes_depots.FSubsidiary,
+                IndepUserCode = c_user
+            }).IgnoreColumns(true).ExecuteCommand();
+
+            // 鏍规嵁閫�鏂欑被鍨�(鑹搧閫�鏂欍�佹潵鏂欎笉鑹��鏂欍�佷綔涓氫笉鑹��鏂�)鏇存柊鐩稿叧鏁版嵁
+            if (C_MES_ITEM_TBL.Tbl005 is "鑹搧閫�鏂�" or "鏉ユ枡涓嶈壇閫�鏂�")
+            {
+                // 鑹搧閫�鏂� - 鏇存柊宸ュ崟琛�(WOMDAB)鐩稿叧鏁伴噺
+                if (C_MES_ITEM_TBL.Tbl005 == "鑹搧閫�鏂�")
+                    totalResult += Db.Updateable<Womdab>()
+                        .SetColumns(it => new Womdab
+                        {
+                            Dab007 = it.Dab007 -
+                                     c_mes_inv_item_barcodes.Quantity, // 鍑忓皯宸ュ崟鏁伴噺
+                            Dab022 = (it.Dab022 ?? 0) +
+                                     c_mes_inv_item_barcodes.Quantity, // 澧炲姞閫�鏂欐暟閲�
+                            LpTl = (it.LpTl ?? 0) +
+                                   (int)c_mes_inv_item_barcodes
+                                       .Quantity, // 澧炲姞鑹搧閫�鏂欐暟閲�
+                            Dab020 = (it.Dab020 ?? 0) -
+                                     c_mes_inv_item_barcodes.Quantity // 鍑忓皯宸插彂鏂欐暟閲�
+                        })
+                        .Where(it => it.Dab001 == c_mes_inv_item_barcodes.WorkNo
+                                     && it.Dab002 == c_mes_inv_item_barcodes
+                                         .WorkLine
+                                     && it.Dab003 == c_mes_inv_item_barcodes
+                                         .ItemId.ToString())
+                        .ExecuteCommand();
+                // 鏉ユ枡涓嶈壇閫�鏂� - 鏇存柊宸ュ崟琛�(WOMDAB)鐩稿叧鏁伴噺
+                else if (C_MES_ITEM_TBL.Tbl005 == "鏉ユ枡涓嶈壇閫�鏂�")
+                    totalResult += Db.Updateable<Womdab>()
+                        .SetColumns(it => new Womdab
+                        {
+                            Dab007 = it.Dab007 -
+                                     c_mes_inv_item_barcodes.Quantity, // 鍑忓皯宸ュ崟鏁伴噺
+                            Dab022 = (it.Dab022 ?? 0) +
+                                     c_mes_inv_item_barcodes.Quantity, // 澧炲姞閫�鏂欐暟閲�
+                            LlBl = (it.LlBl ?? 0) +
+                                   (int)c_mes_inv_item_barcodes
+                                       .Quantity, // 澧炲姞鏉ユ枡涓嶈壇鏁伴噺
+                            Dab020 = (it.Dab020 ?? 0) -
+                                     c_mes_inv_item_barcodes.Quantity // 鍑忓皯宸插彂鏂欐暟閲�
+                        })
+                        .Where(it => it.Dab001 == c_mes_inv_item_barcodes.WorkNo
+                                     && it.Dab002 == c_mes_inv_item_barcodes
+                                         .WorkLine
+                                     && it.Dab003 == c_mes_inv_item_barcodes
+                                         .ItemId.ToString())
+                        .ExecuteCommand();
+
+                // 鏇存柊閫�鏂欏崟鏄庣粏琛ㄥ凡閫�鏁伴噺
+                totalResult += Db.Updateable<MesItemTblDetail>()
+                    .SetColumns(it => new MesItemTblDetail
+                    {
+                        Tld006 = (it.Tld006 ?? 0) +
+                                 (int)c_mes_inv_item_barcodes.Quantity // 澧炲姞宸查��鏁伴噺
+                    })
+                    .Where(it => it.Tlmid == C_MES_ITEM_TBL.Id
+                                 && it.Tld009 == c_mes_inv_item_barcodes.ItemId)
+                                 //&& it.Tld010 ==
+                                 //c_mes_inv_item_barcodes.WorkLine)
+                    .ExecuteCommand();
+            }
+            // 浣滀笟涓嶈壇閫�鏂� - 鏇存柊宸ュ崟琛ㄥ拰閫�鏂欏崟鏄庣粏琛�
+            else if (C_MES_ITEM_TBL.Tbl005 == "浣滀笟涓嶈壇閫�鏂�")
+            {
+                totalResult += Db.Updateable<Womdab>()
+                    .SetColumns(it => new Womdab
+                    {
+                        Dab022 = (it.Dab022 ?? 0) +
+                                 c_mes_inv_item_barcodes.Quantity, // 澧炲姞閫�鏂欐暟閲�
+                        ZyBl = (it.ZyBl ?? 0) +
+                               (int)c_mes_inv_item_barcodes
+                                   .Quantity, // 澧炲姞浣滀笟涓嶈壇鏁伴噺
+                        Dab020 = (it.Dab020 ?? 0) -
+                                 c_mes_inv_item_barcodes.Quantity // 鍑忓皯宸插彂鏂欐暟閲�
+                    })
+                    .Where(it => it.Dab001 == c_mes_inv_item_barcodes.WorkNo
+                                 && it.Dab002 ==
+                                 c_mes_inv_item_barcodes.WorkLine
+                                 && it.Dab003 == c_mes_inv_item_barcodes.ItemId
+                                     .ToString())
+                    .ExecuteCommand();
+
+                // 鏇存柊閫�鏂欏崟鏄庣粏琛ㄥ凡閫�鏁伴噺
+                totalResult += Db.Updateable<MesItemTblDetail>()
+                    .SetColumns(it => new MesItemTblDetail
+                    {
+                        Tld006 = (it.Tld006 ?? 0) +
+                                 (int)c_mes_inv_item_barcodes.Quantity // 澧炲姞宸查��鏁伴噺
+                    })
+                    .Where(it => it.Tlmid == C_MES_ITEM_TBL.Id
+                                 && it.Tld009 == c_mes_inv_item_barcodes.ItemId)
+                                 //&& it.Tld010 ==
+                                 //c_mes_inv_item_barcodes.WorkLine)
+                    .ExecuteCommand();
+            }
+
+            // 濡傛灉寰呴��鏁伴噺绛変簬鏈閫�鏂欐暟閲忥紝鍒欐洿鏂版槑缁嗗畬鎴愮姸鎬�
+            if ((C_MES_ITEM_TBL_DETAIL.Tld005 ?? 0) -
+                (C_MES_ITEM_TBL_DETAIL.Tld006 ?? 0) ==
+                c_mes_inv_item_barcodes.Quantity)
+                totalResult += Db.Updateable<MesItemTblDetail>()
+                    .SetColumns(it => new MesItemTblDetail
+                        { Tld008 = 1 }) // 璁剧疆瀹屾垚鏍囧織
+                    .Where(it => it.Tlid == C_MES_ITEM_TBL_DETAIL.Tlid)
+                    .ExecuteCommand();
+
+            // 妫�鏌ラ��鏂欏崟鏄惁鎵�鏈夋槑缁嗛兘宸插畬鎴�
+            var remainingCount = Db.Queryable<MesItemTbl, MesItemTblDetail>(
+                    (a, b) =>
+                        new JoinQueryInfos(JoinType.Left, a.Id == b.Tlmid))
+                .Where((a, b) =>
+                    a.BillNo == p_bill_no &&
+                    (b.Tld005 ?? 0) - (b.Tld006 ?? 0) > 0)
+                .Count();
+
+            // 濡傛灉鎵�鏈夋槑缁嗛兘宸插畬鎴愶紝鍒欐洿鏂伴��鏂欏崟鐘舵�佷负宸插畬鎴�
+            if (remainingCount < 1)
+                totalResult += Db.Updateable<MesItemTbl>()
+                    .SetColumns(it => it.Tbl020 == 1) // 璁剧疆瀹屾垚鏍囧織
+                    .Where(it => it.BillNo == p_bill_no)
+                    .ExecuteCommand();
+
+            // 妫�鏌ュ繀瑕佺殑鎻掑叆鎿嶄綔鏄惁閮芥垚鍔熸墽琛�
+            var minimumExpectedOperations = 3; // 鑷冲皯闇�瑕佹墽琛岀殑鎻掑叆鎿嶄綔鏁�
+            if (totalResult < minimumExpectedOperations)
+                throw new Exception(
+                    $"鍏抽敭鏁版嵁鎻掑叆澶辫触锛岄鏈熻嚦灏憑minimumExpectedOperations}涓搷浣滐紝瀹為檯鎵ц{totalResult}涓搷浣�");
+
+            // 鍒涘缓 鎻掑叆鏃ュ織
+            var logService = new LogService();
+            var LogMsg = "[PDA]濮斿閫�鏂欍�傛潯鐮併��" + query.barcode + "銆� 閫�鏂欏崟鍙枫��" + c_bill_no + "銆�";
+            logService.CreateLog(db, query.userName, wwgd.Id.ToString(), "WW_GD", LogMsg, wwgd.Daa001);
+
+            return totalResult;
+        });
+
+        query.itemNo = c_mes_inv_item_barcodes.ItemNo;
+        query.Num = c_quantity.Value;
+        return query;
+    }
+
+    #endregion
 
     #region 濮斿琛ユ枡
 
     /// <summary>
-    /// 濮斿宸ュ崟閫�鏂欐壂鐮�
+    /// 濮斿宸ュ崟琛ユ枡鎵爜
     ///     鎵弿鏉$爜  prc_rf_pda_scan_zout_barcode3
     /// </summary>
     /// <param name="query">鏌ヨ鍙傛暟</param>
@@ -283,7 +774,7 @@
         // 鑾峰彇琛ユ枡鍗曟槑缁嗗苟鏍¢獙
         var blDetail = Db.Queryable<MesItemBlDetail>()
             .Where(b =>
-                b.Mid == mesItemBl.Id && b.Bld012 == stockBarcode.ItemId)
+                b.Mid == mesItemBl.Id && b.Bld012 == stockBarcode.ItemId && b.Bld007 > b.Bld008)
             .First();
 
         if (blDetail == null)
@@ -299,6 +790,11 @@
             .Where(a => a.Daa001 == query.billNo)
             .First();
 
+        // 妫�鏌ュ伐鍗曚俊鎭�
+        var WWRWD = Db.Queryable<ProductionOrder>()
+            .Where(a => a.OrderNo == wwgd.Daa014)
+            .First();
+
         if (wwgd == null)
             throw new Exception($"宸ュ崟 {query.billNo} 涓嶅瓨鍦紝璇风‘璁わ紒");
 
@@ -309,6 +805,11 @@
 
         if (womdab == null)
             throw new Exception($"澶囨枡鏄庣粏涓嶅瓨鍦ㄦ鐗╂枡 {stockBarcode.ItemNo} 璇风‘璁わ紒");
+
+
+        var depots = Db.Queryable<MesDepots>()
+            .Where(t => t.DepotId == stockBarcode.DepotId)
+            .First();
 
         if (stockBarcode.Quantity > quantity)
         {
@@ -338,13 +839,24 @@
         // 寮�鍚簨鍔″鐞�
         var success = UseTransaction(db =>
         {
+            //query.Type = "濮斿琛ユ枡";
+            var outNoType = "WWBL(濮斿琛ユ枡)";
+            if (query.Type == "濮斿琛ユ枡")
+            {
+                outNoType = "WWBL(濮斿琛ユ枡)";
+            }
+            else
+            {
+                outNoType = "WWCL(濮斿瓒呴)";
+            }
+            //outNoType = "WWBL(濮斿琛ユ枡)";
             // 鑾峰彇鎴栧垱寤哄嚭搴撳崟
             var outId = Guid.NewGuid();
-            var outNo = BillNo.GetBillNo("BL(宸ュ崟琛ユ枡)");
+            var outNo = BillNo.GetBillNo(outNoType);
 
             var existingOut = db.Queryable<MesInvItemOuts>()
-                .Where(a => a.BbillNo == query.blNo
-                            && a.DepotCode == womdab.Dab017
+                .Where(a => a.TaskNo == query.blNo
+                            && a.DepotId == stockBarcode.DepotId
                             && a.OutDate.Value.Date.ToString("yyyy-MM-dd") ==
                             DateTime.Now.Date.ToString("yyyy-MM-dd")
                             && a.BillTypeId == 200
@@ -372,33 +884,36 @@
                     BillTypeId = 200,
                     TransactionNo = 209,
                     Remark = mesItemBl.Bl007,
-                    DepotCode = womdab.Dab017,
-                    OutPart = wwgd.Daa013.ToString(),
+                    DepotCode = depots.DepotCode,
+                    OutPart = wwgd.DepartId.ToString(),
+                    OutType = query.Type,
                     FType = 0,
                     Factory = stockBarcode.Factory,
                     Company = stockBarcode.Company,
-                    // WorkNo = wwgd.Daa021,
-                    // BoardItem = wwgd.Daa002,
+                    WorkNo = WWRWD.ErpProductionOrderNo,
+                    BoardItem = wwgd.Daa003.ToString(),
                     PbillNo = wwgd.Daa001,
                     OutDate = DateTime.Now,
                     Status = 0,
-                    BbillNo = query.blNo
+                    DepotId = stockBarcode.DepotId,
+                    THORGID = stockBarcode.StockOrgId,
+                    //BbillNo = query.billNo
                 }).IgnoreColumns(true).ExecuteCommand();
             }
 
             // 妫�鏌ュ苟鏇存柊鍑哄簱鍗曠墿鏂欐槑缁�
             var itemCount = db.Queryable<MesInvItemOutItems>()
                 .Where(i =>
-                    i.ItemOutId == outId && i.ItemId == stockBarcode.ItemId)
+                    i.ItemOutId == outId && i.ItemId == stockBarcode.ItemId && i.ItemOutId == outId && i.DepotId == stockBarcode.DepotsId.ToString())
                 .Count();
 
             if (itemCount > 0)
                 // 鏇存柊宸插瓨鍦ㄧ殑鐗╂枡鏄庣粏鏁伴噺
                 db.Updateable<MesInvItemOutItems>()
                     .SetColumns(i =>
-                        i.Quantity == i.Quantity + stockBarcode.Quantity)
+                        i.TlQty == i.TlQty + stockBarcode.Quantity)
                     .Where(i =>
-                        i.ItemOutId == outId && i.ItemId == stockBarcode.ItemId)
+                        i.ItemOutId == outId && i.ItemId == stockBarcode.ItemId && i.ItemOutId == outId && i.DepotId == stockBarcode.DepotsId.ToString())
                     .ExecuteCommand();
             else
                 // 鎻掑叆鏂扮殑鐗╂枡鏄庣粏璁板綍
@@ -408,21 +923,25 @@
                     ItemOutId = outId,
                     ItemNo = blDetail.Bld002,
                     Quantity = stockBarcode.Quantity,
+                    TlQty = stockBarcode.Quantity,
                     CreateBy = query.userName,
                     CreateDate = DateTime.Now,
                     LastupdateBy = query.userName,
                     LastupdateDate = DateTime.Now,
                     Factory = stockBarcode.Factory,
                     Company = stockBarcode.Company,
-                    DepotCode = womdab.Dab017,
+                    DepotCode = depots.DepotCode,
                     TaskNo = query.blNo,
-                    // WorkNo = wwgd.Daa021,
+                    WorkNo = WWRWD.ErpProductionOrderNo,
                     WorkLine = blDetail.Bld013,
                     ErpItemNo = womdab.Dab003.ToString(),
                     ErpId = womdab.Eid,
                     ErpAutoid = womdab.Erpid,
                     PbillNo = query.billNo,
-                    ItemId = blDetail.Bld012
+                    ItemId = blDetail.Bld012,
+                    DepotId = stockBarcode.DepotsId.ToString(),
+                    ItemDabid = blDetail.Id,
+                    //AboutGuid = womdab.Id
                     // Unit = blDetail.Bld009,
                     // DepotId = (int)stockBarcode.DepotsId
                 }).IgnoreColumns(true).ExecuteCommand();
@@ -441,7 +960,7 @@
                 CreateDate = DateTime.Now,
                 LastupdateBy = query.userName,
                 LastupdateDate = DateTime.Now,
-                DepotCode = stockBarcode.DepotsCode,
+                DepotCode = depots.DepotCode,
                 DepotSectionCode = stockBarcode.DepotSectionsCode,
                 Remark = blDetail.Bld010,
                 Factory = stockBarcode.Factory,
@@ -455,7 +974,7 @@
                 PbillNo = query.billNo,
                 ItemId = blDetail.Bld012,
                 Unit = blDetail.Bld009,
-                DepotId = (int)stockBarcode.DepotsId,
+                DepotId = (int)stockBarcode.DepotId,
                 EbelnK3id = womdab.Eid,
                 LineK3id = womdab.Erpid
             }).IgnoreColumns(true).ExecuteCommand();
@@ -466,7 +985,7 @@
                 Guid = Guid.NewGuid(),
                 Status = 1,
                 BillTypeId = 200, // p_bill_type_id
-                TransactionCode = "210", // p_transaction_no
+                TransactionCode = "209", // p_transaction_no
                 BusinessType = -1,
                 ItemBarcode = stockBarcode.ItemBarcode,
                 ItemNo = stockBarcode.ItemNo,
@@ -501,6 +1020,7 @@
                 })
                 .Where(it => it.Id == womdab.Id && it.Dab003 == womdab.Dab003)
                 .ExecuteCommand();
+
 
             // 鏇存柊琛ユ枡鍗曟槑缁嗗凡琛ユ暟閲�
             db.Updateable<MesItemBlDetail>()
@@ -549,6 +1069,11 @@
                     .Where(it => it.Id == mesItemBl.Id)
                     .ExecuteCommand();
 
+            // 鍒涘缓 鎻掑叆鏃ュ織
+            var logService = new LogService();
+            var LogMsg = "[PDA]"+query.Type + "銆傛潯鐮併��" +query.barcode+"銆� 鍑哄簱鍗曞彿銆�"+ outNo +"銆�";
+            logService.CreateLog(db,query.userName,wwgd.Id.ToString(), "WW_GD", LogMsg,wwgd.Daa001);
+
             return 1;
         });
 
@@ -572,5 +1097,475 @@
         return (query, finalPendingList);
     }
 
+    /// <summary>
+    ///     鐢熶骇琛ユ枡鍗曟潯鐮佹媶鍒� prc_rf_pda_prnt_zout_barcode2
+    /// </summary>
+    /// <param name="query">鏌ヨ鍙傛暟</param>
+    /// <returns>(鎴愬姛鏍囧織, 寰呭鐞嗗垪琛�)</returns>
+    /// <remarks>
+    ///     鍓嶅彴闇�瑕佷紶鍏ョ殑鍙傛暟:
+    ///     - userName: 鐢ㄦ埛鍚�(蹇呭~)
+    ///     - billNo: 宸ュ崟鍙�(蹇呭~)
+    ///     - barcode: 鐗╂枡鏉$爜(蹇呭~)
+    ///     - Num: 鍙戞枡鏁伴噺(蹇呭~,蹇呴』澶т簬0)
+    ///     - blNo: 琛ユ枡鍗曞彿(蹇呭~)
+    /// </remarks>
+    public (bool success, List<MesItemBlDetail> pendingList) SplitBarcode(
+        WarehouseQuery query)
+    {
+        if (string.IsNullOrEmpty(query.userName))
+            throw new Exception("鐢ㄦ埛鍚嶄笉鑳戒负绌猴紒");
+
+        if (string.IsNullOrEmpty(query.billNo))
+            throw new Exception("璇烽�夊彇鍗曟嵁鍙凤紒");
+
+        if (string.IsNullOrEmpty(query.barcode))
+            throw new Exception("璇锋壂鎻忔潯鐮侊紒");
+
+        if (query.Num <= 0)
+            throw new Exception("璇疯緭鍏ユ纭殑鍙戞枡鏁伴噺锛�");
+
+        if (string.IsNullOrEmpty(query.blNo))
+            throw new Exception("琛ユ枡鍗曞彿涓嶈兘涓虹┖锛�");
+
+
+        // 妫�楠屾槸鍚﹂噸澶嶆壂鎻�
+        var exists = Db.Queryable<MesInvItemOutCDetails>()
+            .Where(b => b.ItemBarcode == query.barcode)
+            .Any();
+
+        if (exists)
+            throw new Exception("姝ゆ潯鐮佸凡鎵弿,鍕块噸澶嶆壂鐮侊紒");
+
+        // 鏌ヨ鏉$爜搴撳瓨淇℃伅
+        var stockBarcode = Db.Queryable<MesInvItemStocks>()
+            .Where(t => t.ItemBarcode == query.barcode && t.Quantity > 0)
+            .First();
+
+        if (stockBarcode == null)
+            throw new Exception($"搴撳瓨涓棤姝ゆ潯鐮�,璇锋牳瀵癸紒{query.barcode}");
+
+        var totalQty = stockBarcode.Quantity;
+        string newBarcode = null;
+
+        // 寮�鍚簨鍔″鐞�
+        var success = UseTransaction(db =>
+        {
+            var executeCommand = 0;
+
+            // 鎷嗗垎鏉$爜
+            if (totalQty > query.Num)
+            {
+                var mesItems = db.Queryable<MesItems>()
+                    .Where(s => s.Id == stockBarcode.ItemId).First();
+
+                // 鐢熸垚鏂版潯鐮佸彿
+                newBarcode = BillNo.GetBillNo("TM(鏉$爜)", mesItems.ItemNo);
+
+                // 鍐欏叆鏂版潯鐮�
+                executeCommand += db.Insertable(new MesInvItemBarcodes
+                {
+                    Guid = Guid.NewGuid(),
+                    ItemBarcode = newBarcode,
+                    CustNo = stockBarcode.CustomerNo,
+                    // ProductCode = stockBarcode.ProductCode, 
+                    // ItemBarcode2 = stockBarcode.ItemBarcode2,
+                    // ItemCode = stockBarcode.ItemCode,
+                    ItemNo = stockBarcode.ItemNo,
+                    LotNo = stockBarcode.LotNo,
+                    Quantity = query.Num,
+                    EpFlag = true,
+                    TaskNo = stockBarcode.TaskNo,
+                    CreateBy = query.userName,
+                    CreateDate = DateTime.Now,
+                    LastupdateBy = query.userName,
+                    LastupdateDate = DateTime.Now,
+                    OldItemBarcode = query.barcode,
+                    // Mblnr = stockBarcode.Mblnr,
+                    // Zeile = stockBarcode.Zeile,
+                    // RohInId = stockBarcode.RohInId,
+                    Barcodestatus = false,
+                    Oldqty = query.Num as long?,
+                    // Unit = stockBarcode.Unit,
+                    // WeightUnit = stockBarcode.WeightUnit,
+                    Factory = stockBarcode.Factory,
+                    Company = stockBarcode.Company,
+                    BillNo = stockBarcode.BillNo,
+                    BoardStyle = stockBarcode.BoardStyle,
+                    // ColorName = stockBarcode.ColorName,
+                    WorkNo = stockBarcode.WorkNo,
+                    WorkLine = stockBarcode.WorkLine,
+                    // MemoBad = stockBarcode.MemoBad,
+                    ComeFlg = 5,
+                    // Memo = stockBarcode.Memo,
+                    SuppId = stockBarcode.SuppId,
+                    SuppNo = stockBarcode.SuppNo,
+                    InsDate = stockBarcode.IndepDate, // Added InsDate
+                    ItemId = stockBarcode.ItemId
+                    // ItemUnit = stockBarcode.ItemUnit // Added ItemUnit
+                }).IgnoreColumns(true).ExecuteCommand();
+
+                // 鏇存柊鍘熸潯鐮佹暟閲�
+                executeCommand += db.Updateable<MesInvItemBarcodes>()
+                    .SetColumns(it => it.Quantity == totalQty - query.Num)
+                    .Where(it => it.ItemBarcode == query.barcode)
+                    .ExecuteCommand();
+
+                // 鍒犻櫎鍘熸潯鐮佸簱瀛樿褰�
+                executeCommand += db.Deleteable<MesInvItemStocks>()
+                    .Where(it => it.ItemBarcode == query.barcode)
+                    .ExecuteCommand();
+
+                // 鎻掑叆鍓╀綑鏉$爜鏁伴噺鐨勬柊搴撳瓨璁板綍
+                executeCommand += db.Insertable(new MesInvItemStocks
+                {
+                    Guid = Guid.NewGuid(),
+                    TaskNo = stockBarcode.TaskNo,
+                    ItemBarcode = stockBarcode.ItemBarcode,
+                    ItemNo = stockBarcode.ItemNo,
+                    LotNo = stockBarcode.LotNo,
+                    Quantity = totalQty - query.Num,
+                    EpFlag = stockBarcode.EpFlag,
+                    CustomerNo = stockBarcode.CustomerNo,
+                    ItemWt = stockBarcode.ItemWt,
+                    DepotsCode = stockBarcode.DepotsCode,
+                    DepotsId = stockBarcode.DepotsId,
+                    DepotSectionsCode = stockBarcode.DepotSectionsCode,
+                    CheckDate = stockBarcode.CheckDate,
+                    ItemType = stockBarcode.ItemType,
+                    IndepDate = stockBarcode.IndepDate,
+                    Factory = stockBarcode.Factory,
+                    Company = stockBarcode.Company,
+                    IqcStatus = stockBarcode.IqcStatus,
+                    BoardStyle = stockBarcode.BoardStyle,
+                    WorkNo = stockBarcode.WorkNo,
+                    WorkLine = stockBarcode.WorkLine,
+                    SuppNo = stockBarcode.SuppNo,
+                    ItemId = stockBarcode.ItemId
+                    // UnitId = stockBarcode.ItemUnit
+                }).IgnoreColumns(true).ExecuteCommand();
+
+                // 鍐欏叆鏂版潯鐮佺殑浜ゆ槗璁板綍
+                executeCommand += db.Insertable(new MesInvBusiness2
+                {
+                    Guid = Guid.NewGuid(),
+                    Status = 1,
+                    BillTypeId = 200, // p_bill_type_id
+                    TransactionCode = "220", // p_transaction_no
+                    BusinessType = -1,
+                    ItemBarcode = newBarcode,
+                    ItemNo = stockBarcode.ItemNo,
+                    LotNo = stockBarcode.LotNo,
+                    EpFlag = true,
+                    Quantity = query.Num,
+                    FromInvDepotsCode = null,
+                    FromInvDepotSectionsCode = null,
+                    ToInvDepotsCode = stockBarcode.DepotsCode,
+                    ToInvDepotSectionsCode = stockBarcode.DepotSectionsCode,
+                    Description = null,
+                    CreateBy = query.userName,
+                    CreateDate = DateTime.Now,
+                    LastupdateBy = query.userName,
+                    LastupdateDate = DateTime.Now,
+                    Factory = stockBarcode.Factory,
+                    Company = stockBarcode.Company,
+                    TaskNo = stockBarcode.TaskNo,
+                    BillNo = stockBarcode.BillNo,
+                    WorkNo = stockBarcode.WorkNo,
+                    WorkLine = stockBarcode.WorkLine,
+                    SuppNo = stockBarcode.SuppNo,
+                    SuppId = stockBarcode.SuppId,
+                    ItemId = stockBarcode.ItemId
+                    // CkDepot = stockBarcode.DepotsId
+                }).IgnoreColumns(true).ExecuteCommand();
+            }
+            else if (totalQty < query.Num)
+            {
+                throw new Exception("鍙戞枡鏁伴噺澶т簬鏉$爜鏁�,璇锋牳瀵癸紒");
+            }
+
+            if (string.IsNullOrEmpty(newBarcode)) newBarcode = query.barcode;
+
+            // 妫�鏌ヨˉ鏂欏崟鐘舵��
+            var mesItemBl = Db.Queryable<MesItemBl>()
+                .Where(a => a.BlNo == query.blNo && (a.Bl018 ?? false) == false)
+                .First();
+
+            if (mesItemBl == null)
+                throw new Exception($"鐢宠鍗� {query.blNo} 宸叉挙鍥烇紒");
+
+            if (mesItemBl.Bl018 != true)
+                throw new Exception($"鐢宠鍗� {query.blNo} 鏈鏍革紒");
+
+            if (mesItemBl.Bl019 == true)
+                throw new Exception($"鐢宠鍗� {query.blNo} 宸插畬缁擄紒");
+
+            // 鑾峰彇琛ユ枡鍗曟槑缁嗗苟鏍¢獙
+            var blDetail = Db.Queryable<MesItemBlDetail>()
+                .Where(b =>
+                    b.Mid == mesItemBl.Id && b.Bld012 == stockBarcode.ItemId)
+                .First();
+
+            if (blDetail == null)
+                throw new Exception($"鐢宠鍗曚笉瀛樺湪姝ょ墿鏂� {stockBarcode.ItemNo} 璇风‘璁わ紒");
+
+            var remainingQty = (blDetail.Bld007 ?? 0) - (blDetail.Bld008 ?? 0);
+            if (remainingQty == 0)
+                throw new Exception("鐗╂枡宸叉壂鐮佸畬鎴愶紝璇锋牳瀵癸紒");
+
+            if (query.Num > remainingQty)
+                throw new Exception(
+                    $"鎷嗗垎鏁伴噺锛歿query.Num} 澶т簬寰呭彂鏂欐暟閲忥細{remainingQty}锛岃鏍稿锛�");
+
+            // 妫�鏌ュ伐鍗曚俊鎭�
+            var womdaa = Db.Queryable<WwGd>()
+                .Where(a => a.Daa001 == query.billNo)
+                .First();
+
+            if (womdaa == null)
+                throw new Exception($"宸ュ崟 {query.billNo} 涓嶅瓨鍦紝璇风‘璁わ紒");
+
+            var womdab = Db.Queryable<WwGdDetail>()
+                .Where(b =>
+                    b.Dab001 == query.billNo && b.Erpid == blDetail.Bld014)
+                .First();
+
+            if (womdab == null)
+                throw new Exception($"澶囨枡鏄庣粏涓嶅瓨鍦ㄦ鐗╂枡 {stockBarcode.ItemNo} 璇风‘璁わ紒");
+
+            // 妫�鏌ュ凡鍙戞枡鏁伴噺鏄惁瓒呰繃寰呭彂鏂欐暟閲�
+            var sumQty = db.Queryable<MesInvItemOutCDetails>()
+                .Where(it =>
+                    it.TaskNo == query.blNo && it.ItemId == stockBarcode.ItemId)
+                .Sum(it => it.Quantity);
+
+            if (sumQty > remainingQty)
+                throw new Exception(
+                    $"鎷嗗垎鏁伴噺锛歿sumQty} 澶т簬寰呭彂鏂欐暟閲忥細{remainingQty}锛岃鏍稿锛�");
+
+            // 鑾峰彇鎴栧垱寤哄嚭搴撳崟
+            var itemOut = db.Queryable<MesInvItemOuts>()
+                .Where(a => a.BbillNo == query.blNo
+                            && a.DepotCode == womdab.Dab017
+                            && a.OutDate.Value.Date.ToString("yyyy-MM-dd") ==
+                            DateTime.Now.Date.ToString("yyyy-MM-dd")
+                            && a.BillTypeId == 200
+                            && a.TransactionNo == 220
+                            && a.Status == 0)
+                .First();
+
+            if (itemOut == null)
+            {
+                // 鍒涘缓鏂扮殑鍑哄簱鍗�
+                var outId = Guid.NewGuid();
+                var outNo = BillNo.GetBillNo("BL(宸ュ崟琛ユ枡)");
+
+                // 鎻掑叆鍑哄簱鍗曚富琛�
+                executeCommand += db.Insertable(new MesInvItemOuts
+                {
+                    Guid = outId,
+                    ItemOutNo = outNo,
+                    TaskNo = query.blNo,
+                    CreateBy = query.userName,
+                    CreateDate = DateTime.Now,
+                    LastupdateBy = query.userName,
+                    LastupdateDate = DateTime.Now,
+                    BillTypeId = 200,
+                    TransactionNo = 220,
+                    Remark = mesItemBl.Bl007,
+                    DepotCode = womdab.Dab017,
+                    OutPart = womdaa.Daa013.ToString(),
+                    FType = 0,
+                    Factory = stockBarcode.Factory,
+                    Company = stockBarcode.Company,
+                    // WorkNo = womdaa.Daa021,
+                    // BoardItem = womdaa.Daa002,
+                    PbillNo = womdaa.Daa001,
+                    OutDate = DateTime.Now,
+                    Status = 0,
+                    BbillNo = query.blNo
+                }).IgnoreColumns(true).ExecuteCommand();
+            }
+
+            // 妫�鏌ユ槸鍚﹀凡瀛樺湪鍑哄簱鍗曟槑缁�
+            var itemOutItemCount = db.Queryable<MesInvItemOutItems>()
+                .Where(it =>
+                    it.ItemOutId == itemOut.Guid &&
+                    it.ItemId == stockBarcode.ItemId)
+                .Count();
+
+            if (itemOutItemCount == 0)
+                // 鎻掑叆鏂扮殑鍑哄簱鍗曟槑缁�
+                executeCommand += db.Insertable(new MesInvItemOutItems
+                {
+                    Guid = Guid.NewGuid(),
+                    ItemOutId = itemOut.Guid,
+                    ItemNo = blDetail.Bld002,
+                    Quantity = query.Num,
+                    CreateBy = query.userName,
+                    CreateDate = DateTime.Now,
+                    LastupdateBy = query.userName,
+                    LastupdateDate = DateTime.Now,
+                    Factory = stockBarcode.Factory,
+                    Company = stockBarcode.Company,
+                    DepotCode = womdab.Dab017,
+                    TaskNo = query.blNo,
+                    // WorkNo = womdaa.Daa021,
+                    WorkLine = blDetail.Bld013,
+                    ErpItemNo = womdab.Dab003.ToString(),
+                    ErpId = womdab.Eid,
+                    ErpAutoid = womdab.Erpid,
+                    PbillNo = query.billNo,
+                    ItemId = blDetail.Bld012
+                    // Unit = blDetail.Bld009,
+                    // DepotId = (int)stockBarcode.DepotsId
+                }).IgnoreColumns(true).ExecuteCommand();
+            else
+                // 鏇存柊宸叉湁鍑哄簱鍗曟槑缁嗘暟閲�
+                executeCommand += db.Updateable<MesInvItemOutItems>()
+                    .SetColumns(it => it.Quantity == it.Quantity + query.Num)
+                    .Where(it =>
+                        it.ItemOutId == itemOut.Guid &&
+                        it.ItemId == stockBarcode.ItemId)
+                    .ExecuteCommand();
+
+            // 鎻掑叆鍑哄簱鏉$爜鏄庣粏
+            executeCommand += db.Insertable(new MesInvItemOutCDetails
+            {
+                Guid = Guid.NewGuid(),
+                ItemOutId = itemOut.Guid,
+                ItemBarcode = newBarcode ?? query.barcode,
+                ItemNo = stockBarcode.ItemNo,
+                LotNo = stockBarcode.LotNo,
+                Quantity = query.Num,
+                ForceOutFlag = 0,
+                CreateBy = query.userName,
+                CreateDate = DateTime.Now,
+                LastupdateBy = query.userName,
+                LastupdateDate = DateTime.Now,
+                DepotCode = stockBarcode.DepotsCode,
+                DepotSectionCode = stockBarcode.DepotSectionsCode,
+                Remark = blDetail.Bld010,
+                Factory = stockBarcode.Factory,
+                Company = stockBarcode.Company,
+                TaskNoy = mesItemBl.Bl013,
+                BoardStyle = mesItemBl.Bl002,
+                TaskNo = query.blNo,
+                WorkNo = blDetail.Bld001,
+                WorkLine = blDetail.Bld013,
+                SuppNo = stockBarcode.SuppNo,
+                PbillNo = query.billNo,
+                ItemId = blDetail.Bld012,
+                Unit = blDetail.Bld009,
+                DepotId = (int)stockBarcode.DepotsId
+            }).IgnoreColumns(true).ExecuteCommand();
+
+            // 鎻掑叆涓氬姟娴佹按
+            executeCommand += db.Insertable(new MesInvBusiness2
+            {
+                Guid = Guid.NewGuid(),
+                Status = 1,
+                BillTypeId = 200, // p_bill_type_id
+                TransactionCode = "210", // p_transaction_no 
+                BusinessType = 1,
+                ItemBarcode = newBarcode ?? query.barcode,
+                ItemNo = stockBarcode.ItemNo,
+                LotNo = stockBarcode.LotNo,
+                EpFlag = true,
+                Quantity = query.Num,
+                FromInvDepotsCode = stockBarcode.DepotsCode,
+                FromInvDepotSectionsCode = stockBarcode.DepotSectionsCode,
+                Description = null,
+                CreateBy = query.userName,
+                CreateDate = DateTime.Now,
+                LastupdateBy = query.userName,
+                LastupdateDate = DateTime.Now,
+                Factory = stockBarcode.Factory,
+                Company = stockBarcode.Company,
+                TaskNo = mesItemBl.Bl012,
+                BillNo = query.blNo,
+                WorkNo = blDetail.Bld001,
+                WorkLine = blDetail.Bld013,
+                SuppNo = stockBarcode.SuppNo,
+                ItemId = stockBarcode.ItemId
+                // CkDepot = stockBarcode.DepotsId
+            }).IgnoreColumns(true).ExecuteCommand();
+
+            // 鏇存柊宸ュ崟琛ㄦ暟閲�
+            executeCommand += db.Updateable<WwGdDetail>()
+                .SetColumns(it => new WwGdDetail
+                {
+                    Dab007 = (it.Dab007 ?? 0) + (int)query.Num, // 宸ュ崟鏁伴噺
+                    Dab020 = (it.Dab020 ?? 0) + (int)query.Num, // 宸插彂鏂欐暟閲�
+                    Dab021 = (it.Dab021 ?? 0) + (int)query.Num // 宸插彂鏂欐暟閲�
+                })
+                .Where(it => it.Id == womdab.Id && it.Dab003 == womdab.Dab003)
+                .IgnoreColumns(true)
+                .ExecuteCommand();
+
+            // 鏇存柊琛ユ枡鍗曟槑缁嗗凡琛ユ暟閲�
+            executeCommand += db.Updateable<MesItemBlDetail>()
+                .SetColumns(it => new MesItemBlDetail
+                {
+                    Bld008 = (it.Bld008 ?? 0) + (int)query.Num
+                })
+                .Where(it => it.Id == blDetail.Id)
+                .ExecuteCommand();
+
+            // 鑾峰彇鏇存柊鍚庣殑琛ユ枡鍗曟槑缁嗘暟閲�
+            var updatedDetail = db.Queryable<MesItemBlDetail>()
+                .Where(it => it.Id == blDetail.Id)
+                .Select(it => new { it.Bld007, it.Bld008 })
+                .First();
+
+            if ((updatedDetail.Bld007 ?? 0) <= (updatedDetail.Bld008 ?? 0))
+                // 鏇存柊鏄庣粏瀹屾垚鐘舵��
+                executeCommand += db.Updateable<MesItemBlDetail>()
+                    .SetColumns(it => new MesItemBlDetail { Bld011 = 1 })
+                    .Where(it => it.Id == blDetail.Id)
+                    .ExecuteCommand();
+
+            // 妫�鏌ユ槸鍚﹁繕鏈夋湭瀹屾垚鐨勬槑缁�
+
+            var unfinishedDetail = db.Queryable<MesItemBlDetail>()
+                .Where(it => it.Mid == mesItemBl.Id && (it.Bld011 ?? 0) == 0)
+                .First();
+
+            if (unfinishedDetail == null)
+                // 濡傛灉娌℃湁鎵惧埌鏈畬鎴愭槑缁嗭紝鍒欐洿鏂拌ˉ鏂欏崟鐘舵�佷负宸插畬鎴�
+                executeCommand += db.Updateable<MesItemBl>()
+                    .SetColumns(it => new MesItemBl
+                    {
+                        Bl019 = true,
+                        WcUser = query.userName,
+                        WcTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
+                    })
+                    .Where(it => it.Id == mesItemBl.Id)
+                    .ExecuteCommand();
+
+            if (executeCommand <= 1) throw new Exception("鏇存柊澶辫触");
+
+            return executeCommand;
+        }) > 0;
+
+        // 鑾峰彇鏈�缁堢殑寰呭彂鏂欐槑缁嗗垪琛�
+        var pendingList = Db.Queryable<MesItemBl, MesItemBlDetail>((a, b) =>
+                new JoinQueryInfos(JoinType.Left, a.Id == b.Mid))
+            .Where((a, b) => a.BlNo == query.blNo
+                             && (b.Bld007 ?? 0) - (b.Bld008 ?? 0) > 0)
+            .Select((a, b) => new MesItemBlDetail
+            {
+                Bld012 = b.Bld012,
+                Bld002 = b.Bld002,
+                Bld003 = b.Bld003,
+                Bld004 = b.Bld004,
+                Bld007 = b.Bld007,
+                Bld008 = b.Bld008
+            })
+            .ToList();
+
+        return (success, pendingList);
+    }
+
     #endregion
 }
\ No newline at end of file

--
Gitblit v1.9.3