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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using Model;
using Model.WcsModel;
 
namespace BLL.DAL
{
    public class DAL_IpPlcInfo
    {
        #region WCSIP
 
        /// <summary>
        /// 获取WCSIP信息
        /// </summary>
        /// <param name="json"></param>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        public IList<WCSIP> GetList(AjaxGetIpList json, ref PageInfo pageInfo)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                List<SqlParam> para = new List<SqlParam>();
 
                // 0 未下发 1 已下发 2 ? 3已完成
                strSql.Append("select Id,IP, Type,WareHouseNo,Text,IsDel,CreateTime,CreateUser from WCSIP where IsDel = '0'");
                
 
                if (!string.IsNullOrWhiteSpace(json.Ip))
                {
                    strSql.Append(" and IP like '%" + json.Ip + "%' ");
                }
                if (!string.IsNullOrWhiteSpace(json.Type))
                {
                    strSql.Append(" and Type = '" + json.Type + "' ");
                }
 
                SqlParam[] param = null;
                if (para != null)
                {
                    param = para.ToArray();
                }
 
                DataTable dt = DataFactory.SqlDataBase().GetPageList(strSql.ToString(), param, "Id", "asc", ref pageInfo);
                return ModelConvertHelper<WCSIP>.DataTableToModel(dt);
            }
            catch(Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 获取WCSIP单条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public WCSIP GetOneIp(string id)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
 
                strSql.Append($"select Id,IP, Type,WareHouseNo,Text,IsDel,CreateTime,CreateUser from WCSIP where Id = {id} ");
                
                var dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql);
                return ModelConvertHelper<WCSIP>.ReaderToModel(dt);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 获取设备类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public string GetDicTypeHtml(string type = "")
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                string resMenuTemplate = "<option value='{0}' {1}>{2}</option>";
                sb.AppendFormat(resMenuTemplate, "", "", "选择");
                
                var list = new List<DicOption>();
                list.Add(new DicOption()
                {
                    Val="0",
                    Text = "跺机"
                });
                list.Add(new DicOption()
                {
                    Val = "1",
                    Text = "托盘输送线"
                });
                list.Add(new DicOption()
                {
                    Val = "2",
                    Text = "件箱输送线"
                });
 
                foreach (var row in list)
                {
                    sb.AppendFormat(resMenuTemplate, row.Val, row.Val == type ? "selected='selected'" :
                        string.Empty, row.Text);
                }
 
                return sb.ToString();
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 添加IP信息
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public bool IpAdd(AjasWCSIP models)
        {
            bool result = false;
            try
            {
                StringBuilder sqlString = new StringBuilder();
                // 验证任务是否已存在
                //sqlString.Append($"select count(Id) from WCSIP where PalletNo = '{models.PalletNo}';");
                //DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(sqlString);
                //if (dt.Rows[0][0].ToString() != "0")
                //{
                //    throw new Exception("托盘号:" + models.PalletNo + ";已存在!");
                //}
 
                // 新增任务
                sqlString.Clear();
                var str = $"insert into WCSIP (IP ,Type ,WareHouseNo ,Text ,IsDel ,CreateTime ,CreateUser) VALUES('{models.IP}','{models.Type}','{models.WareHouseNo}','{models.Text}','0',GETDATE(),'{models.CreateUser}' ); ";
 
                sqlString.Append(str);
                 
                var num = DataFactory.SqlDataBase().ExecuteBySql(sqlString);
 
                if (num == 1) result = true;
 
                return result;
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 修改IP信息
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public bool IpUpdate(AjasWCSIP models)
        {
            bool result = false;
            try
            {
                StringBuilder sqlString = new StringBuilder();
                // 验证任务是否已存在
                //sqlString.Append($"select count(Id) from WCSIP where PalletNo = '{models.PalletNo}';");
                //DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(sqlString);
                //if (dt.Rows[0][0].ToString() != "0")
                //{
                //    throw new Exception("托盘号:" + models.PalletNo + ";已存在!");
                //}
 
                // 新增任务
                sqlString.Clear();
                var str = $"update WCSIP set IP = '{models.IP}',Type = '{models.Type}',WareHouseNo = '{models.WareHouseNo}', Text = '{models.Text}' where Id = {models.Id} ; ";
 
                sqlString.Append(str);
 
                var num = DataFactory.SqlDataBase().ExecuteBySql(sqlString);
 
                if (num == 1) result = true;
 
                return result;
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 删除IP信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool IpDel(string[] Id)
        {
            bool result = false;
            try
            {
                int dt = DataFactory.SqlDataBase().IsExist("WCSIP", "Id", Id);
                if (dt >= Id.Length)
                {
                    int i = 0;
                    while (i < Id.Length)
                    {
 
                        StringBuilder sql = new StringBuilder();
                        // 验证任务是否已存在
                        sql.Append($"select count(Id) from WCSPlcInfo where PlcIP = {Id[i]};");
                        DataTable dt2 = DataFactory.SqlDataBase().GetDataTableBySQL(sql);
                        if (dt2.Rows[0][0].ToString() != "0")
                        {
                            throw new Exception($"当前IP下存有工位设备信息,请先移除!");
                        }
                        // 获取当前IP信息
                        sql.Clear();
                        sql.Append($"select * from WCSIP where Id = '{Id[i]}';");
                        DataTable monitorDt = DataFactory.SqlDataBase().GetDataTableBySQL(sql);
                        if (monitorDt != null && monitorDt.Rows.Count > 0)
                        {
                            if (monitorDt.Rows[0]["IsDel"].ToString() == "1")
                            {
                                i++;
                                continue;
                            } 
                            sql.Clear();
                            // 修改任务状态
                            sql.Append($"update WCSIP set IsDel = '1' where Id = {Id[i]} ;");
                            int _ret = DataFactory.SqlDataBase().ExecuteBySql(sql);
                            if (_ret >= 1) result = true;
                        }
                        i++;
                    }
 
                }
                return result;
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        #endregion
 
 
        #region WCSPlcInfo 
 
        /// <summary>
        /// 获取WCSPlcInfo信息
        /// </summary>
        /// <param name="json"></param>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        public IList<WCSPlcInfo> GetPlcInfoList(AjaxGetIpList json, ref PageInfo pageInfo)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                List<SqlParam> para = new List<SqlParam>();
 
                // 0 未下发 1 已下发 2 ? 3已完成
                strSql.Append("select Id ,PlcIP ,LedIP ,DbNumber ,Level ,StationNum ,PlcPos ,WcsPos ,PosType ,Text ,IsDel ,CreateTime ,CreateUser from WCSPlcInfo where IsDel = '0' ");
 
                if (!string.IsNullOrWhiteSpace(json.Ip))
                {
                    strSql.Append(" and PlcIP = '" + json.Ip + "' ");
                }
 
                SqlParam[] param = null;
                if (para != null)
                {
                    param = para.ToArray();
                }
 
                DataTable dt = DataFactory.SqlDataBase().GetPageList(strSql.ToString(), param, "Id", "asc", ref pageInfo);
                return ModelConvertHelper<WCSPlcInfo>.DataTableToModel(dt);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 获取 WCSPlcInfo 单条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public WCSPlcInfo GetOnePlc(string id)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
 
                strSql.Append($"select * from WCSPlcInfo where Id = {id} ");
 
                var dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql);
                return ModelConvertHelper<WCSPlcInfo>.ReaderToModel(dt);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 获取设备级别
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public string GetDicLevelHtml(string type = "")
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                string resMenuTemplate = "<option value='{0}' {1}>{2}</option>";
                sb.AppendFormat(resMenuTemplate, "", "", "选择");
 
                var list = new List<DicOption>();
                list.Add(new DicOption()
                {
                    Val = "1",
                    Text = "DB区域级别"
                });
                list.Add(new DicOption()
                {
                    Val = "2",
                    Text = "工位级别"
                });
 
                foreach (var row in list)
                {
                    sb.AppendFormat(resMenuTemplate, row.Val, row.Val == type ? "selected='selected'" :
                        string.Empty, row.Text);
                }
 
                return sb.ToString();
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 获取设备级别
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetDicPlcIpHtml(string code = "")
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                string resMenuTemplate = "<option value='{0}' {1}>{2}</option>";
                sb.AppendFormat(resMenuTemplate, "", "", "选择");
 
                StringBuilder sql = new StringBuilder();
                sql.Append("select * from WCSIP where IsDel = 0;");
 
                DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(sql);
                 
                foreach (DataRow row in dt.Rows)
                {
                    sb.AppendFormat(resMenuTemplate, row["Id"].ToString(), row["Id"].ToString() == code ? "selected='selected'" :
                        string.Empty, row["Text"].ToString());
                }
 
                return sb.ToString();
            }
            catch
            {
                return "";
            }
        }
 
        /// <summary>
        /// 添加PlcInfo信息
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public bool PlcAdd(AjasWCSPlcInfo models)
        {
            bool result = false;
            try
            {
                StringBuilder sqlString = new StringBuilder();
                // 验证任务是否已存在
                //sqlString.Append($"select count(Id) from WCSIP where PalletNo = '{models.PalletNo}';");
                //DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(sqlString);
                //if (dt.Rows[0][0].ToString() != "0")
                //{
                //    throw new Exception("托盘号:" + models.PalletNo + ";已存在!");
                //}
 
                // 新增任务
                sqlString.Clear();
                var str = $"insert into WCSPlcInfo (PlcIP ,LedIP ,DbNumber ,Level,StationNum ,PlcPos ,WcsPos ,PosType ,Text ,IsDel ,CreateTime,CreateUser) VALUES('{models.PlcIP}','{models.LedIP}','{models.DbNumber}','{models.Level}','{models.StationNum}','{models.PlcPos}','{models.WcsPos}','{models.PosType}','{models.Text}','0',GETDATE(),'{models.CreateUser}' ); ";
 
                sqlString.Append(str);
 
                var num = DataFactory.SqlDataBase().ExecuteBySql(sqlString);
 
                if (num == 1) result = true;
 
                return result;
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 修改IP信息
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public bool PlcUpdate(AjasWCSPlcInfo models)
        {
            bool result = false;
            try
            {
                StringBuilder sqlString = new StringBuilder();
                // 验证任务是否已存在
                //sqlString.Append($"select count(Id) from WCSIP where PalletNo = '{models.PalletNo}';");
                //DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(sqlString);
                //if (dt.Rows[0][0].ToString() != "0")
                //{
                //    throw new Exception("托盘号:" + models.PalletNo + ";已存在!");
                //}
 
                // 新增任务
                sqlString.Clear();
                var str = $"update WCSPlcInfo set PlcIP = '{models.PlcIP}',LedIP = '{models.LedIP}',DbNumber ='{models.DbNumber}',Level = '{models.Level}',StationNum = '{models.StationNum}',PlcPos = '{models.PlcPos}',WcsPos = '{models.WcsPos}',PosType = '{models.PosType}',Text = '{models.Text}' where Id = {models.Id} ; ";
 
                sqlString.Append(str);
 
                var num = DataFactory.SqlDataBase().ExecuteBySql(sqlString);
 
                if (num == 1) result = true;
 
                return result;
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 删除IP信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool PlcDel(string[] Id)
        {
            bool result = false;
            try
            {
                int dt = DataFactory.SqlDataBase().IsExist("WCSPlcInfo", "Id", Id);
                if (dt >= Id.Length)
                {
                    int i = 0;
                    while (i < Id.Length)
                    {
 
                        StringBuilder sql = new StringBuilder();
                        // 验证任务是否已存在
                        sql.Append($"select count(Id) from WCSPlcPos where PlcInfoId = {Id[i]};");
                        DataTable dt2 = DataFactory.SqlDataBase().GetDataTableBySQL(sql);
                        if (dt2.Rows[0][0].ToString() != "0")
                        {
                            throw new Exception($"当前IP下存有流程字信息,请先移除!");
                        }
                        // 获取当前IP信息
                        sql.Clear();
                        sql.Append($"select * from WCSPlcInfo where Id = '{Id[i]}';");
                        DataTable monitorDt = DataFactory.SqlDataBase().GetDataTableBySQL(sql);
                        if (monitorDt != null && monitorDt.Rows.Count > 0)
                        {
                            if (monitorDt.Rows[0]["IsDel"].ToString() == "1")
                            {
                                i++;
                                continue;
                            }
                            sql.Clear();
                            // 修改任务状态
                            sql.Append($"update WCSPlcInfo set IsDel = '1' where Id = {Id[i]} ;");
                            int _ret = DataFactory.SqlDataBase().ExecuteBySql(sql);
                            if (_ret >= 1) result = true;
                        }
                        i++;
                    }
 
                }
                return result;
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        
 
        #endregion
 
 
    }
 
    public class DicOption
    {
        public string Val { get; set; }
 
        public string Text { get; set; }
    }
 
}