lss
7 天以前 50cdef950cce005f8eda95dd8fd23c3092d1b433
HH.WCS.Mobox3 - ´ó³µ¼ä/HH.WCS.Mobox3.JiaTong/api/ApiHelper.cs
@@ -39,13 +39,324 @@
        public static object _lockCreateTask = new object();
        /// <summary>
        /// ç«‹åº“任务下发=》创建任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static Result Createtask(CreateTask model)
        {
            Result result = new Result() { code = "0", msg = "创建成功" };
            if (model == null)
            {
                LogHelper.Info($"Createtask==> å‚数为null");
                result.code = "1";
                result.msg = "参数为null值,请检查参数格式";
                AddErrorInfo("下发参数为null", result.msg);
                return result;
            }
            partData partData = model.partData;
            taskData taskData = model.taskData;
            try
            {
                var db = new SqlHelper<object>().GetInstance();
                string CntrCode = "";//托盘编码
                string ItemCode = "";//物料编码
                if (partData != null)
                {
                    CntrCode = partData.rfid;//托盘编码
                    ItemCode = partData.partNumber;//物料编码
                }
                string Start = ""; //取货点
                string End = "";
                string note = ""; //立库下发任务需要通知mes接口物料到位,用备注自动进行区分
                                  //根据物料获取对应库区
                #region æ ¹æ®ä»»åŠ¡æ¥æºå­—æ®µåˆ¤æ–­åº“åŒºæ¥æºæ¥æº
                //var iteminfo = db.Queryable<TN_Material>().Where(a => a.S_ITEM_CODE.Trim() == ItemCode).First();
                //if (iteminfo != null)
                //{
                //    //LogHelper.Info($"Createtask==>物料数据:{JsonConvert.SerializeObject(iteminfo)}");
                //    areacode = iteminfo.S_WH_CODE;
                //}
                //if (string.IsNullOrEmpty(areacode))
                //{
                //    LogHelper.Info($"Createtask==>根据物料编码:{ItemCode}未能找到物料来源,需同步物料数据");
                //    result.code = "1";
                //    result.msg = $"根据物料编码:{ItemCode}未能找到物料来源,需同步物料数据";
                //    AddErrorInfo("寻找出库区失败", result.msg);
                //    return result;
                //}
                int n_type = 0;  //任务来源 1.密炼立库  3.预备库 ,4.原材料库
                string Source = "";
                switch (taskData.source)
                {
                    case "ML":
                        n_type = 1;
                        Source = "密炼";
                        break;
                    case "YBK":
                        n_type = 2;
                        Source = "预备库";
                        break;
                }
                Console.WriteLine($"Createtask==>任务来源:{n_type}");
                if (n_type == 0)
                {
                    result.code = "1";
                    result.msg = $"任务来源:{taskData.source}属于未定义库区,请检查物料来源是否属于约定字段";
                    AddErrorInfo("任务来源未定义", result.msg);
                    return result;
                }
                #endregion
                Location endloc = null;
                Location startloc = null;
                lock (_lockCreateTask)
                {
                    if (taskData.taskType == 1) //出库流程,通过物料机台计算线边货位
                    {
                        note = "出库";
                        startloc = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == taskData.pickStation).First();
                        if (startloc != null)
                        {
                            //创建托盘物料绑定信息,并计算终点货位
                            Start = taskData.pickStation;
                            if (CreateCntrIteminfo(partData, taskData))
                            {
                                endloc = Computeloc(CntrCode, taskData.dropStation, taskData.carrierType);
                                if (endloc != null)
                                {
                                    End = endloc.S_CODE;
                                }
                                else
                                {
                                    result.code = "1";
                                    result.msg = $"机台编码:{taskData.dropStation},未找到可用货位";
                                    AddErrorInfo("寻找出库货位失败", result.msg, Source);
                                    return result;
                                }
                                //endloc = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == taskData.dropStation).First();
                                //if (endloc != null)
                                //{
                                //    End = taskData.dropStation;
                                //}
                                //else
                                //{
                                //    result.code = "1";
                                //    result.msg = $"根据终点{End},找不到对应货位,请检查货位表";
                                //    AddErrorInfo("货位查找失败", result.msg, Source);
                                //    return result;
                                //}
                            }
                            else
                            {
                                result.code = "1";
                                result.msg = "创建托盘物料信息失败";
                                AddErrorInfo("创建托盘失败", result.msg, Source);
                                return result;
                            }
                        }
                        else
                        {
                            result.code = "1";
                            result.msg = $"Putin==> ä»»åŠ¡èµ·ç‚¹:{taskData.pickStation}找不到对应货位,请检查货位表";
                            LogHelper.Info($"Putin==> {result.msg}");
                            AddErrorInfo("货位查找失败", result.msg, Source);
                            return result;
                        }
                    }
                    else if (taskData.taskType == 2)//入库流程,直接使用起点货位和终点货位生成任务,起点需要有托盘
                    {
                        note = "入库";
                        startloc = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == taskData.pickStation).First();
                        if (startloc != null)
                        {
                            Start = startloc.S_CODE;
                            var CntrRel = LocationHelper.GetLocCntrRel(startloc.S_CODE).OrderBy(a => a.T_CREATE).FirstOrDefault();
                            if (CntrRel != null)
                            {
                                if (CntrRel.S_CNTR_CODE != partData.rfid)
                                {
                                    result.code = "1";
                                    result.msg = $"根据起点:{Start}获取托盘:{CntrRel.S_CNTR_CODE}与下发托盘:{partData.rfid}不一致,请检查托盘码是否正确";
                                    AddErrorInfo("托盘码校检失败", result.msg, Source);
                                    return result;
                                }
                                else
                                {
                                    CntrCode = CntrRel.S_CNTR_CODE;
                                }
                            }
                        }
                        else
                        {
                            result.code = "1";
                            result.msg = $"根据起点{taskData.pickStation},找不到对应货位,请检查货位表";
                            AddErrorInfo("货位查找失败", result.msg, Source);
                            return result;
                        }
                        endloc = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == taskData.dropStation).First();
                        if (endloc != null)
                        {
                            End = taskData.dropStation;
                        }
                        else
                        {
                            result.code = "1";
                            result.msg = $"根据终点{End},找不到对应货位,请检查货位表";
                            AddErrorInfo("货位查找失败", result.msg, Source);
                            return result;
                        }
                    }
                    else if (taskData.taskType == 3)//移库流程,直接使用起点货位和终点货位生成任务,
                    {
                        note = "出库";
                        //创建托盘物料绑定信息,并计算终点货位
                        startloc = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == taskData.pickStation).First();
                        if (startloc != null)
                        {
                            Start = taskData.pickStation;
                        }
                        else
                        {
                            result.code = "1";
                            result.msg = $"根据起点{taskData.pickStation},找不到对应货位,请检查货位表";
                            AddErrorInfo("货位查找失败", result.msg, Source);
                            return result;
                        }
                        endloc = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == taskData.dropStation).First();
                        if (endloc != null)
                        {
                            End = taskData.dropStation;
                        }
                        else
                        {
                            result.code = "1";
                            result.msg = $"根据终点{taskData.dropStation},找不到对应货位,请检查货位表";
                            AddErrorInfo("货位查找失败", result.msg, Source);
                            return result;
                        }
                    }
                    else
                    {
                        LogHelper.Info($"Createtask==> ä»»åŠ¡ç±»åž‹æœ‰è¯¯");
                        result.code = "1";
                        result.msg = "任务类型:{taskData.taskType}不在规定范围内";
                        AddErrorInfo("任务类型错误", result.msg, Source);
                    }
                        if (!string.IsNullOrEmpty(Start) && !string.IsNullOrEmpty(End))
                        {
                            if (startloc.N_LOCK_STATE != 0)
                            {
                                result.code = "1";
                                result.msg = $"创建任务失败,起点{Start}有锁";
                                AddErrorInfo("货位有锁", result.msg);
                                return result;
                            }
                            var startArea = db.Queryable<Area>().Where(a => a.S_CODE.Trim() == startloc.S_AREA_CODE.Trim()).First();
                            var endArea = db.Queryable<Area>().Where(a => a.S_CODE.Trim() == endloc.S_AREA_CODE.Trim()).First();
                            if (startArea == null || endArea == null)
                            {
                                LogHelper.Info($"起点:{JsonConvert.SerializeObject(startArea)}");
                                LogHelper.Info($"终点:{JsonConvert.SerializeObject(endArea)}");
                                result.code = "1";
                                result.msg = $"创建任务失败,起点:{startloc.S_AREA_CODE},终点{endloc.S_AREA_CODE}未找到对应库区";
                                AddErrorInfo("库区查找失败", result.msg);
                                return result;
                            }
                            int pri = 1;
                            var area = Settings.wareLoc.Where(a => a.AreaCode == endloc.S_WH_CODE).First();
                            if (area != null)
                            {
                                if (area.XbAreaCodes.Contains(endloc.S_AREA_CODE))
                                {
                                    pri = 2;
                                }
                            }
                            //创建wcs任务
                            var wcsTask = new WCSTask
                            {
                                S_CODE = WCSHelper.GenerateTaskNo(),
                                S_TYPE = startArea.S_NAME + "->" + endArea.S_NAME,
                                S_EQ_NO = taskData.taskNum,
                                S_START_LOC = Start,
                                S_END_LOC = End,
                                N_CNTR_COUNT = 1,
                                S_START_WH = startloc.S_WH_CODE,
                                S_END_WH = endloc.S_WH_CODE,
                                S_END_AREA = endloc.S_AREA_CODE,
                                N_SCHEDULE_TYPE = 1,
                                S_CNTR_CODE = CntrCode,
                                N_PRIORITY = pri,
                                Z_TYPE = n_type,
                                N_START_LAYER = 1,
                                N_END_LAYER = 1,
                                S_WMS_NO = taskData.wmsTaskNo,
                                S_NOTE = note,
                                S_MES_NO = taskData.mesTaskNo
                            };
                            LogHelper.Info("创建任务:" + JsonConvert.SerializeObject(wcsTask), "CreateTask");
                            if (WCSHelper.CreateTask(wcsTask))
                            {
                                // LocationHelper.LockLoc(Start, 2);
                                //LocationHelper.LockLoc(End, 1);
                                LogHelper.Info("创建任务成功");
                            }
                            else
                            {
                                result.code = "1";
                                result.msg = $"任务创建失败";
                                AddErrorInfo("任务创建失败", result.msg, Source);
                                return result;
                            }
                        }
                        else
                        {
                            result.code = "1";
                            result.msg = "创建任务失败,未找到对应的取货点或卸货点";
                            return result;
                        }
                }
                return result;
            }
            catch (Exception ex)
            {
                result.code = "1";
                result.msg = ex.ToString();
                LogHelper.Error("Createtask Error:" + ex.ToString() + "行号:" + ex.StackTrace, ex);
                return result;
            }
        }
        public static object _Point = new object();
        /// <summary>
@@ -394,7 +705,23 @@
        #endregion
        #region åˆè‚¥ä½³é€šä¸šåŠ¡æ–¹æ³•
        /// <summary>
        /// ç«‹åº“任务下发创建托盘物料绑定关系
        /// </summary>
        internal static bool CreateCntrIteminfo(partData partData, taskData taskData)
        {
            if (partData == null && taskData == null)
            {
                LogHelper.Info($"CreateCntrIteminfo==> ç«‹åº“任务下发参数有空,创建物料托盘绑定关系失败");
                return false;
            }
            else
            {
                return ContainerHelper.CreateCntrItem(taskData.pickStation, partData.rfid, partData.partNumber, partData.partDesc, partData.partType, taskData.carrierType, partData.lotNumber, taskData.wmsLot, partData.weight, partData.unit,
                    taskData.tyreType, taskData.supplier, taskData.receiveLot, taskData.subpool, partData.productionTime, partData.maturityTime, taskData.level, partData.qty, partData.qtyCfg);
            }
        }
        /// <summary>
        /// æ‰˜ç›˜å…¥åº“算法