南骏 池
6 天以前 4910c0fa81d93635e19a57c073c3a62c76053320
service/Warehouse/MesItemQtrkManager.cs
@@ -1,8 +1,10 @@
using NewPdaSqlServer.DB;
using NewPdaSqlServer.Dto.service;
using NewPdaSqlServer.entity;
using NewPdaSqlServer.entity.Base;
using NewPdaSqlServer.util;
using SqlSugar;
using System.Text.RegularExpressions;
namespace NewPdaSqlServer.service.Warehouse;
@@ -10,83 +12,85 @@
{
    public List<string> GetQtckList()
    {
        return Db.Queryable<MesItemQtrk>()
            .Where(x => (x.Qt015 ?? 0) == 1 && (x.Qt014 ?? 0) == 0)
            .Select(x => x.Qtck)
            .ToList();
        //return Db.Queryable<MesItemQtrk>()
        //    .Where(x => (x.Qt015 ?? 0) == 1 && (x.Qt014 ?? 0) == 0)
        //    .Select(x => x.Qtck)
        //    .ToList();
        const string sql = @"SELECT qtck
                        FROM MES_ITEM_QTRK
                        WHERE  QT015 = 1 AND QT028 = 1 AND QT032 = 1
                          AND QT014 = 0  ";
        return Db.Ado.SqlQuery<string>(sql);
    }
    public List<MesItemQtrrDetail> GetQtckDetailList(WarehouseQuery query)
    public ProductionPickDto GetQtckDetailList(WarehouseQuery query)
    {
        // 1. 验证申请单是否存在
        var qtrk = Db.Queryable<MesItemQtrk>()
            .Where(x => x.Qtck == query.billNo)
            .First();
        if (qtrk == null)
            throw new Exception($"未查询到此其他入库申请单 {query.billNo}");
        // 关联查询物料表、物料明细表和物料基础信息表
        if (string.IsNullOrEmpty(query.billNo)) throw new Exception("申请单号为空");
        // 2. 验证申请单状态
        if (qtrk.Qt015 != 1)
            throw new Exception($"其他入库申请单 {query.billNo} 未审核,请确认!");
        var mesInvItemOuts = base.GetSingle(it => it.Qtck == query.billNo);
        if (mesInvItemOuts == null) throw new Exception("其他入库申请单不存在");
        if (qtrk.Qt014 == 1)
            throw new Exception($"其他入库申请单 {query.billNo} 已完结,请确认!");
        var sql = @"SELECT c.item_no ItemNo,c.item_name ItemName,c.item_model ItemModel,
                    ISNULL(A.qd007,0) FQty,ISNULL(A.qd008,0) SQty,ISNULL(A.qd007,0) - ISNULL(A.qd008,0) DSQty
                    FROM MES_ITEM_QTRR_DETAIL A
                    LEFT JOIN MES_ITEM_QTRK B ON A.qtrkGuid = B.guid
                    LEFT JOIN MES_ITEMS C ON A.itemId = C.item_id
                    WHERE B.qtck = @billNo AND QT015 = 1 AND QT028 = 1 AND QT032 = 1  ";
        //AND QT014 = 0
        // 3. 查询申请单明细
        var details = Db.Queryable<MesItemQtrk, MesItemQtrrDetail, MesItems>(
                (a, b, c) => new JoinQueryInfos(
                    JoinType.Left, a.Guid == b.QtrkGuid,
                    JoinType.Left, c.Id.ToString() == b.ItemId))
            .Where((a, b, c) =>
                a.Qtck == query.billNo &&
                (b.Qd007 ?? 0) - (b.Qd008 ?? 0) > 0)
            .OrderBy((a, b, c) => b.Qd002)
            .Select((a, b, c) => new MesItemQtrrDetail
            {
                Qtck = a.Qtck,
                ItemNo = c.ItemNo,
                ItemName = c.ItemName,
                Qd007 = b.Qd007 ?? 0,
                Qd008 = b.Qd008 ?? 0
            })
            .ToList();
        var sqlParams = new List<SugarParameter> {
            new("@billNo", query.billNo)
        };
        return details;
        var womdabs = Db.Ado.SqlQuery<ItemDetailModel>(sql, sqlParams);
        if (womdabs.Count < 1)
            throw new Exception($"该其他入库申请单 {query.billNo} 未全部审核或已完结,请确认!");
        var DS_list = womdabs.Where(s => s.DSQty > 0).ToList();
        var YS_list = womdabs.Where(s => s.SQty > 0).ToList();
        var dto = new ProductionPickDto
        {
            //daa001 = womdaa.Daa001,
            //PlanNo = womcaa.Caa020,
            items = DS_list,
            Ysitems = YS_list
        };
        return dto;
    }
    public string ScanInDepotsQT(WarehouseQuery query)
    public dynamic ScanInDepotsQT(WarehouseQuery query)
    {
        var sectionCode = query.sectionCode;
      var sectionCode = query.sectionCode;
        var billNo = query.billNo;
        // 1. 验证库位条码是否为空
        if (string.IsNullOrEmpty(sectionCode)) throw new Exception("请扫库位条码!");
        // 2. 查询库位对应的仓库编码
        var depotCode = Db.Queryable<MesDepotSections, MesDepots>(
                (a, b) => new JoinQueryInfos(
                    JoinType.Inner, a.DepotGuid == b.Guid))
            .Where((a, b) => a.DepotSectionCode == sectionCode)
            .Select((a, b) => b.DepotCode)
            .First();
        var sqlParams = new List<SugarParameter> { new("@sectionCode", sectionCode) };
        if (depotCode == null)
        var sql2 = @"        SELECT TOP 1  b.depot_code,
                      b.depot_id,
                     B.FSubsidiary,
                     B.depot_name
        FROM MES_DEPOT_SECTIONS a
                 INNER JOIN
             MES_DEPOTS b ON a.depot_guid = b.Guid
        WHERE a.depot_section_code = @sectionCode;";
        var depotInfo = Db.Ado.SqlQuery<dynamic>(sql2, sqlParams).First();
        if (depotInfo is  null)
            throw new Exception($"库位编码 {sectionCode} 不存在,请确认!");
        // 3. 查询申请单对应的仓库
        var qtrk = Db.Queryable<MesItemQtrk>()
            .Where(x => x.Qtck == billNo)
            .Select(x => x.Qt011)
            .First();
        if (qtrk == null) throw new Exception($"库位编码 {sectionCode} 不存在,请确认!");
        // 4. 验证库位是否属于申请仓库
        if (depotCode != qtrk)
            throw new Exception($"扫码库位 {sectionCode} 不属于申请此仓库!");
        // 5. 返回成功信息
        return $"仓库:{qtrk} 库位:{sectionCode}";
        return depotInfo;
    }
    public (WarehouseQuery form, List<MesItemQtrrDetail> item)
@@ -118,6 +122,28 @@
        var c_depot_code = depotInfo.DepotCode;
        var c_depot_id = depotInfo.DepotId;
        var sqlParams = new List<SugarParameter> { new("@barcode", p_item_barcode), new("@billNo", p_bill_no) };
        var sql1 = @"   SELECT TOP 1 A.rkCkId
FROM MES_QA_ITEMS_DETECT_01 A
         LEFT JOIN MES_INV_ITEM_BARCODES B ON A.item_id = B.ITEM_ID
WHERE order_type = '其它入库检'
  AND A.lot_no = @billNo
  AND ITEM_BARCODE = @barcode   ";
        var appDepotInfo = Db.Ado.SqlQuery<dynamic>(sql1, sqlParams).First();
        if(appDepotInfo.rkCkId != c_depot_id)
            throw new Exception($"该 {p_section_code} 对应的仓库 与 检验判定的入库仓库不一致,请确认!");
        var mesDepost = Db.Queryable<MesDepots>()
                .Where(s => s.DepotId == c_depot_id).First();
        // 3. 验证条码是否已入库
        var existsInStock = Db.Queryable<MesInvItemInCDetails>()
            .Any(x => x.ItemBarcode == p_item_barcode);
@@ -131,7 +157,7 @@
        if (barcode == null) throw new Exception("条码不存在,请核对!");
        if (barcode.ComeFlg != 3) throw new Exception("条码不是其他入库条码,无法用其他入库!");
        //if (barcode.ComeFlg != 3) throw new Exception("条码不是其他入库条码,无法用其他入库!");
        // 验证条码是否已在库存中
        var stockCount = Db.Queryable<MesInvItemStocks>()
@@ -146,6 +172,27 @@
            .First();
        if (qtrk == null) throw new Exception("其他入库申请单不存在!");
        var sql = @"SELECT c.item_no ItemNo,c.item_name ItemName,c.item_model ItemModel,
                    ISNULL(A.qd007,0) FQty,ISNULL(A.qd008,0) SQty,ISNULL(A.qd007,0) - ISNULL(A.qd008,0) DSQty
                    FROM MES_ITEM_QTRR_DETAIL A
                    LEFT JOIN MES_ITEM_QTRK B ON A.qtrkGuid = B.guid
                    LEFT JOIN MES_ITEMS C ON A.itemId = C.item_id
                    WHERE B.qtck = @billNo AND QT015 = 1 AND QT028 = 1 AND QT032 = 1 AND QT014 = 0 ";
        //AND QT014 = 0
        var sqlParams1 = new List<SugarParameter> {
            new("@billNo", p_bill_no)
        };
        var womdabs = Db.Ado.SqlQuery<ItemDetailModel>(sql, sqlParams1);
        if (womdabs.Count < 1)
            throw new Exception($"该其他入库申请单 {query.billNo} 未全部审核或已完结,请确认!");
        ////  验证库位是否属于申请仓库
        //if (c_depot_id != Convert.ToInt32(qtrk.Qt008))
        //    throw new Exception($"扫码库位 {p_section_code} 不属于申请仓库!");
        // 6. 查询申请单明细
        var detail = Db.Queryable<MesItemQtrrDetail>()
@@ -163,18 +210,37 @@
        var details = new List<MesItemQtrrDetail>();
        // 8. 执行入库事务
        string pattern = @"\(([^)]+)\)";
        Match match = Regex.Match(qtrk.Qt023, pattern);
        var owner_type = "";
        // 8.获取货主类型
        if (match.Success)
        {
            owner_type = match.Groups[1].Value;
        }
        else
        {
            throw new Exception("其他入库申请单货主类型有误,请核对!");
        }
        // 10. 执行入库事务
        UseTransaction(db =>
        {
            var res = 0;
            // 查询是否存在未入库的入库单
            var existingInv = db.Queryable<MesInvItemIns>()
                .Where(x => x.Status == 0
                            && x.InsDate.Value.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd")
                            && x.TransctionNo == transactionNo.ToString()
                            && x.CbillNo == p_bill_no
                            && x.SuppNo == barcode.SuppNo
                            && x.TaskNo == p_bill_no
                            //&& x.CbillNo == p_bill_no
                            && x.BillTypeId == billTypeId
                            && x.DepotsId == Convert.ToInt64(c_depot_id))
                            // && x.DepotsCode == c_depot_code)
                            && x.DepotsId == c_depot_id)
                            //&& x.SuppNo == barcode.SuppNo)
                .First();
            var newId = Guid.Empty;
@@ -183,7 +249,7 @@
            if (existingInv == null)
            {
                newId = Guid.NewGuid();
                billNo = BillNo.GetBillNo("INV_IN_OTHER");
                billNo = BillNo.GetBillNo("QTRK(其他入库)");
                barcode.UrgentFlag ??= false;
@@ -200,11 +266,16 @@
                    CreateDate = DateTime.Now,
                    LastupdateBy = c_user,
                    LastupdateDate = DateTime.Now,
                    UrgentFlag = barcode.UrgentFlag.Value ? (byte)1 : (byte)0,
                    CbillNo = p_bill_no,
                    UrgentFlag = barcode.UrgentFlag.Value ? "1" : "0",
                    //CbillNo = p_bill_no,
                    Fstatus = 0,
                    Status = 0,
                    Reason = qtrk.Qt010
                    Reason = qtrk.Qt010,
                    TaskNo = p_bill_no,
                    DepotsId = Convert.ToInt64(c_depot_id) ,
                    InType = "其他入库",
                    ReceiveOrgId = qtrk.Qt022
                }).IgnoreColumns(true).ExecuteCommand();
            }
            else
@@ -217,8 +288,9 @@
            var existingItem = db.Queryable<MesInvItemInCItems>()
                .Where(x => x.ItemInId == newId
                            && x.ItemId == barcode.ItemId
                            && x.WorkNo == barcode.WorkNo
                            && x.WorkLine == barcode.WorkLine)
                            && x.DepotId == c_depot_id.ToString()
                             )
                //&& x.DepotCode =
                .First();
            if (existingItem == null)
@@ -246,7 +318,9 @@
                    Remark = barcode.Memo,
                    EbelnK3id = barcode.EbelnK3id,
                    LineK3id = barcode.LineK3id,
                    ItemId = barcode.ItemId
                    ItemId = barcode.ItemId,
                    DepotId = c_depot_id.ToString(),
                    itemDabid = barcode.AboutGuid.ToString(),
                }).IgnoreColumns(true).ExecuteCommand();
            else
                // 存在则更新数量
@@ -254,9 +328,8 @@
                    .SetColumns(
                        x => x.Quantity == x.Quantity + barcode.Quantity)
                    .Where(x => x.ItemInId == newId
                                && x.ItemId == barcode.ItemId
                                && x.WorkNo == barcode.WorkNo
                                && x.WorkLine == barcode.WorkLine)
                            && x.ItemId == barcode.ItemId
                            && x.DepotId == c_depot_id.ToString())
                    .ExecuteCommand();
@@ -274,7 +347,7 @@
                LotNo = barcode.LotNo,
                SuppId = barcode.SuppId,
                SuppNo = barcode.SuppNo,
                DepotId = c_depot_id,
                DepotId = Convert.ToInt64(c_depot_id),
                DepotCode = c_depot_code,
                DepotSectionCode = p_section_code,
                ItemSname = barcode.ItemSname,
@@ -361,7 +434,11 @@
                EbelnK3id = barcode.EbelnK3id,
                LineK3id = barcode.LineK3id,
                ItemId = barcode.ItemId,
                BillNo = barcode.BillNo
                BillNo = barcode.BillNo,
                OwnerId = qtrk.Qt024,
                OwnerType = owner_type,
                StockOrgId = qtrk.Qt022,
                IndepUserCode = c_user
                // SalesOrder = barcode.SalesOrder,
                // IsZy = barcode.IsZy,
                // Visable = 0,
@@ -377,7 +454,8 @@
            // 更新申请单明细已入库数量
            res += db.Updateable<MesItemQtrrDetail>()
                .SetColumns(x => x.Qd008 == (x.Qd008 ?? 0) + barcode.Quantity)
                .Where(x => x.Guid == barcode.Guid)
                .Where(x => x.QtrkGuid == qtrk.Guid &&
                x.ItemId == barcode.ItemId.ToString())
                .ExecuteCommand();
            // 检查是否完全入库并更新状态
@@ -415,6 +493,11 @@
                    .Where(x => x.Qtck == p_bill_no)
                    .ExecuteCommand();
            // 创建 插入日志
            var logService = new LogService();
            var LogMsg = "【PDA】其他入库。条码【" + query.barcode + "】数量【" + barcode.Quantity.ToString() + "】 入库单号【" + billNo + "】";
            logService.CreateLog(db, query.userName, qtrk.Guid.ToString(), "MES_ITEM_QTRK", LogMsg, qtrk.Qtck);
            if (res < 5) throw new Exception("插入或更新失败");
            return res;