杨前锦
2025-05-26 d0a265713fd18162f3a82bdcd22e267df7298c7b
HH.WCS.Mobox3/HH.WCS.Mobox3.YNJT_BZP/wms/WMSHelper.cs
@@ -419,25 +419,35 @@
            return result;
        }
       /// <summary>
       /// 获取空托开始货位
       /// </summary>
       /// <param name="trayType"></param>
       /// <returns></returns>
        public static Location GetEmptyTrayStartLoc(int trayType)
        /// <summary>
        /// 获取空托开始货位
        /// </summary>
        /// <param name="trayType"></param>
        /// <param name="exclude">是否排除母拖缓存位 0.不排除 1.排除</param>
        /// <returns></returns>
        public static Location GetEmptyTrayStartLoc(int trayType ,int exclude = 1)
        {
            Location result = null;
            try
            {
                // 1、查询当前库区-排-物料 有托盘的货位
                var db = new SqlHelper<object>().GetInstance();
                result = db.Queryable<Location>()
                var query = db.Queryable<Location>()
                     .LeftJoin<LocCntrRel>((a, b) => a.S_CODE == b.S_LOC_CODE)
                     .LeftJoin<Container>((a, b, c) => b.S_CNTR_CODE == c.S_CODE)
                     .LeftJoin<CntrItemRel>((a,b,c,d) => c.S_CODE == d.S_CNTR_CODE)
                     .Where((a, b, c, d) => a.S_AREA_CODE == Settings.storeAreaCode && (trayType == 0 && a.N_CURRENT_NUM == 1 || trayType > 0 && a.N_CURRENT_NUM > 0) && a.N_LOCK_STATE == 0 && (a.C_ENABLE != "禁用" && a.C_ENABLE != "N") && c.N_TYPE == trayType && d.S_ITEM_CODE == null)
                     .OrderByDescending((a, b, c, d) => a.N_LAYER)
                     .First();
                     .LeftJoin<CntrItemRel>((a, b, c, d) => c.S_CODE == d.S_CNTR_CODE)
                     .Where((a, b, c, d) => a.S_AREA_CODE == Settings.storeAreaCode && (trayType == 0 && a.N_CURRENT_NUM == 1 || trayType > 0 && a.N_CURRENT_NUM > 0) && a.N_LOCK_STATE == 0 && (a.C_ENABLE != "禁用" && a.C_ENABLE != "N") && c.N_TYPE == trayType && d.S_ITEM_CODE == null);
                if (exclude == 1)
                {
                    query = query.Where((a, b, c, d) => a.S_CODE != Settings.baseTrayInBufferLoc);
                }
                else
                {
                    query = query.OrderByDescending((a, b, c, d) => a.S_CODE == Settings.baseTrayInBufferLoc);
                }
                result = query.OrderByDescending((a, b, c, d) => a.N_LAYER).First();
            }
            catch (Exception ex)
            {
@@ -488,8 +498,9 @@
        /// </summary>
        /// <param name="itemCode"></param>
        /// <param name="jbLoc"></param>
        /// <param name="trayCode"></param>
        /// <returns></returns>
        public static Location GetoutStockStartLoc(string itemCode ,string jbLoc = null)
        public static Location GetoutStockStartLoc(string itemCode ,string trayCode = null ,string jbLoc = null)
        {
            LogHelper.Info("【获取出库开始货位】", "WMS");
            var db = new SqlHelper<object>().GetInstance();
@@ -510,6 +521,11 @@
                                    && c.S_EFFECTIVE_TIME != null
                                    && c.S_EXPIRATION_TIME != null
                                    );
                if (trayCode != null)
                {
                    query = query.Where((a, b, c) => c.S_CNTR_CODE == trayCode);
                }
                if (jbLoc != null) 
                {
@@ -737,12 +753,16 @@
            try
            {
                db.BeginTran();
                itemBarcodeInfos.ForEach(a => {
                    var bo = db.Insertable<ItemBarcodeInfo>(a).ExecuteCommand() > 0;
                    if (!bo)
                itemBarcodeInfos.ForEach(item => {
                    var itemBarcodeInfo = db.Queryable<ItemBarcodeInfo>().Where(a => a.S_BC_ENTRIED == item.S_BC_ENTRIED).First();
                    if (itemBarcodeInfo == null)
                    {
                        result = false;
                        LogHelper.Info($"添加物料条码信息失败,物料条码信息:{JsonConvert.SerializeObject(a)}", "GT");
                        var bo = db.Insertable<ItemBarcodeInfo>(item).ExecuteCommand() > 0;
                        if (!bo)
                        {
                            result = false;
                            LogHelper.Info($"添加物料条码信息失败,物料条码信息:{JsonConvert.SerializeObject(item)}", "GT");
                        }
                    }
                });
                if (result)
@@ -774,26 +794,34 @@
            {
                db.BeginTran();
                updateMatlStatuses.ForEach(update => {
                    var bo = db.Updateable<CntrItemRel>().SetColumns(a => new CntrItemRel() { S_ITEM_STATE = update.jdge }).Where(a => a.S_CG_ID == update.bc_entried).ExecuteCommand() > 0;
                    if (!bo)
                    var itemBarcodeInfo = db.Queryable<ItemBarcodeInfo>().Where(a => a.S_BC_ENTRIED == update.bc_entried).First();
                    if (itemBarcodeInfo != null)
                    {
                        result = false;
                        LogHelper.Info($"批量更新物料状态失败,物料条码信息:{JsonConvert.SerializeObject(update)}", "GT");
                        itemBarcodeInfo.S_JDGE = update.jdge;
                        db.Updateable(itemBarcodeInfo).ExecuteCommand();
                        var cntrItemRel = db.Queryable<CntrItemRel>().Where(a => a.S_CG_ID == update.bc_entried).First();
                        if (cntrItemRel != null)
                        {
                            // 将MES的物料状态转化成WMS可识别的物料状态
                            if (update.jdge == "" || update.jdge == null)
                            {
                                update.jdge = "OK";
                            }
                            else if (update.jdge != "OK")
                            {
                                update.jdge = "HOLD";
                            }
                            db.Updateable<CntrItemRel>().SetColumns(a => new CntrItemRel() { S_ITEM_STATE = update.jdge }).Where(a => a.S_CG_ID == update.bc_entried).ExecuteCommand();
                        }
                    }
                });
                if (result)
                {
                    db.CommitTran();
                }
                else
                {
                    db.RollbackTran();
                }
                db.CommitTran();
            }
            catch (Exception ex)
            {
                LogHelper.Info($"批量更新物料状态错误,错误信息:{ex.Message}", "GT");
                db.RollbackTran();
                result = false;
            }
            return result;
        }
@@ -811,48 +839,28 @@
                db.BeginTran();
                foreach (Overage overage in overages) 
                {
                    bool bo = false;
                    var overage1 = db.Queryable<Overage>().Where(a => a.MCNGRP == overage.MCNGRP && a.ITEMPATT == overage.ITEMPATT).First();
                    var overage1 = db.Queryable<Overage>().Where(a => a.RECID == overage.RECID).First();
                    if (overage1 != null)
                    {
                        overage1.MCNGRP = overage.MCNGRP;
                        overage1.ITEMPATT = overage.ITEMPATT;
                        overage1.OVERAGE = overage.OVERAGE;
                        overage1.MINHOUR = overage.MINHOUR;
                        overage1.FLAG_STS = overage.FLAG_STS;
                        bo = db.Updateable(overage1).ExecuteCommand() > 0;
                        db.Updateable(overage1).ExecuteCommand();
                    }
                    else 
                    {
                        overage1 = new Overage()
                        {
                            MCNGRP = overage.MCNGRP,
                            ITEMPATT = overage.ITEMPATT,
                            OVERAGE = overage.OVERAGE,
                            MINHOUR = overage.MINHOUR,
                            FLAG_STS = overage.FLAG_STS
                        };
                        bo = db.Insertable(overage1).ExecuteCommand() > 0;
                    }
                    if (!bo)
                    {
                        result = false;
                        LogHelper.Info($"批量更新物料存放时间配置信息失败,配置信息:{JsonConvert.SerializeObject(overage)}", "GT");
                        break;
                        db.Insertable(overage).ExecuteCommand();
                    }
                }
                if (result)
                {
                    db.CommitTran();
                }
                else
                {
                    db.RollbackTran();
                }
                db.CommitTran();
            }
            catch (Exception ex)
            {
                LogHelper.Info($"批量更新物料存放时间配置信息错误,错误信息:{ex.Message}", "GT");
                db.RollbackTran();
                result = false;
            }
            return result;
        }
@@ -863,22 +871,26 @@
        /// <returns></returns>
        public static Overage getOverage(string bc_entried)
        {
            Overage overage = null;
            var db = new SqlHelper<object>().GetInstance();
            overage = db.Queryable<Overage>().Where(a => a.ITEMPATT == bc_entried && a.MCNGRP == "1").First();
            if (overage == null)
            {
                string sub = bc_entried.Substring(0, 3);
                overage = db.Queryable<Overage>().Where(a => a.ITEMPATT.Contains(sub) && a.MCNGRP == "1").First();
            }
            // 直接执行 SQL(参数化查询)
            var sql = "SELECT get_ovg_bar(@barcode, @mcngrp) AS overage_value";
            var sql1 = "SELECT get_minhour_bar(@barcode, @mcngrp) AS overage_value";
            if (overage == null)
            // 使用匿名对象传递参数
            var ovg_bar = db.Ado.SqlQuery<int>(sql, new { barcode = bc_entried, mcngrp = "1"});
            var minhour_bar = db.Ado.SqlQuery<float>(sql1, new { barcode = bc_entried, mcngrp = "1"});
            if (ovg_bar.Count > 0 && minhour_bar.Count > 0)
            {
                string sub = bc_entried.Substring(0, 7);
                overage = db.Queryable<Overage>().Where(a => a.ITEMPATT.Contains(sub) && a.MCNGRP == "1").First();
                Overage overage = new Overage()
                {
                    MINHOUR = minhour_bar[0],
                    OVERAGE = ovg_bar[0]
                };
                return overage;
            }
            return overage;
            return null;
        }
        /// <summary>
@@ -893,7 +905,18 @@
            try
            {
                db.BeginTran();
                result = db.Insertable(record).ExecuteCommand()> 0;
                var synDataTimeRecord = db.Queryable<SynDataTimeRecord>().Where(a => a.S_SYN_TIME == record.S_SYN_TIME && a.RECORD_TABLE == record.RECORD_TABLE).First();
                if (synDataTimeRecord == null)
                {
                    result = db.Insertable(record).ExecuteCommand() > 0;
                }
                else
                {
                    synDataTimeRecord.N_SYN_NUM = record.N_SYN_NUM;
                    synDataTimeRecord.S_SYN_TIME = record.S_SYN_TIME;
                    result = db.Updateable(synDataTimeRecord).ExecuteCommand() > 0;
                }
                if (result)
                {
                    db.CommitTran();
@@ -905,7 +928,7 @@
            }
            catch (Exception ex)
            {
                LogHelper.Info($"添加同步数据时间记录错误,错误信息:{ex.Message}", "GT");
                LogHelper.Info($"添加数据同步时间记录错误,错误信息:{ex.Message}", "GT");
                db.RollbackTran();
            }
            return result;