service/Wom/WwGdManager.cs
@@ -1,5 +1,6 @@
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks.Dataflow;
using Masuit.Tools;
using NewPdaSqlServer.DB;
using NewPdaSqlServer.Dto.service;
@@ -284,6 +285,8 @@
        if (c_mes_inv_item_barcodes == null)
            throw new Exception("此条码不属于该退料单,请核对!");
        // if (c_mes_inv_item_barcodes.Memo != "生产退料")
        //     throw new Exception("此条码不是生产退料条码,不可使用生产退料模块!");
@@ -296,14 +299,59 @@
        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.Tlmid == C_MES_ITEM_TBL.Id
                        && a.Tld010 == c_mes_inv_item_barcodes.WorkLine)
            .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;
@@ -318,7 +366,7 @@
                .Where(d =>
                    d.Status == 0 && d.TransctionNo ==
                                  p_transction_no.ToString()
                                  && d.CbillNo == c_mes_inv_item_barcodes.BillNo
                                  && d.TaskNo == c_mes_inv_item_barcodes.BillNo
                                  && d.DepotsId == c_depot_code
                                  && d.InsDate.Value.ToString("yyyyMMdd") ==
                                  DateTime.Now.ToString("yyyyMMdd")).First();
@@ -328,7 +376,7 @@
            // 如果入库单不存在则创建新的入库单
            if (mesInvItemIns == null)
            {
                c_bill_no = BillNo.GetBillNo("IN(入库单)");
                c_bill_no = BillNo.GetBillNo("WWTL(委外退料)");
                c_id = Guid.NewGuid();
@@ -337,19 +385,26 @@
                    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_depot_code,
                    DepotsId = c_depot_code,
                    //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 = C_MES_ITEM_TBL.BillNo,
                    RbillNo = C_MES_ITEM_TBL.Tbl002
                    CbillNo = wwgd.Daa001,
                    InType = "委外退料",
                    ReceiveOrgId = c_mes_depots.FSubsidiary,
                    Fstatus = 0,
                    Status = 0,
                    WorkNo = WWRWD.ErpProductionOrderNo
                }).IgnoreColumns(true).ExecuteCommand();
            }
            else
@@ -361,6 +416,57 @@
            // 检查是否为合并打印条码
            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
@@ -396,7 +502,11 @@
                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
                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();
            // 插入业务记录
@@ -438,9 +548,9 @@
                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,
                //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,
@@ -450,7 +560,13 @@
                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
                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();
            // 根据退料类型(良品退料、来料不良退料、作业不良退料)更新相关数据
@@ -507,9 +623,9 @@
                                 (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)
                                 && it.Tld009 == c_mes_inv_item_barcodes.ItemId)
                                 //&& it.Tld010 ==
                                 //c_mes_inv_item_barcodes.WorkLine)
                    .ExecuteCommand();
            }
            // 作业不良退料 - 更新工单表和退料单明细表
@@ -541,9 +657,9 @@
                                 (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)
                                 && it.Tld009 == c_mes_inv_item_barcodes.ItemId)
                                 //&& it.Tld010 ==
                                 //c_mes_inv_item_barcodes.WorkLine)
                    .ExecuteCommand();
            }
@@ -578,6 +694,11 @@
            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;
        });
@@ -653,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)
@@ -819,7 +940,8 @@
                    PbillNo = query.billNo,
                    ItemId = blDetail.Bld012,
                    DepotId = stockBarcode.DepotsId.ToString(),
                    ItemDabid = womdab.Id,
                    ItemDabid = blDetail.Id,
                    //AboutGuid = womdab.Id
                    // Unit = blDetail.Bld009,
                    // DepotId = (int)stockBarcode.DepotsId
                }).IgnoreColumns(true).ExecuteCommand();
@@ -852,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();
@@ -949,7 +1071,7 @@
            // 创建 插入日志
            var logService = new LogService();
            var LogMsg = query.Type + "。条码【" +query.barcode+"】 补料单号【"+ outNo +"】";
            var LogMsg = "[PDA]"+query.Type + "。条码【" +query.barcode+"】 出库单号【"+ outNo +"】";
            logService.CreateLog(db,query.userName,wwgd.Id.ToString(), "WW_GD", LogMsg,wwgd.Daa001);
            return 1;
@@ -1038,7 +1160,7 @@
                    .Where(s => s.Id == stockBarcode.ItemId).First();
                // 生成新条码号
                newBarcode = BillNo.GetBillNo("TMBH(条码编号)", mesItems.ItemNo);
                newBarcode = BillNo.GetBillNo("TM(条码)", mesItems.ItemNo);
                // 写入新条码
                executeCommand += db.Insertable(new MesInvItemBarcodes