zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
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
package project.futures.internal;
 
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.jdbc.core.JdbcTemplate;
 
import kernel.bo.RecordObjectMapper;
import kernel.exception.BusinessException;
import kernel.util.DateUtils;
import kernel.util.StringUtils;
import project.futures.FuturesOrder;
import project.futures.FuturesOrderLocalService;
import project.futures.FuturesPara;
import project.futures.FuturesParaService;
import project.futures.FuturesRedisKeys;
import project.item.ItemService;
import project.item.model.Item;
import project.redis.RedisHandler;
 
public class FuturesOrderLocalServiceImpl implements FuturesOrderLocalService {
    
    protected JdbcTemplate jdbcTemplate;
    protected ItemService itemService;
    protected RedisHandler redisHandler;
    protected FuturesParaService futuresParaService;
 
    public FuturesOrder cacheByOrderNo(String order_no) {
        FuturesOrder futuresOrder = (FuturesOrder) redisHandler
                .get(FuturesRedisKeys.FUTURES_SUBMITTED_ORDERNO + order_no);
//        FuturesOrder futuresOrder = cache.get(order_no);
        if (null == futuresOrder) {
            futuresOrder = findByOrderNo(order_no);
        }
        return futuresOrder;
    }
 
    public FuturesOrder findByOrderNo(String order_no) {
        List<FuturesOrder> list = jdbcTemplate.query("SELECT * FROM T_FUTURES_ORDER WHERE ORDER_NO=?", 
                RecordObjectMapper.newInstance(FuturesOrder.class), order_no);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
    public Map<String, Object> bulidOne(FuturesOrder order) {
//        FuturesOrder order_cache = cache.get(order.getOrder_no());
        FuturesOrder order_cache = (FuturesOrder) redisHandler
                .get(FuturesRedisKeys.FUTURES_SUBMITTED_ORDERNO + order.getOrder_no());
        if (order_cache != null) {
            order = order_cache;
        }
 
        List<FuturesPara> paras = futuresParaService.cacheGetBySymbol(order.getSymbol());
        double ratio_min = 0D;
        double ratio_max = 0D;
        for (int i = 0; i < paras.size(); i++) {
            if (paras.get(i).getTimeUnit().equals(order.getTimeUnit())
                    && paras.get(i).getTimeNum() == order.getTimeNum()) {
                ratio_min = paras.get(i).getProfit_ratio();
                ratio_max = paras.get(i).getProfit_ratio_max();
            }
        }
 
        Item item = this.itemService.cacheBySymbol(order.getSymbol(), false);
        if (item == null) {
            throw new BusinessException("参数错误");
        }
        String decimals = "#.";
 
        for (int i = 0; i < item.getDecimals(); i++) {
            decimals = decimals + "#";
        }
        if (item.getDecimals() == 0) {
            decimals = "#";
        }
        DecimalFormat df_symbol = new DecimalFormat(decimals);
        df_symbol.setRoundingMode(RoundingMode.FLOOR);// 向下取整
 
        DecimalFormat df = new DecimalFormat("#.##");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("order_no", order.getOrder_no());
        map.put("name", item.getName());
        map.put("symbol", order.getSymbol());
        map.put("open_time", DateUtils.format(order.getCreate_time(), DateUtils.DF_yyyyMMddHHmmss));
        if (order.getClose_time() != null) {
            map.put("close_time", DateUtils.format(order.getClose_time(), DateUtils.DF_yyyyMMddHHmmss));
        } else {
            map.put("close_time", "--");
        }
 
        map.put("direction", order.getDirection());
        map.put("open_price", df_symbol.format(order.getTrade_avg_price()));
        map.put("state", order.getState());
        map.put("amount", order.getVolume());
        map.put("fee", order.getFee());
        /**
         * 收益
         */
        if (order.getProfit() > 0) {
//            if ("submitted".equals(order.getState())) {
//                map.put("profit", df.format(Arith.mul(order.getVolume(), ratio_min)) + " " + "~ "
//                        + df.format(Arith.mul(order.getVolume(), ratio_max)));
//            } else {
                map.put("profit", df.format(order.getProfit()));
//            }
            map.put("profit_state", "1");
 
        } else {
            map.put("profit", df.format(order.getProfit()));
            map.put("profit_state", "0");
        }
 
        map.put("volume", order.getVolume());
 
        map.put("settlement_time", DateUtils.format(order.getSettlement_time(), DateUtils.DF_yyyyMMddHHmmss));// 交割时间
        map.put("close_price", df_symbol.format(order.getClose_avg_price()));
        map.put("remain_time", StringUtils.isEmptyString(order.getRemain_time()) ? "0:0:0" : order.getRemain_time());
        map.put("time_num", order.getTimeNum());
        map.put("time_unit", order.getTimeUnit().substring(0, 1));
        return map;
    }
 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    public void setItemService(ItemService itemService) {
        this.itemService = itemService;
    }
 
    public void setRedisHandler(RedisHandler redisHandler) {
        this.redisHandler = redisHandler;
    }
 
    public void setFuturesParaService(FuturesParaService futuresParaService) {
        this.futuresParaService = futuresParaService;
    }
 
}