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
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
using Dapper;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using VueWebCoreApi.Models;
using VueWebCoreApi.Models.ErpOrder;
using VueWebCoreApi.Models.InventoryModel;
using VueWebCoreApi.Models.ReportVerify;
using VueWebCoreApi.Models.UpdateReport;
using VueWebCoreApi.Models.WorkData;
using VueWebCoreApi.Tools;
 
namespace VueWebCoreApi.DLL.DAL
{
    public class WorkOrderDAL
    {
        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 wkshopcode, string erpordercode, string saleordercode, string partcode, string partname, string partspec, int startNum, string datatype, string paydatestartdate, string paydateenddate, string creatuser, 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 (wkshopcode != "" && wkshopcode != null)
                {
                    string[] wkshoplist = Array.ConvertAll<string, string>(wkshopcode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), s => s.ToString()); //string分割转string[] 
                    search += "and A.wkshp_code in @wkshoplist ";
                    dynamicParams.Add("@wkshoplist", wkshoplist);
                }
                if (erpordercode != "" && erpordercode != null)
                {
                    search += "and A.wo like '%'+@erpordercode+'%' ";
                    dynamicParams.Add("@erpordercode", erpordercode);
                }
                if (saleordercode != "" && saleordercode != null)
                {
                    search += "and A.saleOrderCode like '%'+@saleordercode+'%' ";
                    dynamicParams.Add("@saleordercode", saleordercode);
                }
                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)
                {
                    switch (datatype)
                    {
                        case "PS":
                            search += "and A.planstartdate between @paydatestartdate and @paydateenddate ";
                            dynamicParams.Add("@paydatestartdate", paydatestartdate + " 00:00:00");
                            dynamicParams.Add("@paydateenddate", paydateenddate + " 23:59:59");
                            break;
                        case "PE":
                            search += "and A.planenddate between @paydatestartdate and @paydateenddate ";
                            dynamicParams.Add("@paydatestartdate", paydatestartdate + " 00:00:00");
                            dynamicParams.Add("@paydateenddate", paydateenddate + " 23:59:59");
                            break;
                        case "ED":
                            search += "and A.saleOrderDeliveryDate between @paydatestartdate and @paydateenddate ";
                            dynamicParams.Add("@paydatestartdate", paydatestartdate + " 00:00:00");
                            dynamicParams.Add("@paydateenddate", paydateenddate + " 23:59:59");
                            break;
                        default:
                            break;
                    }
                }
                if (creatuser != "" && creatuser != null)
                {
                    search += "and U.username like '%'+@creatuser+'%' ";
                    dynamicParams.Add("@creatuser", creatuser);
                }
 
                if (search == "")
                {
                    search = "and 1=1 ";
                }
                // --------------查询指定数据--------------
                var total = 0; //总条数
                var sql = @"select A.id, A.status,A.wo,A.materiel_code as partcode,B.partname,B.partspec,A.idTopInventory,A.TopInventoryCode,A.TopInventoryName,A.qty,A.relse_qty,A.dept_id,A.dept_code,A.wkshp_code,C.torg_name as wkshp_name,
                            A.stck_code,D.name as stck_name,A.saleOrderCode,A.saleOrderDeliveryDate,A.planstartdate,A.planenddate,U.username as createuser,A.createdate,A.sbid,A.clerkuser,
                            B.priuserdefnvc1,B.priuserdefnvc2,B.priuserdefnvc3,B.priuserdefnvc4,B.priuserdefnvc5,B.priuserdefnvc6
                            from TKimp_Ewo A
                            left join TMateriel_Info B on A.materiel_code=B.partcode
                            left join TOrganization C on A.wkshp_code=C.torg_code
                            left join TSecStck D on A.stck_code=D.code 
                            left join TUser U on A.createuser=U.usercode 
                            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 erporderid, string sbid, string erpordercode, string saleordercode, string partcode,string deptcode, string wkshopcode, string warehousecode, string erpqty, string markqty, string ordernum, string relse_qty, string idTopInventory, string TopInventoryCode, string TopInventoryName, string saleOrderDeliveryDate, string paystartdate, string payenddate, string clerkuser, User us)
        {
            var sql = "";
            string orderstatus = "", isstep = ""; //工单状态、是否绑定工艺
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            DataTable data0 = new DataTable();
            try
            {
                list.Clear();
                //获取token中流程配置信息
                JObject jsonObj = JObject.Parse(us.mesSetting);
                var isroute = (string)jsonObj["route"].ToString().ToLower();// 获取route键的值
                //判断当前工艺流程
                if (isroute == "true")
                {
                    //获取当前物料的工艺路线工艺信息
                    sql = @"select M.default_route,T.seq,T.step_code,T.first_choke,T.last_choke,S.unprice   
                            from TMateriel_Info M
                            inner join TFlw_Rtdt T on M.default_route=T.rout_code
                            left join TPrteEqp_Stad S on M.partcode=S.materiel_code and M.default_route=S.route_code and T.step_code=S.step_code
                            where partcode=@partcode";
                    dynamicParams.Add("@partcode", partcode);
                    data0 = DapperHelper.selectdata(sql, dynamicParams);
                    if (data0.Rows.Count > 0)
                    {
                        orderstatus = "ALLO";
                        isstep = "Y";
                    }
                    else
                    {
                        orderstatus = "NEW";
                        isstep = "N";
                    }
                }
                else
                {
                    //获取当前物料的工序工艺信息
                    sql = @"select M.default_route,T.step_seq as seq,T.step_code,T.isbott as first_choke,T.isend as last_choke,S.unprice   
                            from TMateriel_Info M
                            inner join TMateriel_Step T on M.partcode=T.materiel_code
                            left join TPrteEqp_Stad S on M.partcode=S.materiel_code  and T.step_code=S.step_code
                            where partcode=@partcode";
                    dynamicParams.Add("@partcode", partcode);
                    data0 = DapperHelper.selectdata(sql, dynamicParams);
                    if (data0.Rows.Count > 0)
                    {
                        orderstatus = "ALLO";
                        isstep = "Y";
                    }
                    else
                    {
                        orderstatus = "NEW";
                        isstep = "N";
                    }
                }
 
                //获取拆单数量:向下取整
                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(cast(substring(wo_code,charindex('_',wo_code)+1,len(wo_code)-charindex('_',wo_code)) as numeric)),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,wotype,status,dept_code,wkshp_code,plan_qty,stck_code,sbid,materiel_code,route_code,sourceid,m_po,lm_user,lm_date,saleOrderCode,saleOrderDeliveryDate,plan_startdate,plan_enddate,data_sources,isstep,clerkuser,idTopInventory,TopInventoryCode,TopInventoryName) 
                                values(@wo_code,@wotype,@status,@dept_code,@wkshp_code,@plan_qty,@stck_code,@sbid,@materiel_code,@route_code,@sourceid,@m_po,@username,@CreateDate,@saleOrderCode,@saleOrderDeliveryDate,@plan_startdate,@plan_enddate,@data_sources,@isstep,@clerkuser,@idTopInventory,@TopInventoryCode,@TopInventoryName)";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo_code = wo,
                                wotype = "PO",
                                status = orderstatus,  //"NEW"
                                dept_code= deptcode,
                                wkshp_code = wkshopcode,
                                plan_qty = cdqty + (decimal.Parse(markqty) - sumqty),  //末单下单数量=切分数量+(下单数量-累计切分下单数量)
                                stck_code = warehousecode,
                                sbid = sbid,
                                materiel_code = partcode,
                                route_code = data0.Rows.Count <= 0 ? "" : data0.Rows[0]["default_route"].ToString(),
                                sourceid = erporderid,
                                m_po = erpordercode,
                                username = us.usercode,
                                CreateDate = DateTime.Now.ToString(),
                                saleOrderCode = saleordercode,
                                saleOrderDeliveryDate = Convert.ToDateTime(saleOrderDeliveryDate),
                                plan_startdate = Convert.ToDateTime(paystartdate),
                                plan_enddate = Convert.ToDateTime(payenddate),
                                data_sources = "ERP",
                                isstep = isstep,  //是否关联工序 "N"
                                clerkuser = clerkuser, //销售订单业务员
                                idTopInventory = idTopInventory,
                                TopInventoryCode = TopInventoryCode,
                                TopInventoryName = TopInventoryName
                            }
                        });
                        //判断是否写入工单工序表(当前工单物料是否指定了工艺)
                        if (orderstatus == "ALLO" && isstep == "Y")
                        {
                            //写入工单工序表
                            for (int j = 0; j < data0.Rows.Count; j++)
                            {
                                sql = @"insert into TK_Wrk_Step(wo_code,seq,step_code,route_code,stepprice,plan_quantity,plan_qty,ratio,status,isbott,isend,lm_user,lm_date)
                                values(@wo_code,@seq,@step_code,@route_code,@stepprice,@plan_quantity,@plan_qty,@ratio,@status,@isbott,@isend,@lm_user,@lm_date)";
                                list.Add(new
                                {
                                    str = sql,
                                    parm = new
                                    {
                                        wo_code = wo,
                                        seq = data0.Rows[j]["seq"].ToString(),
                                        step_code = data0.Rows[j]["step_code"].ToString(),
                                        route_code = data0.Rows[j]["default_route"].ToString(),
                                        stepprice = decimal.Parse(data0.Rows[j]["unprice"].ToString() == "" || data0.Rows[j]["unprice"].ToString() == null ? "0" : data0.Rows[j]["unprice"].ToString()),
                                        plan_quantity = cdqty + (decimal.Parse(markqty) - sumqty),  //末单下单数量=切分数量+(下单数量-累计切分下单数量),
                                        plan_qty = cdqty + (decimal.Parse(markqty) - sumqty),  //末单下单数量=切分数量+(下单数量-累计切分下单数量),
                                        ratio = 0,
                                        status = orderstatus,
                                        isbott = data0.Rows[j]["first_choke"].ToString(),
                                        isend = data0.Rows[j]["last_choke"].ToString(),
                                        lm_user = us.usercode,
                                        lm_date = DateTime.Now.ToString()
                                    }
                                });
                            }
                        }
                        sumqty = sumqty + (decimal.Parse(markqty) - sumqty);
                    }
                    else
                    {
 
                        sql = @"insert into TK_Wrk_Man(wo_code,wotype,status,dept_code,wkshp_code,plan_qty,stck_code,sbid,materiel_code,route_code,sourceid,m_po,lm_user,lm_date,saleOrderCode,saleOrderDeliveryDate,plan_startdate,plan_enddate,data_sources,isstep,clerkuser,idTopInventory,TopInventoryCode,TopInventoryName) 
                                values(@wo_code,@wotype,@status,@dept_code,@wkshp_code,@plan_qty,@stck_code,@sbid,@materiel_code,@route_code,@sourceid,@m_po,@username,@CreateDate,@saleOrderCode,@saleOrderDeliveryDate,@plan_startdate,@plan_enddate,@data_sources,@isstep,@clerkuser,@idTopInventory,@TopInventoryCode,@TopInventoryName)";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo_code = wo,
                                wotype = "PO",
                                status = orderstatus, //"NEW"
                                dept_code=deptcode,
                                wkshp_code = wkshopcode,
                                plan_qty = cdqty,
                                stck_code = warehousecode,
                                sbid = sbid,
                                materiel_code = partcode,
                                route_code = data0.Rows.Count <= 0 ? "" : data0.Rows[0]["default_route"].ToString(),
                                sourceid = erporderid,
                                m_po = erpordercode,
                                username = us.usercode,
                                CreateDate = DateTime.Now.ToString(),
                                saleOrderCode = saleordercode,
                                saleOrderDeliveryDate = Convert.ToDateTime(saleOrderDeliveryDate),
                                plan_startdate = Convert.ToDateTime(paystartdate),
                                plan_enddate = Convert.ToDateTime(payenddate),
                                data_sources = "ERP",
                                isstep = isstep,//是否关联工序 "N"
                                clerkuser = clerkuser,
                                idTopInventory = idTopInventory,
                                TopInventoryCode = TopInventoryCode,
                                TopInventoryName = TopInventoryName
                            }
                        });
                        //判断是否写入工单工序表(当前工单物料是否指定了工艺)
                        if (orderstatus == "ALLO" && isstep == "Y")
                        {
                            //写入工单工序表
                            for (int j = 0; j < data0.Rows.Count; j++)
                            {
                                sql = @"insert into TK_Wrk_Step(wo_code,seq,step_code,route_code,stepprice,plan_quantity,plan_qty,ratio,status,isbott,isend,lm_user,lm_date)
                                values(@wo_code,@seq,@step_code,@route_code,@stepprice,@plan_quantity,@plan_qty,@ratio,@status,@isbott,@isend,@lm_user,@lm_date)";
                                list.Add(new
                                {
                                    str = sql,
                                    parm = new
                                    {
                                        wo_code = wo,
                                        seq = data0.Rows[j]["seq"].ToString(),
                                        step_code = data0.Rows[j]["step_code"].ToString(),
                                        route_code = data0.Rows[j]["default_route"].ToString(),
                                        stepprice = decimal.Parse(data0.Rows[j]["unprice"].ToString() == "" || data0.Rows[j]["unprice"].ToString() == null ? "0" : data0.Rows[j]["unprice"].ToString()),
                                        plan_quantity = cdqty + (decimal.Parse(markqty) - sumqty),  //末单下单数量=切分数量+(下单数量-累计切分下单数量),
                                        plan_qty = cdqty + (decimal.Parse(markqty) - sumqty),  //末单下单数量=切分数量+(下单数量-累计切分下单数量),
                                        ratio = 0,
                                        status = orderstatus,
                                        isbott = data0.Rows[j]["first_choke"].ToString(),
                                        isend = data0.Rows[j]["last_choke"].ToString(),
                                        lm_user = us.usercode,
                                        lm_date = DateTime.Now.ToString()
                                    }
                                });
                            }
                        }
                    }
                }
                if (decimal.Parse(erpqty) == decimal.Parse(markqty) + decimal.Parse(relse_qty))   //如果ERP订单=下单数量+已下单数量,则更新ERP订单表状态为CREATED:已创建
                {
                    sql = @"update  TKimp_Ewo set status='CREATED',saleOrderDeliveryDate=@saleOrderDeliveryDate,relse_qty=relse_qty+@sumqty where wo=@wo and id=@erporderid";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wo = erpordercode,
                            erporderid = erporderid,
                            sumqty = sumqty,
                            saleOrderDeliveryDate = Convert.ToDateTime(saleOrderDeliveryDate)
                        }
                    });
                }
                else   //更新ERP订单表状态为CREATING:创建中
                {
                    sql = @"update  TKimp_Ewo set status='CREATING',saleOrderDeliveryDate=@saleOrderDeliveryDate,relse_qty=relse_qty+@sumqty where wo=@wo and id=@erporderid";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wo = erpordercode,
                            erporderid = erporderid,
                            sumqty = sumqty,
                            saleOrderDeliveryDate = Convert.ToDateTime(saleOrderDeliveryDate)
                        }
                    });
                }
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "下达", "下达了工单:" + wo, us.usertype);
                    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[订单批量下达]
        public static ToMessage MarkBatchSaveErpOrder(List<ErpOrderBatch> obj, User us)
        {
            var sql = "";
            string orderstatus = "", isstep = ""; //工单状态、是否绑定工艺
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            DataTable data0 = new DataTable();
            try
            {
                list.Clear();
                string result = "";
                //var groupedModels = obj.GroupBy(m => m.erpordercode);
                var groupedModels = obj.GroupBy(s => s.erpordercode).Select(g => new { erpordercode = g.Key }).ToList();
 
                //获取token中流程配置信息
                JObject jsonObj = JObject.Parse(us.mesSetting);
                var isroute = (string)jsonObj["route"].ToString().ToLower();// 获取route键的值
 
                foreach (var group in groupedModels)
                {
                    string erpordercode = group.erpordercode;
                    int count = 1;
                    foreach (var model in obj.Where(s => s.erpordercode == erpordercode).ToList())
                    {
                        //判断当前工艺流程
                        if (isroute == "true")
                        {
                            //获取当前物料的工艺路线工艺信息
                            sql = @"select M.default_route,T.seq,T.step_code,T.first_choke,T.last_choke,S.unprice   
                            from TMateriel_Info M
                            inner join TFlw_Rtdt T on M.default_route=T.rout_code
                            left join TPrteEqp_Stad S on M.partcode=S.materiel_code and M.default_route=S.route_code and T.step_code=S.step_code
                            where partcode=@partcode";
                            dynamicParams.Add("@partcode", model.partcode);
                            data0 = DapperHelper.selectdata(sql, dynamicParams);
                            if (data0.Rows.Count > 0)
                            {
                                orderstatus = "ALLO";
                                isstep = "Y";
                            }
                            else
                            {
                                orderstatus = "NEW";
                                isstep = "N";
                            }
                        }
                        else
                        {
                            //获取当前物料的工序工艺信息
                            sql = @"select M.default_route,T.step_seq as seq,T.step_code,T.isbott as first_choke,T.isend as last_choke,S.unprice   
                            from TMateriel_Info M
                            inner join TMateriel_Step T on M.partcode=T.materiel_code
                            left join TPrteEqp_Stad S on M.partcode=S.materiel_code  and T.step_code=S.step_code
                            where partcode=@partcode";
                            dynamicParams.Add("@partcode", model.partcode);
                            data0 = DapperHelper.selectdata(sql, dynamicParams);
                            if (data0.Rows.Count > 0)
                            {
                                orderstatus = "ALLO";
                                isstep = "Y";
                            }
                            else
                            {
                                orderstatus = "NEW";
                                isstep = "N";
                            }
                        }
 
                        //获取当前最大工单号
                        sql = @"select isnull(max(cast(substring(wo_code,charindex('_',wo_code)+1,len(wo_code)-charindex('_',wo_code)) as numeric)),0) as worknumb   
                                from TK_Wrk_Man where  m_po=@erpordercode";
                        dynamicParams.Add("@erpordercode", model.erpordercode);
                        var data = DapperHelper.selectdata(sql, dynamicParams);
                        int num = Convert.ToInt32(data.Rows[0]["WORKNUMB"].ToString());
                        string wo = model.erpordercode + "_" + (num + count);
                        result += wo.ToString() + ",";
                        //写入工单表
                        sql = @"insert into TK_Wrk_Man(wo_code,wotype,status,dept_code,wkshp_code,plan_qty,stck_code,sbid,materiel_code,route_code,sourceid,m_po,lm_user,lm_date,saleOrderCode,saleOrderDeliveryDate,plan_startdate,plan_enddate,data_sources,isstep,clerkuser,idTopInventory,TopInventoryCode,TopInventoryName) 
                                values(@wo_code,@wotype,@status,@dept_code,@wkshp_code,@plan_qty,@stck_code,@sbid,@materiel_code,@route_code,@sourceid,@m_po,@username,@CreateDate,@saleOrderCode,@saleOrderDeliveryDate,@plan_startdate,@plan_enddate,@data_sources,@isstep,@clerkuser,@idTopInventory,@TopInventoryCode,@TopInventoryName)";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo_code = wo,
                                wotype = "PO",
                                status = orderstatus, //"NEW"
                                dept_code=model.deptcode,
                                wkshp_code = model.wkshopcode,
                                plan_qty = decimal.Parse(model.erpqty),  //订单数量
                                stck_code = model.warehousecode,
                                sbid = model.sbid,
                                materiel_code = model.partcode,
                                route_code=data0.Rows.Count<=0?"": data0.Rows[0]["default_route"].ToString(),
                                sourceid = model.erporderid,
                                m_po = model.erpordercode,
                                username = us.usercode,
                                CreateDate = DateTime.Now.ToString(),
                                saleOrderCode = model.saleordercode,
                                saleOrderDeliveryDate = Convert.ToDateTime(model.saleOrderDeliveryDate),
                                plan_startdate = Convert.ToDateTime(model.paystartdate),
                                plan_enddate = Convert.ToDateTime(model.payenddate),
                                data_sources = "ERP",
                                isstep = isstep,  //是否关联工序 "N"
                                clerkuser = model.clerkuser, //销售订单业务员
                                idTopInventory =model.idTopInventory,
                                TopInventoryCode = model.TopInventoryCode,
                                TopInventoryName = model.TopInventoryName
                            }
                        });
                        //判断是否写入工单工序表(当前工单物料是否指定了工艺)
                        if (orderstatus == "ALLO" && isstep == "Y")
                        {
                            //写入工单工序表
                            for (int i = 0; i < data0.Rows.Count; i++)
                            {
                                sql = @"insert into TK_Wrk_Step(wo_code,seq,step_code,route_code,stepprice,plan_quantity,plan_qty,ratio,status,isbott,isend,lm_user,lm_date)
                                values(@wo_code,@seq,@step_code,@route_code,@stepprice,@plan_quantity,@plan_qty,@ratio,@status,@isbott,@isend,@lm_user,@lm_date)";
                                list.Add(new
                                {
                                    str = sql,
                                    parm = new
                                    {
                                        wo_code = wo,
                                        seq = data0.Rows[i]["seq"].ToString(),
                                        step_code = data0.Rows[i]["step_code"].ToString(),
                                        route_code = data0.Rows[i]["default_route"].ToString(),
                                        stepprice = decimal.Parse(data0.Rows[i]["unprice"].ToString()==""|| data0.Rows[i]["unprice"].ToString() ==null? "0":data0.Rows[i]["unprice"].ToString()),
                                        plan_quantity = decimal.Parse(model.erpqty),  //订单数量
                                        plan_qty = decimal.Parse(model.erpqty),  //订单数量
                                        ratio = 0,
                                        status = orderstatus,
                                        isbott = data0.Rows[i]["first_choke"].ToString(),
                                        isend = data0.Rows[i]["last_choke"].ToString(),
                                        lm_user = us.usercode,
                                        lm_date = DateTime.Now.ToString()
                                    }
                                });
                            }
                        }
                        //更新订单状态
                        sql = @"update  TKimp_Ewo set status='CREATED',saleOrderDeliveryDate=@saleOrderDeliveryDate,relse_qty=relse_qty+@sumqty where wo=@wo and id=@erporderid";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo = model.erpordercode,
                                erporderid = model.erporderid,
                                sumqty = decimal.Parse(model.markqty),
                                saleOrderDeliveryDate = Convert.ToDateTime(model.saleOrderDeliveryDate)
                            }
                        });
                        count++;
                    }
                }
 
 
 
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "批量下达", "批量下达了工单:" + result.TrimEnd(','), us.usertype);
                    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 erporderid, string erpordercode, User us)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                sql = @"select *  from TK_Wrk_Man where m_po=@erpordercode and sourceid=@sourceid";
                dynamicParams.Add("@erpordercode", erpordercode);
                dynamicParams.Add("@sourceid", erporderid);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    // 使用LINQ和lambda表达式来转换wo_code字段的值为逗号隔开的字符串数组  
                    string[] result = data.AsEnumerable().Select(row => "" + row.Field<string>("wo_code") + "").ToArray();
                    //关闭工序任务
                    sql = @"update  TK_Wrk_Step set status='CLOSED',closebeforestatus=status where wo_code in @wocode";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wocode = result
                        }
                    });
                    //关闭工单
                    sql = @"update  TK_Wrk_Man set status='CLOSED',closebeforestatus=status where m_po=@erpordercode and sourceid=@sourceid";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            erpordercode = erpordercode,
                            sourceid = erporderid
                        }
                    });
                }
                //关闭订单
                sql = @"update  TKimp_Ewo set status='CLOSED',closebeforestatus=status where wo=@wo and id=@erporderid";
                list.Add(new
                {
                    str = sql,
                    parm = new
                    {
                        wo = erpordercode,
                        erporderid = erporderid
                    }
                });
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "关闭", "关闭了订单:" + erpordercode, us.usertype);
                    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[ERP订单反关闭]
        public static ToMessage ReverseClosedErpOrder(string erporderid, string erpordercode, User us)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
 
                sql = @"select *  from TK_Wrk_Man where m_po=@erpordercode and sourceid=@sourceid";
                dynamicParams.Add("@erpordercode", erpordercode);
                dynamicParams.Add("@sourceid", erporderid);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    // 使用LINQ和lambda表达式来转换wo_code字段的值为逗号隔开的字符串数组  
                    string[] result = data.AsEnumerable().Select(row => "" + row.Field<string>("wo_code") + "").ToArray();
                    //关闭工序任务
                    sql = @"update  TK_Wrk_Step set status=closebeforestatus where wo_code in @ordercode";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            ordercode = result
                        }
                    });
                    //关闭工单
                    sql = @"update  TK_Wrk_Man set status=closebeforestatus where m_po=@erpordercode and sourceid=@sourceid";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            erpordercode = erpordercode,
                            sourceid = erporderid
                        }
                    });
                }
                //反关闭订单
                sql = @"update  TKimp_Ewo set status=closebeforestatus where wo=@wo and id=@erporderid";
                list.Add(new
                {
                    str = sql,
                    parm = new
                    {
                        wo = erpordercode,
                        erporderid = erporderid
                    }
                });
 
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "反关闭", "反关闭了订单:" + erpordercode, us.usertype);
                    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[ERP订单删除]
        public static ToMessage DeleteErpOrder(string erporderid, string erpordercode, User us)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                sql = @"select *  from TK_Wrk_Man where m_po=@erpordercode and sourceid=@erporderid and status<>'NEW'";
                dynamicParams.Add("@erpordercode", erpordercode);
                dynamicParams.Add("@erporderid", erporderid);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.message = "当前订单下有工单已派发或已开工或已完工(关闭),不允许删除!";
                    mes.data = null;
                    return mes;
                }
                else
                {
                    //删除工单
                    sql = @"delete  TK_Wrk_Man  where m_po=@wo and sourceid=@erporderid";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wo = erpordercode,
                            erporderid = erporderid
                        }
                    });
                    //删除订单
                    sql = @"delete  TKimp_Ewo  where wo=@wo and id=@erporderid";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wo = erpordercode,
                            erporderid = erporderid
                        }
                    });
                }
                bool aa = DapperHelper.DoTransaction(list);
                LogHelper.WriteLogData(aa.ToString());
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "删除", "删除了订单:" + erpordercode, us.usertype);
                    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 MesOrderSearch(string mesorderstus, string wkshopcode, string mesordercode, string sourceorder, string saleordercode, string ordertype, string partcode, string partname, string partspec, int startNum, string creatuser, string datatype, string paydatestartdate, string paydateenddate, 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 (wkshopcode != "" && wkshopcode != null)
                {
                    string[] wkshoplist = Array.ConvertAll<string, string>(wkshopcode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), s => s.ToString()); //string分割转string[] 
                    search += "and A.wkshp_code in @wkshoplist ";
                    dynamicParams.Add("@wkshoplist", wkshoplist);
                }
                if (mesordercode != "" && mesordercode != null)
                {
                    search += "and A.wo_code like '%'+@mesordercode+'%' ";
                    dynamicParams.Add("@mesordercode", mesordercode);
                }
                if (sourceorder != "" && sourceorder != null)
                {
                    search += "and A.m_po like '%'+@sourceorder+'%' ";
                    dynamicParams.Add("@sourceorder", sourceorder);
                }
                if (saleordercode != "" && saleordercode != null)
                {
                    search += "and W.saleOrderCode like '%'+@saleordercode+'%' ";
                    dynamicParams.Add("@saleordercode", saleordercode);
                }
                if (ordertype != "" && ordertype != null)
                {
                    search += "and A.wotype like '%'+@ordertype+'%' ";
                    dynamicParams.Add("@ordertype", ordertype);
                }
                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 (creatuser != "" && creatuser != null)
                {
                    search += "and U.username like '%'+@creatuser+'%' ";
                    dynamicParams.Add("@creatuser", creatuser);
                }
                if (paydatestartdate != "" && paydatestartdate != null)
                {
                    switch (datatype)
                    {
                        case "PS":
                            search += "and A.plan_startdate between @paydatestartdate and @paydateenddate ";
                            dynamicParams.Add("@paydatestartdate", paydatestartdate + " 00:00:00");
                            dynamicParams.Add("@paydateenddate", paydateenddate + " 23:59:59");
                            break;
                        case "PE":
                            search += "and A.plan_enddate between @paydatestartdate and @paydateenddate ";
                            dynamicParams.Add("@paydatestartdate", paydatestartdate + " 00:00:00");
                            dynamicParams.Add("@paydateenddate", paydateenddate + " 23:59:59");
                            break;
                        case "ED":
                            search += "and A.saleOrderDeliveryDate between @paydatestartdate and @paydateenddate ";
                            dynamicParams.Add("@paydatestartdate", paydatestartdate + " 00:00:00");
                            dynamicParams.Add("@paydateenddate", paydateenddate + " 23:59:59");
                            break;
                        case "CT":
                            search += "and A.lm_date between @paydatestartdate and @paydateenddate ";
                            dynamicParams.Add("@paydatestartdate", paydatestartdate + " 00:00:00");
                            dynamicParams.Add("@paydateenddate", paydateenddate + " 23:59:59");
                            break;
                        default:
                            break;
                    }
                }
                if (search == "")
                {
                    search = "and 1=1 ";
                }
                // --------------查询指定数据--------------
                var total = 0; //总条数
                var sql = @"select A.id, A.status,A.wotype,A.wo_code,A.materiel_code as partcode,B.partname,B.partspec,A.idTopInventory,A.TopInventoryCode,A.TopInventoryName,A.route_code,B.default_route,R.name as route_name,A.plan_qty,A.wkshp_code,C.torg_name as wkshp_name,
                            A.stck_code,D.name as stck_name,A.plan_startdate,A.plan_enddate,A.piroque,A.sourceid,A.m_po,A.saleOrderDeliveryDate,W.saleOrderCode,U.username as lm_user,A.lm_date,A.data_sources,A.isstep,A.clerkuser,
                            B.priuserdefnvc1,B.priuserdefnvc2,B.priuserdefnvc3,B.priuserdefnvc4,B.priuserdefnvc5,B.priuserdefnvc6,A.printcount
                            from TK_Wrk_Man A
                            left join TKimp_Ewo W on A.m_po=W.wo and A.materiel_code=W.materiel_code and A.sbid=W.sbid
                            left join TMateriel_Info B on A.materiel_code=B.partcode
                            left join TOrganization C on A.wkshp_code=C.torg_code
                            left join TSecStck D on A.stck_code=D.code 
                            left join TUser U on A.lm_user=U.usercode 
                            left join TOrganization L on  C.parent_id=L.id
                            left join TFlw_Rout R on A.route_code=R.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[MES报废补单工单查询]
        public static ToMessage MesBadOrderSearch(string wkshopcode, string mesordercode, string sourceorder, string saleordercode, 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 (wkshopcode != "" && wkshopcode != null)
                {
                    search += "and A.wkshp_code=@wkshopcode ";
                    dynamicParams.Add("@wkshopcode", wkshopcode);
                }
                if (mesordercode != "" && mesordercode != null)
                {
                    search += "and A.wo_code like '%'+@mesordercode+'%' ";
                    dynamicParams.Add("@mesordercode", mesordercode);
                }
                if (sourceorder != "" && sourceorder != null)
                {
                    search += "and A.m_po like '%'+@sourceorder+'%' ";
                    dynamicParams.Add("@sourceorder", sourceorder);
                }
                if (saleordercode != "" && saleordercode != null)
                {
                    search += "and W.saleOrderCode like '%'+@saleordercode+'%' ";
                    dynamicParams.Add("@saleordercode", saleordercode);
                }
                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 U.username like '%'+@creatuser+'%' ";
                    dynamicParams.Add("@creatuser", creatuser);
                }
 
                if (search == "")
                {
                    search = "and 1=1 ";
                }
                // --------------查询指定数据--------------
                var total = 0; //总条数
                var sql = @"select A.id, A.status,A.wotype,A.wo_code,A.materiel_code as partcode,B.partname,B.partspec,A.idTopInventory,A.TopInventoryCode,A.TopInventoryName,A.plan_qty,A.wkshp_code,C.torg_name as wkshp_name,
                            A.stck_code,D.name as stck_name,A.plan_startdate,A.plan_enddate,A.piroque,A.sourceid,A.m_po,A.saleOrderDeliveryDate,W.saleOrderCode,U.username as lm_user,A.lm_date,S.laborbad_qty,S.materielbad_qty,
                            B.priuserdefnvc1,B.priuserdefnvc2,B.priuserdefnvc3,B.priuserdefnvc4,B.priuserdefnvc5,B.priuserdefnvc6
                            from TK_Wrk_Man A
                            left join TKimp_Ewo W on A.m_po=W.wo and A.materiel_code=W.materiel_code and A.sbid=W.sbid
                            left join (select wo_code,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty from  TK_Wrk_Step where (laborbad_qty+materielbad_qty)>0 group by wo_code) S on A.wo_code=S.wo_code
                            left join TMateriel_Info B on A.materiel_code=B.partcode
                            left join TOrganization C on A.wkshp_code=C.torg_code
                            left join TSecStck D on A.stck_code=D.code 
                            left join TUser U on A.lm_user=U.usercode 
                            where A.is_delete<>'1'  and (S.laborbad_qty+S.materielbad_qty)>0 " + 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[MES工单新增、获取工单号]
        public static ToMessage AddMesOrderCodeSearch()
        {
            string sql = "";
            string wo_code = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //获取单据号
                sql = @"SELECT 'SGPO'+CONVERT(varchar(12) , getdate(), 112 )+'_'+cast(isnull(max(cast(substring(wo_code,charindex('_',wo_code)+1,len(wo_code)-charindex('_',wo_code)) as numeric)),0)+1 as varchar) as numct
                        FROM TK_Wrk_Man where wo_code like '%SGPO%'";
                var data = DapperHelper.selecttable(sql);
                mes.code = "200";
                mes.message = "查询成功!";
                mes.data = data.Rows[0]["numct"].ToString();
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[MES工单编辑获取工序数据]
        public static ToMessage UpdateMesOrderStepSearch(string sourceid, string sourcewo, string wocode, string data_sources)
        {
            string sql = ""; decimal canupdate_qty = 0;
            var dynamicParams = new DynamicParameters();
            Dictionary<object, object> dir = new Dictionary<object, object>();
            try
            {
                if (data_sources == "ERP")  //数据来源ERP
                {
                    //查询订单任务总数
                    sql = @"select qty,relse_qty  from TKimp_Ewo where id=@sourceid and wo=@sourcewo";
                    dynamicParams.Add("@sourceid", sourceid);
                    dynamicParams.Add("@sourcewo", sourcewo);
                    var data0 = DapperHelper.selectdata(sql, dynamicParams);
                    //查询当前工单可修改数量=订单总数-已下达工单总数
                    sql = @"select isnull(sum(plan_qty),0) as plan_qty   from TK_Wrk_Man 
                            where sourceid=@sourceid and m_po=@sourcewo and wo_code<>@wocode";
                    dynamicParams.Add("@sourceid", sourceid);
                    dynamicParams.Add("@sourcewo", sourcewo);
                    dynamicParams.Add("@wocode", wocode);
                    var data = DapperHelper.selectdata(sql, dynamicParams);
                    //当前工单可修改数量=订单数量-非当前工单总下达工单数量
                    canupdate_qty = decimal.Parse(data0.Rows[0]["qty"].ToString()) - decimal.Parse(data.Rows[0]["plan_qty"].ToString());
                }
                if (data_sources == "MES")  //数据来源MES
                {
                    if (sourceid == "" || sourceid == null) //无源单
                    {
                        //查询当前工单可修改数量=工单总数
                        sql = @"select plan_qty   from TK_Wrk_Man where wo_code=@wo_code";
                        dynamicParams.Add("@wo_code", wocode);
                        var data = DapperHelper.selectdata(sql, dynamicParams);
                        //当前工单可修改数量=工单数量
                        canupdate_qty = decimal.Parse(data.Rows[0]["plan_qty"].ToString());
                    }
                    else //有源单(报废补单)
                    {
                        //不控制 标识为-1
                        canupdate_qty = -1;
                    }
                }
                //获取工序信息
                sql = @"select S.wo_code,S.seq,S.step_code,T.stepname,S.stepprice,S.ratio,(isnull(S.good_qty,0)+isnull(S.ng_qty,0)+isnull(S.laborbad_qty,0)+isnull(S.materielbad_qty,0)) as produceq_qty,
                        S.good_qty,S.ng_qty,S.laborbad_qty,S.materielbad_qty,S.plan_qty,(isnull(S.plan_quantity,0)-(isnull(S.good_qty,0)+isnull(S.ng_qty,0)+isnull(S.laborbad_qty,0)+isnull(S.materielbad_qty,0))) as delive_qty,S.isbott,S.isend 
                        from TK_Wrk_Step S
                        left join TStep  T on S.step_code=T.stepcode
                        where S.wo_code=@wocode order by S.seq ";
                dynamicParams.Add("@wocode", wocode);
                var data1 = DapperHelper.selectdata(sql, dynamicParams);
                dir.Add("canupdate_qty", canupdate_qty);
                dir.Add("stepdata", data1);
                mes.code = "200";
                mes.count = data1.Rows.Count;
                mes.message = "查询成功";
                mes.data = dir;
            }
            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 operType, WorkList json, User us)
        {
            string sql = "", route_code = "";
            var dynamicParams = new DynamicParameters();
            List<object> list = new List<object>();
            try
            {
                dynamic dynObj = JObject.Parse(us.mesSetting);
                bool route = dynObj.route;
                if (route) //工艺路线版
                {
                    route_code = json.routecode;
                }
                else //工序版
                {
                    route_code = null;
                }
                if (operType == "Add")
                {
                    //写入工单表
                    sql = @"insert into TK_Wrk_Man(wo_code,wotype,status,wkshp_code,plan_qty,lm_user,lm_date,materiel_code,route_code,sourceid,m_po,saleOrderDeliveryDate,plan_startdate,plan_enddate,piroque,isaps,data_sources,isstep)
                                values(@wo_code,@wotype,@status,@wkshp_code,@plan_qty,@lm_user,@lm_date,@materiel_code,@route_code,@sourceid,@m_po,@saleOrderDeliveryDate,@plan_startdate,@plan_enddate,@orderlev,@isaps,@data_sources,@isstep)";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wo_code = json.wocode,
                            wotype = json.wotype,
                            status = json.wostatus,
                            wkshp_code = json.wkshopcode,
                            plan_qty = json.woqty,
                            lm_user = us.usercode,
                            lm_date = DateTime.Now.ToString(),
                            materiel_code = json.partcode,
                            route_code = route_code,
                            sourceid = json.sourceid == "" ? null : json.sourceid, //无源单时赋值NULL
                            m_po = json.sourcewo,
                            saleOrderDeliveryDate = json.deliverydate,
                            plan_startdate = json.paystartdate,
                            plan_enddate = json.payenddate,
                            orderlev = "3",//优先级:特级(1) 紧急(2) 正常(3)
                            isaps = "N", //是否排产,默认N  Y=是   N=否
                            data_sources = json.data_sources,
                            isstep = json.isstep  //是否关联工序
                        }
                    });
                    //写入工序任务表
                    for (int i = 0; i < json.WorkListSub.Count; i++)
                    {
                        sql = @"insert into TK_Wrk_Step(wo_code,seq,step_code,route_code,stepprice,plan_quantity,plan_qty,ratio,status,isbott,isend,lm_user,lm_date)
                                values(@wo_code,@seq,@step_code,@route_code,@stepprice,@plan_quantity,@plan_qty,@ratio,@status,@isbott,@isend,@lm_user,@lm_date)";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo_code = json.wocode,
                                seq = json.WorkListSub[i].stepseq,
                                step_code = json.WorkListSub[i].stepcode,
                                route_code = route_code,
                                stepprice = json.WorkListSub[i].stepprice,
                                plan_quantity = json.woqty,
                                plan_qty = json.WorkListSub[i].sumqty,
                                ratio=json.WorkListSub[i].ratio,
                                status = json.wostatus,
                                isbott = json.WorkListSub[i].isbott,
                                isend = json.WorkListSub[i].isend,
                                lm_user = us.usercode,
                                lm_date = DateTime.Now.ToString()
                            }
                        });
                    }
                    bool aa = DapperHelper.DoTransaction(list);
                    if (aa)
                    {
                        //写入操作记录表
                        LogHelper.DbOperateLog(us.usercode, "新增", "新增了工单:" + json.wocode, us.usertype);
                        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 wotype=@wotype,wkshp_code=@wkshp_code,plan_qty=@plan_qty,lm_user=@lm_user,lm_date=@lm_date,
                            materiel_code=@materiel_code,route_code=@route_code,sourceid=@sourceid,m_po=@m_po,saleOrderDeliveryDate=@saleOrderDeliveryDate,plan_startdate=@plan_startdate,plan_enddate=@plan_enddate,isstep=@isstep
                            where wo_code=@wo_code";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wo_code = json.wocode,
                            wotype = json.wotype,
                            wkshp_code = json.wkshopcode,
                            plan_qty = json.woqty,
                            materiel_code = json.partcode,
                            route_code = route_code,
                            sourceid = json.sourceid == "" ? null : json.sourceid, //无源单时赋值NULL
                            m_po = json.sourcewo,
                            saleOrderDeliveryDate = json.deliverydate,
                            plan_startdate = json.paystartdate,
                            plan_enddate = json.payenddate,
                            lm_user = us.usercode,
                            lm_date = DateTime.Now.ToString(),
                            isstep = json.isstep  //是否关联工序
                        }
                    });
                    //删除工单工序表
                    sql = @"delete TK_Wrk_Step where wo_code=@wo_code";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            wo_code = json.wocode
                        }
                    });
                    //写入工单工序表
                    for (int i = 0; i < json.WorkListSub.Count; i++)
                    {
                        sql = @"insert into TK_Wrk_Step(wo_code,seq,step_code,route_code,stepprice,plan_quantity,plan_qty,ratio,status,isbott,isend,lm_user,lm_date)
                                values(@wo_code,@seq,@step_code,@route_code,@stepprice,@plan_quantity,@plan_qty,@ratio,@status,@isbott,@isend,@lm_user,@lm_date)";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo_code = json.wocode,
                                seq = json.WorkListSub[i].stepseq,
                                step_code = json.WorkListSub[i].stepcode,
                                route_code = route_code,
                                stepprice = json.WorkListSub[i].stepprice,
                                plan_quantity = json.woqty,
                                plan_qty = json.WorkListSub[i].sumqty,
                                ratio=json.WorkListSub[i].ratio,
                                status = json.wostatus,
                                isbott = json.WorkListSub[i].isbott,
                                isend = json.WorkListSub[i].isend,
                                lm_user = us.usercode,
                                lm_date = DateTime.Now.ToString()
                            }
                        });
                    }
                    //判断源头单据是否来源ERP
                    if (json.data_sources == "ERP")
                    {
                        //判断工单修改数量差值是否为0
                        if (json.difference != "0")
                        {
                            string staus = "";
                            //查询订单总数,已下达数量
                            sql = @"select qty,relse_qty  from TKimp_Ewo where id=@sourceid and wo=@sourcewo";
                            dynamicParams.Add("@sourceid", json.sourceid);
                            dynamicParams.Add("@sourcewo", json.sourcewo);
                            var data0 = DapperHelper.selectdata(sql, dynamicParams);
                            //当前工单可修改数量=订单数量-非当前工单总下达工单数量
                            decimal qty = decimal.Parse(data0.Rows[0]["qty"].ToString());//订单总数
                            decimal relse_qty = decimal.Parse(data0.Rows[0]["relse_qty"].ToString());//订单已下达总数
                            relse_qty = relse_qty + decimal.Parse(json.difference);//新的下达数量=原始下达数量+差值(正负)
                            if (qty == relse_qty)
                            {
                                staus = "CREATED"; //全部下达
                            }
                            else
                            {
                                staus = "CREATING";//部分下达
                            }
                            //更新订单表状态、已下达数量
                            sql = @"update TKimp_Ewo set status=@status,relse_qty=@relse_qty where id=@sourceid and wo=@sourcewo";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    status = staus,
                                    relse_qty = relse_qty,
                                    sourceid = json.sourceid,
                                    sourcewo = json.sourcewo
                                }
                            });
                        }
                    }
 
                    bool aa = DapperHelper.DoTransaction(list);
                    if (aa)
                    {
                        //写入操作记录表
                        LogHelper.DbOperateLog(us.usercode, "修改", "修改了工单:" + json.wocode, us.usertype);
                        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 UpdateMesOrderStepListSearch(List<WorkStepList> worksteplist)
        {
            string sql = ""; decimal canupdate_qty = 0;
            var dynamicParams = new DynamicParameters();
            List<Dictionary<object, object>> dir = new List<Dictionary<object, object>>();
            try
            {
                for (int i = 0; i < worksteplist.Count; i++)
                {
                    // 创建一个新的字典
                    Dictionary<object, object> dict = new Dictionary<object, object>();
 
                    if (worksteplist[i].data_sources == "ERP")  //数据来源ERP
                    {
                       
                        //查询当前工单可修改数量=订单总数-已下达工单总数
                        sql = @"select isnull(plan_qty,0) as plan_qty   from TK_Wrk_Man 
                            where sourceid=@sourceid and m_po=@sourcewo and wo_code=@wocode";
                        dynamicParams.Add("@sourceid", worksteplist[i].sourceid);
                        dynamicParams.Add("@sourcewo", worksteplist[i].sourcewo);
                        dynamicParams.Add("@wocode", worksteplist[i].wocode);
                        var data = DapperHelper.selectdata(sql, dynamicParams);
                        //当前工单数量
                        canupdate_qty = decimal.Parse(data.Rows[0]["plan_qty"].ToString());
                    }
                    if (worksteplist[i].data_sources == "MES")  //数据来源MES
                    {
                        if (worksteplist[i].sourceid == "" || worksteplist[i].sourceid == null) //无源单
                        {
                            //查询当前工单可修改数量=工单总数
                            sql = @"select plan_qty   from TK_Wrk_Man where wo_code=@wo_code";
                            dynamicParams.Add("@wo_code", worksteplist[i].wocode);
                            var data = DapperHelper.selectdata(sql, dynamicParams);
                            //当前工单工单数量
                            canupdate_qty = decimal.Parse(data.Rows[0]["plan_qty"].ToString());
                        }
                        else //有源单(报废补单)
                        {
                            //不控制 标识为-1
                            canupdate_qty = -1;
                        }
                    }
                    //获取工序信息
                    sql = @"select S.wo_code,S.seq,S.step_code as stepcode,T.stepname,S.stepprice,S.ratio,(isnull(S.good_qty,0)+isnull(S.ng_qty,0)+isnull(S.laborbad_qty,0)+isnull(S.materielbad_qty,0)) as produceq_qty,
                        S.good_qty,S.ng_qty,S.laborbad_qty,S.materielbad_qty,S.plan_qty,(isnull(S.plan_quantity,0)-(isnull(S.good_qty,0)+isnull(S.ng_qty,0)+isnull(S.laborbad_qty,0)+isnull(S.materielbad_qty,0))) as delive_qty,S.isbott,S.isend 
                        from TK_Wrk_Step S
                        left join TStep  T on S.step_code=T.stepcode
                        where S.wo_code=@wocode order by S.seq ";
                    dynamicParams.Add("@wocode", worksteplist[i].wocode);
                    var data1 = DapperHelper.selectdata(sql, dynamicParams);
 
                    // 向字典中添加数据
                    dict.Add("canupdate_qty", canupdate_qty);
                    dict.Add("stepdata", data1); 
                    // 将字典添加到列表中
                    dir.Add(dict);
                    mes.code = "200";
                    mes.count = dir.Count;
                    mes.message = "查询成功";
                    mes.data = dir;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[MES工单新增、编辑批量绑定提交]
        public static ToMessage AddUpdateMesOrderList(string operType, List<WorkList> json, User us)
        {
            string sql = "", route_code = "";
            var dynamicParams = new DynamicParameters();
            List<object> list = new List<object>();
            try
            {
                dynamic dynObj = JObject.Parse(us.mesSetting);
                bool route = dynObj.route;
                if (!route) //工序版
                {
                    route_code = null;
                }
                if (operType == "Add")
                {
                    for (int i = 0; i < json.Count; i++)
                    {
                        route_code = json[i].routecode;
                        //写入工单表
                        sql = @"insert into TK_Wrk_Man(wo_code,wotype,status,wkshp_code,plan_qty,lm_user,lm_date,materiel_code,route_code,sourceid,m_po,saleOrderDeliveryDate,plan_startdate,plan_enddate,piroque,isaps,data_sources,isstep)
                                values(@wo_code,@wotype,@status,@wkshp_code,@plan_qty,@lm_user,@lm_date,@materiel_code,@route_code,@sourceid,@m_po,@saleOrderDeliveryDate,@plan_startdate,@plan_enddate,@orderlev,@isaps,@data_sources,@isstep)";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo_code = json[i].wocode,
                                wotype = json[i].wotype,
                                status = json[i].wostatus,
                                wkshp_code = json[i].wkshopcode,
                                plan_qty = json[i].woqty,
                                lm_user = us.usercode,
                                lm_date = DateTime.Now.ToString(),
                                materiel_code = json[i].partcode,
                                route_code = route_code,
                                sourceid = json[i].sourceid == "" ? null : json[i].sourceid, //无源单时赋值NULL
                                m_po = json[i].sourcewo,
                                saleOrderDeliveryDate = json[i].deliverydate,
                                plan_startdate = json[i].paystartdate,
                                plan_enddate = json[i].payenddate,
                                orderlev = "3",//优先级:特级(1) 紧急(2) 正常(3)
                                isaps = "N", //是否排产,默认N  Y=是   N=否
                                data_sources = json[i].data_sources,
                                isstep = json[i].isstep  //是否关联工序
                            }
                        });
                        //写入工序任务表
                        for (int j = 0; j < json[i].WorkListSub.Count; j++)
                        {
                            sql = @"insert into TK_Wrk_Step(wo_code,seq,step_code,route_code,stepprice,plan_quantity,plan_qty,ratio,status,isbott,isend,lm_user,lm_date)
                                values(@wo_code,@seq,@step_code,@route_code,@stepprice,@plan_quantity,@plan_qty,@ratio,@status,@isbott,@isend,@lm_user,@lm_date)";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    wo_code = json[i].wocode,
                                    seq = json[i].WorkListSub[j].stepseq,
                                    step_code = json[i].WorkListSub[j].stepcode,
                                    route_code = route_code,
                                    stepprice = json[i].WorkListSub[j].stepprice,
                                    plan_quantity = json[i].woqty,
                                    plan_qty = json[i].WorkListSub[j].sumqty,
                                    ratio=json[i].WorkListSub[j].ratio,
                                    status = json[i].wostatus,
                                    isbott = json[i].WorkListSub[j].isbott,
                                    isend = json[i].WorkListSub[j].isend,
                                    lm_user = us.usercode,
                                    lm_date = DateTime.Now.ToString()
                                }
                            });
                        }
                    }
                    bool aa = DapperHelper.DoTransaction(list);
                    if (aa)
                    {
                        //写入操作记录表
                        LogHelper.DbOperateLog(us.usercode, "新增", "新增了工单:" + json[0].wocode, us.usertype);
                        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")
                {
                    for (int i = 0; i < json.Count; i++)
                    {
                        route_code = json[i].routecode;
                        //修改工单表
                        sql = @"update TK_Wrk_Man set wotype=@wotype,wkshp_code=@wkshp_code,plan_qty=@plan_qty,lm_user=@lm_user,lm_date=@lm_date,
                            materiel_code=@materiel_code,route_code=@route_code,sourceid=@sourceid,m_po=@m_po,saleOrderDeliveryDate=@saleOrderDeliveryDate,plan_startdate=@plan_startdate,plan_enddate=@plan_enddate,isstep=@isstep
                            where wo_code=@wo_code";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo_code = json[i].wocode,
                                wotype = json[i].wotype,
                                wkshp_code = json[i].wkshopcode,
                                plan_qty = json[i].woqty,
                                materiel_code = json[i].partcode,
                                route_code = route_code,
                                sourceid = json[i].sourceid == "" ? null : json[i].sourceid, //无源单时赋值NULL
                                m_po = json[i].sourcewo,
                                saleOrderDeliveryDate = json[i].deliverydate,
                                plan_startdate = json[i].paystartdate,
                                plan_enddate = json[i].payenddate,
                                lm_user = us.usercode,
                                lm_date = DateTime.Now.ToString(),
                                isstep = json[i].isstep  //是否关联工序
                            }
                        });
                        //删除工单工序表
                        sql = @"delete TK_Wrk_Step where wo_code=@wo_code";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                wo_code = json[i].wocode
                            }
                        });
                        //写入工单工序表
                        for (int j = 0; j < json[i].WorkListSub.Count; j++)
                        {
                            sql = @"insert into TK_Wrk_Step(wo_code,seq,step_code,route_code,stepprice,plan_quantity,plan_qty,ratio,status,isbott,isend,lm_user,lm_date)
                                values(@wo_code,@seq,@step_code,@route_code,@stepprice,@plan_quantity,@plan_qty,@ratio,@status,@isbott,@isend,@lm_user,@lm_date)";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    wo_code = json[i].wocode,
                                    seq = json[i].WorkListSub[j].stepseq,
                                    step_code = json[i].WorkListSub[j].stepcode,
                                    route_code = route_code,
                                    stepprice = json[i].WorkListSub[j].stepprice,
                                    plan_quantity = json[i].woqty,
                                    plan_qty = json[i].WorkListSub[j].sumqty,
                                    ratio=json[i].WorkListSub[j].ratio,
                                    status = json[i].wostatus,
                                    isbott = json[i].WorkListSub[j].isbott,
                                    isend = json[i].WorkListSub[j].isend,
                                    lm_user = us.usercode,
                                    lm_date = DateTime.Now.ToString()
                                }
                            });
                        }
                        //判断源头单据是否来源ERP
                        if (json[i].data_sources == "ERP")
                        {
                            //判断工单修改数量差值是否为0
                            if (json[i].difference != "0")
                            {
                                string staus = "";
                                //查询订单总数,已下达数量
                                sql = @"select qty,relse_qty  from TKimp_Ewo where id=@sourceid and wo=@sourcewo";
                                dynamicParams.Add("@sourceid", json[i].sourceid);
                                dynamicParams.Add("@sourcewo", json[i].sourcewo);
                                var data0 = DapperHelper.selectdata(sql, dynamicParams);
                                //当前工单可修改数量=订单数量-非当前工单总下达工单数量
                                decimal qty = decimal.Parse(data0.Rows[0]["qty"].ToString());//订单总数
                                decimal relse_qty = decimal.Parse(data0.Rows[0]["relse_qty"].ToString());//订单已下达总数
                                relse_qty = relse_qty + decimal.Parse(json[i].difference);//新的下达数量=原始下达数量+差值(正负)
                                if (qty == relse_qty)
                                {
                                    staus = "CREATED"; //全部下达
                                }
                                else
                                {
                                    staus = "CREATING";//部分下达
                                }
                                //更新订单表状态、已下达数量
                                sql = @"update TKimp_Ewo set status=@status,relse_qty=@relse_qty where id=@sourceid and wo=@sourcewo";
                                list.Add(new
                                {
                                    str = sql,
                                    parm = new
                                    {
                                        status = staus,
                                        relse_qty = relse_qty,
                                        sourceid = json[i].sourceid,
                                        sourcewo = json[i].sourcewo
                                    }
                                });
                            }
                        }
                    }
                    bool aa = DapperHelper.DoTransaction(list);
                    if (aa)
                    {
                        //写入操作记录表
                        LogHelper.DbOperateLog(us.usercode, "修改", "修改了工单:" + string.Join(",", json.Select(j => j.wocode.ToString())), us.usertype);
                        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 UpdateMesOrderPrintCount(string wo_code)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                //更新工单打印次数
                sql = @"update TK_Wrk_Man set printcount=printcount+1  where wo_code=@wo_code";
                list.Add(new { str = sql, parm = new { wo_code = wo_code } });
 
                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 souceid, string wocode, string m_po, string orderqty, User us)
        {
            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 in('NEW','ALLO','NOSCHED')";
                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 and id=@souceid";
                        dynamicParams.Add("@m_po", m_po);
                        dynamicParams.Add("@souceid", souceid);
                        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 and id=@souceid";
                            list.Add(new { str = sql, parm = new { m_po = m_po, souceid = souceid } });
                        }
                        else
                        {
                            //回写订单表状态及已下单数量
                            sql = @"update TKimp_Ewo set status='CREATING',relse_qty=relse_qty-@orderqty  where wo=@m_po and id=@souceid";
                            list.Add(new { str = sql, parm = new { m_po = m_po, souceid = souceid, orderqty = decimal.Parse(orderqty) } });
                        }
                    }
                    //删除工单工序表
                    sql = @"delete TK_Wrk_Step  where wo_code=@wocode";
                    list.Add(new { str = sql, parm = new { wocode = wocode } });
 
                    //删除加工单用料表(子件)
                    //sql = @"delete TK_Wrk_Allo  where wo_code=@wocode";
                    //list.Add(new { str = sql, parm = new { wocode = wocode } });
 
                    //删除工单表
                    sql = @"delete TK_Wrk_Man   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)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "删除", "删除了工单:" + wocode, us.usertype);
                    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 MesOrderNewStepContent(string wkshopcode, string routecode, string partcode, User us)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                dynamic dynObj = JObject.Parse(us.mesSetting);
                bool route = dynObj.route;
                if (route) //工艺路线版
                {
                    //MES工单查找历史引用最新工序信息
                    sql = @"select S.wo_code,S.seq,S.step_code,T.stepname as step_name,S.stepprice,S.isbott,S.isend,S.ratio
                        from TK_Wrk_Step S
                        inner join (
                        select top 1 A.wo_code,A.route_code   from TK_Wrk_Man A
                        inner join TK_Wrk_Step S on A.wo_code=S.wo_code and A.route_code=S.route_code
                        where A.materiel_code=@partcode and A.wkshp_code=@wkshopcode and A.route_code=@routecode
                        order by A.lm_date desc
                        ) as W on S.wo_code=W.wo_code and S.route_code=W.route_code
                        left join TStep  T on S.step_code=T.stepcode
                        order by S.seq";
                    dynamicParams.Add("@wkshopcode", wkshopcode);
                    dynamicParams.Add("@partcode", partcode);
                    dynamicParams.Add("@routecode", routecode);
                }
                else
                {
                    //MES工单查找历史引用最新工序信息
                    sql = @"select S.wo_code,S.seq,S.step_code,T.stepname as step_name,S.stepprice,S.isbott,S.isend,S.ratio
                        from TK_Wrk_Step S
                        inner join (
                        select top 1 A.wo_code   from TK_Wrk_Man A
                        inner join TK_Wrk_Step S on A.wo_code=S.wo_code
                        where A.materiel_code=@partcode and A.wkshp_code=@wkshopcode
                        order by A.lm_date desc
                        ) as W on S.wo_code=W.wo_code
                        left join TStep  T on S.step_code=T.stepcode
                        order by S.seq";
                    dynamicParams.Add("@wkshopcode", wkshopcode);
                    dynamicParams.Add("@partcode", partcode);
                }
 
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.count = data.Rows.Count;
                mes.data = data;
                mes.message = "查询成功!";
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[MES工单查看工单SOP]
        public static ToMessage MesOrderSopSearch(string wocode, string materielcode)
        {
            string sql = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //获取SOP文件信息
                sql = @"select filename,filepath,version from TWrkOrderSop
                        where wo=@wocode and materielcode=@materielcode
                        order by version";
                dynamicParams.Add("@wocode", wocode);
                dynamicParams.Add("@materielcode", materielcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    mes.code = "200";
                    mes.message = "查询成功!";
                    mes.data = data;
                }
                else
                {
                    mes.code = "300";
                    mes.message = "当前工单产品暂无SOP文件!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[MES工单查看工艺SOP]
        public static ToMessage MesOrderProcessSopSearch(string materielcode, string routecode, string stepcode, User us)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                dynamic dynObj = JObject.Parse(us.mesSetting);
                bool route = dynObj.route;
                if (route) //工艺路线版
                {
                    //获取SOP文件信息
                    sql = @"select filename,filepath,version from TProcessSop
                        where  materielcode=@materielcode and routecode=@routecode and stepcode=@stepcode
                        order by version";
                    dynamicParams.Add("@materielcode", materielcode);
                    dynamicParams.Add("@routecode", routecode);
                    dynamicParams.Add("@stepcode", stepcode);
                }
                else
                {
                    //获取SOP文件信息
                    sql = @"select filename,filepath,version from TProcessSop
                        where  materielcode=@materielcode and stepcode=@stepcode
                        order by version";
                    dynamicParams.Add("@materielcode", materielcode);
                    dynamicParams.Add("@stepcode", stepcode);
                }
 
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.count = data.Rows.Count;
                mes.data = data;
                mes.message = "查询成功!";
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
 
        #region[MES工单派发提交]
        public static ToMessage MesOrderDistribution(string[] wocodelist, User us)
        {
            string sql = "";
            List<object> list = new List<object>();
            try
            {
                //更新工单表状态
                sql = @"update TK_Wrk_Man set status=@status,distributionuser=@distributionuser,distributiontime=@distributiontime where wo_code in @wocode";
                list.Add(new
                {
                    str = sql,
                    parm = new
                    {
                        wocode = wocodelist,
                        status = "ALLO",
                        distributionuser=us.usercode,
                        distributiontime= DateTime.Now.ToString()
                    }
                });
                //更新工序任务表状态
                sql = @"update TK_Wrk_Step set status=@status where wo_code in @wocode";
                list.Add(new
                {
                    str = sql,
                    parm = new
                    {
                        wocode = wocodelist,
                        status = "ALLO"
                    }
                });
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "派发", "派发了工单:" + string.Join(",", wocodelist), us.usertype);
                    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[工单关闭列表查询]
        public static ToMessage MesOrderClosedSearch(string mesorderstus, string wkshopcode, string mesordercode, string sourceorder, string saleordercode, string ordertype, 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 == "CLOSED")
                {
                    search += "and A.status=@mesorderstus ";
                    dynamicParams.Add("@mesorderstus", "CLOSED");
                }
                if (mesorderstus == "NOCLOSED")
                {
                    search += "and A.status<>@mesorderstus ";
                    dynamicParams.Add("@mesorderstus", "CLOSED");
                }
                if (wkshopcode != "" && wkshopcode != null)
                {
                    search += "and A.wkshp_code=@wkshopcode ";
                    dynamicParams.Add("@wkshopcode", wkshopcode);
                }
                if (mesordercode != "" && mesordercode != null)
                {
                    search += "and A.wo_code like '%'+@mesordercode+'%' ";
                    dynamicParams.Add("@mesordercode", mesordercode);
                }
                if (sourceorder != "" && sourceorder != null)
                {
                    search += "and A.m_po like '%'+@sourceorder+'%' ";
                    dynamicParams.Add("@sourceorder", sourceorder);
                }
                if (saleordercode != "" && saleordercode != null)
                {
                    search += "and W.saleOrderCode like '%'+@saleordercode+'%' ";
                    dynamicParams.Add("@saleordercode", saleordercode);
                }
                if (ordertype != "" && ordertype != null)
                {
                    search += "and A.wotype like '%'+@ordertype+'%' ";
                    dynamicParams.Add("@ordertype", ordertype);
                }
                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 U.username like '%'+@creatuser+'%' ";
                    dynamicParams.Add("@creatuser", creatuser);
                }
 
                if (search == "")
                {
                    search = "and 1=1 ";
                }
                // --------------查询指定数据--------------
                var total = 0; //总条数
                var sql = @"select A.id, A.status,A.wotype,A.wo_code,A.materiel_code as partcode,B.partname,B.partspec,A.plan_qty,A.wkshp_code,C.torg_name as wkshp_name,
                            A.stck_code,D.name as stck_name,A.plan_startdate,A.plan_enddate,A.piroque,A.sourceid,A.m_po,A.saleOrderDeliveryDate,W.saleOrderCode,U.username as lm_user,A.lm_date,A.data_sources,
                            B.priuserdefnvc1,B.priuserdefnvc2,B.priuserdefnvc3,B.priuserdefnvc4,B.priuserdefnvc5,B.priuserdefnvc6
                            from TK_Wrk_Man A
                            left join TKimp_Ewo W on A.m_po=W.wo and A.materiel_code=W.materiel_code
                            left join TMateriel_Info B on A.materiel_code=B.partcode
                            left join TOrganization C on A.wkshp_code=C.torg_code
                            left join TSecStck D on A.stck_code=D.code 
                            left join TUser U on A.lm_user=U.usercode 
                            left join TOrganization L on  C.parent_id=L.id
                            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[MES工单批量关闭提交]
        public static ToMessage MesOrderBitchClosedSeave(User us, string[] wocodelist)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                for (int i = 0; i < wocodelist.Length; i++)
                {
                    //关闭工单对应工序任务
                    sql = @"update TK_Wrk_Step set status='CLOSED'  where wo_code in @wocode";
                    list.Add(new { str = sql, parm = new { wocode = wocodelist } });
                    //回写工单表状态为(关闭)
                    sql = @"update TK_Wrk_Man set status='CLOSED',closeuser=@username,closedate=@closedate  where wo_code in @wocode";
                    list.Add(new { str = sql, parm = new { wocode = wocodelist, username = us.usercode, closedate = DateTime.Now.ToString() } });
                }
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "关闭", "关闭了工单:" + string.Join(",", wocodelist), us.usertype);
                    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 MesOrderBitchReverseClosedSeave(User us, string[] wocodelist)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                for (int i = 0; i < wocodelist.Length; i++)
                {
                    //关闭工单对应工序任务
                    sql = @"update TK_Wrk_Step set status=closebeforestatus where wo_code in @wocode";
                    list.Add(new { str = sql, parm = new { wocode = wocodelist } });
                    //回写工单表状态为(关闭)
                    sql = @"update TK_Wrk_Man set status=closebeforestatus,closeuser=@username,closedate=@closedate  where wo_code in @wocode";
                    list.Add(new { str = sql, parm = new { wocode = wocodelist, username = us.usercode, closedate = DateTime.Now.ToString() } });
                }
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "反关闭", "关闭了工单:" + string.Join(",", wocodelist), us.usertype);
                    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 MesOrderStepSearch(string wkshopcode, string wocode, string partcode, string partname, string partspec, 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 (wkshopcode != "" && wkshopcode != null)
                {
                    search += "and B.wkshp_code=@wkshopcode ";
                    dynamicParams.Add("@wkshopcode", wkshopcode);
                }
                if (wocode != "" && wocode != null)
                {
                    string[] arra = wocode.Split(';');
                    if (arra.Length == 1) //工单号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                    }
                    if (arra.Length == 2) //工单号+工序号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        stepcode = arra[1]; //获取指定字符串前面的字符
                    }
 
                    if (ordercode != "" && (stepcode == null || stepcode == "")) //工单号不为空,工序号为空
                    {
                        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);
                    }
                }
                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;
                        }
                    }
                }
                if (partcode != "" && partcode != null)
                {
                    search += "and M.partcode like '%'+@partcode+'%' ";
                    dynamicParams.Add("@partcode", partcode);
                }
                if (partname != "" && partname != null)
                {
                    search += "and M.partname like '%'+@partname+'%' ";
                    dynamicParams.Add("@partname", partname);
                }
                if (partspec != "" && partspec != null)
                {
                    search += "and M.partspec like '%'+@partspec+'%' ";
                    dynamicParams.Add("@partspec", partspec);
                }
                //根据条件查询工单工序任务(自制工序)
                sql = @"select A.id,A.status,B.wkshp_code,T.torg_name as wkshp_name,A.wo_code,M.partcode,M.partname,M.partspec,A.seq,A.isbott,A.isend,
                        S.stepcode,S.stepname,S.descr,A.plan_quantity,A.plan_qty,A.good_qty,A.ng_qty,A.laborbad_qty,A.materielbad_qty,B.lm_date,
                        M.priuserdefnvc1,M.priuserdefnvc2,M.priuserdefnvc3,M.priuserdefnvc4,M.priuserdefnvc5,M.priuserdefnvc6
                        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
                        left join TOrganization T on B.wkshp_code=T.torg_code
                        where A.status in('ALLO','START') and S.flwtype='Z'  " + search;
                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                mes.code = "200";
                mes.count = total;
                mes.message = "查询成功!";
                mes.data = data.ToList();
                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 wkshopcode, string wocode, string partcode, string partname, string partspec, 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 (wkshopcode != "" && wkshopcode != null)
                {
                    search += "and B.wkshp_code=@wkshopcode ";
                    dynamicParams.Add("@wkshopcode", wkshopcode);
                }
                if (wocode != "" && wocode != null)
                {
                    string[] arra = wocode.Split(';');
                    if (arra.Length == 1) //工单号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                    }
                    if (arra.Length == 2) //工单号+工序号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        stepcode = arra[1]; //获取指定字符串前面的字符
                    }
 
                    if (ordercode != "" && (stepcode == null || stepcode == "")) //工单号不为空,工序号为空
                    {
                        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);
                    }
                }
                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;
                        }
                    }
                }
                if (partcode != "" && partcode != null)
                {
                    search += "and M.partcode like '%'+@partcode+'%' ";
                    dynamicParams.Add("@partcode", partcode);
                }
                if (partname != "" && partname != null)
                {
                    search += "and M.partname like '%'+@partname+'%' ";
                    dynamicParams.Add("@partname", partname);
                }
                if (partspec != "" && partspec != null)
                {
                    search += "and M.partspec like '%'+@partspec+'%' ";
                    dynamicParams.Add("@partspec", partspec);
                }
                //根据条件查询工单工序任务(自制工序)
                sql = @"select A.id,A.status,B.wkshp_code,T.torg_name as wkshp_name,A.wo_code,M.partcode,M.partname,M.partspec,A.seq,A.isbott,A.isend,
                        S.stepcode,S.stepname,S.descr,A.plan_quantity,A.plan_qty,A.good_qty,A.ng_qty,
                        (select isnull(sum(fqty),0) as fqty   from TK_Wrk_OutRecord where wo_code=A.wo_code and step_code=A.step_code and style='F') as fqty,
                        A.laborbad_qty,A.materielbad_qty,A.plan_startdate,A.plan_enddate,B.lm_date,
                        M.priuserdefnvc1,M.priuserdefnvc2,M.priuserdefnvc3,M.priuserdefnvc4,M.priuserdefnvc5,M.priuserdefnvc6
                        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
                        left join TOrganization T on B.wkshp_code=T.torg_code
                        where A.status in('ALLO','START') and S.flwtype='W'  " + search;
                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                mes.code = "200";
                mes.count = total;
                mes.message = "查询成功!";
                mes.data = data.ToList();
                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 MesOrderNgStepSearch(string wkshopcode, string wocode, string partcode, string partname, string partspec, 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 (wkshopcode != "" && wkshopcode != null)
                {
                    search += "and B.wkshp_code=@wkshopcode ";
                    dynamicParams.Add("@wkshopcode", wkshopcode);
                }
                if (wocode != "" && wocode != null)
                {
                    string[] arra = wocode.Split(';');
                    if (arra.Length == 1) //工单号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                    }
                    if (arra.Length == 2) //工单号+工序号二维码
                    {
                        ordercode = arra[0]; //获取指定字符串前面的字符
                        stepcode = arra[1]; //获取指定字符串前面的字符
                    }
 
                    if (ordercode != "" && (stepcode == null || stepcode == "")) //工单号不为空,工序号为空
                    {
                        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);
                    }
                }
                if (stepcode != "")
                {
                    //查找当前工序任务
                    sql = @"select *  from TK_Wrk_Step  where step_code=@stepcode and wo_code=@ordercode";
                    dynamicParams.Add("@stepcode", stepcode);
                    dynamicParams.Add("@ordercode", ordercode);
                    var data0 = DapperHelper.selectdata(sql, dynamicParams);
                    if (data0.Rows.Count <= 0)
                    {
                        mes.code = "300";
                        mes.count = 0;
                        mes.message = "当前工序任务不存在,无效条码!";
                        mes.data = null;
                        return mes;
                    }
                }
                if (partcode != "" && partcode != null)
                {
                    search += "and M.partcode like '%'+@partcode+'%' ";
                    dynamicParams.Add("@partcode", partcode);
                }
                if (partname != "" && partname != null)
                {
                    search += "and M.partname like '%'+@partname+'%' ";
                    dynamicParams.Add("@partname", partname);
                }
                if (partspec != "" && partspec != null)
                {
                    search += "and M.partspec like '%'+@partspec+'%' ";
                    dynamicParams.Add("@partspec", partspec);
                }
                //根据条件查询工单工序任务(自制工序)
                sql = @"select A.id,B.wkshp_code,T.torg_name as wkshp_name,A.wo_code,M.partcode,M.partname,M.partspec,A.seq,A.isend,
                        S.stepcode,S.stepname,S.descr,A.plan_quantity,A.plan_qty,A.good_qty,A.ng_qty,A.laborbad_qty,A.materielbad_qty,B.lm_date,
                        M.priuserdefnvc1,M.priuserdefnvc2,M.priuserdefnvc3,M.priuserdefnvc4,M.priuserdefnvc5,M.priuserdefnvc6
                        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
                        left join TOrganization T on B.wkshp_code=T.torg_code
                        where A.status in('ALLO','START') and A.ng_qty>0  " + search;
                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                mes.code = "200";
                mes.count = total;
                mes.message = "查询成功!";
                mes.data = data.ToList();
                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 MesOrderNgSubStepSearch(string wocode, string stepcode)
        {
            var sql = "";
            var dynamicParams = new DynamicParameters();
            var dynamicParams1 = new DynamicParameters();
            Dictionary<string, object> list = new Dictionary<string, object>();
            ScanStartReportData rt = new ScanStartReportData();
            try
            {
                //1.根据工单+工序查找当前工序是否首道工序
                sql = @"select A.wo_code,P.partcode,P.partname,P.partspec, T.stepcode,T.stepname,A.seq,T.flwtype,T.descr,A.status,
                        A.plan_quantity,A.plan_qty,A.good_qty,A.ng_qty,A.laborbad_qty,A.materielbad_qty,A.isbott,A.isend
                        from TK_Wrk_Step A
                        left join  TStep T on A.step_code=T.stepcode
                        left join  TK_Wrk_Man M on A.wo_code=M.wo_code
                        left join  TMateriel_Info P on M.materiel_code=P.partcode
                        where A.wo_code=@ordercode and A.step_code=@stepcode";
                dynamicParams.Add("@ordercode", wocode);
                dynamicParams.Add("@stepcode", stepcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    rt.wo_code = data.Rows[0]["wo_code"].ToString(); //工单号
                    rt.partnumber = data.Rows[0]["partcode"].ToString(); //产品编码
                    rt.partname = data.Rows[0]["partname"].ToString(); //产品名称
                    rt.partspec = data.Rows[0]["partspec"].ToString(); //产品规格
                    rt.stepcode = data.Rows[0]["stepcode"].ToString(); //工序编码
                    rt.stepname = data.Rows[0]["stepname"].ToString(); //工序名称
                    rt.stepdesc = data.Rows[0]["descr"].ToString(); //工序描述
                    rt.planqty = decimal.Parse(data.Rows[0]["plan_qty"].ToString()); //任务超产总数量
                    rt.planquantity= decimal.Parse(data.Rows[0]["plan_quantity"].ToString()); //任务数量
                    rt.noreportqty = decimal.Parse(data.Rows[0]["good_qty"].ToString()); //报工数量
                    rt.noputqty = decimal.Parse(data.Rows[0]["ng_qty"].ToString()); //不良数量
                    string isend = data.Rows[0]["isend"].ToString();//末道工序
                    rt.seq = data.Rows[0]["seq"].ToString();//工序序号
 
                    //获取当前工序下道工序
                    sql = @"select A.isbott,A.isend,T.stepcode,T.stepname from TK_Wrk_Step A
                            left join  TStep T on A.step_code=T.stepcode
                            where A.wo_code=@ordercode and A.seq=@seq+1 ";
                    dynamicParams.Add("@ordercode", wocode);
                    dynamicParams.Add("@seq", decimal.Parse(data.Rows[0]["seq"].ToString()));
                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                    if (dt0.Rows.Count > 0) //有下道工序
                    {
                        rt.nextstepcode = dt0.Rows[0]["stepcode"].ToString();//下道工序编码
                        rt.nextstepname = dt0.Rows[0]["stepname"].ToString();//下道工序名称
                    }
                    if (isend == "Y")  //当前工序是末道工序
                    {
                        rt.nextstepcode = "";//赋空
                        rt.nextstepname = "";//赋空
                    }
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.message = "当前工序任务不存在!";
                    mes.data = null;
                    return mes;
                }
                //根据条件查询工单工序报工(收料)记录,且不良数量大于0
                //存储过程名
                sql = @"h_p_IFCLD_MesReportDefectHandleSelect";
                dynamicParams1.Add("@ordercode", wocode);
                dynamicParams1.Add("@stepcode", stepcode);
                DataTable dt = DapperHelper.selectProcedure(sql, dynamicParams1);
                if (dt.Rows.Count > 0)
                {
                    list.Add("data1", rt);
                    list.Add("data2", dt);
                    mes.code = "200";
                    mes.count = 0;
                    mes.message = "查询成功!";
                    mes.data = list;
                }
                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 MesOrderStepStart(string OperType, string SelectType, string wocode, string stepcode, User us)
        {
            var dynamicParams = new DynamicParameters();
            try
            {
                dynamic dynObj = JObject.Parse(us.mesSetting);
                bool isOrder = dynObj.isOrder;
                switch (OperType)
                {
                    case "ZZ":
                        if (isOrder) //按序生产
                        {
                            mes = ScanStartReport.ZZEncodingSeach(wocode, stepcode);
                        }
                        else //不按序生产
                        {
                            mes = ScanStartReport.NoZZEncodingSeach(wocode, stepcode);
                        }
                        break;
                    case "WX":
                        if (isOrder) //按序收发料
                        {
                            mes = ScanStartReport.WXEncodingSeach(SelectType, wocode, stepcode);
                        }
                        else //不按序收发料
                        {
                            mes = ScanStartReport.NoWXEncodingSeach(SelectType, wocode, stepcode);
                        } 
                        break;
                    default:
                        break;
                }
            }
            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 stepprice, string eqpcode, string inbarcode, string reckway, string usergroupcode, string reportuser,string payrate, string taskqty, string startqty, string reportqty, List<ReportDefectList> defectobjs, string remarks, User us)
        {
            var sql = "";
            decimal ngqty = 0;
            string[] arra1 = new string[] { };
            List<object> list0 = new List<object>();
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                string date = DateTime.Now.ToString(); //获取系统时间
                //获取报工人员、分配比例
                //JArray arra = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(reportuser);
                //判断是否有不良数据
                var groupedItems = defectobjs.GroupBy(item => item.defect_code) // 按编码code分组
                        .Select(group => new { defect_code = group.Key, badqty = group.Sum(item => Convert.ToDecimal(item.badqty)) }).ToList(); // 汇总不良数量
                if (groupedItems.Count <= 0)
                {
                    ngqty = 0;
                }
                else
                {
                    //汇总不良数量
                    ngqty = groupedItems.Sum(item => Convert.ToDecimal(item.badqty));
                }
                list.Clear();
                //判断是否有开工记录(无新增)
                sql = @"select *  from TK_Wrk_Record where wo_code=@wo_code and step_code=@step_code and style='S' and eqp_code=@eqpcode";
                dynamicParams.Add("@wo_code", mesordercode);
                dynamicParams.Add("@step_code", stepcode);
                dynamicParams.Add("@eqpcode", eqpcode);
                var data0 = DapperHelper.selectdata(sql, dynamicParams);
                if (data0.Rows.Count <= 0)
                {
                    //写入开报工记录表
                    sql = @"insert into  TK_Wrk_Record(wo_code,step_seq,step_code,step_price,eqp_code,materiel_code,open_person,open_time,task_qty,start_qty,style,lm_user,lm_date) 
                                values(@mesordercode,@stepseq,@stepcode,@step_price,@eqpcode,@partcode,@username,@opentime,@taskqty,@startqty,@style,@lm_user,@lm_date)";
                    list0.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepseq = stepseq, stepcode = stepcode, step_price = stepprice, eqpcode = eqpcode, partcode = partcode, username = us.usercode, opentime = date, taskqty = taskqty, startqty = startqty, style = "S", lm_user = us.usercode, lm_date = date } });
                    //回写工单工序表状态为START: 开工
                    sql = @"update TK_Wrk_Step set status='START'  where wo_code=@mesordercode and step_code=@stepcode";
                    list0.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode } });
 
                    //回写工单表状态为: 开工:START 
                    sql = @"update TK_Wrk_Man set status='START'  where wo_code=@mesordercode";
                    list0.Add(new { str = sql, parm = new { mesordercode = mesordercode } });
                    bool st = DapperHelper.DoTransaction(list0);
                    if (st)
                    {
                        //写入操作记录表
                        LogHelper.DbOperateLog(us.usercode, "开工", "开工了工单:" + mesordercode + "工序:" + stepcode, us.usertype);
                        mes.code = "200";
                        mes.count = 0;
                        mes.message = "操作成功!";
                        mes.data = null;
                    }
                    else
                    {
                        mes.code = "300";
                        mes.count = 0;
                        mes.message = "操作失败!";
                        mes.data = null;
                    }
                }
 
                //判断是否有报工记录(有:修改 无:新增)
                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);
                ////获取开工记录的默认选中的设备(产线)与报工时的设备产线做对比判断
                //sql = @"select A.eqp_code,B.name  from TK_Wrk_Record A
                //        inner join TEqpInfo B on A.eqp_code=B.code
                //        where A.wo_code=@wo_code and A.step_code=@step_code and eqp_code=@eqpcode  and A.style='S'";
                //dynamicParams.Add("@wo_code", mesordercode);
                //dynamicParams.Add("@step_code", stepcode);
                //dynamicParams.Add("@eqpcode", eqpcode);
                //var da = DapperHelper.selectdata(sql, dynamicParams);
                //if (da.Rows[0]["EQP_CODE"].ToString() != eqpcode)
                //{
                //    mes.code = "300";
                //    mes.count = 0;
                //    mes.message = "操作失败,当前报工产线应为:" + da.Rows[0]["NAME"].ToString() + "!";
                //    mes.data = null;
                //    return mes;
                //}
                if (data.Rows.Count > 0)
                {
                    //获取主表最大ID
                    sql = @"select ISNULL(IDENT_CURRENT('TK_Wrk_Record')+1,1) as id";
                    var dt = DapperHelper.selecttable(sql);
 
                    //写入开报工记录表
                    sql = @"insert into  TK_Wrk_Record(wo_code,step_seq,step_code,step_price,eqp_code,materiel_code,task_qty,start_qty,good_qty,ng_qty,style,lm_user,lm_date,inbarcode) 
                                values(@mesordercode,@stepseq,@stepcode,@step_price,@eqpcode,@partcode,@taskqty,@startqty,@reportqty,@ngqty,@style,@lm_user,@lm_date,@inbarcode)";
                    list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepseq = stepseq, stepcode = stepcode, step_price = stepprice, eqpcode = eqpcode, partcode = partcode, taskqty = taskqty, startqty = startqty, reportqty = reportqty, ngqty = ngqty, style = "B", lm_user = us.usercode, lm_date = date, inbarcode = inbarcode } });
 
                    //写入子表
                    sql = @"insert into  TK_Wrk_RecordSub(m_id,eqp_code,payrate,report_person,report_date,report_qty,reckway,usergroup_code,ratio,ng_qty,style,lm_user,lm_date) 
                                values(@m_id,@eqp_code,@payrate,@report_person,@report_date,@report_qty,@reckway,@usergroup_code,@ratio,@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,
                            payrate= payrate,
                            report_person = reportuser,
                            report_date = date,
                            report_qty = reportqty,
                            reckway = reckway,
                            usergroup_code = usergroupcode,
                            ratio = 0, //分配比例
                            ng_qty = ngqty,
                            style = "B",
                            lm_user = us.usercode,
                            lm_date = date
                        }
                    });
                    if (groupedItems.Count > 0)
                    {
                        //写入缺陷记录表
                        for (int i = 0; i < groupedItems.Count; i++)
                        {
                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_pendqty,defect_code,remarks,style,lm_user,lm_date) 
                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_pendqty,@defect_code,@remarks,@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 = groupedItems[i].badqty, defect_pendqty = groupedItems[i].badqty, defect_code = groupedItems[i].defect_code, remarks = remarks, style = "B", lm_user = us.usercode, lm_date = date } });
 
                        }
                    }
                }
                else
                {
                    //获取主表最大ID
                    sql = @"select ISNULL(IDENT_CURRENT('TK_Wrk_Record')+1,1) as id";
                    var dt = DapperHelper.selecttable(sql);
                    //写入开报工记录表
                    sql = @"insert into  TK_Wrk_Record(wo_code,step_seq,step_code,step_price,eqp_code,materiel_code,task_qty,start_qty,good_qty,ng_qty,style,lm_user,lm_date,inbarcode) 
                                values(@mesordercode,@stepseq,@stepcode,@step_price,@eqpcode,@partcode,@taskqty,@startqty,@reportqty,@ngqty,@style,@lm_user,@lm_date,@inbarcode)";
                    list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepseq = stepseq, stepcode = stepcode, step_price = stepprice, eqpcode = eqpcode, partcode = partcode, taskqty = taskqty, startqty = startqty, reportqty = reportqty, ngqty = ngqty, style = "B", lm_user = us.usercode, lm_date = date, inbarcode = inbarcode } });
 
                    //写入子表
                    sql = @"insert into  TK_Wrk_RecordSub(m_id,eqp_code,payrate,report_person,report_date,report_qty,reckway,usergroup_code,ratio,ng_qty,style,lm_user,lm_date) 
                                values(@m_id,@eqp_code,@payrate,@report_person,@report_date,@report_qty,@reckway,@usergroup_code,@ratio,@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,
                            payrate= payrate,
                            report_person = reportuser,
                            report_date = date,
                            report_qty = reportqty,
                            reckway = reckway,
                            usergroup_code = usergroupcode,
                            ratio = 0, //分配比例
                            ng_qty = ngqty,
                            style = "B",
                            lm_user = us.usercode,
                            lm_date = date
                        }
                    });
                    if (groupedItems.Count > 0)
                    {
                        //写入缺陷记录表
                        for (int i = 0; i < groupedItems.Count; i++)
                        {
                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_pendqty,defect_code,remarks,style,lm_user,lm_date) 
                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_pendqty,@defect_code,@remarks,@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 = groupedItems[i].badqty, defect_pendqty = groupedItems[i].badqty, defect_code = groupedItems[i].defect_code, remarks = remarks, style = "B", lm_user = us.usercode, 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 } });
 
                //写入ERP入库单
 
                //判断是否末道工序完工报工(自动关闭工单及工序任务)
                //list = AutosCloseOrder.AutosColseOrderReport(list,mesordercode, partcode, stepseq,stepcode,reportqty,ngqty);
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "报工", "报工了工单:" + mesordercode + "工序:" + stepcode, us.usertype);
                    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, User us)
        {
            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 = us.usercode, 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 = us.usercode, lm_date = date } });
                }
                else
                {
                    //获取主表最大ID
                    sql = @"select ISNULL(IDENT_CURRENT('TK_Wrk_OutRecord')+1,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 = us.usercode, 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 = us.usercode, 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)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "外协发料", "外协发料工单:" + mesordercode + "工序:" + stepcode, us.usertype);
                    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 inbarcode, string inuser, string taskqty, string sqty, List<ReportDefectList> defectobjs, string remarks, User us)
        {
            var sql = "";
            string[] arra1 = new string[] { };
            decimal ngqty = 0;
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                string date = DateTime.Now.ToString(); //获取系统时间
                //判断是否有不良数据
                var groupedItems = defectobjs.GroupBy(item => item.defect_code) // 按编码code分组
                        .Select(group => new { defect_code = group.Key, badqty = group.Sum(item => Convert.ToDecimal(item.badqty)) }).ToList(); // 汇总数量qty
                if (groupedItems.Count <= 0)
                {
                    ngqty = 0;
                }
                else
                {
                    //汇总不良数量
                    ngqty = groupedItems.Sum(item => Convert.ToDecimal(item.badqty));
                }
 
                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);
                //获取发料记录的默认选中的外协供应商与收料时的外协供应商做对比判断
                sql = @"select A.wx_code,B.name,A.fqty   from TK_Wrk_OutRecord A
                        inner join TCustomer B on A.wx_code=B.code
                        where A.wo_code=@wo_code and A.step_code=@step_code and wx_code=@wx_code and A.style='F' ";
                dynamicParams.Add("@wo_code", mesordercode);
                dynamicParams.Add("@step_code", stepcode);
                dynamicParams.Add("@wx_code", wxcode);
                var da = DapperHelper.selectdata(sql, dynamicParams);
                if (da.Rows.Count <= 0)
                {
                    sql = @"select A.wx_code,B.name,A.fqty   from TK_Wrk_OutRecord A
                        inner join TCustomer B on A.wx_code=B.code
                        where A.wo_code=@wo_code and A.step_code=@step_code and A.style='F' ";
                    dynamicParams.Add("@wo_code", mesordercode);
                    dynamicParams.Add("@step_code", stepcode);
                    var da1 = DapperHelper.selectdata(sql, dynamicParams);
                    var dr = da1.AsEnumerable().ToList().Select(x => x.Field<string>("NAME")).ToList();
                    string wxstring = (string.Join(",", dr.Select(x => x.ToString()).ToArray()));
                    mes.code = "300";
                    mes.count = 0;
                    mes.message = "操作失败,当前收料外协供方与发料外协供应商不匹配,应为:【" + wxstring + "】!";
                    mes.data = null;
                    return mes;
                }
                if ((decimal.Parse(sqty) + ngqty) > decimal.Parse(da.Rows[0]["FQTY"].ToString()))  //收料数量+不良数量>发料数量
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.message = "操作失败,当前收料数量+不良数量,不能大于发料数量:" + da.Rows[0]["FQTY"].ToString() + "!";
                    mes.data = null;
                    return mes;
                }
                //if (da.Rows[0]["WX_CODE"].ToString() != wxcode)
                //{
                //    mes.code = "300";
                //    mes.count = 0;
                //    mes.message = "操作失败,当前收料外协供方应为:" + da.Rows[0]["NAME"].ToString() + "!";
                //    mes.data = null;
                //    return mes;
                //}
                if (data.Rows.Count > 0)
                {
                    decimal sum_sqty = data.AsEnumerable().Select(d => d.Field<decimal>("SQTY")).Sum();  //获取同单号,同工序,同外协供应商收料总数量
                    decimal sum_fqty = da.AsEnumerable().Select(d => d.Field<decimal>("FQTY")).Sum();  //获取同单号,同工序,同外协供应商发料总数量
                    if ((sum_sqty + decimal.Parse(sqty) + ngqty) > sum_fqty) //已收料总数+当前收料数量+不良数量>总发料数量
                    {
                        mes.code = "300";
                        mes.count = 0;
                        mes.message = "操作失败,当前收料数量+不良数量,不能大于待收数量:" + (sum_fqty - sum_sqty) + "!";
                        mes.data = null;
                        return mes;
                    }
 
 
                    //获取主表最大ID
                    sql = @"select ISNULL(IDENT_CURRENT('TK_Wrk_OutRecord')+1,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,inbarcode) 
                                values(@mesordercode,@stepseq,@stepcode,@wx_code,@partcode,@style,@sqty,@ngqty,@lm_user,@lm_date,@inbarcode)";
                    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 = us.usercode, lm_date = date, inbarcode = inbarcode } });
 
                    //写入外协记录子表
                    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 = us.usercode, lm_date = date } });
 
                    if (groupedItems.Count > 0)
                    {
                        //写入缺陷记录表
                        for (int i = 0; i < groupedItems.Count; i++)
                        {
                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_pendqty,defect_code,remarks,style,lm_user,lm_date) 
                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_pendqty,@defect_code,@remarks,@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 = groupedItems[i].badqty, defect_pendqty = groupedItems[i].badqty, defect_code = groupedItems[i].defect_code, remarks = remarks, style = "S", lm_user = us.usercode, lm_date = date } });
 
                        }
                    }
                }
                else
                {
                    //获取主表最大ID
                    sql = @"select ISNULL(IDENT_CURRENT('TK_Wrk_OutRecord')+1,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,inbarcode) 
                                values(@mesordercode,@stepseq,@stepcode,@wx_code,@partcode,@style,@sqty,@ngqty,@lm_user,@lm_date,@inbarcode)";
                    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 = us.usercode, lm_date = date, inbarcode = inbarcode } });
 
                    //写入外协记录子表
                    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 = us.usercode, lm_date = date } });
 
                    if (groupedItems.Count > 0)
                    {
                        //写入缺陷记录表
                        for (int i = 0; i < groupedItems.Count; i++)
                        {
                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_pendqty,defect_code,remarks,style,lm_user,lm_date) 
                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_pendqty,@defect_code,@remarks,@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 = groupedItems[i].badqty, defect_pendqty = groupedItems[i].badqty, defect_code = groupedItems[i].defect_code, remarks = remarks, style = "S", lm_user = us.usercode, 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 } });
 
                ////写入ERP入库单
 
                //判断是否末道工序完工报工(自动关闭工单及工序任务)
                //list = AutosCloseOrder.AutosColseOrderReport(list, mesordercode, partcode, stepseq, stepcode, sqty, ngqty);
 
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "外协收料", "外协收料工单:" + mesordercode + "工序:" + stepcode, us.usertype);
                    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 EditOrderNgStepSeave(ReportDefectHandle json, User us)
        {
            var sql = "";
            string[] arra1 = new string[] { };
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            decimal sumrepair_qty = 0, sumlaborbad_qty = 0, summaterielbad_qty = 0;  //累计维修数量、累计工废数量、累计料废数量
            try
            {
                string date = DateTime.Now.ToString(); //获取系统时间
                list.Clear();
 
 
                //循环json数据
                for (int i = 0; i < json.Data.Rows.Count; i++)
                {
                    //自制工序
                    if (json.Data.Rows[i]["STYLE"].ToString() == "Z")
                    {
                        //回写对应的报工记录子表合格数量、不良数量、工废数量、料废数量
                        sql = @"update TK_Wrk_RecordSub set report_qty=report_qty+@repair_qty,ng_qty=ng_qty-@repair_qty-@laborbad_qty-@materielbad_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                                where  m_id=@m_id and style='B'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                m_id = int.Parse(json.Data.Rows[i]["M_ID"].ToString()),
                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
                                laborbad_qty = decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString()),
                                materielbad_qty = decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString())
                            }
                        });
                        //回写对应的报工记录主表合格数量、不良数量、报废数量
                        sql = @"update TK_Wrk_Record set good_qty=good_qty+@repair_qty,ng_qty=ng_qty-@repair_qty-@laborbad_qty-@materielbad_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                        where wo_code=@wo_code and step_code=@step_code and id=@id and style='B'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
                                laborbad_qty = decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString()),
                                materielbad_qty = decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString()),
                                wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
                                step_code = json.Data.Rows[i]["STEP_CODE"].ToString(),
                                id = int.Parse(json.Data.Rows[i]["M_ID"].ToString())
                            }
                        });
                        //回写缺陷记录表的待处理数量
                        sql = @"update CSR_WorkRecord_Defect set defect_qty=defect_qty-@repair_qty-@laborbad_qty-@materielbad_qty, defect_pendqty=defect_pendqty-@repair_qty-@laborbad_qty-@materielbad_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                        where wo_code=@wo_code and step_code=@step_code and id=@id and style='B'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
                                laborbad_qty = decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString()),
                                materielbad_qty = decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString()),
                                wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
                                step_code = json.Data.Rows[i]["STEP_CODE"].ToString(),
                                id = int.Parse(json.Data.Rows[i]["ID"].ToString())
                            }
                        });
                        //判断缺陷记录处理表是否存在记录
                        sql = @"select *  from CSR_WorkRecord_DefectHandle where wo_code=@wo_code and step_code=@step_code and  defect_id=@defect_id";
                        dynamicParams.Add("@wo_code", json.Data.Rows[i]["WO_CODE"].ToString());
                        dynamicParams.Add("@step_code", json.Data.Rows[i]["STEP_CODE"].ToString());
                        dynamicParams.Add("@defect_id", json.Data.Rows[i]["ID"].ToString());
                        var data = DapperHelper.selectdata(sql, dynamicParams);
                        if (data.Rows.Count <= 0)
                        {
                            //写入报工缺陷处理记录表
                            sql = @"insert into  CSR_WorkRecord_DefectHandle(defect_id,wo_code,partnumber,step_seq,step_code,repair_qty,laborbad_qty,materielbad_qty,defect_code,style,lm_user,lm_date) 
                                values(@defect_id,@wo_code,@partcode,@stepseq,@stepcode,@repair_qty,@laborbad_qty,@materielbad_qty,@defect_code,@style,@lm_user,@lm_date)";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    defect_id = int.Parse(json.Data.Rows[i]["ID"].ToString()),
                                    wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
                                    partcode = json.Data.Rows[i]["MATERIEL_CODE"].ToString(),
                                    stepseq = json.Data.Rows[i]["SEQ"].ToString(),
                                    stepcode = json.Data.Rows[i]["STEP_CODE"].ToString(),
                                    repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
                                    laborbad_qty = decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString()),
                                    materielbad_qty = decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString()),
                                    defect_code = json.Data.Rows[i]["DEFECT_CODE"].ToString(),
                                    style = "B",
                                    lm_user = us.usercode,
                                    lm_date = date
                                }
                            });
                        }
                        else
                        {
                            //更新报工缺陷处理记录表
                            sql = @"update CSR_WorkRecord_DefectHandle set repair_qty=repair_qty+@repair_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                                    where wo_code=@wo_code and step_code=@step_code and defect_id=@defect_id";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    defect_id = int.Parse(json.Data.Rows[i]["ID"].ToString()),
                                    wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
                                    step_code = json.Data.Rows[i]["STEP_CODE"].ToString(),
                                    repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
                                    laborbad_qty = decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString()),
                                    materielbad_qty = decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString())
                                }
                            });
                        }
 
                        sumrepair_qty = sumrepair_qty + decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString());
                        sumlaborbad_qty = sumlaborbad_qty + decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString());
                        summaterielbad_qty = summaterielbad_qty + decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString());
                    }
                    //外协工序
                    if (json.Data.Rows[i]["STYLE"].ToString() == "S")
                    {
                        //回写对应的收料记录子表收料数量、不良数量、报废数量
                        sql = @"update TK_Wrk_OutRecordSub set sqty=sqty+@repair_qty,ng_qty=ng_qty-@repair_qty-@laborbad_qty-@materielbad_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                                where  m_id=@m_id and style='S'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                m_id = int.Parse(json.Data.Rows[i]["M_ID"].ToString()),
                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
                                laborbad_qty = decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString()),
                                materielbad_qty = decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString())
                            }
                        });
                        //回写对应的收料记录主表合格数量、不良数量、报废数量
                        sql = @"update TK_Wrk_OutRecord set sqty=sqty+@repair_qty,ng_qty=ng_qty-@repair_qty-@laborbad_qty-@materielbad_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                        where wo_code=@wo_code and step_code=@step_code and id=@id and style='S'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
                                laborbad_qty = decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString()),
                                materielbad_qty = decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString()),
                                wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
                                step_code = json.Data.Rows[i]["STEP_CODE"].ToString(),
                                id = int.Parse(json.Data.Rows[i]["M_ID"].ToString())
                            }
                        });
                        //回写缺陷记录表的待处理数量
                        sql = @"update CSR_WorkRecord_Defect set defect_qty=defect_qty-@repair_qty-@laborbad_qty-@materielbad_qty,defect_pendqty=defect_pendqty-@repair_qty-@laborbad_qty-@materielbad_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                        where wo_code=@wo_code and step_code=@step_code and id=@id and style='S'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
                                laborbad_qty = decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString()),
                                materielbad_qty = decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString()),
                                wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
                                step_code = json.Data.Rows[i]["STEP_CODE"].ToString(),
                                id = int.Parse(json.Data.Rows[i]["ID"].ToString())
                            }
                        });
                        //判断缺陷记录处理表是否存在记录
                        sql = @"select *  from CSR_WorkRecord_DefectHandle where wo_code=@wo_code and step_code=@step_code and  defect_id=@defect_id";
                        dynamicParams.Add("@wo_code", json.Data.Rows[i]["WO_CODE"].ToString());
                        dynamicParams.Add("@step_code", json.Data.Rows[i]["STEP_CODE"].ToString());
                        dynamicParams.Add("@defect_id", json.Data.Rows[i]["ID"].ToString());
                        var data = DapperHelper.selectdata(sql, dynamicParams);
                        if (data.Rows.Count <= 0)
                        {
                            //写入报工缺陷处理记录表
                            sql = @"insert into  CSR_WorkRecord_DefectHandle(defect_id,wo_code,partnumber,step_seq,step_code,repair_qty,laborbad_qty,materielbad_qty,defect_code,style,lm_user,lm_date) 
                                values(@defect_id,@wo_code,@partcode,@stepseq,@stepcode,@repair_qty,@laborbad_qty,@materielbad_qty,@defect_code,@style,@lm_user,@lm_date)";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    defect_id = int.Parse(json.Data.Rows[i]["ID"].ToString()),
                                    wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
                                    partcode = json.Data.Rows[i]["MATERIEL_CODE"].ToString(),
                                    stepseq = json.Data.Rows[i]["SEQ"].ToString(),
                                    stepcode = json.Data.Rows[i]["STEP_CODE"].ToString(),
                                    repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
                                    laborbad_qty = decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString()),
                                    materielbad_qty = decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString()),
                                    defect_code = json.Data.Rows[i]["DEFECT_CODE"].ToString(),
                                    style = "S",
                                    lm_user = us.usercode,
                                    lm_date = date
                                }
                            });
                        }
                        else
                        {
                            //更新报工缺陷处理记录表
                            sql = @"update CSR_WorkRecord_DefectHandle set repair_qty=repair_qty+@repair_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                                    where wo_code=@wo_code and step_code=@step_code and defect_id=@defect_id";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    defect_id = int.Parse(json.Data.Rows[i]["ID"].ToString()),
                                    wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
                                    step_code = json.Data.Rows[i]["STEP_CODE"].ToString(),
                                    repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
                                    laborbad_qty = decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString()),
                                    materielbad_qty = decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString())
                                }
                            });
                        }
                        sumrepair_qty = sumrepair_qty + decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString());
                        sumlaborbad_qty = sumlaborbad_qty + decimal.Parse(json.Data.Rows[i]["LABORBAD_QTY"].ToString());
                        summaterielbad_qty = summaterielbad_qty + decimal.Parse(json.Data.Rows[i]["MATERIELBAD_QTY"].ToString());
                    }
                }
 
                //回写工单工序表合格数量、不良数量
                sql = @"update TK_Wrk_Step set good_qty=good_qty+@sumrepair_qty,ng_qty=ng_qty-@sumrepair_qty-@sumlaborbad_qty-@summaterielbad_qty,laborbad_qty=laborbad_qty+@sumlaborbad_qty,materielbad_qty=materielbad_qty+@summaterielbad_qty  
                        where wo_code=@wo_code and step_code=@stepcode";
                list.Add(new
                {
                    str = sql,
                    parm = new
                    {
                        wo_code = json.Data.Rows[0]["WO_CODE"].ToString(),
                        stepcode = json.Data.Rows[0]["STEP_CODE"].ToString(),
                        sumrepair_qty = sumrepair_qty,
                        sumlaborbad_qty = sumlaborbad_qty,
                        summaterielbad_qty = summaterielbad_qty
                    }
                });
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "不良处理", "工单:" + json.Data.Rows[0]["WO_CODE"].ToString() + "工序:" + json.Data.Rows[0]["STEP_CODE"].ToString(), us.usertype);
                    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 MesOrderStepVerifySearch(string wkshopcode, string wo_code, string partnumber, string partname, string partspec,string stepcode, string reportuser, string reportdateopendate, string reportdateclosedate, int startNum, int endNum, string prop, string order)
        {
            var dynamicParams = new DynamicParameters();
            string search = "";
            try
            {
                if (wkshopcode != "" && wkshopcode != null)
                {
                    search += "and AA.wkshp_code=@wkshopcode ";
                    dynamicParams.Add("@wkshopcode", wkshopcode);
                }
                if (wo_code != "" && wo_code != null)
                {
                    search += "and AA.wo_code like '%'+@wo_code+'%' ";
                    dynamicParams.Add("@wo_code", wo_code);
                }
                if (partnumber != "" && partnumber != null)
                {
                    search += "and AA.partnumber like '%'+@partnumber+'%' ";
                    dynamicParams.Add("@partnumber", partnumber);
                }
                if (partname != "" && partname != null)
                {
                    search += "and AA.partname like '%'+@partname+'%' ";
                    dynamicParams.Add("@partname", partname);
                }
                if (partspec != "" && partspec != null)
                {
                    search += "and AA.partspec like '%'+@partspec+'%' ";
                    dynamicParams.Add("@partspec", partspec);
                }
                if (stepcode != "" && stepcode != null)
                {
                    search += "and AA.step_code=@stepcode ";
                    dynamicParams.Add("@stepcode", stepcode);
                }
                if (reportuser != "" && reportuser != null)
                {
                    search += "and AA.usercode like '%'+@reportuser+'%' ";
                    dynamicParams.Add("@reportuser", reportuser);
                }
                if (reportdateopendate != "" && reportdateopendate != null)
                {
                    search += "and AA.report_date between @reportdateopendate and @reportdateclosedate ";
                    dynamicParams.Add("@reportdateopendate", reportdateopendate + " 00:00:00");
                    dynamicParams.Add("@reportdateclosedate", reportdateclosedate + " 23:59:59");
                }
 
 
                if (search == "")
                {
                    search = "and 1=1 ";
                }
                search = search.Substring(3);//截取索引2后面的字符
                // --------------查询指定自制报工外协收料数据--------------
                var total = 0; //总条数
                var sql = @"select *  from(
                            select A.id,B.id as sbid,A.wo_code,A.materiel_code as partnumber,P.partname,P.partspec,K.plan_quantity,K.plan_qty as task_qty,M.wkshp_code,T.torg_name as wkshp_name,A.eqp_code,E.name as eqp_name,
                            A.step_seq,A.step_code,S.stepname,S.flwtype as steptype,A.style,k.isbott as first_choke,k.isend as last_choke,A.step_price,B.reckway,B.usergroup_code,G.usergroupname as usergroup_name,
                            B.report_person as usercode,
                             STUFF((SELECT ',' + U.username
                                    FROM TUser U
                                    WHERE CHARINDEX(',' + U.usercode + ',', ',' + B.report_person + ',') > 0
                                    FOR XML PATH('')), 1, 1, '') AS username,
                            B.report_date,B.report_qty,B.ng_qty,B.laborbad_qty,B.materielbad_qty,'' as wx_code,'' as wx_name
                            from TK_Wrk_Record A
                            inner join TK_Wrk_RecordSub B on A.id=B.m_id
                            left join TK_Wrk_Man M on A.wo_code=M.wo_code
                            left join TK_Wrk_Step K on M.wo_code=K.wo_code and A.step_code=K.step_code
                            left join TStep S on A.step_code=S.stepcode
                            left join TMateriel_Info P on A.materiel_code=P.partcode
                            left join TOrganization T on M.wkshp_code=T.torg_code
                            left join TEqpInfo E on A.eqp_code=E.code
                            left join TGroup G on G.usergroupcode=B.usergroup_code
                            where A.style='B' and B.style='B' and M.status<>'CLOSED' and A.verify='N'
                            union all
                            select A.id,B.id as sbid,A.wo_code,A.materiel_code as partnumber,P.partname,P.partspec,K.plan_quantity,K.plan_qty as task_qty,M.wkshp_code,T.torg_name as wkshp_name,A.wx_code as eqp_code,E.name as eqp_name,
                            A.step_seq,A.step_code,S.stepname,S.flwtype as steptype,A.style,k.isbott as first_choke,k.isend as last_choke,A.step_price,'person' as reckway,'' as usergroup_code,'' as usergroup_name,
                            B.in_person as usercode,
                            STUFF((SELECT ',' + U.username
                                    FROM TUser U
                                    WHERE CHARINDEX(',' + U.usercode + ',', ',' + B.in_person + ',') > 0
                                    FOR XML PATH('')), 1, 1, '') AS username,
                            B.in_time as report_date,B.sqty as report_qty,B.ng_qty,B.laborbad_qty,B.materielbad_qty,A.wx_code,C.name as wx_name
                            from TK_Wrk_OutRecord A
                            inner join TK_Wrk_OutRecordSub B on A.id = B.m_id
                            left join TK_Wrk_Man M on A.wo_code = M.wo_code
                            left join TK_Wrk_Step K on M.wo_code=K.wo_code and A.step_code=K.step_code
                            left join TStep S on A.step_code = S.stepcode
                            left join TMateriel_Info P on A.materiel_code = P.partcode
                            left join TOrganization T on M.wkshp_code = T.torg_code
                            left join TCustomer E on A.wx_code = E.code 
                            left join TCustomer C on A.wx_code=C.code
                            where A.style = 'S' and B.style = 'S' and M.status<>'CLOSED' and A.verify='N'
                            ) as AA where" + 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 MesOrderStepModelSearch(string wo_code, string step_code, string step_type, string isbott, string isend, string id, string sbid)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                if (step_type == "Z")
                {
                    sql = @"select D.id,D.record_id,D.defect_code,
                            STUFF((SELECT ',' + F.name
                                    FROM TDefect F
                                    WHERE CHARINDEX(',' + F.code + ',', ',' + D.defect_code + ',') > 0
                                    FOR XML PATH('')), 1, 1, '') AS defect_name,
                            D.defect_qty,laborbad_qty,materielbad_qty   
                            from CSR_WorkRecord_Defect D  
                            where wo_code=@wo_code and step_code=@step_code and record_id=@id";
                    dynamicParams.Add("@wo_code", wo_code);
                    dynamicParams.Add("@step_code", step_code);
                    dynamicParams.Add("@id", id);
                    var data = DapperHelper.selectdata(sql, dynamicParams);
                    mes.code = "200";
                    mes.count = 0;
                    mes.message = "查询成功!";
                    mes.data = data;
 
                }
                if (step_type == "W")
                {
                    sql = @"select D.id,D.record_id,D.defect_code,
                            STUFF((SELECT ',' + F.name
                                    FROM TDefect F
                                    WHERE CHARINDEX(',' + F.code + ',', ',' + D.defect_code + ',') > 0
                                    FOR XML PATH('')), 1, 1, '') AS defect_name,
                            D.defect_qty,laborbad_qty,materielbad_qty   
                            from CSR_WorkRecord_Defect D  
                            where wo_code=@wo_code and step_code=@step_code and record_id=@id";
                    dynamicParams.Add("@wo_code", wo_code);
                    dynamicParams.Add("@step_code", step_code);
                    dynamicParams.Add("@id", id);
                    var data = DapperHelper.selectdata(sql, dynamicParams);
                    mes.code = "200";
                    mes.count = 0;
                    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 MesOrderStepUpdateSeave(User us, List<UpdateProductReport> json)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                dynamic dynObj = JObject.Parse(us.mesSetting);
                bool isOrder = dynObj.isOrder;
                //获取此次报工调整提交的数据(合格数量、不良数量、工废数量、料废数量)
                decimal this_reportqty = decimal.Parse(json[0].report_qty.ToString()); //报工数量(合格)
                decimal this_ngqty = json[0].children.Sum(item => decimal.Parse(item.ng_qty));//不良数量汇总
                decimal this_laborbadqty = json[0].children.Sum(item => decimal.Parse(item.laborbad_qty));//工废数量汇总
                decimal this_materielbadqty = json[0].children.Sum(item => decimal.Parse(item.materielbad_qty));//料废数量汇总
                decimal this_ng_dvalue = json[0].children.Sum(item => decimal.Parse(item.ng_dvalue));//不良数量差值汇总
                decimal this_laborbad_dvalue = json[0].children.Sum(item => decimal.Parse(item.laborbad_dvalue));//工废数量差值汇总
                decimal this_materielbad_dvalue = json[0].children.Sum(item => decimal.Parse(item.materielbad_dvalue));//料废数量差值汇总
                string date = DateTime.Now.ToString(); //获取系统时间
                if (isOrder) //按序
                {
                    //控制逻辑:首道工序调整-> 本道工序当前调整总数(合格+不良+报废)+本道工序非当前报工总数(合格+不良+报废)>任务数量   ==不能大于任务数量
                    //控制逻辑:首道工序调整-> (本道工序当前调整合格数+本道工序非当前报工合格总数)<下道工序报工总数(合格+不良+报废)   ==不能小于下道报工总数
                    //控制逻辑:末道工序调整-> 本道工序当前调整总数(合格+不良+报废)+本道工序非当前报工总数(合格+不良+报废)>上道工序报工合格总数   ==不能大于上道工序报工合格总数
                    //控制逻辑:中间工序调整-> 本道工序当前调整总数(合格+不良+报废)+本道工序非当前报工总数(合格+不良+报废)>上道工序报工合格总数   ==不能大于上道工序报工合格总数
                    //控制逻辑:中间工序调整-> (本道工序当前调整合格数+本道工序非当前报工合格总数)<下道工序报工总数(合格+不良+报废)   ==不能小于下道报工总数
 
 
                    list.Clear();
                    //获取当前工序上道工序及属性
                    sql = @"select T.stepcode,T.stepname,T.flwtype from TK_Wrk_Step A
                                left join  TStep T on A.step_code=T.stepcode
                                where A.wo_code=@ordercode and A.seq=@seq-1 ";
                    dynamicParams.Add("@ordercode", json[0].wo_code);
                    dynamicParams.Add("@seq", json[0].step_seq);
                    var pre = DapperHelper.selectdata(sql, dynamicParams);
                    //获取当前工序下道工序及属性
                    sql = @"select T.stepcode,T.stepname,T.flwtype from TK_Wrk_Step A
                                left join  TStep T on A.step_code=T.stepcode
                                where A.wo_code=@ordercode and A.seq=@seq+1 ";
                    dynamicParams.Add("@ordercode", json[0].wo_code);
                    dynamicParams.Add("@seq", json[0].step_seq);
                    var next = DapperHelper.selectdata(sql, dynamicParams);
                    //判断当前工序是自制工序还是外协工序
                    if (json[0].flw_type.ToString() == "Z")//自制工序
                    {
                        //首道工序的报工
                        if (json[0].first_choke == "Y")
                        {
                            //查询当前首道报工工序非此次报工:总报工数量、总不良数量、总工废数量、总料废数量
                            sql = @"select isnull(sum(good_qty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                from TK_Wrk_Record where wo_code=@wo_code and style='B' and id<>@id and step_code=@step_code";
                            dynamicParams.Add("@wo_code", json[0].wo_code);
                            dynamicParams.Add("@id", json[0].id);
                            dynamicParams.Add("@step_code", json[0].step_code);
                            var dt = DapperHelper.selectdata(sql, dynamicParams);
                            decimal notthis_reportqty = decimal.Parse(dt.Rows[0]["good_qty"].ToString());  //当前工序非本次报工总数
                            decimal notthis_ngqty = decimal.Parse(dt.Rows[0]["ng_qty"].ToString());  //当前工序非本次报工总不良数
                            decimal notthis_laborbadqty = decimal.Parse(dt.Rows[0]["laborbad_qty"].ToString());  //当前工序非本次报工总工废数
                            decimal notthis_materielbadqty = decimal.Parse(dt.Rows[0]["materielbad_qty"].ToString());  //当前工序非本次报工总料废数
                            //判断:当前工序报工记录:本次报工数量+本次不良数量+本次工废数量+本次料废数量+当前工序非本次报工总数+当前工序非本次不良总数+当前工序非本次工废总数+当前工序非本次料废总数>工单任务数量
                            decimal updatereportsumqty = this_reportqty + this_ngqty + this_laborbadqty + this_materielbadqty + notthis_reportqty + notthis_ngqty + notthis_laborbadqty + notthis_materielbadqty;
                            if (updatereportsumqty > decimal.Parse(json[0].task_qty.ToString()))
                            {
                                mes.code = "300";
                                mes.count = 0;
                                mes.message = "当前首道工序修改报工总数量:【" + updatereportsumqty + "】不能大于任务超产总数量:【" + json[0].task_qty.ToString() + "】!";
                                mes.data = null;
                                return mes;
                            }
                            //判断是否存在下道工序及属性
                            if (next.Rows.Count > 0)
                            {
                                if (next.Rows[0]["flwtype"].ToString() == "Z")
                                {
                                    //查询当前工序下道工序:总报工数量、总不良数量、总报废数量
                                    sql = @"select isnull(sum(good_qty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                    from TK_Wrk_Record where wo_code=@wo_code and style='B'  and step_seq=@step_seq+1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        //判断当前工序:报工总数数量+不良总数数量+工废总数量+料废总数量<下道工序报工总数量+下道工序不良总数量+下道工序工废总数量+下道工序料废总数量
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //下道工序报工总数量
                                        decimal last_ngqty = decimal.Parse(dt0.Rows[0]["ng_qty"].ToString());  //下道工序不良总数量
                                        decimal last_laborbad_qty = decimal.Parse(dt0.Rows[0]["laborbad_qty"].ToString());  //下道工序工废总数量
                                        decimal last_materielbad_qty = decimal.Parse(dt0.Rows[0]["materielbad_qty"].ToString());  //下道工序料废总数量
                                        decimal last_updatereportsumqty = last_reportqty + last_ngqty + last_laborbad_qty + last_materielbad_qty;
                                        //判断(当前非本次报工总合格数+本次报工调整合格数)<下道工序报工总数
                                        if ((notthis_reportqty + this_reportqty) < last_updatereportsumqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前首道工序修改报工总合格数量:【" + (notthis_reportqty + this_reportqty) + "】不能小于下道自制工序报工总数量:【" + last_updatereportsumqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                                else
                                {
                                    //查询当前工序下道工序:总发料数量
                                    sql = @"select isnull(sum(fqty),0) as good_qty
                                    from TK_Wrk_OutRecord where wo_code=@wo_code and style='S'  and step_seq=@step_seq+1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        //判断当前工序:发料总数数量
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //下道工序发料总数量
                                                                                                                     //判断(当前非本次报工总合格数+本次报工调整合格数)<下道工序发料总数
                                        if ((notthis_reportqty + this_reportqty) < last_reportqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前首道工序修改报工总合格数量:【" + (notthis_reportqty + this_reportqty) + "】不能小于下道外协工序发料总数量:【" + last_reportqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                            }
 
                        }
                        //末道工序的报工
                        else if (json[0].last_choke == "Y")
                        {
                            //查询当前末道报工工序非此次报工:总报工数量、总不良数量、总工废数量、总料废数量
                            sql = @"select isnull(sum(good_qty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                from TK_Wrk_Record where wo_code=@wo_code and style='B' and id<>@id and step_code=@step_code";
                            dynamicParams.Add("@wo_code", json[0].wo_code);
                            dynamicParams.Add("@id", json[0].id);
                            dynamicParams.Add("@step_code", json[0].step_code);
                            var dt = DapperHelper.selectdata(sql, dynamicParams);
                            decimal notthis_reportqty = decimal.Parse(dt.Rows[0]["good_qty"].ToString());  //当前末道工序非本次报工总数
                            decimal notthis_ngqty = decimal.Parse(dt.Rows[0]["ng_qty"].ToString());  //当前末道工序非本次报工总数
                            decimal notthis_laborbad_qty = decimal.Parse(dt.Rows[0]["laborbad_qty"].ToString());  //当前末道工序非本次报工工废总数
                            decimal notthis_materielbad_qty = decimal.Parse(dt.Rows[0]["materielbad_qty"].ToString());  //当前末道工序非本次报工料废总数
                                                                                                                        //获取当前末道工序报工总数量:本次修改报工数量+本次修改不良数量+本次修改工废数量+本次修改报工料废数量+当前末道工序非本次报工总数+当前末道工序非本次不良总数+当前末道工序非本次工废总数+当前末道工序非本次料废总数
                            decimal updatereportsumqty = this_reportqty + this_ngqty + this_laborbadqty + this_materielbadqty + notthis_reportqty + notthis_ngqty + notthis_laborbad_qty + notthis_materielbad_qty;
 
                            //判断是否存在上道工序及属性
                            if (pre.Rows.Count > 0)
                            {
                                if (pre.Rows[0]["flwtype"].ToString() == "Z")
                                {
                                    //查询当前末道工序上道工序:总报工数量、总不良数量、总报废数量
                                    sql = @"select isnull(sum(good_qty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                    from TK_Wrk_Record where wo_code=@wo_code and style='B'  and step_seq=@step_seq-1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //上道工序报工总数量
                                        decimal last_ngqty = decimal.Parse(dt0.Rows[0]["ng_qty"].ToString());  //上道工序不良总数量
                                        decimal last_laborbad_qty = decimal.Parse(dt0.Rows[0]["laborbad_qty"].ToString());  //上道工序工废总数量
                                        decimal last_materielbad_qty = decimal.Parse(dt0.Rows[0]["materielbad_qty"].ToString());  //上道工序料废总数量
                                        decimal last_updatereportsumqty = last_reportqty + last_ngqty + last_laborbad_qty + last_materielbad_qty;
                                        //判断:当前末道工序报工记录:当前末道工序报工总数量>上道工序报工合格总数
                                        if (updatereportsumqty > last_reportqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前末道工序修改报工总数量:【" + updatereportsumqty + "】不能大于上道工序报工总合格数量:【" + last_reportqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                                else
                                {
                                    //查询当前工序上道工序:总收料数量
                                    sql = @"select isnull(sum(sqty),0) as good_qty
                                    from TK_Wrk_OutRecord where wo_code=@wo_code and style='S'  and step_seq=@step_seq-1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        //判断当前工序:收料总数数量
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //下道工序发料总数量
                                                                                                                     //判断:当前末道工序报工记录:当前末道工序报工总数量>上道工序收料合格总数
                                        if (updatereportsumqty > last_reportqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前末道工序修改报工总数量:【" + updatereportsumqty + "】不能大于上道工序收料总合格数量:【" + last_reportqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                            }
                        }
                        else //中间工序的报工
                        {
                            //查询当前中间报工工序非此次报工:总报工数量、总不良数量、总报废数量
                            sql = @"select isnull(sum(good_qty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                    from TK_Wrk_Record where wo_code=@wo_code and style='B' and id<>@id and step_code=@step_code";
                            dynamicParams.Add("@wo_code", json[0].wo_code);
                            dynamicParams.Add("@id", json[0].id);
                            dynamicParams.Add("@step_code", json[0].step_code);
                            var dt = DapperHelper.selectdata(sql, dynamicParams);
                            decimal notthis_reportqty = decimal.Parse(dt.Rows[0]["good_qty"].ToString());  //当前工序非本次报工总数
                            decimal notthis_ngqty = decimal.Parse(dt.Rows[0]["ng_qty"].ToString());  //当前工序非本次报工总数
                            decimal notthis_laborbad_qty = decimal.Parse(dt.Rows[0]["laborbad_qty"].ToString());  //当前工序非本次报工工费总数
                            decimal notthis_materielbad_qty = decimal.Parse(dt.Rows[0]["materielbad_qty"].ToString());  //当前工序非本次报工料废总数
                                                                                                                        //获取当前中间工序报工总数量:本次修改报工数量+本次修改不良数量+本次修改工废数量+本次修改料废总数+当前工序非本次报工总数+当前工序非本次不良总数+当前工序非本次工废总数+当前工序非本次料废总数
                            decimal updatereportsumqty = this_reportqty + this_ngqty + this_laborbadqty + this_materielbadqty + notthis_reportqty + notthis_ngqty + notthis_laborbad_qty + notthis_materielbad_qty;
 
                            //判断是否存在上道工序及属性
                            if (pre.Rows.Count > 0)
                            {
                                if (pre.Rows[0]["flwtype"].ToString() == "Z")
                                {
                                    //查询当前工序上道工序:总报工数量、总不良数量、总工废数量、总料废数量
                                    sql = @"select isnull(sum(good_qty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                    from TK_Wrk_Record where wo_code=@wo_code and style='B'  and step_seq=@step_seq-1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //上道工序报工总合格数量
                                        decimal last_ngqty = decimal.Parse(dt0.Rows[0]["ng_qty"].ToString());  //上道工序不良总数量
                                        decimal last_laborbad_qty = decimal.Parse(dt0.Rows[0]["laborbad_qty"].ToString());  //上道工序工废总数量
                                        decimal last_materielbad_qty = decimal.Parse(dt0.Rows[0]["materielbad_qty"].ToString());//上道工序料废总数量
                                        decimal last_updatereportsumqty = last_reportqty + last_ngqty + last_laborbad_qty + last_materielbad_qty;
                                        //判断:当前工序报工记录:当前工序报工总数>上道工序报工总合格数
                                        if (updatereportsumqty > last_reportqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前工序修改报工总数量:【" + updatereportsumqty + "】不能大于上道工序报工总合格数量:【" + last_reportqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                                else
                                {
                                    //查询当前工序上道工序:总收料数量
                                    sql = @"select isnull(sum(sqty),0) as good_qty
                                    from TK_Wrk_OutRecord where wo_code=@wo_code and style='S'  and step_seq=@step_seq-1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        //判断当前工序:收料总数数量
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //上道工序收料总合格数量
                                                                                                                     //判断:当前末道工序报工记录:当前工序报工总数量>上道工序收料总合格数量
                                        if (updatereportsumqty > last_reportqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前工序修改报工总数量:【" + updatereportsumqty + "】不能大于上道工序收料总合格数量:【" + last_reportqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                            }
                            //判断是否存在下道工序属性
                            if (next.Rows.Count > 0)
                            {
                                if (next.Rows[0]["flwtype"].ToString() == "Z")
                                {
                                    //查询当前工序下道工序:总报工数量、总不良数量、总报废数量
                                    sql = @"select isnull(sum(good_qty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                    from TK_Wrk_Record where wo_code=@wo_code and style='B'  and step_seq=@step_seq+1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt1 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt1.Rows.Count > 0)
                                    {
                                        decimal last_reportqty = decimal.Parse(dt1.Rows[0]["good_qty"].ToString());  //下道工序报工总数量
                                        decimal last_ngqty = decimal.Parse(dt1.Rows[0]["ng_qty"].ToString());  //下道工序不良总数量
                                        decimal last_laborbad_qty = decimal.Parse(dt1.Rows[0]["laborbad_qty"].ToString());  //下道工序工废总数量
                                        decimal last_materielbad_qty = decimal.Parse(dt1.Rows[0]["materielbad_qty"].ToString());  //下道工序料废总数量
                                        decimal last_updatereportsumqty = last_reportqty + last_ngqty + last_laborbad_qty + last_materielbad_qty;
                                        //判断(当前非本次报工总合格数+本次报工调整合格数)<下道工序报工总数
                                        if ((notthis_reportqty + this_reportqty) < last_updatereportsumqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前工序修改报工总合格数量:【" + (notthis_reportqty + this_reportqty) + "】不能小于下道工序报工总数量:【" + last_updatereportsumqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                                else
                                {
                                    //查询当前工序下道工序:总发料数量
                                    sql = @"select isnull(sum(fqty),0) as good_qty
                                    from TK_Wrk_OutRecord where wo_code=@wo_code and style='F'  and step_seq=@step_seq+1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        //判断当前工序:发料总数数量
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //下道工序发料总合格数量
                                                                                                                     //判断(当前非本次报工总合格数+本次报工调整合格数)<下道工序发料总数
                                        if ((notthis_reportqty + this_reportqty) < last_reportqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前工序修改报工总数量:【" + (notthis_reportqty + this_reportqty) + "】不能大于下道工序发料总数量:【" + last_reportqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (json[0].flw_type.ToString() == "W")//外协工序
                    {
                        //查询当前首道工序非此次供应商收料:总收料数量
                        sql = @"select isnull(sum(sqty),0) as sqty
                                from TK_Wrk_OutRecord where wo_code=@wo_code and style='S' and id<>@id and step_code=@step_code and wx_code<>@wx_code";
                        dynamicParams.Add("@wo_code", json[0].wo_code);
                        dynamicParams.Add("@id", json[0].id);
                        dynamicParams.Add("@step_code", json[0].step_code);
                        dynamicParams.Add("@wx_code", json[0].wxcode);
                        var dt_c = DapperHelper.selectdata(sql, dynamicParams);
 
                        //获取当前工序、供应商对应的发料数量
                        sql = @"select isnull(sum(fqty),0) as fqty
                                from TK_Wrk_OutRecord where wo_code=@wo_code and style='F' and id<>@id and step_code=@step_code and wx_code=@wx_code";
                        dynamicParams.Add("@wo_code", json[0].wo_code);
                        dynamicParams.Add("@id", json[0].id);
                        dynamicParams.Add("@step_code", json[0].step_code);
                        dynamicParams.Add("@wx_code", json[0].wxcode);
                        var dt_0 = DapperHelper.selectdata(sql, dynamicParams);
                        //首道工序的收料
                        if (json[0].first_choke == "Y")
                        {
                            //查询当前首道工序非此次收料:总收料数量、总不良数量、总工废数量、总料废数量
                            sql = @"select isnull(sum(sqty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                from TK_Wrk_OutRecord where wo_code=@wo_code and style='S' and id<>@id and step_code=@step_code and wx_code=@wx_code";
                            dynamicParams.Add("@wo_code", json[0].wo_code);
                            dynamicParams.Add("@id", json[0].id);
                            dynamicParams.Add("@step_code", json[0].step_code);
                            dynamicParams.Add("@wx_code", json[0].wxcode);
                            var dt = DapperHelper.selectdata(sql, dynamicParams);
                            decimal notthis_reportqty = decimal.Parse(dt.Rows[0]["good_qty"].ToString());  //当前工序非本次收料总数
                            decimal notthis_ngqty = decimal.Parse(dt.Rows[0]["ng_qty"].ToString());  //当前工序非本次报工总不良数
                            decimal notthis_laborbadqty = decimal.Parse(dt.Rows[0]["laborbad_qty"].ToString());  //当前工序非本次报工总工废数
                            decimal notthis_materielbadqty = decimal.Parse(dt.Rows[0]["materielbad_qty"].ToString());  //当前工序非本次报工总料废数
                            //判断:当前工序报工记录:本次收料数量+本次不良数量+本次工废数量+本次料废数量+当前工序非本次收料总数+当前工序非本次不良总数+当前工序非本次工废总数+当前工序非本次料废总数>发料数量
                            decimal updatereportsumqty = this_reportqty + this_ngqty + this_laborbadqty + this_materielbadqty + notthis_reportqty + notthis_ngqty + notthis_laborbadqty + notthis_materielbadqty;
                            if (updatereportsumqty > decimal.Parse(dt_0.Rows[0]["fqty"].ToString()))
                            {
                                mes.code = "300";
                                mes.count = 0;
                                mes.message = "当前外协工序对应供应商收料总数量:【" + updatereportsumqty + "】不能大于发料数量:【" + decimal.Parse(dt_0.Rows[0]["fqty"].ToString()) + "】!";
                                mes.data = null;
                                return mes;
                            }
                            //判断是否存在下道工序及属性
                            if (next.Rows.Count > 0)
                            {
                                if (next.Rows[0]["flwtype"].ToString() == "Z")
                                {
                                    //查询当前工序下道工序:总报工数量、总不良数量、总报废数量
                                    sql = @"select isnull(sum(good_qty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                    from TK_Wrk_Record where wo_code=@wo_code and style='B'  and step_seq=@step_seq+1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        //判断当前工序:报工总数数量+不良总数数量+工废总数量+料废总数量<下道工序报工总数量+下道工序不良总数量+下道工序工废总数量+下道工序料废总数量
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //下道工序报工总数量
                                        decimal last_ngqty = decimal.Parse(dt0.Rows[0]["ng_qty"].ToString());  //下道工序不良总数量
                                        decimal last_laborbad_qty = decimal.Parse(dt0.Rows[0]["laborbad_qty"].ToString());  //下道工序工废总数量
                                        decimal last_materielbad_qty = decimal.Parse(dt0.Rows[0]["materielbad_qty"].ToString());  //下道工序料废总数量
                                        decimal last_updatereportsumqty = last_reportqty + last_ngqty + last_laborbad_qty + last_materielbad_qty;
                                        //判断(当前非本次收料数+本次收料调整数+非当前供应商同工序收料总数)<下道工序报工总数
                                        if ((notthis_reportqty + this_reportqty + decimal.Parse(dt_c.Rows[0]["sqty"].ToString())) < last_updatereportsumqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前外协工序收料数量:【" + (notthis_reportqty + this_reportqty + decimal.Parse(dt_c.Rows[0]["sqty"].ToString())) + "】不能小于下道自制工序报工总数量:【" + last_updatereportsumqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                                else
                                {
                                    //查询当前工序下道工序:总发料数量
                                    sql = @"select isnull(sum(fqty),0) as good_qty
                                    from TK_Wrk_OutRecord where wo_code=@wo_code and style='F'  and step_seq=@step_seq+1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        //判断当前工序:发料总数数量
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //下道工序发料总数量
                                                                                                                     //判断(当前非本次收料数+本次报工调整收料数)<下道工序发料总数
                                        if ((notthis_reportqty + this_reportqty) < last_reportqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前外协工序收料总数量:【" + (notthis_reportqty + this_reportqty) + "】不能小于下道外协工序发料总数量:【" + last_reportqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                            }
 
                        }
                        //末道工序的报工
                        else if (json[0].last_choke == "Y")
                        {
                            //查询当前末道报工工序非此次收料:总收料数量、总不良数量、总工废数量、总料废数量
                            sql = @"select isnull(sum(sqty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                from TK_Wrk_OutRecord where wo_code=@wo_code and style='S' and id<>@id and step_code=@step_code  and wx_code=@wx_code";
                            dynamicParams.Add("@wo_code", json[0].wo_code);
                            dynamicParams.Add("@id", json[0].id);
                            dynamicParams.Add("@step_code", json[0].step_code);
                            var dt = DapperHelper.selectdata(sql, dynamicParams);
                            decimal notthis_reportqty = decimal.Parse(dt.Rows[0]["good_qty"].ToString());  //当前末道工序非本次收料总数
                            decimal notthis_ngqty = decimal.Parse(dt.Rows[0]["ng_qty"].ToString());  //当前末道工序非本次报工总数
                            decimal notthis_laborbad_qty = decimal.Parse(dt.Rows[0]["laborbad_qty"].ToString());  //当前末道工序非本次报工工废总数
                            decimal notthis_materielbad_qty = decimal.Parse(dt.Rows[0]["materielbad_qty"].ToString());  //当前末道工序非本次报工料废总数
                                                                                                                        //获取当前末道工序收料总数量:本次修改收料数量+本次修改不良数量+本次修改工废数量+本次修改报工料废数量+当前末道工序非本次收料总数+当前末道工序非本次不良总数+当前末道工序非本次工废总数+当前末道工序非本次料废总数
                            decimal updatereportsumqty = this_reportqty + this_ngqty + this_laborbadqty + this_materielbadqty + notthis_reportqty + notthis_ngqty + notthis_laborbad_qty + notthis_materielbad_qty;
                            //判断当前工序供应商收料总数>当前工序供应商对应发料数量
                            if (updatereportsumqty > decimal.Parse(dt_0.Rows[0]["fqty"].ToString()))
                            {
                                mes.code = "300";
                                mes.count = 0;
                                mes.message = "当前工序供应商收料总数量:【" + updatereportsumqty + "】不能大于工序供应商发料总数量:【" + decimal.Parse(dt_0.Rows[0]["fqty"].ToString()) + "】!";
                                mes.data = null;
                                return mes;
                            }
 
                        }
                        else //中间工序的报工
                        {
 
                            //查询当前首道工序供应商非此次收料:总收料数量、总不良数量、总工废数量、总料废数量
                            sql = @"select isnull(sum(sqty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                from TK_Wrk_OutRecord where wo_code=@wo_code and style='S' and id<>@id and step_code=@step_code and wx_code=@wx_code";
                            dynamicParams.Add("@wo_code", json[0].wo_code);
                            dynamicParams.Add("@id", json[0].id);
                            dynamicParams.Add("@step_code", json[0].step_code);
                            dynamicParams.Add("@wx_code", json[0].wxcode);
                            var dt = DapperHelper.selectdata(sql, dynamicParams);
                            decimal notthis_reportqty = decimal.Parse(dt.Rows[0]["good_qty"].ToString());  //当前工序非本次收料总数
                            decimal notthis_ngqty = decimal.Parse(dt.Rows[0]["ng_qty"].ToString());  //当前工序非本次报工总不良数
                            decimal notthis_laborbadqty = decimal.Parse(dt.Rows[0]["laborbad_qty"].ToString());  //当前工序非本次报工总工废数
                            decimal notthis_materielbadqty = decimal.Parse(dt.Rows[0]["materielbad_qty"].ToString());  //当前工序非本次报工总料废数
                                                                                                                       //判断:当前工序报工记录:本次收料数量+本次不良数量+本次工废数量+本次料废数量+当前工序非本次收料总数+当前工序非本次不良总数+当前工序非本次工废总数+当前工序非本次料废总数>发料数量
                            decimal updatereportsumqty = this_reportqty + this_ngqty + this_laborbadqty + this_materielbadqty + notthis_reportqty + notthis_ngqty + notthis_laborbadqty + notthis_materielbadqty;
                            if (updatereportsumqty > decimal.Parse(dt_0.Rows[0]["fqty"].ToString()))
                            {
                                mes.code = "300";
                                mes.count = 0;
                                mes.message = "当前外协工序对应供应商收料总数量:【" + updatereportsumqty + "】不能大于发料数量:【" + decimal.Parse(dt_0.Rows[0]["fqty"].ToString()) + "】!";
                                mes.data = null;
                                return mes;
                            }
                            //判断是否存在下道工序及属性
                            if (next.Rows.Count > 0)
                            {
                                if (next.Rows[0]["flwtype"].ToString() == "Z")
                                {
                                    //查询当前工序下道工序:总报工数量、总不良数量、总报废数量
                                    sql = @"select isnull(sum(good_qty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                    from TK_Wrk_Record where wo_code=@wo_code and style='B'  and step_seq=@step_seq+1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        //判断当前工序:报工总数数量+不良总数数量+工废总数量+料废总数量<下道工序报工总数量+下道工序不良总数量+下道工序工废总数量+下道工序料废总数量
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //下道工序报工总数量
                                        decimal last_ngqty = decimal.Parse(dt0.Rows[0]["ng_qty"].ToString());  //下道工序不良总数量
                                        decimal last_laborbad_qty = decimal.Parse(dt0.Rows[0]["laborbad_qty"].ToString());  //下道工序工废总数量
                                        decimal last_materielbad_qty = decimal.Parse(dt0.Rows[0]["materielbad_qty"].ToString());  //下道工序料废总数量
                                        decimal last_updatereportsumqty = last_reportqty + last_ngqty + last_laborbad_qty + last_materielbad_qty;
                                        //判断(当前非本次收料数+本次收料调整数+非当前供应商同工序收料总数)<下道工序报工总数
                                        if ((notthis_reportqty + this_reportqty + decimal.Parse(dt_c.Rows[0]["sqty"].ToString())) < last_updatereportsumqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前外协工序收料数量:【" + (notthis_reportqty + this_reportqty + decimal.Parse(dt_c.Rows[0]["sqty"].ToString())) + "】不能小于下道自制工序报工总数量:【" + last_updatereportsumqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                                else
                                {
                                    //查询当前工序下道工序:总发料数量
                                    sql = @"select isnull(sum(fqty),0) as good_qty
                                    from TK_Wrk_OutRecord where wo_code=@wo_code and style='F'  and step_seq=@step_seq+1";
                                    dynamicParams.Add("@wo_code", json[0].wo_code);
                                    dynamicParams.Add("@step_seq", json[0].step_seq);
                                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
                                    if (dt0.Rows.Count > 0)
                                    {
                                        //判断当前工序:发料总数数量
                                        decimal last_reportqty = decimal.Parse(dt0.Rows[0]["good_qty"].ToString());  //下道工序发料总数量
                                                                                                                     //判断(当前非本次收料数+本次报工调整收料数+非当前供应商同工序收料总数)<下道工序发料总数
                                        if ((notthis_reportqty + this_reportqty + decimal.Parse(dt_c.Rows[0]["sqty"].ToString())) < last_reportqty)
                                        {
                                            mes.code = "300";
                                            mes.count = 0;
                                            mes.message = "当前外协工序收料总数量:【" + (notthis_reportqty + this_reportqty + decimal.Parse(dt_c.Rows[0]["sqty"].ToString())) + "】不能小于下道外协工序发料总数量:【" + last_reportqty + "】!";
                                            mes.data = null;
                                            return mes;
                                        }
                                    }
                                }
                            }
                        }  
                    }
                }
                else //不按序
                {
                    //控制逻辑:当前工序报工调整-> (本道工序当前调整合格数+本道工序非当前报工合格总数)<下道工序报工总数(合格+不良+报废)   ==不能小于下道报工总数
                    list.Clear();
                    //判断当前工序是自制工序还是外协工序
                    if (json[0].flw_type.ToString() == "Z") 
                    {
                        
                       //查询当前报工工序非此次报工:总报工数量、总不良数量、总工废数量、总料废数量
                        sql = @"select isnull(sum(good_qty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                from TK_Wrk_Record where wo_code=@wo_code and style='B' and id<>@id and step_code=@step_code";
                        dynamicParams.Add("@wo_code", json[0].wo_code);
                        dynamicParams.Add("@id", json[0].id);
                        dynamicParams.Add("@step_code", json[0].step_code);
                        var dt = DapperHelper.selectdata(sql, dynamicParams);
                        decimal notthis_reportqty = decimal.Parse(dt.Rows[0]["good_qty"].ToString());  //当前工序非本次报工总数
                        decimal notthis_ngqty = decimal.Parse(dt.Rows[0]["ng_qty"].ToString());  //当前工序非本次报工总不良数
                        decimal notthis_laborbadqty = decimal.Parse(dt.Rows[0]["laborbad_qty"].ToString());  //当前工序非本次报工总工废数
                        decimal notthis_materielbadqty = decimal.Parse(dt.Rows[0]["materielbad_qty"].ToString());  //当前工序非本次报工总料废数
                        //判断:当前工序报工记录:本次报工数量+本次不良数量+本次工废数量+本次料废数量+当前工序非本次报工总数+当前工序非本次不良总数+当前工序非本次工废总数+当前工序非本次料废总数>工单任务数量
                        decimal updatereportsumqty = this_reportqty + this_ngqty + this_laborbadqty + this_materielbadqty + notthis_reportqty + notthis_ngqty + notthis_laborbadqty + notthis_materielbadqty;
                        if (updatereportsumqty > decimal.Parse(json[0].task_qty.ToString()))
                        {
                            mes.code = "300";
                            mes.count = 0;
                            mes.message = "当前工序修改报工总数量:【" + updatereportsumqty + "】不能大于任务超产总数量:【" + json[0].task_qty.ToString() + "】!";
                            mes.data = null;
                            return mes;
                        }
                    }
                    if (json[0].flw_type.ToString() == "W") 
                    {
                        //获取当前工序、供应商对应的总发料数量
                        sql = @"select isnull(sum(fqty),0) as fqty
                                from TK_Wrk_OutRecord where wo_code=@wo_code and style='F' and id<>@id and step_code=@step_code and wx_code=@wx_code";
                        dynamicParams.Add("@wo_code", json[0].wo_code);
                        dynamicParams.Add("@id", json[0].id);
                        dynamicParams.Add("@step_code", json[0].step_code);
                        dynamicParams.Add("@wx_code", json[0].wxcode);
                        var dt_0 = DapperHelper.selectdata(sql, dynamicParams);
 
                        //查询当前工序非此次收料:总收料数量、总不良数量、总工废数量、总料废数量
                        sql = @"select isnull(sum(sqty),0) as good_qty,isnull(sum(ng_qty),0) as ng_qty,isnull(sum(laborbad_qty),0) as laborbad_qty,isnull(sum(materielbad_qty),0) as materielbad_qty   
                                from TK_Wrk_OutRecord where wo_code=@wo_code and style='S' and id<>@id and step_code=@step_code  and wx_code=@wx_code";
                        dynamicParams.Add("@wo_code", json[0].wo_code);
                        dynamicParams.Add("@id", json[0].id);
                        dynamicParams.Add("@step_code", json[0].step_code);
                        var dt = DapperHelper.selectdata(sql, dynamicParams);
                        decimal notthis_reportqty = decimal.Parse(dt.Rows[0]["good_qty"].ToString());  //当前末道工序非本次收料总数
                        decimal notthis_ngqty = decimal.Parse(dt.Rows[0]["ng_qty"].ToString());  //当前末道工序非本次报工总数
                        decimal notthis_laborbad_qty = decimal.Parse(dt.Rows[0]["laborbad_qty"].ToString());  //当前末道工序非本次报工工废总数
                        decimal notthis_materielbad_qty = decimal.Parse(dt.Rows[0]["materielbad_qty"].ToString());  //当前末道工序非本次报工料废总数
                       //获取当前末道工序收料总数量:本次修改收料数量+本次修改不良数量+本次修改工废数量+本次修改报工料废数量+当前工序非本次收料总数+当前工序非本次不良总数+当前工序非本次工废总数+当前工序非本次料废总数
                        decimal updatereportsumqty = this_reportqty + this_ngqty + this_laborbadqty + this_materielbadqty + notthis_reportqty + notthis_ngqty + notthis_laborbad_qty + notthis_materielbad_qty;
                        //判断当前工序供应商收料总数>当前工序供应商对应发料数量
                        if (updatereportsumqty > decimal.Parse(dt_0.Rows[0]["fqty"].ToString()))
                        {
                            mes.code = "300";
                            mes.count = 0;
                            mes.message = "当前工序供应商收料总数量:【" + updatereportsumqty + "】不能大于工序供应商发料总数量:【" + decimal.Parse(dt_0.Rows[0]["fqty"].ToString()) + "】!";
                            mes.data = null;
                            return mes;
                        }
                    }
                }
 
                switch (json[0].flw_type.ToString())
                {
                    case "Z":
                        ///////////////////////////////修改报工//////////////////////////////
 
                        //回写对应的报工记录子表合格数量、不良数量、报废数量
                        sql = @"update TK_Wrk_RecordSub set report_qty=report_qty+@repair_qty,ng_qty=ng_qty+@ng_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty,
                            updatereportuser=@updatereportuser,updatereportdate=@updatereportdate
                            where  m_id=@m_id and id=@id and style='B'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                m_id = int.Parse(json[0].id),
                                id = int.Parse(json[0].sbid),
                                repair_qty = decimal.Parse(json[0].report_dvalue),
                                ng_qty = this_ng_dvalue,
                                laborbad_qty = this_laborbad_dvalue,
                                materielbad_qty = this_materielbad_dvalue,
                                //bad_money = decimal.Parse(json[i].badmoney_dvalue),
                                updatereportuser = us.usercode,
                                updatereportdate = date
                            }
                        });
                        //回写对应的报工记录主表合格数量、不良数量、报废数量
                        sql = @"update TK_Wrk_Record set step_price=@step_price,start_qty=start_qty+@good_qty, good_qty=good_qty+@good_qty,ng_qty=ng_qty+@ng_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty,
                            updatereportuser=@updatereportuser,updatereportdate=@updatereportdate
                            where wo_code=@wo_code and step_code=@step_code and id=@id and style='B'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                step_price=decimal.Parse(json[0].unprice),
                                good_qty = decimal.Parse(json[0].report_dvalue),
                                ng_qty = this_ng_dvalue,
                                laborbad_qty = this_laborbad_dvalue,
                                materielbad_qty = this_materielbad_dvalue,
                                wo_code = json[0].wo_code,
                                step_code = json[0].step_code,
                                id = int.Parse(json[0].id),
                                updatereportuser = us.usercode,
                                updatereportdate = date
                            }
                        });
                        //回写工单工序表
                        sql = @"update TK_Wrk_Step set good_qty=good_qty+@good_qty,ng_qty=ng_qty+@ng_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                            where wo_code=@wo_code and step_code=@step_code";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                good_qty = decimal.Parse(json[0].report_dvalue),
                                ng_qty = this_ng_dvalue,
                                laborbad_qty = this_laborbad_dvalue,
                                materielbad_qty = this_materielbad_dvalue,
                                wo_code = json[0].wo_code,
                                step_code = json[0].step_code
                            }
                        });
 
                        for (int i = 0; i < json[0].children.Count; i++)
                        {
                            //回写不良
                            sql = @"update CSR_WorkRecord_Defect set defect_qty=defect_qty+@ng_qty,defect_pendqty=defect_pendqty+@ng_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty,
                                updatereportuser=@updatereportuser,updatereportdate=@updatereportdate
                                where wo_code=@wo_code and step_code=@step_code and id=@ng_id and record_id=@record_id and style='B'";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    ng_qty = decimal.Parse(json[0].children[i].ng_dvalue),
                                    laborbad_qty = decimal.Parse(json[0].children[i].laborbad_dvalue),
                                    materielbad_qty = decimal.Parse(json[0].children[i].materielbad_dvalue),
                                    wo_code = json[0].wo_code,
                                    step_code = json[0].step_code,
                                    ng_id = int.Parse(json[0].children[i].ng_id),
                                    record_id = json[0].id,
                                    updatereportuser = us.usercode,
                                    updatereportdate = date
                                }
                            });
                            //回写不良处理
                            sql = @"update CSR_WorkRecord_DefectHandle set laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                                where wo_code=@wo_code and step_code=@step_code and defect_id=@defect_id and style='B'";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    laborbad_qty = decimal.Parse(json[0].children[i].laborbad_dvalue),
                                    materielbad_qty = decimal.Parse(json[0].children[i].materielbad_dvalue),
                                    wo_code = json[0].wo_code,
                                    step_code = json[0].step_code,
                                    defect_id = int.Parse(json[0].children[i].ng_id),
                                    updatereportuser = us.usercode,
                                    updatereportdate = date
                                }
                            });
                        }
                        break;
                    case "W":
                        ///////////////////////////////修改报工//////////////////////////////
 
                        //回写对应的外协记录子表收料数量、不良数量、报废数量
                        sql = @"update TK_Wrk_OutRecordSub set sqty=sqty+@repair_qty,ng_qty=ng_qty+@ng_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty,
                            updatereportuser=@updatereportuser,updatereportdate=@updatereportdate
                            where  m_id=@m_id and id=@id and style='S' and wx_code=@wx_code";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                m_id = int.Parse(json[0].id),
                                id = int.Parse(json[0].sbid),
                                repair_qty = decimal.Parse(json[0].report_dvalue),
                                ng_qty = this_ng_dvalue,
                                laborbad_qty = this_laborbad_dvalue,
                                materielbad_qty = this_materielbad_dvalue,
                                wx_code = json[0].wxcode,
                                //bad_money = decimal.Parse(json[i].badmoney_dvalue),
                                updatereportuser = us.usercode,
                                updatereportdate = date
                            }
                        });
                        //回写对应的收料记录主表收料数量、不良数量、报废数量
                        sql = @"update TK_Wrk_OutRecord set step_price=@step_price,sqty=sqty+@good_qty,ng_qty=ng_qty+@ng_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty,
                            updatereportuser=@updatereportuser,updatereportdate=@updatereportdate
                            where wo_code=@wo_code and step_code=@step_code and id=@id and style='S' and wx_code=@wx_code";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                step_price=decimal.Parse(json[0].unprice),
                                good_qty = decimal.Parse(json[0].report_dvalue),
                                ng_qty = this_ng_dvalue,
                                laborbad_qty = this_laborbad_dvalue,
                                materielbad_qty = this_materielbad_dvalue,
                                wx_code = json[0].wxcode,
                                wo_code = json[0].wo_code,
                                step_code = json[0].step_code,
                                id = int.Parse(json[0].id),
                                updatereportuser = us.usercode,
                                updatereportdate = date
                            }
                        });
                        //回写工单工序表
                        sql = @"update TK_Wrk_Step set good_qty=good_qty+@good_qty,ng_qty=ng_qty+@ng_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                            where wo_code=@wo_code and step_code=@step_code";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                good_qty = decimal.Parse(json[0].report_dvalue),
                                ng_qty = this_ng_dvalue,
                                laborbad_qty = this_laborbad_dvalue,
                                materielbad_qty = this_materielbad_dvalue,
                                wo_code = json[0].wo_code,
                                step_code = json[0].step_code
                            }
                        });
 
                        for (int i = 0; i < json[0].children.Count; i++)
                        {
                            //回写不良
                            sql = @"update CSR_WorkRecord_Defect set defect_qty=defect_qty+@ng_qty,defect_pendqty=defect_pendqty+@ng_qty,laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty,
                                updatereportuser=@updatereportuser,updatereportdate=@updatereportdate
                                where wo_code=@wo_code and step_code=@step_code and id=@ng_id and record_id=@record_id and style='S'";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    ng_qty = decimal.Parse(json[0].children[i].ng_dvalue),
                                    laborbad_qty = decimal.Parse(json[0].children[i].laborbad_dvalue),
                                    materielbad_qty = decimal.Parse(json[0].children[i].materielbad_dvalue),
                                    wo_code = json[0].wo_code,
                                    step_code = json[0].step_code,
                                    ng_id = int.Parse(json[0].children[i].ng_id),
                                    record_id = json[0].id,
                                    updatereportuser = us.usercode,
                                    updatereportdate = date
                                }
                            });
                            //回写不良处理
                            sql = @"update CSR_WorkRecord_DefectHandle set laborbad_qty=laborbad_qty+@laborbad_qty,materielbad_qty=materielbad_qty+@materielbad_qty
                                where wo_code=@wo_code and step_code=@step_code and defect_id=@defect_id and style='S'";
                            list.Add(new
                            {
                                str = sql,
                                parm = new
                                {
                                    laborbad_qty = decimal.Parse(json[0].children[i].laborbad_dvalue),
                                    materielbad_qty = decimal.Parse(json[0].children[i].materielbad_dvalue),
                                    wo_code = json[0].wo_code,
                                    step_code = json[0].step_code,
                                    defect_id = int.Parse(json[0].children[i].ng_id),
                                    updatereportuser = us.usercode,
                                    updatereportdate = date
                                }
                            });
                        }
                        break;
                    default:
                        break;
                }
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "报工调整", "工单:" + json[0].wo_code + "工序:" + json[0].step_code, us.usertype);
                    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 MesOrderStepPriceBatchUpdateSeave(User us, List<BatchPrice> json)
        {
            string sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                if (json.Count > 0)
                {
                    // 使用Lambda表达式判断类型分布:全部报工或者全部收料
                    bool allB = json.All(p => p.type == "B");
                    bool allS = json.All(p => p.type == "S");
                    if (allB)//全部报工
                    {
                        //将List<BatchPrice>中的id字段转换为字符串数组
                        string[] idArray = json.Select(bp => bp.id).ToArray();
                        //回写对应的报工记录主表工价
                        sql = @"update TK_Wrk_Record set step_price=@step_price where id in @idArray and style='B'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                step_price = decimal.Parse(json[0].price),
                                idArray = idArray
                            }
                        });
                    }
                    else if (allS)//全部收料
                    {
                        //将List<BatchPrice>中的id字段转换为字符串数组
                        string[] idArray = json.Select(bp => bp.id).ToArray();
                        //回写对应的报工记录主表工价
                        sql = @"update TK_Wrk_OutRecord set step_price=@step_price where id in @idArray and style='S'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                step_price = decimal.Parse(json[0].price),
                                idArray = idArray
                            }
                        });
                    }
                    else // 混合情况
                    {
                        //将List<BatchPrice>中为报工的id字段转换为字符串数组
                        var bList = json.Where(p => p.type == "B").ToList();
                        string[] idBArray = bList.Select(bp => bp.id).ToArray();
                        //回写对应的报工记录主表工价
                        sql = @"update TK_Wrk_Record set step_price=@step_price where id in @idBArray and style='B'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                step_price = decimal.Parse(json[0].price),
                                idBArray = idBArray
                            }
                        });
                        //将List<BatchPrice>中为收料的id字段转换为字符串数组
                        var sList = json.Where(p => p.type == "S").ToList();
                        string[] idSArray = sList.Select(bp => bp.id).ToArray();
                        //回写对应的报工记录主表工价
                        sql = @"update TK_Wrk_OutRecord set step_price=@step_price where id in @idSArray and style='S'";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                step_price = decimal.Parse(json[0].price),
                                idSArray = idSArray
                            }
                        });
                    }
                    bool aa = DapperHelper.DoTransaction(list);
                    if (aa)
                    {
                        //写入操作记录表
                        LogHelper.DbOperateLog(us.usercode, "批量修改工价", "报工ID:" + string.Join(",",json.Select(bp => bp.id).ToArray())+",操作类型:"+ string.Join(",", json.Select(bp => bp.type).ToArray()), us.usertype);
                        mes.code = "200";
                        mes.count = 0;
                        mes.message = "操作成功!";
                        mes.data = null;
                    }
                    else
                    {
                        mes.code = "300";
                        mes.count = 0;
                        mes.message = "操作失败!";
                        mes.data = null;
                    }
                }
                else 
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.message = "请选择需要批量调整工价的报工记录!";
                    mes.data = null;
                    return mes;
                }
                return mes;
            }
            catch (Exception)
            {
 
                throw;
            }
        }
        #endregion
 
 
 
        #region[生产执行,报工审核列表数据查询接口]
        public static ToMessage MesOrderStepReportVerifySearch(string reviewstatus, string wkshopcode, string wo_code, string partnumber, string partname, string partspec, string stepname, string reportuser, string reportdateopendate, string reportdateclosedate, int startNum, int endNum, string prop, string order)
        {
            var dynamicParams = new DynamicParameters();
            string search = "";
            try
            {
                if (reviewstatus != "" && reviewstatus != null)
                {
                    search += "and AA.verify=@reviewstatus ";
                    dynamicParams.Add("@reviewstatus", reviewstatus);
                }
                if (wkshopcode != "" && wkshopcode != null)
                {
                    search += "and AA.wkshp_code=@wkshopcode ";
                    dynamicParams.Add("@wkshopcode", wkshopcode);
                }
                if (wo_code != "" && wo_code != null)
                {
                    search += "and AA.wo_code like '%'+@wo_code+'%' ";
                    dynamicParams.Add("@wo_code", wo_code);
                }
                if (partnumber != "" && partnumber != null)
                {
                    search += "and AA.partnumber like '%'+@partnumber+'%' ";
                    dynamicParams.Add("@partnumber", partnumber);
                }
                if (partname != "" && partname != null)
                {
                    search += "and AA.partname like '%'+@partname+'%' ";
                    dynamicParams.Add("@partname", partname);
                }
                if (partspec != "" && partspec != null)
                {
                    search += "and AA.partspec like '%'+@partspec+'%' ";
                    dynamicParams.Add("@partspec", partspec);
                }
                if (stepname != "" && stepname != null)
                {
                    search += "and AA.stepname like '%'+@stepname+'%' ";
                    dynamicParams.Add("@stepname", stepname);
                }
                if (reportuser != "" && reportuser != null)
                {
                    search += "and AA.usercode like '%'+@reportuser+'%' ";
                    dynamicParams.Add("@reportuser", reportuser);
                }
                if (reportdateopendate != "" && reportdateopendate != null)
                {
                    search += "and AA.report_date between @reportdateopendate and @reportdateclosedate ";
                    dynamicParams.Add("@reportdateopendate", reportdateopendate + " 00:00:00");
                    dynamicParams.Add("@reportdateclosedate", reportdateclosedate + " 23:59:59");
                }
 
 
                if (search == "")
                {
                    search = "and 1=1 ";
                }
                search = search.Substring(3);//截取索引2后面的字符
                // --------------查询指定自制报工外协收料数据--------------
                var total = 0; //总条数
                var sql = @"select *  from(
                            select A.id,B.id as sbid,A.wo_code,A.materiel_code as partnumber,P.partname,P.partspec,K.plan_quantity,K.plan_qty as task_qty,M.wkshp_code,T.torg_name as wkshp_name,A.eqp_code,E.name as eqp_name,
                            A.step_seq,A.step_code,S.stepname,S.flwtype as steptype,k.isbott as first_choke,k.isend as last_choke,A.step_price,B.reckway,B.usergroup_code,G.usergroupname as usergroup_name,
                            B.report_person as usercode,
                             STUFF((SELECT ',' + U.username
                                    FROM TUser U
                                    WHERE CHARINDEX(',' + U.usercode + ',', ',' + B.report_person + ',') > 0
                                    FOR XML PATH('')), 1, 1, '') AS username,
                            B.report_date,B.report_qty,B.ng_qty,B.laborbad_qty,B.materielbad_qty,'' as wx_code,'' as wx_name,A.verify
                            from TK_Wrk_Record A
                            inner join TK_Wrk_RecordSub B on A.id=B.m_id
                            left join TK_Wrk_Man M on A.wo_code=M.wo_code
                            left join TK_Wrk_Step K on M.wo_code=K.wo_code and A.step_code=K.step_code
                            left join TStep S on A.step_code=S.stepcode
                            left join TMateriel_Info P on A.materiel_code=P.partcode
                            left join TOrganization T on M.wkshp_code=T.torg_code
                            left join TEqpInfo E on A.eqp_code=E.code
                            left join TGroup G on G.usergroupcode=B.usergroup_code
                            where A.style='B' and B.style='B' and M.status<>'CLOSED' 
                            union all
                            select A.id,B.id as sbid,A.wo_code,A.materiel_code as partnumber,P.partname,P.partspec,K.plan_quantity,K.plan_qty as task_qty,M.wkshp_code,T.torg_name as wkshp_name,A.wx_code as eqp_code,E.name as eqp_name,
                            A.step_seq,A.step_code,S.stepname,S.flwtype as steptype,k.isbott as first_choke,k.isend as last_choke,A.step_price,'person' as reckway,'' as usergroup_code,'' as usergroup_name,
                            B.in_person as usercode,
                            STUFF((SELECT ',' + U.username
                                    FROM TUser U
                                    WHERE CHARINDEX(',' + U.usercode + ',', ',' + B.in_person + ',') > 0
                                    FOR XML PATH('')), 1, 1, '') AS username,
                            B.in_time as report_date,B.sqty as report_qty,B.ng_qty,B.laborbad_qty,B.materielbad_qty,A.wx_code,C.name as wx_name,A.verify
                            from TK_Wrk_OutRecord A
                            inner join TK_Wrk_OutRecordSub B on A.id = B.m_id
                            left join TK_Wrk_Man M on A.wo_code = M.wo_code
                            left join TK_Wrk_Step K on M.wo_code=K.wo_code and A.step_code=K.step_code
                            left join TStep S on A.step_code = S.stepcode
                            left join TMateriel_Info P on A.materiel_code = P.partcode
                            left join TOrganization T on M.wkshp_code = T.torg_code
                            left join TCustomer E on A.wx_code = E.code 
                            left join TCustomer C on A.wx_code=C.code
                            where A.style = 'S' and B.style = 'S' and M.status<>'CLOSED'
                            ) as AA where " + 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 MesOrderStepReportVerifySeave(User us, DataModel json)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                if (json.zdata.Count > 0)
                {
                    //回写报工记录主表审核状态、审核人、审核时间
                    sql = @"update TK_Wrk_Record set verify='Y',verifyuser=@verifyuser,verifydate=@verifydate  where id in @id";
                    list.Add(new { str = sql, parm = new { verifyuser = us.usercode, verifydate = DateTime.Now.ToString(), id = json.zdata } });
                    //回写报工记录子表审核状态、审核人、审核时间
                    sql = @"update TK_Wrk_RecordSub set verify='Y',verifyuser=@verifyuser,verifydate=@verifydate  where m_id in @id";
                    list.Add(new { str = sql, parm = new { verifyuser = us.usercode, verifydate = DateTime.Now.ToString(), id = json.zdata } });
                }
                if (json.wdata.Count > 0)
                {
                    //回写外协记录主表审核状态、审核人、审核时间
                    sql = @"update TK_Wrk_OutRecord set verify='Y',verifyuser=@verifyuser,verifydate=@verifydate  where id in @id";
                    list.Add(new { str = sql, parm = new { verifyuser = us.usercode, verifydate = DateTime.Now.ToString(), id = json.wdata } });
                    //回写外协记录子表审核状态、审核人、审核时间
                    sql = @"update TK_Wrk_OutRecordSub set verify='Y',verifyuser=@verifyuser,verifydate=@verifydate  where m_id in @id";
                    list.Add(new { str = sql, parm = new { verifyuser = us.usercode, verifydate = DateTime.Now.ToString(), id = json.wdata } });
                }
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    //写入操作记录表
                    LogHelper.DbOperateLog(us.usercode, "报工审核", "自制报工记录id:" + string.Join(",", json.zdata), us.usertype);
                    LogHelper.DbOperateLog(us.usercode, "报工审核", "外协收料记录id:" + string.Join(",", json.wdata), us.usertype);
                    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 MesOrderStepReportNotVerifySeave(User us, string id, string steptype)
        {
            var sql = "";
            List<object> list = new List<object>();
            var dynamicParams = new DynamicParameters();
            try
            {
                if (steptype == "Z")//自制工序
                {
                    //回写报工记录主表审核状态、审核人、审核时间
                    sql = @"update TK_Wrk_Record set verify='N',verifyuser=@verifyuser,verifydate=@verifydate  where id=@id";
                    list.Add(new { str = sql, parm = new { verifyuser = "", verifydate = "", id = id } });
                    //回写报工记录子表审核状态、审核人、审核时间
                    sql = @"update TK_Wrk_RecordSub set verify='N',verifyuser=@verifyuser,verifydate=@verifydate  where m_id=@id";
                    list.Add(new { str = sql, parm = new { verifyuser = "", verifydate = "", id = id } });
                }
                if (steptype == "W")//外协工序
                {
                    //回写外协记录主表审核状态、审核人、审核时间
                    sql = @"update TK_Wrk_OutRecord set verify='N',verifyuser=@verifyuser,verifydate=@verifydate  where id=@id";
                    list.Add(new { str = sql, parm = new { verifyuser = "", verifydate = "", id = id } });
                    //回写外协记录子表审核状态、审核人、审核时间
                    sql = @"update TK_Wrk_OutRecordSub set verify='N',verifyuser=@verifyuser,verifydate=@verifydate  where m_id=@id";
                    list.Add(new { str = sql, parm = new { verifyuser = "", verifydate = "", id = id } });
                }
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    if (steptype == "Z")
                    {
                        //写入操作记录表
                        LogHelper.DbOperateLog(us.usercode, "报工弃审", "自制报工记录id:" + string.Join(",", id), us.usertype);
                    }
                    if (steptype == "W")
                    {
                        LogHelper.DbOperateLog(us.usercode, "报工弃审", "外协收料记录id:" + string.Join(",", id), us.usertype);
                    }
                    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 ProductInHouseLabCode(string ordercode)
        {
            string sql = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //获取末道工序报工条码数据
                sql = @"select *   from(
                        select A.inbarcode,A.wo_code,P.partcode,P.partname,P.partspec,
                        A.good_qty,U.username,A.lm_date   
                        from  TK_Wrk_Record A
                        inner join TK_Wrk_Step S on A.wo_code=S.wo_code and A.step_code=S.step_code
                        inner join TMateriel_Info P on A.materiel_code=P.partcode
                        inner join TUser U on A.lm_user=U.usercode
                        where A.style='B' and S.isend='Y' and A.good_qty>0 and A.inbarcode<>''
                        union all
                        select A.inbarcode,A.wo_code,P.partcode,P.partname,P.partspec,
                        A.sqty as sqty,U.username,A.lm_date   
                        from  TK_Wrk_OutRecord A
                        inner join TK_Wrk_Step S on A.wo_code=S.wo_code and A.step_code=S.step_code
                        inner join TMateriel_Info P on A.materiel_code=P.partcode
                        inner join TUser U on A.lm_user=U.usercode
                        where A.style='S' and S.isend='Y' and A.sqty>0 and A.inbarcode<>''
                        ) as AA where AA.wo_code=@ordercode";
                dynamicParams.Add("@ordercode", ordercode);
                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 ProductInHouseOrderSearch(string issouceorder, string deptno, string saleordercode, string wkshopcode, string erpordercode, string mesordercode, string partcode, string partname, string partspec)
        {
            var dynamicParams = new DynamicParameters();
            string search = "";
            try
            {
                if (issouceorder != "" && issouceorder != null)
                {
                    switch (issouceorder)
                    {
                        case "Y":
                            search += " and AA.m_po<>'' ";
                            break;
                        case "N":
                            search += " and AA.m_po='' ";
                            break;
                        default:
                            break;
                    }
                }
                if (deptno != "" && deptno != null)
                {
                    search += "and AA.dept_code=@deptno ";
                    dynamicParams.Add("@deptno", deptno);
                }
                if (saleordercode != "" && saleordercode != null)
                {
                    search += "and AA.saleOrderCode like '%'+@saleordercode+'%' ";
                    dynamicParams.Add("@saleordercode", saleordercode);
                }
                if (wkshopcode != "" && wkshopcode != null)
                {
                    search += "and AA.wkshp_code=@wkshopcode ";
                    dynamicParams.Add("@wkshopcode", wkshopcode);
                }
                if (erpordercode != "" && erpordercode != null)
                {
                    search += "and AA.m_po like '%'+@erpordercode+'%' ";
                    dynamicParams.Add("@erpordercode", erpordercode);
                }
                if (mesordercode != "" && mesordercode != null)
                {
                    search += "and AA.wo_code like '%'+@mesordercode+'%' ";
                    dynamicParams.Add("@mesordercode", mesordercode);
                }
                if (partcode != "" && partcode != null)
                {
                    search += "and AA.partcode like '%'+@partcode+'%' ";
                    dynamicParams.Add("@partcode", partcode);
                }
                if (partname != "" && partname != null)
                {
                    search += "and AA.partname like '%'+@partname+'%' ";
                    dynamicParams.Add("@partname", partname);
                }
                if (partspec != "" && partspec != null)
                {
                    search += "and AA.partspec like '%'+@partspec+'%' ";
                    dynamicParams.Add("@partspec", partspec);
                }
                // --------------查询指定数据--------------
                var total = 0; //总条数
                var sql = @"select *   from(
                              select A.inbarcode,E.saleOrderid,isnull(E.saleOrderCode,'') as saleOrderCode,E.saleOrderDetailId,E.woid as mpoid,M.m_po,E.sbid,M.id as wo_id,A.wo_code,E.materiel_id,P.partcode,P.partname,P.partspec,E.unitid,E.unitcode,E.unitname,
                              A.step_code,T.stepname,M.wkshp_code,O.torg_name as wkshp_name,COALESCE(K.noid, E.stck_id) as stockid,COALESCE(K.code, E.stck_code) as stockcode,k.name as stockname,E.dept_id,E.dept_code,
                              E.saleOrderqty,E.qty,M.plan_qty,A.good_qty,isnull(A.inhouseqty,0) as inhouseqty,A.good_qty-isnull(A.inhouseqty,0) as stinhouseqty,M.lm_date,A.style,
                              (case when E.voucherdate is null then M.lm_date else E.voucherdate end) as voucherdate 
                              from  TK_Wrk_Record A
                              inner join TK_Wrk_Step S on A.wo_code=S.wo_code and A.step_code=S.step_code
                              inner join TK_Wrk_Man M on S.wo_code=M.wo_code
                              inner join TMateriel_Info P on M.materiel_code=P.partcode
                              left join TKimp_Ewo E on M.m_po=E.wo and M.sourceid= E.id
                              left join TSecStck K on COALESCE(P.idwarehouse, E.stck_code)=K.code
                              left join TStep T on A.step_code=T.stepcode
                              left join TOrganization O on M.wkshp_code=O.torg_code
                              where A.style='B' and A.inbarcode<>'' and S.isend='Y' and A.good_qty>0
                              union all
                              select A.inbarcode,E.saleOrderid,isnull(E.saleOrderCode,'') as saleOrderCode,E.saleOrderDetailId,E.woid as mpoid,M.m_po,E.sbid,M.id as wo_id,A.wo_code,E.materiel_id,P.partcode,P.partname,P.partspec,E.unitid,E.unitcode,E.unitname,
                              A.step_code,T.stepname,M.wkshp_code,O.torg_name as wkshp_name,COALESCE(K.noid, E.stck_id) as stockid,COALESCE(K.code, E.stck_code) as stockcode,k.name as stockname,E.dept_id,E.dept_code,
                              E.saleOrderqty,E.qty,M.plan_qty,A.sqty as sqty,isnull(A.inhouseqty,0) as inhouseqty,A.sqty-isnull(A.inhouseqty,0) as stinhouseqty,M.lm_date,A.style,
                              (case when E.voucherdate is null then M.lm_date else E.voucherdate end) as voucherdate 
                              from  TK_Wrk_OutRecord A
                              inner join TK_Wrk_Step S on A.wo_code=S.wo_code and A.step_code=S.step_code
                              inner join TK_Wrk_Man M on S.wo_code=M.wo_code
                              inner join TMateriel_Info P on M.materiel_code=P.partcode
                              left join TKimp_Ewo E on M.m_po=E.wo and M.sourceid= E.id
                              left join TSecStck K on COALESCE(P.idwarehouse, E.stck_code)=K.code
                              left join TStep T on A.step_code=T.stepcode
                              left join TOrganization O on M.wkshp_code=O.torg_code
                              where A.style='S' and A.inbarcode<>'' and S.isend='Y' and A.sqty>0
                            ) as AA  where AA.good_qty>AA.inhouseqty " + search;
                var data = DapperHelper.selectdata(sql, dynamicParams);
                mes.code = "200";
                mes.message = "查询成功!";
                mes.count = total;
                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 ProductInHouseOrderCodeSearch(string rightcode)
        {
            try
            {
                mes = SeachEncodeJob.EncodingSeach(rightcode);
                return mes;
 
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[MES生产入库提交-存储过程版]
        public static ToMessage InHouseOrderSpSeave(InReptModel data, User us)
        {
            var sql = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //存储过程名
                sql = @"h_p_IFCLD_InProductOrder";
                dynamicParams.Add("@RecordMin", dbType: DbType.Object, value: data.TableData[0]);
                dynamicParams.Add("@RecordSub", dbType: DbType.Object, value: data.TableData[1]);
                dynamicParams.Add("@rightcode", data.rightcode);
                dynamicParams.Add("@incbit", data.incbit);
                dynamicParams.Add("@username", us.usercode);
                // 添加输出参数  
                dynamicParams.Add("@StatusCode", dbType: DbType.Int32, direction: ParameterDirection.Output);
                dynamicParams.Add("@Message", dbType: DbType.String, size: 255, direction: ParameterDirection.Output);
                bool a = DapperHelper.IsProcedure(sql, dynamicParams);
                // 获取输出参数的值  
                var statusCode = dynamicParams.Get<int>("@StatusCode");
                var message = dynamicParams.Get<string>("@Message");
                if (a)
                {
                    mes.code = statusCode.ToString();
                    mes.count = 0;
                    mes.message = message;
                    mes.data = null;
                }
                else
                {
                    mes.code = statusCode.ToString();
                    mes.count = 0;
                    mes.message = message;
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
 
        #region[畅捷通T+生产入库提交-存储过程版(适用同一台服务器,同一个数据库)]
        public static ToMessage TProductInHouseOrderSpSeave(InReptModel data, User us)
        {
            var sql = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //存储过程名
                sql = @"h_p_IFCLD_TCloudInProductOrder";
                dynamicParams.Add("@RecordMin", dbType: DbType.Object, value: data.TableData[0]);
                dynamicParams.Add("@RecordSub", dbType: DbType.Object, value: data.TableData[1]);
                dynamicParams.Add("@rightcode", data.rightcode);
                dynamicParams.Add("@incbit", data.incbit);
                dynamicParams.Add("@username", us.usercode);
                // 添加输出参数  
                dynamicParams.Add("@StatusCode", dbType: DbType.Int32, direction: ParameterDirection.Output);
                dynamicParams.Add("@Message", dbType: DbType.String, size: 255, direction: ParameterDirection.Output);
                bool a = DapperHelper.IsProcedure(sql, dynamicParams);
                // 获取输出参数的值  
                var statusCode = dynamicParams.Get<int>("@StatusCode");
                var message = dynamicParams.Get<string>("@Message");
                if (a)
                {
                    mes.code = statusCode.ToString();
                    mes.count = 0;
                    mes.message = message;
                    mes.data = null;
                }
                else
                {
                    mes.code = statusCode.ToString();
                    mes.count = 0;
                    mes.message = message;
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
    }
}