1
zj
2024-07-12 bc9801d6adf582325e8213df11f597f82deda973
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package project.project.web.api;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.servlet.http.HttpServletRequest;
 
import kernel.web.BaseAction;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import kernel.exception.BusinessException;
import kernel.sessiontoken.SessionTokenService;
import kernel.util.StringUtils;
import kernel.util.ThreadUtils;
import kernel.web.ApplicationUtil;
import kernel.web.ResultObject;
import project.data.DataService;
import project.data.model.Realtime;
import project.futures.FuturesLock;
import project.futures.FuturesOrder;
import project.futures.FuturesOrderLocalService;
import project.futures.FuturesOrderService;
import project.futures.FuturesPara;
import project.futures.FuturesParaService;
import project.party.PartyService;
import project.party.model.Party;
import project.wallet.Wallet;
import project.wallet.WalletService;
 
/**
 * 交割合约订单
 */
@RestController
@CrossOrigin
public class  FuturesOrderController extends BaseAction {
    @Autowired
    private WalletService walletService;
    @Autowired
    private PartyService partyService;
    @Autowired
    private FuturesOrderService futuresOrderService;
    @Autowired
    private FuturesOrderLocalService futuresOrderLocalService;
    @Autowired
    private FuturesParaService futuresParaService;
    @Autowired
    private SessionTokenService sessionTokenService;
    @Autowired
    private DataService dataService;
 
    private final String action = "/api/futuresOrder!";
    
 
    /**
     * 开仓页面参数
     * 
     * symbol 币种
     */
    @RequestMapping(action + "openview.action")
    public Object openview(HttpServletRequest request) throws IOException {
        String symbol = request.getParameter("symbol");
 
        ResultObject resultObject = new ResultObject();
 
        try {
 
            Map<String, Object> data = new HashMap<String, Object>();
 
            List<Map> futuresPara = new ArrayList<Map>();
            for (FuturesPara obj : this.futuresParaService.cacheGetBySymbolSort(symbol)) {
                FuturesPara para = new FuturesPara();
                BeanUtils.copyProperties(obj, para);
                futuresPara.add(this.futuresParaService.bulidOne(para));
            }
            data.put("para", futuresPara);
 
            String partyId = this.getLoginPartyId();
            if (!StringUtils.isNullOrEmpty(partyId) && futuresPara != null) {
                Wallet wallet = this.walletService.saveWalletByPartyId(this.getLoginPartyId());
 
                // 账户剩余资金
                //DecimalFormat df = new DecimalFormat("#");
                int money = (int)wallet.getMoney();
                data.put("amount", money);
 
                String session_token = this.sessionTokenService.savePut(partyId);
                data.put("session_token", session_token);                
            } else {
                data.put("amount", 0);
            }
 
            resultObject.setData(data);
 
        } catch (BusinessException e) {
            resultObject.setCode("1");
            resultObject.setMsg(e.getMessage());
        } catch (Throwable t) {
            resultObject.setCode("1");
            resultObject.setMsg("参数错误");
        }
 
        return resultObject;
    }
 
    /**
     * 开仓
     * 
     * symbol 币种
     * direction "buy":多 "sell":空
     * amount 委托数量(张)
     * para_id 交割合约参数
     */
    @RequestMapping(action + "open.action")
    public Object open(HttpServletRequest request) throws IOException {
        String session_token = request.getParameter("session_token");
        String symbol = request.getParameter("symbol");
        String direction = request.getParameter("direction");
        String amount = request.getParameter("amount");
        String para_id = request.getParameter("para_id");
 
        ResultObject resultObject = new ResultObject();
        resultObject = this.readSecurityContextFromSession(resultObject);
        if (!"0".equals(resultObject.getCode())) {
            return resultObject;
        }
        
        String partyId = this.getLoginPartyId();
        boolean lock = false;
        
        try {
            
            Map<String, String> data = new HashMap<String, String>();
            
            if (!FuturesLock.add(partyId)) {
                throw new BusinessException("请稍后再试");
            }
            
            lock = true;
 
            Object object = this.sessionTokenService.cacheGet(session_token);
            this.sessionTokenService.del(session_token);
            if (null == object || !this.getLoginPartyId().equals((String) object)) {
                throw new BusinessException("请稍后再试");
            }
            
            if (StringUtils.isNullOrEmpty(amount)) {
                throw new BusinessException("请输入正确的金额");
            }
            if (!StringUtils.isDouble(amount)) {
                throw new BusinessException("请输入正确的金额");
            }
            if (Double.valueOf(amount).doubleValue() <= 0) {
                throw new BusinessException("请输入正确的金额");
            }
 
            double amount_double = Double.valueOf(request.getParameter("amount")).doubleValue();
                        
            Party party = this.partyService.cachePartyBy(partyId, false);
            if (!party.getEnabled()) {
                resultObject.setCode("506");
                resultObject.setMsg("用户已锁定");
                return resultObject;
            }
 
            FuturesOrder order = new FuturesOrder();
            order.setId(ApplicationUtil.getCurrentTimeUUID());
            order.setPartyId(partyId);
            order.setSymbol(symbol);
            order.setDirection(direction);
            order.setVolume(amount_double);
 
            order = this.futuresOrderService.saveOpen(order, para_id);
            data.put("order_no", order.getOrder_no());
            data.put("open_price", order.getTrade_avg_price().toString());
            
            resultObject.setData(data);
            
        } catch (BusinessException e) {
            resultObject.setCode("1");
            resultObject.setMsg(e.getMessage());
        } catch (Throwable t) {
            resultObject.setCode("1");
            resultObject.setMsg("程序错误");
        } finally {
            if (lock) {
                ThreadUtils.sleep(100);
                FuturesLock.remove(partyId);
            }
        }
        
        return resultObject;
    }
 
    /**
     * 查询交割持仓列表
     * 
     * page_no 页码
     * symbol 币种
     * type 开仓页面订单类型
     */
    @RequestMapping(action + "list.action")
    public Object list(HttpServletRequest request) throws IOException {
        String page_no = request.getParameter("page_no");
        String symbol = request.getParameter("symbol");
        String type = request.getParameter("type");
        
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
 
        ResultObject resultObject = new ResultObject();
        resultObject = this.readSecurityContextFromSession(resultObject);
        if (!"0".equals(resultObject.getCode())) {
            resultObject.setData(data);
            return resultObject;
        }
 
        try {
            
            String loginPartyId = this.getLoginPartyId();
            
            if ("orders".equals(type)) {
                
                List<FuturesOrder> list = this.futuresOrderService.cacheSubmitted();
                List<FuturesOrder> result = new LinkedList<FuturesOrder>();
                
                if (StringUtils.isNotEmpty(loginPartyId)) {
                    // 登录才添加到列表
                    
                    for (FuturesOrder order : list) {
                        if (StringUtils.isNotEmpty(symbol)) {
                            // 有种类,满足登录人和种类添加
                            if (symbol.equals(order.getSymbol()) && loginPartyId.equals(order.getPartyId())) {
                                result.add(order);                                
                            }
                        } else {
                            if (loginPartyId.equals(order.getPartyId())) {
                                result.add(order);                                
                            }
                        }
                    }
                }
 
                data = this.futuresOrderService.bulidData(result);
 
            } else if ("hisorders".equals(type)) {
                
                if (StringUtils.isNullOrEmpty(page_no)) {
                    page_no = "1";
                }
                if (!StringUtils.isInteger(page_no)) {
                    throw new BusinessException("页码不是整数");
                }
                if (Integer.valueOf(page_no).intValue() <= 0) {
                    throw new BusinessException("页码不能小于等于0");
                }
 
                int page_no_int = Integer.valueOf(page_no).intValue();
                
                List<FuturesOrder> list = this.futuresOrderService.getPaged(page_no_int, 10, this.getLoginPartyId(), symbol, type).getElements();
                data = this.futuresOrderService.bulidData(list);
            }
            
            String symbolsStr = "";
            Set<String> symbols = new HashSet<String>();
            for (int i = 0; i < data.size(); i++) {
                String sym = data.get(i).get("symbol").toString();
                if (!symbols.contains(sym)) {
                    symbols.add(sym);
                    if (i != 0) {
                        symbolsStr = symbolsStr + "," + sym;
                    } else {
                        symbolsStr = sym;
                    }
                }            
            }
            
            List<Realtime> realtime_all = this.dataService.realtime(symbolsStr);
            if (realtime_all.size() <= 0) {
                realtime_all = new ArrayList<Realtime>();
//                throw new BusinessException("系统错误,请稍后重试");
            }
            
            Map<String, Realtime> realtimeMap = new HashMap<String, Realtime>();
            for (int i = 0; i < realtime_all.size(); i++) {
                realtimeMap.put(realtime_all.get(i).getSymbol(), realtime_all.get(i));
            }
            
            for (int i = 0; i < data.size(); i++) {
                Map<String, Object> map = data.get(i);
 
                // 标记价格
                Realtime realtime = realtimeMap.get(map.get("symbol"));
                if (null == realtime) {
                    map.put("mark_price", 0);
                } else {
                    map.put("mark_price", realtime.getClose());
                }
            }
 
            resultObject.setData(data);
 
        } catch (BusinessException e) {
            resultObject.setCode("1");
            resultObject.setMsg(e.getMessage());
        } catch (Throwable t) {
            resultObject.setCode("1");
            resultObject.setMsg("程序错误");
        }
        
        return resultObject;
    }
 
    /**
     * 查询交割持仓详情
     * 
     * order_no 订单号
     */
    @RequestMapping(action + "get.action")
    public Object get(HttpServletRequest request) throws IOException {
        String order_no = request.getParameter("order_no");
 
        ResultObject resultObject = new ResultObject();
        resultObject = this.readSecurityContextFromSession(resultObject);
        if (!"0".equals(resultObject.getCode())) {
            return resultObject;
        }
 
        try {
            
            if (StringUtils.isNullOrEmpty(order_no)) {
                throw new BusinessException("订单不存在");
            }
 
            FuturesOrder order = this.futuresOrderLocalService.cacheByOrderNo(order_no);
            
            if (null == order) {
                throw new BusinessException("订单不存在");
            }
            
            resultObject.setData(this.futuresOrderLocalService.bulidOne(order));
            
        } catch (BusinessException e) {
            resultObject.setCode("1");
            resultObject.setMsg(e.getMessage());
        } catch (Throwable t) {
            resultObject.setCode("1");
            resultObject.setMsg("程序错误");
        }
 
        return resultObject;
    }
 
}