1
zj
2 days ago 5e57de9b12ee136e45ce5754c7fe2e7eb12af05a
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
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.item.domain.Item;
import com.yami.trading.bean.purchasing.dto.ExchangeLock;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.ThreadUtils;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.exchange.ExchangeApplyOrderService;
import com.yami.trading.service.item.ItemPreMarketService;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.syspara.SysparaService;
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 ExchangeApplyOrderHandleJob implements Runnable {
    @Autowired
    ExchangeApplyOrderService exchangeApplyOrderService;
    @Autowired
    DataService dataService;
    @Autowired
    ItemService itemService;
    @Autowired
    ItemPreMarketService itemPreMarketService;
    @Autowired
    SysparaService sysparaService;
 
    public void start() {
        new Thread(this, "ExchangeApplyOrderHandleJob").start();
        log.info("现货交易委托单处理线程启动!");
    }
 
    @Override
    public void run() {
        // 系统启动先暂停30秒
        ThreadUtils.sleep(1000 * 30);
        while (true) {
            try {
                List<ExchangeApplyOrder> list = this.exchangeApplyOrderService.findSubmitted();
                for (int i = 0; i < list.size(); i++) {
                    ExchangeApplyOrder order = list.get(i);
                    Item bySymbol = itemService.findBySymbol(order.getSymbol());
                    if (bySymbol == null) {
                        throw new YamiShopBindException("当前币对不存在");
                    }
                    boolean isOpen = itemService.isOpen(order.getSymbol());
                    if (!isOpen) {
                        continue;
                    }
                    Realtime realtime = this.dataService.realtime(order.getSymbol()).get(0);
                    Double fillPrice = realtime.getClose();
                    boolean preMarketActive = itemPreMarketService != null
                            && itemPreMarketService.isPreMarketTradingActive(order.getSymbol());
                    if (preMarketActive) {
                        Double prePrice = itemPreMarketService.getActivePreMarketPrice(order.getSymbol());
                        if (prePrice != null) {
                            fillPrice = prePrice;
                        }
                    }
                    // 限价单
                    if (ExchangeApplyOrder.ORDER_PRICE_TYPE_LIMIT.equals(order.getOrderPriceType())) {
                        if (preMarketActive) {
                            this.handle(order, fillPrice);
                        } else if (ExchangeApplyOrder.OFFSET_OPEN.equals(order.getOffset())) {
                            if (realtime.getClose() <= order.getPrice()) {
                                this.handle(order, realtime.getClose());
                            }
                        } else {
                            if (realtime.getClose() >= order.getPrice()) {
                                this.handle(order, realtime.getClose());
                            }
                        }
                    }
                    // 市价单
                    else {
                        this.handle(order, fillPrice);
                    }
                }
            } catch (Exception e) {
                log.error("run fail", e);
            } finally {
                ThreadUtils.sleep(1000 * 1);
            }
        }
    }
 
    public void handle(ExchangeApplyOrder applyOrder, Double orderPrice) {
        boolean lock = false;
        try {
            if (!ExchangeLock.add(applyOrder.getOrderNo())) {
                return;
            }
            lock = true;
            if (ExchangeApplyOrder.OFFSET_OPEN.equals(applyOrder.getOffset())) {
                // 现货交易订单
                if (null == applyOrder.getRelationOrderNo()) {
                    this.exchangeApplyOrderService.saveSpotTradOpenCreated(applyOrder, orderPrice);
                }
                // 闪兑订单
                else {
                    this.exchangeApplyOrderService.saveOpenCreated(applyOrder, orderPrice);
                }
            } else if (ExchangeApplyOrder.OFFSET_CLOSE.equals(applyOrder.getOffset())) {
                // 现货交易订单
                if (null == applyOrder.getRelationOrderNo()) {
                    this.exchangeApplyOrderService.saveSpotTradCloseCreated(applyOrder, orderPrice);
                }
                // 闪兑订单
                else {
                    this.exchangeApplyOrderService.saveCloseCreated(applyOrder, orderPrice);
                }
            }
        } catch (Exception e) {
            log.error("error:", e);
        } finally {
            if (lock) {
                ThreadUtils.sleep(100);
                ExchangeLock.remove(applyOrder.getOrderNo());
            }
        }
    }
 
}