zj
2025-05-28 bcd36411feb0351347289476eb7ee9e5dd1c3b9d
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package project.project.web.admin;
 
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
 
import kernel.exception.BusinessException;
import kernel.util.Arith;
import kernel.util.JsonUtils;
import kernel.util.StringUtils;
import kernel.util.ThreadUtils;
import kernel.web.Page;
import kernel.web.PageActionSupport;
import project.Constants;
import project.contract.AdminContractOrderService;
import project.contract.ContractOrder;
import project.contract.ContractOrderService;
import project.data.AdjustmentValue;
import project.data.AdjustmentValueService;
import project.data.DataService;
import project.data.model.Realtime;
import project.item.ItemService;
import project.item.model.Item;
 
/**
 * 永续持仓单 历史单
 */
@RestController
public class AdminHistoryContractOrderController extends PageActionSupport {
 
    @Autowired
    private DataService dataService;
    
    @Autowired
    private ItemService itemService;
    
    @Autowired
    private ContractOrderService contractOrderService;
    
    @Autowired
    private AdjustmentValueService adjustmentValueService;
    
    @Autowired
    private AdminContractOrderService adminContractOrderService;
 
    private final String action = "normal/adminHistoryContractOrderAction!";
    
    private static final Logger logger = LoggerFactory.getLogger(AdminHistoryContractOrderController.class);
 
    /**
     * 获取 永续持仓历史单列表
     */
    @RequestMapping(action + "list.action")
    public ModelAndView list(HttpServletRequest request) {
        String pageNoStr = request.getParameter("pageNo");
        String message = request.getParameter("message");
        String error = request.getParameter("error");
        String order_no_para = request.getParameter("order_no_para");
        String name_para = request.getParameter("name_para");
        String rolename_para = request.getParameter("rolename_para");
        String start_time = request.getParameter("start_time");
        String end_time = request.getParameter("end_time");
        String status_para = request.getParameter("status_para");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("contract_order_history_list");
 
        int pageNo = 1;
        int pageSize = 10;
        Page page = null;
        try {
            pageNo=checkAndSetPageNo(pageNoStr);
            
//            if (StringUtils.isEmptyString(rolename_para)) {
//                rolename_para = Constants.SECURITY_ROLE_MEMBER;
//            }
 
            String loginPartyId = this.getLoginPartyId();
 
            page = this.adminContractOrderService.pagedQuery(pageNo, pageSize, status_para,
                    rolename_para, loginPartyId, start_time, end_time, name_para, order_no_para);
 
            List<Map> list = page.getElements();
            for (int i = 0; i < list.size(); i++) {
                Map map = list.get(i);                
                if (null == map.get("rolename")) {
                    map.put("roleNameDesc", "");
                } else {
                    String roleName = map.get("rolename").toString();
                    map.put("roleNameDesc", Constants.ROLE_MAP.containsKey(roleName) ? Constants.ROLE_MAP.get(roleName) : roleName);
                }
            }
 
            Map<String, String> symbols = new LinkedHashMap<String, String>();
            for (Item item : this.itemService.cacheGetAll()) {
                symbols.put(item.getSymbol(), item.getName());
            }
            
            modelAndView.addObject("symbols", symbols);
 
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            return modelAndView;
        } catch (Throwable t) {
            logger.error(" error ", t);
            modelAndView.addObject("error", "[ERROR] " + t.getMessage());
            return modelAndView;
        }
 
        modelAndView.addObject("pageNo", pageNo);
        modelAndView.addObject("pageSize", pageSize);
        modelAndView.addObject("page", page);
        modelAndView.addObject("message", message);
        modelAndView.addObject("error", error);
        modelAndView.addObject("order_no_para", order_no_para);
        modelAndView.addObject("name_para", name_para);
        modelAndView.addObject("rolename_para", rolename_para);
        modelAndView.addObject("start_time", start_time);
        modelAndView.addObject("end_time", end_time);
        modelAndView.addObject("status_para", status_para);
        return modelAndView;
    }
 
    /**
     * 显示 调整页面
     */
    @RequestMapping(action + "showModal.action")
    public String showModal(HttpServletRequest request) {
        String symbol = request.getParameter("symbol");
        
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        try {
            
            if (!StringUtils.isNullOrEmpty(this.getLoginPartyId())) {
                throw new BusinessException("无权限");
            }        
            
            Realtime realtime = this.dataService.realtime(symbol).get(0);
 
            Item item = this.itemService.cacheBySymbol(symbol, false);
            
            Double currentValue = this.adjustmentValueService.getCurrentValue(symbol);
            if (null == currentValue) {
                resultMap.put("adjust_value", 0D);
                resultMap.put("new_price", realtime.getClose());
            } else {
                resultMap.put("adjust_value", currentValue);
                resultMap.put("new_price", Arith.sub(realtime.getClose(), currentValue));
            }
 
            resultMap.put("pips", item.getPips());
 
            AdjustmentValue delayValue = this.adjustmentValueService.getDelayValue(symbol);
            if (delayValue != null) {
                resultMap.put("delay_value", delayValue.getValue());
                resultMap.put("delay_second", delayValue.getSecond());
            }
 
        } catch (BusinessException e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        } catch (Throwable t) {
            logger.error(" error ", t);
            resultMap.put("code", 500);
            resultMap.put("message", "程序错误");
        }
 
        return JsonUtils.getJsonString(resultMap);
    }
 
    /**
     * 调整 页面计算
     * 
     * type 0增加一个pips;1减少一个pips;2直接修改调整值;
     * value 调整值
     */
    @RequestMapping(action + "getValue.action")
    public String getValue(HttpServletRequest request) {
        String symbol = request.getParameter("symbol");
        String type = request.getParameter("type");
        String value = request.getParameter("value");
 
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        try {
            
            if (!StringUtils.isNullOrEmpty(this.getLoginPartyId())) {
                throw new BusinessException("无权限");
            }
 
            String error = this.verif(type, value);
            if (!StringUtils.isNullOrEmpty(error)) {
                throw new BusinessException(error);
            }
            
            int type_int = Integer.valueOf(request.getParameter("type")).intValue();
            double value_double = Double.valueOf(request.getParameter("value")).doubleValue();
            
            Realtime realtime = this.dataService.realtime(symbol).get(0);
 
            Item item = this.itemService.cacheBySymbol(symbol, false);
            
            Double currentValue = this.adjustmentValueService.getCurrentValue(symbol);
            if (null == currentValue) {
                resultMap.put("new_price", realtime.getClose());
            } else {
                resultMap.put("new_price", Arith.sub(realtime.getClose(), currentValue));
            }
 
            double temp;
            
            if (0 == type_int) {
                temp = Arith.add(value_double, item.getPips());
                // 调整量
                resultMap.put("adjust_current_value", Double.valueOf(temp));
                // 调整后的值
                resultMap.put("adjust_value_after", Double.valueOf(Arith.add(realtime.getClose(), temp)));
            } else if (1 == type_int) {
                temp = Arith.sub(value_double, item.getPips());
                resultMap.put("adjust_current_value", Double.valueOf(temp));
                resultMap.put("adjust_value_after", Arith.add(realtime.getClose(), temp));
            } else {
                temp = value_double;
                resultMap.put("adjust_current_value", Double.valueOf(temp));
                resultMap.put("adjust_value_after", Arith.add(realtime.getClose(), temp));
            }
 
            if (null == currentValue) {
                resultMap.put("adjust_value", Double.valueOf(item.getPips()));
            } else {
                resultMap.put("adjust_value", Arith.add(temp, currentValue));
            }
            
            AdjustmentValue delayValue = this.adjustmentValueService.getDelayValue(symbol);
            if (delayValue != null) {
                resultMap.put("delay_value", delayValue.getValue());
                resultMap.put("delay_second", delayValue.getSecond());
            }
 
        } catch (BusinessException e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        } catch (Throwable t) {
            logger.error(" error ", t);
            resultMap.put("code", 500);
            resultMap.put("message", "程序错误");
        }
 
        return JsonUtils.getJsonString(resultMap);
    }
 
    /**
     * 调整
     * 
     * value 调整值
     * second 延迟秒
     */
    @RequestMapping(action + "adjust.action")
    public ModelAndView adjust(HttpServletRequest request) {
        String symbol = request.getParameter("symbol");
        String value = request.getParameter("value");
        String second = request.getParameter("second");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/" + action + "list.action");
        
        try {
 
            String error = this.verifAdjust(second, value);
            if (!StringUtils.isNullOrEmpty(error)) {
                throw new BusinessException(error);
            }
            
            double value_double = Double.valueOf(request.getParameter("value")).doubleValue();
            double second_double = Double.valueOf(request.getParameter("second")).doubleValue();
 
            this.adjustmentValueService.adjust(symbol, value_double, second_double);
 
            ThreadUtils.sleep(1000);
 
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            return modelAndView;
        } catch (Throwable t) {
            logger.error(" error ", t);
            modelAndView.addObject("error", "[ERROR] " + t.getMessage());
            return modelAndView;
        }
 
        modelAndView.addObject("message", "操作成功");
        return modelAndView;
    }
 
    /**
     * 平仓或撤单
     */
    @RequestMapping(action + "close.action")
    public ModelAndView close(HttpServletRequest request) {
        String order_no = request.getParameter("order_no");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/" + action + "list.action");
        
        try {
            
            ContractOrder order = this.contractOrderService.findByOrderNo(order_no);
            if (order != null) {
                CloseDelayThread lockDelayThread = new CloseDelayThread(order.getPartyId().toString(), order_no, this.contractOrderService);
                Thread t = new Thread(lockDelayThread);
                t.start();
            }
            
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            return modelAndView;
        } catch (Throwable t) {
            logger.error(" error ", t);
            modelAndView.addObject("error", "[ERROR] " + t.getMessage());
            return modelAndView;
        }
 
        modelAndView.addObject("message", "操作成功");
        return modelAndView;
    }
 
    private String getPath(HttpServletRequest request) {
        return String.format("%s://%s:%s%s", request.getScheme(), request.getServerName(), request.getServerPort(), request.getContextPath());
    }
 
    protected String verif(String type, String value) {
        
        if (StringUtils.isNullOrEmpty(type)) {
            return "限制天数必填";
        }
        if (!StringUtils.isInteger(type)) {
            return "限制天数输入错误,请输入整数";
        }
        if (Integer.valueOf(type).intValue() < 0) {
            return "限制天数不能小于0";
        }
 
        if (StringUtils.isNullOrEmpty(value)) {
            return "调整值必填";
        }
        if (!StringUtils.isDouble(value)) {
            return "调整值不是浮点数";
        }
        if (Double.valueOf(value).doubleValue() < 0) {
            return "调整值不能小于0";
        }
        
        return null;
    }
 
    protected String verifAdjust(String second, String value) {
 
        if (StringUtils.isNullOrEmpty(second)) {
            return "调整时间必填";
        }
        if (!StringUtils.isDouble(second)) {
            return "调整时间不是浮点数";
        }
 
        if (StringUtils.isNullOrEmpty(value)) {
            return "调整值必填";
        }
        if (!StringUtils.isDouble(value)) {
            return "调整值不是浮点数";
        }
        if (Double.valueOf(value).doubleValue() <= 0) {
            return "调整值不能小于等于0";
        }
        
        return null;
    }
 
    /**
     * 新线程处理,直接拿到订单锁处理完成后退出
     */
    public class CloseDelayThread implements Runnable {
        private String partyId;
        private String order_no;
        private ContractOrderService contractOrderService;
 
        public void run() {
            
            try {
                
                while (true) {
                    
                    if (this.contractOrderService.lock(order_no)) {
                        this.contractOrderService.saveClose(partyId, order_no);
                        // 处理完退出
                        break;
                    }
                    ThreadUtils.sleep(500);
                }
 
            } catch (Throwable t) {
                logger.error("error:", t);
            } finally {
                this.contractOrderService.unlock(order_no);
            }
        }
 
        public CloseDelayThread(String partyId, String order_no, ContractOrderService contractOrderService) {
            this.partyId = partyId;
            this.order_no = order_no;
            this.contractOrderService = contractOrderService;
        }
    }
 
}