admin
8 天以前 6cd92a169cbc0db35042f243a09d976fd3e1445c
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
// FloatTradeDlg.cpp: 实现文件
//
 
#include "common/pch.h"
#include "FloatTrade.h"
#include "FloatTradeDlg.h"
#include "afxdialogex.h"
#include "../common/JsonUtil.h"
#include <iostream>
#include "base64.h"
#include "../common/StringUtil.h"
#include "ConfigUtil.h"
#include "../common/NetworkApi.h"
#include "../common/LowSuctionNetworkApi.h"
#include "../common/TimeUtil.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include <string>
#include <locale>
#include <codecvt>
#include <thread>
#include "../common/Win32Util.h"
#include "../common/TimeUtil.h"
#include "ThsUtil.h"
#include "CSettingDlg.h"
#include "CBuyDlg.h"
#include "SellRuleDlg.h"
#include "MyApi.h"
#include "CCBBuyDlg.h"
#include "Constant.h"
#include "PositionDlg.h"
#include "DealQueueDlg.h"
#include <regex>
 
struct ViewDetailData {
    string code;
};
 
 
// CFloatTradeDlg 对话框
 
//定义快捷键
const int HOT_KEY_SELL_ALL = 1;
const int HOT_KEY_CANCEL = 2;
const int HOT_KEY_BUY = 3;
const int HOT_KEY_UPDATE_BSD = 4;// 更新板上盯
const int HOT_KEY_ADD_WANT_BUY = 6;// 加想买
 
// 可转债消息
#define WM_CB_WINDOW (WM_APP + 1000)
 
 
bool CFloatTradeDlg::breakCancelMsg;
HHOOK CFloatTradeDlg::hMouseHook;
long  CFloatTradeDlg::ocrCodeExpireTimestamp;
 
 
 
string CFloatTradeDlg::getCode()
{
    CString csX;
    editCode.GetWindowText(csX);
    string code = StringUtil::cstring2String(csX);
    if (code.length() == 0) {
        throw wstring(L"尚未获取到目标代码");
    }
    else if (code.length() != 6) {
        throw wstring(L"目标代码长度不为6位");
    }
    return code;
}
 
void CFloatTradeDlg::setMsg(CString msg, MSG_TYPE msgType, bool autoCancel)
{
    labelAtrribute.SetWindowTextW(msg);
    if (msgType == MSG_TYPE_INFO) {
        msgColor = COLOR_BLACK;
    }
    else if (msgType == MSG_TYPE_SUCCESS) {
        msgColor = COLOR_RED;
    }
    else if (msgType == MSG_TYPE_FAIL) {
        msgColor = COLOR_GREEN;
    }
    //重新绘制
    labelAtrribute.Invalidate();
 
    breakCancelMsg = TRUE;
    if (autoCancel) {
        if (cancelMsgThread != nullptr) {
            //
            delete cancelMsgThread;
        }
        cancelMsgThread = new std::thread(clearMsg, this);
        cancelMsgThread->detach();
    }
}
 
 
void CFloatTradeDlg::showTips(CString message, int msgType, int delayMs, int position)
{
    TipDlg* p_MsgWindow = new TipDlg(delayMs, position, this);
    p_MsgWindow->Create(IDD_DIALOG_TIPS, this);
    if (msgType == MSG_TYPE_INFO) {
        msgColor = COLOR_BLACK;
    }
    else if (msgType == MSG_TYPE_SELL_SUCCESS) {
        msgColor = COLOR_BLUE;
    }
    else if (msgType == MSG_TYPE_SUCCESS) {
        msgColor = COLOR_RED;
    }
    else if (msgType == MSG_TYPE_FAIL) {
        msgColor = COLOR_GREEN;
    }
    else if (msgType == MSG_TYPE_WARNING) {
        msgColor = COLOR_ORANGE;
    }
    p_MsgWindow->setMsg(message, msgColor);
    p_MsgWindow->ShowWindow(SW_SHOW);
}
 
void CFloatTradeDlg::getTradeState(CFloatTradeDlg* context)
{
    try {
        bool can = NetworkApi::get_buy_state();
        if (can) {
            context->btnOpenBuy.SetWindowTextW(L"开启交易※");
            context->btnCloseBuy.SetWindowTextW(L"关闭交易");
        }
        else {
            context->btnOpenBuy.SetWindowTextW(L"开启交易");
            context->btnCloseBuy.SetWindowTextW(L"关闭交易※");
        }
    }
    catch (...) {
        context->setMsg(L"获取交易状态失败", MSG_TYPE_FAIL);
    }
}
 
void CFloatTradeDlg::getAutoCancelSellMode(CFloatTradeDlg* context, string code)
{
    try {
        int mode = NetworkApi::get_auto_cancel_sell_mode(code);
        if (mode == 0) {
            context->checkAutoCancelSell.SetCheck(TRUE);
        }
        else {
            context->checkAutoCancelSell.SetCheck(FALSE);
        }
    }
    catch (...) {
        context->setMsg(L"获取交易状态失败", MSG_TYPE_FAIL);
    }
}
 
void CFloatTradeDlg::getContinueBuyInfo(CFloatTradeDlg* context, string code)
{
    try {
        string msg = NetworkApi::get_continue_buy_info(code);
        auto doc = JsonUtil::parseUTF8(msg);
        if (doc.IsObject()) {
            if (doc["code"].GetInt() == 0) {
                int money = doc["data"]["money"].GetInt();
                ((CButton*)context->GetDlgItem(IDC_CHECK_CONTINUE_BUY))->SetCheck(money>0);
                CComboBox *comboMoneyList = (CComboBox*)context->GetDlgItem(IDC_COMBO_CONTINUE_BUY_MONEY);
                int count = comboMoneyList->GetCount();
                if (count == 0) {
                    auto money_list = doc["data"]["money_list"].GetArray();
                    for (int i = 0; i < money_list.Size(); i++) {
                        comboMoneyList->AddString(to_wstring(money_list[i].GetInt()).c_str());
                    }
                }
                // 查看是否有和money相等的选项
                for (int i = 0; i < comboMoneyList->GetCount(); i++) {
                    CString strItem;
                    comboMoneyList->GetLBText(i, strItem);
                    if (_ttoi(strItem) == money) {
                        comboMoneyList -> SetCurSel(i);
                        break;
                    }
                }
            }
        }
    }
    catch (...) {
        
    }
}
 
void CFloatTradeDlg::setContinueBuyMoney(CFloatTradeDlg* context, string code, int money)
{
 
}
 
 
void CFloatTradeDlg::getTradeMode(CFloatTradeDlg* context)
{
    /*try {
        int mode = NetworkApi::get_buy_mode();
        if (mode == 0) {
            context->btnBuyModeAll.SetWindowTextW(L"全部都买*");
            context->btnBuyModeWant.SetWindowTextW(L"仅买想买");
        }
        else {
            context->btnBuyModeAll.SetWindowTextW(L"全部都买");
            context->btnBuyModeWant.SetWindowTextW(L"仅买想买*");
        }
    }
    catch (...) {
        context->setMsg(L"获取交易模式失败", MSG_TYPE_FAIL);
    }*/
 
 
}
 
void CFloatTradeDlg::clearMsg(CFloatTradeDlg* context)
{
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
    breakCancelMsg = FALSE;
    //10s之后小时
    for (int i = 0; i < 1000; i++) {
        if (breakCancelMsg) {
            return;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
 
    context->labelAtrribute.SetWindowTextW(L"");
}
 
void CFloatTradeDlg::getCodeDesc(string code, CFloatTradeDlg* context)
{
 
    //请求网络获取消息
    try {
        wstring result = NetworkApi::getCodeDesc(code);
        CString resultC(result.c_str());
        //context->setMsg(resultC, MSG_TYPE_INFO, FALSE);
        context->labelCodeInfo.SetWindowTextW(resultC);
    }
    catch (wstring st) {
        /*CString msgc;
        msgc.Append(CString(code.c_str()));
        msgc.Append(L":");
        msgc.Append(st.c_str());
        context->setMsg(msgc, MSG_TYPE_FAIL);*/
    }
    catch (CString st) {
        /*    CString msgc;
            msgc.Append(CString(code.c_str()));
            msgc.Append(L":");
            msgc.Append(st);
            context->setMsg(msgc, MSG_TYPE_FAIL);*/
    }
 
}
 
void CFloatTradeDlg::requestCodePosition(string code, CFloatTradeDlg* context, bool showCodeDesc, int delayMs)
{
    if (delayMs > 0) {
        std::this_thread::sleep_for(std::chrono::milliseconds(delayMs));
    }
    context->sellManager->init(code);
    try {
        CodePosition newCodePosition;
        // 如果是持仓代码就不更新代码信息
        if (context->positionMap.find(code) != context->positionMap.end()) {
            newCodePosition = context->positionMap[code];
            CString resultC;
            resultC.Append(CString(newCodePosition.code.c_str()));
            resultC.Append(L" ");
            resultC.Append(newCodePosition.name);
            context->setMsg(resultC, MSG_TYPE_INFO, FALSE);
            std::thread t1(updateCodePosition, code, context);
            t1.detach();
        }
        else {
            string result = NetworkApi::get_code_position_info(code);
            auto doc = JsonUtil::parseUTF16(result);
            if (doc.IsObject()) {
 
                if (doc[_T("data")].HasMember(L"code_info"))
                {
                    auto data = doc[_T("data")].GetObjectW();
                    CString c = data[_T("code_info")].GetArray()[0].GetString();
                    wstring name = L"";
                    // 获取输入框的内容
                    if (context != nullptr) {
                        CString realCode;
                        context->editCode.GetWindowTextW(realCode);
                        if (c != realCode) {
                            return;
                        }
                    }
                }
                newCodePosition.total = doc[L"data"][L"total"].GetInt();
                newCodePosition.available = doc[L"data"][L"available"].GetInt();
                newCodePosition.costPrice = doc[L"data"][L"cost_price"].GetFloat();
                newCodePosition.sell_rules_count = doc[L"data"][L"sell_rules_count"].GetInt();
                list<int> sellVolumes;
                if (doc[L"data"].HasMember(L"sell_orders")) {
                    auto sell_orders_array = doc[L"data"][L"sell_orders"].GetArray();
 
                    for (int i = 0; i < sell_orders_array.Size(); i++) {
                        sellVolumes.push_back(sell_orders_array[i].GetInt());
                    }
                }
                newCodePosition.sell_orders = sellVolumes;
 
                if (showCodeDesc && doc[_T("data")].HasMember(L"code_info"))
                {
                    auto data = doc[_T("data")].GetObjectW();
                    CString c = data[_T("code_info")].GetArray()[0].GetString();
                    wstring name = L"";
                    if (!data[_T("code_info")].GetArray()[1].IsNull()) {
                        name = data[_T("code_info")].GetArray()[1].GetString();
                    }
                    if (data.HasMember(L"desc")) {
                        wstring desc = data[_T("desc")].GetString();
                        wstring fresult;
                        fresult.append(c).append(L" ").append(name).append(L"  ").append(desc);
                        CString resultC(fresult.c_str());
                        context->setMsg(resultC, MSG_TYPE_INFO, FALSE);
                    }
                }
 
            }
        }
 
 
        CodePosition* codePosition = context->sellManager->getCodePosition(code);
        /*codePosition->total = newCodePosition.total;
        codePosition->available = newCodePosition.available;
        codePosition->sell_rules_count = newCodePosition.sell_rules_count;
        codePosition->costPrice = newCodePosition.costPrice;*/
 
 
 
        // TODO 测试
        //codePosition->sell_orders.clear();
        //codePosition->sell_orders.push_back(900);
        //codePosition->available = 900;
 
 
        // 设置卖框显示的内容
        if (context->sellDlgMap.find(code) != context->sellDlgMap.end()) {
            context->sellDlgMap[code]->SetPositionInfo(newCodePosition.total, newCodePosition.available);
        }
 
        //设置内容
        context->showCodePositionInfo();
        context->initSellSettingView();
    }
    catch (wstring st) {
        context->setMsg(CString(st.c_str()), MSG_TYPE_FAIL);
    }
    // 设置数据请求完毕
    if (context->requestingCode == code) {
        context->requestingCode = "";
    }
 
    string nowTime = TimeUtil::getNowTime("%H%M%S");
    // 09:26:00之前都是跌停价卖
    if (stoi(nowTime) < stoi("092500")) {
        context->comboSellPriceType.SetCurSel(1);
    }
    else {
        context->comboSellPriceType.SetCurSel(0);
    }
 
    SellSetting* sellSetting = context->sellManager->getSellSetting(code);
 
    if (sellSetting->lockBuyMoney > 0) {
        context->editBuyMoney.SetWindowTextW(to_wstring(sellSetting->lockBuyMoney).c_str());
    }
    else {
        context->editBuyMoney.SetWindowTextW(to_wstring(ConfigUtil::getBuyMoney()).c_str());
    }
 
    if (sellSetting->lock) {
        context->checkLockVolume.SetCheck(TRUE);
    }
    else {
        context->checkLockVolume.SetCheck(FALSE);
    }
 
    int money = context->sellManager->computeCurrentMoney(code);
    context->editSellMoney.SetWindowTextW(to_wstring(money).c_str());
 
}
 
void CFloatTradeDlg::updateCodePosition(string code, CFloatTradeDlg* context)
{
    try {
        string result = NetworkApi::get_code_position_info(code);
        auto doc = JsonUtil::parseUTF16(result);
        if (doc.IsObject()) {
            if (context->positionMap.find(code) != context->positionMap.end()) {
                CodePosition codePosition = context->positionMap[code];
                codePosition.total = doc[L"data"][L"total"].GetInt();
                codePosition.available = doc[L"data"][L"available"].GetInt();
                codePosition.costPrice = doc[L"data"][L"cost_price"].GetFloat();
            }
            CodePosition* codePosition = context->sellManager->getCodePosition(code);
            if (codePosition != nullptr) {
                codePosition->total = doc[L"data"][L"total"].GetInt();
                codePosition->available = doc[L"data"][L"available"].GetInt();
                codePosition->costPrice = doc[L"data"][L"cost_price"].GetFloat();
            }
        }
    }
    catch (...) {
        cout << "出错" << endl;
    }
}
 
void CFloatTradeDlg::requestSellResult(int order_ref, CFloatTradeDlg* context, int delayMs)
{
    if (delayMs > 0) {
        std::this_thread::sleep_for(std::chrono::milliseconds(delayMs));
    }
    try {
        // 测试
        string result = NetworkApi::get_sell_result(order_ref);
        //string result =  string(StringUtil::cstring2String(L"{\"code\":0,\"data\":{\"msg\":\"成交\"}}"));
 
        auto doc = JsonUtil::parseUTF16(result);
        if (doc.IsObject()) {
            if (doc[L"code"] == 0) {
                auto data = doc[L"data"].GetObjectW();
                CString msg = data[L"msg"].GetString();
                MSG_TYPE msgType = MSG_TYPE_INFO;
                if (msg.Find(L"成交") >= 0) {
                    msgType = MSG_TYPE_SELL_SUCCESS;
                }
                else if (msg.Find(L"撤单") >= 0) {
                    msgType = MSG_TYPE_FAIL;
                }
 
                showFloatMsg(context, msg, msgType);
 
 
                CString code;
                context->editCode.GetWindowText(code);
                if (code && code.GetLength() == 6) {
                    requestCodePosition(StringUtil::cstring2String(code), context, FALSE);
                }
            }
            else {
                showFloatMsg(context, doc[L"msg"].GetString(), MSG_TYPE_FAIL);
            }
        }
    }
    catch (wstring st) {
        showFloatMsg(context, st.c_str(), MSG_TYPE_FAIL);
    }
    // 更新持仓
    requestAllPositions(context);
    if (context->positionDialog != nullptr) {
        context->positionDialog->updatePositions();
    }
}
 
void CFloatTradeDlg::regularUpdatePositionInfo(CFloatTradeDlg* context)
{
 
    while (TRUE) {
 
        try {
            string nowTime = TimeUtil::getNowTime("%H%M%S");
            // 09:26:00之前都是跌停价卖
            if (stoi(nowTime) >= stoi("093000") && stoi(nowTime) <= stoi("093003")) {
                //if (stoi(nowTime) >= stoi("154000") && stoi(nowTime) <= stoi("155000")) {
                CString codew;
                context->editCode.GetWindowTextW(codew);
                if (codew.GetLength() == 6) {
                    string code = StringUtil::cstring2String(codew);
                    // 更新持仓 
                    requestCodePosition(code, context, FALSE);
                }
            }
        }
        catch (...) {
 
        }
 
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
}
 
void CFloatTradeDlg::runPushMsgHeart(CFloatTradeDlg* context, SOCKET socket)
{
 
    while (TRUE) {
        rapidjson::StringBuffer buf;
        rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
        writer.StartObject();
        writer.Key("type");
        writer.String("heart");
        writer.Key("data");
        writer.StartObject();
        writer.EndObject();
        writer.EndObject();
        string json_content = buf.GetString();
        string data = NetworkApi::load_request_data(json_content);
        try {
            int result = send(socket, data.c_str(), strlen(data.c_str()), 0);
            if (result < 0) {
                throw string("发送失败");
            }
        }
        catch (...) {
            break;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(5 * 1000));
    }
 
}
 
void CFloatTradeDlg::runPushMsgReceiver(CFloatTradeDlg* context)
{
    while (TRUE) {
        try {
            SOCKET socket = SocketManager::createSocket();
 
            rapidjson::StringBuffer buf;
            rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
            writer.StartObject();
            writer.Key("type");
            writer.String("register_msg_receiver");
            writer.Key("data");
            writer.StartObject();
            writer.Key("types");
            writer.StartArray();
            writer.String("order_almost_deal");
            writer.String("delegate_order_danger");
            writer.EndArray();
            writer.EndObject();
            writer.EndObject();
            string json_content = buf.GetString();
            string data = NetworkApi::load_request_data(json_content);
            SocketManager::sendMsg(socket, data.c_str());
            std::thread t1(runPushMsgHeart, context, socket);
            t1.detach();
            // 注册成功
            while (TRUE) {
                // 接收消息
                string result = SocketManager::receiveMsg(socket);
                auto doc = JsonUtil::parseUTF16(result);
                if (doc.IsObject() && doc[L"code"] == 0) {
                    if (doc.HasMember(L"data")) {
                        CString type = doc[L"data"][L"type"].GetString();
                        if (type == L"order_almost_deal") {
                            auto data = doc[L"data"][L"data"].GetObjectW();
                            if (data.HasMember(L"code") && data.HasMember(L"code_name"))
                            {
                                CString st;
                                st.Append(data[L"code"].GetString());
                                st.Append(L" ");
                                st.Append(data[L"code_name"].GetString());
                                st.Append(L" 即将买入:");
                                st.Append(data[L"msg"].GetString());
 
                                std::thread t1(context->showFloatMsg, context, st, MSG_TYPE_WARNING, 5000, TipDlg::POSITION_RIGHT_BOTTOM);
                                t1.detach();
                            }
                        }
                        else if (type == L"delegate_order_danger") {
                            auto data = doc[L"data"][L"data"].GetObjectW();
                            if (data.HasMember(L"code") && data.HasMember(L"code_name"))
                            {
                                CString st;
                                st.Append(data[L"code"].GetString());
                                st.Append(L" ");
                                st.Append(data[L"code_name"].GetString());
                                st.Append(L" 封单不足:");
                                st.Append(data[L"msg"].GetString());
                                std::thread t1(context->showFloatMsg, context, st, MSG_TYPE_WARNING, 5000, TipDlg::POSITION_RIGHT_BOTTOM);
                                t1.detach();
                            }
                        }
                    }
                }
                cout << result << endl;
            }
 
        }
        catch (...) {
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        }
 
    }
 
}
 
void CFloatTradeDlg::startShake(CFloatTradeDlg* context, CString text)
{
    
}
 
void CFloatTradeDlg::showCodePositionInfo()
{
    string code = getInputCode();
    sellManager->init(code);
    CodePosition* codePosition = sellManager->getCodePosition(code);
    SellSetting* sellSetting = sellManager->getSellSetting(code);
    if (codePosition->sell_rules_count > 0) {
        btnSellRule.SetWindowTextW(L"条件单※");
    }
    else {
        btnSellRule.SetWindowTextW(L"条件单");
    }
 
    CString totalStr;
    totalStr.Append(L"¥");
    totalStr.Append(std::to_wstring((int)(codePosition->total * codePosition->costPrice * 100) / 100).c_str());
    labelTotalPosition.SetWindowTextW(totalStr);
 
    CString availableStr;
    availableStr.Append(L"¥");
    availableStr.Append(std::to_wstring((int)(codePosition->available * codePosition->costPrice * 100) / 100).c_str());
 
 
    labelSelledVolume.SetWindowTextW(availableStr);
    fillSellMoney(code, codePosition->available);
 
    checkLockBuyVolume.SetCheck(sellSetting->lockBuy);
    //if (sellSetting->lockBuy) {
    //    if (sellSetting->lockBuyMoney > 0) {
    //        editBuyMoney.SetWindowTextW(to_wstring(sellSetting->lockBuyMoney).c_str());
    //    }
    //}
    //else {
    //    string nowTime = TimeUtil::getNowTime("%H%M%S");
    //    // 09:25:00之前都是跌停价卖
    //    if (stoi(nowTime) < stoi("093000")) {
    //        editBuyMoney.SetWindowTextW(std::to_wstring((int)(codePosition->total * codePosition->costPrice * 100) / 100).c_str());
    //    }
    //    else {
    //        editBuyMoney.SetWindowTextW(L"");
    //    }
    //}
 
}
 
list<UINT> CFloatTradeDlg::distributeVolume(UINT total, UINT percent)
{
    list<UINT> volumeList;
    UINT base = total / percent;
    UINT left = total % percent;
    for (UINT i = 0; i < percent; i++) {
        if (i + 1 <= left) {
            volumeList.push_back(base + 1);
        }
        else {
            volumeList.push_back(base);
        }
    }
    return volumeList;
}
 
UINT CFloatTradeDlg::computeSellVolume(list<UINT> volumeList, list<int> sell_volumes)
{
    if (sell_volumes.size() >= volumeList.size()) {
        return 0;
    }
    list<UINT>::iterator el = volumeList.begin();
    advance(el, sell_volumes.size());
    if (el != volumeList.end()) {
        return *el;
    }
    return 0;
}
 
CFloatTradeDlg::CFloatTradeDlg(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_TRADE, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
 
void CFloatTradeDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, label_atrribute, labelAtrribute);
    DDX_Control(pDX, btn_close_buy, btnCloseBuy);
    DDX_Control(pDX, btn_open_buy, btnOpenBuy);
    //DDX_Control(pDX, btn_buy_mode_want, btnBuyModeWant);
    //DDX_Control(pDX, btn_buy_mode_all, btnBuyModeAll);
    DDX_Control(pDX, check_auto_refresh, checkAutoRefresh);
    DDX_Control(pDX, check_auto_click, checkAutoCancelSell);
    DDX_Control(pDX, check_trade_quick_key, checkTradeQuickKey);
    DDX_Control(pDX, btn_white_list, whiteList);
    DDX_Control(pDX, btn_black_list, blackList);
    DDX_Control(pDX, btn_sell, btnSell);
    DDX_Control(pDX, btn_already_canceled, btnCancelBuy);
 
 
    DDX_Control(pDX, edit_code, editCode);
    //DDX_Control(pDX, btn_buy, btnBuy);
    DDX_Control(pDX, IDC_STATIC_TOTAL_POSITION, labelTotalPosition);
    DDX_Control(pDX, IDC_STATIC_ALREADY_SELL, labelSelledVolume);
    DDX_Control(pDX, IDC_EDIT_SELL_VOLUME, editSellMoney);
    DDX_Control(pDX, IDC_COMBO_SELL_PRICE, comboSellPriceType);
    DDX_Control(pDX, btn_code_trade_info, btnSellRule);
    DDX_Control(pDX, IDC_CHECK_LOCK_SELL_VOLUME, checkLockVolume);
    DDX_Control(pDX, IDC_CHECK_LOCK_BUY_VOLUME, checkLockBuyVolume);
    DDX_Control(pDX, IDC_EDIT_BUY_VOLUME, editBuyMoney);
    DDX_Control(pDX, label_code_info, labelCodeInfo);
    DDX_Control(pDX, btn_buy, btnBuy);
    DDX_Control(pDX, IDC_COMBO_BUY_PRICE, comboBuyPriceType);
}
 
BEGIN_MESSAGE_MAP(CFloatTradeDlg, CDialogEx)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_WM_HOTKEY()
    ON_BN_CLICKED(btn_already_canceled, &CFloatTradeDlg::OnClickedBtnAlreadyCanceled)
    ON_BN_CLICKED(btn_black, &CFloatTradeDlg::OnClickedBtnBlack)
    ON_BN_CLICKED(btn_close_buy, &CFloatTradeDlg::OnClickedBtnCloseBuy)
    ON_BN_CLICKED(btn_open_buy, &CFloatTradeDlg::OnClickedBtnOpenBuy)
    //ON_BN_CLICKED(btn_pause_buy, &CFloatTradeDlg::OnClickedBtnPauseBuy)
    //ON_BN_CLICKED(btn_pause_buy_remove, &CFloatTradeDlg::OnClickedBtnPauseBuyRemove)
    ON_BN_CLICKED(btn_remove_black, &CFloatTradeDlg::OnClickedBtnRemoveBlack)
    ON_BN_CLICKED(btn_remove_white, &CFloatTradeDlg::OnClickedBtnRemoveWhite)
    ON_BN_CLICKED(btn_sell, &CFloatTradeDlg::OnClickedBtnSell)
    ON_BN_CLICKED(btn_want_buy, &CFloatTradeDlg::OnClickedBtnWantBuy)
    ON_BN_CLICKED(btn_want_buy_remove, &CFloatTradeDlg::OnClickedBtnWantBuyRemove)
    ON_BN_CLICKED(btn_white, &CFloatTradeDlg::OnClickedBtnWhite)
    ON_BN_CLICKED(check_auto_click, &CFloatTradeDlg::OnClickedCheckAutoClick)
    ON_BN_CLICKED(check_auto_refresh, &CFloatTradeDlg::OnClickedCheckAutoRefresh)
    ON_BN_CLICKED(check_trade_quick_key, &CFloatTradeDlg::OnClickedCheckTradeQuickKey)
    ON_WM_COPYDATA()
    ON_BN_CLICKED(btn_want_list, &CFloatTradeDlg::OnBnClickedwantlist)
    //ON_BN_CLICKED(btn_pause_buy_list, &CFloatTradeDlg::OnBnClickedpausebuylist)
    ON_BN_CLICKED(btn_black_list, &CFloatTradeDlg::OnBnClickedblacklist)
    ON_BN_CLICKED(btn_white_list, &CFloatTradeDlg::OnBnClickedwhitelist)
    ON_WM_CLOSE()
    ON_BN_CLICKED(btn_buy, &CFloatTradeDlg::OnBnClickedbuy)
    ON_BN_CLICKED(btn_config, &CFloatTradeDlg::OnBnClickedconfig)
    ON_WM_CTLCOLOR()
    ON_BN_CLICKED(btn_code_trade_info, &CFloatTradeDlg::OnBnClickedcodetradeinfo)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_1, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_2, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_3, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_4, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_5, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_6, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_7, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_8, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_9, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_10, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_11, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_SELL_VOLUME_13, &CFloatTradeDlg::OnBnClickedButtonSellVolume1)
    ON_BN_CLICKED(IDC_BUTTON_REFRESH, &CFloatTradeDlg::OnBnClickedButtonRefresh)
    ON_BN_CLICKED(IDC_BUTTON_MUST_LIST, &CFloatTradeDlg::OnBnClickedmustlist)
    ON_BN_CLICKED(IDC_BUTTON_ADD_MUST, &CFloatTradeDlg::OnBnClickedmust)
    ON_BN_CLICKED(IDC_BUTTON_REMOVE_MUST, &CFloatTradeDlg::OnBnClickedremovefrommust)
    ON_MESSAGE(WM_FLOAT_MSG, &CFloatTradeDlg::OnFloatMsg)
    ON_BN_CLICKED(IDC_CHECK_LOCK_SELL_VOLUME, &CFloatTradeDlg::OnBnClickedCheckLockSellVolume)
    ON_BN_CLICKED(IDC_BUTTON_ADD_WANT, &CFloatTradeDlg::OnBnClickedButtonAddWant)
    ON_BN_CLICKED(IDC_BUTTON_REMOVE_WANT, &CFloatTradeDlg::OnBnClickedButtonRemoveWant)
    ON_BN_CLICKED(IDC_BUTTON_WANT_LIST, &CFloatTradeDlg::OnBnClickedButtonWantList)
    ON_BN_CLICKED(IDC_BUTTON_GET_POSITIONS, &CFloatTradeDlg::OnBnClickedButtonGetPositions)
    ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_VOLUME, &CFloatTradeDlg::OnDeltaposSpinVolume)
    ON_BN_CLICKED(IDC_CHECK_LOCK_BUY_VOLUME, &CFloatTradeDlg::OnBnClickedCheckLockBuyVolume)
    ON_WM_KEYDOWN()
    ON_BN_CLICKED(btn_code_trade_update_watch_buy1, &CFloatTradeDlg::OnBnClickedcodetradeupdatewatchbuy1)
    ON_BN_CLICKED(IDC_BUTTON_ADD_GREEN, &CFloatTradeDlg::OnBnClickedButtonAddGreen)
    ON_BN_CLICKED(IDC_BUTTON_REMOVE_GREEN, &CFloatTradeDlg::OnBnClickedButtonRemoveGreen)
    ON_BN_CLICKED(IDC_BUTTON_GREEN_LIST, &CFloatTradeDlg::OnBnClickedButtonGreenList)
    ON_BN_CLICKED(btn_add_watch_limit_up_for_sell, &CFloatTradeDlg::OnBnClickedaddwatchlimitupforsell)
    ON_WM_LBUTTONDOWN()
    ON_BN_CLICKED(check_ocr_code, &CFloatTradeDlg::OnClickedCheckOcrCode)
    ON_WM_DESTROY()
    ON_STN_CLICKED(IDC_STATIC_TOTAL_POSITION, &CFloatTradeDlg::OnStnClickedStaticTotalPosition)
    ON_STN_CLICKED(IDC_STATIC_LS_CODE_NAME, &CFloatTradeDlg::OnStnClickedStaticLsCodeName)
    ON_BN_CLICKED(IDC_BUTTON_LS_ADD_FORBIDDEN, &CFloatTradeDlg::OnBnClickedButtonLsAddForbidden)
    ON_BN_CLICKED(IDC_BUTTON_LS_BUY, &CFloatTradeDlg::OnBnClickedButtonLsBuy)
    ON_BN_CLICKED(IDC_BUTTON_LS_NOT_BUY, &CFloatTradeDlg::OnBnClickedButtonLsNotBuy)
    ON_STN_CLICKED(IDC_STATIC_LS_PLATES, &CFloatTradeDlg::OnStnClickedStaticLsPlates)
    ON_CBN_SELCHANGE(IDC_COMBO_CONTINUE_BUY_MONEY, &CFloatTradeDlg::OnCbnSelchangeComboContinueBuyList)
    ON_BN_CLICKED(IDC_CHECK_CONTINUE_BUY, &CFloatTradeDlg::OnBnClickedContinueBuy)
END_MESSAGE_MAP()
 
// CFloatTradeDlg 消息处理程序
 
void CFloatTradeDlg::initView()
{
    bigFont.CreateFont(22 * 2, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, L"微软雅黑");
    middleFont.CreateFont(24, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, L"微软雅黑");
 
    // 设置透明度(0透明,255完全不透明)
    ModifyStyleEx(0, WS_EX_LAYERED);
    BYTE opacity = 230; // 透明度值取值范围为0~255
    SetLayeredWindowAttributes(RGB(0, 0, 0), opacity, LWA_ALPHA);
 
    //设置字体
    labelAtrribute.SetFont(&middleFont, FALSE);
    labelCodeInfo.SetFont(&middleFont, FALSE);
    btnCancelBuy.SetFont(&bigFont, FALSE);
    btnSell.SetFont(&bigFont, FALSE);
    btnBuy.SetFont(&bigFont, FALSE);
 
    ((CStatic *)GetDlgItem(IDC_STATIC_LS_TIME))->SetFont(&middleFont, FALSE);
    ((CStatic*)GetDlgItem(IDC_STATIC_LS_CODE_NAME))->SetFont(&middleFont, FALSE);
    ((CStatic*)GetDlgItem(IDC_STATIC_LS_PLATES))->SetFont(&middleFont, FALSE);
    ((CStatic*)GetDlgItem(IDC_STATIC_LS_BIG_ORDERS))->SetFont(&middleFont, FALSE);
 
 
    if (dealQueueDlg == nullptr) {
        dealQueueDlg = new DealQueueDlg();
        dealQueueDlg->Create(IDD_DIALOG_DEAL_QUEUE, this);
    }
}
 
void CFloatTradeDlg::initData()
{
    sellManager = new SellManager();
    buyWin = 0;
    requestingCode = "";
    if (ConfigUtil::isTradeRefresh()) {
        checkAutoRefresh.SetCheck(TRUE);
    }
    else {
        checkAutoRefresh.SetCheck(FALSE);
    }
 
    if (ConfigUtil::isTradeQuickKey()) {
        checkTradeQuickKey.SetCheck(TRUE);
    }
    else {
        checkTradeQuickKey.SetCheck(FALSE);
    }
 
    // 初始化刷新频率
 
 
    HWND hWnd = CWnd::GetSafeHwnd();
    //设置热键/快捷键
    RegisterHotKey(hWnd, HOT_KEY_SELL_ALL, MOD_SHIFT, VK_F10);
    RegisterHotKey(hWnd, HOT_KEY_CANCEL, MOD_SHIFT, VK_F1);
    RegisterHotKey(hWnd, HOT_KEY_BUY, MOD_SHIFT, VK_F9);
    RegisterHotKey(hWnd, HOT_KEY_UPDATE_BSD, MOD_SHIFT, VK_F8); // 板上盯
    RegisterHotKey(hWnd, HOT_KEY_ADD_WANT_BUY, MOD_SHIFT, VK_F7); // 成交队列
 
 
 
    //置顶
    SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    //请求网络数据
 
    std::thread t1(getTradeState, this);
    t1.detach();
 
    std::thread t2(getTradeMode, this);
    t2.detach();
 
    //启动交易刷新
    std::thread t3(ThsUtil::run_trade_refresh);
    t3.detach();
 
    // 启动代码识别
    std::thread t4(runTHSCodeOCR, this);
    t4.detach();
 
    // 设置位置
    MyPoint p = ConfigUtil::getWindowPos();
    RECT rect;
    Win32Util::getWindowRect(GetSafeHwnd(), &rect);
    Win32Util::moveWin(GetSafeHwnd(), p.x, p.y, (rect.right - rect.left + 1), (rect.bottom - rect.top + 1));
    comboSellPriceType.AddString(_T("价格笼子"));
    comboSellPriceType.AddString(_T("跌停价"));
    comboSellPriceType.AddString(_T("涨停价"));
    comboSellPriceType.AddString(_T("现价"));
    comboSellPriceType.AddString(_T("买5价"));
    comboSellPriceType.SetCurSel(0);
 
    comboBuyPriceType.AddString(_T("价格笼子"));
    comboBuyPriceType.AddString(_T("跌停价"));
    comboBuyPriceType.AddString(_T("涨停价"));
    comboBuyPriceType.AddString(_T("现价"));
    comboBuyPriceType.AddString(_T("买5价"));
    comboBuyPriceType.SetCurSel(0);
 
 
    std::thread thread_ru(regularUpdatePositionInfo, this);
    thread_ru.detach();
 
    std::thread thread_push_msg_receive(runPushMsgReceiver, this);
    thread_push_msg_receive.detach();
 
    // 获取持仓
    std::thread thread_request_all_position(requestAllPositions, this);
    thread_request_all_position.detach();
 
    // 加载代码名称
    Constant::loadCodeNames();
 
 
    // 按下组合键
    list<HWND> hwndList = Win32Util::searchWindow("同花顺");
    if (hwndList.size() > 0) {
        UINT hwnd = (UINT) * (hwndList.begin());
        Win32Util::focus((HWND)hwnd);
        //SendMessage(hwnd, WM_KEYDOWN, VK_SHIFT);
        //SendMessage(hwnd, WM_KEYDOWN, VK_F12);
        //// 发送 SHIFT 键释放事件
        //PostMessage(hwnd, WM_KEYUP, VK_F12);
        //PostMessage(hwnd, WM_KEYUP, VK_SHIFT);
        keybd_event(VK_SHIFT, 0, KEYEVENTF_EXTENDEDKEY, 0); // 按下按键
        keybd_event(VK_F12, 0, KEYEVENTF_EXTENDEDKEY, 0);
 
        keybd_event(VK_SHIFT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); // 释放按键
        keybd_event(VK_F12, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); // 释放按键
    }
 
    editBuyMoney.SetWindowTextW(to_wstring(ConfigUtil::getBuyMoney()).c_str());
 
    fillMoneys();
    lastBuyClickTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
    canOCRCode = ConfigUtil::getEnableOCRCode();
    if (canOCRCode) {
        this->hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, 0, 0);
    }
    ((CButton*)GetDlgItem(check_ocr_code))->SetCheck(canOCRCode);
    thread(runReadLSPlaceOrderRecords, this).detach();
}
 
 
BOOL CFloatTradeDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    // 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
    //  执行此操作
    SetIcon(m_hIcon, TRUE);            // 设置大图标
    SetIcon(m_hIcon, FALSE);        // 设置小图标
 
    //ShowWindow(SW_MINIMIZE);
    ShowWindow(SW_SHOW);
 
    //设置透明度
    // 设置对话框样式为Layered窗口
 
 
    initView();
 
 
 
    // TODO: 在此添加额外的初始化代码
    //初始化配置文件
    initData();
 
 
    // 测试
    return TRUE;
}
 
// 鼠标钩子过程函数示例
LRESULT CALLBACK CFloatTradeDlg::MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode >= 0) {
        MSLLHOOKSTRUCT* pMouseStruct = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam);
        switch (wParam) {
        case WM_LBUTTONDOWN:
            CFloatTradeDlg::ocrCodeExpireTimestamp = TimeUtil::getNowTimestamp() + 1000;
            break;
        }
    }
    return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
 
 
 
 
 
 
 
 
 
 
void CFloatTradeDlg::OnHotKey(UINT nHotKeyId, UINT nKey, UINT nModifiers) {
 
    if (!checkTradeQuickKey.GetCheck()) {
        //未开启交易快捷键
        return;
    }
 
    if (nHotKeyId == HOT_KEY_SELL_ALL) {
 
        list<HWND> hwnds = Win32Util::searchWindow("可转债交易");
        HWND cbHwnd = 0;
        if (hwnds.size() > 0) {
            for (list<HWND>::iterator e = hwnds.begin(); e != hwnds.end(); ++e) {
                HWND hwnd = *e;
                if (string("可转债交易")._Equal(Win32Util::getWindowName(hwnd))) {
                    auto fw = ::GetForegroundWindow();
                    if (fw == hwnd) {
                        cbHwnd = hwnd;
                    }
                    break;
                }
            }
        }
 
        if (cbHwnd > 0) {
            string data = "你是不是傻";
            COPYDATASTRUCT cds;
            cds.dwData = 100; // 可以自定义标识数据
            cds.cbData = (DWORD)data.size() + 1; // 字符串数据长度
            cds.lpData = (PVOID)data.c_str();
            ::SendMessage(cbHwnd, WM_COPYDATA, NULL, (LPARAM)&cds);
        }
        else {
            cout << "卖" << endl;
            OnClickedBtnSell();
        }
    }
    else if (nHotKeyId == HOT_KEY_CANCEL) {
        cout << "撤当前" << endl;
        OnClickedBtnAlreadyCanceled();
    }
    else if (nHotKeyId == HOT_KEY_BUY) {
        cout << "买入" << endl;
        buy();
    }
    else if (nHotKeyId == HOT_KEY_UPDATE_BSD) {
        cout << "更板上盯" << endl;
        OnBnClickedcodetradeupdatewatchbuy1();
    }
    else if (nHotKeyId == HOT_KEY_ADD_WANT_BUY) {
        OnBnClickedButtonAddWant();
    }
 
 
}
 
 
// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。
 
void CFloatTradeDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // 用于绘制的设备上下文
 
        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
 
        // 使图标在工作区矩形中居中
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;
 
        // 绘制图标
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}
 
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CFloatTradeDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}
 
 
 
void CFloatTradeDlg::OnClickedBtnAlreadyCanceled()
{
 
    // showTips(L"这是条测试消息~~~~~~~", MSG_TYPE_FAIL,1000);
 
    //list<HWND> hwnds = Win32Util::searchWindow("副屏1");
    //HWND hwnd = *(hwnds.begin());
    //::SetWindowPos(hwnd, HWND_TOPMOST,0,0,0,0, SWP_NOMOVE | SWP_NOSIZE);
    //return;
 
    // 撤单
    try {
        string code = getCode();
        NetworkApi::cancel_buy(CString(code.c_str()));
        CString msg(code.c_str());
        msg.Append(_T("撤单提交成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
    catch (CString st) {
        setMsg(st, MSG_TYPE_FAIL);
    }
}
 
void CFloatTradeDlg::OnBnClickedbuy()
{
    /*
    CString codew;
    editCode.GetWindowTextW(codew);
    CBuyDlg dlg(codew);
    INT_PTR nResponse = dlg.DoModal();
    */
    buy();
}
 
 
 
void CFloatTradeDlg::OnClickedBtnBlack()
{
    // 加入黑名单
    try {
        string code = getCode();
        NetworkApi::add_black(code);
        CString msg(code.c_str());
        msg.Append(_T("加入黑名单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
 
void CFloatTradeDlg::OnClickedBtnCloseBuy()
{
    // 关闭交易
    try {
        NetworkApi::set_buy_state(FALSE);
        CString msg;
        msg.Append(_T("关闭交易成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
        getTradeState(this);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnClickedBtnOpenBuy()
{
    // 开启交易
    try {
        NetworkApi::set_buy_state(TRUE);
        CString msg;
        msg.Append(_T("开启交易成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
        getTradeState(this);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnClickedBtnPauseBuy()
{
    // 加入暂不买
    try {
        string code = getCode();
        NetworkApi::add_pause_buy(code);
        CString msg(code.c_str());
        msg.Append(_T("加入暂不买成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnClickedBtnPauseBuyRemove()
{
    // 移除暂不买
    try {
        string code = getCode();
        NetworkApi::remove_pause_buy(code);
        CString msg(code.c_str());
        msg.Append(_T("移除暂不买成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnClickedBtnRemoveBlack()
{
    // 移除黑名单
    try {
        // 批量移除黑名单
        /*string codes_str = "603580,003010,605388,603511,002133,603106,000888,603729,301396,603517,600718,300643,603566,603716,002025,603228,002296,000599,002760,603881,601619,600590,001389,002384,605186,600120,603296,002380,002734,002357,002875,002707,002354,002993,300078,600183,000056,000710,002305,002800,002261,600719,002970,300067,600095,002883,605069,002560,300253,002444,300819,002913,000428,300454,301299,300766,603028,600280,600665,603829,000592,600143,603238,002285,000757,002946,000722,000007,001282,002976,000818,002152,002295,603108,603183,001333,000796,002248,603280,002982,603860,600818,002205,001298,002122,605228,300244,002743,603578,002335,300153,603305,600595,600588,600336,000899,002953,605255,605398,605011,300155,002622,000715,603312,002436,002529,002475,002881,603063,000503,002173,002364,002134,300534,002418,002105,300599,603938,600868,600376,603109,300528,002905,600889,002785,002044,002363,002065,002463,603888,000048,301230,601921,300746,002188,000151,603269,600708,002779,003038,603693,002209,603001,002273,002559,002662,600238,600650,002733,000657,000920,000815,600322,600456,000558,603920,002448,603506,600032,000688,603193,600728,600960,002068,603979,002888,001313,600506,300947,300451,600319,001279,002666,603286,600979,002484,002802,605162,300058,300069,002847,603121,002990,605333,002599,002394,600602,600101,002358,002715,603882,300946,300941,600758,603901,603967,603268,603038,603950,001258,300488,002851,002943,002580,603610,603215,600533,603876,002938,600318,002229,002298,605259,603273,002163,601777,600114,002725,002286,600785,300288,600825,002965,603076,601579,603500,601869,002518,002829,000659,600173,601218,601566";
        std::vector<std::string> tokens;
        size_t start = 0;
        size_t end = codes_str.find(',');
 
        while (end != std::string::npos) {
            tokens.push_back(codes_str.substr(start, end - start));
            start = end + 1;
            end = codes_str.find(',', start);
        }
 
        tokens.push_back(codes_str.substr(start));
        for (vector<string>::iterator e = tokens.begin();e != tokens.end();++e) {
            auto    code = *e;
            NetworkApi::remove_black(code);
        }*/
 
        string code = getCode();
        NetworkApi::remove_black(code);
        CString msg(code.c_str());
        msg.Append(_T("移除黑名单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnClickedBtnRemoveWhite()
{
    // 移除白名单
    try {
        string code = getCode();
        NetworkApi::remove_white(code);
        CString msg(code.c_str());
        msg.Append(_T("移除白名单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnClickedBtnSell()
{
    bool showMsg = true;
    try {
        string code = "";
        try {
            code = getCode();
        }
        catch (...) {
 
        }
        //if (requestingCode.size() >= 6) {
        //    showTips(L"持仓请求未结束(150ms以后才能卖)", MSG_TYPE_FAIL, 1000);
        //    return;
        //}
 
        CString sell_money;
        editSellMoney.GetWindowTextW(sell_money);
        if (!StringUtil::isNumber(StringUtil::cstring2String(sell_money))) {
            showTips(L"卖金额输入错误", MSG_TYPE_FAIL, 1000);
            return;
        }
        int sell_money_int = stoi(StringUtil::cstring2String(sell_money));
        if (sell_money_int <= 0) {
            showTips(L"卖金额不能为0", MSG_TYPE_FAIL, 1000);
            return;
        }
 
 
        // 卖
 
 
        int sell_volume_int = sellMoneyToVolume(code, sell_money_int);
        if (sell_volume_int == 0) {
            sell_volume_int = 100;
        }
        // 获取持仓
        CodePosition* pos = sellManager->getCodePosition(code);
        if (pos != nullptr) {
            if (pos->available > 0 && sell_volume_int > pos->available) {
                // 卖剩余的所有
                sell_volume_int = pos->available;
            }
        }
 
        std::thread t1(this->sellByVolume, code, sell_volume_int, this);
        t1.detach();
    }
    catch (wstring st) {
        CString msg(st.c_str());
        //setMsg(msg, MSG_TYPE_FAIL);
        showTips(msg, MSG_TYPE_FAIL, 1000);
    }
 
}
 
 
void CFloatTradeDlg::OnClickedBtnWantBuy()
{
    // 加入想买
    try {
        string code = getCode();
        NetworkApi::add_want_buy(code);
        CString msg(code.c_str());
        msg.Append(_T("加入想买单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnClickedBtnWantBuyRemove()
{
    // 移除想买
    try {
        string code = getCode();
        NetworkApi::remove_want_buy(code);
        CString msg(code.c_str());
        msg.Append(_T("移除想买单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnClickedBtnWhite()
{
    // 加入白名单
    try {
        string code = getCode();
        NetworkApi::add_white(code);
        CString msg(code.c_str());
        msg.Append(_T("加入白名单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
void CFloatTradeDlg::OnBnClickedmustlist()
{
    try {
        std::list<wstring> wlist = NetworkApi::list_must_buy();
 
        MessageBox(formatListResult(wlist), TEXT("红名单"), MB_TASKMODAL);
    }
    catch (wstring st) {
        CString cs(st.c_str());
        AfxMessageBox(cs);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedmust()
{
    try {
        string code = getCode();
        NetworkApi::add_must_buy(code);
        CString msg(code.c_str());
        msg.Append(_T("加入红名单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedremovefrommust()
{
    // 移除白名单
    try {
        string code = getCode();
        NetworkApi::remove_must_buy(code);
        CString msg(code.c_str());
        msg.Append(_T("移除红名单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
 
void CFloatTradeDlg::OnClickedCheckAutoClick()
{
    // 判断是否选中了代码
    string code = "";
    try {
        code = this->getInputCode();
    }
    catch (wstring msg) {
        checkAutoCancelSell.SetCheck(!checkAutoCancelSell.GetCheck());
        AfxMessageBox(msg.c_str());
        return;
    }
 
    if (checkAutoCancelSell.GetCheck()) {
        // 撤所有
        try {
            NetworkApi::set_auto_cancel_sell_mode(code, 0);
        }
        catch (wstring msg) {
            checkAutoCancelSell.SetCheck(FALSE);
            AfxMessageBox(msg.c_str());
        }
    }
    else {
        // 撤机器
        try {
            NetworkApi::set_auto_cancel_sell_mode(code, 1);
        }
        catch (wstring msg) {
            checkAutoCancelSell.SetCheck(TRUE);
            AfxMessageBox(msg.c_str());
        }
    }
}
 
 
void CFloatTradeDlg::OnClickedCheckAutoRefresh()
{
    // 交易刷新
    ConfigUtil::setTradeRefresh(checkAutoRefresh.GetCheck());
}
 
 
 
void CFloatTradeDlg::OnClickedCheckTradeQuickKey()
{
    // 交易快捷键
    ConfigUtil::setTradeQuickKey(checkTradeQuickKey.GetCheck());
}
 
LRESULT CFloatTradeDlg::OnFloatMsg(WPARAM wParam, LPARAM lParam)
{
 
    FloatMsg* pData = (FloatMsg*)lParam;
 
    showTips(CString(pData->msg.c_str()), pData->msgType, pData->showMS, pData->position);
 
    return LRESULT();
}
 
 
 
CString CFloatTradeDlg::formatListResult(std::list<wstring> wlist) {
    CString st;
    int index = 0;
    for (std::list<wstring>::iterator ele = wlist.begin(); ele != wlist.end(); ++ele) {
        wstring ws = *ele;
        st.Append(ws.c_str());
        if (index + 1 != wlist.size() && index % 2 == 0)
        {
            st.Append(L",");
        }
        if (index % 2 == 1) {
            st.Append(L"\n");
        }
        index++;
    }
    return st;
}
 
void CFloatTradeDlg::initSellSettingView()
{
    SellSettingView sellSettingView;
    string code = "";
    try {
        code = getInputCode();
    }
    catch (...) {
        return;
    }
 
}
 
string CFloatTradeDlg::getInputCode()
{
    CString codew;
    editCode.GetWindowTextW(codew);
    if (codew.GetLength() != 6) {
        throw wstring(L"代码输入错误");
    }
    return StringUtil::cstring2String(codew);
}
 
void CFloatTradeDlg::OnSellCallback(string code, CWnd* context)
{
    CFloatTradeDlg* dlg = ((CFloatTradeDlg*)context);
    try {
        dlg->sell(code, dlg);
    }
    catch (wstring msg) {
        dlg->showTips(msg.c_str(), MSG_TYPE_FAIL);
    }
}
 
void CFloatTradeDlg::OnSellCloseCallback(string code, CWnd* context)
{
    SellDlg* dlg = ((CFloatTradeDlg*)context)->sellDlgMap[code];
    dlg->CloseWindow();
    delete dlg;
    ((CFloatTradeDlg*)context)->sellDlgMap.erase(code);
}
 
void CFloatTradeDlg::sellByVolume(string code, int volume, CFloatTradeDlg* context)
{
 
    //0-价格笼子 1-跌停价  2-涨停价 3-现价
    int sellPriceType = context->comboSellPriceType.GetCurSel();
    if (sellPriceType < 0 || sellPriceType > 10) {
        context->showTips(L"请选择卖价类型", MSG_TYPE_FAIL, 1000);
        return;
    }
 
    string nowTime = TimeUtil::getNowTime("%H%M%S");
    // 09:25:00之前都是跌停价卖
    if (stoi(nowTime) >= stoi("092500")) {
        if (sellPriceType == 1) {
            sellPriceType = 0;
            context->comboSellPriceType.SetCurSel(0);
        }
    }
 
    // 卖
    try {
        int orderRef = context->sellManager->sell(code, volume, sellPriceType);
        std::thread t1(context->requestSellResult, orderRef, context, 2000);
        t1.detach();
 
        std::thread t2(context->requestAllPositions, context);
        t2.detach();
 
 
        //context->showTips(L"卖提交成功", MSG_TYPE_SUCCESS, 1000);
        showFloatMsg(context, L"卖提交成功", MSG_TYPE_SUCCESS, 1000);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        //setMsg(msg, MSG_TYPE_FAIL);
        showFloatMsg(context, msg, MSG_TYPE_FAIL, 1000);
    }
 
}
 
void CFloatTradeDlg::sell(string code, CFloatTradeDlg* dlg)
{
    //0-价格笼子 1-跌停价  2-涨停价 3-现价
    int sellPriceType = dlg->comboSellPriceType.GetCurSel();
    if (sellPriceType < 0 || sellPriceType > 10) {
        dlg->showTips(L"请选择卖价类型", MSG_TYPE_FAIL, 1000);
        return;
    }
 
    string nowTime = TimeUtil::getNowTime("%H%M%S");
    // 09:25:00之前都是跌停价卖
    if (stoi(nowTime) >= stoi("092500")) {
        if (sellPriceType == 1) {
            sellPriceType = 0;
            dlg->comboSellPriceType.SetCurSel(0);
        }
    }
 
    // 卖
    try {
        int orderRef = dlg->sellManager->sell(code, sellPriceType);
        std::thread t1(dlg->requestSellResult, orderRef, dlg, 2000);
        t1.detach();
        dlg->showTips(L"卖提交成功", MSG_TYPE_SUCCESS, 1000);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        dlg->showTips(msg, MSG_TYPE_FAIL, 1000);
    }
}
 
void CFloatTradeDlg::fillSellMoney(string code, int volume)
{
    CodePosition* codePosition = sellManager->getCodePosition(code);
    editSellMoney.SetWindowTextW(to_wstring((int)(codePosition->costPrice * volume * 100) / 100).c_str());
}
 
int CFloatTradeDlg::sellMoneyToVolume(string code, int money)
{
    CodePosition* codePosition = sellManager->getCodePosition(code);
    int volume = ((int)round((money / codePosition->costPrice) * 100)) / 100;
    return SellManager::sellMoneyToVolume(money, codePosition->costPrice);
}
 
void CFloatTradeDlg::buy()
{
 
 
    try {
        auto now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
        // 买入频率限制
        if (now - lastBuyClickTime < 1000) {
            throw wstring(L"买入时间小于1000ms");
        }
        lastBuyClickTime = now;
        string code = getInputCode();
        CString buyMoneyw;
        editBuyMoney.GetWindowTextW(buyMoneyw);
        if (buyMoneyw.GetLength() < 1) {
            throw wstring(L"请输入买入金额");
        }
 
        /*CodePosition* pos = sellManager->getCodePosition(code);
        if (pos == nullptr) {
            throw wstring(L"没有获取到现价");
        }*/
        int buyMoney = stoi(StringUtil::cstring2String(buyMoneyw));
        /*if (round(pos->costPrice) <= 0) {
            throw wstring(L"未获取到现价");
        }*/
        /*int volume = SellManager::sellMoneyToVolume(buyMoney, pos->costPrice);
        if (volume <= 0) {
            CString msg = L"买入量不能小于100(单价:";
            msg.Append(to_wstring(pos->costPrice).c_str());
            msg.Append(L")");
            throw wstring(msg);
        }*/
        NetworkApi::buy(code, buyMoney, comboBuyPriceType.GetCurSel());
        showTips(L"买入提交成功", MSG_TYPE_SUCCESS, 1000);
    }
    catch (wstring st) {
        showTips(st.c_str(), MSG_TYPE_FAIL, 1000);
    }
}
 
void CFloatTradeDlg::showFloatMsg(CFloatTradeDlg* context, CString msg, MSG_TYPE msgType, int showMS, int position)
{
    FloatMsg params;
    params.msg = StringUtil::cstring2String(msg);
    params.msgType = msgType;
    params.showMS = showMS;
    params.position = position;
    LPARAM lm = (LPARAM)&params;
    ::SendMessage(context->GetSafeHwnd(), WM_FLOAT_MSG, 0, lm);
}
 
void CFloatTradeDlg::requestAllPositions(CFloatTradeDlg* context)
{
    try {
        string result = NetworkApi::get_all_positions();
        auto doc = JsonUtil::parseUTF16(result);
        if (doc[L"code"].GetInt() == 0) {
            auto data = doc[L"data"].GetArray();
            for (int i = 0; i < data.Size(); i++) {
                auto item = data[i].GetObjectW();
                string code = StringUtil::cstring2String(item[L"code"].GetString());
                context->sellManager->init(code);
                CodePosition codePosition;
                codePosition.total = item[L"total"].GetInt();
                codePosition.available = item[L"available"].GetInt();
                codePosition.code = code;
                codePosition.name = item[L"code_name"].GetString();
                codePosition.costPrice = item[L"cost_price"].GetFloat();
                codePosition.sell_rules_count = 0;
                context->positionMap[codePosition.code] = codePosition;
 
                CodePosition* positionForSell = context->sellManager->getCodePosition(code);
                if (positionForSell != nullptr) {
                    positionForSell->total = item[L"total"].GetInt();
                    positionForSell->available = item[L"available"].GetInt();
                    positionForSell->code = code;
                    positionForSell->name = item[L"code_name"].GetString();
                    positionForSell->costPrice = item[L"cost_price"].GetFloat();
                    positionForSell->sell_rules_count = 0;
                }
            }
        }
    }
    catch (...) {
 
    }
}
 
void CFloatTradeDlg::fillMoneys()
{
    list<int> volumeList = ConfigUtil::getVolumesSetting();
    int ids[] = {
    IDC_BUTTON_SELL_VOLUME_13,IDC_BUTTON_SELL_VOLUME_11,IDC_BUTTON_SELL_VOLUME_10,IDC_BUTTON_SELL_VOLUME_9,IDC_BUTTON_SELL_VOLUME_8,IDC_BUTTON_SELL_VOLUME_7,IDC_BUTTON_SELL_VOLUME_6,IDC_BUTTON_SELL_VOLUME_5, IDC_BUTTON_SELL_VOLUME_4, IDC_BUTTON_SELL_VOLUME_3, IDC_BUTTON_SELL_VOLUME_2, IDC_BUTTON_SELL_VOLUME_1
    };
 
    if (volumeList.size() == 12) {
        int index = 0;
        for (list<int>::iterator e = volumeList.begin(); e != volumeList.end(); ++e) {
            int volume = *e;
            CButton* edit = (CButton*)GetDlgItem(ids[index]);
            edit->SetWindowTextW(to_wstring(volume).c_str());
            index++;
        }
    }
}
 
void CFloatTradeDlg::runTHSCodeOCR(CFloatTradeDlg* context)
{
    if (context->thsCodeOCR == nullptr) {
        context->thsCodeOCR = new ThsCodeOCR();
    }
 
    while (TRUE)
    {
        try {
            if (context->canOCRCode) {
                if (TimeUtil::getNowTimestamp() < context->ocrCodeExpireTimestamp) {
                    string code = context->thsCodeOCR->ocrCode();
                    // 设置目标代码
                    if (!code._Equal(context->lastOCRCode)) {
                        context->lastOCRCode = code;
                        context->setTargetCode(code);
                    }
                }
            }
        }
        catch (...) {
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
    }
}
 
void CFloatTradeDlg::setTargetCode(string code)
{
    editCode.SetWindowTextW(CString(code.c_str()));
    requestingCode = code;
 
    //请求网络获取消息
    std::thread t1(getCodeDesc, code, this);
    t1.detach();
 
    std::thread t2(requestCodePosition, code, this, TRUE, 0);
    t2.detach();
 
    std::thread t3(getAutoCancelSellMode, this, code);
    t3.detach();
 
    std::thread t4(getContinueBuyInfo, this, code);
    t4.detach();
 
    if (dealQueueDlg != nullptr && dealQueueDlg->IsWindowVisible()) {
        dealQueueDlg->setTargetCode(code);
    }
 
 
}
 
void CFloatTradeDlg::setLockInfo(string code, bool lock, int lockMoney)
{
    string nowTimeStr = TimeUtil::getNowTime("%H%M%S");
    SellSetting* sellSetting = sellManager->getSellSetting(code);
    sellSetting->lock = TRUE;
    sellSetting->lockMoney = lockMoney;
    sellSetting->lockTime = stoi(nowTimeStr.c_str());
    sellManager->syncSettingToFile(code);
}
 
void CFloatTradeDlg::setLockBuyInfo(string code, bool lock, int lockMoney)
{
    string nowTimeStr = TimeUtil::getNowTime("%H%M%S");
    SellSetting* sellSetting = sellManager->getSellSetting(code);
    sellSetting->lockBuy = TRUE;
    sellSetting->lockBuyMoney = lockMoney;
    sellSetting->lockTime = stoi(nowTimeStr.c_str());
    sellManager->syncSettingToFile(code);
}
 
void CFloatTradeDlg::requestReadLSPlaceOrderRecords(CFloatTradeDlg* context)
{
    string result = LowSuctionNetworkApi::read_place_order_queue();
   auto doc =    JsonUtil::parseUTF16(result);
   if (doc.IsObject()) {
       if (doc[L"code"].GetInt() == 0) {
          auto data =  doc[L"data"].GetObjectW();
          LowSuctionQueue info;
          info.id = StringUtil::cstring2String( data[L"id"].GetString());
          info.code = StringUtil::cstring2String(data[L"code"].GetString());
          info.name = StringUtil::cstring2String(data[L"name"].GetString());
          info.time_str = StringUtil::cstring2String(data[L"time_str"].GetString());
          info.bigOrderInfo = StringUtil::cstring2String(data[L"bigOrderInfo"].GetString());
 
          auto platesInfo = data[L"platesInfo"].GetObjectW();
          for (auto it = platesInfo.MemberBegin();it != platesInfo.MemberEnd();++it) {
              CString k = it->name.GetString();
              auto value = it->value.GetArray();
              info.plates.push_back(StringUtil::cstring2String(k));
              for (int i = 0;i < value.Size();i++) {
 
                  info.plateLimitUpCodes[StringUtil::cstring2String(k)].push_back(LowSuctionCodeInfo({ StringUtil::cstring2String(value[i][0].GetString()), value[i][1].IsString()?StringUtil::cstring2String(value[i][1].GetString()):""}));
              }
          }
 
          ((CStatic*)context->GetDlgItem(IDC_STATIC_LS_TIME))->SetWindowTextW(CString(info.time_str.c_str()));
          ((CStatic*)context->GetDlgItem(IDC_STATIC_LS_CODE_NAME))->SetWindowTextW(CString(info.name.c_str()));
          CString plates = L"";
          for (list<string>::iterator e = info.plates.begin();e != info.plates.end();++e) {
              plates.Append(CString((*e).c_str()));
              plates.Append(L"/");
          }
          if (plates.GetLength() > 0) {
              plates = plates.Left(plates.GetLength() - 1);
          }
          ((CStatic*)context->GetDlgItem(IDC_STATIC_LS_PLATES))->SetWindowTextW(plates);
          ((CStatic*)context->GetDlgItem(IDC_STATIC_LS_BIG_ORDERS))->SetWindowTextW(CString(info.bigOrderInfo.c_str()));
          context->lowSuctionQueue = info;
       }
       else {
           context->lowSuctionQueue.id = "";
           ((CStatic*)context->GetDlgItem(IDC_STATIC_LS_TIME))->SetWindowTextW(L"--:--:--");
           ((CStatic*)context->GetDlgItem(IDC_STATIC_LS_CODE_NAME))->SetWindowTextW(L"----");
           ((CStatic*)context->GetDlgItem(IDC_STATIC_LS_PLATES))->SetWindowTextW(L"----");
           ((CStatic*)context->GetDlgItem(IDC_STATIC_LS_BIG_ORDERS))->SetWindowTextW(L"----");
       }
   
   }
}
 
void CFloatTradeDlg::runReadLSPlaceOrderRecords(CFloatTradeDlg* context)
{
    while (TRUE) {
        try {
            requestReadLSPlaceOrderRecords(context);
        }
        catch (...) {
        
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
 
}
 
 
void CFloatTradeDlg::OnBnClickedwantlist()
{
    try {
        // 想买单
        std::list<wstring> wlist = NetworkApi::list_want_buy();
        MessageBox(formatListResult(wlist), TEXT("想买单"), MB_TASKMODAL);
    }
    catch (wstring st) {
        CString cs(st.c_str());
        AfxMessageBox(cs);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedpausebuylist()
{
    try {
        //暂不买单
        std::list<wstring> wlist = NetworkApi::list_pause_buy();
 
        MessageBox(formatListResult(wlist), TEXT("暂不买单"), MB_TASKMODAL);
    }
    catch (wstring st) {
        CString cs(st.c_str());
        AfxMessageBox(cs);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedblacklist()
{
    try {
        // 黑名单
        std::list<wstring> wlist = NetworkApi::list_black();
 
        MessageBox(formatListResult(wlist), TEXT("黑名单"), MB_TASKMODAL);
    }
    catch (wstring st) {
        CString cs(st.c_str());
        AfxMessageBox(cs);
    }
}
 
// 白名单
void CFloatTradeDlg::OnBnClickedwhitelist()
{
    try {
        std::list<wstring> wlist = NetworkApi::list_white();
 
        MessageBox(formatListResult(wlist), TEXT("白名单"), MB_TASKMODAL);
    }
    catch (wstring st) {
        CString cs(st.c_str());
        AfxMessageBox(cs);
    }
}
 
BOOL SendCopyData(HWND hwndTo, const void* pData, DWORD dwDataSize) {
    COPYDATASTRUCT cds;
    cds.dwData = 0;           // 自定义数据标识,可以根据需要设置
    cds.cbData = dwDataSize;  // 数据大小
    cds.lpData = (PVOID)pData;// 数据指针
    // 发送 WM_COPYDATA 消息
    return SendMessage(hwndTo, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&cds);
}
 
 
BOOL CFloatTradeDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
    //this->labelAtrribute.Set
    //将当前页面显示在最前面并获取焦点
 
    BYTE* pBuf = (BYTE*)(pCopyDataStruct->lpData);
    DWORD dwSize = (DWORD)(pCopyDataStruct->cbData);
 
    // 读取数据
    if (pBuf != nullptr && dwSize > 0)
    {
        std::string str(reinterpret_cast<const char*>(pBuf), dwSize);
        wstring wstr = StringUtil::Unescape(str);
        rapidjson::GenericDocument<rapidjson::UTF16<>> root;
        root.Parse(wstr.c_str());
 
        wstring type = root[_T("type")].GetString();
        if (type == _T("set_code")) {
            //this->SetForegroundWindow();
            //this->SetFocus();
            wstring codew = root[_T("data")][_T("code")].GetString();
 
            string code = StringUtil::wstringToString(codew);
            // 设置正在请求的代码
            setTargetCode(code);
 
 
            // 判断是否有下单窗口
            /*if (!buyWin || !Win32Util::isWindowShow(buyWin)) {
                list<HWND> hwndlist = Win32Util::searchWindow("下单");
                for (list<HWND>::iterator el = hwndlist.begin(); el != hwndlist.end(); el++) {
                    HWND hw = *el;
                    if (Win32Util::getWindowName(hw) == "下单" && Win32Util::isWindowShow(hw)) {
                        buyWin = hw;
                        break;
                    }
                }
            }
            if (buyWin) {
                SendCopyData(buyWin, pBuf, dwSize);
            }*/
        }
        else if (type == _T("set_msg")) {
            wstring msg = root[_T("data")][_T("msg")].GetString();
            CString msgc(msg.c_str());
            setMsg(msgc, MSG_TYPE_INFO, FALSE);
        }
    }
    return CDialogEx::OnCopyData(pWnd, pCopyDataStruct);
}
 
 
void CFloatTradeDlg::OnClose()
{
    // 保存位置
 
    // 获取对话框位置
    RECT rect;
    Win32Util::getWindowRect(GetSafeHwnd(), &rect);
    if (rect.left < -3000 || rect.top < -3000) {
        // 错误的坐标
    }
    else {
        ConfigUtil::setWindowPos(rect.left, rect.top);
    }
    CDialogEx::OnClose();
 
}
 
 
 
 
void CFloatTradeDlg::OnBnClickedconfig()
{
    //参数设置
    CSettingDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
 
    fillMoneys();
}
 
 
HBRUSH CFloatTradeDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 
    if (nCtlColor == CTLCOLOR_STATIC) {
        int nID = pWnd->GetDlgCtrlID();
        switch (nID) {
        case label_atrribute:
            pDC->SetTextColor(msgColor);
            break;
        case IDC_STATIC_ALREADY_SELL:
            pDC->SetTextColor(RGB(255, 0, 0));
            break;
        }
    }
    return hbr;
}
 
 
void CFloatTradeDlg::OnBnClickedcodetradeinfo()
{
    CString code;
    editCode.GetWindowText(code);
    SellRuleDlg dlg(code, 0);
    //dlg.Create(IDD_BUY,this);
    //dlg.ShowWindow(SW_SHOW);
    //return;
    INT_PTR nResponse = dlg.DoModal();
}
 
 
void CFloatTradeDlg::OnBnClickedButtonSellVolume1()
{
    int nID = ((CButton*)GetFocus())->GetDlgCtrlID();
    CString num_str;
    GetDlgItem(nID)->GetWindowTextW(num_str);
    editSellMoney.SetWindowTextW(num_str);
 
    try {
        string code = getInputCode();
        int sellMoney_int = stoi(StringUtil::cstring2String(num_str).c_str());
        setLockInfo(code, true, sellMoney_int);
    }
    catch (wstring msg) {
        AfxMessageBox(msg.c_str());
    }
}
 
void CFloatTradeDlg::OnBnClickedButtonRefresh()
{
    try {
        requestCodePosition(getInputCode(), this, TRUE);
    }
    catch (wstring msg) {
        AfxMessageBox(msg.c_str());
    }
 
    std::thread t3(getAutoCancelSellMode, this, getInputCode());
    t3.detach();
 
    std::thread t4(getContinueBuyInfo, this, getInputCode());
    t4.detach();
}
 
 
 
 
void CFloatTradeDlg::OnBnClickedCheckLockSellVolume()
{
    // 锁量, 
    // 判断代码是否填充
    try {
        string code = getInputCode();
        sellManager->init(code);
        if (!checkLockVolume.GetCheck()) {
            setLockInfo(code, false, 0);
            return;
        }
        // 判断量是否输入
        CString sellMoneyw;
        editSellMoney.GetWindowTextW(sellMoneyw);
        string sellMoney = StringUtil::cstring2String(sellMoneyw);
        if (!StringUtil::isNumber(sellMoney)) {
            throw wstring(L"请输入正确的金额");
        }
        int sellMoney_int = stoi(sellMoney);
        setLockInfo(code, true, sellMoney_int);
    }
    catch (wstring msg) {
        AfxMessageBox(msg.c_str());
    }
}
 
 
void CFloatTradeDlg::OnBnClickedButtonAddWant()
{
    try {
        string code = getCode();
        NetworkApi::add_want_buy(code);
        CString msg(code.c_str());
        msg.Append(_T("加入想买单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedButtonRemoveWant()
{
 
    try {
        string code = getCode();
        NetworkApi::remove_want_buy(code);
        CString msg(code.c_str());
        msg.Append(_T("移除想买单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedButtonWantList()
{
 
    try {
        std::list<wstring> wlist = NetworkApi::list_want_buy();
 
        MessageBox(formatListResult(wlist), TEXT("想买单"), MB_TASKMODAL);
    }
    catch (wstring st) {
        CString cs(st.c_str());
        AfxMessageBox(cs);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedButtonGetPositions()
{
    requestAllPositions(this);
    MessageBox(L"拉取成功");
}
 
 
void CFloatTradeDlg::OnDeltaposSpinVolume(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
    try {
        CString st;
        editSellMoney.GetWindowTextW(st);
        int old = stoi(StringUtil::cstring2String(st));
        if (pNMUpDown->iDelta < 0) {
            st = std::to_wstring(old + 1000).c_str();
        }
        else {
            if (old >= 1000)
            {
                st = std::to_wstring(old - 1000).c_str();
            }
            else {
                st = std::to_wstring(0).c_str();
            }
        }
        editSellMoney.SetWindowTextW(st);
    }
    catch (...) {
 
    }
    *pResult = 0;
}
 
 
void CFloatTradeDlg::OnBnClickedCheckLockBuyVolume()
{
    // 锁量, 
    // 判断代码是否填充
    try {
        string code = getInputCode();
        sellManager->init(code);
        if (!checkLockBuyVolume.GetCheck()) {
            SellSetting* sellSetting = sellManager->getSellSetting(code);
            sellSetting->lockBuy = FALSE;
            sellSetting->lockBuyMoney = 0;
            sellManager->syncSettingToFile(code);
            return;
        }
        // 判断量是否输入
        CString buyMoneyw;
        editBuyMoney.GetWindowTextW(buyMoneyw);
        string buyMoney = StringUtil::cstring2String(buyMoneyw);
        if (buyMoney.length() == 0 || !StringUtil::isNumber(buyMoney)) {
            throw wstring(L"请输入正确的金额");
        }
        int buyMoney_int = stoi(buyMoney);
        SellSetting* sellSetting = sellManager->getSellSetting(code);
        sellSetting->lockBuy = TRUE;
        sellSetting->lockBuyMoney = buyMoney_int;
        sellManager->syncSettingToFile(code);
    }
    catch (wstring msg) {
        checkLockBuyVolume.SetCheck(!checkLockBuyVolume.GetCheck());
 
        AfxMessageBox(msg.c_str());
    }
}
 
 
BOOL CFloatTradeDlg::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN) // 按下 Enter 键
    {
        // 关闭窗口
        // EndDialog(IDOK);
        if (CWnd::GetFocus() == GetDlgItem(IDC_EDIT_SELL_VOLUME)) {
            if (checkLockVolume.GetCheck()) {
                // 保存锁定的金额
                try {
                    string code = getInputCode();
                    CString sellMoneyw;
                    editSellMoney.GetWindowTextW(sellMoneyw);
                    string sellMoney = StringUtil::cstring2String(sellMoneyw);
                    if (!StringUtil::isNumber(sellMoney)) {
                        throw wstring(L"请输入正确的金额");
                    }
                    int sellMoney_int = stoi(sellMoney);
                    setLockInfo(code, true, sellMoney_int);
                }
                catch (wstring msg) {
                    AfxMessageBox(msg.c_str());
                }
            }
        }
        else if (CWnd::GetFocus() == GetDlgItem(IDC_EDIT_BUY_VOLUME))
        {
            // 保存锁定的金额
            try {
                string code = getInputCode();
                CString buyMoneyw;
                editBuyMoney.GetWindowTextW(buyMoneyw);
                string buyMoney = StringUtil::cstring2String(buyMoneyw);
                if (!StringUtil::isNumber(buyMoney)) {
                    throw wstring(L"请输入正确的金额");
                }
                int buyMoney_int = stoi(buyMoney);
                setLockBuyInfo(code, true, buyMoney_int);
            }
            catch (wstring msg) {
                AfxMessageBox(msg.c_str());
            }
        }
 
        return TRUE;
    }
 
    return CDialogEx::PreTranslateMessage(pMsg);
}
 
 
void CFloatTradeDlg::OnBnClickedcodetradeupdatewatchbuy1()
{
    // 批量更新填充数据
    try {
        list<SellRule> rules = MyApi::listSellRules();
        if (rules.size() > 0)
        {
            for (list<SellRule>::iterator e = rules.begin(); e != rules.end(); ++e) {
                SellRule rule = *e;
                if (rule.type == 2) {
                    if (rule.buy1_volume > 0) {
                        // 获取买1
                        Buy1Info info = MyApi::getBuy1Info(StringUtil::cstring2String(rule.code));
                        int volume = (info.volume / 100) * 2 / 5;
                        int buy1Volume_int = volume;
                        rapidjson::StringBuffer buf;
                        rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
                        writer.StartObject();
                        writer.Key("id");
                        writer.String(StringUtil::cstring2String(rule.id_).c_str());
                        writer.Key("code");
                        writer.String(StringUtil::cstring2String(rule.code).c_str());
                        writer.Key("buy1_volume");
                        writer.Int(buy1Volume_int * 100);
 
                        writer.Key("buy1_price");
                        writer.String(StringUtil::to_string(info.price).c_str());
 
                        writer.Key("sell_volume");
                        writer.Int(rule.sell_volume);
 
                        writer.Key("sell_price_type");
                        writer.Int(rule.sell_price_type);
 
                        writer.Key("end_time");
                        writer.String(StringUtil::cstring2String(rule.end_time).c_str());
                        writer.EndObject();
                        const char* json_content = buf.GetString();
                        string result = NetworkApi::update_sell_rule(JsonUtil::parseUTF8(json_content));
                        rapidjson::GenericDocument<rapidjson::UTF16<>> doc = JsonUtil::parseUTF16(result);
                        if (doc[L"code"].GetInt() != 0) {
                            throw doc[L"msg"].GetString();
                        }
                    }
                }
            }
            showTips(L"更新完成", MSG_TYPE_INFO, 1000);
        }
        else {
            throw CString(L"没有板上盯数据");
        }
    }
 
 
 
 
    catch (CString st) {
        AfxMessageBox(st);
    }
    catch (wstring st) {
        AfxMessageBox(CString(st.c_str()));
    }
 
}
 
 
 
void CFloatTradeDlg::OnBnClickedButtonAddGreen()
{
    try {
        string code = getCode();
        NetworkApi::add_green_buy(code);
        CString msg(code.c_str());
        msg.Append(_T("加入绿名单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedButtonRemoveGreen()
{
    // 移除绿名单
    try {
        string code = getCode();
        NetworkApi::remove_green_buy(code);
        CString msg(code.c_str());
        msg.Append(_T("移除绿名单成功"));
        setMsg(msg, MSG_TYPE_SUCCESS);
    }
    catch (wstring st) {
        CString msg(st.c_str());
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedButtonGreenList()
{
    try {
        std::list<wstring> wlist = NetworkApi::list_green_buy();
        MessageBox(formatListResult(wlist), TEXT("绿名单"), MB_TASKMODAL);
    }
    catch (wstring st) {
        CString cs(st.c_str());
        AfxMessageBox(cs);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedaddwatchlimitupforsell()
{
    try {
        string code = getCode();
        // 获取板上盯列表
        // 批量更新填充数据
        CodePosition* codePosition = sellManager->getCodePosition(code);
        if (codePosition == nullptr) {
            throw CString(L"当前代码无持仓");
        }
 
        if (codePosition->available <= 0) {
            throw CString(L"当前代码无剩余持仓");
        }
 
        list<SellRule> rules = MyApi::listSellRules();
        if (rules.size() > 0)
        {
            for (list<SellRule>::iterator e = rules.begin(); e != rules.end(); ++e) {
                SellRule rule = *e;
                if (rule.excuted) {
                    continue;
                }
                if (rule.type == 2) {
                    if (rule.buy1_volume > 0) {
                        if (code._Equal(StringUtil::cstring2String(rule.code))) {
                            throw CString(L"已存在板上盯");
                            return;
                        }
                    }
                }
            }
        }
        // 获取买1
        Buy1Info info = MyApi::getBuy1Info(code);
        int volume = (info.volume / 100) * 2 / 5;
        int buy1Volume_int = volume;
        rapidjson::StringBuffer buf;
        rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
        writer.StartObject();
        writer.Key("code");
        writer.String(code.c_str());
        writer.Key("buy1_volume");
        writer.Int(buy1Volume_int * 100);
 
        writer.Key("buy1_price");
        writer.String(StringUtil::to_string(info.price).c_str());
 
        writer.Key("sell_volume");
        writer.Int(codePosition->available);
 
        writer.Key("sell_price_type");
        writer.Int(0);
 
        writer.Key("end_time");
        writer.String("14:56:56");
 
        writer.Key("type");
        writer.Int(2);
 
        writer.EndObject();
        const char* json_content = buf.GetString();
        string result = NetworkApi::add_sell_rule(JsonUtil::parseUTF8(json_content));
        rapidjson::GenericDocument<rapidjson::UTF16<>> doc = JsonUtil::parseUTF16(result);
        if (doc[L"code"].GetInt() != 0) {
            throw doc[L"msg"].GetString();
        }
        MessageBox(L"添加板上盯完成", L"提示");
    }
    catch (CString msg) {
        setMsg(msg, MSG_TYPE_FAIL);
    }
    catch (wstring msg) {
        setMsg(msg.c_str(), MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    RECT rect;
    labelTotalPosition.GetWindowRect(&rect);
    ScreenToClient(&rect);
    if (point.x > rect.left && point.x < rect.right && point.y > rect.top && point.y < rect.bottom) {
        if (positionDialog == nullptr) {
            positionDialog = new PositionDlg;
            positionDialog->Create(IDD_DIALOG_POSITION, this);
        }
        positionDialog->ShowWindow(SW_SHOW);
    }
    ((CStatic*) GetDlgItem(IDC_STATIC_LS_PLATES))->GetWindowRect(&rect);
    ScreenToClient(&rect);
    if (point.x > rect.left && point.x < rect.right && point.y > rect.top && point.y < rect.bottom) {
        // 点击低吸的板块
        if (lowSuctionQueue.id.size() <= 0) {
            return;
        }
        map<string, list<LowSuctionCodeInfo>> plateLimitUpCodes = lowSuctionQueue.plateLimitUpCodes;
        for (map<string, list<LowSuctionCodeInfo>>::iterator e = plateLimitUpCodes.begin();e != plateLimitUpCodes.end();++e) {
            string title = (*e).first;
            list<LowSuctionCodeInfo> limitUpList = (*e).second;
            CString msg = L"";
            for (list<LowSuctionCodeInfo>::iterator ee = limitUpList.begin();ee != limitUpList.end();++ee) {
                msg.Append(CString((*ee).code.c_str()));
                msg.Append(L"【");
                msg.Append(CString((*ee).name.c_str()));
                msg.Append(L"】\n");
            }
            MessageBox(msg, CString(title.c_str()));
        }
 
        
    }
 
    ((CStatic*)GetDlgItem(IDC_STATIC_LS_CODE_NAME))->GetWindowRect(&rect);
    ScreenToClient(&rect);
    if (point.x > rect.left && point.x < rect.right && point.y > rect.top && point.y < rect.bottom) {
        // 点击低吸的板块
        if (lowSuctionQueue.id.size() <= 0) {
            return;
        }
        // 添加到同花顺
        try {
            Win32Util::addToTHS(lowSuctionQueue.code);
        }
        catch (CString msg) {
        }
        catch (...) {
        }
 
    }
 
 
 
 
    CDialogEx::OnLButtonDown(nFlags, point);
 
 
 
}
 
 
void CFloatTradeDlg::OnClickedCheckOcrCode()
{
    if (((CButton*)GetDlgItem(check_ocr_code))->GetCheck()) {
        canOCRCode = true;
        this->hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, 0, 0);
    }
    else {
        canOCRCode = false;
        UnhookWindowsHookEx(this->hMouseHook);
    }
    ConfigUtil::setEnableOCRCode(canOCRCode);
}
 
 
void CFloatTradeDlg::OnDestroy()
{
    CDialogEx::OnDestroy();
 
    if (thsCodeOCR != nullptr) {
        delete thsCodeOCR;
    }
}
 
 
void CFloatTradeDlg::OnStnClickedStaticTotalPosition()
{
 
}
 
 
void CFloatTradeDlg::OnStnClickedStaticLsCodeName()
{
    // TODO: 在此添加控件通知处理程序代码
}
 
 
void CFloatTradeDlg::OnBnClickedButtonLsAddForbidden()
{
    try {
        OnBnClickedButtonLsNotBuy();
        if (lowSuctionQueue.id.size() <= 0) {
            throw CString(L"没有待下单数据");
            return;
        }
        string result = LowSuctionNetworkApi::add_black_list(lowSuctionQueue.code);
        auto doc = JsonUtil::parseUTF16(result);
        if (!doc.IsObject()) {
            throw CString(L"网络请求出错");
        }
        if (doc[L"code"].GetInt() != 0) {
            throw CString(doc[L"msg"].GetString());
        }
        setMsg(L"低吸拉黑代码成功", MSG_TYPE_SUCCESS);
    
    }
    catch (CString msg) {
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedButtonLsBuy()
{
    try {
        if (lowSuctionQueue.id.size() <= 0) {
            throw CString(L"没有待下单数据");
            return;
        }
        string result = LowSuctionNetworkApi::set_buy(lowSuctionQueue.id);
        auto doc = JsonUtil::parseUTF16(result);
        if (!doc.IsObject()) {
            throw CString(L"网络请求出错");
        }
        if (doc[L"code"].GetInt() != 0) {
            throw CString(doc[L"msg"].GetString());
        }
        setMsg(L"确认下单成功", MSG_TYPE_SUCCESS);
        requestReadLSPlaceOrderRecords(this);
    }
    catch (CString msg) {
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnBnClickedButtonLsNotBuy()
{
    try {
        if (lowSuctionQueue.id.size() <= 0) {
            throw CString(L"没有待下单数据");
            return;
        }
        string result = LowSuctionNetworkApi::set_not_buy(lowSuctionQueue.id);
        auto doc = JsonUtil::parseUTF16(result);
        if (!doc.IsObject()) {
            throw CString(L"网络请求出错");
        }
        if (doc[L"code"].GetInt() != 0) {
            throw CString(doc[L"msg"].GetString());
        }
        setMsg(L"确认不下单成功", MSG_TYPE_SUCCESS);
        requestReadLSPlaceOrderRecords(this);
    }
    catch (CString msg) {
        setMsg(msg, MSG_TYPE_FAIL);
    }
}
 
 
void CFloatTradeDlg::OnStnClickedStaticLsPlates()
{
    
 
 
 
 
 
 
 
}
 
void CFloatTradeDlg::OnCbnSelchangeComboContinueBuyList()
{
    bool check = ((CButton*)GetDlgItem(IDC_CHECK_CONTINUE_BUY))->GetCheck();
    if (!check) {
        return;
    }
 
    OnBnClickedContinueBuy();
}
 
void CFloatTradeDlg::OnBnClickedContinueBuy()
{
    auto checkControl = (CButton*)GetDlgItem(IDC_CHECK_CONTINUE_BUY);
    string code = "";
    try {
        code = getInputCode();
    }
    catch (wstring msg){
        setMsg(msg.c_str(), MSG_TYPE_FAIL);
        checkControl->SetCheck(!checkControl->GetCheck());
        return;
    }
    try {
      bool check = ((CButton*)GetDlgItem(IDC_CHECK_CONTINUE_BUY))->GetCheck();
      if (check) {
          CComboBox* comboMoneyList = (CComboBox*)GetDlgItem(IDC_COMBO_CONTINUE_BUY_MONEY);
          int index = comboMoneyList->GetCurSel();
          if (index < 0) {
              checkControl->SetCheck(!checkControl->GetCheck());
            throw wstring(L"请选择金额");
          }
          CString strText;
          comboMoneyList->GetLBText(index, strText);
          int money = _ttoi(strText);
          auto result = NetworkApi::set_continue_buy_money(code, money);
          auto doc = JsonUtil::parseUTF16(result);
          if (!doc.IsObject()) {
              checkControl->SetCheck(!checkControl->GetCheck());
              throw CString(L"网络请求出错");
          }
          if (doc[L"code"].GetInt() != 0) {
              checkControl->SetCheck(!checkControl->GetCheck());
              throw CString(doc[L"msg"].GetString());
          }
          setMsg(L"续买金额设置成功", MSG_TYPE_SUCCESS);
      }
      else {
          int money = 0;
          auto result = NetworkApi::set_continue_buy_money(code, money);
          auto doc = JsonUtil::parseUTF16(result);
          if (!doc.IsObject()) {
              checkControl->SetCheck(!checkControl->GetCheck());
              throw CString(L"网络请求出错");
          }
          if (doc[L"code"].GetInt() != 0) {
              checkControl->SetCheck(!checkControl->GetCheck());
              throw CString(doc[L"msg"].GetString());
          }
          setMsg(L"续买金额取消成功", MSG_TYPE_SUCCESS);
      }
 
    }
    catch (wstring msg) {
        setMsg(msg.c_str(), MSG_TYPE_FAIL);
    }
 
}