gao
2020-06-30 e7ce8d314ebd415aa8bf8d93f73aaac035f9858d
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
package com.xcong.excoin.rabbit.init;
 
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.common.enumerates.RabbitPriceTypeEnum;
import com.xcong.excoin.modules.contract.dao.ContractEntrustOrderDao;
import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao;
import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity;
import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity;
import com.xcong.excoin.rabbit.pricequeue.OrderModel;
import com.xcong.excoin.rabbit.producer.OrderProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
 
/**
 * 后台开启 APP不开启
 *
 * @author helius
 */
@Slf4j
@Component
@ConditionalOnProperty(prefix = "app", name = "newest-price-update-job", havingValue = "true")
public class OrderProducerInit {
 
    @Resource
    private ContractEntrustOrderDao contractEntrustOrderDao;
 
    @Resource
    private ContractHoldOrderDao contractHoldOrderDao;
 
    @Resource
    private OrderProducer producer;
 
    @PostConstruct
    public void initOrder() {
        log.info("=======初始化未完成订单信息=======");
 
        // 查询所有未平仓的单
        List<ContractHoldOrderEntity> holdOrderEntities = contractHoldOrderDao.selectAllHoldOrder();
        // 查询所有未完成的委托单
        List<ContractEntrustOrderEntity> entrustOrderEntities = contractEntrustOrderDao.selectAllEntrustOrder();
 
        if (CollectionUtils.isNotEmpty(holdOrderEntities)) {
            for (ContractHoldOrderEntity order : holdOrderEntities) {
                // 开多1,开空 2
                int openingType = order.getOpeningType();
                // 1:买入委托2:开多3:开空4:平多5:平空6:爆仓平多7:爆仓平空
                // 9:止盈平多10:止盈平空11:止损平多12:止损平空
                if (ContractHoldOrderEntity.OPENING_TYPE_MORE == openingType) {
                    // 开多 发送开多止损 止盈 爆仓
                    // 爆仓价
                    BigDecimal forceSetPrice = order.getForceClosingPrice();
                    if (forceSetPrice != null) {
                        OrderModel model = new OrderModel(order.getId(), RabbitPriceTypeEnum.CLOSE_MORE_BOMB.getValue(), forceSetPrice.toPlainString(),
                                order.getSymbol(), order.getOperateNo());
                        producer.sendPriceOperate(JSONObject.toJSONString(model));
                    }
                    // 止损
                    BigDecimal stopLossPrice = order.getStopLossPrice();
                    if (stopLossPrice != null && stopLossPrice.compareTo(BigDecimal.ZERO) > 0) {
                        OrderModel model = new OrderModel(order.getId(), RabbitPriceTypeEnum.CLOSE_MORE_STOP_LESS.getValue(),
                                stopLossPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(),
                                order.getSymbol());
                        producer.sendPriceOperate(JSONObject.toJSONString(model));
                    }
                    // 止盈
                    BigDecimal stopProfitPrice = order.getStopProfitPrice();
                    if (stopProfitPrice != null && stopProfitPrice.compareTo(BigDecimal.ZERO) > 0) {
                        OrderModel model = new OrderModel(order.getId(), RabbitPriceTypeEnum.CLOSE_MORE_STOP_PROFIT.getValue(),
                                stopProfitPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(),
                                order.getSymbol());
                        producer.sendPriceOperate(JSONObject.toJSONString(model));
                    }
 
                } else {
                    // 开空 发送开空止损 止盈 爆仓
                    // 爆仓价
                    BigDecimal forceSetPrice = order.getForceClosingPrice();
                    if (forceSetPrice != null) {
                        OrderModel model = new OrderModel(order.getId(), RabbitPriceTypeEnum.CLOSE_LESS_BOMB.getValue(), forceSetPrice.toPlainString(),
                                order.getSymbol(), order.getOperateNo());
                        producer.sendPriceOperate(JSONObject.toJSONString(model));
                    }
                    // 止损
                    BigDecimal stopLossPrice = order.getStopLossPrice();
                    if (stopLossPrice != null && stopLossPrice.compareTo(BigDecimal.ZERO) > 0) {
                        OrderModel model = new OrderModel(order.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_LESS.getValue(),
                                stopLossPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(),
                                order.getSymbol());
                        producer.sendPriceOperate(JSONObject.toJSONString(model));
                    }
                    // 止盈
                    BigDecimal stopProfitPrice = order.getStopProfitPrice();
                    if (stopProfitPrice != null && stopProfitPrice.compareTo(BigDecimal.ZERO) > 0) {
                        OrderModel model = new OrderModel(order.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_PROFIT.getValue(),
                                stopProfitPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(),
                                order.getSymbol());
                        producer.sendPriceOperate(JSONObject.toJSONString(model));
                    }
                }
 
            }
        }
 
        if (CollectionUtils.isNotEmpty(entrustOrderEntities)) {
            for (ContractEntrustOrderEntity order : entrustOrderEntities) {
                // 开多1,开空 2
                int entrustType = order.getEntrustType();
                // 开多
                BigDecimal entrustPrice = order.getEntrustPrice();
                OrderModel model;
                if (ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_MORE == entrustType) {
                    // 开多委托
                    model = new OrderModel(order.getId(), RabbitPriceTypeEnum.ENTRUST_OPEN_MORE.getValue(),
                            entrustPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(),
                            order.getSymbol());
 
                } else {
                    model = new OrderModel(order.getId(), RabbitPriceTypeEnum.ENTRUST_OPEN_LESS.getValue(),
                            entrustPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(),
                            order.getSymbol());
                }
                producer.sendPriceOperate(JSONObject.toJSONString(model));
            }
        }
    }
}