| | |
| | | import com.yami.trading.common.lang.LangUtils; |
| | | import com.yami.trading.common.util.DateTimeTools; |
| | | import com.yami.trading.common.util.StringUtils; |
| | | import com.yami.trading.common.util.ThreadUtils; |
| | | import com.yami.trading.security.common.util.SecurityUtils; |
| | | import com.yami.trading.service.contract.ContractLockService; |
| | | import com.yami.trading.service.contract.ContractOrderService; |
| | |
| | | */ |
| | | @RequestMapping(action + "close.action") |
| | | public Result<String> close(@RequestParam String order_no) { |
| | | String partyId = SecurityUtils.getCurrentUserId(); |
| | | try { |
| | | ContractOrder order = contractOrderService.findByOrderNo(order_no); |
| | | if (order != null && ContractOrder.ORDER_FOLLOW == order.getFollow()) { |
| | | if (order == null) { |
| | | return Result.failed("订单不存在"); |
| | | } |
| | | if (!partyId.equals(order.getPartyId())) { |
| | | return Result.failed("无权限"); |
| | | } |
| | | if (ContractOrder.ORDER_FOLLOW == order.getFollow()) { |
| | | return Result.failed("跟单订单不支持手动平仓,请先停止跟单"); |
| | | } |
| | | // 建议使用线程池 TODO |
| | | CloseDelayThread lockDelayThread = new CloseDelayThread(SecurityUtils.getCurrentUserId(), order_no, this.contractOrderService, false); |
| | | Thread t = new Thread(lockDelayThread); |
| | | t.start(); |
| | | if (!contractLockService.getContractLock(order_no)) { |
| | | return Result.failed("请稍后再试"); |
| | | } |
| | | try { |
| | | ContractOrder closed = contractOrderService.saveClose(partyId, order_no); |
| | | if (closed == null) { |
| | | return Result.failed("平仓失败,请刷新持仓后重试"); |
| | | } |
| | | } finally { |
| | | contractLockService.removeContractLock(order_no); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("平仓失败", e); |
| | | return Result.failed("平仓失败"); |
| | | return Result.failed(e.getMessage() != null ? e.getMessage() : "平仓失败"); |
| | | } |
| | | |
| | | return Result.succeed("平仓成功"); |
| | | } |
| | | |
| | |
| | | */ |
| | | @RequestMapping(action + "closeAll.action") |
| | | public Result<String> closeAll() { |
| | | String partyId = SecurityUtils.getCurrentUserId(); |
| | | try { |
| | | String partyId = SecurityUtils.getCurrentUserId(); |
| | | List<ContractOrder> submittedOrders = contractOrderService.findSubmitted(partyId); |
| | | if (submittedOrders != null) { |
| | | for (ContractOrder one : submittedOrders) { |
| | | if (ContractOrder.ORDER_FOLLOW == one.getFollow()) { |
| | | return Result.failed("包含跟单订单,不能手动一键平仓,请先停止跟单"); |
| | | } |
| | | if (submittedOrders == null || submittedOrders.isEmpty()) { |
| | | return Result.succeed("一键平仓成功"); |
| | | } |
| | | for (ContractOrder one : submittedOrders) { |
| | | if (ContractOrder.ORDER_FOLLOW == one.getFollow()) { |
| | | return Result.failed("包含跟单订单,不能手动一键平仓,请先停止跟单"); |
| | | } |
| | | } |
| | | CloseDelayThread lockDelayThread = new CloseDelayThread(SecurityUtils.getCurrentUserId(), "", this.contractOrderService, true); |
| | | Thread t = new Thread(lockDelayThread); |
| | | t.start(); |
| | | if (!contractLockService.getContractLock("all")) { |
| | | return Result.failed("请稍后再试"); |
| | | } |
| | | try { |
| | | contractOrderService.saveCloseRemoveAllByPartyId(partyId); |
| | | } finally { |
| | | contractLockService.removeContractLock("all"); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("一键平仓失败", e); |
| | | return Result.failed("一键平仓失败"); |
| | | return Result.failed(e.getMessage() != null ? e.getMessage() : "一键平仓失败"); |
| | | } |
| | | |
| | | return Result.succeed("一键平仓成功"); |
| | | } |
| | | |
| | |
| | | dateN.put("expectedProfitAndLoss", Double.valueOf(String.format("%.2f", expectedProfitAndLoss))); |
| | | dateN.put("count", count); |
| | | return Result.ok(dateN); |
| | | } |
| | | |
| | | /** |
| | | * 新线程处理,直接拿到订单锁处理完成后退出 |
| | | */ |
| | | public class CloseDelayThread implements Runnable { |
| | | private String partyId; |
| | | private String order_no; |
| | | private ContractOrderService contractOrderService; |
| | | private boolean all = false; |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | while (true) { |
| | | if (true == all) { |
| | | // 一键平仓 |
| | | // if (ContractLock.add("all")) { |
| | | if (contractLockService.getContractLock("all")) { |
| | | this.contractOrderService.saveCloseRemoveAllByPartyId(partyId); |
| | | // 处理完退出 |
| | | break; |
| | | } |
| | | ThreadUtils.sleep(500); |
| | | } else { |
| | | // if (ContractLock.add(order_no)) { |
| | | if (contractLockService.getContractLock(order_no)) { |
| | | this.contractOrderService.saveClose(partyId, order_no); |
| | | // 处理完退出 |
| | | break; |
| | | } |
| | | ThreadUtils.sleep(500); |
| | | } |
| | | } |
| | | |
| | | } catch (Throwable t) { |
| | | log.error("error:", t); |
| | | } finally { |
| | | if (true == all) { |
| | | // ContractLock.remove("all"); |
| | | contractLockService.removeContractLock("all"); |
| | | } else { |
| | | // ContractLock.remove(order_no); |
| | | contractLockService.removeContractLock(order_no); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public CloseDelayThread(String partyId, String order_no, ContractOrderService contractOrderService, boolean all) { |
| | | this.partyId = partyId; |
| | | this.order_no = order_no; |
| | | this.contractOrderService = contractOrderService; |
| | | this.all = all; |
| | | } |
| | | } |
| | | |
| | | } |