bklLiudl
2024-07-23 675b8bcc4a3630d95e3d0b97d933e63442075ecb
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Commom.DotNetDevices;
using Common;
using Model;
using Model;
 
namespace BLL.DAL
{
    public class DAL_ConveyorInfo
    {
        public List<WCSPlcInfo> GetList(AjaxPlcInfoList Json, ref PageInfo pageInfo)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select a.IP,b.* from WCSPlcInfo b left join WCSIP a on b.PlcIP = a.Id where b.IsDel = 0 and a.Type = '1' ");
                if (!string.IsNullOrWhiteSpace(Json.StationNum))
                {
                    strSql.Append($"and b.StationNum = '{Json.StationNum}'  ");
                }
                if (!string.IsNullOrWhiteSpace(Json.Text))
                {
                    strSql.Append($"and b.Text = '{Json.Text}'  ");
                }
                DataTable dt = DataFactory.SqlDataBase().GetPageList(strSql.ToString(), null, "CreateTime", "", ref pageInfo);
                if (dt != null)
                {
                    DALWcsMessage wcsSql = new DALWcsMessage();
                    var plcIpDt = wcsSql.GetPlcIps();
                    dt.Columns.Add("PlcStatusVal", Type.GetType("System.String"));
                    foreach (DataRow item in dt.Rows)
                    {
                        DataRow ipRows = plcIpDt.Select($" Id = {item["PlcIP"].ToString()} ")[0];
                        string plcStatus = "", plcValue = "";
                        var statusVal = "";
                        PlcIO plc = new PlcIO(ipRows["IP"].ToString(), 1, ref plcStatus);
                        if (plcStatus.Length > 0)
                        {
                            plc.ClosePlc();
                        }
                        else
                        {
                            plcValue = plc.GetPlcDBValue(int.Parse(item["DbNumber"].ToString()), item["PosType"].ToString(), item["PlcPos"].ToString());
                        }
 
                        switch (plcValue)
                        {
                            case "0":  // 申请储位
                                statusVal = item["DbNumber"].ToString() =="7"|| item["DbNumber"].ToString() == "11" ? "有货,不可放货" : "空闲";
                                break;
                            case "10":  // 申请储位
                                statusVal = "申请储位";
                                break;
                            case "20":  // 到达出库口
                                statusVal = "出库完成";
                                break;
                            case "100":  // 堆垛机可以取货
                                statusVal = "堆垛机可以取货";
                                break;
                            case "120":  // 堆垛机可以取货
                                statusVal = "堆垛机可以放货";
                                break;
                            default:
                                statusVal = "连接PLC失败";
                                break;
                        }
 
                        item["PlcStatusVal"] = statusVal;
                        plc.ClosePlc();
                    }
 
                    var list = ModelConvertHelper<WCSPlcInfo>.DataTableToModel(dt);
                    return list.ToList();
                }
                else
                {
                    List<WCSPlcInfo> list = new List<WCSPlcInfo>();
                    return list;
                }
 
            }
            catch
            {
                throw new NotImplementedException();
            }
 
            #region MyRegion
            //try
            //{
            //    StringBuilder strSql = new StringBuilder();
            //    strSql.Append("select a.*,b.userName as CreateUserName,c.userName as UpdateUserName from WCSConveyorInfo a left join UserInfo b on a.CreateUser = b.Id left join UserInfo c on a.UpdateUser = c.Id where a.IsDel = '0' ");
 
            //    if (!string.IsNullOrWhiteSpace(Json.SkuType))
            //    {
            //        strSql.Append(" and SkuType = '" + Json.SkuType + "'  ");
            //    }
            //    if (!string.IsNullOrWhiteSpace(Json.Conveyor))
            //    {
            //        strSql.Append(" and Conveyor = '" + Json.Conveyor + "'  ");
            //    }
            //    if (!string.IsNullOrWhiteSpace(Json.LotNo))
            //    {
            //        strSql.Append(" and LotNo like '%" + Json.LotNo + "%'  ");
            //    }
            //    if (!string.IsNullOrWhiteSpace(Json.IsEndLot))
            //    {
            //        strSql.Append(" and IsEndLot = '" + Json.IsEndLot + "'  ");
            //    }
            //    if (!string.IsNullOrWhiteSpace(Json.Status))
            //    {
            //        strSql.Append(" and Status = '" + Json.Status + "'  ");
            //    }
            //    if (Json.BeginTime != null && Json.BeginTime != DateTime.MinValue && Json.BeginTime != DateTime.MaxValue)
            //    {
            //        strSql.Append($"and LoginTime >= '{Convert.ToDateTime(Json.BeginTime).ToShortDateString()}' ");
            //    }
            //    if (Json.BeginTime != null && Json.EndTime != DateTime.MinValue && Json.EndTime != DateTime.MaxValue)
            //    {
            //        strSql.Append($"and LoginTime <= '{Convert.ToDateTime(Json.EndTime).ToShortDateString() + " 23:59:59.999"}' ");
 
            //    }
 
            //    DataTable dt = DataFactory.SqlDataBase().GetPageList(strSql.ToString(), null, "CreateTime", "Desc", ref pageInfo);
            //    return ModelConvertHelper<GetWCSConveyorInfo>.DataTableToModel(dt);
            //}
            //catch
            //{
            //    throw new NotImplementedException();
            //}
            #endregion
 
        }
 
 
        public string AddConveyor(ConveyorModel model, string loginUser,string orgin)
        {
            try
            {
                //转换user信息 user Code变成id
 
                UserInfo us = null;
                StringBuilder strSql = new StringBuilder();
                strSql.Append($"select * from UserInfo where IsDel = 0 and UserCode = '{loginUser}' ");
 
                IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, null);
 
                us = ModelConvertHelper<UserInfo>.ReaderToModel(dt);
                if (us == null)
                {
                    return "未查询到当前登录人信息";
                }
                string plcStatus = "";  // PLC连接状态
                PlcIO plc = new PlcIO("192.168.21.2", 1, ref plcStatus);
                if (plcStatus.Length > 0)
                {
                    return "连接输送线失败";
                }
                DALWcsMessage wcsSql = new DALWcsMessage();
                var PlcInfoDt = wcsSql.GetPlcInfos();
                DataRow[] plcRows = PlcInfoDt.Select($"Level = '2' and StationNum='{model.StartLocatNo}' ");
                if (plcRows.Length == 0)
                {
                    return "未查询到起始工位信息";
                }
                var posDt = wcsSql.GetPlcPos(plcRows[0]["Id"].ToString());
                // 写入起始工位
                var posRos101 = posDt.Select($"Name = 'StartLocatNo' ")[0];
                plc.SetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos101["PosType"].ToString(), model.StartLocatNo, posRos101["PlcPos"].ToString());
 
                // 写入目的工位
                var posRos102 = posDt.Select($"Name = 'EndLocatNo' ")[0];
                plc.SetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos102["PosType"].ToString(), model.EndLocatNo, posRos102["PlcPos"].ToString());
 
                //写入托盘号
                var posRos103 = posDt.Select($"Name = 'PalletNo' ")[0];// 托盘号 
                plc.SetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos103["PosType"].ToString(), model.PalletNo, posRos103["PlcPos"].ToString());
 
                //写入任务号
                var posRos104 = posDt.Select($"Name = 'TaskNo' ")[0];
                plc.SetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos104["PosType"].ToString(), model.TaskNo, posRos104["PlcPos"].ToString());
 
                // 写入wcs控制字流程10
                bool inBl10 = plc.SetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), plcRows[0]["PosType"].ToString(),model.WcsPos, plcRows[0]["WcsPos"].ToString(), true);
 
 
                plc.ClosePlc();
                if (orgin == "Edit")
                {
                    DAL_OperationRecord oper = new DAL_OperationRecord();
                    var operation = new OperationModel()
                    {
                        MenuName = "输送线监控",
                        FkNo = model.TaskNo + "-" + model.PalletNo,
                        Msg = "手动编辑输送线任务信息",
                        Type = "编辑",
                    };
                    oper.AddOperation(operation, loginUser);
                }
                return "";
 
            }
            catch(Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
 
        public ConveyorModel GetConveyor(string id)
        {
            try
            {
                string plcStatus = "";  // PLC连接状态
                ConveyorModel model = new ConveyorModel();
                PlcIO plc = new PlcIO("192.168.21.2", 1, ref plcStatus);
                if (plcStatus.Length > 0)
                {
                    return model; //throw new Exception("连接输送线失败");
                }
                DALWcsMessage wcsSql = new DALWcsMessage();
                var PlcInfoDt = wcsSql.GetPlcInfos();
                DataRow[] plcRows = PlcInfoDt.Select($"Level = '2' and Id={id} ");
                if (plcRows.Length == 0)
                {
                    return model; //throw new Exception("未查询到起始工位信息");
                }
                var posDt = wcsSql.GetPlcPos(id);
 
                // 获取起始工位
                var posRos101 = posDt.Select($"Name = 'StartLocatNo' ")[0];
                model.StartLocatNo = plc.GetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos101["PosType"].ToString(), posRos101["PlcPos"].ToString());
 
                // 获取目的工位
                var posRos102 = posDt.Select($"Name = 'EndLocatNo' ")[0];
                model.EndLocatNo = plc.GetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos102["PosType"].ToString(), posRos102["PlcPos"].ToString());
 
                // 获取托盘号
                var posRos103 = posDt.Select($"Name = 'PalletNo' ")[0];// 托盘号 
                model.PalletNo = plc.GetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos103["PosType"].ToString(), posRos103["PlcPos"].ToString());
 
                // 获取任务号
                var posRos104 = posDt.Select($"Name = 'TaskNo' ")[0];
                model.TaskNo = plc.GetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), posRos104["PosType"].ToString(), posRos104["PlcPos"].ToString());
 
                // 获取wcs控制字流程
                model.WcsPos = plc.GetPlcDBValue(int.Parse(plcRows[0]["DbNumber"].ToString()), plcRows[0]["PosType"].ToString(), plcRows[0]["WcsPos"].ToString());
 
 
                plc.ClosePlc();  
 
 
                return model;
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
 
        /// <summary>
        /// 添加物料输送信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="loginUser"></param>
        /// <returns></returns>
        public bool Add(WCSConveyorInfo model, string loginUser)
        {
            bool result = false;
            try
            {
                //转换user信息 user Code变成id
                
                UserInfo us = null;
                StringBuilder strSql = new StringBuilder();
                strSql.Append($"select * from UserInfo where IsDel = 0 and UserCode = '{loginUser}' "); 
                
                IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, null);
 
                us = ModelConvertHelper<UserInfo>.ReaderToModel(dt);
                if (us == null)
                {
                    return false;
                }
                Hashtable ht = new Hashtable();
 
                ht["SkuType"] = "'" + model.SkuType + "'";
                ht["Conveyor"] = "'" + model.Conveyor + "'";
                ht["LotNo"] = "'" + model.LotNo + "'";
                ht["SupplierLot"] = "'" + model.SupplierLot + "'";
                ht["ManuFactureDate"] = "'" + model.ManuFactureDate + "'";
                ht["IsEndLot"] = "0";
                ht["Status"] = "0";
 
                ht["IsDel"] = "0";
                ht["CreateUser"] = "" + us.ID + ""; 
                ht["CreateTime"] = "GetDate()";
                
                var ret = DataFactory.SqlDataBase().InsertByHashtableNullParam("WCSConveyorInfo", ht);
 
                if (ret == 1) result = true;
 
                return result;
 
            }
            catch
            {
                return result;
            }
        }
 
        public bool Delete(string[] infoIds, string loginUser)
        {
            bool result = false;
            try
            {
                UserInfo us = null;
                StringBuilder strSql = new StringBuilder();
                strSql.Append($"select * from UserInfo where IsDel = 0 and UserCode = '{loginUser}' ");
 
                IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, null);
 
                us = ModelConvertHelper<UserInfo>.ReaderToModel(dt);
                if (us == null)
                {
                    return false;
                }
                var dt2 = DataFactory.SqlDataBase().IsExist("WcsConveyorInfo", "Id", infoIds);
                if (dt2 >= infoIds.Length)
                {
                    StringBuilder sql = new StringBuilder();
                    foreach (string code in infoIds)
                    {
                        sql.Append("update WcsConveyorInfo set IsDel=1,UpdateTime = getdate(),UpdateUser = '" + us.ID + "' where Id='" + code + "';");
                    }
 
                    int _ret = DataFactory.SqlDataBase().ExecuteBySql(sql);
                    if (_ret > infoIds.Length) result = true;
                }
                return result;
 
            }
            catch
            {
                return result;
            }
        }
 
        public bool Run(string[] infoIds, string loginUser)
        {
            bool result = false;
            try
            {
                UserInfo us = null;
                StringBuilder strSql = new StringBuilder();
                strSql.Append($"select * from UserInfo where IsDel = 0 and UserCode = '{loginUser}' ");
 
                IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, null);
 
                us = ModelConvertHelper<UserInfo>.ReaderToModel(dt);
                if (us == null)
                {
                    return false;
                }
                var dt2 = DataFactory.SqlDataBase().IsExist("WCSConveyorInfo", "Id", infoIds);
                if (dt2 >= infoIds.Length)
                {
                    StringBuilder sql = new StringBuilder();
                    foreach (string code in infoIds)
                    {
                        StringBuilder sql2 = new StringBuilder();
                        sql2.Append($"select * from WCSConveyorInfo where IsDel = 0 and Id = '{code}' ");
 
                        IDataReader dt3 = DataFactory.SqlDataBase().GetDataReaderBySQL(sql2, null);
 
                        var info = ModelConvertHelper<WCSConveyorInfo>.ReaderToModel(dt3);
                        
                        sql.Append("update WcsConveyorInfo set Status=1,UpdateTime = getdate(),UpdateUser = '" + us.ID + "' where Id='" + code + "';");
                        sql.Append("update WcsConveyorInfo set Status=0,UpdateTime = getdate(),UpdateUser = '" + us.ID + "' where Id !='" + code + "' and IsDel = '0' and Conveyor = '"+ info.Conveyor + "';");
                    }
 
                    int _ret = DataFactory.SqlDataBase().ExecuteBySql(sql);
                    if (_ret > infoIds.Length) result = true;
                }
                return result;
 
            }
            catch
            {
                return result;
            }
        }
 
        public bool JiePi(string[] infoIds, string loginUser)
        {
            bool result = false;
            try
            {
                UserInfo us = null;
                StringBuilder strSql = new StringBuilder();
                strSql.Append($"select * from UserInfo where IsDel = 0 and UserCode = '{loginUser}' ");
 
                IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, null);
 
                us = ModelConvertHelper<UserInfo>.ReaderToModel(dt);
                if (us == null)
                {
                    return false;
                }
                var dt2 = DataFactory.SqlDataBase().IsExist("WCSConveyorInfo", "Id", infoIds);
                if (dt2 >= infoIds.Length)
                {
                    StringBuilder sql = new StringBuilder();
                    foreach (string code in infoIds)
                    {
                        sql.Append("update WcsConveyorInfo set IsEndLot=1,Status =0,UpdateTime = getdate(),UpdateUser = '" + us.ID + "' where Id='" + code + "';");
                       
                    }
 
                    int _ret = DataFactory.SqlDataBase().ExecuteBySql(sql);
                    if (_ret > infoIds.Length) result = true;
                }
                return result;
 
            }
            catch
            {
                return result;
            }
        }
    }
}