分支自 SuZhouGuanHong/TaiYuanTaiZhong

陈勇
2024-02-23 c9dd47185d603ccd09dd567f7eb5baeaf03b2746
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.FreeDB;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Comm;
using static System.Collections.Specialized.BitVector32;
 
namespace WIDESEA_WCS.JobsPart.Common
{
    public class StationTask
    {
 
        FreeDB freeDB = new FreeDB();
        public void CreateTask()
        {
            VOLContext context = new VOLContext();
            Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
            Idt_WorkinfoRepository workinfoRepository = new dt_WorkinfoRepository(context);
 
            List<dt_stationinfo> stationinfos = stationinfoRepository.Find(x => x.stationCode.Contains('X'));
 
            foreach (var item in stationinfos)
            {
                //根据下料口的绑定信息查询对应工单,检测此工单是否已完成  完成即可将其送入库
 
                dt_mes_head mesinfo = freeDB.Select<dt_mes_head>().Where(x => x.jobID == item.Number).First();
                int CompeletedNum = Convert.ToInt32(mesinfo.quantity) - Convert.ToInt32(mesinfo.finishNum);
 
                //判定任务是否已创建//如已存在  
                if (freeDB.Select<dt_agvtask>().Where(x => x.agv_fromaddress == item.stationCode).Count() > 0)
                    continue;
 
                if (item.quantity == 5 || CompeletedNum == 0)  //循环读取车轮数为5或者订单已完成数量的下料口    →工单人工关闭的工单
                {
                    if (mesinfo.quantity <= 50)    //小于50件直接出库
                    {
                        //todo 寻找可放货外协放货台
                        dt_agvtask agvtask = new dt_agvtask()
                        {
                            agv_fromaddress = item.stationCode,
                            agv_id = Guid.NewGuid(),
                            agv_grade = 0,
                            agv_barcode = "",
                            agv_createtime = DateTime.Now,
                            agv_taskstate = TaskStatus.Created.ToString(),
                            //agv_toaddress = ,
 
                        };
                        freeDB.Add(agvtask);
                    }
                    else
                    {
                        dt_stationinfo TargetLocation = GetEmptyLocation(stationinfoRepository, mesinfo, item);
 
                        if (TargetLocation != null)
                        {
                            //todo:  调用WMS接口创建任务
                            dt_agvtask agvtask = new dt_agvtask()
                            {
                                agv_fromaddress = item.stationCode,
                                agv_id = Guid.NewGuid(),
                                agv_grade = 0,
                                agv_barcode = "",
                                agv_createtime = DateTime.Now,
                                agv_taskstate = TaskStatus.Created.ToString(),
                                agv_toaddress = TargetLocation.stationCode,
                            };
 
                            freeDB.Add(agvtask);
                            TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                            freeDB.Update(TargetLocation);
                        }
                    }
                }
            }
        }
 
        /// <summary>
        /// 获取空库位
        /// </summary>
        /// <param name="stationinfoRepository">库存</param>
        /// <param name="workinfo">订单</param>
        /// <param name="stationinfo">下料口信息</param>
        /// <returns></returns>
        private dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository, dt_mes_head mesinfo, dt_stationinfo stationinfo)
        {
            //放货位
            dt_stationinfo TargetLocation = null;
 
 
            #region 根据订单数量来寻找对应库区且同物料类型库位
            //if (workinfo.PlannedQuantity < 50)    //库区1 物料类型多
            //{
            //    dt_stationinfo station = stationinfoRepository.Find(x => x.stationType == stationinfo.stationType && x.area == "1").OrderByDescending(x => x.lastUpdateTime).FirstOrDefault();
            //    if (station != null)
            //    {
            //        //如存在同物料类型且在1行的库存则新入库的物料放至同列 不存在则寻找新行库位
            //        if (station.line == 1)
            //        {
            //            TargetLocation = stationinfoRepository.Find(x => x.area == station.area && x.location_state == LocationStateEnum.Empty.ToString() && x.column == station.column && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
            //        }
            //        TargetLocation = stationinfoRepository.Find(x => x.area == station.area && x.location_state == LocationStateEnum.Empty.ToString() && x.line == 1 && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
            //    }
            //    else
            //    {
            //        TargetLocation = stationinfoRepository.Find(x => x.area == "1" && x.location_state == LocationStateEnum.Empty.ToString() && x.line == 1 && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
            //    }
            //}
            //else
            //{
            //int quantity = Convert.ToInt32(workinfo.PlannedQuantity);
            //string area = GetArea(quantity);
 
            //dt_materielinfo materielinfo = freeDB.Select<dt_materielinfo>().Where(x => x.materiel_id == stationinfo.stationType).First();
 
            //if (materielinfo == null)
            //    throw new Exception($"无此物料类型{stationinfo.stationType}库区");
 
            //string area = materielinfo.materiel_areaid.ToString();
 
            //如存在同类型物料则放置同行库位 不存在则放置新行的第一列中
            //dt_stationinfo station = stationinfoRepository.Find(x => x.stationType == stationinfo.stationType && x.area == area).OrderBy(x => x.lastUpdateTime).FirstOrDefault();
            //if (station != null)
            //{
            //    TargetLocation = stationinfoRepository.Find(x => x.area == station.area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.line == station.line).OrderBy(x => x.column).FirstOrDefault();
            //    if (TargetLocation == null)
            //    {
            //        TargetLocation = stationinfoRepository.Find(x => x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.column == '1').OrderBy(x => x.line).FirstOrDefault();
            //    }
            //}
            //else
            //{
            //    TargetLocation = stationinfoRepository.Find(x => x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.column == '1').OrderBy(x => x.line).FirstOrDefault();
            //}
 
            //todo  如物料已满则放置其他库区
            //if (TargetLocation == null)
            //{
            //    stationinfoRepository.Find(x => x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.column == '1').OrderBy(x => x.line).FirstOrDefault();
            //}
            //}
            //if (TargetLocation == null)
            //{
            //    throw new Exception("库位已满");
            //}
 
            //else if (workinfo.PlannedQuantity >= 50 && workinfo.PlannedQuantity < 110)  //库区2 
            //{
            //    //如存在同类型物料则放置同行库位 不存在则放置新行的第一列中
            //    dt_stationinfo station = stationinfoRepository.Find(x => x.stationType == stationinfo.stationType && x.area == "2").OrderBy(x => x.lastUpdateTime).FirstOrDefault();
            //    if (station != null)
            //    {
            //        TargetLocation  = stationinfoRepository.Find(x => x.area == station.area && x.location_state == LocationStateEnum.Empty.ToString() && x.line == station.line).OrderBy(x => x.column).FirstOrDefault();
            //    }
            //    else
            //    {
            //        TargetLocation = stationinfoRepository.Find(x => x.area == "2" && x.location_state == LocationStateEnum.Empty.ToString() && x.column == '1').OrderBy(x => x.line).FirstOrDefault();
            //    }
            //}
            //else if(workinfo.PlannedQuantity >= 110 && workinfo.PlannedQuantity < 180)
            //{
 
            //} 
            #endregion
 
            //根据订单炉号/工单号/类型
            if (mesinfo.quantity >= 180)
            {
                //查询库存记录中是否存在同类型/工单/炉号的记录  如有则放置同一行,无则寻找新库位
                var detail = freeDB.Select<dt_mes_detail>().Where(x => x.jobID == mesinfo.jobID).First();
                var inventory = freeDB.Select<dt_inventory>().Where(x => x.HeatNumber == detail.heatID /*&& x.*/).First();
                if (inventory != null)
                {
                    int line = Convert.ToInt16(inventory.stationCode.Split("-")[0]);
                    TargetLocation = stationinfoRepository.Find(x => x.line == line && x.location_state == LocationStateEnum.Empty.ToString() && x.area == inventory.area).FirstOrDefault();
                }
                else
                {
                    TargetLocation = stationinfoRepository.Find(x => x.area == "2" && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
                }
            }
 
            if (TargetLocation == null)
            {
                throw new Exception("库位已满");
            }
            return TargetLocation;
        }
 
        /// <summary>
        /// 根据订单数量返回库区(暂时弃用)   //更换为库位类型匹配
        /// </summary>
        /// <param name="quantity"></param>
        /// <returns></returns>
        private string GetArea(int quantity)
        {
            if (quantity >= 50 && quantity < 180)
                return "2";
            else if (quantity >= 180 && quantity < 180)
                return "3";
            else if (quantity >= 180 && quantity < 220)
                return "4";
            return null;
        }
    }
}