分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-03-25 65151e7ac63e10113ecbeeabf302a4240bf08e97
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Comm.TaskNo;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Extensions;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static FreeSql.Internal.GlobalFilter;
using static System.Collections.Specialized.BitVector32;
 
namespace WIDESEA_WCS.JobsPart.Common
{
    public class OutboundTask
    {
        /// <summary>
        /// 创建出库任务
        /// </summary>
        public static void CreateOutboundTask()
        {
            try
            {
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(context);
                Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(context);
                Idt_inventoryRepository inventoryRepository = new dt_inventoryRepository(context);
                Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context);
 
                var mes_heads = mes_HeadRepository.Find(x => x.processCode == "28").OrderBy(x => x.expectedStartTime).ToList();
                //var mes_heads = mes_HeadRepository.Find(x => x.processCode == "28").OrderByDescending(x => x.CreateTime).ToList();
                foreach (var mes_head in mes_heads)
                {
                    #region 根据库存入库时间最晚查询
                    var inventory = inventoryRepository.Find(x => x.FigureNumber == mes_head.drawingNo).OrderByDescending(x => x.OnlineTime).FirstOrDefault();
                    if (inventory != null)
                    {
                        var Station = stationinfoRepository.Find(x => x.stationCode == inventory.stationCode).FirstOrDefault();
                        if (Station != null)
                        {
                            #region 存在入库任务不能出库
                            var InTask = stationinfoRepository.Find(x => x.line == Station.line && x.area == Station.area && x.location_state == LocationStateEnum.InBusy.ToString()).Any();
                            if (InTask) continue;
                            #endregion
                            var stationinfo = stationinfoRepository.Find(x => x.line == Station.line && x.area == Station.area && x.enable && x.location_state == LocationStateEnum.Stroge.ToString()).OrderByDescending(x => x.column).FirstOrDefault();
                            if (stationinfo != null)
                            {
                                if (string.IsNullOrEmpty(stationinfo.bindSN))
                                {
                                    stationinfo.remark = "车轮SN号信息异常";
                                    stationinfo.location_state = "Abnormal";
                                    stationinfoRepository.Update(stationinfo, true);
                                    continue;
                                    //throw new Exception(stationinfo.stationCode + "车轮SN号信息异常!");
                                }
                                if (agvtaskRepository.Find(x => x.agv_fromaddress == stationinfo.stationCode).Any())
                                    continue;
                                List<string> lists = new List<string>();
                                var SNS = stationinfo.bindSN.Split(",");
                                foreach (var SN in SNS)
                                {
                                    if (!string.IsNullOrEmpty(SN))
                                        lists.Add(SN);
                                }
                                foreach (var item in lists)
                                {
                                    //var a = mes_DetailRepository.Find(x => x.jobID == mes_head.jobID /*&& x.SN == item*/).ToList();
                                    //var b = a.Where(x => x.SN == item).FirstOrDefault();
 
                                    if (mes_DetailRepository.Find(x => x.jobID == mes_head.jobID && x.SN == item).Any())
                                    {
                                        dt_agvtask agvtask = new dt_agvtask()
                                        {
                                            agv_fromaddress = stationinfo.stationCode,
                                            agv_id = Guid.NewGuid(),
                                            agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                                            agv_grade = 2,
                                            agv_createtime = DateTime.Now,
                                            agv_taskstate = "Queue",
                                            agv_materielid = stationinfo.stationType,
                                            agv_qty = stationinfo.quantity,
                                            agv_tasktype = "TaskType_Outbound",
                                            agv_toaddress = "",
                                            agv_userid = "系统",
                                            bindSN = stationinfo.bindSN,
                                            jobID = mes_head.jobID,
                                            agv_worktype = Convert.ToInt32(mes_head.processCode),
                                            agv_materbarcode = mes_head.materialCode,
                                            agv_Traytype = stationinfo.tray_type,
                                            agv_TrayStatus = stationinfo.tray_status
                                        };
                                        stationinfo.location_state = LocationStateEnum.OutBusy.ToString();
                                        stationinfoRepository.Update(stationinfo, true);
                                        agvtaskRepository.Add(agvtask, true);
                                        WriteDBLog.Success("创建出库任务", $"任务编号:{agvtask.agv_tasknum}", "PCS");
                                        return;
                                    }
                                }
                            }
                            var stationinfos = stationinfoRepository.Find(x => x.stationType == mes_head.drawingNo && x.line != Station.line && x.area == Station.area && x.location_state == "Stroge").OrderBy(x => x.line).ThenByDescending(x => x.column).ToList();
                            foreach (var item in stationinfos)
                            {
                                if (item != null && stationinfo.enable)
                                {
                                    if (item.bindSN == null)
                                    {
                                        stationinfo.remark = "车轮SN号信息异常";
                                        stationinfo.location_state = "Abnormal";
                                        stationinfoRepository.Update(stationinfo, true);
                                        continue;
                                    }
                                    if (agvtaskRepository.Find(x => x.agv_fromaddress == stationinfo.stationCode).Any())
                                        continue;
                                    List<string> lists = new List<string>();
                                    var SNS = stationinfo.bindSN.Split(",");
                                    foreach (var SN in SNS)
                                    {
                                        if (!string.IsNullOrEmpty(SN))
                                            lists.Add(SN);
                                    }
                                    foreach (var list in lists)
                                    {
                                        if (mes_DetailRepository.Find(x => x.jobID == mes_head.jobID && x.SN == list).Any())
                                        {
                                            dt_agvtask agvtask = new dt_agvtask()
                                            {
                                                agv_fromaddress = stationinfo.stationCode,
                                                agv_id = Guid.NewGuid(),
                                                agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                                                agv_grade = 1,
                                                agv_createtime = DateTime.Now,
                                                agv_taskstate = "Queue",
                                                agv_materielid = stationinfo.stationType,
                                                agv_qty = stationinfo.quantity,
                                                jobID = mes_head.jobID,
                                                agv_tasktype = "TaskType_Outbound",
                                                agv_toaddress = "",
                                                agv_userid = "系统",
                                                bindSN = stationinfo.bindSN,
                                                agv_worktype = Convert.ToInt32(mes_head.processCode),
                                                agv_materbarcode = mes_head.materialCode,
                                                agv_Traytype = stationinfo.tray_type,
                                                agv_TrayStatus = stationinfo.tray_status
                                            };
                                            WriteDBLog.Success("创建出库任务", $"任务编号:{agvtask.agv_tasknum}", "PCS");
                                            agvtaskRepository.Add(agvtask, true);
                                            return;
                                        }
                                    }
                                }
                            }
 
                            #region MyRegion
                            //if (stationinfo != null && stationinfo.enable)
                            //{
                            //    if (stationinfo.bindSN == null)
                            //        throw new Exception(stationinfo.stationCode + "车轮SN号信息异常!");
                            //    List<string> lists = new List<string>();
                            //    var SNS = stationinfo.bindSN.Split(",");
                            //    foreach (var SN in SNS)
                            //    {
                            //        if (!string.IsNullOrEmpty(SN))
                            //            lists.Add(SN);
                            //    }
                            //    foreach (var item in lists)
                            //    {
                            //        if (freeDB.Select<dt_mes_detail>().Where(x => x.jobID == mes_head.jobID && x.SN == item).Any())
                            //        {
                            //            dt_agvtask agvtask = new dt_agvtask()
                            //            {
                            //                agv_fromaddress = stationinfo.stationCode,
                            //                agv_id = Guid.NewGuid(),
                            //                agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                            //                agv_grade = 1,
                            //                agv_createtime = DateTime.Now,
                            //                agv_taskstate = "Queue",
                            //                agv_materielid = stationinfo.stationType,
                            //                agv_qty = stationinfo.quantity,
                            //                agv_tasktype = "TaskType_Outbound",
                            //                agv_toaddress = "",
                            //                agv_userid = "系统",
                            //                bindSN = stationinfo.bindSN,
                            //                agv_worktype = Convert.ToInt32(mes_head.processCode),
                            //                agv_materbarcode = mes_head.materialCode,
                            //                agv_Traytype = stationinfo.tray_type,
                            //                agv_TrayStatus = stationinfo.tray_status
                            //            };
                            //            dt_stationinfo TargetLocation = GetEmptyLocation(stationinfoRepository);
                            //            if (TargetLocation != null)
                            //            {
                            //                agvtask.agv_taskstate = "Create";
                            //                agvtask.agv_toaddress = TargetLocation.stationCode;
                            //                TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                            //                freeDB.Update(TargetLocation);
                            //            }
                            //            freeDB.Add(agvtask);
                            //            return;
                            //        }
                            //    }
                            //}
                            #endregion
                        }
                    }
                    #endregion
 
                    #region MyRegion
                    //var station = stationinfoRepository.Find(x => x.stationType == mes_head.drawingNo && x.line != x.line).OrderBy(x => x.line).ThenByDescending(x => x.column).FirstOrDefault();
                    //bool ok = false;
                    //List<string> list = new List<string>();
                    //if (station != null && station.location_state == LocationStateEnum.Stroge.ToString() && station.enable)
                    //{
                    //    var SNS = station.bindSN.Split(",");
                    //    foreach (var SN in SNS)
                    //    {
                    //        if (!string.IsNullOrEmpty(SN))
                    //            list.Add(SN);
                    //    }
                    //}
                    //foreach (var SN in list)
                    //{
                    //    if (ok = freeDB.Select<dt_mes_detail>().Where(x => x.jobID == mes_head.jobID && x.SN == SN).Any()) continue;
                    //}
                    //if (ok)
                    //{
                    //    dt_agvtask agvtask = new dt_agvtask()
                    //    {
                    //        agv_fromaddress = station.stationCode,
                    //        agv_id = Guid.NewGuid(),
                    //        agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                    //        agv_grade = 1,
                    //        agv_createtime = DateTime.Now,
                    //        agv_taskstate = "Queue",
                    //        agv_materielid = station.stationType,
                    //        agv_qty = station.quantity,
                    //        agv_tasktype = "TaskType_Outbound",
                    //        agv_toaddress = "",
                    //        agv_userid = "系统",
                    //        bindSN = station.bindSN,
                    //        agv_worktype = Convert.ToInt32(mes_head.processCode),
                    //        agv_materbarcode = mes_head.materialCode,
                    //        agv_barcode = station.tray_type
                    //    };
                    //    dt_stationinfo TargetLocation = GetEmptyLocation(stationinfoRepository);
                    //    if (TargetLocation != null)
                    //    {
                    //        agvtask.agv_taskstate = "Create";
                    //        agvtask.agv_toaddress = TargetLocation.stationCode;
                    //        TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                    //        freeDB.Update(TargetLocation);
                    //    }
                    //    freeDB.Add(agvtask);
                    //}
                    #endregion
 
                }
            }
            catch (Exception ex)
            {
                WriteDBLog.Error("创建出库任务", $"错误信息:{ex.Message}", "PCS");
            }
        }
 
    }
}