lss
2025-07-07 ba528c7a15058967ee3af7364035209aa0048025
HH.WCS.Mobox3/HH.WCS.Mobox3.DaYang/api/ApiHelper.cs
@@ -5,6 +5,7 @@
using HH.WCS.DaYang.wms;
using Newtonsoft.Json;
using NLog.Fluent;
using Opc.Ua;
using SqlSugar;
using System;
using System.Collections.Concurrent;
@@ -159,11 +160,10 @@
                return result;
            }
            var db = new SqlHelper<object>().GetInstance();
            string start = model.startBit;
            // string start = model.startBit;
            Location endloc = new Location();
            Location startloc = new Location();
            string Type = "";
            string cntrcode = "";
            lock (lockObj)
            {
                try
@@ -182,25 +182,12 @@
                            LogHelper.Info($"creattask:{result.Msg}");
                            return result;
                        }
                        else
                        {
                            var cntr = LocationHelper.GetLocCntrRel(endloc.S_CODE).FirstOrDefault();
                            if (cntr == null)
                            {
                                result.Code = -1;
                                result.Msg = $"根据起点:{model.startBit}未找到绑定托盘";
                                LogHelper.Info($"creattask:{result.Msg}");
                                return result;
                            }
                            else
                            {
                                cntrcode = cntr.S_CNTR_CODE;
                            }
                        }
                        #endregion
                        #region 计算立库终点
                        endloc = LocationHelper.InStorage(startloc);
                        endloc = LocationHelper.InStorage("");
                        if (endloc == null)
                        {
                            result.Code = -1;
@@ -213,15 +200,24 @@
                        #region 创建作业
                        if (startloc != null && endloc != null)
                        {
                            //获取托盘
                            var Cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == startloc.S_CODE).First();
                            if (Cntr == null)
                            {
                                result.Code = -1;
                                result.Msg = $"根据起点:{startloc.S_CODE}未找到托盘货位绑定关系";
                                LogHelper.Info($"creattask:{result.Msg}");
                                return result;
                            }
                            //创建作业
                            var wcsTask = new WMSTask
                            {
                                S_CODE = WMSHelper.GenerateTaskNo(),
                                S_TYPE = Type,
                                N_TYPE = int.Parse(model.TaskType),
                                S_START_LOC = start,
                                S_START_LOC = startloc.S_CODE,
                                S_END_LOC = endloc.S_CODE,
                                S_CNTR_CODE = cntrcode,
                                S_CNTR_CODE = Cntr.S_CNTR_CODE,
                                S_END_WH = endloc.S_WH_CODE,
                                S_START_WH = startloc.S_WH_CODE,
                                S_START_AREA = startloc.S_AREA_CODE,
@@ -235,8 +231,8 @@
                                LocationHelper.LockLoc(endloc.S_CODE, 1);
                                LogHelper.Info("创建作业成功");
                                //空托回库成功,如果存在物料数据,删除
                                var itemcntr = db.Queryable<CntrItemDetail>().Where(a => a.S_CNTR_CODE.Trim() == cntrcode).First();
                                if (itemcntr != null) { db.Deleteable<CntrItemDetail>().Where(a => a.S_CNTR_CODE.Trim() == cntrcode).ExecuteCommand(); }
                                var itemcntr = db.Queryable<CntrItemDetail>().Where(a => a.S_CNTR_CODE.Trim() == Cntr.S_CNTR_CODE).First();
                                if (itemcntr != null) { db.Deleteable<CntrItemDetail>().Where(a => a.S_CNTR_CODE.Trim() == Cntr.S_CNTR_CODE).ExecuteCommand(); }
                                LogHelper.Info("创建任务成功");
                            }
                        }
@@ -249,8 +245,8 @@
                        Type = "出库";
                        #region 获取终点信息
                        startloc = LocationHelper.GetLoc(model.endBit);
                        if (startloc == null)
                        endloc = LocationHelper.GetLoc(model.endBit);
                        if (endloc == null)
                        {
                            result.Code = -1;
                            result.Msg = $"根据终点:{model.startBit}未找到货位信息";
@@ -261,10 +257,10 @@
                        #region 根据物料计算出库托盘,先进先出,优先出外侧
                        //先计算内侧货位,判断是否有单组托盘
                        var locations = LocationHelper.GetLocByItemCodeOrderyTime("", model.ItemCode);
                        if (locations.Count > 0)
                        var location = LocationHelper.GetLocByItemCodeOrderyTime("YCLLKQ", model.ItemCode);
                        if (location != null)
                        {
                            endloc = locations.OrderBy(a => a.N_COL).First();
                            startloc = location;
                        }
                        #endregion
@@ -272,15 +268,24 @@
                        #region 创建作业
                        if (startloc != null && endloc != null)
                        {
                            //获取托盘
                            var Cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == startloc.S_CODE).First();
                            if (Cntr == null)
                            {
                                result.Code = -1;
                                result.Msg = $"根据起点:{startloc.S_CODE}未找到托盘货位绑定关系";
                                LogHelper.Info($"creattask:{result.Msg}");
                                return result;
                            }
                            //创建作业
                            var wcsTask = new WMSTask
                            {
                                S_CODE = WMSHelper.GenerateTaskNo(),
                                S_TYPE = Type,
                                N_TYPE = int.Parse(model.TaskType),
                                S_START_LOC = start,
                                S_START_LOC = startloc.S_CODE,
                                S_END_LOC = endloc.S_CODE,
                                S_CNTR_CODE = cntrcode,
                                S_CNTR_CODE = Cntr.S_CNTR_CODE,
                                S_END_WH = endloc.S_WH_CODE,
                                S_START_WH = startloc.S_WH_CODE,
                                S_START_AREA = startloc.S_AREA_CODE,
@@ -337,74 +342,87 @@
            Location endloc = new Location();
            Location startloc = new Location();
            string cntrcode = model.CntrNo;
            string DiePanLoc = "";
            // string itemCode = "";
            var db = new SqlHelper<object>().GetInstance();
            if (model.DeviceNo == "1")//叠盘机
            {
                #region 获取起点信息,并判断空托是否叠满
                //根据起点 获取绑定托盘数量
                var CntrLoc = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == model.Loc).ToList();
                if (CntrLoc.Count < 4)
                //判断设备状态是否属于叠盘状态
                var DeviceStatu = db.Queryable<EquipmentStatus>().Where(a => a.deviceNo == model.DeviceNo).First();
                if (DeviceStatu != null && DeviceStatu.Status == "1")
                {
                    result.Code = -1;
                    result.Msg = $"根据货位:{model.Loc}获取托盘数量小于4";
                    LogHelper.Info($"EquipmentInfo:{result.Msg}");
                    return result;
                    #region 获取起点信息,并判断空托是否叠满
                    //根据起点 获取绑定托盘数量
                    //var CntrLoc = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == model.Loc).ToList();
                    //if (CntrLoc.Count < 4)
                    //{
                    //    result.Code = -1;
                    //    result.Msg = $"根据货位:{model.Loc}获取托盘数量小于4";
                    //    LogHelper.Info($"EquipmentInfo:{result.Msg}");
                    //    return result;
                    //}
                    //else
                    //{
                    //    //拼接托盘号
                    //    cntrcode = CntrLoc.Select(a => a.S_CNTR_CODE).ToString();
                    //}
                    cntrcode = model.CntrNo;
                    startloc = LocationHelper.GetLoc(model.Loc);
                    if (startloc == null)
                    {
                        result.Code = -1;
                        result.Msg = $"根据起点:{model.Loc}未找到货位信息";
                        LogHelper.Info($"EquipmentInfo:{result.Msg}");
                        return result;
                    }
                    #endregion
                    #region 计算立库终点
                    endloc = LocationHelper.InStorage("");
                    if (endloc == null)
                    {
                        result.Code = -1;
                        result.Msg = $"未计算到立库终点货位,请检查货位情况";
                        LogHelper.Info($"EquipmentInfo:{result.Msg}");
                        return result;
                    }
                    #endregion
                    #region 创建任务
                    if (startloc != null && endloc != null)
                    {
                        var wcsTask = new WCSTask
                        {
                            S_CODE = WCSHelper.GenerateTaskNo(),
                            S_TYPE = "叠盘机入库",
                            S_START_LOC = startloc.S_CODE,
                            S_END_LOC = endloc.S_CODE,
                            S_START_WH = startloc.S_WH_CODE,
                            S_START_AREA = startloc.S_AREA_CODE,
                            S_END_WH = endloc.S_WH_CODE,
                            S_END_AREA = endloc.S_AREA_CODE,
                            N_CNTR_COUNT = 1,
                            N_SCHEDULE_TYPE = 2,
                            S_CNTR_CODE = cntrcode
                        };
                        LogHelper.Info("创建入平库任务:" + JsonConvert.SerializeObject(wcsTask));
                        if (WCSHelper.CreateTask(wcsTask))
                        {
                            LocationHelper.LockLoc(startloc.S_CODE, 2);
                            LocationHelper.LockLoc(endloc.S_CODE, 1);
                            LogHelper.Info("创建任务成功");
                        }
                    }
                    #endregion
                }
                else
                {
                    //拼接托盘号
                    cntrcode = CntrLoc.Select(a => a.S_CNTR_CODE).ToString();
                }
                startloc = LocationHelper.GetLoc(model.Loc);
                if (startloc == null)
                {
                    result.Code = -1;
                    result.Msg = $"根据起点:{model.Loc}未找到货位信息";
                    LogHelper.Info($"EquipmentInfo:{result.Msg}");
                    return result;
                }
                #endregion
                #region 计算立库终点
                endloc = LocationHelper.InStorage(startloc);
                if (endloc == null)
                {
                    result.Code = -1;
                    result.Msg = $"未计算到立库终点货位,请检查货位情况";
                    LogHelper.Info($"EquipmentInfo:{result.Msg}");
                    return result;
                }
                #endregion
                #region 创建任务
                if (startloc != null && endloc != null)
                {
                    var wcsTask = new WCSTask
                    {
                        S_CODE = WCSHelper.GenerateTaskNo(),
                        S_TYPE = "入库",
                        S_START_LOC = startloc.S_CODE,
                        S_END_LOC = endloc.S_CODE,
                        S_START_WH = startloc.S_WH_CODE,
                        S_START_AREA = startloc.S_AREA_CODE,
                        S_END_WH = endloc.S_WH_CODE,
                        S_END_AREA = endloc.S_AREA_CODE,
                        N_CNTR_COUNT = 1,
                        N_SCHEDULE_TYPE = 2,
                        S_CNTR_CODE = cntrcode
                    };
                    LogHelper.Info("创建入平库任务:" + JsonConvert.SerializeObject(wcsTask));
                    if (WCSHelper.CreateTask(wcsTask))
                    {
                        LocationHelper.LockLoc(startloc.S_CODE, 2);
                        LocationHelper.LockLoc(endloc.S_CODE, 1);
                        LogHelper.Info("创建任务成功");
                    }
                }
                #endregion
            }
            else //入库空
            {
@@ -413,11 +431,12 @@
                {
                    #region 获取起点信息
                    //根据托盘获取起点信息
                    var CntrLoc = db.Queryable<LocCntrRel>().Where(a => a.S_CNTR_CODE == cntrcode).First();
                    var CntrLoc = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == model.Loc).First();
                    if (CntrLoc == null)
                    {
                        result.Code = -1;
                        result.Msg = $"根据托盘:{cntrcode}未找到绑定托盘";
                        result.Msg = $"根据起点:{model.Loc}未找到绑定托盘";
                        LogHelper.Info($"EquipmentInfo:{result.Msg}");
                        return result;
                    }
@@ -433,7 +452,16 @@
                    #endregion
                    #region 计算立库终点
                    endloc = LocationHelper.InStorage(startloc);
                    //获取托盘物料绑定关系
                    var cntrItem = db.Queryable<CntrItemDetail>().Where(a => a.S_CNTR_CODE == CntrLoc.S_CNTR_CODE).First();
                    if (cntrItem == null)
                    {
                        result.Code = -1;
                        result.Msg = $"根据托盘:{model.CntrNo}未找到物料信息";
                        LogHelper.Info($"EquipmentInfo:{result.Msg}");
                        return result;
                    }
                    endloc = LocationHelper.InStorage(cntrItem.S_ITEM_CODE);
                    if (endloc == null)
                    {
                        result.Code = -1;
@@ -449,7 +477,7 @@
                        var wcsTask = new WCSTask
                        {
                            S_CODE = WCSHelper.GenerateTaskNo(),
                            S_TYPE = "入库",
                            S_TYPE = "物料入库",
                            S_START_LOC = startloc.S_CODE,
                            S_END_LOC = endloc.S_CODE,
                            S_START_WH = startloc.S_WH_CODE,
@@ -458,7 +486,7 @@
                            S_END_AREA = endloc.S_AREA_CODE,
                            N_CNTR_COUNT = 1,
                            N_SCHEDULE_TYPE = 2,
                            S_CNTR_CODE = cntrcode
                            S_CNTR_CODE = CntrLoc.S_CNTR_CODE
                        };
                        LogHelper.Info("创建入平库任务:" + JsonConvert.SerializeObject(wcsTask));
                        if (WCSHelper.CreateTask(wcsTask))
@@ -499,7 +527,7 @@
                        #endregion
                        #region 获取终点信息
                        string DiePanLoc = "";
                        //根据托盘获取起点信息
                        endloc = db.Queryable<Location>().Where(a => a.S_CODE == DiePanLoc).First();
                        if (endloc == null)
@@ -553,18 +581,51 @@
            return result;
        }
        internal static Result<Empty> ApplyDest(ApplyDestModel model)
        internal static Result<ReseltApplyDestModel> ApplyDest(ApplyDestModel model)
        {
            Result<Empty> result = new Result<Empty> { Code = 0, Msg = "创建成功" };
            Result<ReseltApplyDestModel> result = new Result<ReseltApplyDestModel> { Code = 0, Msg = "创建成功" };
            if (model == null)
            {
                result.Code = -1;
                result.Msg = "入参为空";
                return result;
            }
            var db = new SqlHelper<object>().GetInstance();
            string ItemCode = "";
            var task = WCSHelper.GetTask(model.TaskNo);
            if (task != null)
            {
                //根据任务托盘获取物料信息
                var cntrItem = db.Queryable<CntrItemDetail>().Where(a => a.S_CNTR_CODE == task.S_CNTR_CODE).First();
                if (cntrItem != null)
                {
                    ItemCode = cntrItem.S_ITEM_CODE;
                }
                var endloc = LocationHelper.InStorage(ItemCode);
                if (endloc != null)
                {
                    result.Data = new ReseltApplyDestModel() { Loc = endloc.S_CODE };
                }
                else
                {
                    result.Code = -1;
                    result.Msg = "当前没有可用终点可以分配";
                    return result;
                }
            }
            else
            {
                result.Code = 1;
                result.Msg = "任务不存在";
                return result;
            }
            return result;
        }