分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-05-19 429bb4abbaea6fd6be33dcf62735d4ada5070b63
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Comm;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static System.Collections.Specialized.BitVector32;
 
namespace WIDESEA_WCS.JobsPart.Common
{
    public class GetLocation
    {
        private static readonly object GetEmptyLocationobj = new object();
        /// <summary>
        /// 获取可入库空货位
        /// </summary>
        /// <param name="stationinfoRepository"></param>
        /// <param name="mesinfo"></param>
        /// <param name="stationinfo"></param>
        /// <returns></returns>
        public static dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository, VV_Mes_Workinfo mesinfo, dt_agvtask agvtask)
        {
            lock (GetEmptyLocationobj)
            {
                try
                {
                    VOLContext context = new VOLContext();
                    Idt_inventoryRepository inventoryRepository = new dt_inventoryRepository(context);
                    string area1 = "";
                    if (agvtask.agv_Traytype == "LargeTray")
                        area1 = "4";
 
                    dt_stationinfo TargetLocation = null;
 
                    #region 寻找已分配同图号入库任务库位
                    TargetLocation = area1 == "4" ? stationinfoRepository.Find(x => x.stationType == agvtask.agv_materielid && x.location_state == LocationStateEnum.InBusy.ToString() && x.enable && x.area == area1).FirstOrDefault() : stationinfoRepository.Find(x => x.stationType == agvtask.agv_materielid && x.location_state == LocationStateEnum.InBusy.ToString() && (x.area == "2" || x.area == "3") && x.enable).FirstOrDefault();
                    if (TargetLocation != null)
                    {
                        TargetLocation = stationinfoRepository.Find(x => x.line == TargetLocation.line && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == TargetLocation.area).OrderBy(x => x.column).FirstOrDefault();
                        if (TargetLocation != null)
                            if (!GetStation.OutBusyStation(TargetLocation.stationCode) && !GetStation.QueueStation(TargetLocation.stationCode)) return TargetLocation;
                    }
                    #endregion
 
                    #region 查找库存
                    dt_inventory inventory = null;
                    inventory = area1 == "4" ? inventoryRepository.Find(x => x.FigureNumber == agvtask.agv_materielid && x.area == area1).OrderByDescending(x => x.OnlineTime).FirstOrDefault()
                        : inventoryRepository.Find(x => x.FigureNumber == agvtask.agv_materielid && (x.area == "2" || x.area == "3")).OrderByDescending(x => x.OnlineTime).FirstOrDefault();
                    #endregion
 
 
                    #region 根据库存查找可入库空货位
                    if (inventory != null)
                    {
                        var station = stationinfoRepository.FindFirst(x => x.stationCode == inventory.stationCode);
 
                        #region 寻找已分配同图号入库任务库位
                        //TargetLocation = stationinfoRepository.Find(x => x.stationType == agvtask.agv_materielid && x.location_state == LocationStateEnum.InBusy.ToString() && x.area == station.area).FirstOrDefault();
                        //if (TargetLocation != null)
                        //{
                        //    TargetLocation = stationinfoRepository.Find(x => x.line == TargetLocation.line && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == TargetLocation.area).OrderBy(x => x.column).FirstOrDefault();
                        //    if (TargetLocation != null)
                        //    {
                        //        if (!stationinfoRepository.Find(x => x.line == TargetLocation.line && x.area == TargetLocation.area && x.location_state == LocationStateEnum.OutBusy.ToString()).Any())
                        //            return TargetLocation;
                        //    }
                        //}
                        #endregion
 
                        #region 判断当前行是否还有空货位
                        TargetLocation = stationinfoRepository.Find(x => x.line == station.line && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == station.area).OrderBy(x => x.column).FirstOrDefault();
                        if (TargetLocation != null)
                        {
                            if (!GetStation.OutBusyStation(TargetLocation.stationCode) && !GetStation.QueueStation(TargetLocation.stationCode))
                                return TargetLocation;
                        }
                        #endregion
 
                        #region 寻找空货位
                        //var TargetLocations1 = stationinfoRepository.Find(x => x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == station.area).OrderBy(x => x.line).ThenBy(x => x.column).ToList();
                        //foreach (var Location in TargetLocations1)
                        //{
                        //    if (stationinfoRepository.Find(x => x.line == Location.line && x.area == Location.area && x.stationType == agvtask.agv_materielid).Any())
                        //        if (!stationinfoRepository.Find(x => x.line == Location.line && x.area == Location.area && x.location_state == LocationStateEnum.OutBusy.ToString()).Any()) return Location;
                        //}
                        #endregion
 
                    }
                    #endregion
 
                    #region 寻找已分配同图号入库任务库位
                    //TargetLocation = stationinfoRepository.Find(x => x.stationType == agvtask.agv_materielid && x.location_state == LocationStateEnum.InBusy.ToString() && (x.area == "1" || x.area == "2" || x.area == "3" || x.area == "4")).FirstOrDefault();
                    //if (TargetLocation != null)
                    //{
                    //    if (TargetLocation.area == "1")
                    //    {
                    //        TargetLocation = stationinfoRepository.Find(x => x.line == TargetLocation.line && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == TargetLocation.area).OrderBy(x => x.column).FirstOrDefault();//按行存放
 
                    //        if (TargetLocation != null)
                    //        {
                    //            if (!stationinfoRepository.Find(x => x.column == TargetLocation.column && x.area == TargetLocation.area /*&& x.enable*/ && x.location_state == LocationStateEnum.OutBusy.ToString()).Any())
                    //                return TargetLocation;
                    //        }
                    //    }
                    //    else
                    //    {
                    //        TargetLocation = stationinfoRepository.Find(x => x.line == TargetLocation.line && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == TargetLocation.area).OrderBy(x => x.column).FirstOrDefault();
                    //        if (TargetLocation != null)
                    //        {
                    //            if (!stationinfoRepository.Find(x => x.line == TargetLocation.line && x.area == TargetLocation.area && x.location_state == LocationStateEnum.OutBusy.ToString()).Any())
                    //                return TargetLocation;
                    //        }
                    //    }
                    //}
                    #endregion
 
                    #region 寻找对应库区空货位
                    //var TargetLocations = area1 == "4" ? stationinfoRepository.Find(x => x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == "4").OrderBy(x => x.line).ThenBy(x => x.column).ToList()
                    //    : stationinfoRepository.Find(x => x.location_state == LocationStateEnum.Empty.ToString() && x.enable && (x.area == "2" || x.area == "3")).OrderBy(x => x.line).ThenBy(x => x.column).ToList();
                    //foreach (var Location in TargetLocations)
                    //{
                    //    if (stationinfoRepository.Find(x => x.line == Location.line && x.area == Location.area && x.stationType == agvtask.agv_materielid).Any())
                    //        if (!stationinfoRepository.Find(x => x.line == Location.line && x.area == Location.area && x.location_state == LocationStateEnum.OutBusy.ToString()).Any()) return Location;
                    //}
                    #endregion
 
                    #region 未找到库存或当前库存行已满,寻找新的一行
                    var area = GetArea(mesinfo.quantity, agvtask.agv_Traytype);
                    TargetLocation = stationinfoRepository.Find(x => x.column == (area == "2" ? 1 : 2) && x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).FirstOrDefault();
                    if (TargetLocation == null && area != "4")
                    {
                        area = area == "2" ? "3" : "2";
                        TargetLocation = stationinfoRepository.Find(x => x.column == (area == "2" ? 1 : 2) && x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).FirstOrDefault();
                    }
 
                    #region 查看当前空货位后是否存在有料
                    if (TargetLocation != null)
                        if (GetStation.QueueStation(TargetLocation.stationCode)) TargetLocation = null;
                    #endregion
 
                    #endregion
 
                    if (TargetLocation == null) throw new Exception("库位已满");
                    return TargetLocation;
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
 
        /// <summary>
        /// 获取入库空货位
        /// </summary>
        /// <param name="stationinfoRepository"></param>
        /// <param name="mesinfo"></param>
        /// <param name="stationinfo"></param>
        /// <param name="CompeletedNum"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository, dt_mes_head mesinfo, dt_stationinfo stationinfo)
        {
            lock (GetEmptyLocationobj)
            {
                try
                {
                    VOLContext context = new VOLContext();
                    Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(context);
                    Idt_inventoryRepository inventoryRepository = new dt_inventoryRepository(context);
                    var detail = mes_DetailRepository.Find(x => x.jobID == mesinfo.jobID).FirstOrDefault();
                    if (detail == null)
                    {
                        stationinfo.remark = "触发入库任务,但未找到工单子表!";
                        stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                        stationinfoRepository.Update(stationinfo, true);
                        return null;
                    }
                    #region 查找库存
                    dt_inventory inventory = null;
                    if (detail.heatID != null)
                    {
                        inventory = inventoryRepository.Find(x => /*x.HeatNumber == detail.heatID &&*/ x.FigureNumber == mesinfo.drawingNo /*&& x.jobID == mesinfo.jobID*/).OrderByDescending(x => x.OnlineTime).FirstOrDefault();
                    }
                    else
                    {
                        inventory = inventoryRepository.Find(x => x.FigureNumber == mesinfo.drawingNo /*&& x.jobID == mesinfo.jobID*/).OrderByDescending(x => x.OnlineTime).FirstOrDefault();
                    }
                    #endregion
 
                    dt_stationinfo TargetLocation = null;
 
                    #region 根据库存查找可入库空货位
                    if (inventory != null)
                    {
                        var station = stationinfoRepository.FindFirst(x => x.stationCode == inventory.stationCode);
                        if (station.area == "1")
                        {
                            TargetLocation = stationinfoRepository.Find(x => x.column == station.column && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == inventory.area).OrderByDescending(x => x.line).FirstOrDefault();
                            if (TargetLocation != null)
                            {
                                if (!stationinfoRepository.Find(x => x.column == TargetLocation.column && x.area == TargetLocation.area /*&& x.enable*/ && x.location_state == LocationStateEnum.OutBusy.ToString()).Any())
                                    return TargetLocation;
                            }
                        }
                        else
                        {
                            TargetLocation = stationinfoRepository.Find(x => x.line == station.line && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == inventory.area).OrderBy(x => x.column).FirstOrDefault();
                            if (TargetLocation != null)
                            {
                                if (!stationinfoRepository.Find(x => x.line == TargetLocation.line && x.area == TargetLocation.area && x.location_state == LocationStateEnum.OutBusy.ToString()).Any())
                                    return TargetLocation;
                            }
                        }
                    }
                    #endregion
 
                    #region 未找到库存或当前库存行已满,寻找新的一行
                    var area = string.IsNullOrEmpty(mesinfo.area) ? GetArea(mesinfo.quantity, stationinfo.tray_type) : mesinfo.area;
                    TargetLocation = stationinfoRepository.Find(x => x.column == (area == "2" ? 1 : 10) && x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
                    if (TargetLocation == null)
                    {
                        if (area == "2")
                            area = "3";
                        else if (area == "3")
                            area = "2";
                        TargetLocation = stationinfoRepository.Find(x => x.column == (area == "2" ? 1 : 10) && x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
                        if (TargetLocation == null && area != "4")
                            TargetLocation = stationinfoRepository.Find(x => x.line == 2 && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == "1").OrderBy(x => x.column).FirstOrDefault();
                    }
                    #endregion
 
                    if (TargetLocation == null) throw new Exception("库位已满");
                    return TargetLocation;
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// 根据托盘类型选择库区
        /// </summary>
        /// <param name="quantity"></param>
        /// <param name="tray_type"></param>
        /// <returns></returns>
        private static string GetArea(int quantity, string tray_type)
        {
            if (tray_type == "SmallTray")
            {
                if (quantity <= 180)
                    return "2";
                else
                    return "3";
            }
            else
                return "4";
        }
 
        /// <summary>
        /// 根据车轮大小选择库区
        /// </summary>
        /// <param name="quantity"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private static string GetArea(int quantity, float e)
        {
            if (e < 1100)
            {
                if (quantity <= 180)
                    return "2";
                else
                    return "3";
            }
            else
                return "4";
        }
    }
}