add
yirongjin
2025-07-23 d518ecbcbc853e891c389c422d1470b028ed1dc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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();
        }
 
        /// <summary>
        /// 下发任务线程
        /// </summary>
        public void sendWmsTask()
        {
            while (true)
            {
                using (Model edm = new Model())
                {
                    List<string> statusList=new List<string>();
                    statusList.Add("任务创建成功");
                    statusList.Add("任务下发失败");
                    statusList.Add("分配货位失败");
                    statusList.Add("目标货位未分配");
                    statusList.Add("等待移库任务完成");
                    statusList.Add("未分配到库存");
                    statusList.Add("源货位重复分配");
                    statusList.Add("设备故障已处理");
                    statusList.Add("目标货位重复分配");
 
                    List<Task_Queue> 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);
            }
        }
 
        /// <summary>
        /// 完成任务线程
        /// </summary>
        public void finishWmsTask()
        {
            while (true)
            {
                Thread.Sleep(1000);
                using (Model edm = new Model())
                {
                    List<string> statusList=new List<string>();
                    statusList.Add("任务下发成功");
                    statusList.Add("任务完成失败");
                    statusList.Add("任务完成中");
                    List<Task_Queue> 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();
 
                }
            }
        }
    }
}