lss
2025-06-25 0c6f09c80809eeaf4576a6bb506776ffbd9d34a9
HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/api/ApiHelper.cs
@@ -693,63 +693,23 @@
            try
            {
                string[] state = new string[] { "取消", "失败", "错误", "完成" };
                if (model.type == "1")
                //找到任务
                var task = db.Queryable<WCSTask>().Where(a => a.S_EQ_NO.Trim() == model.task_no && !state.Contains(a.S_B_STATE)).First();
                if (task != null)
                {
                    //找到任务
                    var task = db.Queryable<WCSTask>().Where(a => a.S_START_LOC.Trim() == model.loc && !state.Contains(a.S_B_STATE)).ToList();
                    if (task.Count > 0)
                    if (model.type == "1" || model.type == "2")
                    {
                        foreach (var item in task)
                        {
                            var taskAction = db.Queryable<TaskAction>().Where(a => a.S_TASK_CODE == item.S_CODE).OrderByDescending(a => a.T_CREATE).First();
                            if (taskAction.N_ACTION_CODE == 1101)
                            {
                                NDCApi.ChangeOrderParam(item.S_CODE.Trim(), 6, "1");
                                return result;
                            }
                            result.code = "1";
                            result.msg = $"根据下发货位{model.loc}未找到对应申请取货状态任务";
                            AddErrorInfo("未找到任务", result.msg, "", model.task_no);
                            return result;
                        }
                        NDCApi.ChangeOrderParam(task.S_CODE.Trim(), 6, "1");
                    }
                    else
                    {
                        result.code = "1";
                        result.msg = $"根据下发货位{model.loc}未找到对应任务";
                        AddErrorInfo("未找到任务", result.msg, "", model.task_no);
                        return result;
                    }
                }
                else if (model.type == "2")
                {
                    //找到任务
                    var task = db.Queryable<WCSTask>().Where(a => a.S_END_LOC.Trim() == model.loc && !state.Contains(a.S_B_STATE)).ToList();
                    if (task.Count > 0)
                    {
                        foreach (var item in task)
                        {
                            var taskAction = db.Queryable<TaskAction>().Where(a => a.S_TASK_CODE == item.S_CODE).OrderByDescending(a => a.T_CREATE).First();
                            if (taskAction.N_ACTION_CODE == 1103)
                            {
                                NDCApi.ChangeOrderParam(item.S_CODE.Trim(), 6, "1");
                                return result;
                            }
                        }
                        result.code = "1";
                        result.msg = $"根据下发货位{model.loc}未找到对应申请卸货状态任务";
                        AddErrorInfo("未找到任务", result.msg, "", model.task_no);
                        return result;
                    }
                    else
                    {
                        result.code = "1";
                        result.msg = $"根据下发货位{model.loc}未找到对应任务";
                        AddErrorInfo("未找到任务", result.msg, "", model.task_no);
                        return result;
                    }
                }
                }
                else
                {
                    result.code = "1";
                    result.msg = $"根据下发任务号{model.task_no}未找到对应任务";
                    AddErrorInfo("未找到任务", result.msg, "", model.task_no);
                    return result;
                }
                return result;
            }
            catch (Exception ex)
@@ -920,6 +880,7 @@
                        return result;
                    }
                    loc = StorageCompute(itemcode, areacode);
                    if (loc != null)
@@ -1064,6 +1025,10 @@
                    {
                        areacode = whcode.PMK;
                        cntrType = "PM";
                    }
                    else if (model.PartNumber.StartsWith("32"))
                    {
                        areacode = whcode.ZCW2;
                    }
                    else
                    {
@@ -1230,37 +1195,49 @@
            {
                lock (_lockEmpty)
                {
                    #region 判断托盘绑定货位是否和下发绑定起点相同
                    //var CntrLoc = db.Queryable<LocCntrRel>().Where(a => a.S_CNTR_CODE.Trim() == model.Rfid).First();
                    //if (CntrLoc != null)
                    //{
                    //    if (CntrLoc.S_LOC_CODE != Start)
                    //    {
                    //        LogHelper.Info($"ItemBack:创建任务失败,托盘:{model.Rfid}绑定位置不是起点:{Start}");
                    //        result.code = "1";
                    //        result.msg = $"创建任务失败,托盘:{model.Rfid}绑定位置不是起点:{Start}";
                    //        AddErrorInfo("托盘绑定位置不同", result.msg);
                    //        return result;
                    //    }
                    //}
                    //else
                    //{
                    //    LogHelper.Info($"ItemBack:创建任务失败,托盘:{model.Rfid}未找到绑定货位");
                    //    result.code = "1";
                    //    result.msg = $"创建任务失败,托盘:{model.Rfid}未找到绑定货位";
                    //    AddErrorInfo("查找托盘货位失败", result.msg);
                    //    return result;
                    //}
                    #endregion
                    #region 根据货位楼层判断,3楼去往叠盘位 其他的回库
                    var startloc = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == Start).First();
                    if (startloc != null)
                    {
                        if (startloc.N_LAYER == 3)
                        if (!string.IsNullOrEmpty(startloc.S_NOTE1))
                        {
                            //叠盘货位计算叠盘货位
                            var dploc = db.Queryable<Location>().Where(a => a.S_NOTE.Trim() == "叠盘" && a.N_LAYER == startloc.N_LAYER && a.S_WH_CODE.Trim() == startloc.S_WH_CODE && a.C_ENABLE == "Y").ToList();
                            // var dploc = db.Queryable<Location>().Where(a => a.S_NOTE.Trim() == "叠盘" && a.N_LAYER == startloc.N_LAYER && a.S_WH_CODE.Trim() == startloc.S_WH_CODE && a.C_ENABLE == "Y").ToList();
                            var dploc = db.Queryable<Location>().Where(a => a.S_NOTE1.Trim() == startloc.S_NOTE1 + "叠盘" &&  a.S_WH_CODE.Trim() == startloc.S_WH_CODE).ToList();
                            if (dploc.Count > 0)
                            {
                                // LogHelper.Info($"获取叠盘货位:{JsonConvert.SerializeObject(dploc)}");
                                loc = dploc.FindAll(a => a.N_LOCK_STATE == 0 && a.N_CURRENT_NUM < 6).OrderByDescending(a => a.N_CURRENT_NUM).FirstOrDefault();
                                if (loc == null)
                                {
                                    LogHelper.Info($"{startloc.S_NOTE1}叠盘位,都已经有锁或者已经叠满,请检查货位状态");
                                    result.code = "1";
                                    result.msg = $"{startloc.S_NOTE1}叠盘货位不可用,终点库区没有可用货位,请检查锁状态和数量";
                                    AddErrorInfo("查找库区失败", result.msg, Source);
                                    return result;
                                }
                                else
                                {
                                    endlayer = loc.N_CURRENT_NUM + 1;
                                }
                            }
                            else
                            {
                                // LogHelper.Info($"根据货位楼层:{startloc.N_LAYER},仓库编码:{startloc.S_WH_CODE},货位备注:叠盘,未找到叠盘货位");
                                result.code = "1";
                                result.msg = $"{startloc.S_NOTE1}叠盘货位不可用,未找到可叠盘货位";
                                LogHelper.Info($"根据货位备注:{startloc.S_NOTE1}叠盘,货位仓库:{startloc.S_WH_CODE},找不到叠盘货位");
                                AddErrorInfo("查找货位失败", result.msg, Source);
                                return result;
                            }
                        }
                        else if (startloc.N_LAYER == 3)
                        {
                            //叠盘货位计算叠盘货位
                            // var dploc = db.Queryable<Location>().Where(a => a.S_NOTE.Trim() == "叠盘" && a.N_LAYER == startloc.N_LAYER && a.S_WH_CODE.Trim() == startloc.S_WH_CODE && a.C_ENABLE == "Y").ToList();
                            var dploc = db.Queryable<Location>().Where(a => a.S_NOTE.Trim() == "叠盘" && a.N_LAYER == startloc.N_LAYER && a.S_WH_CODE.Trim() == startloc.S_WH_CODE).ToList();
                            if (dploc.Count > 0)
                            {
                                // LogHelper.Info($"获取叠盘货位:{JsonConvert.SerializeObject(dploc)}");
@@ -1291,7 +1268,8 @@
                        else if (startloc.S_NOTE == "PB")
                        {
                            //找块胶堆叠区
                            var KjDploc = db.Queryable<Location>().Where(a => a.S_NOTE.Trim() == "片胶堆叠位" && a.S_WH_CODE.Trim() == startloc.S_WH_CODE && a.C_ENABLE == "Y").ToList();
                            //  var KjDploc = db.Queryable<Location>().Where(a => a.S_NOTE.Trim() == "片胶堆叠位" && a.S_WH_CODE.Trim() == startloc.S_WH_CODE && a.C_ENABLE == "Y").ToList();
                            var KjDploc = db.Queryable<Location>().Where(a => a.S_NOTE.Trim() == "片胶堆叠位" && a.S_WH_CODE.Trim() == startloc.S_WH_CODE).ToList();
                            if (KjDploc.Count > 0)
                            {
                                loc = KjDploc.FindAll(a => a.N_LOCK_STATE == 0 && a.N_CURRENT_NUM < 6).OrderByDescending(a => a.N_CURRENT_NUM).FirstOrDefault();
@@ -1413,6 +1391,12 @@
                            //  空托回库判断托盘有无绑定物料信息 如果有则删除
                            var itemcntr = db.Queryable<CntrItemDetail>().Where(a => a.S_CNTR_CODE.Trim() == model.Rfid).First();
                            if (itemcntr != null) { db.Deleteable<CntrItemDetail>().Where(a => a.S_CNTR_CODE.Trim() == model.Rfid).ExecuteCommand(); }
                            var container = db.Queryable<Container>().Where(a => a.S_CODE.Trim() == model.Rfid).First();
                            if (container == null)
                            {
                                container = new Container { S_CODE = model.Rfid };
                                db.Insertable<Container>(container).ExecuteCommand();
                            }
                            LogHelper.Info("创建任务成功");
                        }
                        else
@@ -1478,11 +1462,24 @@
            {
                lock (_lockcallfixture)
                {
                    #region 根据仓库编码判断库区 并计算出库货位
                    var endloc = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == End).First();
                    //根据仓库 计算库区
                    if (endloc != null)
                    {
                        #region 校验终点是否可送
                        if (endloc.N_CURRENT_NUM >= 1 || endloc.N_LOCK_STATE != 0)
                        {
                            result.code = "1";
                            result.msg = $"终点货位:{End}不可用";
                            LogHelper.Info($"Putin==> {result.msg}");
                            AddErrorInfo("货位不可用", result.msg, Source);
                            return result;
                        }
                        #endregion
                        var whcode = Settings.wareLoc.Where(a => a.AreaCode == endloc.S_WH_CODE).FirstOrDefault();
                        if (whcode != null)
                        {
@@ -1640,34 +1637,7 @@
            {
                lock (_lockItemBack)
                {
                    #region 余料返回根据托盘找出库任务
                    //余料返回根据托盘找出库任务
                    //msts = WCSHelper.GetTaskBycntrcode(model.Rfid);
                    //if (msts.Count() > 0)
                    //{
                    //    msts.RemoveAll(a => string.IsNullOrEmpty(a.S_CNTR_CODE));
                    //    mst = msts.OrderByDescending(a => a.T_CREATE).FirstOrDefault();
                    //}
                    //if (mst != null)
                    //{
                    //    areacode = mst.S_START_AREA;
                    //}
                    //else
                    //{
                    //    result.code = "1";
                    //    result.msg = $"创建任务失败,根据托盘编码{model.Rfid}未找到托盘出库任务";
                    //    AddErrorInfo("查找任务失败", result.msg);
                    //    return result;
                    //}
                    //if (string.IsNullOrEmpty(areacode))
                    //{
                    //    LogHelper.Info($"创建任务失败,根据托盘编码{model.Rfid}找到出库任务{mst.S_CODE}的起点库区为空");
                    //    result.code = "1";
                    //    result.msg = $"创建任务失败,根据托盘编码{model.Rfid}找到出库任务{mst.S_CODE}的起点库区为空";
                    //    AddErrorInfo("查找库区失败", result.msg);
                    //    return result;
                    //}
                    #endregion
                    #region 判断托盘绑定货位是否和下发绑定起点相同
                    var CntrLoc = db.Queryable<LocCntrRel>().Where(a => a.S_CNTR_CODE.Trim() == model.Rfid).First();
@@ -1715,17 +1685,18 @@
                                //    loc = emptyRow(areacode);
                                //}
                            }
                            else if (model.PartNumber.StartsWith("32"))
                            {
                                areacode = whcode.ZCW2;
                            }
                            else
                            {
                                if (model.PartNumber.StartsWith("3"))
                                {
                                    areacode = whcode.ZCW;
                                }
                                else
                                {
                                    result.code = "1";
                                    result.msg = $"创建任务失败,物料号{model.PartNumber}不属于规定物料";
                                    LogHelper.Info($"ItemBack:{result.msg}");
@@ -1870,9 +1841,6 @@
                    }
                    #endregion
                }
                return result;
            }
@@ -2134,7 +2102,8 @@
                var startloc = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == model.Data.start_loc_code).First();
                var endloc = db.Queryable<Location>().Where(a => a.S_AREA_CODE.Trim() == model.Data.end_loc_code && a.N_CURRENT_NUM == 0 && a.N_LOCK_STATE == 0 && a.C_ENABLE == "Y").First();
                //  var endloc = db.Queryable<Location>().Where(a => a.S_AREA_CODE.Trim() == model.Data.end_loc_code && a.N_CURRENT_NUM == 0 && a.N_LOCK_STATE == 0 && a.C_ENABLE == "Y").First();
                var endloc = db.Queryable<Location>().Where(a => a.S_AREA_CODE.Trim() == model.Data.end_loc_code && a.N_CURRENT_NUM == 0 && a.N_LOCK_STATE == 0).First();
                //判断楼层是否相等,如果不相同需要生成分段任务进行电梯调用
@@ -2389,8 +2358,8 @@
                }
            }
            return result;
        }
@@ -2586,14 +2555,23 @@
        /// <returns></returns>
        internal static Location StorageCompute(string itemcode, string areacode)
        {
            //Location result = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM == 0 && a.S_AREA_CODE.Trim() == areacode && a.N_LOCK_STATE == 0).First();
            var db = new SqlHelper<object>().GetInstance();
            Location result = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM == 0 && a.S_AREA_CODE.Trim() == areacode && a.N_LOCK_STATE == 0 && a.C_ENABLE == "Y").First();
            Location result = new Location();
            var list = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM == 0 && a.S_AREA_CODE.Trim() == areacode && a.N_LOCK_STATE == 0).ToList();
            if (list.Count > 0)
            {
                result= FindEndcolByLocList(list);
            }
            //查询所有有托盘的排没锁的排
            // var list = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM > 0 && a.S_AREA_CODE.Trim() == areacode && a.N_LOCK_STATE == 0).OrderByDescending(a => a.N_COL).Take(1).PartitionBy(a => a.N_ROW).ToList();
            // if (list.Count > 0)
            // var list = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM == 0 && a.S_AREA_CODE.Trim() == areacode && a.N_LOCK_STATE == 0).OrderByDescending(a => a.N_COL).Take(1).PartitionBy(a => a.N_ROW).ToList();
            //if (list.Count > 0)
            //{
            //    for (int i = 0; i < list.Count; i++)
            //    {
            //        LogHelper.Info($"有托盘排号{list[i].N_ROW},物料编码{itemcode},库区{areacode}");
            //        //查询货位绑定托盘
            //        var cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == list[i].S_CODE).First();
@@ -2784,7 +2762,7 @@
            lock (_lockLocation)
            {
                var loc = db.Queryable<Location>().Where(a => a.S_AREA_CODE.Trim() == jtcode.Trim() && a.S_NOTE == cntrType && a.N_CURRENT_NUM == 0 && a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无" && a.C_ENABLE == "Y").First();
                var loc = db.Queryable<Location>().Where(a => a.S_AREA_CODE.Trim() == jtcode.Trim() && a.S_NOTE == cntrType && a.N_CURRENT_NUM == 0 && a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无").First();
                if (loc != null)
                {
                    End = loc;
@@ -2851,13 +2829,16 @@
                var rows = locations.Select(a => a.N_ROW).Distinct().ToList();
                for (int i = 0; i < rows.Count; i++)
                {
                    var rowList = locations.Where(r => r.N_ROW == rows[i]).ToList();
                    LogHelper.Info($"当前排:{rows[i]},获得货位{JsonConvert.SerializeObject(rowList)}");
                    if (rowList.Count(a => a.N_CURRENT_NUM == 0) > 0)
                    {
                        Location other = null;
                        //当前排没有锁并且有空位置
                        //先找满位,然后后面一层要么是空,要么不存在
                        other = rowList.OrderBy(a => a.N_COL).Where(a => a.N_CURRENT_NUM == 0 && a.N_LOCK_STATE == 0 && a.C_ENABLE == "Y").FirstOrDefault();
                        // other = rowList.OrderBy(a => a.N_COL).Where(a => a.N_CURRENT_NUM == 0 && a.N_LOCK_STATE == 0 && a.C_ENABLE == "Y").FirstOrDefault();
                        other = rowList.OrderBy(a => a.N_COL).Where(a => a.N_CURRENT_NUM == 0 && a.N_LOCK_STATE == 0).FirstOrDefault();
                        //if (full == null)
                        //{
                        //    //没有满位,那就找最小的空位
@@ -2893,6 +2874,57 @@
        }
        /// <summary>
        /// 找终点空货位计算排锁
        /// </summary>
        /// <param name="locations"></param>
        /// <returns></returns>
        internal static Location FindEndcolByLocListLock(List<Location> locations)
        {
            try
            {
                var db = new SqlHelper<object>().GetInstance();
                Location end = null;
                //根据终点货位找空闲货位
                var rows = locations.Select(a => a.N_ROW).Distinct().ToList();
                for (int i = 0; i < rows.Count; i++)
                {
                    var rowList = locations.Where(r => r.N_ROW == rows[i]).ToList();
                    if (rowList.Count(a => a.S_LOCK_STATE != "无") == 0 && rowList.Count(a => a.N_CURRENT_NUM == 0) > 0)
                    {
                        Location other = null;
                        //当前排没有锁并且有空位置
                        //先找满位,然后后面一位要么是空,要么不存在
                        var full = rowList.OrderByDescending(a => a.N_COL).Where(a => a.N_CURRENT_NUM == 1).FirstOrDefault();
                        if (full == null)
                        {
                            //没有满位,那就找最小的空位
                            other = rowList.OrderBy(a => a.N_COL).FirstOrDefault();
                        }
                        else
                        {
                            other = rowList.OrderBy(a => a.N_COL).Where(a => a.N_COL > full.N_COL).FirstOrDefault();
                        }
                        if (other != null)
                        {
                            end = other;
                            break;
                        }
                    }
                }
                return end;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 找起点满货位
        /// </summary>
        /// <param name="locations"></param>