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
530
531
532
using Commom.Utility;
using Common;
using Model;
using Org.BouncyCastle.Bcpg.OpenPgp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
 
namespace BLL.DAL
{
    public class DALCheckTask
    {
        /// <summary>
        /// 加载盘库任务明细
        /// </summary>
        /// <param name="dictionarys">查询条件</param>
        /// <param name="page">分页信息</param>
        /// <returns></returns>
        public IList<CheckTask> GetList(CheckTask checkTask, ref PageInfo page)
        {
            try
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("select * from View_CheckTask_Userinfo where IsDel = 0 " + $" AND DepartGuid='{checkTask.DepartGuid}' ");
                if (!string.IsNullOrEmpty(checkTask.CDNo))
                {
                    stringBuilder.Append("and CDNO like '%" + checkTask.CDNo + "%' ");
                }
                if (!string.IsNullOrEmpty(checkTask.LocationCode))
                {
                    stringBuilder.Append("and (LocationCode1 like '%" + checkTask.LocationCode + "%'" +
                        "or  LocationCode2 like '%" + checkTask.LocationCode + "%') ");
                }
                if (!string.IsNullOrEmpty(checkTask.Palno))
                {
                    stringBuilder.Append("and (Palno1 like '%" + checkTask.Palno + "%' or " +
                        "Palno1 like '%" + checkTask.Palno + "%') ");
                }
                if (!string.IsNullOrEmpty(checkTask.Statu))
                {
                    stringBuilder.Append("and StatuId = " + checkTask.Statu.AddQuotes());
                }
                SqlParam[] para = null;
 
                DataTable dt = DataFactory.SqlDataBase().GetPageList(stringBuilder.ToString(), para, "ID", "ASC", ref page);
                IList<CheckTask> list = ModelConvertHelper<CheckTask>.DataTableToModel(dt);
 
                return list;
            }
            catch
            {
                throw new NotImplementedException();
            }
        }
 
        /// <summary>
        /// 获取托盘内物料明细信息
        /// </summary>
        /// <param name="TaskID"></param>
        /// <returns></returns>
        public DataTable GetCheckTask(string TaskID)
        {
            try
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("select * from CheckTask where Guid = " + TaskID.AddQuotes());
                DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
 
                stringBuilder.Clear();
                stringBuilder.Append("select '" + TaskID + "' as TaskID,unit as UnitFrist, * from CheckDataDetail where IsDel = 0 ");
                stringBuilder.Append(" and CDNO = " + dt.Rows[0]["CDNO"].ToString().AddQuotes());
                stringBuilder.Append(" and LocationCode in (" + dt.Rows[0]["LocationCode1"].ToString().AddQuotes() + ",");
                stringBuilder.Append(dt.Rows[0]["LocationCode2"].ToString().AddQuotes() + ") ");
                stringBuilder.Append("and Palno in (" + dt.Rows[0]["Palno1"].ToString().AddQuotes() + ",");
                stringBuilder.Append(dt.Rows[0]["Palno2"].ToString().AddQuotes() + ") ");
                DataTable dtItems = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
 
                return dtItems;
            }
            catch
            {
                return null;
            }
        }
 
        /// <summary>
        /// 实时保存物料实际数量
        /// </summary>
        /// <param name="LoginUserCode"></param>
        /// <param name="Mats"></param>
        /// <returns></returns>
        public bool SetItems(string LoginUserCode, CheckMat Mats)
        {
            bool result = false;
            try
            {
                StringBuilder sqlstr = new StringBuilder();
 
                int rowCount = 0;
                Hashtable ht = new Hashtable();
                ht["AQuant"] = Mats.AQuant.AddQuotes();
                ht["CheckUser"] = LoginUserCode.AddQuotes();
                ht["UpdateTime"] = "convert(varchar(20),getdate(),120)";
 
                rowCount += DataFactory.SqlDataBase().UpdateByHashtable("CheckDataDetail",
                    "Guid", Mats.Guid.AddQuotes(), ht);
 
                if (rowCount > 0)
                {
                    result = true;
                }
 
                return result;
            }
            catch
            {
                return result;
            }
        }
 
        /// <summary>
        /// 自动生成盘库指令
        /// </summary>
        /// <param name="LoginUserCode"></param>
        /// <returns></returns>
        public bool AutoOrders(string LoginUserCode)
        {
            bool result = false;
            try
            {
                StringBuilder stringBuilder = new StringBuilder();
                // 查询WH_CMD表 确认是否存在盘库单待执行的指令
                stringBuilder.Append("select count(CMDID) from WH_CMD where NumberNo like 'CTNO%' and CMDStatu = 0 and IsDel = 0;");
                DataRow row = DataFactory.SqlDataBase().GetDataRowBySQL(stringBuilder);
                // 不存在
                if (row[0].ToString() == "0")
                {
                    stringBuilder.Clear();
                    stringBuilder.Append("select top 1 * from CheckTask where statu = '01' and IsDel = 0 order by ID asc");
                    DataRow taskRow = DataFactory.SqlDataBase().GetDataRowBySQL(stringBuilder);
                    if (taskRow != null && taskRow["guid"].ToString() != "")
                    {
                        result = this.CreateOrder(taskRow["guid"].ToString(), LoginUserCode);
                    }
                }
                else
                {
                    result = false;
                }
            }
            catch
            {
                return result;
            }
            return result;
        }
 
        /// <summary>
        /// 生成盘库指令
        /// </summary>
        /// <param name="TaskId"></param>
        /// <param name="LoginUserCode"></param>
        /// <returns></returns>
        public bool CreateOrder(string TaskId, string LoginUserCode)
        {
            bool result = false;
            try
            {
                // 获取任务明细
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append($@"
SELECT ct.*,
       cd.AccessCode,
       d.TypeName AccessName
FROM dbo.CheckTask ct
    JOIN dbo.CheckData cd
        ON cd.CDNO = ct.CDNO
    LEFT OUTER JOIN dbo.Dictionary d
    ON cd.AccessCode=d.Code AND d.TopCode='Access'
WHERE ct.statu = '01'
      AND ct.guid = '{TaskId}';");
                DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
 
                if (dt != null && dt.Rows.Count > 0)
                {
                    DALWMSApi dalWMSApi = new DALWMSApi();
 
                    foreach (DataRow drTask in dt.Rows)
                    {
                        string locationCode = drTask["LocationCode1"].ToString();
                        string palNo = drTask["Palno1"].ToString();
                        stringBuilder.Clear();
                        stringBuilder.Append($"SELECT TurnoverDemand FROM dbo.DepotsLocation WHERE LocationCode='{locationCode}' AND IsDel=0");
                        DataTable dtDL = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
                        if (dtDL != null && dtDL.Rows.Count >= 0)
                        {
                            string turnoverDemand = dtDL.Rows[0]["TurnoverDemand"].ToString();
                            switch (turnoverDemand)
                            {
                                case "02":
                                    {
                                        dalWMSApi.Send(palNo, locationCode, drTask["AccessCode"].ToString());
                                        stringBuilder.Clear();
                                        stringBuilder.Append($"UPDATE dbo.DepotsLocation SET TurnoverDemand='04' WHERE LocationCode='{locationCode}'");
                                        DataFactory.SqlDataBase().ExecuteBySql(stringBuilder);
                                        break;
                                    }
                                case "03":
                                    {
                                        var resultApi = dalWMSApi.OutStorage(palNo, locationCode);
                                        if (resultApi.Code != "01")
                                        {
                                            return false;
                                        }
                                        break;
                                    }
                                default:
                                    break;
                            }
                        }
                    }
 
                    // 更改盘点任务数据状态
                    Hashtable htt = new Hashtable();
                    htt["statu"] = "'02'";
                    htt["CheckUser"] = LoginUserCode.AddQuotes();
                    htt["CheckTime"] = "convert(varchar(20),getdate(),120)";
 
                    DataFactory.SqlDataBase().UpdateByHashtable("CheckTask",
                        "Guid", TaskId.AddQuotes(), htt);
 
                    return true;
                }
                #region 原方法,注释
                /*
                if (dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                    Hashtable ht = new Hashtable();
                    ht["TaskID"] = TaskId.AddQuotes();
                    ht["CMDStatu"] = 0;
                    ht["CreateUser"] = LoginUserCode.AddQuotes();
                    ht["NumberNo"] = row["CDNO"].ToString().AddQuotes();
 
                    if (row["LocationCode2"].ToString() != "")               // 外货位是否有盘库任务
                    {
                        // 生成外货位指令
                        ht["Palno"] = row["Palno2"].ToString().AddQuotes();
                        ht["CMDType"] = "'out'";
                        ht["OldAddre"] = row["LocationCode2"].ToString().AddQuotes();
                        DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
                        // 收到WCS出库成功回执后 将货位状态设置为待入库状态 占住此位置
 
                        if (row["LocationCode1"].ToString() != "")          // 判断内货位是否有盘库任务
                        {
                            // 生成内货位出库指令
                            ht["Palno"] = row["Palno1"].ToString().AddQuotes();
                            ht["CMDType"] = "'out'";
                            ht["OldAddre"] = row["LocationCode1"].ToString().AddQuotes();
                            DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
 
                            // 生成内货位回库记录 -- 不主动下发wcs用来接收到申请货位请求时返回货位地址 
                            ht["CMDType"] = "'to'";
                            DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
                        }
 
                        // 生成外货位回库记录 -- 不主动下发wcs用来接收到申请货位请求时返回货位地址 
                        ht["Palno"] = row["Palno2"].ToString().AddQuotes();
                        ht["CMDType"] = "'to'";
                        ht["OldAddre"] = row["LocationCode2"].ToString().AddQuotes();
                        DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
 
                        // 更改盘点任务数据状态
                        Hashtable htt = new Hashtable();
                        htt["statu"] = "'02'";
                        htt["CheckUser"] = LoginUserCode.AddQuotes();
                        htt["CheckTime"] = "convert(varchar(20),getdate(),120)";
 
                        DataFactory.SqlDataBase().UpdateByHashtable("CheckTask",
                            "Guid", TaskId.AddQuotes(), htt);
 
                        return true;
                    }
                    else if (row["LocationCode1"].ToString() != "")   // 内货位是否有出库任务
                    {
                        // 获取内货位对应的外货位是否存在托盘
                        DataRow dttRow = this.GetlLocationItem(row["LocationCode1"].ToString());
 
                        string newLocation = string.Empty;
                        if (dttRow != null)
                        {
                            // 存在托盘 先生成外货位移库命令
                            newLocation = this.GetNewLocation(dttRow["Addre"].ToString());
                            ht["Palno"] = dttRow["Palno"].ToString().AddQuotes();
                            ht["OldAddre"] = dttRow["Addre"].ToString().AddQuotes();
                            ht["NowAddre"] = newLocation.AddQuotes();
                            // 如果没有空余货位,直接出库
                            if (newLocation == "")
                            {
                                ht["CMDType"] = "'out'";
                            }
                            else
                            {
                                ht["CMDType"] = "'move'";
                            }
 
                            DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
                        }
 
                        // 生成内货位出库命令 
                        ht["Palno"] = row["Palno1"].ToString().AddQuotes();
                        ht["CMDType"] = "'out'";
                        ht["OldAddre"] = row["LocationCode1"].ToString().AddQuotes();
                        ht["NowAddre"] = "''";
                        DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
 
                        // 生成内货位回库命令  -- 不主动下发wcs,用来接收到申请货位请求时返回货位地址 
                        ht["CMDType"] = "'to'";
                        DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
 
                        if (dttRow != null)
                        {
                            // 生成移库货位回移指令 ,移库不需要回移暂时不删除以防有变。
                            ht["Palno"] = dttRow["Palno"].ToString().AddQuotes();
                            ht["CMDType"] = "'move'";
                            ht["NowAddre"] = dttRow["Addre"].ToString().AddQuotes();
                            ht["OldAddre"] = newLocation.AddQuotes();
 
                            // 移库回库
                            if (newLocation == "")
                            {
                                ht["CMDType"] = "'to'";
                                ht["NowAddre"] = newLocation.AddQuotes(); ;
                                ht["OldAddre"] = dttRow["Addre"].ToString().AddQuotes();
                            }
                            else
                            {
                                ht["CMDType"] = "'move'";
                                ht["NowAddre"] = dttRow["Addre"].ToString().AddQuotes();
                                ht["OldAddre"] = newLocation.AddQuotes();
                            }
 
                            DataFactory.SqlDataBase().InsertByHashtableNullParam("WH_CMD", ht);
                        }
 
 
                        // 更改盘库任务状态
                        Hashtable htt = new Hashtable();
                        htt["statu"] = "'02'";
                        htt["CheckUser"] = LoginUserCode.AddQuotes();
                        htt["CheckTime"] = "convert(varchar(20),getdate(),120)";
 
                        DataFactory.SqlDataBase().UpdateByHashtable("CheckTask",
                            "Guid", TaskId.AddQuotes(), htt);
 
                        return true;
                    }
                }
                */
                #endregion
                return result;
            }
            catch
            {
                return result;
            }
        }
 
        /// <summary>
        /// 获取指定货位地址的外货位地址
        /// </summary>
        /// <returns></returns>
        private DataRow GetlLocationItem(string Locationcode1)
        {
            string Locationcode2 = string.Empty;
            if (Locationcode1.Contains("-001"))
            {
                Locationcode2 = Locationcode1.Replace("-001", "-002");
            }
            else
            {
                Locationcode2 = Locationcode1.Replace("-004", "-003");
            }
 
            StringBuilder stringBuilder = new StringBuilder();      //  一个货位地址只会对应一个托盘码
            stringBuilder.Append("select top 1 * from log_Store where LocationCode = " + Locationcode2.AddQuotes());
            DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0];
            }
 
            return null;
        }
 
        /// <summary>
        /// 获取移库空货位地址
        /// </summary>
        /// <param name="Location">当前货位地址</param>
        /// <returns>空货位地址</returns>
        private string GetNewLocation(string Location)
        {
            string newLocation = "";                    // 移库目标货位      
            int lRow = int.Parse(Location.Substring(3, 3));
            int lColumn = int.Parse(Location.Substring(6, 3));
            int lLayer = int.Parse(Location.Substring(9, 3));
 
            // 获取外货架的所有空货位
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("select LocationCode,LRow,LColumn,LLayer from DepotsLocation ");
            stringBuilder.Append(" where IsDel = 0 and TurnoverDemand = '01'");
            stringBuilder.Append(" and Type >= (select Type from DepotsLocation where LocationCode = " + Location.AddQuotes() + ") ");
            stringBuilder.Append(" and (LocationCode like '%-002%' or LocationCode like '%-003%') ");
            stringBuilder.Append(" order by LocationCode; ");
            DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
 
            if (dt != null)
            {
                int value = 600;
                foreach (DataRow row in dt.Rows)
                {
                    int dtrow = int.Parse(row["LRow"].ToString());
                    int dtColumn = int.Parse(row["LColumn"].ToString());
                    int dtLayer = int.Parse(row["LLayer"].ToString());
                    int number = 0;
 
                    int a = lRow - dtrow;
                    if (a < 0)
                    {
                        number += (0 - a);
                    }
                    else
                    {
                        number += a;
                    }
 
                    int b = lColumn - dtColumn;
                    if (b < 0)
                    {
                        number += (0 - b);
                    }
                    else
                    {
                        number += b;
                    }
 
                    int c = lLayer - dtLayer;
                    if (c < 0)
                    {
                        number += (0 - c);
                    }
                    else
                    {
                        number += c;
                    }
 
                    if (value > number)
                    {
                        value = number;
                        newLocation = row["LocationCode"].ToString();
                    }
                }
 
                return newLocation;
            }
            else
            {
                stringBuilder.Clear();
                stringBuilder.Append("select LocationCode,LRow,LColumn,LLayer from DepotsLocation ");
                stringBuilder.Append(" where IsDel = 0 and TurnoverDemand = '01'");
                stringBuilder.Append(" and Type >= (select Type from DepotsLocation where LocationCode = " + Location.AddQuotes() + ") ");
                stringBuilder.Append(" and (LocationCode like '%-001%' or LocationCode like '%-004%') ");
                stringBuilder.Append(" order by LocationCode); ");
                dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
 
                if (dt != null)
                {
                    int value = 600;
                    foreach (DataRow row in dt.Rows)
                    {
                        int dtrow = int.Parse(row["LRow"].ToString());
                        int dtColumn = int.Parse(row["LColumn"].ToString());
                        int dtLayer = int.Parse(row["LLayer"].ToString());
                        int number = 0;
 
                        int a = lRow - dtrow;
                        if (a < 0)
                        {
                            number += (0 - a);
                        }
                        else
                        {
                            number += a;
                        }
 
                        int b = lColumn - dtColumn;
                        if (b < 0)
                        {
                            number += (0 - b);
                        }
                        else
                        {
                            number += b;
                        }
 
                        int c = lLayer - dtLayer;
                        if (c < 0)
                        {
                            number += (0 - c);
                        }
                        else
                        {
                            number += c;
                        }
 
                        if (value > number)
                        {
                            value = number;
                            newLocation = row["LocationCode"].ToString();
                        }
                    }
 
                    return newLocation;
                }
            }
 
            return newLocation;
        }
    }
}