Exemple #1
0
        /// <summary>
        /// 转换托盘抵达分拣口报文。
        /// </summary>
        /// <param name="xml">托盘抵达分拣口报文。</param>
        /// <param name="dbContext">数据库上下文。</param>
        /// <returns>成功解析的实体。</returns>
        public static AST_PalletArrived ConvertRequest(string xml, GeelyPtlEntities dbContext)
        {
            XDocument xDocument      = XDocument.Parse(xml);
            XElement  serviceElement = xDocument.Descendants("Service").First();
            XElement  dataElement    = serviceElement.Descendants("Data").First();
            XElement  requestElement = dataElement.Descendants("Request").First();
            XElement  arrivedElement = requestElement.Descendants("ASSEMBLE").First();

            AST_PalletArrived astPalletArrived = new AST_PalletArrived();

            astPalletArrived.ProjectCode = arrivedElement.Element("ProjectCode").Value;
            astPalletArrived.WbsId       = arrivedElement.Element("PS_POSID").Value;
            astPalletArrived.ProjectStep = arrivedElement.Element("StageCode").Value;

            string batchCode = arrivedElement.Element("BatchCode").Value;

            if (!dbContext.AST_LesTasks.Any(lt => lt.BatchCode == batchCode))
            {
                throw new InterfaceDataException("该批次的任务未下发:" + batchCode);
            }

            astPalletArrived.BatchCode = batchCode;

            string      cfgChannelCode = arrivedElement.Element("ChannelCode").Value;
            CFG_Channel cfgChannel     = dbContext.CFG_Channels
                                         .FirstOrDefault(c => c.Code == cfgChannelCode);

            if (cfgChannel == null)
            {
                throw new InterfaceDataException("无效的分拣巷道:" + cfgChannelCode);
            }

            astPalletArrived.CFG_ChannelId = cfgChannel.Id;

            string     cfgPalletCode = arrivedElement.Element("PalletCode").Value;
            CFG_Pallet cfgPallet     = dbContext.CFG_Pallets
                                       .FirstOrDefault(c => c.Code == cfgPalletCode);

            if (cfgPallet == null)
            {
                throw new InterfaceDataException("无效的托盘编码:" + cfgPalletCode);
            }

            cfgPallet.PalletType = arrivedElement.Element("PalletType").Value;

            astPalletArrived.CFG_PalletId = cfgPallet.Id;
            astPalletArrived.PickBillIds  = arrivedElement.Element("PickBillIDs").Value;
            astPalletArrived.ArrivedTime  = DateTime.Now;

            return(astPalletArrived);
        }
        /// <summary>
        /// 按托合并原始分拣任务。
        /// </summary>
        /// <param name="astPalletArrived">从接口解析还未持久化的托盘抵达记录。</param>
        /// <param name="dbContext">数据上下文。</param>
        public static void Generate(AST_PalletArrived astPalletArrived, GeelyPtlEntities dbContext)
        {
            List <AST_LesTask> astLesTasks = dbContext.AST_LesTasks
                                             .Where(lt => lt.BatchCode == astPalletArrived.BatchCode &&
                                                    lt.CFG_PalletId == astPalletArrived.CFG_PalletId &&
                                                    lt.CFG_ChannelId == astPalletArrived.CFG_ChannelId &&
                                                    astPalletArrived.PickBillIds.Contains(lt.BillCode) &&
                                                    !lt.TaskGenerated)
                                             .ToList();

            if (astLesTasks.Count > 0)
            {
                AST_LesTask mainAstLesTask = astLesTasks.First();

                AST_PalletTask astPalletTask = new AST_PalletTask();
                astPalletTask.CFG_PalletId  = astPalletArrived.CFG_PalletId;
                astPalletTask.BatchCode     = mainAstLesTask.BatchCode;
                astPalletTask.PickBillIds   = astPalletArrived.PickBillIds;
                astPalletTask.ProjectCode   = astPalletArrived.ProjectCode;
                astPalletTask.WbsId         = mainAstLesTask.WbsId;
                astPalletTask.ProjectStep   = astPalletArrived.ProjectStep;
                astPalletTask.CFG_ChannelId = astPalletArrived.CFG_ChannelId;
                astPalletTask.PickStatus    = PickStatus.New;
                astPalletTask.CreateTime    = DateTime.Now;

                dbContext.AST_PalletTasks.Add(astPalletTask);

                //提取当前目标工位、当前批次、当前巷道、当前托盘未合并的原始任务
                List <int> cfgWorkStationIds = astLesTasks
                                               .Select(lt => lt.CFG_WorkStationId)
                                               .Distinct()
                                               .ToList();
                foreach (int cfgWorkStationId in cfgWorkStationIds)
                {
                    ILookup <int, AST_LesTask> astLesTaskLookupByFromPalletPosition = astLesTasks
                                                                                      .Where(lt => lt.CFG_WorkStationId == cfgWorkStationId)
                                                                                      .OrderBy(lt => lt.FromPalletPosition)
                                                                                      .ToLookup(lt => lt.FromPalletPosition);

                    //明细的合并,特殊件单独拣
                    foreach (IGrouping <int, AST_LesTask> astLesTaskGroupingByFromPalletPosition in astLesTaskLookupByFromPalletPosition)
                    {
                        int         fromPalletPosition          = astLesTaskGroupingByFromPalletPosition.Key;
                        AST_LesTask mainAstLesTaskByWorkStation = astLesTaskGroupingByFromPalletPosition.First();

                        List <AST_LesTaskItem> normalAstLesTaskItems  = new List <AST_LesTaskItem>();
                        List <AST_LesTaskItem> specialAstLesTaskItems = new List <AST_LesTaskItem>();

                        foreach (AST_LesTask astLesTask in astLesTaskGroupingByFromPalletPosition)
                        {
                            normalAstLesTaskItems.AddRange(astLesTask.AST_LesTaskItems.Where(lti => !lti.IsSpecial));
                            specialAstLesTaskItems.AddRange(astLesTask.AST_LesTaskItems.Where(lti => lti.IsSpecial));
                        }

                        //普通件
                        if (normalAstLesTaskItems.Count > 0)
                        {
                            AST_LesTaskItem mainAstLesTaskItem  = normalAstLesTaskItems.First();
                            int             totalNormalQuantity = normalAstLesTaskItems.Sum(lti => lti.ToPickQuantity);

                            AST_PalletTaskItem astPalletTaskItem = new AST_PalletTaskItem();
                            astPalletTaskItem.AST_PalletTask     = astPalletTask;
                            astPalletTaskItem.CFG_WorkStationId  = cfgWorkStationId;
                            astPalletTaskItem.BoxCode            = mainAstLesTaskByWorkStation.BoxCode;
                            astPalletTaskItem.FromPalletPosition = fromPalletPosition;
                            astPalletTaskItem.MaterialCode       = mainAstLesTaskItem.MaterialCode;
                            astPalletTaskItem.MaterialName       = mainAstLesTaskItem.MaterialName;
                            astPalletTaskItem.MaterialBarcode    = mainAstLesTaskItem.MaterialBarcode;
                            astPalletTaskItem.ToPickQuantity     = totalNormalQuantity;
                            astPalletTaskItem.MaxQuantityInSingleCartPosition = mainAstLesTaskItem.MaxQuantityInSingleCartPosition;
                            astPalletTaskItem.IsSpecial  = false;
                            astPalletTaskItem.IsBig      = mainAstLesTaskItem.IsBig;
                            astPalletTaskItem.PickStatus = PickStatus.New;

                            dbContext.AST_PalletTaskItems.Add(astPalletTaskItem);

                            foreach (AST_LesTaskItem normalAstLesTaskItem in normalAstLesTaskItems)
                            {
                                normalAstLesTaskItem.AST_PalletTaskItem = astPalletTaskItem;
                            }
                        }

                        //特殊件
                        foreach (AST_LesTaskItem astLesTaskItem in specialAstLesTaskItems)
                        {
                            AST_PalletTaskItem astPalletTaskItem = new AST_PalletTaskItem();
                            astPalletTaskItem.AST_PalletTask     = astPalletTask;
                            astPalletTaskItem.CFG_WorkStationId  = cfgWorkStationId;
                            astPalletTaskItem.BoxCode            = mainAstLesTaskByWorkStation.BoxCode;
                            astPalletTaskItem.FromPalletPosition = fromPalletPosition;
                            astPalletTaskItem.MaterialCode       = astLesTaskItem.MaterialCode;
                            astPalletTaskItem.MaterialName       = astLesTaskItem.MaterialName;
                            astPalletTaskItem.MaterialBarcode    = astLesTaskItem.MaterialBarcode;
                            astPalletTaskItem.ToPickQuantity     = astLesTaskItem.ToPickQuantity;
                            astPalletTaskItem.MaxQuantityInSingleCartPosition = astLesTaskItem.MaxQuantityInSingleCartPosition;
                            astPalletTaskItem.IsSpecial  = true;
                            astPalletTaskItem.IsBig      = astLesTaskItem.IsBig;
                            astPalletTaskItem.PickStatus = PickStatus.New;

                            dbContext.AST_PalletTaskItems.Add(astPalletTaskItem);

                            astLesTaskItem.AST_PalletTaskItem = astPalletTaskItem;
                        }

                        //标记已合并按托任务
                        foreach (AST_LesTask astLesTask in astLesTaskGroupingByFromPalletPosition)
                        {
                            astLesTask.TaskGenerated = true;
                        }
                    }
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 托盘抵达分拣口。
        /// </summary>
        public string LesSendBoxPTL(string xml)
        {
            try { XDocument.Parse(xml); }
            catch (Exception ex) { return("ERROR:" + ex.Message); }

            try
            {
                using (GeelyPtlEntities dbContext = new GeelyPtlEntities())
                {
                    AST_PalletArrived astPalletArrived = AST_PalletArrivedConverter.ConvertRequest(xml, dbContext);
                    if (!dbContext.AST_PalletArriveds.Any(pa => pa.BatchCode == astPalletArrived.BatchCode &&
                                                          pa.CFG_PalletId == astPalletArrived.CFG_PalletId &&
                                                          pa.PickBillIds == astPalletArrived.PickBillIds))
                    {
                        AST_PalletArrivedMessage astPalletArrivedMessage = new AST_PalletArrivedMessage();
                        astPalletArrivedMessage.AST_PalletArrived = astPalletArrived;
                        astPalletArrivedMessage.ReceivedXml       = xml;
                        astPalletArrivedMessage.ReceivedTime      = DateTime.Now;

                        dbContext.AST_PalletArriveds.Add(astPalletArrived);
                        dbContext.AST_PalletArrivedMessages.Add(astPalletArrivedMessage);

                        //托盘到达时按托合并任务
                        CFG_ChannelCurrentPallet cfgChannelCurrentPallet = dbContext.CFG_ChannelCurrentPallets
                                                                           .First(ccp => ccp.CFG_ChannelId == astPalletArrived.CFG_ChannelId);
                        cfgChannelCurrentPallet.CFG_PalletId = astPalletArrived.CFG_PalletId;
                        cfgChannelCurrentPallet.ArrivedTime  = astPalletArrived.ArrivedTime;

                        CFG_Pallet cfgPallet = astPalletArrived.CFG_Pallet;
                        cfgPallet.PalletRotationStatus = PalletRotationStatus.Normal;

                        //按托任务生成
                        AssortingTaskGenerator.Generate(astPalletArrived, dbContext);

                        dbContext.SaveChanges();
                    }
                }

                return(AST_PalletArrivedConverter.ConvertResponse(xml, true, string.Empty));
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                DbEntityValidationException dbEntityValidationException = ex as DbEntityValidationException;
                if (dbEntityValidationException != null)
                {
                    foreach (DbEntityValidationResult validationResult in dbEntityValidationException.EntityValidationErrors)
                    {
                        foreach (DbValidationError validationError in validationResult.ValidationErrors)
                        {
                            message += Environment.NewLine + validationError.ErrorMessage;
                        }
                    }
                }
                message += Environment.NewLine + ex.StackTrace;

                Logger.Log("LesSendBoxPTL", DateTime.Now.ToString("HH:mm:ss") + Environment.NewLine +
                           xml + Environment.NewLine
                           + message + Environment.NewLine
                           + Environment.NewLine);
                return(AST_PalletArrivedConverter.ConvertResponse(xml, false, message));
            }
        }