分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-05-31 53d6a24cb335b0c9b4449e1211ce8ea644d68d67
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
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Comm.MES_Info;
using WIDESEA_Comm.MES_Info.Request;
using WIDESEA_Comm.TaskNo;
using WIDESEA_Common;
using WIDESEA_Core.BaseProvider;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.ManageUser;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Entity.DomainModels.Mes;
using WIDESEA_WMS.Common;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static WIDESEA_Comm.MES_Info.BasicSN;
 
namespace WIDESEA_WMS
{
    public partial class ToMesServer
    {
        /// <summary>
        /// 人工出库
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent Outsource(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            VOLContext context = new VOLContext();
            Idt_inventoryRepository inventoryRepository = new dt_inventoryRepository(context);
            Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
            string sn = saveModel.MainData["dataSN"].ToString();   //车轮SN号
            string user = saveModel.MainData["creator"].ToString();
            string str = "";
            try
            {
                List<string> list = new List<string>();
                foreach (var SN in sn.Split(","))
                {
                    if (!string.IsNullOrEmpty(SN))
                        list.Add(SN);
                }
                var inventoryList = inventoryRepository.Find(x => list.Contains(x.SN)).ToList();
 
                foreach (var inventorys in inventoryList.GroupBy(x => x.stationCode))
                {
                    try
                    {
                        List<detail> lists = new List<detail>();
                        foreach (var inventory in inventorys)
                        {
                            detail detail = new detail();
                            detail.sn = inventory.SN;
                            lists.Add(detail);
                            str += str == "" ? inventory.SN : "," + inventory.SN;
                        }
                        agvInWarehousePara outWarehousePara = new agvInWarehousePara()
                        {
                            zoneID = inventorys.Key,
                            details = lists,
                            layerNo = 1,
                            stackID = "1",
                            warehouseName = "Agv库",
                            Operator = UserContext.Current.UserName,
                        };
 
                        Idt_info_to_mesRepository mesRepository = new dt_info_to_mesRepository(context);
                        var postJson = JsonConvert.SerializeObject(outWarehousePara);
                        dt_info_to_mes info_To_Mes = new dt_info_to_mes()
                        {
                            Info = postJson,
                            ActionName = "agvOutWarehouse",
                            Createtime = DateTime.Now,
                            Remark = "人工同步MES车轮出库",
                            State = false
                        };
                        mesRepository.Add(info_To_Mes, true);
 
                        foreach (var inventory in inventorys)
                        {
                            var station = stationinfoRepository.Find(x => x.stationCode == inventory.stationCode).FirstOrDefault();
                            station.quantity = station.quantity - 1;
                            if (station.quantity < 1)
                            {
                                station.quantity = 0;
                                station.stationType = string.Empty;
                                station.heatNumber = string.Empty;
                                station.Number = string.Empty;
                                station.billetID = string.Empty;
                                station.bindSN = string.Empty;
                                station.enable = false;
                                station.tray_status = string.Empty;
                                station.location_state = LocationStateEnum.Empty.ToString();
                            }
                            else
                            {
                                var bindSNS = station.bindSN.Split(",");
                                station.bindSN = OperStr(bindSNS, inventory.SN);
                                var billetS = station.billetID.Split(",");
                                station.billetID = OperStr(billetS, inventory.BilletNumber.ToString());
                            }
 
                            #region 取消跟踪DbContext中被跟踪的实体
                            var currentEntry = stationinfoRepository.DbContext.ChangeTracker.Entries<dt_stationinfo>().FirstOrDefault();
                            if (currentEntry != null) currentEntry.State = EntityState.Detached;
                            #endregion
 
                            stationinfoRepository.Update(station, true);
                            inventoryRepository.Delete(inventory, true);
                        }
                        WriteDBLog.Write($"人工出库成功 ", $"货位编号:{inventorys.Key};SN号:{str}", LogState.Sucess, "PDA", user);
 
                    }
                    catch (Exception ex)
                    {
                        content.Message = ex.Message;
                        WriteDBLog.Write($"人工出库失败 ", new { 错误信息 = ex.Message, 数据 = str }, LogState.Error, "PDA", user);
                    }
 
                }
 
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error($"人工出库失败!:{ex.Message}");
                WriteDBLog.Write($"人工出库失败 ", new { 错误信息 = ex.Message, 数据 = sn }, LogState.Error, "PDA", user);
            }
            return content;
        }
        public static string OperStr(string[] strArrty, string SN)
        {
            string[] newstr = strArrty.Where(x => x != SN).ToArray();
            string Newsn = string.Join(",", newstr);
 
            return Newsn;
        }
        /// <summary>
        /// 外协出库(检测上料)
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent OutsourceInbound(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string stationNo = saveModel.MainData["stationNo"].ToString();  //缓存架编号
                if (string.IsNullOrEmpty(stationNo)) return content.Error("请选择下料口!");
                string user = saveModel.MainData["creator"].ToString();
                string sn = saveModel.MainData["dataSN"].ToString();   //车轮SN号
 
                #region 判断SN号长度、是否存在相同SN号
                bool strOK = false;
                bool SNOK = false;
                for (int i = 1; i < sn.Split(",").Length; i++)
                {
                    for (int j = 0; j < i; j++)
                    {
                        if (sn.Split(",")[j] == sn.Split(",")[i])
                            strOK = true;
                        if (sn.Split(",")[j].Length != 10)
                            SNOK = true;
                    }
                }
                if (strOK)
                    return content.Error("存在相同SN号的车轮信息!请重新扫描!");
                if (SNOK)
                    return content.Error("车轮SN号有误!请重新扫描!");
                #endregion
 
                //dt_mes_detail info = null;
                //dt_mes_head mes_head = null;
                VV_Mes_Workinfo mes_Work = null;
 
                int count = 0;  //车轮数量
                string bindSN = "";
                List<string> list = new List<string>();
                foreach (var SN in sn.Split(","))
                {
                    if (!string.IsNullOrEmpty(SN))
                        list.Add(SN);
                }
                foreach (var item in list)
                {
                    if (item != null)
                    {
                        count++;
                        bindSN += bindSN == "" ? item : "," + item;
 
                        mes_Work = freeDB.Select<VV_Mes_Workinfo>().Where(x => x.SN == item && x.processCode == "28").First();
                        if (mes_Work == null)
                            return content.Error($"第{count}个车轮无检测上料工单信息,请核查工单后在扫描!");
                        //info = freeDB.Select<dt_mes_detail>().Where(x => x.SN == item).First();
                        //if (info == null)
                        //    return content.Error($"第{count}个车轮无检测上料工单详情,请核查工单后在扫描!");
                        //mes_head = freeDB.Select<dt_mes_head>().Where(x => x.jobID == info.jobID && x.processCode == "28").First();
                        //if (mes_head == null)
                        //    return content.Error($"第{count}个车轮无检测上料工单信息,请核查工单后在扫描!");
                    }
                }
                VOLContext Context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
                var station = freeDB.Select<dt_stationinfo>().Where(x => x.stationCode == stationNo).First();
                if (freeDB.Select<dt_agvtask>().Where(x => x.agv_fromaddress == station.stationCode).Any())
                    return content.Error($"下料口{stationNo}存在AGV任务,请核实!");
                if (!station.enable)
                    return content.Error($"下料口{stationNo}被禁用,请核实!");
                station.quantity = count;
                station.bindSN = bindSN;
                station.stationType = mes_Work.drawingNo;
                station.location_state = "Stroge";
                station.Number = mes_Work.jobID;
                if (mes_Work.heatID != null)
                    station.heatNumber = mes_Work.heatID;
                station.tray_status = "StrogeTray";
 
                stationinfoRepository.Update(station, true);
                #region MyRegion
                //dt_agvtask agvtask = new dt_agvtask
                //{
                //    //agv_barcode = stationNo,
                //    //agv_code = user,
                //    agv_createtime = DateTime.Now,
                //    agv_fromaddress = stationNo,
                //    agv_qty = i,
                //    agv_grade = 1,
                //    agv_tasktype = AGVTaskTypeEnum.TaskType_Inbound.ToString(),
                //    agv_taskstate = AGVTaskStateEnum.Create.ToString(),
                //    agv_toaddress = "",
                //};
                #endregion
 
                dt_agvtask agvtask = new dt_agvtask()
                {
                    agv_fromaddress = station.stationCode,
                    agv_id = Guid.NewGuid(),
                    agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                    agv_grade = 3,
                    agv_createtime = DateTime.Now,
                    agv_taskstate = "Queue",
                    agv_materielid = station.stationType,
                    agv_qty = station.quantity,
                    agv_tasktype = "TaskType_OutsourceOutbound",
                    agv_toaddress = "",
                    agv_userid = user,//"系统",
                    jobID = mes_Work.jobID,
                    bindSN = station.bindSN,
                    agv_worktype = Convert.ToInt32(mes_Work.processCode),
                    agv_materbarcode = mes_Work.materialCode,
                    agv_Traytype = station.tray_type,
                    agv_TrayStatus = station.tray_status
                };
                freeDB.Add(agvtask);
                return content.OK();
            }
            catch (Exception ex)
            {
                return content.Error($"呼叫AGV失败:{ex.Message}");
            }
        }
    }
}