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
110
111
112
113
114
115
116
117
118
119
120
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.MarketOpenChecker;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.exchange.ExchangeApplyOrderService;
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
    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 = MarketOpenChecker.isMarketOpenByItemCloseType(bySymbol.getOpenCloseType());
                    if (!isOpen) {
                        continue;
                    }
                    Realtime realtime = this.dataService.realtime(order.getSymbol()).get(0);
                    // 限价单
                    if (ExchangeApplyOrder.ORDER_PRICE_TYPE_LIMIT.equals(order.getOrderPriceType())) {
                        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, realtime.getClose());
                    }
                }
            } 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());
            }
        }
    }
 
}