1
zj
2025-06-23 dc9bd22833255bc602dd42c7f603ecb50842ab35
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
package project.contract.job;
 
import java.util.List;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import kernel.util.ThreadUtils;
import project.contract.ContractApplyOrder;
import project.contract.ContractApplyOrderService;
import project.contract.ContractLock;
import project.contract.ContractOrder;
import project.contract.ContractOrderService;
import project.data.DataService;
import project.data.model.Realtime;
 
/**
 * 
 * 委托单进入市场
 */
public class ContractApplyOrderHandleJob implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(ContractApplyOrderHandleJob.class);
    private ContractOrderService contractOrderService;
    private ContractApplyOrderService contractApplyOrderService;
    private DataService dataService;
 
    public void run() {
        /*
         * 系统启动先暂停30秒
         */
        ThreadUtils.sleep(1000 * 30);
        while (true)
            try {
                List<ContractApplyOrder> list = this.contractApplyOrderService.findSubmitted();
                for (int i = 0; i < list.size(); i++) {
                    ContractApplyOrder order = list.get(i);
                    List<Realtime> realtime_list = this.dataService.realtime(order.getSymbol());
                    Realtime realtime = null;
                    if (realtime_list.size() > 0) {
                        realtime = realtime_list.get(0);
                    } else {
                        continue;
                    }
 
                    if ("limit".equals(order.getOrder_price_type())) {
                        /**
                         * 限价单
                         */
                        if ("buy".equals(order.getDirection())) {
                            /**
                             * 买涨
                             */
                            if (realtime.getClose() <= order.getPrice()) {
 
                                this.handle(order, realtime);
 
                            }
 
                        } else {
                            /**
                             * 买跌
                             */
                            if (realtime.getClose() >= order.getPrice()) {
                                this.handle(order, realtime);
                            }
 
                        }
 
                    } else {
                        /**
                         * 非限制,直接进入市 场
                         */
                        this.handle(order, realtime);
                    }
 
                }
 
            } catch (Exception e) {
                logger.error("run fail", e);
            } finally {
                ThreadUtils.sleep(1000 * 1);
            }
 
    }
 
    public void handle(ContractApplyOrder applyOrder, Realtime realtime) {
        boolean lock = false;
        try {
            if (!ContractLock.add(applyOrder.getOrder_no())) {
                return;
            }
            lock = true;
            if ("open".equals(applyOrder.getOffset())) {
                this.contractOrderService.saveOpen(applyOrder, realtime);
            } else if ("close".equals(applyOrder.getOffset())) {
 
                /**
                 * 平仓
                 */
                List<ContractOrder> list = this.contractOrderService.findSubmitted(applyOrder.getPartyId().toString(),
                        applyOrder.getSymbol(), applyOrder.getDirection());
                if (list.size() == 0) {
                    applyOrder.setVolume(0D);
                    applyOrder.setState(ContractApplyOrder.STATE_CREATED);
                    this.contractApplyOrderService.update(applyOrder);
                }
                for (int i = 0; i < list.size(); i++) {
                    ContractOrder order = list.get(i);
                    boolean lock_order = false;
                    try {
                        if (!ContractLock.add(order.getOrder_no())) {
                            continue;
                        }
                        lock_order = true;
                        applyOrder = this.contractOrderService.saveClose(applyOrder, realtime, order.getOrder_no());
 
                        if (ContractApplyOrder.STATE_CREATED.equals(applyOrder.getState())) {
                            break;
                        }
                    } catch (Exception e) {
                        logger.error("error:", e);
                    } finally {
                        if (lock_order) {
                            ThreadUtils.sleep(100);
                            ContractLock.remove(order.getOrder_no());
                        }
 
                    }
 
                }
 
            }
 
        } catch (Exception e) {
            logger.error("error:", e);
        } finally {
            if (lock) {
                ThreadUtils.sleep(100);
                ContractLock.remove(applyOrder.getOrder_no());
            }
 
        }
 
    }
    public void start(){
        new Thread(this, "ContractApplyOrderHandleJob").start();
        if (logger.isInfoEnabled())
            logger.info("委托单处理线程启动!");
    }
 
    public void setContractOrderService(ContractOrderService contractOrderService) {
        this.contractOrderService = contractOrderService;
    }
 
    public void setContractApplyOrderService(ContractApplyOrderService contractApplyOrderService) {
        this.contractApplyOrderService = contractApplyOrderService;
    }
 
    public void setDataService(DataService dataService) {
        this.dataService = dataService;
    }
 
}