using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using WMSPatch.EF; namespace WMSPatch { public partial class Form1 : Form { Thread sendWmsTaskThread; Thread finishWmsTaskThread; public Form1() { InitializeComponent(); sendWmsTaskThread = new Thread(sendWmsTask); sendWmsTaskThread.Start(); finishWmsTaskThread = new Thread(finishWmsTask); finishWmsTaskThread.Start(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { sendWmsTaskThread.Abort(); finishWmsTaskThread.Abort(); } /// /// 下发任务线程 /// public void sendWmsTask() { while (true) { using (Model edm = new Model()) { List statusList=new List(); statusList.Add("任务创建成功"); statusList.Add("任务下发失败"); statusList.Add("分配货位失败"); statusList.Add("目标货位未分配"); statusList.Add("等待移库任务完成"); statusList.Add("未分配到库存"); statusList.Add("源货位重复分配"); statusList.Add("设备故障已处理"); statusList.Add("目标货位重复分配"); List taskList = edm.Task_Queue.Where(x => statusList.Contains(x.TaskStatus) && x.FromPositionName.Contains("-") && x.DoCount < 2000).ToList(); for (int i = 1; i < 10; i++) { //优先移库 Task_Queue task = taskList.OrderBy(x => x.OrderNumber).ThenBy(x => x.Task_Id).FirstOrDefault(x => statusList.Contains(x.TaskStatus) && x.FromPositionName.Contains("-") && x.ToPositionName.Contains("-") && x.FromPositionName.Substring(0, 1) == i.ToString()); Task_Queue sendTask = new Task_Queue(); if (task != null) { //存在移库 sendTask=task; } else { task = taskList.OrderBy(x => x.OrderNumber).ThenBy(x => x.Task_Id).FirstOrDefault(x => statusList.Contains(x.TaskStatus) && x.FromPositionName.Contains("-") && !x.ToPositionName.Contains("-") && x.FromPositionName.Substring(0, 1) == i.ToString()); if (task != null) { //存在出库 //判断移库 int row = int.Parse(task.FromPositionName.Substring(3, 1)); if (row == 4) { row = 3; } if (row == 1) { row = 2; } string sidePosistionName = task.FromPositionName.Remove(3, 1); sidePosistionName = sidePosistionName.Insert(3, row.ToString()); Base_ProductPosition pp = edm.Base_ProductPosition.FirstOrDefault(x => x.ProductStorage > 0 && x.PositionName == sidePosistionName); if (pp != null) { string layer = pp.PositionName.Substring(8,2); Base_Position bpEmpty = edm.Base_Position.FirstOrDefault(x => x.IsFull == "N" && x.AreaCode == pp.AreaCode && x.RowCode == layer); //需要移库 Task_Queue newTask = new Task_Queue(); newTask.TaskType = "移库单"; newTask.Bill_Id = task.Bill_Id; newTask.BillCode = task.BillCode; newTask.TaskStatus = "任务创建成功"; newTask.PlatUser_Id = task.PlatUser_Id; newTask.UserProduct_Id = task.UserProduct_Id; newTask.CreateID = task.CreateID; newTask.Creator = task.Creator; newTask.CreateDate = DateTime.Now; newTask.ModifyDate = DateTime.Now; newTask.Enable = 1; newTask.DoCount = 0; newTask.IsDoing = 0; newTask.RelationTask_Id = task.Task_Id; newTask.FromPositionName = pp.PositionName; newTask.ToPositionName = bpEmpty.PositionName; newTask.PlateType = task.PlateType; newTask.PlateCode = task.PlateCode; newTask.OrderNumber = task.OrderNumber; newTask.ProductPosition_Id = pp.ProductPosition_Id; edm.Task_Queue.Add(newTask); edm.SaveChanges(); continue; } else { //可以出库 //出库重分配 if (task.FromPositionName.Contains("-")) { Base_Position bp = edm.Base_Position.FirstOrDefault(x => x.PositionName == task.FromPositionName); if (bp.IsFull != "Y") { //起始位无货(占用),另选 v3D展示数据 bpNow = edm.v3D展示数据.FirstOrDefault(x => x.器具编号 == task.PlateCode); Base_Position bpFrom = edm.Base_Position.FirstOrDefault(x => x.IsFull == "Y" && x.PositionName==bpNow.货位编号); if (bpFrom != null) { task.ToPositionName = bpFrom.PositionName; edm.SaveChanges(); } continue; } } sendTask = task; } } else { task = taskList.OrderBy(x => x.OrderNumber).ThenBy(x => x.Task_Id).FirstOrDefault(x => statusList.Contains(x.TaskStatus) && !x.FromPositionName.Contains("-") && x.ToPositionName.Contains("-") && x.FromPositionName.Substring(0, 1) == i.ToString()); if (task != null) { //入库重定向 if (task.ToPositionName.Contains("-")) { Base_Position bp = edm.Base_Position.FirstOrDefault(x => x.PositionName == task.ToPositionName); if (!string.IsNullOrEmpty(bp.IsFull) && bp.IsFull != "N") { //目标位有货(占用),另选一个 Base_Position bpEmpty = edm.Base_Position.FirstOrDefault(x => x.IsFull == "N" && x.AreaCode == bp.AreaCode && x.PlateType == bp.PlateType); task.ToPositionName = bpEmpty.PositionName; edm.SaveChanges(); continue; } } //存在入库 sendTask = task; } } } if (sendTask != null) { //下发任务 string sourcePlace = sendTask.FromPositionName; string toPlace = sendTask.ToPositionName; string palletTypeString = edm.Base_PlateType.FirstOrDefault(x => x.PlateType == sendTask.PlateType).PalletType; int palletType = int.Parse(palletTypeString); int taskType = 0; int outType = 0; if (sendTask.TaskType=="移库单") { taskType = 3; } else if (sendTask.ToPositionName.Contains("-")) { taskType = 1; } else { taskType = 2; } if (sendTask.TaskType == "出库单") { Sale_Order orderInfo = edm.Sale_Order.FirstOrDefault(x => x.Order_Id == sendTask.Bill_Id); if (!string.IsNullOrEmpty(orderInfo.OutTransId)) { if (orderInfo.DestinationName.Contains("分拣区")) { toPlace = "34"; // 5号口 } else if (orderInfo.OrderType == "焊装出库") { toPlace = "999"; } else if (orderInfo.OrderType == "调件出库") { toPlace = "997"; } } else { toPlace = orderInfo.OutTransId; } } //发送 SrmServer.SrmServiceClient srv = new SrmServer.SrmServiceClient(); SrmServer.SendTaskClass result = srv.SendTask(palletType,sourcePlace,toPlace,sendTask.Task_Id,taskType,outType); if (result.result) { //下发成功 Task_Queue taskSunccess = edm.Task_Queue.FirstOrDefault(x => x.Task_Id == sendTask.Task_Id); taskSunccess.TaskStatus = "任务下发成功"; if (taskType == 2) { int toTranId = result.outTransId; if (taskSunccess.Bill_Id != 0 && result.statusId != 90) { Sale_Order so = edm.Sale_Order.FirstOrDefault(x => x.Order_Id == taskSunccess.Bill_Id); so.OutTransId = toTranId.ToString(); taskSunccess.ToPositionName = toTranId.ToString(); } } Base_Position fromBp = edm.Base_Position.FirstOrDefault(x => x.PositionName == taskSunccess.FromPositionName); Base_Position toBp = edm.Base_Position.FirstOrDefault(x => x.PositionName == taskSunccess.ToPositionName); if (fromBp!=null) { fromBp.IsFull = "TEMPOUT"; } if (toBp != null) { toBp.IsFull = "TEMPIN"; } edm.SaveChanges(); } else { //下发失败 Task_Queue taskFail = edm.Task_Queue.FirstOrDefault(x => x.Task_Id == sendTask.Task_Id); if (result.statusId == 0) { taskFail.TaskStatus = "任务完成中"; } else if (result.statusId == 1) { taskFail.TaskStatus = "任务下发失败"; } else if (result.statusId == 2) { taskFail.TaskStatus = "输送线故障中"; } else if (result.statusId == 3) { taskFail.TaskStatus = "RGV故障"; } else if (result.statusId == 4) { taskFail.TaskStatus = "码垛机故障"; } else { taskFail.TaskStatus = "任务下发失败"; } edm.SaveChanges(); } Thread.Sleep(1000); } } } Thread.Sleep(1000); } } /// /// 完成任务线程 /// public void finishWmsTask() { while (true) { Thread.Sleep(1000); using (Model edm = new Model()) { List statusList=new List(); statusList.Add("任务下发成功"); statusList.Add("任务完成失败"); statusList.Add("任务完成中"); List taskList = edm.Task_Queue.Where(x => statusList.Contains(x.TaskStatus)).ToList(); foreach (var i in taskList) { SrmServer.SrmServiceClient srv = new SrmServer.SrmServiceClient(); SrmServer.TaskFinishClass result = srv.TaskFinish(i.Task_Id); ////异常 //if (result.isError == 1) //{ //} if (result.result == true) { //任务完成 Base_Position fromPlace = edm.Base_Position.FirstOrDefault(x => x.PositionName == i.FromPositionName); Base_Position toPlace = edm.Base_Position.FirstOrDefault(x => x.PositionName == i.ToPositionName); if (fromPlace != null) { fromPlace.IsFull = "N"; } if (toPlace != null) { toPlace.IsFull = "Y"; } } } edm.SaveChanges(); } } } } }