333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
//#region import
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import * as mssql from "mssql";
import { In, Like, MoreThan, Raw } from "typeorm";
import { SaleOuter } from "../../entity/outbound/sale/saleOuter";
import { SaleSendList } from "../../entity/outbound/sale/saleSendList";
import { SaleOrderPrintList } from "../../entity/outbound/manufacture/saleOrderPrintList";
import { SaleSortingRulePosition } from "../../entity/outbound/sortingrule/saleSortingRulePosition";
import { BaseExpressSpareCode } from "../../entity/express/spare/baseExpressSpareCode";
import * as XLSX from "xlsx";
import * as path from "path";
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
import { BaseExpressCorp } from "../../entity/basicInfo/base/baseExpressCorp";
import { SaleOrderList } from "../../entity/outbound/sale/saleOrderList";
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
import { BaseClient } from "../../entity/crm/client/baseClient";
import { BaseConsignor } from "../../entity/basicInfo/consignor/baseConsignor";
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
import { isNumber } from "util";
import moment = require("moment");
import { SaleOrderMerge } from "../../entity/outbound/sale/saleOrderMerge";
import { BaseProductPlaceHolder } from "../../entity/storage/storage/baseProductPlaceHolder";
import { SaleReturn } from "../../entity/outbound/service/saleReturn";
import { SaleOrderStatusHistory } from "../../entity/outbound/order/saleOrderStatusHistory";
import { BasePlateType } from "../../entity/basicInfo/base/basePlateType";
import { vBaseProductPlaceHolder } from "../../entity/storage/storage/vBaseProductPlaceHolder";
import { BaseProductPosition } from "../../entity/storage/product/baseProductPosition";
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
import { SaleReturnList } from "../../entity/outbound/service/saleReturnList";
import { PurchaseOrder } from "../../entity/inbound/purchase/purchaseOrder";
import { PurchaseOrderList } from "../../entity/inbound/purchase/purchaseOrderList";
import { BasePlate } from "../../entity/basicInfo/base/basePlate";
import { vBaseProductPosition } from "../../entity/storage/product/vBaseProductPosition";
import { BaseDestination } from "../../entity/basicInfo/workshop/baseDestination";
import { BaseProductInfoHistory } from "../../entity/basicInfo/base/baseProductInfoHistory";
import * as xlsxStye from "xlsx-style";
import { LackOrder } from "../../entity/outbound/sale/lackOrder";
import { SysUserLog } from "../../entity/sys/core/sysUserLog";
//import { LackOrderList } from "../../entity/outbound/sale/lackOrderList";
//#endregion
 
/**
 * 出库单
 */
export default class OrderController extends BaseController {
  //#region 批量审核
  /**
   * 修改日期:2019/1/11
   * 如果明细为空则不让审核通过
   */
  @Post()
  public async multiConfirm() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let product_Ids = [];
 
    try {
      //#region 校验数据
      if (!Array.isArray(body.selectIDs)) {
        this.info.result = false;
        this.info.msg = "数据不存在";
        ctx.body = this.info;
        return;
      }
 
      let orders = await this.dbRead.findByIds(SaleOrder, body.selectIDs);
      for (var orderInfo of orders) {
        let erp_expressCorp_Id = await this.ctx.service.common.getConfig("erp_expressCorp_Id");
        if (!orderInfo.expressCorp_Id) {
          let corpInfo = await this.dbRead.findOne(BaseExpressCorp, {
            userProduct_Id: userInfo.userProduct_Id,
            expressCorp_Id: Number(erp_expressCorp_Id)
          });
          if (corpInfo) {
            orderInfo.expressCorp_Id = Number(erp_expressCorp_Id);
            orderInfo.expressCorpName = corpInfo.expressCorpName;
            orderInfo.expressCorpType = corpInfo.expressCorpType;
          }
        }
        if (!orderInfo.storage_Id) {
          this.info.result = false;
          this.info.msg = orderInfo.orderCode + "仓库不能为空";
          ctx.body = this.info;
          return;
        }
        if (["用户取消"].indexOf(orderInfo.statusText) >= 0) {
          this.info.result = false;
          this.info.msg = `出库单【${orderInfo.orderCode}】状态为【${orderInfo.statusText}】不允许进行审核操作!`;
          ctx.body = this.info;
          return;
        }
        var detailList = await this.dbRead.find(SaleOrderList, {
          order_Id: orderInfo.order_Id
        });
        product_Ids.concat(detailList.map(item => item.product_Id));
 
        if (!detailList.length) {
          this.info.result = false;
          this.info.msg = orderInfo.orderCode + "请填写运单明细!";
          ctx.body = this.info;
          return;
        }
        if (detailList.filter(row => row.quantityOrder <= 0).length > 0) {
          this.info.result = false;
          this.info.msg = orderInfo.orderCode + "订单的明细预出库数量不允许小于零!";
          ctx.body = this.info;
          return;
        }
      }
      //#endregion
 
      //#region 审核
      await this.dbWrite.update(
        SaleOrder,
        {
          order_Id: In(body.selectIDs)
        },
        {
          statusID: 2,
          statusText: "审核成功",
          auditor: userInfo.userTrueName,
          auditing: 2,
          auditDate: new Date()
        }
      );
      //#endregion
 
      //#region 新增保存后记录订单状态轨迹
      for (var orderInfo of orders) {
        ctx.service.outbound.orderHelper.setStatusHistory(orderInfo.order_Id, orderInfo.statusID, 2, "订单状态", "订单审核");
      }
      //#endregion
 
      //#region 激活分拣
      //激活分拣池
      const connection: any = await this.dbWrite.connection;
      let request = new mssql.Request(connection.driver.master);
      request.input("Order_Ids", body.selectIDs.join(","));
      request.input("Product_Ids", product_Ids.join(","));
      request.input("createID", userInfo.user_Id);
      request.input("creator", userInfo.userTrueName);
      let result = await request.execute("sp_SortOrder");
      let outMsg = result.returnValue;
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        ctx.body = this.info;
        return;
      }
 
      //启动分拣
      setTimeout(async () => {
        for (let id of body.selectIDs) {
          const connection: any = await this.dbWrite.connection;
          let request = new mssql.Request(connection.driver.master);
          request.input("order_Id", id);
          request.input("user_Id", userInfo.user_Id);
          request.output("outMsg", mssql.NVarChar(2000));
          let result = await request.execute("sp_Sale_Order_Sorting");
          let outMsg = result.output.outMsg;
          if (outMsg != null && outMsg) {
            this.info.msg = outMsg;
            this.info.result = false;
          }
        }
      }, 0);
      //#endregion
      this.info.result = true;
      this.info.msg = "审核成功";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 开始分拣
  /**
   * 修改日期:2019/1/12
   * 开始分拣
   */
  @Post()
  public async sorting() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    if (!Array.isArray(body.selectIDs)) {
      this.info.result = false;
      this.info.msg = "数据不存在";
      ctx.body = this.info;
      return;
    }
 
    try {
      // 执行分拣
      for (let id of body.selectIDs) {
        const connection: any = await this.dbWrite.connection;
        let request = new mssql.Request(connection.driver.master);
        request.input("order_Id", id);
        request.input("user_Id", userInfo.user_Id);
        request.output("outMsg", mssql.NVarChar(2000));
        let result = await request.execute("sp_Sale_Order_Sorting");
        let outMsg = result.output.outMsg;
        if (outMsg != null && outMsg) {
          this.info.msg = outMsg;
          this.info.result = false;
          ctx.body = this.info;
          return;
        }
        this.info.msg = "分拣完成";
        this.info.result = true;
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 销售订单改变快递公司
  @Post()
  public async changeExpressCorp() {
    let userInfo = await this.userInfo;
    let { ctx } = this;
    let body = ctx.request.body;
    if (!Array.isArray(body.ids) || !body.ids.length) {
      this.info.result = false;
      this.info.msg = "没有可执行的单号";
      return;
    }
    let orderList = await this.dbRead.find(SaleOrder, {
      order_Id: In(body.ids),
      userProduct_Id: userInfo.userProduct_Id
    });
 
    for (let orderInfo of orderList) {
      await this.dbWrite.update(
        SaleOrder,
        {
          order_Id: orderInfo.order_Id,
          statusText: In(["待审核", "审核成功", "改变物流", "发运完成", "打包完成"])
        },
        {
          expressCode: null,
          expressCorp_Id: body.expressCorp_Id,
          expressCorpName: body.expressCorpName,
          expressCorpType: body.expressCorpType
        }
      );
 
      await this.dbWrite.update(
        SaleOuter,
        {
          order_Id: orderInfo.order_Id,
          statusText: In(["待审核", "审核成功", "改变物流", "发运完成", "打包完成"])
        },
        {
          expressCode: null,
          expressCorp_Id: body.expressCorp_Id,
          expressCorpName: body.expressCorpName
        }
      );
 
      // 圆通号段分配清除
      await this.dbWrite.update(
        BaseExpressSpareCode,
        {
          order_Id: orderInfo.order_Id
        },
        {
          order_Id: null,
          orderCode: null
        }
      );
 
      ctx.service.outbound.orderHelper.setStatusHistory(body.order_Id, orderInfo.statusID, 25, "订单状态", "改变物流");
    }
 
    //调度快递接口数据
    const connection: any = await this.dbWrite.connection;
    let request = new mssql.Request(connection.driver.master);
    let result = await request.execute("sp_Interface_ExpressOrder");
    let outMsg = result.output.outMsg;
    if (outMsg != null && outMsg) {
      this.info.msg = outMsg;
      this.info.result = false;
      ctx.body = this.info;
    }
 
    if (result) {
      this.info.result = true;
      this.info.msg = "确认成功";
    } else {
      this.info.result = false;
      this.info.msg = "执行失败";
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region updateExpressCode 更新快递单号
  @Post()
  public async updateExpressCode() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    //#region 更新运单号
    if (!body.expressCode) {
      await this.dbWrite.update(
        SaleOrder,
        {
          order_Id: body.order_Id,
          userProduct_Id: userInfo.userProduct_Id
        },
        {
          expressCode: null
        }
      );
 
      await this.dbWrite.update(
        SaleOuter,
        {
          order_Id: body.order_Id,
          userProduct_Id: userInfo.userProduct_Id
        },
        {
          expressCode: null
        }
      );
 
      await this.dbWrite.update(
        SaleSendList,
        {
          order_Id: body.order_Id,
          userProduct_Id: userInfo.userProduct_Id
        },
        {
          expressCode: null
        }
      );
 
      await this.dbWrite.update(
        SaleOrderPrintList,
        {
          order_Id: body.order_Id,
          userProduct_Id: userInfo.userProduct_Id
        },
        {
          expressCode: null
        }
      );
    } else {
      await this.dbWrite.update(
        SaleOrder,
        {
          order_Id: body.order_Id,
          userProduct_Id: userInfo.userProduct_Id
        },
        {
          expressCode: body.expressCode
        }
      );
 
      await this.dbWrite.update(
        SaleOuter,
        {
          order_Id: body.order_Id,
          userProduct_Id: userInfo.userProduct_Id
        },
        {
          expressCode: body.expressCode
        }
      );
 
      await this.dbWrite.update(
        SaleSendList,
        {
          order_Id: body.order_Id,
          userProduct_Id: userInfo.userProduct_Id
        },
        {
          expressCode: body.expressCode
        }
      );
 
      await this.dbWrite.update(
        SaleOrderPrintList,
        {
          order_Id: body.order_Id,
          userProduct_Id: userInfo.userProduct_Id
        },
        {
          expressCode: body.expressCode
        }
      );
    }
 
    this.info.result = true;
    this.info.msg = "快递单号更新成功!";
 
    ctx.body = this.info;
    //#endregion
  }
  //#endregion
 
  //#region 获得分拣规则
  @Post()
  public async getSortingRule() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    var orderList_Id = body.orderList_Id;
    var dataList = await this.dbRead.find(SaleSortingRulePosition, {
      orderList_Id: orderList_Id
    });
    this.info.result = true;
    this.info.data = dataList;
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 添加单据
  @Post()
  public async addNewOrder() {
    let { ctx } = this;
    try {
      let body = ctx.request.body;
      let userInfo = await ctx.helper.userInfo();
      var taskForm = body.taskForm;
      if (!taskForm.product_Id) {
        this.info.result = false;
        this.info.msg = "请选择物料号";
        ctx.body = this.info;
        return;
      }
      if (!taskForm.destination_Id) {
        this.info.result = false;
        this.info.msg = "请选择目的地";
        ctx.body = this.info;
        return;
      }
      if (["焊装出库", "调件出库"].indexOf(taskForm.orderType) >= 0) {
        // if (!taskForm.outTransId) {
        //   this.info.result = false;
        //   this.info.msg = "请选择出库口";
        //   ctx.body = this.info;
        //   return;
        // }
 
        let where = {
          order_Id: Raw(() => {
            return `order_Id IN(Select Order_Id from Sale_OrderList L where SaleOrder.order_Id=L.order_Id And L.Product_Id=${taskForm.product_Id})`;
          }),
          userProduct_Id: userInfo.userProduct_Id,
          orderType: In(["焊装出库", "调件出库"]),
          statusText: "新建"
        };
        let orderInfo = await this.dbRead.findOne(SaleOrder, where);
        if (orderInfo) {
          this.info.result = false;
          this.info.msg = `物料[${taskForm.productName}]已创建,不可重复创建!`;
          this.ctx.body = this.info;
          return;
        }
      }
      let consignorInfo = await this.dbRead.findOne(BaseConsignor, { userProduct_Id: userInfo.userProduct_Id });
      // 获取物料信息
      let productInfo = await this.dbRead.findOne(BaseProductInfo, {
        product_Id: taskForm.product_Id
      });
 
      // 记录当前用户常用的物料
      var historyInfo = await this.dbRead
        .createQueryBuilder(BaseProductInfoHistory, "t")
        .where("user_Id=:user_Id and userProduct_Id=:userProduct_Id and product_Id=:product_Id", {
          user_Id: userInfo.user_Id,
          userProduct_Id: userInfo.userProduct_Id,
          product_Id: productInfo.product_Id
        })
        .getOne();
      if (historyInfo) {
        historyInfo.useNum += 1;
        await this.dbWrite.save(historyInfo);
      } else {
        historyInfo = new BaseProductInfoHistory();
        historyInfo.user_Id = userInfo.user_Id;
        historyInfo.userProduct_Id = userInfo.userProduct_Id;
        historyInfo.product_Id = productInfo.product_Id;
        historyInfo.useNum = 1;
        await this.setAccountInfo(historyInfo);
        await this.dbWrite.save(historyInfo);
      }
 
      let plateTypeInfo = await this.dbRead.findOne(BasePlateType, {
        plateType: productInfo.plateType
      });
      if (!plateTypeInfo) {
        this.info.result = false;
        this.info.msg = `${productInfo.plateType}器具种类基本信息中不存在`;
        ctx.body = this.info;
        return;
      }
      if (!plateTypeInfo.storage_Id) {
        this.info.result = false;
        this.info.msg = `${productInfo.plateType}器具种类未设置仓库信息`;
        ctx.body = this.info;
        return;
      }
 
      // 获取目的地
      let destinationInfo = await this.dbRead.findOne(BaseDestination, {
        destination_Id: taskForm.destination_Id
      });
      let ppList = await this.dbRead.find(vBaseProductPosition, {
        where: {
          storage_Id: plateTypeInfo.storage_Id,
          product_Id: taskForm.product_Id,
          validQty: MoreThan(0),
          positionType: 1
        },
        order: {
          inStorageDate: "ASC"
        }
      });
      if (!ppList.length) {
        this.info.result = false;
        this.info.msg = `${productInfo.productCode}物料库存不足`;
        ctx.body = this.info;
        return;
      }
 
      // 创建单据, validQty需要出库数量
      let createOrder = async (validQty: number, ppInfo?: vBaseProductPosition) => {
        var orderInfo = new SaleOrder();
        orderInfo.orderCode = await ctx.service.common.getCodeRegular(112);
        orderInfo.consignorName = consignorInfo.consignorName;
        orderInfo.consignor_Id = consignorInfo.consignor_Id;
        orderInfo.consignorCode = consignorInfo.consignorCode;
        orderInfo.storageName = plateTypeInfo.storageName;
        orderInfo.storage_Id = plateTypeInfo.storage_Id;
        orderInfo.statusText = "新建";
        orderInfo.statusID = 1;
        orderInfo.sortingStatus = 1;
        orderInfo.orderType = taskForm.orderType;
        orderInfo.orderExit = taskForm.orderExit; // 出库区域
        // 立体库需要
        if (ppInfo) {
          orderInfo.plateCode = ppInfo.plateCode;
          orderInfo.plateType = ppInfo.plateType;
        }
        orderInfo.totalQuantityOrder = validQty;
        orderInfo.destinationName = destinationInfo.destinationName;
        orderInfo.destination_Id = destinationInfo.destination_Id;
        orderInfo.outTransId = taskForm.outTransId;
        await this.setAccountInfo(orderInfo);
        await this.dbWrite.save(orderInfo);
 
        var orderListInfo = new SaleOrderList();
        orderListInfo.order_Id = orderInfo.order_Id;
        orderListInfo.productName = productInfo.productName;
        orderListInfo.productCode = productInfo.productCode;
        orderListInfo.product_Id = productInfo.product_Id;
        orderListInfo.productModel = productInfo.productModel;
        orderListInfo.sortingStatus = 1;
        if (ppInfo) {
          orderListInfo.quantityOrder = ppInfo.productStorage;
          orderListInfo.positionName = ppInfo.positionName;
          orderListInfo.planQty = ppInfo.productStorage;
        }
        await this.dbWrite.save(orderListInfo);
      };
 
      // 需要出库数量
      let validQty = body.taskForm.validQty;
      if (["EU箱出库", "外协件出库"].indexOf(taskForm.orderType) >= 0) {
        await createOrder(validQty);
      } else {
        // 焊装出库任务,调件出库任务
        for (let ppInfo of ppList) {
          if (validQty <= 0) break;
          if (validQty > ppInfo.validQty) {
            await createOrder(ppInfo.validQty, ppInfo);
            validQty -= ppInfo.validQty;
          } else {
            await createOrder(validQty, ppInfo);
            validQty = 0;
          }
        }
      }
      this.info.result = true;
      this.info.msg = "添加成功!";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 出库指令
  @Post()
  public async outCommand() {
    let { ctx } = this;
    try {
      let body = ctx.request.body;
      let userInfo = await ctx.helper.userInfo();
      var ids = body.ids;
      if (!Array.isArray(ids) || !ids.length) {
        this.info.result = false;
        this.info.msg = "至少选择一条进行出库指令";
        ctx.body = this.info;
        return;
      }
      for (let id of ids) {
        let orderInfo = await this.dbRead.findOne(SaleOrder, id); // 获取出库主表数据
        let orderInfoList = await this.dbRead.find(SaleOrderList, {
          order_Id: id
        });
        if (orderInfo.statusID === 2) {//枚举Sale_Order_StatusEnum,2 表示已下发 
          this.info.result = true;
          this.info.msg = "任务指令单已生成,不允许重复生成";
          ctx.body = this.info;
          return;
        }
        //debugger
        //增加对明细的判断 【Editby shaocx,2022-10-22】
        if (orderInfoList.length == 0) {
          this.info.result = true;
          this.info.msg = "出库单号" + orderInfo.orderCode + "请至少有一个明细";
          ctx.body = this.info;
          return;
        }
        let num = 0;
        for (var item of orderInfoList) {
          num += item.quantityOrder;
        }
        if (num == 0) {
          this.info.result = true;
          this.info.msg = "出库单号" + orderInfo.orderCode + "明细本次出库数量不能全部都为0";
          ctx.body = this.info;
          return;
        }
 
        await this.dbWrite.update(
          SaleOrder,
          {
            order_Id: id,
            userProduct_Id: userInfo.userProduct_Id
          },
          {
            statusText: "待下架",
            statusID: 8
          }
        );
        for (var item of orderInfoList) {
          await this.dbWrite.update(
            SaleOrderList,
            {
              orderList_Id: item.orderList_Id,
              userProduct_Id: userInfo.userProduct_Id
            },
            {
              extendField03: "待下架"
            }
          );
        }
 
        this.info.result = true;
        this.info.msg = "出库指令执行成功!";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 出库指令
  @Post()
  public async lackAnalysis() {
    let { ctx } = this;
    try {
      let body = ctx.request.body;
      let userInfo = await ctx.helper.userInfo();
      var ids = body.ids;
 
      let orderInfo = await this.dbRead.findOne(SaleOrder, ids); // 获取出库主表数据
      let orderInfoList = await this.dbRead.find(SaleOrderList, {
        order_Id: ids
      });
      //let lackInfo=
      if (orderInfo.statusText === "部分出库") {
        this.info.result = true;
        this.info.msg = "任务指令单已生成,不允许重复生成";
        ctx.body = this.info;
        return;
      }
      if (orderInfo.statusText === "新建") {
        this.info.result = true;
        this.info.msg = "任务指令单已生成,不允许重复生成";
        ctx.body = this.info;
        return;
      }
 
      await this.dbWrite.update(
        SaleOrder,
        {
          order_Id: ids,
          userProduct_Id: userInfo.userProduct_Id
        },
        {
          statusText: "部分出库",
          statusID: 1
        }
      );
      for (var item of orderInfoList) {
        await this.dbWrite.update(
          SaleOrderList,
          {
            orderList_Id: item.orderList_Id,
            userProduct_Id: userInfo.userProduct_Id
          },
          {
            extendField03: "待下架",
            quantityOrder: 0,
            validQuantity: item.validQuantity - item.quantityOrder
          }
        );
        let orderInfos = await this.dbRead.find(SaleOrderList, {
          order_Id: ids,
          validQuantity: MoreThan(0)
        });
        if (orderInfos.length === 0) {
          await this.dbWrite.update(
            SaleOrder,
            {
              order_Id: ids,
              userProduct_Id: userInfo.userProduct_Id
            },
            {
              statusText: "已完成",
              statusID: 7
            }
          );
        }
      }
      let where = await ctx.service.common.getWhere();
      let fieldStr = `k.SaleCode AS '销售单号',k.SaleCodeItem AS '销售项号',K.MaterailCode AS '物料编号',
      K.MaterailName AS '物料名称',K.LackQuantity AS '缺料数量',k.TemporaryQuantity AS '暂存区数量',
      k.StorageQuantity AS '立库区数量',k.StorageUnit AS 库存单位`;
      let fields = fieldStr.split(",");
      let wayBillList: Array<any> = await this.dbRead
        .createQueryBuilder(LackOrder, "t")
        .select(fields)
        .innerJoin("LackOrderList", "k", "k.LackId = t.LackId")
        .where(where)
        .andWhere("t.WorkCode = WorkCode", {
          WorkCode: orderInfo.orderCode
        })
        // .orderBy({
        //   lackId: "DESC"
        // })
        .take(5000)
        .getRawMany();
      let lackOrders = await this.dbRead.findOne(LackOrder, {
        workCode: orderInfo.orderCode
      });
 
      let root = path.resolve();
      let url = "/download/缺料统计表.xlsx";
      let fileName = root + url.replace(/\//g, path.sep);
      let pathToCreate = fileName.substring(0, fileName.lastIndexOf(path.sep));
      ctx.helper.mkdir(pathToCreate);
      let opts = {
        cellStyles: true,
        origin: 2 // 跳过前N行
      };
 
      let jsonWorkSheet = XLSX.utils.json_to_sheet(wayBillList, opts);
 
      // 构造workBook
      let workBook = {
        SheetNames: ["缺料单"],
        Sheets: {
          缺料单: jsonWorkSheet
          // 商品信息: arrayWorkSheet
        }
      };
      jsonWorkSheet["!cols"] = [
        //设置宽度
        { wpx: 100 }, //1-第一列
        { wpx: 140 }, //2-第二列
        { wpx: 180 }, //3
        { wpx: 180 }, //3
        { wpx: 100 }, //3
        { wpx: 100 }, //3
        { wpx: 100 }, //3
        { wpx: 100 }, //3
        { wpx: 100 } //3
      ];
 
      let worksheet = workBook.Sheets["缺料单"];
      // let worksheet1 = workBook.Sheets["商品信息"];
      // 设置标题格式
      for (let index = 0; index < 29; index++) {
        let col = XLSX.utils.encode_col(index); // 转为字母
        let cell = worksheet[col + "1"];
        if (!cell) continue;
 
        cell.s = {
          alignment: {
            //对齐方式
            vertical: "center",
            horizontal: "center"
          },
          font: {
            sz: 12,
            bold: false
          }
        };
      }
      // 设置标题格式
      for (let index = 0; index < 11; index++) {
        let col = XLSX.utils.encode_col(index); // 转为字母
        let cell = worksheet[col + "1"];
        if (!cell) continue;
 
        cell.s = {
          alignment: {
            //对齐方式
            vertical: "center",
            horizontal: "center"
          },
          font: {
            sz: 10,
            bold: true,
            name: "宋体"
          },
          fill: {
            fgColor: {
              rgb: "ff0000" // 单元格背景颜色
            }
          }
        };
      }
      worksheet["A1"] = {
        v: "广州西门子配变智能化工厂-立库区PICKLIST缺料报表",
        s: {
          font: {
            sz: 30,
            bold: true,
            name: "Arial"
          },
          alignment: {
            vertical: "center"
          }
        }
      };
      worksheet["A2"] = {
        v: "生产订单号:",
        s: {
          font: {
            sz: 10,
            bold: true,
            name: "Arial"
          },
          alignment: {
            //垂直水平居中
            vertical: "center",
            horizontal: "center"
          }
        }
      };
      worksheet["B2"] = {
        v: lackOrders.workCode,
        s: {
          font: {
            sz: 10,
            bold: true,
            name: "Arial"
          },
          alignment: {
            //垂直水平居中
            vertical: "center",
            horizontal: "center"
          }
        }
      };
      worksheet["C2"] = {
        v: "跟踪号:",
        s: {
          font: {
            sz: 10,
            bold: true,
            name: "Arial"
          },
          alignment: {
            //垂直水平居中
            vertical: "center",
            horizontal: "center"
          }
        }
      };
      worksheet["D2"] = {
        v: lackOrders.trackCode,
        s: {
          font: {
            sz: 10,
            bold: true,
            name: "Arial"
          },
          alignment: {
            //垂直水平居中
            vertical: "center",
            horizontal: "center"
          }
        }
      };
      worksheet["E2"] = {
        v: "总套数:",
        s: {
          font: {
            sz: 10,
            bold: true,
            name: "Arial"
          },
          alignment: {
            //垂直水平居中
            vertical: "center",
            horizontal: "center"
          }
        }
      };
      worksheet["F2"] = {
        v: lackOrders.totalCount,
        s: {
          font: {
            sz: 10,
            bold: true,
            name: "Arial"
          },
          alignment: {
            //垂直水平居中
            vertical: "center",
            horizontal: "center"
          }
        }
      };
      worksheet["G2"] = {
        v: "出库套数:",
        s: {
          font: {
            sz: 10,
            bold: true,
            name: "Arial"
          },
          alignment: {
            //垂直水平居中
            vertical: "center",
            horizontal: "center"
          }
        }
      };
      worksheet["H2"] = {
        v: lackOrders.counting + "_" + lackOrders.finishCount,
        s: {
          font: {
            sz: 10,
            bold: true,
            name: "Arial"
          },
          alignment: {
            //垂直水平居中
            vertical: "center",
            horizontal: "center"
          }
        }
      };
      worksheet["I2"] = {
        v: "完成套数:",
        s: {
          font: {
            sz: 10,
            bold: true,
            name: "Arial"
          },
          alignment: {
            //垂直水平居中
            vertical: "center",
            horizontal: "center"
          }
        }
      };
      worksheet["J2"] = {
        v: lackOrders.finishCount,
        s: {
          font: {
            sz: 10,
            bold: true,
            name: "Arial"
          },
          alignment: {
            //垂直水平居中
            vertical: "center",
            horizontal: "center"
          }
        }
      };
      var wopts = { bookType: "xlsx", bookSST: false, type: "file" };
      xlsxStye.writeFileSync(workBook, fileName, wopts);
 
      // XLSX.writeFile(workBook, fileName);
 
      //#endregion
      this.info.result = true;
      this.info.data = {
        url: url
      };
      this.info.msg = "导出成功!";
 
      this.info.result = true;
      this.info.msg = "缺料分析指令执行成功!";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 领取任务/结束任务
  @Post()
  public async getTask() {
    let { ctx } = this;
    try {
      let body = ctx.request.body;
      var id = body.id;
      var relationState = body.relationState;
      if (!id) {
        this.info.result = false;
        this.info.msg = "请选中一条";
        ctx.body = this.info;
        return;
      }
 
      var dataInfo = await this.dbRead.findOne(SaleOrder, {
        order_Id: id
      });
      dataInfo.relationState = relationState;
      await this.dbWrite.save(dataInfo);
      this.info.result = true;
      this.info.msg = "操作成功!";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 返修
  @Post()
  public async backRepairOrder() {
    let { ctx } = this;
    try {
      let body = ctx.request.body;
      var order_Id = body.order_Id;
      var nowQuantityOrder = body.nowQuantityOrder;
      // var nowQuantityOrder = body.nowQuantityOrder;
      var orderList_Id = body.orderList_Id;
      if (!order_Id) {
        this.info.result = false;
        this.info.msg = "order_Id不能为空";
        ctx.body = this.info;
        return;
      }
      var orderInfo = await this.dbRead.findOne(SaleOrder, {
        order_Id: order_Id
      });
      if (orderInfo.statusText !== "完全出库") {
        this.info.result = false;
        this.info.msg = "只有完全出库可以进行操作!";
        ctx.body = this.info;
        return;
      }
      var saleOrderList = await this.dbRead.findOne(SaleOrderList, {
        orderList_Id: orderList_Id
      });
 
      // 当前商品只会占一条库存明细
      var holderInfo = await this.dbRead.findOne(vBaseProductPlaceHolder, {
        mainID: order_Id,
        detailID: orderList_Id,
        className: "销售订单"
      });
      if (!holderInfo) {
        this.info.result = false;
        this.info.msg = "获取库存异常";
        ctx.body = this.info;
        return;
      }
 
      var postionInfo = await this.dbRead.findOne(BaseProductPosition, {
        productPosition_Id: holderInfo.productPosition_Id
      });
 
      orderInfo.statusText = "已回库";
      await this.dbWrite.save(orderInfo);
 
      // 新建焊装返修回库单
      var purchaseInfo = new PurchaseOrder();
      purchaseInfo.orderCode = await ctx.service.common.getCodeRegular(1803);
      purchaseInfo.plateCode = orderInfo.plateCode;
      purchaseInfo.plateName = orderInfo.plateName;
      purchaseInfo.sourceId = saleOrderList.order_Id;
      purchaseInfo.repairType = "焊装返修";
      purchaseInfo.orderType = "焊装返修";
      purchaseInfo.storageName = orderInfo.storageName;
      purchaseInfo.storage_Id = orderInfo.storage_Id;
      purchaseInfo.statusText = "新建";
      purchaseInfo.totalQuantity = nowQuantityOrder;
      purchaseInfo.consignor_Id = orderInfo.consignor_Id;
      purchaseInfo.consignorCode = orderInfo.consignorCode;
      purchaseInfo.consignorName = orderInfo.consignorName;
      // purchaseInfo.partStatus = "焊装待返修";
      await this.dbWrite.save(purchaseInfo);
 
      var purchaselistInfo = new PurchaseOrderList();
      purchaselistInfo.order_Id = purchaseInfo.order_Id;
      purchaselistInfo.sourceMainID = saleOrderList.order_Id;
      purchaselistInfo.sourceListID = saleOrderList.orderList_Id;
      purchaselistInfo.product_Id = postionInfo.product_Id;
      purchaselistInfo.productCode = postionInfo.productCode;
      purchaselistInfo.productName = postionInfo.productName;
      purchaselistInfo.productModel = postionInfo.productModel;
      purchaselistInfo.quantity = nowQuantityOrder;
      purchaselistInfo.ratePrice = postionInfo.ratePrice;
      purchaselistInfo.purchaseMoney = nowQuantityOrder * postionInfo.ratePrice;
      purchaselistInfo.totalPackageQty = nowQuantityOrder;
      purchaselistInfo.batchNumber = postionInfo.batchNumber;
      purchaselistInfo.produceDate = postionInfo.produceDate;
      await this.dbWrite.save(purchaseInfo);
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 余料回库
  @Post()
  public async backOutOrder() {
    let { ctx } = this;
    try {
      let body = ctx.request.body;
      var order_Id = body.order_Id;
      //var nowQuantityOrder = body.order_Id;
      var nowQuantityOrder = body.nowQuantityOrder;
      var orderList_Id = body.orderList_Id;
      if (!order_Id) {
        this.info.result = false;
        this.info.msg = "order_Id不能为空";
        ctx.body = this.info;
        return;
      }
      var orderInfo = await this.dbRead.findOne(SaleOrder, {
        order_Id: order_Id
      });
      if (orderInfo.statusText !== "完全出库") {
        this.info.result = false;
        this.info.msg = "只有完全出库可以进行操作!";
        ctx.body = this.info;
        return;
      }
      var saleOrderList = await this.dbRead.findOne(SaleOrderList, {
        orderList_Id: orderList_Id
      });
 
      // 当前商品只会占一条库存明细
      var holderInfo = await this.dbRead.findOne(vBaseProductPlaceHolder, {
        mainID: order_Id,
        detailID: orderList_Id,
        className: "销售订单"
      });
      if (!holderInfo) {
        this.info.result = false;
        this.info.msg = "获取库存异常";
        ctx.body = this.info;
        return;
      }
 
      var postionInfo = await this.dbRead.findOne(BaseProductPosition, {
        productPosition_Id: holderInfo.productPosition_Id
      });
 
      // 通过物料寻找器具种类,取器具种类的库区和仓库 寻找空货位
      var baseProductInfo = await this.dbRead.findOne(BaseProductInfo, {
        product_Id: saleOrderList.product_Id
      });
      if (!baseProductInfo) {
        this.info.result = false;
        this.info.msg = "获取商品信息异常";
        ctx.body = this.info;
        return;
      }
      var basePlateTypeInfo = await this.dbRead.findOne(BasePlateType, {
        plateType_Id: baseProductInfo.plateType_Id
      });
 
      if (!basePlateTypeInfo) {
        this.info.result = false;
        this.info.msg = "获取器具种类异常";
        ctx.body = this.info;
        return;
      }
      let basePositionInfo = await this.dbRead
        .createQueryBuilder(BasePosition, "t")
        .where(
          "storage_Id=:storage_Id and areaCode=:areaCode and   NOT EXISTS(SELECT 1 FROM dbo.vBase_ProductPosition where positionName=t.positionName  and validQty>0 )",
          { storage_Id: basePlateTypeInfo.storage_Id, areaCode: basePlateTypeInfo.areaCode }
        )
        .getOne();
      if (!basePositionInfo) {
        this.info.result = false;
        this.info.msg = basePlateTypeInfo.storageName + basePlateTypeInfo.areaCode + "没有空货位了";
        ctx.body = this.info;
        return;
      }
 
      postionInfo.positionName = basePositionInfo.positionName;
      postionInfo.areaCode = basePositionInfo.areaCode;
      postionInfo.storageName = basePositionInfo.storageName;
      postionInfo.storage_Id = basePositionInfo.storage_Id;
      postionInfo.productPosition_Id = null;
      postionInfo.productStorage = nowQuantityOrder;
      await this.dbWrite.save(postionInfo);
 
      orderInfo.statusText = "已回库";
      await this.dbWrite.save(orderInfo);
 
      // 新建出库余料回库单
      var saleReturnInfo = new SaleReturn();
      saleReturnInfo.returnCode = await ctx.service.common.getCodeRegular(112);
      saleReturnInfo.order_Id = orderInfo.order_Id;
      saleReturnInfo.orderCode = orderInfo.orderCode;
      saleReturnInfo.consignor_Id = orderInfo.consignor_Id;
      saleReturnInfo.consignorCode = orderInfo.consignorCode;
      saleReturnInfo.consignorName = orderInfo.consignorName;
      saleReturnInfo.orderType = "出库余料回库";
      saleReturnInfo.storage_Id = basePositionInfo.storage_Id;
      saleReturnInfo.storageName = basePositionInfo.storageName;
      saleReturnInfo.statusText = "全部收货";
      saleReturnInfo.totalQuantity = postionInfo.productStorage;
      await this.dbWrite.save(saleReturnInfo);
 
      var saleReturnListInfo = new SaleReturnList();
      saleReturnListInfo.return_Id = saleReturnInfo.return_Id;
      saleReturnListInfo.product_Id = postionInfo.product_Id;
      saleReturnListInfo.productCode = postionInfo.productCode;
      saleReturnListInfo.productName = postionInfo.productName;
      saleReturnListInfo.quantityOrder = postionInfo.productStorage;
      saleReturnListInfo.plateCode = orderInfo.plateCode;
      saleReturnListInfo.plateName = orderInfo.plateName;
      await this.dbWrite.save(saleReturnListInfo);
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 出库操作
  @Post()
  public async outOrder() {
    let { ctx } = this;
    try {
      let body = ctx.request.body;
      var order_Id = body.order_Id;
      var nowQuantityOrder = body.nowQuantityOrder;
      var orderList_Id = body.orderList_Id;
      if (!order_Id) {
        this.info.result = false;
        this.info.msg = "order_Id不能为空";
        ctx.body = this.info;
        return;
      }
      var orderInfo = await this.dbRead.findOne(SaleOrder, {
        order_Id: order_Id
      });
      if (orderInfo.statusText !== "下架完成") {
        this.info.result = false;
        this.info.msg = "只有下架完成可以进行操作!";
        ctx.body = this.info;
        return;
      }
      var saleOrderList = await this.dbRead.findOne(SaleOrderList, {
        orderList_Id: orderList_Id
      });
      // 减去该单原库存明细库存 当前商品只会占一条库存明细
      var holderInfo = await this.dbRead.findOne(vBaseProductPlaceHolder, {
        mainID: order_Id,
        detailID: orderList_Id,
        className: "销售订单"
      });
      if (holderInfo && holderInfo.placeholderStorage > 0 && holderInfo.placeholderStorage >= nowQuantityOrder) {
        var postionInfo = await this.dbRead.findOne(BaseProductPosition, {
          productPosition_Id: holderInfo.productPosition_Id
        });
        postionInfo.productStorage = postionInfo.productStorage - nowQuantityOrder;
        await this.dbWrite.save(postionInfo);
      } else {
        this.info.result = false;
        this.info.msg = "占位异常,请核对占位信息";
        ctx.body = this.info;
        return;
      }
 
      // 清除该单据所有占位
      var holderList = await this.dbRead.find(BaseProductPlaceHolder, {
        mainID: order_Id,
        className: "销售订单"
      });
      for (var hold of holderList) {
        hold.placeholderStorage = 0;
        await this.dbWrite.save(hold);
      }
 
      orderInfo.statusID = nowQuantityOrder === holderInfo.placeholderStorage ? 12 : 36;
      orderInfo.statusText = nowQuantityOrder === holderInfo.placeholderStorage ? "完全出库" : "部分出库";
      await this.dbWrite.save(orderInfo);
      saleOrderList.quantityOuted = nowQuantityOrder;
      saleOrderList.validQuantity = saleOrderList.quantityOrder - nowQuantityOrder;
      await this.dbWrite.save(saleOrderList);
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 根据器具编号获取部分出库的单据
  @Post()
  public async GetBackByplateCode() {
    let { ctx } = this;
    try {
      let body = ctx.request.body;
      var plateCode = body.plateCode;
      if (!plateCode) {
        this.info.result = false;
        this.info.msg = "器具编号不能为空";
        ctx.body = this.info;
        return;
      }
 
      var dataInfo = await this.dbRead.findOne(SaleOrder, {
        where: {
          plateCode: plateCode,
          statusText: "部分出库"
        },
        order: { createDate: "DESC" }
      });
      if (dataInfo) {
        var saleOrderList = await this.dbRead.find(SaleOrderList, {
          order_Id: dataInfo.order_Id
        });
        dataInfo.saleOrderList = saleOrderList;
        this.info.data = dataInfo;
        this.info.result = true;
        this.info.msg = "获取成功";
      } else {
        this.info.result = false;
        this.info.msg = "当前器具编号未获取到部分出库的单据";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 根据器具编号获取完全出库的单据
  @Post()
  public async GetOutByplateCode() {
    let { ctx } = this;
    try {
      let body = ctx.request.body;
      var plateCode = body.plateCode;
      if (!plateCode) {
        this.info.result = false;
        this.info.msg = "器具编号不能为空";
        ctx.body = this.info;
        return;
      }
 
      var dataInfo = await this.dbRead.findOne(SaleOrder, {
        where: {
          plateCode: plateCode,
          statusText: "完全出库"
        },
        order: { createDate: "DESC" }
      });
      if (dataInfo) {
        var saleOrderList = await this.dbRead.find(SaleOrderList, {
          order_Id: dataInfo.order_Id
        });
        dataInfo.saleOrderList = saleOrderList;
        this.info.data = dataInfo;
        this.info.result = true;
        this.info.msg = "获取成功";
      } else {
        this.info.result = false;
        this.info.msg = "当前器具编号未获取到完全出库的单据";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 设置分拣规则
  @Post()
  public async setSortingRule() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    var ruleInfo = new SaleSortingRulePosition();
    ruleInfo.batchNumber = body.batchNumber;
    ruleInfo.consignor_Id = body.consignor_Id;
    ruleInfo.consignorCode = body.consignorCode;
    ruleInfo.consignorName = body.consignorName;
    ruleInfo.createDate = new Date();
    ruleInfo.createID = userInfo.user_Id;
    ruleInfo.creator = userInfo.userTrueName;
    ruleInfo.enable = 1;
 
    ruleInfo.order_Id = body.order_Id;
    ruleInfo.orderCode = body.orderCode;
    ruleInfo.orderList_Id = body.orderList_Id;
    ruleInfo.plateCode = body.plateCode;
    ruleInfo.positionName = body.positionName;
    ruleInfo.produceDate = body.produceDate || null;
    ruleInfo.product_Id = body.product_Id;
    ruleInfo.productCode = body.productCode;
    ruleInfo.singleSignCode = body.singleSignCode;
    ruleInfo.storage_Id = body.storage_Id;
    ruleInfo.storageName = body.storageName;
    await this.dbWrite.save(ruleInfo);
 
    this.info.result = true;
    this.info.msg = "添加成功!";
 
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 关闭分拣规则
  @Post()
  public async deleteSortingRule() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    var rule_Id = body.rule_Id;
    await this.dbWrite.delete(SaleSortingRulePosition, {
      rule_Id: rule_Id,
      userProduct_Id: userInfo.userProduct_Id
    });
 
    this.info.result = true;
    this.info.msg = "关闭成功!";
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 出库单数据导入
  @Post()
  public async importExcel() {
    setTimeout(async () => {
      await this.importExcelWork();
    }, 0);
 
    this.info.result = true;
    this.ctx.body = this.info;
  }
 
  private async importExcelWork() {
    let { ctx } = this;
    let body = ctx.request.body;
    let startDate = new Date();
    let fileUrl = body.fileUrl;
    let userInfo = await ctx.helper.userInfo();
    let storage = null;
    if (!fileUrl) {
      this.setMsg(`上传文件不存在`, "red");
      return;
    }
    try {
      let rootPath = path.resolve(); // 获得根目录
      let filePath = rootPath + path.sep + fileUrl.replace(/\//gi, path.sep); // 上传文件路径
      var workbook = XLSX.readFile(filePath); //整个 excel 文档
      var sheetNames = workbook.SheetNames; //获取所有工作薄名
      var sheet1 = workbook.Sheets[sheetNames[0]]; //根据工作薄名获取工作薄
      let dataList = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
      this.info.result = true;
      if (!dataList.length) {
        this.setMsg(`没有可导入的数据`, "red");
        return;
      }
      storage = await this.dbRead.findOne(BaseStorage, {
        storageName: body.storageName,
        userProduct_Id: userInfo.userProduct_Id
      });
 
      // 全部快递公司
      // let expressList = await this.dbRead.find(BaseExpressCorp, {
      //   userProduct_Id: userInfo.userProduct_Id
      // });                        暂时不要
      // 全部货主信息
      let consignorList = await this.dbRead.find(BaseConsignor, {
        consignorName: body.consignorName,
        userProduct_Id: userInfo.userProduct_Id
      });
      // 记录已经验证过的物料,不在读取数据库
      let existProductList = [];
      let index = 0;
      for (let item of dataList) {
        let productCode = "" + item["物料编号"];
        // let expressCorpName = "" + item["快递公司"];
        let storeOrderCode = "" + (item["店铺单号"] || "");
        // let consignorName = "" + item["货主"];
 
        // if (consignorName) {
        //   consignorName = body.consignorName;
        // }
 
        let _index = index + 1;
        if (_index % 20 === 1) {
          this.setMsg(`第${_index}行开始校验`, "blue");
        }
        // !item["收货人"] || !item["地址"] || 收货人、地址、|| !item["快递公司"]  不能删除,德胜不需要
        if (!item["数量"]) {
          this.setMsg(`${_index}、标黄[货主、数量、快递公司]部分数据不能为空`, "red");
        }
        //判断店铺是否已重复导入
        if (storeOrderCode) {
          let isExists = await this.dbRead.findOne(SaleOrder, {
            // consignorName: "" + item["货主"],
            storeOrderCode: storeOrderCode,
            userProduct_Id: userInfo.userProduct_Id
          });
          if (isExists) {
            this.setMsg(`${_index}、店铺订单号[${item["店铺单号"]}"]已存在,不能再次导入`, "red");
          }
        }
        //判断快递公司是否存在        德胜的不要
        // if (item["快递公司"]) {
        //   let isexisorder = expressList.find(item => item.expressCorpName === expressCorpName);
        //   if (!isexisorder) {
        //     this.setMsg(`${_index}、快递公司【${item["快递公司"]}】不存在`, "red");
        //   }
        // }
        //判断物料
        let isexesproduct = await this.dbRead.findOne(BaseProductInfo, {
          userProduct_Id: userInfo.userProduct_Id,
          productCode: productCode
        });
 
        if (!isexesproduct) {
          this.setMsg(`${_index}、物料编号在物料信息中[${productCode}"]不存在,数据导入失败`, "red");
        } else {
          existProductList.push(isexesproduct);
        }
        //判断货主
        // let isExisConsign = consignorList.find(item => item.consignorName === consignorName);
        // if (!isExisConsign) {
        //   this.setMsg(`${_index}、货主[${consignorName}"]不存在,数据导入失败`, "red");
        // }
        index++;
      }
 
      if (this.isMsgError) {
        this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
        this.setMsg("-1");
        return;
      }
 
      //订单分组
      let orderGroupList = dataList.reduce(
        (all: Array<any>, next) =>
          all.some(
            item =>
              item["货主"] == next["货主"] &&
              item["客户名称"] == next["客户名称"] &&
              item["收货人"] == next["收货人"] &&
              item["手机"] == next["手机"] &&
              item["快递公司"] == next["快递公司"] &&
              item["地址"] == next["地址"] &&
              item["订单类型"] == next["订单类型"] &&
              item["业务员"] == next["业务员"]
          )
            ? all
            : [...all, next],
        []
      );
      let groupIndex = 0;
      let details = [];
      for (let orderitem of orderGroupList) {
        groupIndex++;
        let orderCode = await ctx.service.common.getCodeRegular(112);
        // if (groupIndex % 20 === 1) {
        this.setMsg(`${groupIndex}/${orderGroupList.length}单、${orderCode}订单开始导入`, "blue");
        // }
        // #region 创建主表数据
        // let expressCorp = await this.dbRead.findOne(BaseExpressCorp, {
        //   expressCorpName: "" + orderitem["快递公司"],
        //   userProduct_Id: userInfo.userProduct_Id
        // });
        //判断货主
        let consign = await this.dbRead.findOne(BaseConsignor, {
          // consignorName: "" + orderitem["货主"],
          consignorName: "" + orderitem["货主"],
 
          userProduct_Id: userInfo.userProduct_Id
        });
        let isExisConsign = consignorList.find(item => item.consignorName === body.consignorName);
        // let corp = null;
        // let expressCorpName = orderitem["快递公司"];   // 德胜不需要,别的要
        let orderInfo = new SaleOrder();
        // if (expressCorpName) {                         // 德胜不需要,别的要
        //   let corp = expressList.find(item => item.expressCorpName === expressCorpName);
        //   if (corp) {
        //     orderInfo.expressCorp_Id = corp.expressCorp_Id;
        //     orderInfo.expressCorpName = expressCorpName;
        //     orderInfo.expressCorpType = corp.expressCorpType;
        //   }
        // }
        orderInfo.orderCode = orderCode;
        orderInfo.orderType = orderitem["订单类型"];
        orderInfo.storeOrderCode = orderitem["店铺单号"];
        if (!orderitem["货主"]) {
          orderInfo.consignor_Id = isExisConsign.consignor_Id;
          orderInfo.consignorCode = isExisConsign.consignorCode;
          orderInfo.consignorName = isExisConsign.consignorName;
        } else {
          orderInfo.consignor_Id = consign.consignor_Id;
          orderInfo.consignorCode = consign.consignorCode;
          orderInfo.consignorName = consign.consignorName;
        }
 
        orderInfo.storageName = storage.storageName;
        //客户信息
        // let kh = await this.dbRead.findOne(BaseClient, {
        //   clientShortName: "" + orderitem["客户名称"]
        // });
 
        // sql = `Select top 1 client_Id, clientCode from Base_Client Where clientShortName='${orderitem.clientShortName}'`;
        // let ht = await this.dbWrite.query(sql);
        // orderInfo.client_Id = kh.client_Id;
        // orderInfo.clientCode = kh.clientCode;
        orderInfo.clientShortName = orderitem["客户名称"] || orderitem["门店名称"];
        orderInfo.clientCode = orderitem["客户编号"] || orderitem["门店编号"];
        orderInfo.shippingName = orderitem["收货人"];
        orderInfo.expressCode = orderitem["快递单号"];
        orderInfo.provinceName = orderitem["省份"];
        orderInfo.cityName = orderitem["城市"];
        orderInfo.regionName = orderitem["区县"];
        orderInfo.shippingAddress = orderitem["地址"];
        orderInfo.mobile = orderitem["手机"];
        orderInfo.telephone = orderitem["座机"];
        // if (expressCorp) {
        //   orderInfo.expressCorp_Id = expressCorp.expressCorp_Id;
        //   orderInfo.expressCorpName = expressCorp.expressCorpName;
        //   orderInfo.expressCorpType = expressCorp.expressCorpType;
 
        // }
        orderInfo.shippingAmount = 0;
        orderInfo.totalPaid = 0;
        orderInfo.totalQuantityOrder = 0;
        if (storage) {
          orderInfo.storage_Id = storage.storage_Id;
          orderInfo.storageName = storage.storageName;
        }
        orderInfo.createID = userInfo.user_Id;
        orderInfo.creator = userInfo.userTrueName;
        orderInfo.createDate = new Date();
 
        orderInfo.statusID = 1;
        orderInfo.statusText = "待审核";
        orderInfo.auditing = 0;
        orderInfo.sortingStatus = 1;
        orderInfo.remark = null;
        orderInfo.userTrueName = orderitem["业务员"];
        orderInfo.applyDate = new Date();
        orderInfo.platUser_Id = userInfo.platUser_Id;
        orderInfo.platUserCode = userInfo.platUserCode;
        orderInfo.platUserName = userInfo.platUserName;
        orderInfo.platCorpName = userInfo.platCorpName;
        orderInfo.userProduct_Id = userInfo.userProduct_Id;
        orderInfo.userProductCode = userInfo.userProductCode;
        orderInfo.userProductAlias = userInfo.userProductAlias;
 
        // 获得当前运单下明细
        let detailList = dataList.filter(
          row =>
            row["货主"] === orderitem["货主"] &&
            row["客户名称"] === orderitem["客户名称"] &&
            row["收货人"] === orderitem["收货人"] &&
            row["手机"] === orderitem["手机"] &&
            row["快递公司"] === orderitem["快递公司"] &&
            row["地址"] === orderitem["地址"] &&
            row["订单类型"] === orderitem["订单类型"] &&
            row["业务员"] === orderitem["业务员"]
        );
 
        // 主表求和字段计算
        let total: any = detailList.reduce(
          (totalItem: any, currItem) => {
            totalItem.totalQuantityOrder += parseFloat(currItem["数量"]);
            totalItem.discountAmount += 0;
            totalItem.weight += 0;
            totalItem.grandTotal += 0;
            return totalItem;
          },
          { totalQuantityOrder: 0, discountAmount: 0, weight: 0, grandTotal: 0 }
        );
 
        orderInfo.totalQuantityOrder = total.totalQuantityOrder;
        orderInfo.discountAmount = total.discountAmount;
        orderInfo.weight = total.weight;
        orderInfo.grandTotal = total.grandTotal;
        await this.dbWrite.insert(SaleOrder, orderInfo);
        //#endregion
        // let saleorderInfo = await this.dbRead.findOne(SaleOrder, {
        //   orderCode: orderCode,
        //   userProduct_Id: userInfo.userProduct_Id
        // });
        //#region 明细数据
        for (let _detailInfo of detailList) {
          let expandFields = {}; // 扩展字段值
          let prodInfo = existProductList.find(item => item.productCode === "" + _detailInfo["物料编号"]);
          let detail = new SaleOrderList();
          detail.order_Id = orderInfo.order_Id;
          detail.product_Id = prodInfo.product_Id;
          detail.productCode = "" + prodInfo.productCode;
          detail.productName = _detailInfo["物料名称"] || prodInfo.productName;
          detail.productModel = _detailInfo["物料编号"] || "" + prodInfo.productModel;
          detail.productSpec = _detailInfo["物料规格"] || prodInfo.productSpec;
 
          detail.quantityOrder = _detailInfo["数量"];
          detail.salePrice = _detailInfo["销售单价"] || prodInfo.salePrice || 0;
          detail.discountRate = _detailInfo["货品折扣"] || 1;
          detail.salePriceDiscount = detail.salePrice * detail.discountRate;
          detail.subTotal = detail.salePriceDiscount * detail.quantityOrder;
          detail.discountAmount = _detailInfo["优惠金额"] || 0;
          detail.rowTotal = detail.salePrice || detail.quantityOrder;
          detail.rowPaid = _detailInfo["实收金额"] || 0;
          detail.attachAmount = _detailInfo["附加金额"];
          detail.purchasePrice = prodInfo.purchasePrice || 0;
          detail.createID = userInfo.user_Id;
          detail.creator = userInfo.userTrueName;
          detail.createDate = new Date();
          detail.remark = null;
          detail.weight = prodInfo.weight;
          detail.totalWeight = _detailInfo["数量"] * prodInfo.weight;
          detail.smallUnit = prodInfo.smallUnit;
          detail.bigUnit = prodInfo.bigUnit;
          detail.remark = _detailInfo["备注"];
          detail.batchNumber = _detailInfo["批次号"];
          detail.singleSignCode = _detailInfo["定制唯一码"];
 
          detail.rate = prodInfo.rate;
          detail.ratePrice = prodInfo.salePrice * prodInfo.rate;
          detail.rateMoney = prodInfo.ratePrice * detail.quantityOrder;
          // 拓展字段
          expandFields["purchaseamount"] = _detailInfo["数量"] * prodInfo.purchasePrice || 0;
          expandFields["salesvolume"] = _detailInfo["数量"] * _detailInfo["销售单价"] || prodInfo.salePrice || 0;
          expandFields["volume"] = _detailInfo["体积"];
          detail.expandFields = JSON.stringify(expandFields);
          details.push(detail);
          if (details.length >= 50) {
            await this.dbWrite.insert(SaleOrderList, details);
            details = [];
          }
        }
        //#endregion
      }
      if (details.length > 0) {
        await this.dbWrite.insert(SaleOrderList, details);
        details = [];
      }
      let sql = `
      UPDATE dbo.Sale_Order SET
      totalQuantityOrder = (SELECT SUM(quantityOrder) FROM dbo.Sale_OrderList WHERE order_Id = Sale_Order.order_Id),
      discountAmount = (SELECT SUM(discountAmount) FROM dbo.Sale_OrderList WHERE order_Id = Sale_Order.order_Id),    /*优惠金额*/
      subTotal = (SELECT SUM(subTotal) FROM dbo.Sale_OrderList WHERE order_Id = Sale_Order.order_Id),    /*折后金额*/
      unpaid = (SELECT SUM(subTotal) FROM dbo.Sale_OrderList WHERE order_Id = Sale_Order.order_Id),    /*未支付*/
      totalPaid = 0,                /*实收金额*/
      grandTotal = (SELECT SUM(rowTotal) FROM dbo.Sale_OrderList WHERE order_Id = Sale_Order.order_Id),            /*销售金额*/
      totalWeight = ISNULL((SELECT SUM(totalWeight) FROM dbo.Sale_OrderList WHERE order_Id = dbo.Sale_Order.order_Id), 0)
      WHERE dbo.Sale_Order.userProduct_Id = '${userInfo.userProduct_Id}' And CreateDate>=DateAdd(day, -1, getdate());`;
      await this.dbWrite.query(sql);
 
      let endDate = moment(Date.now());
      let totalSeconds = endDate.diff(startDate, "seconds");
      let msg = `导入完成,共耗时${totalSeconds}秒`;
      this.setMsg(msg);
    } catch (ex) {
      this.setMsg("出现异常:" + ex.message, "red");
    }
    this.setMsg("-1");
  }
  //#endregion
 
  //#region 新增出库单
  /**
   * 新增出库单
   */
  @Post()
  public async add() {
    let { ctx } = this;
    let body = ctx.body;
    let userInfo = await this.userInfo;
    let db = this.app.mongodb;
    try {
      // 记录数据到mongodb日志中
      let collection = db.collection("apiSaleOrder");
      let mongoData = JSON.parse(JSON.stringify(body));
      mongoData.createDate = new Date();
      mongoData.userProduct_Id = userInfo.userProduct_Id;
      let re = await collection.insert(mongoData);
      ctx.logger.info("apiSaleOrder将数据保存至MongoDB after:" + JSON.stringify(re));
    } catch { }
 
    if (!userInfo && !userInfo.userProduct_Id) {
      this.info.result = false;
      this.info.msg = "数据不存在";
      ctx.body = this.info;
      return;
    }
    let userProduct_Id = userInfo.userProduct_Id;
 
    if (!body.consignorCode) {
      this.info.result = false;
      this.info.msg = "货主编号不能为空";
      ctx.body = this.info;
      return;
    }
 
    if (!body.consignorName) {
      this.info.result = false;
      this.info.msg = "货主名称不能为空";
      ctx.body = this.info;
      return;
    }
 
    if (!Array.isArray(body.details) || !body.details.length) {
      this.info.result = false;
      this.info.msg = "出库单明细不能为空";
      ctx.body = this.info;
      return;
    }
 
    try {
      if (!body.consignorCode) {
        this.info.result = false;
        this.info.msg = "货主编号不能为空";
        ctx.body = this.info;
        return;
      }
 
      if (!body.consignorName) {
        this.info.result = false;
        this.info.msg = "货主名称不能为空";
        ctx.body = this.info;
        return;
      }
 
      if (!body.storeOrderCode) {
        this.info.result = false;
        this.info.msg = "ERP订单号不能为空";
        ctx.body = this.info;
        return;
      }
 
      if (!Array.isArray(body.details) || !body.details.length) {
        this.info.result = false;
        this.info.msg = "出库单明细不能为空";
        ctx.body = this.info;
        return;
      }
 
      // 验证单号是否已推送
      let storeOrderInfo = await this.dbRead.findOne(SaleOrder, {
        storeOrderCode: body.storeOrderCode,
        userProduct_Id: userProduct_Id
      });
      if (storeOrderInfo) {
        this.info.result = true;
        this.info.statusCode = 660;
        this.info.msg = body.storeOrderCode + "已存在,不允许重复推送";
        this.info.data = {
          order_Id: storeOrderInfo.order_Id,
          orderCode: storeOrderInfo.orderCode
        };
        ctx.body = this.info;
        return;
      }
 
      // 验证货主
      let consignorInfo = await this.dbRead.findOne(BaseConsignor, {
        consignorCode: body.consignorCode,
        userProduct_Id: userProduct_Id
      });
      if (!consignorInfo) {
        this.info.result = false;
        this.info.msg = "货主不存在";
        ctx.body = this.info;
        return;
      }
      this.body.consignor_Id = consignorInfo.consignor_Id;
      this.body.consignorCode = consignorInfo.consignorCode;
 
      // 接口推送时需要仓库
      let erp_storage_Id = await this.ctx.service.common.getConfigBool("erp_storage_Id");
      if (erp_storage_Id) {
        // 验证仓库
        let storageInfo = await this.dbRead.findOne(BaseStorage, {
          storageName: body.storageName,
          userProduct_Id: userProduct_Id
        });
        if (!storageInfo) {
          this.info.result = false;
          this.info.msg = "仓库不存在";
          ctx.body = this.info;
          return;
        }
        this.body.storage_Id = storageInfo.storage_Id;
      } else {
        // 清掉仓库
        this.body.storage_Id = null;
        this.body.storageName = null;
      }
      this.body.applyDate = new Date();
 
      let detailInfo: SaleOrderList;
      for (detailInfo of this.body.details) {
        let prodInfo = await this.dbRead.findOne(BaseProductInfo, {
          where: {
            productCode: detailInfo.productCode,
            userProduct_Id: userProduct_Id
          }
        });
        if (!prodInfo) {
          this.info.result = false;
          this.info.msg = detailInfo.productCode + "物料编号不存在";
          ctx.body = this.info;
          return;
        }
        detailInfo.product_Id = prodInfo.product_Id;
        detailInfo.productModel = prodInfo.productModel;
        detailInfo.productName = prodInfo.productName;
        detailInfo.productSpec = prodInfo.productSpec;
        detailInfo.weight = prodInfo.weight;
        detailInfo.totalWeight = prodInfo.weight * detailInfo.quantityOrder;
 
        if (!isNumber(detailInfo.quantityOrder)) {
          this.info.result = false;
          this.info.msg = detailInfo.productModel + "数量必须为数字";
          ctx.body = this.info;
          return;
        }
        if (!detailInfo.quantityOrder || detailInfo.quantityOrder <= 0) {
          this.info.result = false;
          this.info.msg = detailInfo.productModel + "数量必须大于0";
          ctx.body = this.info;
          return;
        }
      }
 
      // 主表求和字段计算
      let total: any = this.body.details.reduce(
        (totalItem: any, currItem) => {
          totalItem.totalQuantityOrder += currItem["quantityOrder"];
          totalItem.rowTotal += currItem.quantityOrder * (currItem.salePrice || 0);
          totalItem.subTotal += currItem.quantityOrder * (currItem.salePriceDiscount || 0);
 
          return totalItem;
        },
        { totalQuantityOrder: 0, rowTotal: 0, subTotal: 0 }
      );
 
      let code = await this.ctx.service.common.getCodeRegular(112);
      let dataInfo = new SaleOrder();
      dataInfo = Object.assign(dataInfo, this.body);
      dataInfo.orderCode = code;
      dataInfo.statusID = 1;
      dataInfo.statusText = "待审核";
      dataInfo.orderType = body.orderType || "常规出库单";
      dataInfo.totalQuantityOrder = total.totalQuantityOrder;
      dataInfo.weight = 0;
      dataInfo.totalWeight = 0;
      dataInfo.grandTotal = total.rowTotal;
      dataInfo.subTotal = total.subTotal;
      dataInfo.unpaid = dataInfo.subTotal;
      dataInfo.totalPaid = 0;
 
      // 接口默认快递类型、快递公司
      let erp_expressCorp_Id = await this.ctx.service.common.getConfig("erp_expressCorp_Id");
      if (erp_expressCorp_Id) {
        let corpInfo = await this.dbWrite.findOne(BaseExpressCorp, erp_expressCorp_Id);
        if (corpInfo) {
          dataInfo.expressCorp_Id = Number(erp_expressCorp_Id);
          dataInfo.expressCorpName = corpInfo.expressCorpName;
          dataInfo.expressCorpType = corpInfo.expressCorpType;
        }
      }
 
      // let erp_expressCorp_Id = this.ctx.service.common.getConfig("erp_expressCorp_Id");
      // if (!dataInfo.expressCorp_Id) {
      //   let corpInfo = await this.dbRead.findOne(BaseExpressCorp, {
      //     userProduct_Id: userInfo.userProduct_Id,
      //     expressCorp_Id: Number(erp_expressCorp_Id)
      //   });
      //   if (corpInfo) {
      //     dataInfo.expressCorp_Id = Number(erp_expressCorp_Id);
      //     dataInfo.expressCorpName = corpInfo.expressCorpName;
      //     dataInfo.expressCorpType = corpInfo.expressCorpType;
      //   }
      // }
 
      await this.setAccountInfo(dataInfo);
      await this.dbWrite.save(dataInfo);
 
      for (let detailInfo of this.body.details) {
        let detail = new SaleOrderList();
        detail = Object.assign(detail, detailInfo);
        detail.order_Id = dataInfo.order_Id;
        await this.dbWrite.save(detail);
      }
 
      this.info.result = true;
      this.info.msg = "订单保存成功";
      this.info.data = {
        order_Id: dataInfo.order_Id,
        orderCode: dataInfo.orderCode
      };
      let logColl = db.collection("taskLogs");
      let data = {
        type: "order.add",
        msg: "订单保存成功",
        order_Id: dataInfo.order_Id,
        orderCode: dataInfo.orderCode,
        createDate: new Date()
      };
      await logColl.insert(data);
    } catch (error) {
      this.info.result = false;
      this.info.data = error.message;
      let logColl = db.collection("taskLogs");
      let data = {
        type: "order.add",
        msg: "推送订单失败," + error.messages,
        order_Id: null,
        orderCode: body.storeOrderCode,
        createDate: new Date()
      };
      await logColl.insert(data);
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 取消订单
  @Post()
  public async cancel() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      // 记录数据到mongodb日志中
      let db = this.app.mongodb;
      let collection = db.collection("apiSaleOrderCancel");
      let mongoData = JSON.parse(JSON.stringify(body));
      mongoData.createDate = new Date();
      mongoData.userProduct_Id = userInfo.userProduct_Id;
      let re = await collection.insert(mongoData);
      ctx.logger.info("apiSaleOrderCancel将数据保存至MongoDB after:" + JSON.stringify(re));
    } catch { }
 
    try {
      //#region 校验数据
      let orderInfo = await this.dbRead.findOne(SaleOrder, {
        storeOrderCode: body.storeOrderCode,
        consignorCode: body.consignorCode,
        userProduct_Id: userInfo.userProduct_Id
      });
      if (!orderInfo) {
        this.info.result = false;
        this.info.msg = "出库单不存在";
        ctx.body = this.info;
        return;
      }
 
      if (["部分打包", "打包完成", "部分发运", "发运完成"].indexOf(orderInfo.statusText) >= 0) {
        this.info.result = false;
        this.info.msg = orderInfo.statusText + "不允许取消";
        ctx.body = this.info;
        return;
      }
      //#endregion
 
      //#region 取消操作
      // 删除占位
      await this.dbWrite.delete(BaseProductPlaceHolder, {
        className: "销售订单",
        mainID: orderInfo.order_Id,
        userProduct_Id: userInfo.userProduct_Id
      });
      // 更新订单状态为:用户取消
      await this.dbWrite.update(
        SaleOrder,
        {
          order_Id: orderInfo.order_Id
        },
        {
          statusID: 0,
          statusText: "用户取消"
        }
      );
      //#endregion
 
      this.info.result = true;
      this.info.msg = "取消成功";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 取消订单 - 自动回退库存
  @Post()
  public async autoReturnOrder() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let db = this.app.mongodb;
 
    try {
      // 记录数据到mongodb日志中
      let db = this.app.mongodb;
      let collection = db.collection("apiReturnOrder");
      let mongoData = JSON.parse(JSON.stringify(body));
      mongoData.createDate = new Date();
      mongoData.userProduct_Id = userInfo.userProduct_Id;
      let re = await collection.insert(mongoData);
      ctx.logger.info("apiReturnOrder将数据保存至MongoDB after:" + JSON.stringify(re));
    } catch { }
 
    try {
      let order_Id = body.order_Id;
      //#region 校验数据
      let orderInfo = await this.dbRead.findOne(SaleOrder, {
        order_Id: body.order_Id,
        userProduct_Id: userInfo.userProduct_Id
      });
      if (!orderInfo) {
        this.info.result = false;
        this.info.msg = "出库单不存在";
        ctx.body = this.info;
        return;
      }
 
      if (["发运完成"].indexOf(orderInfo.statusText) < 0) {
        this.info.result = false;
        this.info.msg = "当前状态为:" + orderInfo.statusText + ",只有发运完成的才允许做退货操作";
        ctx.body = this.info;
        return;
      }
      //#endregion
 
      const connection: any = await this.dbWrite.connection;
      let request = new mssql.Request(connection.driver.master);
      request.input("order_Id", order_Id);
      request.input("user_Id", userInfo.userProduct_Id);
      request.output("outMsg", mssql.NVarChar(2000));
      let result = await request.execute("sp_Sale_Return_AutoShelve");
      let outMsg = result.output.outMsg;
      if (outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        let logColl = db.collection("taskLogs");
        let data = {
          type: "autoReturnOrder",
          msg: "确认退货失败," + outMsg,
          order_Id: orderInfo.order_Id,
          orderCode: orderInfo.orderCode,
          createDate: new Date()
        };
        await logColl.insert(data);
      } else {
        let returnInfo = await this.dbRead.findOne(SaleReturn, {
          select: ["return_Id", "returnCode"],
          where: {
            order_Id: order_Id
          }
        });
        this.info.msg = "确认退货成功";
        this.info.result = true;
        this.info.data = returnInfo;
        let logColl = db.collection("taskLogs");
        let data = {
          type: "autoReturnOrder",
          msg: "确认退货成功",
          order_Id: orderInfo.order_Id,
          orderCode: orderInfo.orderCode,
          createDate: new Date()
        };
        await logColl.insert(data);
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region  合并订单
  @Post()
  public async addOrderMerge() {
    let { ctx } = this;
    let body = ctx.request.body;
    let mainOrderCode = body.mainOrderCode;
    let orderIds = body.selectIDs;
    let userInfo = await ctx.helper.userInfo();
    if (mainOrderCode || orderIds) {
      this.info.result = false;
      this.info.msg = "主订单和要合并的订单都不能为空!";
      ctx.body = this.info;
    }
    if (orderIds.length < 2) {
      this.info.result = false;
      this.info.msg = "两个以上订单才能合并!";
      ctx.body = this.info;
    }
    // let items = await this.dbRead.find()
    let dicOriginOrderStatusID = [];
    let ids: any = {};
    const items = await this.dbRead
      .createQueryBuilder()
      .from(SaleOrderMerge, "post")
      .where({
        order_Id: In(orderIds)
      })
      .getRawMany();
    if (items.length > 0) {
      let orderCodes = "";
      for (let info in items) {
        orderCodes += info["OrderCode"] + ",";
      }
      this.info.result = false;
      this.info.msg = "订单" + orderCodes + "已经合并过了!";
      ctx.body = this.info;
    }
    try {
      let mergedOrderCode = await ctx.service.common.getCodeRegular(112);
      mergedOrderCode = mergedOrderCode.replace(/SO/, "MO");
      const table = await this.dbRead
        .createQueryBuilder()
        .select("order_Id")
        .addSelect("statusID")
        .from(SaleOrder, "post")
        .where({
          order_Id: In(orderIds)
        })
        .getRawMany();
      for (let info of table) {
        ids = {
          oid: info["order_Id"],
          status_id: info["statusID"]
        };
        dicOriginOrderStatusID.push(ids);
      }
 
      // 合并sql
      const connection: any = await this.dbWrite.connection;
      let request = new mssql.Request(connection.driver.master);
      request.input("MergedOrderCode", mergedOrderCode);
      request.input("OrderIds", orderIds);
      request.input("MainOrderCode", mainOrderCode);
      request.input("User_Id", userInfo.user_Id);
      request.output("outMsg", mssql.NVarChar(2000));
      let result = await request.execute("sp_Sale_OrderMerge");
      let outMsg = result.output.outMsg;
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
      }
      //记录订单历史状态
      let sql = `SELECT TOP 1 order_Id FROM dbo.Sale_Order WHERE OrderCode =@0 ORDER BY Order_Id DESC`;
      let toOrderId = await this.dbRead.query(sql, [mergedOrderCode]);
      if (!toOrderId) {
        this.info.result = false;
        this.info.msg = "合并失败,未知情况";
        ctx.body = this.info;
      }
      let toOrder = await this.dbRead.findOne(SaleOrder, {
        order_Id: toOrderId[0].order_Id
      });
      let orderIdArray = orderIds;
      for (let fromOrderId of orderIdArray) {
        let from_statusid = dicOriginOrderStatusID["fromOrderId"];
        let fromOrder = await this.dbRead.findOne(SaleOrder, {
          order_Id: parseInt(fromOrderId)
        });
        ctx.service.outbound.orderHelper.setStatusHistory(fromOrder.order_Id, from_statusid, 33, "订单状态", "合并订单(到" + toOrder.orderCode + ")");
        ctx.service.outbound.orderHelper.setStatusHistory(toOrder.order_Id, 0, 1, "订单状态", "合并订单(原" + fromOrder.orderCode + ")");
      }
      this.info.result = true;
      this.info.msg = "合并成功";
      ctx.body = this.info;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "合并失败:" + error.message;
      ctx.body = this.info;
    }
  }
  //#endregion
 
  //#region 批量关闭订单
  @Post()
  public async closeOrder() {
    let { ctx } = this;
    let body = ctx.request.body;
    let ids = body.order_Ids;
    let className = "销售订单";
    let userInfo = await ctx.helper.userInfo();
    try {
      // let sql = ` Update Base_ProductPlaceHolder Set PlaceholderStorage=0 Where MainID in(select col from dbo.split(@0, ',')) And ClassName=@1;
      // Update Sale_OrderList Set LackStorage=null Where Order_Id in(select col from dbo.split(@0, ','));
      // Update Sale_Order Set StatusID=37, StatusText='已关闭', SortingStatus=1, SortingDate=null, Auditing=0,
      // ModifyID=@2, Modifier=@3, ModifyDate=getdate()
      // Where Order_Id in(select col from dbo.split(@0, ','));
      // Update Sale_OrderList set SortingStatus=1, LackStorage=QuantityOrder Where Order_Id in(select col from dbo.split(@0, ','));`;
 
      // await this.dbRead.query(sql, [ids, className, userInfo.user_Id, userInfo.userTrueName]);
 
      await this.dbWrite
        .createQueryBuilder()
        .update(BaseProductPlaceHolder)
        .set({
          placeholderStorage: 0
        })
        .where("MainID in(select col from dbo.split(:selectIDs, ',')) and ClassName=:className", {
          selectIDs: ids,
          className: className
        })
        .execute();
 
      await this.dbWrite
        .createQueryBuilder()
        .update(SaleOrderList)
        .set({
          lackStorage: null,
          sortingStatus: 1
        })
        .where("Order_Id in(select col from dbo.split(:selectIDs, ',')) ", {
          selectIDs: ids
        })
        .execute();
 
      await this.dbWrite.update(
        SaleOrder,
        {
          order_Id: In(ids)
        },
        {
          statusID: 37,
          statusText: "已关闭",
          sortingStatus: 1,
          // sortingDate: null,
          auditing: 0,
          modifyID: userInfo.user_Id,
          modifier: userInfo.userTrueName
        }
      );
 
      this.info.result = true;
      this.info.msg = "批量关闭成功";
      let orderList = await this.dbRead.find(SaleOrder, {
        order_Id: In(ids)
      });
      for (let orderInfo of orderList) {
        await ctx.service.outbound.orderHelper.setStatusHistory(orderInfo.order_Id, orderInfo.statusID, 37, "订单状态", "订单关闭");
      }
      ctx.body = this.info;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "关闭失败:" + error.message;
      ctx.body = this.info;
    }
  }
  //#endregion
 
  //#region 拆分订单
  @Post()
  public async splitOrder() {
    let { ctx } = this;
    let userInfo = await this.userInfo;
    let body = ctx.request.body;
    let order_Id = body.order_Id;
    let productModelList = body.productModelList;
    let sql = "";
    let orderInfo = await this.dbRead.findOne(SaleOrder, {
      order_Id: order_Id
    });
    if (orderInfo.statusText != "待审核") {
      this.info.result = false;
      this.info.msg = "只有待审核的出库单才允许拆分";
      ctx.body = this.info;
    }
    try {
      let saleOrderList = await this.dbRead.find(SaleOrderList, {
        order_Id: order_Id
      });
      let newOrderInfo = new SaleOrder();
      newOrderInfo = { ...orderInfo };
      if (productModelList.length > 0) {
        //#region 新订单处理
        newOrderInfo.order_Id = 0;
        newOrderInfo.orderCode = await ctx.service.common.getCodeRegular(112);
        //拆分订单的时候,如果店铺订单号是空的,则用销售单号作为-1、-2这样的前缀
        let storeOrderCode = orderInfo.storeOrderCode ? orderInfo.storeOrderCode : orderInfo.orderCode;
        let codes = storeOrderCode.split("-");
        let leftChar = "";
        if (codes.length >= 2) {
          let lastNum = codes[codes.length - 1];
          if (lastNum.length <= 2) {
            leftChar = storeOrderCode.replace("-" + lastNum, "");
          } else {
            leftChar = storeOrderCode;
          }
        } else {
          leftChar = storeOrderCode;
        }
        //查找最后的一个订单编号
        // let sqlStoreOrderCode = "SELECT TOP 1 StoreOrderCode FROM dbo.Sale_Order WHERE StoreOrderCode LIKE '" + leftChar +
        //   "%' ORDER BY StoreOrderCode DESC";
        let lastOrder = await this.dbRead.findOne(SaleOrder, {
          select: ["storeOrderCode"],
          where: {
            userProduct_Id: userInfo.userProduct_Id,
            storeOrderCode: Like(`%${leftChar}%`)
          },
          order: {
            storeOrderCode: "DESC"
          }
        });
 
        let lastOrderCode = "";
        if (lastOrder) {
          lastOrderCode = lastOrder.storeOrderCode;
        }
        //如果店铺订单号是空的则用销售单号
        if (!lastOrderCode) {
          lastOrderCode = orderInfo.orderCode;
        }
        codes = lastOrderCode.split("-");
        let newStoreOrderCode;
        if (codes.length >= 2) {
          let lastNum = codes[codes.length - 1];
          if (lastNum.length <= 2) {
            newStoreOrderCode = lastOrderCode.substring(0, lastOrderCode.lastIndexOf("-" + lastNum));
            let c = Number(lastNum) + 1;
            newStoreOrderCode += "-" + c;
          } else {
            newStoreOrderCode = lastOrderCode + "-1";
          }
        } else {
          newStoreOrderCode = lastOrderCode + "-1";
        }
 
        newOrderInfo.storeOrderCode = newStoreOrderCode;
        newOrderInfo.statusID = orderInfo.statusID;
        newOrderInfo.statusText = orderInfo.statusText;
        newOrderInfo.auditDate = orderInfo.auditDate;
        newOrderInfo.auditing = orderInfo.auditing;
        newOrderInfo.auditor = orderInfo.auditor;
        newOrderInfo.createDate = new Date();
 
        newOrderInfo.sortingDate = null;
        newOrderInfo.sortingStatus = 1;
        await this.dbWrite.insert(SaleOrder, newOrderInfo);
        let newOrder_Id = newOrderInfo.order_Id;
        for (let dyn of productModelList) {
          let orderList_Id = dyn.orderList_Id;
          let detail = saleOrderList.filter(s => s.orderList_Id == orderList_Id)[0];
          if (dyn.quantity >= detail.quantityOrder) {
            detail.order_Id = newOrderInfo.order_Id;
            await this.dbWrite.update(
              SaleOrderList,
              {
                orderList_Id: detail.orderList_Id
              },
              {
                order_Id: newOrderInfo.order_Id
              }
            );
          } else {
            // 如果拆分的数量小于明细数量,将原来明细数量减少,同时在新订单下面新增明细
            let newdetail = new SaleOrderList();
            newdetail = { ...detail };
            newdetail.orderList_Id = 0;
            newdetail.order_Id = newOrderInfo.order_Id;
            newdetail.quantityOrder = dyn.quantity;
            newdetail.rowTotal = dyn.quantity * newdetail.salePrice;
            newdetail.subTotal = newdetail.rowTotal - newdetail.discountAmount;
            await this.dbWrite.insert(SaleOrderList, newdetail);
 
            detail.quantityOrder -= dyn.quantity;
            detail.rowTotal = detail.quantityOrder * detail.salePrice;
            detail.subTotal = detail.rowTotal - detail.discountAmount;
            await this.dbWrite.save(detail);
          }
        }
        //#endregion
 
        //#region 新单状态的处理
        if (newOrder_Id > 0) {
          sql = `
          UPDATE Sale_Order SET
          TotalQuantityOrder = (SELECT SUM(QuantityOrder) FROM dbo.Sale_OrderList WHERE Order_Id = Sale_Order.Order_Id),
          SubTotal = (SELECT SUM(RowTotal) FROM dbo.Sale_OrderList WHERE Order_Id = Sale_Order.Order_Id)
          WHERE Order_Id IN(@0, @1);
 
          UPDATE Sale_Order SET
            BaseTaxAmount = TaxAmount * StoreBaseRate,
            BaseShippingAmount = ShippingAmount * StoreBaseRate,
            BaseDiscountAmount = DiscountAmount * StoreBaseRate,
            GrandTotal = SubTotal + ISNULL(ShippingAmount, 0),
            BaseGrandTotal = (SubTotal + ISNULL(ShippingAmount, 0)) * StoreBaseRate,
            BaseTotalPaid = TotalPaid * StoreBaseRate,
            BaseTotalRefunded = TotalRefunded * StoreBaseRate,
            BaseTotalCanceled = TotalCanceled * StoreBaseRate,
            BaseTotalInvoiced = TotalInvoiced * StoreBaseRate,
            BaseTotalOnlineRefunded = TotalOnlineRefunded * StoreBaseRate,
            BaseTotalOffRefunded = TotalOffRefunded * StoreBaseRate
          WHERE Order_Id IN(@0, @1);`;
          await this.dbRead.query(sql, [orderInfo.order_Id, newOrder_Id]);
 
          this.info.result = true;
          this.info.msg = "拆分成功,新单号为" + newOrderInfo.orderCode + "!";
          await ctx.service.outbound.orderHelper.setStatusHistory(orderInfo.order_Id, orderInfo.statusID, 0, "订单状态", "拆分订单");
        } else {
          this.info.result = false;
          this.info.msg = "拆分失败,请重试!";
        }
        this.ctx.body = this.info;
        //#endregion
      }
    } catch (error) {
      this.info.result = false;
      this.info.msg = "拆分失败," + error.message;
      ctx.body = this.info;
    }
  }
  //#endregion
 
  //#region 出库单明细导入Excel
  /// <summary>
  /// 导入Excel
  /// </summary>
  /// <returns></returns>
  @Post()
  public async detailimportExel() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
    let fileUrl = body.url;
    redis.expire(body.key, 5 * 60);
    if (!fileUrl) {
      redis.rpush(body.key, "上传文件不存在");
      return;
    }
    try {
      let rootPath = path.resolve(); // 获得根目录
      let filePath = rootPath + path.sep + fileUrl.replace(/\//gi, path.sep); // 上传文件路径
      var workbook = XLSX.readFile(filePath); //整个 excel 文档
      var sheetNames = workbook.SheetNames; //获取所有工作薄名
      var sheet1 = workbook.Sheets[sheetNames[0]]; //根据工作薄名获取工作薄
      let dataList = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
 
      //#region 验证数据正确性
      this.info.result = true;
      if (!dataList.length) {
        redis.rpush(body.key, "没有可导入的数据");
        return;
      }
      let msg = "";
      let i = 0;
      for (let row of dataList) {
        i++;
        let productCode = row["物料编号"];
        let productModel = row["条形码"];
        let quantity = row["预出库数量"];
        if (!productCode) {
          msg += `${i}、物料编号不能为空`;
          redis.rpush(body.key, msg);
          this.info.result = false;
        }
        if (!productModel) {
          msg += `${i}、条形码不能为空`;
          redis.rpush(body.key, msg);
          this.info.result = false;
        }
        if (!quantity) {
          msg += `${i}、预出库数量不能为空`;
          redis.rpush(body.key, msg);
          this.info.result = false;
        }
        // #endregion
 
        if (msg) {
          this.info.result = false;
          this.info.msg = msg;
        } else {
          let productinfo = await this.dbRead.findOne(BaseProductInfo, {
            productModel: productModel,
            userProduct_Id: userInfo.userProduct_Id
          });
          // 日期转换
          let produceDate = row["生产日期"];
          let d = moment(new Date(1900, 0, produceDate - 1));
          // 物料基础数据处理
          let productSpec = row["物料规格"] ? row["物料规格"] : productinfo.productSpec;
          let salePrice = row["销售单价"] ? row["销售单价"] : productinfo.salePrice;
          let weight = row["单位重量"] ? row["单位重量"] : productinfo.weight;
          let bigUnit = row["大单位"] ? row["大单位"] : productinfo.bigUnit;
          let rate = row["税率"] ? row["税率"] : productinfo.rate;
          let ratePrice = row["含税价"] ? row["含税价"] : productinfo.ratePrice;
          let _detail = new SaleOrderList();
          _detail.order_Id = body.order_Id;
          _detail.createID = userInfo.user_Id;
          _detail.creator = userInfo.userTrueName;
          _detail.product_Id = productinfo.product_Id;
          _detail.productCode = row["物料编号"];
          _detail.productName = productinfo.productName;
          _detail.productModel = row["条形码"];
          _detail.productSpec = productSpec;
          _detail.quantityOrder = row["预出库数量"];
          _detail.salePrice = salePrice;
          _detail.discountRate = row["货品折扣"] ? row["货品折扣"] : 0;
          _detail.discountAmount = row["优惠金额"];
          if (produceDate != null) {
            _detail.produceDate = d.toDate();
          }
          _detail.singleSignCode = row["定制唯一码"];
          _detail.weight = weight;
          _detail.smallUnit = row["小单位"];
          _detail.bigUnit = bigUnit;
          _detail.rate = rate;
          _detail.ratePrice = ratePrice;
          //折扣单价
          _detail.rateMoney = ratePrice * _detail.quantityOrder;
          _detail.salePriceDiscount = _detail.salePrice * _detail.discountRate;
          _detail.rowTotal = _detail.quantityOrder * _detail.salePrice;
          _detail.subTotal = _detail.quantityOrder * _detail.salePriceDiscount;
          _detail.totalWeight = _detail.quantityOrder * _detail.weight;
          _detail.rowTotal = _detail.rowTotal * rate;
          await this.dbWrite.insert(SaleOrderList, _detail);
 
          // 更新主表合计
          let sql = `
        update Sale_Order set
          grandTotal=(select sum(rowTotal) from Sale_OrderList where order_Id=Sale_Order.order_Id),
          totalQuantityOrder=(select sum(quantityOrder) from Sale_OrderList where order_Id=Sale_Order.order_Id),
          weight=(select sum(totalWeight) from Sale_OrderList where order_Id=Sale_Order.order_Id),
          totalWeight=(select sum(totalWeight) from Sale_OrderList where order_Id=Sale_Order.order_Id),
          subTotal=(select sum(subTotal) from Sale_OrderList where order_Id=Sale_Order.order_Id)
          where order_Id=@0;
        `;
          await this.dbWrite.query(sql, [body.order_Id]);
        }
      }
      this.info.result = true;
      this.info.msg = "导入成功";
    } catch (ex) {
      this.info.msg = "出现异常:" + ex.message;
      this.info.result = false;
    }
 
    ctx.body = this.info;
  }
 
  // #endregion
 
  //#region 一键出库
  @Post()
  public async quickOut() {
    let userInfo = await this.userInfo;
    let body = this.ctx.request.body;
    if (!isNumber(body.order_Id)) {
      this.info.msg = "单据不存在";
      this.info.result = false;
      this.ctx.body = this.info;
      return;
    }
 
    try {
      const connection: any = await this.dbWrite.connection;
      let request = new mssql.Request(connection.driver.master);
      request.input("order_Id", body.order_Id);
      request.input("user_Id", userInfo.user_Id);
 
      request.output("outMsg", mssql.NVarChar(2000));
      let result = await request.execute("sp_Sale_Order_AutoOut");
      let outMsg = result.output.outMsg;
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
      } else {
        this.info.msg = "确认出库成功";
        this.info.result = true;
      }
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region modifyfeeItems 修改一次性收费项
  @Post()
  public async modifyfeeItems() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    //#region 修改一次性收费项
    await this.dbWrite.update(
      SaleOrder,
      {
        order_Id: body.order_Id,
        userProduct_Id: userInfo.userProduct_Id
      },
      {
        feeItem_Ids: body.feeItem_Ids
      }
    );
    this.info.result = true;
    this.info.msg = "一次性收费项更新成功!";
 
    ctx.body = this.info;
    //#endregion
  }
  //#endregion
 
  //#region  发货信息导入
  @Post()
  public async outimportExcel() {
    setTimeout(async () => {
      await this.importoutExcelWork();
    }, 0);
 
    this.info.result = true;
    this.ctx.body = this.info;
  }
 
  private async importoutExcelWork() {
    let { ctx } = this;
    let body = ctx.request.body;
    let startDate = new Date();
    let fileUrl = body.fileUrl;
    let userInfo = await ctx.helper.userInfo();
    // let storage = null;
    // let consignorList = null;
    // let productlist = null;
    if (!fileUrl) {
      this.setMsg(`上传文件不存在`, "red");
      return;
    }
    try {
      let rootPath = path.resolve(); // 获得根目录
      let filePath = rootPath + path.sep + fileUrl.replace(/\//gi, path.sep); // 上传文件路径
      var workbook = XLSX.readFile(filePath); //整个 excel 文档
      var sheetNames = workbook.SheetNames; //获取所有工作薄名
      var sheet1 = workbook.Sheets[sheetNames[0]]; //根据工作薄名获取工作薄
      let dataList = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
      this.info.result = true;
      if (!dataList.length) {
        this.setMsg(`没有可导入的数据`, "red");
        return;
      }
      let orders = await this.dbRead.find(SaleOrder, {
        where: [
          {
            orderCode: In(dataList.map(item => item["出库订单号"]))
          },
          {
            storeOrderCode: In(dataList.map(item => item["店铺订单号"]))
          }
        ]
      });
      // 验证
      let index = 0;
      for (let item of dataList) {
        let _index = index + 1;
        if (!item["出库订单号"]) {
          if (!item["店铺订单号"]) {
            this.setMsg(`第${_index}行、标黄[出库订单号、店铺订单号]不能都为空!`, "red");
          }
        }
        let istrue = orders.find(row => {
          return row.orderCode === item["出库订单号"] || row.storeOrderCode === item["店铺订单号"];
        });
        if (!istrue) {
          this.setMsg(`第${_index}行、出库订单中没有该出库单号或者该店铺订单号,请修改!`, "red");
        }
        if (item["订单状态"] === "已妥投") {
          if (istrue.statusText != "打包完成" && istrue.statusText != "发运完成") {
            this.setMsg(`第${_index}行、只有订单状态为打包完成或者发运完成的才可妥投!`, "red");
          }
        }
        index++;
      }
      if (this.isMsgError) {
        this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
        this.setMsg("-1");
        return;
      }
      for (let row of dataList) {
        let orderCode = row["出库订单号"];
        let storeOrderCode = row["店铺订单号"];
        let history = new SaleOrderStatusHistory();
        let orderinfo = orders.find(item => item.orderCode === orderCode || item.storeOrderCode === storeOrderCode);
        if (row["快递单号"]) {
          orderinfo.expressCode = row["快递单号"];
        }
        if (row["订单状态"] == "已妥投") {
          orderinfo.statusText = row["订单状态"];
        }
        if (row["妥投天数"]) {
          orderinfo.tuotouDays = row["妥投天数"];
        }
        if (row["订单重量"]) {
          orderinfo.totalWeight = row["订单重量"];
        }
        history.order_Id = orderinfo.order_Id;
        history.statusType = "订单状态";
        history.operationType = "信息导入";
        history.fromStatus = orderinfo.statusText;
        history.fromStatusID = orderinfo.statusID;
        history.toStatus = "已妥投";
        history.billCode = orderinfo.orderCode;
        history.creator = userInfo.userTrueName;
        // history.createDate = new Date();
        history.createDate = moment().add(-row["妥投天数"], "day").toDate();
 
        await this.dbWrite.save(orderinfo);
        await this.dbWrite.save(history);
      }
      let endDate = moment(Date.now());
      let totalSeconds = endDate.diff(startDate, "seconds");
      let msg = `导入完成,共耗时${totalSeconds}秒`;
      this.setMsg(msg);
    } catch (ex) {
      this.setMsg("出现异常:" + ex.message, "red");
    }
    this.setMsg("-1");
  }
 
  //#endregion
 
  //#region 根据货主得到门店
  @Post()
  public async getShortName() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let dataList = await this.dbRead.find(BaseClient, {
      consignorCode: body.consignorCode,
      userProduct_Id: userInfo.userProduct_Id
    });
    this.info.result = true;
    this.info.data = dataList;
    this.ctx.body = this.info;
  }
 
  //#endregion
 
  //#region 获取明细页面数据
  @Post()
  public async getdetailList() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    let sql = `SELECT b.productCode ,V.validQuantity,T.destinationName,T.orderType,T.OrderExit FROM Sale_Order AS T LEFT JOIN dbo.Sale_OrderList AS B ON T.order_Id = B.Order_Id LEFT JOIN vBase_ProductPositionGroup AS V ON
    V.ProductCode = B.ProductCode
    WHERE OrderCode = @0 and UserProduct_Id=@1`; //orderCode
    let orderlinfo = await this.dbRead.query(sql, [body.orderCode, userInfo.userProduct_Id]);
 
    let dataList = await this.dbRead.find(BaseClient, {
      consignorCode: body.consignorCode,
      userProduct_Id: userInfo.userProduct_Id
    });
    this.info.result = true;
    this.info.data = dataList;
    this.info.data2 = orderlinfo;
    this.ctx.body = this.info;
  }
 
  //#endregion
 
  //#region 青岛一汽一键出库
  @Post()
  public async QingqiQuickOut() {
    let body = this.ctx.request.body;
    if (!body.id) {
      this.info.msg = "单据不存在";
      this.info.result = false;
      this.ctx.body = this.info;
      return;
    }
 
    try {
      let saleOrderInfo = await this.dbRead.findOne(SaleOrder, {
        order_Id: body.id
      });
      let baseProductPlaceHolderList = await this.dbRead.find(BaseProductPlaceHolder, {
        className: "销售订单",
        code: saleOrderInfo.orderCode
      });
      // 目的地是分拣区的 将订单分拣库存货位变成分拣区货位并更新单据为下架完成
      if (saleOrderInfo.destinationName === "分拣区") {
        for (var holderInfo of baseProductPlaceHolderList) {
          var productPositionInfo = await this.dbRead.findOne(BaseProductPosition, {
            productPosition_Id: holderInfo.productPosition_Id
          });
          productPositionInfo.positionName = "分拣区";
          await this.dbWrite.save(productPositionInfo);
        }
        saleOrderInfo.statusText = "下架完成";
        await this.dbWrite.save(saleOrderInfo);
      } else {
        // 目的地是其他的,出库消减库存清除占位,并更新单据为出库完成
        for (var holderInfo of baseProductPlaceHolderList) {
          var productPositionInfo = await this.dbRead.findOne(BaseProductPosition, {
            productPosition_Id: holderInfo.productPosition_Id
          });
          // 消减库存
          productPositionInfo.productStorage = productPositionInfo.productStorage - holderInfo.placeholderStorage;
          await this.dbWrite.save(productPositionInfo);
          // 清除占位
          holderInfo.placeholderStorage = 0;
          await this.dbWrite.save(holderInfo);
        }
        saleOrderInfo.statusText = "出库完成";
        await this.dbWrite.save(saleOrderInfo);
        await this.dbWrite.update(
          BasePlate,
          {
            plateCode: saleOrderInfo.plateCode
          },
          {
            currentArea: saleOrderInfo.storageName,
            currentLocation: saleOrderInfo.destinationName,
            plateState: "已出库"
          }
        );
      }
      this.info.result = true;
      this.info.msg = "一键出库完成。";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 青岛一汽拣选更新获取明细
  @Post()
  public async getOrderDetails() {
    let body = this.ctx.request.body;
    if (!body.order_Id) {
      this.info.msg = "单据不存在";
      this.info.result = false;
      this.ctx.body = this.info;
      return;
    }
 
    try {
      let saleOrderList = await this.dbRead.find(SaleOrderList, {
        order_Id: body.order_Id
      });
      for (var saleOrderInfo of saleOrderList) {
        // 占位
        let baseProductPlaceHolderInfo = await this.dbRead.findOne(BaseProductPlaceHolder, {
          className: "销售订单",
          mainID: saleOrderInfo.order_Id,
          detailID: saleOrderInfo.orderList_Id
        });
        saleOrderInfo.validQuantity = baseProductPlaceHolderInfo.placeholderStorage;
      }
      this.info.result = true;
      this.info.data = saleOrderList;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 青岛一汽拣选更新提交
  @Post()
  public async saveCheck() {
    let body = this.ctx.request.body;
    if (!body.jsonDetails) {
      this.info.msg = "单据不存在";
      this.info.result = false;
      this.ctx.body = this.info;
      return;
    }
    try {
      var flag = true;
      for (var val of body.jsonDetails) {
        let saleOrderListInfo = await this.dbRead.findOne(SaleOrderList, {
          orderList_Id: val.orderList_Id
        });
        // 占位
        let holderInfo = await this.dbRead.findOne(BaseProductPlaceHolder, {
          className: "销售订单",
          mainID: saleOrderListInfo.order_Id,
          detailID: saleOrderListInfo.orderList_Id
        });
        // 库存
        var productPositionInfo = await this.dbRead.findOne(BaseProductPosition, {
          productPosition_Id: holderInfo.productPosition_Id
        });
        // 消减库存
        productPositionInfo.productStorage = productPositionInfo.productStorage - parseInt(val.pickUpNum);
        await this.dbWrite.save(productPositionInfo);
        // 清除占位
        holderInfo.placeholderStorage = holderInfo.placeholderStorage - parseInt(val.pickUpNum);
        await this.dbWrite.save(holderInfo);
 
        saleOrderListInfo.validQuantity = productPositionInfo.productStorage;
        saleOrderListInfo.quantityOuted = productPositionInfo.orignStorage - productPositionInfo.productStorage;
        if (saleOrderListInfo.validQuantity !== 0) {
          flag = false;
        }
        await this.dbWrite.save(saleOrderListInfo);
      }
 
      let saleOrderInfo = await this.dbRead.findOne(SaleOrder, {
        order_Id: body.jsonDetails[0].order_Id
      });
      if (flag) {
        saleOrderInfo.statusText = "完全出库";
        await this.dbWrite.save(saleOrderInfo);
      } else {
        saleOrderInfo.statusText = "部分出库";
        await this.dbWrite.save(saleOrderInfo);
      }
 
      this.info.result = true;
      this.info.msg = "拣选更新完成";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 青岛一汽预料回库获取明细
  @Post()
  public async getBackOrderDetails() {
    let body = this.ctx.request.body;
    if (!body.order_Id) {
      this.info.msg = "单据不存在";
      this.info.result = false;
      this.ctx.body = this.info;
      return;
    }
 
    try {
      let saleOrderList = await this.dbRead.find(SaleOrderList, {
        order_Id: body.order_Id
      });
      for (var saleOrderInfo of saleOrderList) {
        // 占位
        let baseProductPlaceHolderInfo = await this.dbRead.findOne(BaseProductPlaceHolder, {
          className: "销售订单",
          mainID: saleOrderInfo.order_Id,
          detailID: saleOrderInfo.orderList_Id
        });
        saleOrderInfo.validQuantity = baseProductPlaceHolderInfo.placeholderStorage;
      }
      this.info.result = true;
      this.info.data = saleOrderList;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    this.ctx.body = this.info;
  }
  //#endregion
  //#region 添加新增记录
  @Post()
  public async orderLog() {
    try {
      let userInfo = await this.userInfo;
      if (this.body.statusText === "新建" || this.body.statusText === "待下架") {
        if (this.body.orderType === "出库任务") {
          // 添加日志
          let userLog = new SysUserLog();
          userLog.operateType = "出库任务";
          userLog.action = "新建单号:-" + this.body.orderCode;
          userLog.iP = this.ctx.request.ip;
          userLog.userTrueName = userInfo.userTrueName;
 
          await this.setAccountInfo(userLog);
          await this.dbWrite.save(userLog);
        }
        if (this.body.orderType === "手动出库") {
          // 添加日志
          let userLog = new SysUserLog();
          userLog.operateType = "手动出库";
          userLog.action = "新建单号:-" + this.body.orderCode;
          userLog.iP = this.ctx.request.ip;
          userLog.userTrueName = userInfo.userTrueName;
 
          await this.setAccountInfo(userLog);
          await this.dbWrite.save(userLog);
        }
 
        this.info.result = true;
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误消息:" + ex.message;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 删除
  @Post()
  public async deleteOrderCode() {
    try {
      let userInfo = await this.userInfo;
      // 根据id删除
      await this.dbWrite.delete(SaleOrder, {
        order_Id: In(this.body.ids)
      });
      if (this.body.orderType === "出库任务") {
        // 添加日志
        let userLog = new SysUserLog();
        userLog.operateType = "出库任务";
        userLog.action = "删除单号:-" + this.body.orderCode;
        userLog.iP = this.ctx.request.ip;
        userLog.userTrueName = userInfo.userTrueName;
 
        await this.setAccountInfo(userLog);
        await this.dbWrite.save(userLog);
      }
      if (this.body.orderType === "手动出库") {
        // 添加日志
        let userLog = new SysUserLog();
        userLog.operateType = "手动出库";
        userLog.action = "删除单号:-" + this.body.orderCode;
        userLog.iP = this.ctx.request.ip;
        userLog.userTrueName = userInfo.userTrueName;
 
        await this.setAccountInfo(userLog);
        await this.dbWrite.save(userLog);
      }
 
      this.info.result = true;
      this.info.msg = "删除成功!";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误消息:" + ex.message;
    }
    this.ctx.body = this.info;
  }
  //#endregion
}