/// <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; } } } } }
/// <summary> /// 转换 LES 原始任务报文。 /// </summary> /// <param name="xml">LES 原始任务报文。</param> /// <param name="dbContext">数据库上下文。</param> /// <returns>成功解析的实体。</returns> public static AST_LesTask 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 taskElement = requestElement.Descendants("ASSEMBLE").First(); List <XElement> taskItemElements = requestElement.Descendants("ASSEMBLEITEM").ToList(); if (taskItemElements.Count == 0) { throw new InterfaceDataException("没有明细。"); } AST_LesTask astLesTask = new AST_LesTask(); string projectCode = taskElement.Element("ProjectCode").Value; string projectStep = taskElement.Element("StageCode").Value; string[] codes = projectCode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string[] steps = projectStep.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (codes.Length > 1 && codes[0] == codes[1]) { projectCode = codes[0]; } if (steps.Length > 1 && steps[0] == steps[1]) { projectStep = steps[0]; } astLesTask.ProjectCode = projectCode; astLesTask.WbsId = taskElement.Element("PS_POSID").Value; astLesTask.ProjectStep = projectStep; astLesTask.BillCode = taskElement.Element("PickNO").Value; astLesTask.BillDate = DateTime.Parse(taskElement.Element("Bill_Date").Value, CultureInfo.InvariantCulture); //工位随时会新增 string cfgWorkStationCode = taskElement.Element("STATIONCODE").Value; CFG_WorkStation cfgWorkStation = dbContext.CFG_WorkStations .FirstOrDefault(ws => ws.Code == cfgWorkStationCode); if (cfgWorkStation == null) { using (GeelyPtlEntities innerDbContext = new GeelyPtlEntities()) { cfgWorkStation = new CFG_WorkStation(); cfgWorkStation.Code = cfgWorkStationCode; cfgWorkStation.Name = cfgWorkStation.Code; innerDbContext.CFG_WorkStations.Add(cfgWorkStation); //每个工位有 8 个车位 for (int position = 1; position <= 8; position++) { CFG_WorkStationCurrentCart cfgWorkStationCurrentCart = new CFG_WorkStationCurrentCart(); cfgWorkStationCurrentCart.CFG_WorkStation = cfgWorkStation; cfgWorkStationCurrentCart.Position = position; innerDbContext.CFG_WorkStationCurrentCarts.Add(cfgWorkStationCurrentCart); } innerDbContext.SaveChanges(); } } astLesTask.CFG_WorkStationId = cfgWorkStation.Id; astLesTask.GzzList = taskElement.Element("GZZLIST").Value; astLesTask.BatchCode = taskElement.Element("BatchCode").Value; string cfgChannelCode = taskElement.Element("ChannelCode").Value; CFG_Channel cfgChannel = dbContext.CFG_Channels .FirstOrDefault(c => c.Code == cfgChannelCode); if (cfgChannel == null) { throw new InterfaceDataException("无效的分拣巷道:" + cfgChannelCode); } astLesTask.CFG_ChannelId = cfgChannel.Id; //托盘随时会增加 string cfgPalletCode = taskElement.Element("PalletCode").Value; CFG_Pallet cfgPallet = dbContext.CFG_Pallets .FirstOrDefault(p => p.Code == cfgPalletCode); if (cfgPallet == null) { using (GeelyPtlEntities innerDbContext = new GeelyPtlEntities()) { cfgPallet = new CFG_Pallet(); cfgPallet.Code = cfgPalletCode; cfgPallet.PalletType = "01"; innerDbContext.CFG_Pallets.Add(cfgPallet); innerDbContext.SaveChanges(); } } astLesTask.CFG_PalletId = cfgPallet.Id; astLesTask.BoxCode = taskElement.Element("BoxCode").Value; astLesTask.FromPalletPosition = int.Parse(taskElement.Element("FromPalletPosition").Value, CultureInfo.InvariantCulture); if (astLesTask.FromPalletPosition < 1 || astLesTask.FromPalletPosition > 10) { astLesTask.FromPalletPosition = 1; } astLesTask.RequestTime = DateTime.Now; foreach (XElement taskItemElement in taskItemElements) { AST_LesTaskItem astLesTaskItem = new AST_LesTaskItem(); astLesTaskItem.AST_LesTask = astLesTask; astLesTaskItem.BillDetailId = taskItemElement.Element("BillDtlID").Value; astLesTaskItem.MaterialCode = taskItemElement.Element("MaterialCode").Value; astLesTaskItem.MaterialName = taskItemElement.Element("MaterialName").Value; astLesTaskItem.MaterialBarcode = taskItemElement.Element("MaterialBarcode").Value; astLesTaskItem.ToPickQuantity = (int)decimal.Parse(taskItemElement.Element("NEED_PICK_NUM").Value, CultureInfo.InvariantCulture); astLesTaskItem.MaxQuantityInSingleCartPosition = (int)decimal.Parse(taskItemElement.Element("MaxQuantityInSingleCartPosition").Value, CultureInfo.InvariantCulture); if (astLesTaskItem.MaxQuantityInSingleCartPosition <= 0) { astLesTaskItem.MaxQuantityInSingleCartPosition = int.MaxValue; } astLesTaskItem.IsSpecial = taskItemElement.Element("IsSpecial").Value == "1"; astLesTaskItem.IsBig = taskItemElement.Element("STORETYPE").Value == "04"; astLesTask.AST_LesTaskItems.Add(astLesTaskItem); } return(astLesTask); }