bklLiudl
2024-07-23 277bbae216debe7e6c04e8cc6ee6e1ba9763e14b
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BLL;
using Common;
using Model;
 
namespace BLL
{
    public class DALPalletBind : IDALPalletBind
    {
        public IList<PalBind> GetList(PalBindList ajaxPbl, ref PageInfo page)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                List<SqlParam> para = new List<SqlParam>();
                strSql.Append("Select Palno,OrdNo,Statu,StatuName," +
                    "LocationCode from View_IPalletBind where DepartGuid = @DepartGuid ");
 
                para.Add(new SqlParam("@DepartGuid", ajaxPbl.DepartGuid));
                if (!string.IsNullOrEmpty(ajaxPbl.OrdNo))
                {
                    strSql.Append(" and OrdNo like '%' + @OrdNo + '%' ");
                    para.Add(new SqlParam("@OrdNo", ajaxPbl.OrdNo));
                }
                if (!string.IsNullOrEmpty(ajaxPbl.Palno))
                {
                    strSql.Append(" and Palno like '%' + @Palno + '%' ");
                    para.Add(new SqlParam("@Palno", ajaxPbl.Palno));
                }
                if (!string.IsNullOrEmpty(ajaxPbl.Statu))
                {
                    strSql.Append(" and Statu = @Statu ");
                    para.Add(new SqlParam("@Statu", ajaxPbl.Statu));
                }
                if (ajaxPbl.BeginTime != DateTime.MinValue && ajaxPbl.BeginTime != null && ajaxPbl.BeginTime != DateTime.MaxValue)
                {
                    strSql.Append(" and CreateTime >= @CreateTime1 ");
                    para.Add(new SqlParam("@CreateTime1", Convert.ToDateTime(ajaxPbl.BeginTime).ToShortDateString()));
                }
                if (ajaxPbl.EndTime != DateTime.MinValue && ajaxPbl.EndTime != null && ajaxPbl.EndTime != DateTime.MaxValue)
                {
                    strSql.Append("and CreateTime <= @CreateTime2 ");
                    para.Add(new SqlParam("@CreateTime2", Convert.ToDateTime(ajaxPbl.EndTime).ToShortDateString() + " 23:59:59.999"));
                }
                strSql.Append(" Group By Palno,OrdNo,Statu,StatuName,LocationCode ");
 
                SqlParam[] param = null;
                if (para != null)
                {
                    param = para.ToArray();
                }
 
                DataTable dt = DataFactory.SqlDataBase().GetPageList(strSql.ToString(), param, "OrdNo", "desc", ref page);
                return ModelConvertHelper<PalBind>.DataTableToModel(dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public bool GetPalletBind(PalBind palBind, ref IList<PalBind> lst)
        {
            try
            {
                IList<PalletBind> ls = new List<PalletBind>();
                StringBuilder strSql = new StringBuilder();
                strSql.Append("Select * from View_IPalletBind ");
                strSql.Append("where Palno = @Palno and OrdNo = @OrdNo ");
                strSql.Append("and DepartGuid = @DepartGuid ");
 
                SqlParam[] para = new SqlParam[]
                {
                    new SqlParam("@Palno", palBind.Palno),
                    new SqlParam("@OrdNo", palBind.OrdNo),
                    new SqlParam("@DepartGuid", palBind.DepartGuid)
                };
 
                IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, para);
                lst = ModelConvertHelper<PalBind>.DataReaderToModel(dt);
 
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public PalBind GetInNoDetail(PalBind models, List<PalBind> lists)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select OrdNo,MatNo,LingNo,cast(CurQuant as int) as CurQuant,sum(cast(MatCount as int)) as MatCount,MatGuid ");
                strSql.Append("from View_GetMatNum where matno = '" + models.MatNo + $"' AND DepartGuid='{models.DepartGuid}' ");
                if (!string.IsNullOrEmpty(models.OrdNo))
                {
                    strSql.Append(" and OrdNo = '" + models.OrdNo + "' ");
                }
                strSql.Append("group by OrdNo,MatNo,CurQuant,LingNo,MatGuid order by OrdNo;");
                DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(strSql);
 
                PalBind pallet = new PalBind();
                foreach (DataRow row in dt.Rows)
                {
                    if (row["CurQuant"].ToString() == row["MatCount"].ToString())
                    {
                        continue;
                    }
 
                    string ordNo = row["OrdNo"].ToString();
                    pallet.OrdNo = ordNo;
                    pallet.CurQuant = row["CurQuant"].ToString();
                    pallet.MatNo = models.MatNo;
                    pallet.LingNo = row["LingNo"].ToString();
                    int matNoNb = int.Parse(row["CurQuant"].ToString()) - int.Parse(row["MatCount"].ToString());
                    pallet.MatCount = matNoNb.ToString();
                    pallet.MatGuid = row["MatGuid"].ToString();
 
                    // liudl 此处逻辑待确认
                    if (lists != null)
                    {
                        foreach (PalBind palBind in lists)
                        {
                            if (palBind.OrdNo == ordNo && palBind.MatNo == models.MatNo)
                            {
                                matNoNb = matNoNb + int.Parse(palBind.MatCount);
                                //matNoNb = matNoNb - int.Parse(palBind.MatCount);
                                if (matNoNb > 0)
                                {
                                    pallet.MatCount = matNoNb.ToString();
                                    break;
                                }
                                else
                                {
                                    pallet.MatGuid = "";
                                    pallet.LingNo = "";
                                    pallet.CurQuant = "";
                                    pallet.OrdNo = "";
                                    pallet.MatCount = "";
                                    matNoNb = 0;
                                    break;
                                }
                            }
                        }
                    }
 
                    if (matNoNb > 0)
                    {
                        break;
                    }
                }
 
                return pallet;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public bool GetErpinByMatNo(PalBind models, ref IList<PalBind> lstPd)
        {
            bool ret = false;
            //try
            //{
            //    IList<PalletBind> ls = new List<PalletBind>();
            //    StringBuilder strSql = new StringBuilder();
            //    if (string.IsNullOrEmpty(models.OrdNo))
            //    {
            //        strSql.Append("Select OrdNo, LingNo, TuNo, PageNo, CurQuant As MatCount from View_ErpInMatDetail where ");
            //        strSql.Append("MatNo = '" + models.MatNo + "' and Statu != 'ER03'");
            //        DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(strSql);
            //        if (dt.Rows.Count > 0)
            //        {
 
            //            for (int i = 0; i < dt.Rows.Count; i++)
            //            {
            //                //循环根据物料单号查询数量是否已经组盘完成,完成则查询此物料的下一个单号
            //                StringBuilder strhql = new StringBuilder();
            //                strhql.Append("select sum(cast(MatCount as int)) as MatCount ,OrdNo from IPalletBind  where IsDel=0 and OrdNo='" + dt.Rows[i]["OrdNo"].ToString() + "' and MatNo ='" + models.MatNo + "' group by OrdNo");
            //                DataTable dd = DataFactory.SqlDataBase().GetDataTableBySQL(strhql);
            //                if (dd.Rows.Count > 0)
            //                {
            //                    if (dt.Rows[i]["MatCount"].ToInt() > dd.Rows[0]["MatCount"].ToInt())
            //                    {
            //                        lstPd = ModelConvertHelper<PalletBind>.DataTableToModel(dt);
            //                        break;
            //                    }
            //                    else
            //                    {
 
            //                        dt.Rows.RemoveAt(i);
            //                        i--;
 
            //                    }
            //                }
            //                else
            //                {
            //                    lstPd = ModelConvertHelper<PalletBind>.DataTableToModel(dt);
            //                    break;
            //                }
            //            }
            //            dt.AcceptChanges();
            //        }
 
            //        lstPd = ModelConvertHelper<PalletBind>.DataTableToModel(dt);
            //    }
            //    else
            //    {
            //        strSql.Append("Select OrdNo, LingNo, TuNo, PageNo, CurQuant As MatCount from View_ErpInMatDetail where ");
            //        strSql.Append("MatNo = @MatNo and Statu != @Statu and OrdNo = @OrdNo");
            //        SqlParam[] para = new SqlParam[]
            //        {
            //            new SqlParam("@MatNo", models.MatNo),
            //            new SqlParam("@Statu", "ER03"),
            //            new SqlParam("@OrdNo", models.OrdNo),
            //        };
            //        IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, para);
            //        lstPd = ModelConvertHelper<PalletBind>.DataReaderToModel(dt);
            //    }
 
            //    ret = true;
            //}
            //catch
            //{
            //    ret = false;
            //    throw new NotImplementedException();
            //}
            return true;
        }
 
        /// <summary>
        /// 验证物料绑定数量是否超过入库数量
        /// </summary>
        /// <param name="OrdNo"></param>
        /// <param name="MatNo">物料编码guid</param>
        /// <param name="CurCount"></param>
        /// <param name="BindCount"></param>
        /// <param name="remainCount"></param>
        /// <param name="PalletNo"></param>
        /// <returns></returns>
        public bool GetRemainBindCount(string OrdNo, string MatNo, ref int CurCount, ref int BindCount, ref int remainCount, string PalletNo)
        {
            bool bR = false;
            try
            {
                //获取该物料在入库单中数量
                StringBuilder sqlSelOrdCount = new StringBuilder();
                sqlSelOrdCount.Append("select CurQuant  from ErpinDetail where OrdNo = @OrdNo and MatNo = @MatNo");
                SqlParam[] para = new SqlParam[]
                {
                    new SqlParam("@OrdNo", OrdNo),
                    new SqlParam("@MatNo", MatNo),
                };
                IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(sqlSelOrdCount, para);
                OrdMatCount omc = ModelConvertHelper<OrdMatCount>.ReaderToModel(dt);
                if (omc == null)
                    return false;
                CurCount = int.Parse(omc.CurQuant);
 
                //获取该物料已经绑定数量
                StringBuilder sqlBindCount = new StringBuilder();
                sqlBindCount.Append("select MatCount from IPalletBind where OrdNo = @OrdNo and MatGuid = @MatNo and IsDel = 0 and Palno != @PalletNo");
                SqlParam[] para1 = new SqlParam[]
                {
                    new SqlParam("@OrdNo", OrdNo),
                    new SqlParam("@MatNo", MatNo),
                    new SqlParam("@PalletNo", PalletNo),
                };
                IDataReader dt1 = DataFactory.SqlDataBase().GetDataReaderBySQL(sqlBindCount, para1);
                IList<PalBind> lstCount = new List<PalBind>();
                lstCount = ModelConvertHelper<PalBind>.DataReaderToModel(dt1);
                for (int i = 0; i < lstCount.Count; i++)
                {
                    BindCount += int.Parse(lstCount[i].MatCount);
 
                }
                //获取当前绑定数量(修改绑定时考虑)                
                remainCount = CurCount - BindCount;
 
            }
            catch (Exception ex)
            {
                bR = false;
                throw ex;
            }
            return bR;
        }
 
        public bool Add(PalBind model, List<PalBind> lst)
        {
            bool result = false;
            try
            {
                if (lst.Count > 0)
                {
                    return false;
                }
 
                int _ret = DataFactory.SqlDataBase().DeleteData("IPalletBind", "Palno", lst[0].Palno);
                if (_ret < 0)
                {
                    return false;
                }
                StringBuilder strSqlTime = new StringBuilder();
                strSqlTime.Append("SELECT GETDATE() as time");
                var dtTime = DataFactory.SqlDataBase().GetDataTableBySQL(strSqlTime);
                model.CreateTime = DateTime.Parse(dtTime.Rows[0][0].ToString());
                List<SqlParam> para = new List<SqlParam>();
                StringBuilder strSql = new StringBuilder();
 
                strSql.Append(" Insert into IPalletBind (PalletNo,MatNo,MatCount,GoodsPos,Statu,LingNo,TuNo,PageNo,CreateUser,CreateTime,Demo,Addre,IsDel,OrdNo) values ");
                for (int i = 0; i < lst.Count; i++)
                {
                    string Statu = "1";
                    if (lst[i].LocationCode != null)
                    {
                        if (lst[i].LocationCode.Length > 10)       // 指定货位单据状态为正在执行
                        {
                            Statu = "2";
 
                            DAL.DAL_Pub pub = new DAL.DAL_Pub();
                            StringBuilder sqlStr = new StringBuilder();
                            sqlStr.Append("select count(*) from WH_CMD where palno = '" + lst[i].Palno + "'");
                            sqlStr.Append(" and CMDStatu = '2' and IsDel = '0';");
                            DataTable whdt = DataFactory.SqlDataBase().GetDataTableBySQL(sqlStr);
                            if (whdt.Rows[0][0].ToString() == "0")
                            {
                                // 生成指令
                                sqlStr.Clear();
                                sqlStr.Append("INSERT INTO WH_CMD(TaskID,Palno,Height,CMDType,OldAddre,CMDStatu,CreateUser,IsDel)");
                                sqlStr.Append("VALUES ('" + lst[i].OrdNo + "','" + lst[i].Palno + "','200','in','");
                                sqlStr.Append(lst[i].LocationCode + "','2','" + model.CreatUser + "','0');");
                                DataFactory.SqlDataBase().ExecuteBySql(sqlStr);
 
                                // 更改库位状态
                                pub.UpdateLocationState(lst[i].Palno, "03");
                            }
 
                            // 更改入库单状态
                            sqlStr.Clear();
                            sqlStr.Append("Update Erp_in set Statu = '02' where Ordno='" + lst[i].OrdNo + "';");
                            DataFactory.SqlDataBase().ExecuteBySql(sqlStr);
                        }
                    }
 
                    strSql.Append("(@PalletNo" + i + ",");
                    para.Add(new SqlParam("@PalletNo" + i, lst[i].Palno));
                    strSql.Append("@MatNo" + i + ",");
                    para.Add(new SqlParam("@MatNo" + i, lst[i].MatNo));
                    strSql.Append("@MatCount" + i + ",");
                    para.Add(new SqlParam("@MatCount" + i, lst[i].MatCount));
                    strSql.Append("@Statu" + i + ",");
                    para.Add(new SqlParam("@Statu" + i, Statu));
                    strSql.Append("@LingNo" + i + ",");
                    para.Add(new SqlParam("@LingNo" + i, lst[i].LingNo));
                    strSql.Append("@CreateUser" + i + ",");
                    para.Add(new SqlParam("@CreateUser" + i, model.CreatUser));
                    strSql.Append("@CreateTime" + i + ",");
                    para.Add(new SqlParam("@CreateTime" + i, model.CreateTime));
                    strSql.Append("@Demo" + i + ",");
                    para.Add(new SqlParam("@Demo" + i, model.Demo));
                    strSql.Append("@Addre" + i + ",");
                    para.Add(new SqlParam("@Addre" + i, lst[i].LocationCode));
                    strSql.Append("@IsDel" + i + ",");
                    para.Add(new SqlParam("@IsDel" + i, 0));
                    strSql.Append("@OrdNo" + i + "),");
                    para.Add(new SqlParam("@OrdNo" + i, lst[i].OrdNo));
                }
 
                SqlParam[] param = null;
                if (para != null)
                {
                    strSql.Remove(strSql.Length - 1, 1);
                    param = para.ToArray();
                }
 
                int dt = DataFactory.SqlDataBase().ExecuteBySql(strSql, param);
 
                if (dt > -1) result = true;
 
                return result;
            }
            catch
            {
                return result;
            }
        }
 
        public bool UpdateErpinStatus(string OrderNo, string Statu)
        {
            try
            {
                bool bl = false;
                StringBuilder strSQL = new StringBuilder();
                Hashtable ht = new Hashtable();
 
                ht["Statu"] = "'" + Statu + "'";
                int ret = DataFactory.SqlDataBase().UpdateByHashtable("Erp_in", "Ordno", "'" + OrderNo + "'", ht);
                if (ret > 0)
                {
                    bl = true;
                }
 
                return bl;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public string SetPalBinds(string uesrCode, string departGuid, List<PalBind> palBinds)
        {
            try
            {
                string strMessage = "";
                int rowCount = 0;
                //StringBuilder strSqlTime = new StringBuilder();
                //strSqlTime.Append("SELECT GETDATE() as time");
                //var dtTime = DataFactory.SqlDataBase().GetDataTableBySQL(strSqlTime);
                //var creatTime = DateTime.Parse(dtTime.Rows[0][0].ToString());
                foreach (PalBind pal in palBinds)
                {
                    StringBuilder sqlString = new StringBuilder();
                    if (string.IsNullOrEmpty(pal.Guid))
                    {
                        // 新增编码绑定信息
                        Hashtable htitems = new Hashtable();
                        htitems.Add("OrdNo", "'" + pal.OrdNo + "'");
                        htitems.Add("Palno", "'" + pal.Palno + "'");
                        htitems.Add("MatGuid", "'" + pal.MatGuid + "'");
                        htitems.Add("MatNo", "'" + pal.MatNo + "'");
                        htitems.Add("MatCount", "'" + pal.MatCount + "'");
                        htitems.Add("LocationCode", "'" + pal.LocationCode + "'");
                        htitems.Add("Statu", "'01'");
                        htitems.Add("Demo", "'" + pal.Demo + "'");
                        htitems.Add("DepartGuid", "'" + departGuid + "'");
                        htitems.Add("CreateUser", "'" + uesrCode + "'");
                        htitems.Add("CreateTime", "GETDATE()");
                        int num = DataFactory.SqlDataBase().InsertByHashtableNullParam("IPalletBind", htitems);
 
                        if (pal.LocationCode != null && pal.LocationCode != "")
                        {
                            Hashtable ht = new Hashtable();
 
                            ht["TurnoverDemand"] = "'03'";
                            int ret = DataFactory.SqlDataBase().UpdateByHashtable("DepotsLocation", "LocationCode", "'" + pal.LocationCode + "'", ht);
                        }
 
                        if (num > 0)
                        {
                            rowCount += num;
                        }
                        else
                        {
                            strMessage += pal.MatNo + "物料绑定失败!";
                        }
                    }
                    else
                    {
 
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.Append("select * from IPalletBind where IsDel = 0 and Statu = '01' and palno = '"+pal.Palno+"'");
                        DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
                        foreach (DataRow dr in dt.Rows)
                        {
                            Hashtable htitems2 = new Hashtable();
                            if (dr["OrdNo"].ToString() != pal.OrdNo)
                            {
                                htitems2.Clear();
                                htitems2.Add("LocationCode", "'" + pal.LocationCode + "'");
                                string guid2 = "'" + dr["Guid"].ToString() + "'";
 
                                int num2 = DataFactory.SqlDataBase().UpdateByHashtable("IPalletBind", "Guid", guid2, htitems2);
 
                                if (num2 > 0)
                                {
                                    // 修改入库单状态
                                    this.UpdateErpinStatus(dr["OrdNo"].ToString(), "02");
                                }
                            }
 
                            if (dr["OrdNo"].ToString() == pal.OrdNo)
                            {
                                if (pal.LocationCode != null && pal.LocationCode != "")
                                {
                                    Hashtable ht = new Hashtable();
 
                                    ht["TurnoverDemand"] = "'03'";
                                    int ret = DataFactory.SqlDataBase()
                                        .UpdateByHashtable("DepotsLocation", "LocationCode", "'" + pal.LocationCode + "'", ht);
 
 
                                }
                                else
                                {
                                    Hashtable ht = new Hashtable();
 
                                    ht["TurnoverDemand"] = "'01'";
                                    int ret = DataFactory.SqlDataBase()
                                        .UpdateByHashtable("DepotsLocation", "LocationCode", "'" + dr["LocationCode"].ToString() + "'", ht);
                                }
                            }
                        }
                        // 维护编码绑定信息
                        Hashtable htitems = new Hashtable();
                        htitems.Add("OrdNo", "'" + pal.OrdNo + "'");
                        htitems.Add("Palno", "'" + pal.Palno + "'");
                        htitems.Add("MatGuid", "'" + pal.MatGuid + "'");
                        htitems.Add("MatNo", "'" + pal.MatNo + "'");
                        htitems.Add("MatCount", "'" + pal.MatCount + "'");
                        htitems.Add("LocationCode", "'" + pal.LocationCode + "'");
                        htitems.Add("Demo", "'" + pal.Demo + "'");
                        htitems.Add("DepartGuid", "'" + departGuid + "'");
                        htitems.Add("UpdateTime", "getdate()");
                        htitems.Add("UpdateUser", "'" + uesrCode + "'");
                        string guid = "'" + pal.Guid + "'";
 
                        int num = DataFactory.SqlDataBase().UpdateByHashtable("IPalletBind", nameof(pal.Guid), guid, htitems);
                        
                        if (num > 0)
                        {
                            rowCount += num;
                        }
                        else
                        {
                            strMessage += pal.MatNo + "物料编辑失败!";
                        }
                    }
                    if (rowCount > 0)
                    {
                        // 修改入库单状态
                        this.UpdateErpinStatus(pal.OrdNo, "02");
                    }
                }
                return strMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public string DelPalBind(string userCode, string palno, string ordNo, string LocationCode)
        {
            try
            {
                string strMessage = "";
                // 验证组盘单状态
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("select isnull(max(statu),'-1') as statu from View_IPalletBind ");
                stringBuilder.Append("where Palno = '" + palno + "' and OrdNo = '" + ordNo + "';");
                DataRow row = DataFactory.SqlDataBase().GetDataRowBySQL(stringBuilder);
                if (row["statu"].ToString() != "01")
                {
                    strMessage = "单据状态变更,删除失败!";
                }
                else
                {
                    stringBuilder.Clear();
                    stringBuilder.Append("Update IPalletBind set IsDel = '1',UpdateUser ='" + userCode + "',UpdateTime = getDate() ");
                    stringBuilder.Append("where isDel = '0' and statu = '01' and Palno = '" + palno + "' and OrdNo = '" + ordNo + "';");
                    int rowCount = DataFactory.SqlDataBase().ExecuteBySql(stringBuilder);
                    if (rowCount > 0)
                    {
                        stringBuilder.Clear();
                        stringBuilder.Append("select count(*) from IPalletBind where isdel = '0' and OrdNo = '" + ordNo + "';");
                        DataRow rows = DataFactory.SqlDataBase().GetDataRowBySQL(stringBuilder);
                        if (rows != null && rows[0].ToString() == "0")
                        {
                            this.UpdateErpinStatus(ordNo, "01");
                        }
 
                        stringBuilder.Clear();
                        stringBuilder.Append("select * from IPalletBind where IsDel = 0 and Statu = '01' and palno = '" + palno + "'");
                        DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(stringBuilder);
                        if (dt.Rows.Count <= 0)
                        {
                            Hashtable ht = new Hashtable();
 
                            ht["TurnoverDemand"] = "'01'";
                            int ret = DataFactory.SqlDataBase()
                                .UpdateByHashtable("DepotsLocation", "LocationCode", "'" + LocationCode + "'", ht);
                        }
                    }
                    else
                    {
                        strMessage = "单据状态变更,删除失败!";
                    }
                }
 
                return strMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public void DelPalBindDetail(string userCode, string guid, string ordNo)
        {
            try
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("Update IPalletBind set IsDel = '1',UpdateUser ='" + userCode + "',UpdateTime = getDate() ");
                stringBuilder.Append("where isDel = '0' and guid = '" + guid + "';");
                int rowCount = DataFactory.SqlDataBase().ExecuteBySql(stringBuilder);
                if (rowCount > 0)
                {
                    stringBuilder.Clear();
                    stringBuilder.Append("select count(*) from IPalletBind where isdel = '0' and OrdNo = '" + ordNo + "';");
                    DataRow rows = DataFactory.SqlDataBase().GetDataRowBySQL(stringBuilder);
                    if (rows != null && rows[0].ToString() == "0")
                    {
                        this.UpdateErpinStatus(ordNo, "01");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
 
 
 
 
 
 
 
 
        public bool GetPalletBind(string PalletNo, ref IList<PalletBind> lst)
        {
            bool ret = false;
            try
            {
                IList<PalletBind> ls = new List<PalletBind>();
                StringBuilder strSql = new StringBuilder();
                strSql.Append("Select * from View_IPalletBindDetail where ");
                strSql.Append("PalletNo = @PalletNo");
 
                SqlParam[] para = new SqlParam[]
                {
                    new SqlParam("@PalletNo", PalletNo),
                };
 
                IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, para);
 
                lst = ModelConvertHelper<PalletBind>.DataReaderToModel(dt);
 
                ret = true;
            }
            catch
            {
                ret = false;
                throw new NotImplementedException();
            }
            return ret;
        }
 
        public bool GetOrderMatByMatNoAndOrd(string OrdNo, string MatNo, ref ErpInDetail eid)
        {
            bool br = true;
            try
            {
                StringBuilder strSQL = new StringBuilder();
 
                strSQL.Append("select OrdNo, MatNo, MatName, CurQuant, LingNo, TuNo, PageNo, Demo from View_ErpInDetail where ");
                if (!string.IsNullOrEmpty(OrdNo))
                {
                    strSQL.Append("OrdNo = '");
                    strSQL.Append(OrdNo);
                    strSQL.Append("' and MatNo = '");
                    strSQL.Append(MatNo);
                    strSQL.Append("'");
                }
                else
                {
                    strSQL.Append(" MatNo = '");
                    strSQL.Append(MatNo);
                    strSQL.Append("'");
                }
 
 
                IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSQL);
                if (dt != null)
                {
                    List<ErpInDetail> ls = new List<ErpInDetail>();
                    ls = (List<ErpInDetail>)ModelConvertHelper<ErpInDetail>.DataReaderToModel(dt);
                    eid = ls[0];
                    br = true;
                }
            }
            catch (Exception ex)
            {
                br = false;
            }
            return br;
        }
 
        public bool IsPalletUsing(string PalletNo)
        {
            bool br = false;
            StringBuilder strSQL = new StringBuilder();
 
            // 判断组盘中是否存在  组盘是否已使用
            //strSQL.Append("select 1 from IPalletBind where Palno = '");
            //strSQL.Append(PalletNo);
            //strSQL.Append("' and IsDel = 0 and Statu <> '03'");
 
            //DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(strSQL);
            //if (dt != null && dt.Rows.Count > 0)
            //{
            //    br = true;
            //}
 
            strSQL.Clear();
            strSQL.Append("select 1 from IPalletEmptyIn where Palno = '");
            strSQL.Append(PalletNo);
            strSQL.Append("' and IsDel = 0 and Statu <> '03' and Statu <> '01'");
 
            DataTable dt2 = DataFactory.SqlDataBase().GetDataTableBySQL(strSQL);
            if (dt2 != null && dt2.Rows.Count > 0)
            {
                br = true;
            }
 
            // 判断库中是否存在  是否已使用
            strSQL.Clear();
            strSQL.Append("select TurnoverDemand from DepotsLocation where LocationCode in (");
            strSQL.Append("select LocationCode from log_Store where Palno = '" + PalletNo + "' AND IsDel=0)");
            DataTable dtt = DataFactory.SqlDataBase().GetDataTableBySQL(strSQL);
            if (dtt != null && dtt.Rows.Count > 0)
            {
                string tDemand = dtt.Rows[0][0].ToString();
                if (tDemand == "02" || tDemand == "04")
                {
                    br = true;
                }
            }
 
            return br;
        }
 
        //private bool UpdatePalletBind(string PalletNo)
        //{
        //    return true;
        //}
 
        //private bool UpdateLogStore(string PalletNo)
        //{
        //    return true;
        //}
 
        //private bool UpdateLogDetail(string PalletNo)
        //{
        //    return true;
        //}
 
        public bool UpdateErpinByPalletNo(IList<ErpIn> lst)
        {
            bool r = true;
 
            try
            {
                for (int i = 0; i < lst.Count; i++)
                {
                    StringBuilder strSQL = new StringBuilder();
 
                    strSQL.Append("select * from IPalletBind where OrdNo = '");
                    strSQL.Append(lst[i].OrdNo);
                    strSQL.Append("' and IsDel = 0");
 
                    DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(strSQL);
                    if (dt != null)
                    {
                        if (dt.Rows.Count == 0)
                            UpdateErpinStatus(lst[i].OrdNo, "01");
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return r;
        }
 
        public bool GetPalletBindStatus(string PalletNo, string status)
        {
            bool r = false;
 
            StringBuilder strSQL = new StringBuilder();
 
            strSQL.Append("select * from View_IPalletBind where PalletNo = '");
            strSQL.Append(PalletNo);
            strSQL.Append("' and Statu = '");
            strSQL.Append(status);
            strSQL.Append("'");
 
 
            DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(strSQL);
            if (dt == null)
            {
                return false;
            }
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        //public bool GetIPalletEmptyInCount(string Palno)
        //{
        //    try
        //    {
        //        IList<PalletBind> ls = new List<PalletBind>();
        //        StringBuilder strSql = new StringBuilder();
        //        strSql.Append("select * from IPalletBind ");
        //        strSql.Append("where Palno = @Palno and OrdNo = @OrdNo ");
        //        strSql.Append("and DepartGuid = @DepartGuid ");
 
        //        SqlParam[] para = new SqlParam[]
        //        {
        //            new SqlParam("@Palno", palBind.Palno),
        //            new SqlParam("@OrdNo", palBind.OrdNo),
        //            new SqlParam("@DepartGuid", palBind.DepartGuid)
        //        };
 
        //        IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql, para);
        //        lst = ModelConvertHelper<PalBind>.DataReaderToModel(dt);
 
        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}
    }
}