南骏 池
3 天以前 4f8d1da89bc7cf399e6d90d6230ad6c28114443a
service/Warehouse/MesInvItemInCDetailsManager.cs
@@ -2,9 +2,12 @@
using NewPdaSqlServer.DB;
using NewPdaSqlServer.Dto.service;
using NewPdaSqlServer.entity;
using NewPdaSqlServer.entity.Base;
using NewPdaSqlServer.service.@base;
using NewPdaSqlServer.util;
using Newtonsoft.Json;
using SqlSugar;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace NewPdaSqlServer.service.Warehouse;
@@ -12,7 +15,7 @@
{
    //当前类已经继承了 Repository 增、删、查、改的方法
    public PurchaseInventory SaveBarCodes(WarehouseQuery entity)
    public ItemInBaseModel SaveBarCodes(WarehouseQuery entity)
    {
        string functionName = "采购入库", fieldName = null, inFieldVal = null;
        string user = entity.userName,
@@ -33,15 +36,15 @@
        if (itemBarcodeDetails == null)
            throw new Exception("条码不存在,请核对!");
        if (itemBarcodeDetails.ComeFlg == 0)
        if (string.IsNullOrEmpty(itemBarcodeDetails.Memo) || itemBarcodeDetails.Memo != "采购入库")
            throw new Exception("此条码不属于到货条码,无法用采购入库!");
        var inventory = Db.Queryable<MesInvItemArn>()
            .Where(it => it.BillNo == itemBarcodeDetails.BillNo)
            .Where(it => it.BillNo == itemBarcodeDetails.BillNo && it.Fstatus == true )
            .First();
        if (inventory == null)
            throw new Exception("此条码找不到对应收货单!");
            throw new Exception("此条码找不到对应收货单或未审核!");
        // var inventoryDetails = Db.Queryable<MesInvItemArnDetail>()
        //     .Where(it => it.ParentGuid == inventory.Guid
@@ -61,6 +64,16 @@
        if (inventoryDetails == null)
            throw new Exception("此条码找不到对应收货单明细!");
        //获取到货检验明细
        var sqlParams = new List<SugarParameter> { new("@dhmxGuid", itemBarcodeDetails.AboutGuid) };
        var sql1 = @"SELECT *FROM v_dhmx WHERE dhmxGuid = @dhmxGuid ";
        var dhjymx = Db.Ado.SqlQuery<vDhmx>(sql1, sqlParams).First();
        //判定检验能否入库
        if (dhjymx.CanStore!= 1)
        {
            throw new Exception($"该条码对应的收料单检验结果:【{dhjymx.InspectionResult}】,判定结果:【{dhjymx.JudgmentResult}】,处理意见:【{dhjymx.HandlingSuggestion}】,无法入库");
        }
        var depotCode = Db.Queryable<MesDepotSections>()
            .Where(it => it.DepotSectionCode == sectionCode)
            .Select(it => it.DepotGuid)
@@ -74,10 +87,10 @@
        MesDepots mesDepost = null;
        var checkGuid = UtilityHelper.CheckGuid(depotCode);
        if (checkGuid && depotCode2 != null)
        if (checkGuid && depotCode != null)
        {
            mesDepost = Db.Queryable<MesDepots>()
                .Where(s => s.DepotId.ToString() == depotCode2).First();
                .Where(s => s.DepotId.ToString() == depotCode).First();
            // if (depotCode != mesDepost.Guid)
            //     throw new Exception("扫描库位与采购入库库位不一致!");
@@ -88,89 +101,111 @@
        }
        num = Db.Queryable<MesInvItemInCDetails>()
            .Where(it => it.ItemBarcode == itemBarcode)
            .Count();
        //num = Db.Queryable<MesInvItemInCDetails>()
        //    .Where(it => it.ItemBarcode == itemBarcode)
        //    .Count();
        if (num > 0)
            throw new Exception("此条码已扫码入库完成,请核对!");
        //if (num > 0)
        //    throw new Exception("此条码已扫码入库完成,请核对!");
        num = Db.Queryable<MesInvItemStocks>()
            .Where(it => it.ItemBarcode == itemBarcode)
            .Count();
        //num = Db.Queryable<MesInvItemStocks>()
        //    .Where(it => it.ItemBarcode == itemBarcode)
        //    .Count();
        if (num > 0)
            throw new Exception("此条码已扫码入库,请核对!");
        //if (num > 0)
        //    throw new Exception("此条码已扫码入库,请核对!");
        num = Db.Queryable<MesInvItemArn>()
            .Where(a => a.BillNo == itemBarcodeDetails.BillNo)
            .InnerJoin<MesInvItemArnDetail>((a, b) =>
                b.ParentGuid == a.Guid &&
                b.ItemId == itemBarcodeDetails.ItemId && b.Ischeck == 1)
            .Count();
        var wmsManager = new WmsBaseMangeer();
        // 新增入库校验(调用存储过程)
        var checkResult = wmsManager.pdaInvJY(
            db: Db,
            edtUserNo: entity.userName,
            barcode: entity.barcode,
            sectionCode: sectionCode,
            stockId: mesDepost.DepotId.ToString(),
            stockOrgId: mesDepost.FSubsidiary,
            billNo: "", // 根据实际单据号传值
            transactionNo: "101" // 事务类型与存储过程匹配
        );
        num2 = Db.Queryable<MesInvItemArn>()
            .Where(a => a.BillNo == itemBarcodeDetails.BillNo)
            .InnerJoin<MesInvItemArnDetail>((a, b) => b.ParentGuid == a.Guid
                && b.ItemId == itemBarcodeDetails.ItemId
                && (b.CheckRes == "合格" || b.CheckStates == "特采直接使用"))
            .Count();
        if (Convert.ToInt32(checkResult.result) < 1)
            throw new Exception($"入库校验失败:{checkResult.strMsg}");
        switch (num)
        {
            case 0 when num2 == 0:
            case > 0 when num2 == 0:
                freeze = 1;
                break;
            case 0 when num2 > 0:
                throw new Exception("入库失败,到货单据有问题!");
        }
        var owner_type = "";
        if (Db.Queryable<SysOrganization>().Any(x => x.Fid == cgddDetails.DemandOrg))
        {
            owner_type = "BD_OwnerOrg";
        }
        else
        {
            // 第二层判断:检查 MES_CUSTOMER
            if (Db.Queryable<MesCustomer>().Any(x => x.Id == Convert.ToInt32(cgddDetails.DemandOrg)))
            {
                owner_type = "BD_Customer";
            }
            else
            {
                // 第三层判断:检查 MES_SUPPLIER
                if (Db.Queryable<MesSupplier>().Any(x => x.Id == Convert.ToInt32(cgddDetails.DemandOrg)))
                {
                    owner_type = "BD_Supplier";
                }
                else
                {
                    // 第四层判断:再次检查 SYS_ORGANIZATION
                    if (Db.Queryable<SysOrganization>().Any(x => x.Fid == cgddDetails.ReceivingOrg))
                    {
                        owner_type = "BD_OwnerOrg";
                    }
                    else
                    {
                        throw new Exception("入库失败,到货单据对应的需求组织有问题!");
                    }
                }
            }
        }
        //num = Db.Queryable<MesInvItemArn>()
        //    .Where(a => a.BillNo == itemBarcodeDetails.BillNo)
        //    .InnerJoin<MesInvItemArnDetail>((a, b) =>
        //        b.ParentGuid == a.Guid &&
        //        b.ItemId == itemBarcodeDetails.ItemId && b.Ischeck == 1)
        //    .Count();
        var result = new PurchaseInventory();
        //num2 = Db.Queryable<MesInvItemArn>()
        //    .Where(a => a.BillNo == itemBarcodeDetails.BillNo)
        //    .InnerJoin<MesInvItemArnDetail>((a, b) => b.ParentGuid == a.Guid
        //        && b.ItemId == itemBarcodeDetails.ItemId
        //        && (b.CheckRes == "合格" || b.CheckStates == "特采直接使用"))
        //    .Count();
        //switch (num)
        //{
        //    case 0 when num2 == 0:
        //    case > 0 when num2 == 0:
        //        freeze = 1;
        //        break;
        //    case 0 when num2 > 0:
        //        throw new Exception("入库失败,到货单据有问题!");
        //}
        // 判断货主类型
        var owner_type = wmsManager.GetOwnerType(cgddDetails.DemandOrg);
        // if (Db.Queryable<SysOrganization>().Any(x => x.Fid == cgddDetails.DemandOrg))
        // {
        //     owner_type = "BD_OwnerOrg";
        // }
        // else
        // {
        //     // 第二层判断:检查 MES_CUSTOMER
        //     if (Db.Queryable<MesCustomer>().Any(x => x.Id == Convert.ToInt32(cgddDetails.DemandOrg)))
        //     {
        //         owner_type = "BD_Customer";
        //     }
        //     else
        //     {
        //         // 第三层判断:检查 MES_SUPPLIER
        //         if (Db.Queryable<MesSupplier>().Any(x => x.Id == Convert.ToInt32(cgddDetails.DemandOrg)))
        //         {
        //             owner_type = "BD_Supplier";
        //         }
        //         else
        //         {
        //             // 第四层判断:再次检查 SYS_ORGANIZATION
        //             if (Db.Queryable<SysOrganization>().Any(x => x.Fid == cgddDetails.ReceivingOrg))
        //             {
        //                 owner_type = "BD_OwnerOrg";
        //             }
        //             else
        //             {
        //                 throw new Exception("入库失败,到货单据对应的需求组织有问题!");
        //             }
        //         }
        //     }
        //}
        var result = new ItemInBaseModel();
        UseTransaction(db =>
        {
            var existingRecord = db.Queryable<MesInvItemIns>()
                .Where(it => it.InsDate.Value.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd")
                             && it.TaskNo == itemBarcodeDetails.BillNo
                             && it.Status == 0
                             && it.TransctionNo == transactionNo
                             && it.CbillNo == itemBarcodeDetails.BillNo
                             && it.DepotsCode == mesDepost.DepotCode)
                             && it.CreateBy == entity.userName
                             && it.BillTypeId == billTypeId
                             && it.DepotsId == mesDepost.DepotId)
                .Select(it => new
                {
                    it.Guid,
@@ -218,6 +253,7 @@
                    Status = 0,
                    ReceiveOrgId = inventory.ReceiveOrgId,
                    InType = "采购入库",
                    TaskNo = itemBarcodeDetails.BillNo
                    //WorkNo = itemBarcodeDetails.WorkNo
                }).IgnoreColumns(true).ExecuteCommand();
            }
@@ -262,7 +298,7 @@
                            .Where(it =>
                                it.ItemInId == cId &&
                                    it.ItemId == detail.ItemId &&
                                    it.DepotId == depotCode2 &&
                                    it.DepotId == mesDepost.DepotId.ToString() &&
                                    it.EbelnK3id == detail.EbelnK3id &&
                                    it.LineK3id == detail.LineK3id)
                            .Count();
@@ -291,7 +327,8 @@
                                Remark = itemBarcodeDetails.Memo,
                                EbelnK3id = detail.EbelnK3id,
                                LineK3id = detail.LineK3id,
                                DepotId = depotCode2
                                DepotId = mesDepost.DepotId.ToString(),
                                itemDabid = itemBarcodeDetails.AboutGuid.ToString()
                            }).IgnoreColumns(true).ExecuteCommand();
                        else
                            db.Updateable<MesInvItemInCItems>()
@@ -300,7 +337,7 @@
                                .Where(it =>
                                    it.ItemInId == cId &&
                                    it.ItemId == detail.ItemId &&
                                    it.DepotId == depotCode2 &&
                                    it.DepotId == mesDepost.DepotId.ToString() &&
                                    it.EbelnK3id == detail.EbelnK3id &&
                                    it.LineK3id == detail.LineK3id)
                                .IgnoreColumns(true)
@@ -358,7 +395,7 @@
                            .Where(it =>
                                it.ItemInId == cId &&
                                    it.ItemId == detail.ItemId &&
                                    it.DepotId == depotCode2 &&
                                    it.DepotId == mesDepost.DepotId.ToString() &&
                                    it.EbelnK3id == detail.EbelnK3id &&
                                    it.LineK3id == detail.LineK3id)
                            .Count();
@@ -386,7 +423,7 @@
                                EbelnK3id = detail.EbelnK3id,
                                LineK3id = detail.LineK3id,
                                ItemId = detail.ItemId,
                                DepotId = depotCode2
                                DepotId = mesDepost.DepotId.ToString()
                            }).IgnoreColumns(true).ExecuteCommand();
                        else
                            db.Updateable<MesInvItemInCItems>()
@@ -395,7 +432,7 @@
                                .Where(it =>
                                    it.ItemInId == cId &&
                                    it.ItemId == detail.ItemId &&
                                    it.DepotId == depotCode2 &&
                                    it.DepotId == mesDepost.DepotId.ToString() &&
                                    it.EbelnK3id == detail.EbelnK3id &&
                                    it.LineK3id == detail.LineK3id)
                                .IgnoreColumns(true)
@@ -484,9 +521,8 @@
                    .Where(it =>
                    it.ItemInId == cId &&
                                    it.ItemId == detailone.ItemId &&
                                    it.DepotId == depotCode2 &&
                                    it.EbelnK3id == detailone.EbelnK3id &&
                                    it.LineK3id == detailone.LineK3id)
                                    it.DepotId == mesDepost.DepotId.ToString() &&
                                    it.itemDabid == itemBarcodeDetails.AboutGuid.ToString())
                    .Count();
                if (existingCount == 0)
@@ -513,7 +549,8 @@
                        EbelnK3id = itemBarcodeDetails.EbelnK3id,
                        LineK3id = itemBarcodeDetails.LineK3id,
                        ItemId = itemBarcodeDetails.ItemId,
                        DepotId = depotCode2
                        DepotId = mesDepost.DepotId.ToString(),
                        itemDabid = itemBarcodeDetails.AboutGuid.ToString()
                    }).IgnoreColumns(true).ExecuteCommand();
                else
                    // 存在时更新数量
@@ -523,11 +560,10 @@
                            Quantity = SqlFunc.IsNull(it.Quantity, 0) + itemBarcodeDetails.Quantity // 确保 Quantity 不为 null
                        })
                        .Where(it =>
                                it.ItemInId == cId &&
                                it.ItemId == detailone.ItemId &&
                                it.DepotId == depotCode2 &&
                                it.EbelnK3id == detailone.EbelnK3id &&
                                it.LineK3id == detailone.LineK3id)
                                    it.ItemInId == cId &&
                                    it.ItemId == detailone.ItemId &&
                                    it.DepotId == mesDepost.DepotId.ToString() &&
                                    it.itemDabid == itemBarcodeDetails.AboutGuid.ToString())
                        //.IgnoreColumns(true) // 保留 IgnoreColumns
                        .ExecuteCommand();
@@ -570,7 +606,7 @@
                    CheckRes = inventoryDetails.CheckRes,
                    CheckStates = inventoryDetails.CheckStates,
                    ReceiveOrgId = inventory.ReceiveOrgId,
                    DepotId = Convert.ToInt64(depotCode2)
                    DepotId = Convert.ToInt64(mesDepost.DepotId.ToString())
                }).IgnoreColumns(true).ExecuteCommand();
                // 插入 mes_inv_business2 表
@@ -631,7 +667,7 @@
                LineK3id = itemBarcodeDetails.LineK3id,
                ItemId = itemBarcodeDetails.ItemId,
                BillNo = itemBarcodeDetails.BillNo,
                DepotId = Convert.ToInt32(depotCode2),
                DepotId = Convert.ToInt32(mesDepost.DepotId),
                OwnerId = cgddDetails.DemandOrg,
                OwnerType = owner_type,
                StockOrgId = mesDepost.FSubsidiary,
@@ -642,18 +678,19 @@
            if (itemBarcodeDetails.Hbdy != 1)
            {
                //更新收料单明细
                db.Updateable<MesInvItemArnDetail>()
                    .SetColumns(d => new MesInvItemArnDetail
                    {
                        OkRkqty = (int)((d.OkRkqty ?? 0) + cSyQty)
                    })
                    .Where(d => d.Guid == inventoryDetails.Guid)
                    .Where(d => d.Guid == itemBarcodeDetails.AboutGuid)
                    .ExecuteCommand();
                var first = db
                    .Queryable<MesInvItemArnDetail>()
                    .Where(b =>
                        b.Guid == inventoryDetails.Guid)
                        b.Guid == itemBarcodeDetails.AboutGuid)
                    .Select(b => new
                    {
                        TotalQuantity =
@@ -668,7 +705,7 @@
                if (TotalQuantity == TotalOkRkQty)
                    db.Updateable<MesInvItemArnDetail>()
                        .SetColumns(s => s.IsdepsIn == true)
                        .Where(s => s.Guid == inventoryDetails.Guid)
                        .Where(s => s.Guid == itemBarcodeDetails.AboutGuid)
                        .ExecuteCommand();
@@ -691,9 +728,9 @@
                        .ExecuteCommand();
                var totalCDetailsQuantity = db.Queryable<MesInvItemInCDetails>()
                    .Where(it => it.WorkLine == itemBarcodeDetails.WorkLine &&
                    .Where(it => it.ItemId == itemBarcodeDetails.ItemId &&
                                 it.WorkNo == itemBarcodeDetails.WorkNo &&
                                 it.CbillNo == itemBarcodeDetails.BillNo)
                                 it.ItemInId == cId)
                    .Sum(it => it.Quantity);
                var detailSummary = db.Queryable<MesInvItemArnDetail>()
@@ -715,12 +752,51 @@
                var mesItems = db.Queryable<MesItems>()
                    .Where(s => s.Id == itemBarcodeDetails.ItemId).First();
                result.Message =
                    $"条码数量 {itemBarcodeDetails.Quantity},采购订单 {itemBarcodeDetails.WorkNo} 项次 {itemBarcodeDetails.WorkLine} 物料 {mesItems.ItemNo} 本次入库总数:{totalCDetailsQuantity} 总到 {comeQty} 已入 {invQty} 欠 {diffQty}";
                //result.Message =
                //    $"条码数量 {itemBarcodeDetails.Quantity},采购订单 {itemBarcodeDetails.WorkNo} 项次 {itemBarcodeDetails.WorkLine} 物料 {mesItems.ItemNo} 本次入库总数:{totalCDetailsQuantity} 总到 {comeQty} 已入 {invQty} 欠 {diffQty}";
                result.Message = $"物料{mesItems.ItemNo}入库成功数量{itemBarcodeDetails.Quantity.ToString()}";
                result.itemDetail = mesItems;
                result.ItemInDetails = db.Queryable<MesInvItemInCItems,MesItems>(
                    (a, b) => new JoinQueryInfos(
                    JoinType.Left,
                    a.ItemId == b.ItemId)
                    )
                    .Where(a => a.ItemInId == cId && a.ItemId == itemBarcodeDetails.ItemId)
                    .Select<dynamic>((a, b) => new
                    {
                        ItemNo = b.ItemNo,
                        ItemName = b.ItemName,
                        ItemModel = b.ItemModel,
                        FQty = a.Quantity, // 申请数量
                        FMaterialId = b.ItemId,
                        Id = b.Guid.ToString()
                    })
                    .ToList();
                result.ItemBarCDetails = db.Queryable<MesInvItemInCDetails, MesItems>(
                    (a, b) => new JoinQueryInfos(
                    JoinType.Left,
                    a.ItemId == b.ItemId)
                    )
                    .Where(a => a.ItemInId == cId && a.ItemId == itemBarcodeDetails.ItemId)
                    .Select<dynamic>((a, b) => new
                    {
                        ItemNo = b.ItemNo,
                        ItemName = b.ItemName,
                        ItemModel = b.ItemModel,
                        FQty = a.Quantity, // 申请数量
                        FMaterialId = b.ItemId,
                        Id = a.Guid.ToString(),
                        kw = a.DepotSectionCode,
                        barcode = a.ItemBarcode
                    })
                    .ToList();
                result.ItemNo = mesItems.ItemNo;
                result.SumQuantity = Convert.ToDecimal(totalCDetailsQuantity);
                //result.ItemNo = mesItems.ItemNo;
                result.SumQuantity = Convert.ToDecimal(invQty);
                return 1;
            }
@@ -747,14 +823,12 @@
                // Step 2: Calculate total sum from `mes_inv_item_in_c_details`
                var totalCDetailsQuantity = db.Queryable<MesInvItemInCDetails>()
                    .Where(it => it.CbillNo == itemBarcodeDetails.BillNo &&
                                 it.ItemId == itemBarcodeDetails.ItemId)
                    .Where(it => it.Guid == itemBarcodeDetails.AboutGuid)
                    .Sum(it => it.Quantity);
                // Step 3: Calculate comeQty and invQty from `mes_inv_item_arn_detail`
                var detailSummary = db.Queryable<MesInvItemArnDetail>()
                    .Where(it => it.CbillNo == itemBarcodeDetails.BillNo &&
                                 it.ItemId == itemBarcodeDetails.ItemId)
                    .Where(it => it.Guid == itemBarcodeDetails.AboutGuid)
                    .Select(it => new
                    {
                        TotalComeQty = SqlFunc.AggregateSum(it.Quantity),
@@ -777,7 +851,7 @@
                    $" 条码数量:{itemBarcodeDetails.Quantity},物料 {mesItems.ItemNo} 本次入库总数:{totalCDetailsQuantity} 总到 {comeQty} 已入 {invQty} 欠 {diffQty}";
                result.ItemNo = mesItems.ItemNo;
                //result.ItemNo = mesItems.ItemNo;
                result.SumQuantity = Convert.ToDecimal(totalCDetailsQuantity);
            }
@@ -787,25 +861,30 @@
            if (mesInvItemInCDetails == null)
                throw new Exception("物料入库条码明细不存在"); // 抛出异常以供前台处理
            // 创建 插入日志
            var logService = new LogService();
            var LogMsg = "【PDA】采购入库。条码【" + entity.barcode + "】 入库单号【" + cBillNo + "】";
            logService.CreateLog(db, entity.userName, inventory.Guid.ToString(), "MES_INV_ITEM_ARN", LogMsg, inventory.BillNo);
            return 1;
        });
        var purchaseInventory = getPurchaseInventory(entity);
        result.ItemIns = purchaseInventory.ItemIns;
        result.ItemInDetails = purchaseInventory.ItemInDetails;
        result.InvItemInCDetails = purchaseInventory.InvItemInCDetails;
        //result.ItemInDetails = purchaseInventory.ItemInDetails;
        //result.InvItemInCDetails = purchaseInventory.InvItemInCDetails;
        return result;
    }
    public PurchaseInventory getPurchaseInventory(WarehouseQuery query)
    public ItemInBaseModel getPurchaseInventory(WarehouseQuery query)
    {
        return new PurchaseInventory
        return new ItemInBaseModel
        {
            ItemIns = GetInvItemInsList(query).Items[0],
            ItemInDetails = GetItemInDetails(query.id),
            InvItemInCDetails = GetInvItemInCDetails(query.id)
            //ItemIns = GetInvItemInsList(query).Items[0],
            //ItemInDetails = GetItemInDetails(query.id),
            //InvItemInCDetails = GetInvItemInCDetails(query.id)
        };
    }