1
zj
2024-06-13 a4662cc65a02f258062bf6cc392ceb1017db9292
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
package com.yami.trading.service.exchange.job;
 
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.exchange.ExchangeApplyOrder;
import com.yami.trading.bean.exchange.ExchangeLeverApplyOrder;
import com.yami.trading.bean.purchasing.dto.ExchangeLeverLock;
import com.yami.trading.common.util.ThreadUtils;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.exchange.ExchangeLeverApplyOrderService;
import com.yami.trading.service.exchange.ExchangeLeverOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
/**
 *
 * 委托单进入市场
 */
 
@Slf4j
@Component
public class ExchangeLeverApplyOrderHandleJob implements Runnable {
    @Autowired
    ExchangeLeverOrderService exchangeLeverOrderService;
    @Autowired
    ExchangeLeverApplyOrderService exchangeLeverApplyOrderService;
    @Autowired
    private DataService dataService;
 
    public void run() {
        /*
         * 系统启动先暂停30秒
         */
        ThreadUtils.sleep(1000 * 30);
        while (true)
            try {
                List<ExchangeLeverApplyOrder> list =exchangeLeverApplyOrderService.findSubmitted();
                for (int i = 0; i < list.size(); i++) {
                    ExchangeLeverApplyOrder 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) {
                log.error("run fail", e);
            } finally {
                ThreadUtils.sleep(1000 * 1);
            }
    }
 
    public void handle(ExchangeLeverApplyOrder applyOrder, Realtime realtime) {
        boolean lock = false;
        try {
            if (!ExchangeLeverLock.add(applyOrder.getOrderNo())) {
                return;
            }
            lock = true;
            if (ExchangeApplyOrder.OFFSET_OPEN.equals(applyOrder.getOffset())) {
              exchangeLeverApplyOrderService.saveOpen(applyOrder, realtime);
            } else if (ExchangeApplyOrder.OFFSET_CLOSE.equals(applyOrder.getOffset())) {
                exchangeLeverApplyOrderService.saveCloseSymbol(applyOrder);
            }
        } catch (Exception e) {
            log.error("error:", e);
        } finally {
            if (lock) {
                ThreadUtils.sleep(100);
                ExchangeLeverLock.remove(applyOrder.getOrderNo());
            }
        }
    }
 
    public void start() {
        new Thread(this, "ExchangeLeverApplyOrderHandleJob").start();
        if (log.isInfoEnabled())
            log.info("币币交易杠杆委托单处理线程启动!");
    }
}