yl
2022-07-16 4545b915123fb7b7535b697f6e82b12f525f8b71
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VueWebApi.Tools;
 
namespace VueWebApi.DLL.DAL
{
    public class ProductionManagementDAL
    {
        public static DataTable dt;    //定义全局变量dt
        public static bool res;       //定义全局变量dt
        public static ToMessage mes = new ToMessage(); //定义全局返回信息对象
        public static string strProcName = ""; //定义全局sql变量
        public static List<SqlParameter> listStr = new List<SqlParameter>(); //定义全局参数集合
        public static SqlParameter[] parameters; //定义全局SqlParameter参数数组
 
 
        #region[ERP订单查询]
        public static ToMessage ErpOrderSearch(string erporderstus, string erpordercode, string partcode, string partname, string partspec, int startNum, string paydatestartdate, string paydateenddate, string creatuser, string createstartdate, string createenddate, int endNum, string prop, string order)
        {
            var dynamicParams = new DynamicParameters();
            string search = "";
            try
            {
                if (erporderstus != "" && erporderstus != null)
                {
                    search += "and A.status=@erporderstus ";
                    dynamicParams.Add("@erporderstus", erporderstus);
                }
                if (erpordercode != "" && erpordercode != null)
                {
                    search += "and A.wo like '%'+@erpordercode+'%' ";
                    dynamicParams.Add("@erpordercode", erpordercode);
                }
                if (partcode != "" && partcode != null)
                {
                    search += "and A.materiel_code like '%'+@partcode+'%' ";
                    dynamicParams.Add("@partcode", partcode);
                }
                if (partname != "" && partname != null)
                {
                    search += "and B.partname like '%'+@partname+'%' ";
                    dynamicParams.Add("@partname", partname);
                }
                if (partspec != "" && partspec != null)
                {
                    search += "and B.partspec like '%'+@partspec+'%' ";
                    dynamicParams.Add("@partspec", partspec);
                }
                if (paydatestartdate != "" && paydatestartdate != null)
                {
                    search += "and A.paydate between @paydatestartdate and @paydateenddate ";
                    dynamicParams.Add("@paydatestartdate", paydatestartdate+" 00:00:00");
                    dynamicParams.Add("@paydateenddate", paydateenddate + " 23:59:59");
                }
                if (createstartdate != "" && createstartdate != null)
                {
                    search += "and A.createdate between @createstartdate and @createenddate ";
                    dynamicParams.Add("@createstartdate", createstartdate);
                    dynamicParams.Add("@createenddate", createenddate + " 23:59:59");
                }
                if (creatuser != "" && creatuser != null)
                {
                    search += "and A.createuser like '%'+@creatuser+'%' ";
                    dynamicParams.Add("@creatuser", creatuser);
                }
 
                if (search == "")
                {
                    search = "and 1=1 ";
                }
                // --------------查询指定数据--------------
                var total = 0; //总条数
                var sql = @"select A.status,A.wo,A.materiel_code as partcode,B.partname,B.partspec,A.qty,A.relse_qty,A.wkshp_code,C.org_name as wkshp_name,
                            A.stck_code,D.name as stck_name,A.paydate,A.createuser,A.createdate 
                            from TKimp_Ewo A
                            left join TMateriel_Info B on A.materiel_code=B.partcode
                            left join TOrganization C on A.wkshp_code=C.org_code
                            left join T_Sec_Stck D on A.stck_code=D.code where A.is_delete<>'1' " + search;
                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.count = total;
                mes.data = data.ToList();
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[ERP订单下达]
        public static ToMessage MarkSaveErpOrder(string erpordercode, string partcode, string wkshopcode, string warehousecode, string erpqty, string markqty, string ordernum, string relse_qty, string username)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                list.Clear();
                //获取拆单数量:向下取整
                decimal cdqty = Math.Floor(decimal.Parse(markqty) / decimal.Parse(ordernum));
                //定义累计下单数量
                decimal sumqty = 0;
                //定义最新生成的工单号
                string wo = "";
                //定义工单流水号
                int num = 0;
                //循环下单单数(生成对应几张MES工单)
                for (int i = 1; i <= Convert.ToInt32(ordernum); i++)
                {
                    sumqty += cdqty;
                    //获取最大单据号
                    if (i == 1)  //首单获取工单号
                    {
                        sql = @"select isnull(max(substring(wo_code,charindex('_',wo_code)+1,len(wo_code)-charindex('_',wo_code))),0)+1 as worknumb from TK_Wrk_Man where m_po=@erpordercode";
                        dynamicParams.Add("@erpordercode", erpordercode);
                        var data = DapperHelper.selectdata(sql, dynamicParams);
                        num = Convert.ToInt32(data.Rows[0]["WORKNUMB"].ToString());
                        wo = erpordercode + "_" + num;
                    }
                    else
                    {
                        num = num + 1;
                        wo = erpordercode + "_" + num;
                    }
                    if (i == Convert.ToInt32(ordernum))  //最后一单时
                    {
                        sql = @"insert into TK_Wrk_Man(wo_code,status,wkshp_code,plan_qty,stck_code,materiel_code,m_po,lm_user,lm_date) values(@wo_code,@status,@wkshp_code,@plan_qty,@stck_code,@materiel_code,@m_po,@username,@CreateDate)";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo_code = wo,
                                status = "NEW",
                                wkshp_code = wkshopcode,
                                plan_qty = cdqty + (decimal.Parse(markqty) - sumqty),  //末单下单数量=切分数量+(下单数量-累计切分下单数量)
                                stck_code = warehousecode,
                                materiel_code = partcode,
                                m_po = erpordercode,
                                username = username,
                                CreateDate = DateTime.Now.ToString()
                            }
                        });
                        sumqty = sumqty + (decimal.Parse(markqty) - sumqty);
                    }
                    else
                    {
 
                        sql = @"insert into TK_Wrk_Man(wo_code,status,wkshp_code,plan_qty,stck_code,materiel_code,m_po,lm_user,lm_date) values(@wo_code,@status,@wkshp_code,@plan_qty,@stck_code,@materiel_code,@m_po,@username,@CreateDate)";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo_code = wo,
                                status = "NEW",
                                wkshp_code = wkshopcode,
                                plan_qty = cdqty,
                                stck_code = warehousecode,
                                materiel_code = partcode,
                                m_po = erpordercode,
                                username = username,
                                CreateDate = DateTime.Now.ToString()
                            }
                        });
                    }
                }
                if (decimal.Parse(erpqty) == decimal.Parse(markqty) + decimal.Parse(relse_qty))   //如果ERP订单=下单数量+已下单数量,则更新ERP订单表状态为CREATED:已创建
                {
                    sql = @"update  TKimp_Ewo set status='CREATED',relse_qty=relse_qty+@sumqty where wo=@wo";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wo = erpordercode,
                            sumqty = sumqty
                        }
                    });
                }
                else   //更新ERP订单表状态为CREATING:创建中
                {
                    sql = @"update  TKimp_Ewo set status='CREATING',relse_qty=relse_qty+@sumqty where wo=@wo";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wo = erpordercode,
                            sumqty = sumqty
                        }
                    });
                }
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    mes.code = "200";
                    mes.count = 0;
                    mes.Message = "下达MES工单成功!";
                    mes.data = null;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "下达MES工单成功失败!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[ERP订单关闭]
        public static ToMessage ClosedErpOrder(string erpordercode, string username)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                sql = @"select *  from TK_Wrk_Man where m_po=@erpordercode and status<> 'CLOSED'";
                dynamicParams.Add("@erpordercode", erpordercode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "当前订单有下达未关闭的MES工单,订单不允许关闭,请先删除或关闭相关工单!";
                    mes.data = null;
                }
                else
                {
                    //关闭订单
                    sql = @"update  TKimp_Ewo set status='CLOSED' where wo=@wo";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wo = erpordercode
                        }
                    });
                }
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    mes.code = "200";
                    mes.count = 0;
                    mes.Message = "订单关闭成功!";
                    mes.data = null;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "订单关闭失败!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
 
 
        #region[MES工单查询]
        public static ToMessage ErpOrderSearch(string mesorderstus, string mesordercode, string partcode, string partname, string partspec, int startNum, string creatuser, string createdate, int endNum, string prop, string order)
        {
            var dynamicParams = new DynamicParameters();
            string search = "";
            try
            {
                if (mesorderstus != "" && mesorderstus != null)
                {
                    search += "and A.status=@mesorderstus ";
                    dynamicParams.Add("@mesorderstus", mesorderstus);
                }
                if (mesordercode != "" && mesordercode != null)
                {
                    search += "and A.wo_code like '%'+@mesordercode+'%' ";
                    dynamicParams.Add("@mesordercode", mesordercode);
                }
                if (partcode != "" && partcode != null)
                {
                    search += "and A.materiel_code like '%'+@partcode+'%' ";
                    dynamicParams.Add("@partcode", partcode);
                }
                if (partname != "" && partname != null)
                {
                    search += "and B.partname like '%'+@partname+'%' ";
                    dynamicParams.Add("@partname", partname);
                }
                if (partspec != "" && partspec != null)
                {
                    search += "and B.partspec like '%'+@partspec+'%' ";
                    dynamicParams.Add("@partspec", partspec);
                }
                if (createdate != "" && createdate != null)
                {
                    search += "and CONVERT(varchar(100),A.lm_date,23)=@createdate ";
                    dynamicParams.Add("@createdate", createdate);
                }
                if (creatuser != "" && creatuser != null)
                {
                    search += "and A.lm_user like '%'+@creatuser+'%' ";
                    dynamicParams.Add("@creatuser", creatuser);
                }
 
                if (search == "")
                {
                    search = "and 1=1 ";
                }
                // --------------查询指定数据--------------
                var total = 0; //总条数
                var sql = @"select A.status,A.wo_code,A.materiel_code as partcode,B.partname,B.partspec,A.plan_qty,A.wkshp_code,C.org_name as wkshp_name,
                            A.route_code,E.name as route_name,A.stck_code,F.name as stck_name,A.plan_startdate,A.plan_enddate,A.piroque,A.m_po,A.lm_user,A.lm_date
                            from TK_Wrk_Man A
                            left join TMateriel_Info B on A.materiel_code=B.partcode
                            left join TOrganization C on A.wkshp_code=C.org_code
                            left join T_Sec_Stck D on A.stck_code=D.code 
                            left join TFlw_Rout E on A.route_code=E.code
                            left join T_Sec_Stck F on A.stck_code=F.code where A.is_delete<>'1' " + search;
                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.count = total;
                mes.data = data.ToList();
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[产品编码查找工艺路线下拉接口]
        public static ToMessage PartSelectRoute(string partcode)
        {
            string sql = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //获取车间下拉框数据
                sql = @"select R.code,R.name,A.default_route  from TMateriel_Info A
                        inner join  TMateriel_Route M on A.partcode=M.materiel_code
                        inner join  TFlw_Rout R on M.route_code=R.code
                        where A.partcode=@partcode ";
                dynamicParams.Add("@partcode", partcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.data = data;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[工艺路线查找车间下拉接口]
        public static ToMessage RouteSelectWkshop(string partcode, string routecode)
        {
            string sql = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //获取车间下拉框数据
                sql = @"select distinct F.org_code,F.org_name  from TFlw_Rout A
                        inner join  TMateriel_Route M on A.code=M.route_code
                        inner join   TFlw_Rtdt B on A.code=B.rout_code
                        inner join TStep C on B.step_code=C.stepcode
                        inner join TFlw_Rteqp D on C.stepcode=D.step_code
                        inner join TEqpInfo  E on D.eqp_code=E.code
                        left join TOrganization F on E.wksp_code=F.org_code
                        where A.code=@routecode and M.materiel_code=@partcode ";
                dynamicParams.Add("@partcode", partcode);
                dynamicParams.Add("@routecode", routecode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.data = data;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[根据选择工艺路线查看工序接口]
        public static ToMessage SelectRouteStep(string routecode)
        {
            string sql = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //获取工艺路线对应工序信息
                sql = @"select A.seq,T.stepcode,T.stepname,T.flwtype,T.descr  from TFlw_Rtdt A
                        left join  TStep T on A.step_code=T.stepcode
                        where A.rout_code=@partcode ";
                dynamicParams.Add("@partcode", routecode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.data = data;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[MES工单新增、编辑提交]
        public static ToMessage AddUpdateMesOrder(string mesorderstus, string mesordercode, string partcode, string mesqty, string routecode, string wkshopcode, string planstartdate, string planenddate, string orderlev, string username, string opertype)
        {
            var sql = "";
            var dynamicParams = new DynamicParameters();
            List<object> list = new List<object>();
            try
            {
                if (opertype == "Add")
                {
                    //写入工单表
                    sql = @"insert into TK_Wrk_Man(wo_code,status,wkshp_code,plan_qty,plan_startdate,plan_enddate,route_code,stck_code,lm_user,lm_date,materiel_code,m_po,piroque)
                                values(@mesordercode,@mesorderstus,@wkshopcode,@mesqty,@planstartdate,@planenddate,@routecode,@stck_code,@username,@CreateDate,@materiel_code,@m_po,@orderlev)";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            mesordercode = mesordercode,
                            mesorderstus = "ALLO", //派发
                            wkshopcode = wkshopcode,
                            mesqty = mesqty,
                            planstartdate = planstartdate,
                            planenddate = planenddate,
                            routecode = routecode,
                            stck_code = "",
                            username = username,
                            CreateDate = DateTime.Now.ToString(),
                            materiel_code = partcode,
                            m_po = "",
                            orderlev = orderlev
                        }
                    });
                    //写入工序任务表
                    sql = @"insert into TK_Wrk_Step(wo_code,seq,step_code,plan_qty,plan_startdate,plan_enddate,status,isbott,isend,route_code,lm_user,lm_date)
                            select @mesordercode as wo_code,A.seq,A.step_code,@mesqty as plan_qty,@planstartdate as plan_startdate,@planenddate as plan_enddate,@status as status,  
                            A.first_choke,A.last_choke,A.rout_code,@username,@CreateDate
                            from TFlw_Rtdt A
                            left join TStep B on A.step_code=B.stepcode
                            left join TFlw_Rout C on A.rout_code=C.code
                            where A.rout_code=@routecode
                            and B.is_delete<>'1' and C.enable='Y' and C.is_delete<>'1'";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            mesordercode = mesordercode,
                            mesqty = mesqty,
                            planstartdate = planstartdate,
                            planenddate = planenddate,
                            routecode = routecode,
                            status = "ALLO",  //派发
                            username = username,
                            CreateDate = DateTime.Now.ToString()
                        }
                    });
 
                    bool aa = DapperHelper.DoTransaction(list);
                    if (aa)
                    {
                        mes.code = "200";
                        mes.count = 0;
                        mes.Message = "MES工单新建派发成功!";
                        mes.data = null;
                    }
                    else
                    {
                        mes.code = "300";
                        mes.count = 0;
                        mes.Message = "MES工单新建派发失败!";
                        mes.data = null;
                    }
                }
                if (opertype == "Update")
                {
                    sql = @"update TK_Wrk_Man set route_code=@routecode,wkshp_code=@wkshopcode,plan_startdate=@planstartdate,plan_enddate=@planenddate,status=@status,piroque=@orderlev,lm_user=@username,lm_date=@CreateDate where wo_code=@mesordercode";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            mesordercode = mesordercode,
                            wkshopcode = wkshopcode,
                            planstartdate = planstartdate,
                            planenddate = planenddate,
                            status = "ALLO",  //派发
                            routecode = routecode,
                            username = username,
                            CreateDate = DateTime.Now.ToString(),
                            orderlev = orderlev
                        }
                    });
                    //写入工序任务表
                    sql = @"insert into TK_Wrk_Step(wo_code,seq,step_code,plan_qty,plan_startdate,plan_enddate,status,isbott,isend,route_code,lm_user,lm_date)
                            select @mesordercode as wo_code,A.seq,A.step_code,@mesqty as plan_qty,@planstartdate as plan_startdate,@planenddate as plan_enddate,@status as status,  
                            A.first_choke,A.last_choke,A.rout_code,@username,@CreateDate
                            from TFlw_Rtdt A
                            left join TStep B on A.step_code=B.stepcode
                            left join TFlw_Rout C on A.rout_code=C.code
                            where  A.rout_code=@routecode
                            and B.is_delete<>'1' and C.enable='Y' and C.is_delete<>'1'";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            mesordercode = mesordercode,
                            mesqty = mesqty,
                            planstartdate = planstartdate,
                            planenddate = planenddate,
                            status = "ALLO",  //派发
                            username = username,
                            routecode= routecode,
                            CreateDate = DateTime.Now.ToString()
                        }
                    });
                    bool aa = DapperHelper.DoTransaction(list);
                    if (aa)
                    {
                        mes.code = "200";
                        mes.count = 0;
                        mes.Message = "修改操作成功!";
                        mes.data = null;
                    }
                    else
                    {
                        mes.code = "300";
                        mes.count = 0;
                        mes.Message = "修改操作失败!";
                        mes.data = null;
                    }
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[MES工单删除]
        public static ToMessage DeleteMesOrder(string wocode, string m_po, string orderqty)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                //判断工单是否为未开始状态或者已派发状态(满足其中一种都可删除,否则不允许删除)
                sql = @"select *  from TK_Wrk_Man where wo_code=@wocode and status='NEW' or status='ALLO'";
                dynamicParams.Add("@wocode", wocode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    if (m_po != "" && m_po != null)
                    {
                        //查询生产订单表数据
                        sql = @"select *  from TKimp_Ewo where wo=@m_po";
                        dynamicParams.Add("@m_po", m_po);
                        var data0 = DapperHelper.selectdata(sql, dynamicParams);
                        decimal relse_qty = decimal.Parse(data0.Rows[0]["RELSE_QTY"].ToString());//以下单数量
                        if ((relse_qty - decimal.Parse(orderqty)) == 0)  //全部撤销 订单状态回写未开始,已下单数量为0
                        {
                            //回写订单表状态及已下单数量
                            sql = @"update TKimp_Ewo set status='NEW',relse_qty=0  where wo=@m_po";
                            list.Add(new { str = sql, parm = new { m_po = m_po } });
                        }
                        else
                        {
                            //回写订单表状态及已下单数量
                            sql = @"update TKimp_Ewo set status='CREATING',relse_qty=relse_qty-@orderqty  where wo=@m_po";
                            list.Add(new { str = sql, parm = new { m_po = m_po, orderqty = decimal.Parse(orderqty) } });
                        }
                    }
                    //删除工单工序表
                    sql = @"delete TK_Wrk_Step  where wo_code=@wocode";
                    list.Add(new { str = sql, parm = new { wocode = wocode } });
 
                    //删除工单表
                    sql = @"update TK_Wrk_Man set is_delete='1'  where wo_code=@wocode";
                    list.Add(new { str = sql, parm = new { wocode = wocode } });
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "工单执行中或已关闭,不允许删除!";
                    mes.data = null;
                }
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    mes.code = "200";
                    mes.count = 0;
                    mes.Message = "删除成功!";
                    mes.data = null;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "删除失败!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[MES工单关闭]
        public static ToMessage ClosedMesOrder(string wocode, string m_po)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                //关闭工单对应工序任务
                sql = @"update TK_Wrk_Step set status='CLOSED'  where wo_code=@wocode";
                list.Add(new { str = sql, parm = new { wocode = wocode } });
                //回写工单表状态为(关闭)
                sql = @"update TK_Wrk_Man set status='CLOSED'  where wo_code=@wocode";
                list.Add(new { str = sql, parm = new { wocode = wocode } });
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    mes.code = "200";
                    mes.count = 0;
                    mes.Message = "工单关闭成功!";
                    mes.data = null;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "工单关闭失败!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[MES工单查看工序任务]
        public static ToMessage SearchWorkStep(string wo_code)
        {
            string sql = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //获取工序任务信息
                sql = @"select A.wo_code,A.seq,B.stepcode,B.stepname,A.plan_qty,A.good_qty,A.ng_qty  from  TK_Wrk_Step A
                        left join TStep B on A.step_code=B.stepcode
                        where A.wo_code=@wo_code";
                dynamicParams.Add("@wo_code", wo_code);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.data = data;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
 
 
        #region[生产开报工扫码获取工单对应工序任务(自制)]
        public static ToMessage MesOrderStepSearch(string orderstepqrcode, int startNum, int endNum, string prop, string order)
        {
            var sql = "";
            string search = "";
            string ordercode = "";
            string stepcode = "";
            var dynamicParams = new DynamicParameters();
            var total = 0; //总条数
            try
            {
                if (orderstepqrcode != "" && orderstepqrcode != null)
                {
                    string[] arra = orderstepqrcode.Split(';');
                    if (arra.Length == 1) //工单号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        mes.code = "300";
                        mes.count = 0;
                        mes.Message = "请扫描工序二维码!";
                        mes.data = null;
                        return mes;
                    }
                    if (arra.Length == 2) //工单号+工序号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        stepcode = arra[1]; //获取指定字符串前面的字符
                    }
 
                    if (ordercode != "" && ordercode != null) //工单号不为空,工序号为空
                    {
                        search += "and A.wo_code=@ordercode ";
                        dynamicParams.Add("@ordercode", ordercode);
                    }
                    if (ordercode != "" && stepcode != "") //工单号不为空,工序号不为空
                    {
                        search += "and A.wo_code=@ordercode ";
                        dynamicParams.Add("@ordercode", ordercode);
                        search += "and S.stepcode=@stepcode ";
                        dynamicParams.Add("@stepcode", stepcode);
                    }
                }
                //else
                //{
                //    mes.code = "300";
                //    mes.count = 0;
                //    mes.Message = "二维码信息为空!";
                //    mes.data = null;
                //    return mes;
                //}
                if (stepcode != "")
                {
                    //查找当前工序属性
                    sql = @"select *  from TStep  where stepcode=@stepcode";
                    dynamicParams.Add("@stepcode", stepcode);
                    var data0 = DapperHelper.selectdata(sql, dynamicParams);
                    if (data0.Rows.Count > 0) 
                    {
                        if (data0.Rows[0]["FLWTYPE"].ToString() == "W") 
                        {
                            mes.code = "300";
                            mes.count = 0;
                            mes.Message = "当前工序任务为外协工序任务,请前往外协操作页签执行!";
                            mes.data = null;
                            return mes;
                        }
                    }
                }
                //根据条件查询工单工序任务(自制工序)
                sql = @"select A.status,A.wo_code,B.route_code,M.partcode,M.partname,M.partspec,A.seq,A.isbott,A.isend,
                            S.stepcode,S.stepname,S.descr,A.plan_qty,A.good_qty,A.ng_qty,A.plan_startdate
                            from TK_Wrk_Step A
                            left join TK_Wrk_Man B on A.wo_code=B.wo_code
                            left join TMateriel_Info M on B.materiel_code=M.partcode
                            left join TStep S on A.step_code=S.stepcode
                            where A.status<>'CLOSED' and S.flwtype='Z'  " + search;
                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                if (data.ToList().Count > 0)
                {
                    mes.code = "200";
                    mes.count = total;
                    mes.Message = "查询成功!";
                    mes.data = data.ToList();
                    return mes;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "无可执行的生产任务,任务已完成或已关闭!";
                    mes.data = null;
                    return mes;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[生产开报工扫码获取工单对应工序任务(外协)]
        public static ToMessage MesOrderWxStepSearch(string orderstepqrcode, int startNum, int endNum, string prop, string order)
        {
            var sql = "";
            string search = "";
            string ordercode = "";
            string stepcode = "";
            var dynamicParams = new DynamicParameters();
            var total = 0; //总条数
            try
            {
                if (orderstepqrcode != "" && orderstepqrcode != null)
                {
                    string[] arra = orderstepqrcode.Split(';');
                    if (arra.Length == 1) //工单号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        mes.code = "300";
                        mes.count = 0;
                        mes.Message = "请扫描工序二维码!";
                        mes.data = null;
                        return mes;
                    }
                    if (arra.Length == 2) //工单号+工序号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        stepcode = arra[1]; //获取指定字符串前面的字符
                    }
                    if (ordercode != "" && ordercode != null) //工单号不为空,工序号为空
                    {
                        search += "and A.wo_code=@ordercode ";
                        dynamicParams.Add("@ordercode", ordercode);
                    }
                    if (ordercode != "" && stepcode != "") //工单号不为空,工序号不为空
                    {
                        search += "and A.wo_code=@ordercode ";
                        dynamicParams.Add("@ordercode", ordercode);
                        search += "and S.stepcode=@stepcode ";
                        dynamicParams.Add("@stepcode", stepcode);
                    }
                }
                //else
                //{
                //    mes.code = "300";
                //    mes.count = 0;
                //    mes.Message = "二维码信息为空!";
                //    mes.data = null;
                //    return mes;
                //}
                if (stepcode != "")
                {
                    //查找当前工序属性
                    sql = @"select *  from TStep  where stepcode=@stepcode";
                    dynamicParams.Add("@stepcode", stepcode);
                    var data0 = DapperHelper.selectdata(sql, dynamicParams);
                    if (data0.Rows.Count > 0)
                    {
                        if (data0.Rows[0]["FLWTYPE"].ToString() == "Z")
                        {
                            mes.code = "300";
                            mes.count = 0;
                            mes.Message = "当前工序任务为自制工序任务,请前往自制操作页签执行!";
                            mes.data = null;
                            return mes;
                        }
                    }
                }
                //根据条件查询工单工序任务(自制工序)
                sql = @"select A.status,A.wo_code,B.route_code,M.partcode,M.partname,M.partspec,A.seq,A.isbott,A.isend,
                            S.stepcode,S.stepname,S.descr,A.plan_qty,A.good_qty,A.ng_qty,A.plan_startdate
                            from TK_Wrk_Step A
                            left join TK_Wrk_Man B on A.wo_code=B.wo_code
                            left join TMateriel_Info M on B.materiel_code=M.partcode
                            left join TStep S on A.step_code=S.stepcode
                            where A.status<>'CLOSED' and S.flwtype='W'  " + search;
                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                if (data.ToList().Count > 0)
                {
                    mes.code = "200";
                    mes.count = total;
                    mes.Message = "查询成功!";
                    mes.data = data.ToList();
                    return mes;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "无可执行的生产任务,任务已完成或已关闭!";
                    mes.data = null;
                    return mes;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region [生产开报工:开工(开始/报工)/外协发料时条件判断及数据返回接口]
        public static ToMessage MesOrderStepStart(string OperType, string SelectType, string orderstepqrcode)
        {
            var sql = "";
            string search = "";
            string ordercode = "";
            string serialnumber = "";
            string stepcode = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                if (orderstepqrcode != "" && orderstepqrcode != null)
                {
                    string[] arra = orderstepqrcode.Split(';');
                    if (arra.Length == 1) //工单号二维码
                    {
                        mes.code = "300";
                        mes.count = 0;
                        mes.Message = "请扫描工序条码!";
                        mes.data = null;
                        return mes;
                    }
                    if (arra.Length == 2) //工单号+工序号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        stepcode = arra[1]; //获取指定字符串前面的字符
                    }
                    switch (OperType)
                    {
                        case "ZZ":
                            mes = ScanStartReport.ZZEncodingSeach(ordercode, stepcode);
                            break;
                        case "WX":
                            mes = ScanStartReport.WXEncodingSeach(SelectType, ordercode, stepcode);
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "二维码信息为空!";
                    mes.data = null;
                    return mes;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
 
        #endregion
 
        #region[生产开报工:开工时获取设备下拉列表]
        public static ToMessage MesOrderStepStartSelectEqp(string orderstepqrcode)
        {
            string sql = "";
            string ordercode = "";
            string stepcode = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                if (orderstepqrcode != "" && orderstepqrcode != null)
                {
                    string[] arra = orderstepqrcode.Split(';');
                    if (arra.Length == 1) //工单号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                    }
                    if (arra.Length == 2) //工单号+工序号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        stepcode = arra[1]; //获取指定字符串前面的字符
                    }
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "二维码信息为空!";
                    mes.data = null;
                    return mes;
                }
                //获取工序关联的设备
                sql = @"select B.code,B.name from TFlw_Rteqp A
                        left join TEqpInfo B on A.eqp_code=B.code
                        where A.style='E' and A.step_code=@stepcode";
                dynamicParams.Add("@stepcode", stepcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.data = data;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[生产开报工:报工时获取生产班组下拉框]
        public static ToMessage MesOrderStepReportSelectUserGroup()
        {
            string sql = "";
            try
            {
                //获取用户组
                sql = @"select group_code,group_name from TGroup";
                var data = DapperHelper.selecttable(sql);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.data = data;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[生产开报工:根据生产班组查找人员列表]
        public static ToMessage MesOrderGroupSelectUser(string usergroupcode)
        {
            string sql = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //班组获取人员列表
                sql = @"select usercode,username  from TUser where usergroup_code=@usergroupcode and is_delete<>'1'";
                dynamicParams.Add("@usergroupcode", usergroupcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.data = data;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[生产开报工:人员下拉列表]
        public static ToMessage MesOrderSelectUser(string usercode)
        {
            string sql = "";
            string search = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                if (usercode != "" && usercode != null) //工单号不为空,工序号为空
                {
                    search += "and usercode=@usercode ";
                    dynamicParams.Add("@usercode", usercode);
                }
                //班组获取人员列表
                sql = @"select usercode,username  from TUser where is_delete<>'1' "+search;
                dynamicParams.Add("@usercode", usercode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.data = data;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[生产开报工:发料/收料时获取外协下拉列表]
        public static ToMessage MesOrderStepSelectWX(string orderstepqrcode)
        {
            string sql = "";
            string ordercode = "";
            string stepcode = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                if (orderstepqrcode != "" && orderstepqrcode != null)
                {
                    string[] arra = orderstepqrcode.Split(';');
                    if (arra.Length == 1) //工单号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                    }
                    if (arra.Length == 2) //工单号+工序号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        stepcode = arra[1]; //获取指定字符串前面的字符
                    }
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "二维码信息为空!";
                    mes.data = null;
                    return mes;
                }
                //获取外协下拉框
                sql = @"select C.code,C.name from TFlw_Rteqp A
                        left join TCustomer C on A.eqp_code=C.code
                        where A.step_code=@stepcode and A.style='W'";
                dynamicParams.Add("@stepcode", stepcode);
                var data = DapperHelper.selectdata(sql,dynamicParams);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.data = data;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[生产报工:报工/外协收料 获取不良原因下拉列表]
        public static ToMessage MesOrderStepSelectCause(string orderstepqrcode)
        {
            string sql = "";
            string ordercode = "";
            string stepcode = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                if (orderstepqrcode != "" && orderstepqrcode != null)
                {
                    string[] arra = orderstepqrcode.Split(';');
                    if (arra.Length == 1) //工单号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                    }
                    if (arra.Length == 2) //工单号+工序号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        stepcode = arra[1]; //获取指定字符串前面的字符
                    }
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "二维码信息为空!";
                    mes.data = null;
                    return mes;
                }
                //获取工序关联缺陷
                sql = @"select B.code,B.name from TDefect_Step A
                        left join TDefect B on A.defect_code=B.code
                        where A.step_code=@stepcode";
                dynamicParams.Add("@stepcode", stepcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.Message = "查询成功!";
                mes.data = data;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[生产开报工,开工(开始)提交]
        public static ToMessage SavaMesOrderStepStart(string mesordercode, string partcode, string stepseq, string stepcode, string eqpcode, string taskqty, string startqty, string username)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                string date = DateTime.Now.ToString(); //获取系统时间
                list.Clear();
                //判断是否有开工记录
                sql = @"select *  from TK_Wrk_Record where wo_code=@wo_code and step_code=@step_code  and style='S'";
                dynamicParams.Add("@wo_code", mesordercode);
                dynamicParams.Add("@step_code", stepcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "当前工序任务已开工!";
                    mes.data = null;
                    return mes;
                }
 
                //写入开报工记录表
                sql = @"insert into  TK_Wrk_Record(wo_code,step_seq,step_code,eqp_code,materiel_code,open_person,open_time,task_qty,start_qty,style,lm_user,lm_date) 
                                values(@mesordercode,@stepseq,@stepcode,@eqpcode,@partcode,@username,@opentime,@taskqty,@startqty,@style,@lm_user,@lm_date)";
                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepseq = stepseq, stepcode = stepcode, eqpcode = eqpcode, partcode = partcode, username = username, opentime = date, taskqty = taskqty, startqty = startqty, style = "S", lm_user = username, lm_date = date } });
                //回写工单工序表状态为START: 开工
                sql = @"update TK_Wrk_Step set status='START'  where wo_code=@mesordercode and step_code=@stepcode";
                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode } });
 
 
                //回写工单表状态为: 开工:START 
                sql = @"update TK_Wrk_Man set status='START'  where wo_code=@mesordercode";
                list.Add(new { str = sql, parm = new { mesordercode = mesordercode } });
 
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    mes.code = "200";
                    mes.count = 0;
                    mes.Message = "开工成功!";
                    mes.data = null;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "开工失败!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[生产开报工,报工提交]
        public static ToMessage SavaMesOrderStepReport(string mesordercode, string partcode, string stepseq, string stepcode, string eqpcode, string usergroupcode, string reportuser, string taskqty, string startqty, string reportqty, string ngqty, string badcode, string username)
        {
            var sql = "";
            string[] arra = new string[] { };
            string[] arra1 = new string[] { };
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                string date = DateTime.Now.ToString(); //获取系统时间
                //截取报工人员
                arra = reportuser.Split(';');
                if (ngqty != "" || ngqty != "0")
                {
                    //截取不良原因
                    arra1 = badcode.Split(';');
                }
 
                list.Clear();
                //判断是否有报工记录(有:修改 无:新增)
                sql = @"select *  from TK_Wrk_Record where wo_code=@wo_code and step_code=@step_code and style='B'";
                dynamicParams.Add("@wo_code", mesordercode);
                dynamicParams.Add("@step_code", stepcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    //修改报工记录
                    sql = @"update TK_Wrk_Record set good_qty=good_qty+@reportqty,ng_qty=ng_qty+@ngqty,
                                lm_user=@username,lm_date=@CreateDate where wo_code=@mesordercode and step_code=@stepcode and style='B'";
                    list.Add(new { str = sql, parm = new { reportqty = decimal.Parse(reportqty), ngqty = decimal.Parse(ngqty), mesordercode = mesordercode, stepcode = stepcode, username = username, CreateDate = date } });
                    //写入子表
                    for (int i = 0; i < arra.Length; i++)
                    {
                        sql = @"insert into  TK_Wrk_RecordSub(m_id,eqp_code,report_person,report_date,report_qty,usergroup_code,ng_qty,style,lm_user,lm_date) 
                                values(@m_id,@eqp_code,@report_person,@report_date,@report_qty,@usergroup_code,@ng_qty,@style,@lm_user,@lm_date)";
                        list.Add(new { str = sql, parm = new { m_id = int.Parse(data.Rows[0]["ID"].ToString()), eqp_code = eqpcode, report_person = arra[i], report_date = date, report_qty = reportqty, usergroup_code = usergroupcode, ng_qty = ngqty, style = "B", lm_user = username, lm_date = date } });
 
                    }
                    if (badcode != "" &&  ngqty != "0")
                    {
                        //写入缺陷记录表
                        for (int i = 0; i < arra1.Length; i++)
                        {
                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,style,lm_user,lm_date) 
                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@style,@lm_user,@lm_date)";
                            list.Add(new { str = sql, parm = new { record_id = int.Parse(data.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], style = "B", lm_user = username, lm_date = date } });
 
                        }
                    }
                }
                else
                {
                    //获取主表最大ID
                    sql = @"select IDENT_CURRENT('TK_Wrk_Record')+1 as id";
                    var dt = DapperHelper.selecttable(sql);
                    //写入开报工记录表
                    sql = @"insert into  TK_Wrk_Record(wo_code,step_seq,step_code,eqp_code,materiel_code,task_qty,start_qty,good_qty,ng_qty,style,lm_user,lm_date) 
                                values(@mesordercode,@stepseq,@stepcode,@eqpcode,@partcode,@taskqty,@startqty,@reportqty,@ngqty,@style,@lm_user,@lm_date)";
                    list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepseq = stepseq, stepcode = stepcode, eqpcode = eqpcode, partcode = partcode, taskqty = taskqty, startqty = startqty, reportqty = reportqty, ngqty = ngqty, style = "B", lm_user = username, lm_date = date } });
 
                    //写入子表
                    for (int i = 0; i < arra.Length; i++)
                    {
                        sql = @"insert into  TK_Wrk_RecordSub(m_id,eqp_code,report_person,report_date,report_qty,usergroup_code,ng_qty,style,lm_user,lm_date) 
                                values(@m_id,@eqp_code,@report_person,@report_date,@report_qty,@usergroup_code,@ng_qty,@style,@lm_user,@lm_date)";
                        list.Add(new { str = sql, parm = new { m_id = int.Parse(dt.Rows[0]["ID"].ToString()), eqp_code = eqpcode, report_person = arra[i], report_date = date, report_qty = reportqty, usergroup_code = usergroupcode, ng_qty = ngqty, style = "B", lm_user = username, lm_date = date } });
 
                    }
                    if (badcode != "" && ngqty != "0")
                    {
                        //写入缺陷记录表
                        for (int i = 0; i < arra1.Length; i++)
                        {
                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,style,lm_user,lm_date) 
                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@style,@lm_user,@lm_date)";
                            list.Add(new { str = sql, parm = new { record_id = int.Parse(dt.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], style = "B", lm_user = username, lm_date = date } });
 
                        }
                    }
                }
 
 
                //回写工单工序表合格数量、不良数量
                sql = @"update TK_Wrk_Step set good_qty=good_qty+@reportqty,ng_qty=ng_qty+@ngqty  where wo_code=@mesordercode and step_code=@stepcode";
                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode,reportqty=reportqty,ngqty=ngqty } });
 
                //回写工单表合格数量、不良数量
                sql = @"update TK_Wrk_Man set good_qty=good_qty+@reportqty,ng_qty=ng_qty+@ngqty  where wo_code=@mesordercode";
                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, reportqty = reportqty, ngqty = ngqty } });
 
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    mes.code = "200";
                    mes.count = 0;
                    mes.Message = "操作成功!";
                    mes.data = null;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "操作失败!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[生产开报工,发料提交]
        public static ToMessage SavaMesOrderStepOut(string mesordercode, string partcode, string stepseq, string stepcode, string wxcode, string outuser, string taskqty, string fqty, string username)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                string date = DateTime.Now.ToString(); //获取系统时间
                list.Clear();
                //判断是否有发料记录(有(同工单+工序+外协供方修改) 无:新增)
                sql = @"select *  from TK_Wrk_OutRecord where wo_code=@wo_code and step_code=@step_code and wx_code=@wx_code and style='F'";
                dynamicParams.Add("@wo_code", mesordercode);
                dynamicParams.Add("@step_code", stepcode);
                dynamicParams.Add("@wx_code", wxcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    //修改发料记录
                    sql = @"update TK_Wrk_OutRecord set fqty=fqty+@fqty,lm_user=@username,lm_date=@CreateDate
                             where wo_code=@mesordercode and step_code=@stepcode and wx_code=@wx_code and style='F'";
                    list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode, wx_code = wxcode, fqty = decimal.Parse(fqty), username = username, CreateDate = date } });
                    //写入子表
                    sql = @"insert into  TK_Wrk_OutRecordSub(m_id,wx_code,out_person,out_time,fqty,style,lm_user,lm_date) 
                                values(@m_id,@wx_code,@out_person,@out_time,@fqty,@style,@lm_user,@lm_date)";
                    list.Add(new { str = sql, parm = new { m_id = int.Parse(data.Rows[0]["ID"].ToString()), wx_code = wxcode, out_person = outuser, out_time = date, fqty = fqty, style = 'F', lm_user = username, lm_date = date } });
                }
                else
                {
                    //获取主表最大ID
                    sql = @"select IDENT_CURRENT('TK_Wrk_OutRecord')+1 as id";
                    var dt = DapperHelper.selecttable(sql);
                    //写入外协记录主表
                    sql = @"insert into  TK_Wrk_OutRecord(wo_code,step_seq,step_code,wx_code,materiel_code,style,fqty,lm_user,lm_date) 
                                values(@mesordercode,@stepseq,@stepcode,@wx_code,@partcode,@style,@fqty,@lm_user,@lm_date)";
                    list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepseq = stepseq, stepcode = stepcode, wx_code = wxcode, partcode = partcode, style = 'F', fqty = fqty, lm_user = username, lm_date = date } });
 
                    //写入子表
                    sql = @"insert into  TK_Wrk_OutRecordSub(m_id,wx_code,out_person,out_time,fqty,style,lm_user,lm_date) 
                                values(@m_id,@wx_code,@out_person,@out_time,@fqty,@style,@lm_user,@lm_date)";
                    list.Add(new { str = sql, parm = new { m_id = int.Parse(dt.Rows[0]["ID"].ToString()), wx_code = wxcode, out_person = outuser, out_time = date, fqty = fqty, style = 'F', lm_user = username, lm_date = date } });
                }
                //回写工单工序表状态为START: 开工
                sql = @"update TK_Wrk_Step set status='START'  where wo_code=@mesordercode and step_code=@stepcode";
                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode } });
 
 
                //回写工单表状态为: 开工:START 
                sql = @"update TK_Wrk_Man set status='START'  where wo_code=@mesordercode";
                list.Add(new { str = sql, parm = new { mesordercode = mesordercode } });
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    mes.code = "200";
                    mes.count = 0;
                    mes.Message = "发料成功!";
                    mes.data = null;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "发料失败!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[生产开报工, 收料提交]
        public static ToMessage SavaMesOrderStepIn(string mesordercode, string partcode, string stepseq, string stepcode, string wxcode, string inuser, string taskqty, string sqty, string ngqty, string badcode, string username)
        {
            var sql = "";
            string[] arra1 = new string[] { };
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                string date = DateTime.Now.ToString(); //获取系统时间
                //截取不良原因
                if (ngqty != "" || ngqty != "0")
                {
                    //截取不良原因
                    arra1 = badcode.Split(';');
                }
 
                list.Clear();
                //判断是否有收料记录(有:(同工单+工序+外协供方修改) 无:新增)
                sql = @"select *  from TK_Wrk_OutRecord where wo_code=@wo_code and step_code=@step_code and wx_code=@wx_code and style='S'";
                dynamicParams.Add("@wo_code", mesordercode);
                dynamicParams.Add("@step_code", stepcode);
                dynamicParams.Add("@wx_code", wxcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    //修改外协记录主表
                    sql = @"update TK_Wrk_OutRecord set sqty=sqty+@sqty,ng_qty=ng_qty+@ngqty,lm_user=@username,lm_date=@CreateDate
                             where wo_code=@mesordercode and step_code=@stepcode and wx_code=@wx_code and style='S'";
                    list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode, wx_code = wxcode, sqty = decimal.Parse(sqty), ngqty = decimal.Parse(ngqty), username = username, CreateDate = date } });
                    //写入外协记录子表
                    sql = @"insert into  TK_Wrk_OutRecordSub(m_id,wx_code,in_person,in_time,sqty,ng_qty,style,lm_user,lm_date) 
                                values(@m_id,@wx_code,@in_person,@in_time,@sqty,@ngqty,@style,@lm_user,@lm_date)";
                    list.Add(new { str = sql, parm = new { m_id = int.Parse(data.Rows[0]["ID"].ToString()), wx_code = wxcode, in_person = inuser, in_time = date, sqty = sqty, ngqty = ngqty, style = 'S', lm_user = username, lm_date = date } });
 
                    if (badcode != "" && ngqty != "0")
                    {
                        //写入缺陷记录表
                        for (int i = 0; i < arra1.Length; i++)
                        {
                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,style,lm_user,lm_date) 
                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@style,@lm_user,@lm_date)";
                            list.Add(new { str = sql, parm = new { record_id = int.Parse(data.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], style = "S", lm_user = username, lm_date = date } });
 
                        }
                    }
                }
                else
                {
                    //获取主表最大ID
                    sql = @"select IDENT_CURRENT('TK_Wrk_OutRecord')+1 as id";
                    var dt = DapperHelper.selecttable(sql);
                    //写入外协记录主表
                    sql = @"insert into  TK_Wrk_OutRecord(wo_code,step_seq,step_code,wx_code,materiel_code,style,sqty,ng_qty,lm_user,lm_date) 
                                values(@mesordercode,@stepseq,@stepcode,@wx_code,@partcode,@style,@sqty,@ngqty,@lm_user,@lm_date)";
                    list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepseq = stepseq, stepcode = stepcode, wx_code = wxcode, partcode = partcode, style = 'S', sqty = sqty, ngqty = ngqty, lm_user = username, lm_date = date } });
 
                    //写入外协记录子表
                    sql = @"insert into  TK_Wrk_OutRecordSub(m_id,wx_code,in_person,in_time,sqty,ng_qty,style,lm_user,lm_date) 
                                values(@m_id,@wxcode,@in_person,@in_time,@sqty,@ng_qty,@style,@lm_user,@lm_date)";
                    list.Add(new { str = sql, parm = new { m_id = int.Parse(dt.Rows[0]["ID"].ToString()), wxcode = wxcode, in_person = inuser, in_time = date, sqty = sqty, ng_qty = ngqty, style = "S", lm_user = username, lm_date = date } });
 
                    if (badcode != "" && ngqty != "0")
                    {
                        //写入缺陷记录表
                        for (int i = 0; i < arra1.Length; i++)
                        {
                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,style,lm_user,lm_date) 
                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@style,@lm_user,@lm_date)";
                            list.Add(new { str = sql, parm = new { record_id = int.Parse(dt.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], style = "S", lm_user = username, lm_date = date } });
 
                        }
                    }
                }
                //回写工单工序表合格数量、不良数量
                sql = @"update TK_Wrk_Step set good_qty=good_qty+@sqty,ng_qty=ng_qty+@ngqty  where wo_code=@mesordercode and step_code=@stepcode";
                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode, sqty = sqty, ngqty = ngqty } });
 
                //回写工单表合格数量、不良数量
                sql = @"update TK_Wrk_Man set good_qty=good_qty+@sqty,ng_qty=ng_qty+@ngqty  where wo_code=@mesordercode";
                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, sqty = sqty, ngqty = ngqty } });
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    mes.code = "200";
                    mes.count = 0;
                    mes.Message = "收成功!";
                    mes.data = null;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "收料失败!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion'
 
    }
}