1
zj
2025-07-10 8d9197af3ec0b06565aa44aae631f0f8a661897b
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package project.web.admin;
 
import java.util.ArrayList;
import java.util.HashMap;
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.StringUtils;
import kernel.web.PageActionSupport;
import project.futures.AdminContractManageService;
import project.futures.AdminFuturesParaService;
import project.futures.FuturesPara;
import project.futures.FuturesPara.TIMENUM;
import project.item.AdminItemService;
import project.item.model.Item;
 
/**
 * 交割合约管理
 */
@RestController
public class AdminContractManageController extends PageActionSupport {
 
    private Logger logger = LoggerFactory.getLogger(AdminContractManageController.class);
 
    @Autowired
    private AdminContractManageService adminContractManageService;
    @Autowired
    private AdminItemService adminItemService;
    @Autowired
    private AdminFuturesParaService adminFuturesParaService;
 
    private final String action = "normal/adminContractManageAction!";
 
    /**
     * 获取 交易对 列表
     */
    @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 para_symbol = request.getParameter("para_symbol");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("contract_manage_list");
        
        Map<String, Object> contractResult = new HashMap<String, Object>();
 
        int pageNo=1;
        int pageSize=20;
        try {
            pageNo=checkAndSetPageNo(pageNoStr);
            List<Item> itemsRet = new ArrayList<Item>();
            
            List<Item> items = this.adminItemService.getItems();
            
            if (StringUtils.isEmptyString(para_symbol)) {
                itemsRet = items;
            } else {
                for (int i = 0; i < items.size(); i++) {
                    Item item = items.get(i);
                    if (item.getSymbol().equalsIgnoreCase(para_symbol)) {
                        itemsRet.add(item);
                    }
                }                
            }
            
            contractResult.put("contractItems", itemsRet);
            
        } 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("para_symbol", para_symbol);
        modelAndView.addObject("contractResult", contractResult);
        return modelAndView;
    }
 
    /**
     * 获取 交易参数 列表
     */
    @RequestMapping(action + "listPara.action")
    public ModelAndView listPara(HttpServletRequest request) {
        String pageNoStr = request.getParameter("pageNo");
        String message = request.getParameter("message");
        String error = request.getParameter("error");
        String query_symbol = request.getParameter("query_symbol");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("contract_para_list");
        
        Map<String, Object> contractResult = new HashMap<String, Object>();
 
        int pageNo=1;
        int pageSize=10;
        try {
            pageNo=checkAndSetPageNo(pageNoStr);
            contractResult.put("futures", this.adminFuturesParaService.pagedQuery(pageNo,pageSize,query_symbol));
        } 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("message", message);        
        modelAndView.addObject("error", error);
        modelAndView.addObject("query_symbol", query_symbol);
        modelAndView.addObject("contractResult", contractResult);
        if (StringUtils.isEmptyString(query_symbol)) {
            modelAndView.addObject("pageNo", 1);
            modelAndView.addObject("pageSize", 1);
            modelAndView.addObject("contractResult", new HashMap<String, Object>());
            error = "请选择合约代码";
        }
        return modelAndView;
    }
    
//    public String toAdd() {
//        basePath = getPath(ServletActionContext.getRequest());
//        if (StringUtils.isNotEmpty(itemId)) {
//            Item item = adminItemService.get(itemId);
//            this.itemId = item.getId().toString();
//            this.name = item.getName();
//            this.symbol = item.getSymbol();
//            this.decimals = item.getDecimals();
//            this.symbol_data = item.getSymbol_data();
//        }
//        return "add";
//    }
    
//    public String addContractItem() {
//        try {
//            this.error = verifyItem();
//            if (StringUtils.isNotEmpty(this.error))
//                return toAdd();
//            Item entity = new Item();
//            entity.setId(this.itemId);
//            entity.setName(name);
//            entity.setSymbol(symbol);
//            entity.setDecimals(decimals);
//            entity.setSymbol_data(symbol_data);
//            this.error = adminContractManageService.addContractItem(entity);
//        } catch (BusinessException e) {
//            this.error = e.getMessage();
//        }
//        return StringUtils.isNotEmpty(error) ? toAdd() : list();
//    }
    
    /**
     * 新增/修改 交易参数 页面
     */
    @RequestMapping(action + "toAddInstall.action")
    public ModelAndView toAddInstall(HttpServletRequest request) {
        String futuresId = request.getParameter("futuresId");
        String query_symbol = request.getParameter("query_symbol");
 
        ModelAndView modelAndView = new ModelAndView();
        
        try {
            
            Map<String, String> symbolMap = new HashMap<String, String>();
            symbolMap = this.adminContractManageService.getFuturesSymbols();
            
            FuturesPara futuresPara = new FuturesPara();
            if (StringUtils.isNotEmpty(futuresId)) {
                // 修改
                
                futuresPara = this.adminFuturesParaService.getById(futuresId);
                if (null == futuresPara) {
                    throw new BusinessException("交易参数不存在");
                }
                
                futuresPara.setProfit_ratio(Arith.mul(futuresPara.getProfit_ratio(), 100));
                futuresPara.setProfit_ratio_max(Arith.mul(futuresPara.getProfit_ratio_max(), 100));
                futuresPara.setUnit_fee(Arith.mul(futuresPara.getUnit_fee(), 100));
 
                futuresPara.setTimeUnitCn(TIMENUM.valueOf(futuresPara.getTimeUnit()).getCn());
 
                modelAndView.addObject("futuresId", futuresId);
                modelAndView.addObject("query_symbol", query_symbol);
                modelAndView.addObject("symbolMap", symbolMap);
                modelAndView.addObject("futuresPara_id", futuresPara.getId());
                modelAndView.addObject("futuresPara_symbol", futuresPara.getSymbol());
                modelAndView.addObject("futuresPara_timeNum", futuresPara.getTimeNum());
                modelAndView.addObject("futuresPara_timeUnit", futuresPara.getTimeUnit());
                modelAndView.addObject("futuresPara_profit_ratio", futuresPara.getProfit_ratio());
                modelAndView.addObject("futuresPara_profit_ratio_max", futuresPara.getProfit_ratio_max());
                modelAndView.addObject("futuresPara_unit_amount", futuresPara.getUnit_amount());
                modelAndView.addObject("futuresPara_unit_fee", futuresPara.getUnit_fee());
                modelAndView.addObject("futuresPara_timeUnitCn", futuresPara.getTimeUnitCn());
                modelAndView.addObject("futuresPara_unit_max_amount", futuresPara.getUnit_max_amount());
                
            } else {
                // 新增
                if (StringUtils.isEmptyString(query_symbol)) {
                    throw new BusinessException("请选择合约代码");
                }
                
                modelAndView.addObject("futuresId", futuresId);
                modelAndView.addObject("query_symbol", query_symbol);
                modelAndView.addObject("symbolMap", symbolMap);
            }
            
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            modelAndView.setViewName("redirect:/" + action + "list.action");
            return modelAndView;
        } catch (Throwable t) {
            logger.error(" error ", t);
            modelAndView.addObject("error", "[ERROR] " + t.getMessage());
            modelAndView.setViewName("redirect:/" + action + "list.action");
            return modelAndView;
        }
        
        modelAndView.setViewName("contract_install_add");
        return modelAndView;
    }
    
    /**
     * 新增/修改 交易参数
     */
    @RequestMapping(action + "addFutures.action")
    public ModelAndView addFutures(HttpServletRequest request) {
        String futuresId = request.getParameter("futuresId");
        String query_symbol = request.getParameter("query_symbol");
        String futuresPara_id = request.getParameter("futuresPara_id");
        String futuresPara_symbol = request.getParameter("futuresPara_symbol");
        String futuresPara_timeNum = request.getParameter("futuresPara_timeNum");
        String futuresPara_timeUnit = request.getParameter("futuresPara_timeUnit");
        String futuresPara_profit_ratio = request.getParameter("futuresPara_profit_ratio");
        String futuresPara_profit_ratio_max = request.getParameter("futuresPara_profit_ratio_max");
        String futuresPara_unit_amount = request.getParameter("futuresPara_unit_amount");
        String futuresPara_unit_fee = request.getParameter("futuresPara_unit_fee");
        String futuresPara_timeUnitCn = request.getParameter("futuresPara_timeUnitCn");
        String futuresPara_unit_max_amount = request.getParameter("futuresPara_unit_max_amount");
        String login_safeword = request.getParameter("login_safeword");
 
        ModelAndView modelAndView = new ModelAndView();
        
        Map<String, String> symbolMap = new HashMap<String, String>();
        symbolMap = this.adminContractManageService.getFuturesSymbols();
 
        try {
            
            if (StringUtils.isNullOrEmpty(futuresPara_unit_max_amount)) {
                futuresPara_unit_max_amount = "0";
            }
 
            String error = this.verification(query_symbol, futuresPara_timeNum, futuresPara_timeUnit, futuresPara_unit_amount, 
                    futuresPara_unit_fee, futuresPara_profit_ratio, futuresPara_profit_ratio_max, futuresPara_unit_max_amount);
            if (StringUtils.isNotEmpty(error)) {
                throw new BusinessException(error);
            }
            
            int futuresPara_timeNum_int = Integer.valueOf(futuresPara_timeNum).intValue();
            double futuresPara_unit_amount_double = Double.valueOf(futuresPara_unit_amount).doubleValue();
            double futuresPara_unit_fee_double = Double.valueOf(futuresPara_unit_fee).doubleValue();
            double futuresPara_profit_ratio_double = Double.valueOf(futuresPara_profit_ratio).doubleValue();
            double futuresPara_profit_ratio_max_double = Double.valueOf(futuresPara_profit_ratio_max).doubleValue();
            double futuresPara_unit_max_amount_double = Double.valueOf(futuresPara_unit_max_amount).doubleValue();
 
            FuturesPara futuresPara = new FuturesPara();
            futuresPara.setId(futuresPara_id);
            futuresPara.setSymbol(futuresPara_symbol);
            futuresPara.setTimeNum(futuresPara_timeNum_int);
            futuresPara.setTimeUnit(futuresPara_timeUnit);
            futuresPara.setProfit_ratio(Arith.div(futuresPara_profit_ratio_double, 100));
            futuresPara.setProfit_ratio_max(Arith.div(futuresPara_profit_ratio_max_double, 100));
            futuresPara.setUnit_amount(futuresPara_unit_amount_double);
            futuresPara.setUnit_fee(Arith.div(futuresPara_unit_fee_double, 100));
            futuresPara.setTimeUnitCn(futuresPara_timeUnitCn);
            futuresPara.setUnit_max_amount(futuresPara_unit_max_amount_double);
                        
            String error1 = this.adminContractManageService.addFutures(futuresPara, this.getIp(), this.getUsername_login(), login_safeword);
            if (StringUtils.isNotEmpty(error1)) {
                throw new BusinessException(error1);
            }
 
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            modelAndView.addObject("futuresId", futuresId);
            modelAndView.addObject("query_symbol", query_symbol);
            modelAndView.addObject("symbolMap", symbolMap);            
            modelAndView.addObject("futuresPara_id", futuresPara_id);
            modelAndView.addObject("futuresPara_symbol", futuresPara_symbol);
            modelAndView.addObject("futuresPara_timeNum", futuresPara_timeNum);
            modelAndView.addObject("futuresPara_timeUnit", futuresPara_timeUnit);
            modelAndView.addObject("futuresPara_profit_ratio", futuresPara_profit_ratio);
            modelAndView.addObject("futuresPara_profit_ratio_max", futuresPara_profit_ratio_max);
            modelAndView.addObject("futuresPara_unit_amount", futuresPara_unit_amount);
            modelAndView.addObject("futuresPara_unit_fee", futuresPara_unit_fee);
            modelAndView.addObject("futuresPara_timeUnitCn", futuresPara_timeUnitCn);
            modelAndView.addObject("futuresPara_unit_max_amount", futuresPara_unit_max_amount);
            modelAndView.setViewName("contract_install_add");
            return modelAndView;
        } catch (Throwable t) {
            logger.error(" error ", t);
            modelAndView.addObject("error", "[ERROR] " + t.getMessage());
            modelAndView.addObject("futuresId", futuresId);
            modelAndView.addObject("query_symbol", query_symbol);
            modelAndView.addObject("symbolMap", symbolMap);            
            modelAndView.addObject("futuresPara_id", futuresPara_id);
            modelAndView.addObject("futuresPara_symbol", futuresPara_symbol);
            modelAndView.addObject("futuresPara_timeNum", futuresPara_timeNum);
            modelAndView.addObject("futuresPara_timeUnit", futuresPara_timeUnit);
            modelAndView.addObject("futuresPara_profit_ratio", futuresPara_profit_ratio);
            modelAndView.addObject("futuresPara_profit_ratio_max", futuresPara_profit_ratio_max);
            modelAndView.addObject("futuresPara_unit_amount", futuresPara_unit_amount);
            modelAndView.addObject("futuresPara_unit_fee", futuresPara_unit_fee);
            modelAndView.addObject("futuresPara_timeUnitCn", futuresPara_timeUnitCn);
            modelAndView.addObject("futuresPara_unit_max_amount", futuresPara_unit_max_amount);
            modelAndView.setViewName("contract_install_add");
            return modelAndView;
        }
        
        modelAndView.addObject("message", "操作成功");
        modelAndView.addObject("query_symbol", query_symbol);
        modelAndView.setViewName("redirect:/" + action + "listPara.action");
        return modelAndView;
    }
 
    /**
     * 删除 交易参数
     */
    @RequestMapping(action + "toDeleteFuturesPara.action")
    public ModelAndView toDeleteFuturesPara(HttpServletRequest request) {
        String futuresId = request.getParameter("futuresId");
        String login_safeword = request.getParameter("login_safeword");
        String super_google_auth_code = request.getParameter("super_google_auth_code");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/" + action + "listPara.action");
        
        try {
 
            this.adminContractManageService.deleteFuturesPara(futuresId, this.getIp(), this.getUsername_login(), login_safeword, super_google_auth_code);
 
        } 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 verification(String symbol, String timeNum, String timeUnit, String unit_amount, String unit_fee, 
            String profit_ratio, String profit_ratio_max, String unit_max_amount) {
        
        if (StringUtils.isEmptyString(symbol)) {
            return "请选择合约代码";
        }
        
        if (StringUtils.isNullOrEmpty(timeNum)) {
            return "时间必填";
        }
        if (!StringUtils.isInteger(timeNum)) {
            return "时间不是整数";
        }
        if (Integer.valueOf(timeNum).intValue() <= 0) {
            return "时间不能小于等于0";
        }
 
        if (StringUtils.isEmptyString(timeUnit)) {
            return "请选择时间单位";
        }
        
        if (StringUtils.isNullOrEmpty(unit_amount)) {
            return "每手金额必填";
        }
        if (!StringUtils.isDouble(unit_amount)) {
            return "每手金额不是浮点数";
        }
        if (Double.valueOf(unit_amount).doubleValue() <= 0) {
            return "每手金额不能小于等于0";
        }
        
        if (StringUtils.isNullOrEmpty(unit_amount)) {
            return "手续费必填";
        }
        if (!StringUtils.isDouble(unit_amount)) {
            return "手续费不是浮点数";
        }
        if (Double.valueOf(unit_amount).doubleValue() <= 0) {
            return "手续费不能小于等于0";
        }
        
        if (StringUtils.isNullOrEmpty(profit_ratio)) {
            return "最小收益率必填";
        }
        if (!StringUtils.isDouble(profit_ratio)) {
            return "最小收益率不是浮点数";
        }
        if (Double.valueOf(profit_ratio).doubleValue() <= 0) {
            return "最小收益率不能小于等于0";
        }
        
        if (StringUtils.isNullOrEmpty(profit_ratio_max)) {
            return "最大收益率必填";
        }
        if (!StringUtils.isDouble(profit_ratio_max)) {
            return "最大收益率不是浮点数";
        }
        if (Double.valueOf(profit_ratio_max).doubleValue() <= 0) {
            return "最大收益率不能小于等于0";
        }
        
        if (StringUtils.isNullOrEmpty(unit_max_amount)) {
            return "最高购买金额必填";
        }
        if (!StringUtils.isDouble(unit_max_amount)) {
            return "最高购买金额不是浮点数";
        }
//        if (Double.valueOf(unit_max_amount).doubleValue() <= 0) {
//            return "最高购买金额不能小于等于0";
//        }
        double unit_amount_double = Double.valueOf(unit_amount).doubleValue();
        double unit_max_amount_double = Double.valueOf(unit_max_amount).doubleValue();
        if (unit_max_amount_double != 0 && unit_max_amount_double < unit_amount_double) {
            return "最高购买金额需大于最低购买金额";
        }
        
        return null;
    }
 
//    private String verifyItem() {
//        if (StringUtils.isEmpty(this.name))
//            return "合约名称不能为空";
//        if (StringUtils.isEmpty(this.symbol))
//            return "代码不能为空";
//        if (StringUtils.isEmpty(this.symbol_data))
//            return "请选择交易对";
//        return null;
//    }
 
    private String getPath(HttpServletRequest request) {
        return String.format("%s://%s:%s%s", request.getScheme(), request.getServerName(), request.getServerPort(), request.getContextPath());
    }
 
}