| | |
| | | import com.yami.trading.common.util.StringUtils; |
| | | import com.yami.trading.dao.contract.ContractApplyOrderMapper; |
| | | import com.yami.trading.service.MoneyLogService; |
| | | import com.yami.trading.service.StrongLevelCalculationService; |
| | | import com.yami.trading.service.data.DataService; |
| | | import com.yami.trading.service.user.UserService; |
| | | import com.yami.trading.service.WalletService; |
| | |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | |
| | | private WalletService walletService; |
| | | @Autowired |
| | | private ContractOrderService contractOrderService; |
| | | @Autowired |
| | | private StrongLevelCalculationService strongLevelCalculationService; |
| | | |
| | | public Page<Map<String, Object>> findList(Page<ContractApplyOrder> page, String partyId, String symbol, String type, String startTime, String endTime, String symbolType) { |
| | | QueryWrapper<ContractApplyOrder> queryWrapper = new QueryWrapper<>(); |
| | |
| | | if(realtime.size()>=1){ |
| | | map.put("mark_price", realtime.get(0).getClose()); |
| | | } |
| | | map.put("amount", order.getVolume().multiply(order.getUnitAmount())); |
| | | map.put("amount_open", order.getVolumeOpen().multiply(order.getUnitAmount())); |
| | | map.put("fee", order.getFee()); |
| | | map.put("deposit", order.getDeposit()); |
| | | map.put("amount", (order.getVolume() != null && order.getUnitAmount() != null) |
| | | ? order.getVolume().multiply(order.getUnitAmount()) |
| | | : BigDecimal.ZERO); |
| | | |
| | | map.put("amount_open", (order.getVolumeOpen() != null && order.getUnitAmount() != null) |
| | | ? order.getVolumeOpen().multiply(order.getUnitAmount()) |
| | | : BigDecimal.ZERO); |
| | | |
| | | map.put("fee", order.getFee() != null ? order.getFee() : BigDecimal.ZERO); |
| | | map.put("deposit", order.getDeposit() != null ? order.getDeposit() : BigDecimal.ZERO); |
| | | data.add(map); |
| | | } |
| | | return data; |
| | |
| | | |
| | | public void saveCreate(ContractApplyOrder order) { |
| | | |
| | | if (order.getVolumeOpen().doubleValue() % 1 != 0) { |
| | | throw new YamiShopBindException("Parameter Error1"); |
| | | } |
| | | |
| | | if (order.getVolumeOpen().compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw new YamiShopBindException("Parameter Error2"); |
| | | // if (order.getVolumeOpen().doubleValue() % 1 != 0) { |
| | | // throw new YamiShopBindException("Parameter Error1"); |
| | | // } |
| | | // |
| | | // if (order.getVolumeOpen().compareTo(BigDecimal.ZERO) <= 0) { |
| | | // throw new YamiShopBindException("Parameter Error2"); |
| | | // } |
| | | if(order.getMoney().compareTo(BigDecimal.ZERO) <= 0){ |
| | | throw new YamiShopBindException("Please enter the order amount"); |
| | | } |
| | | |
| | | boolean orderOpen = this.sysparaService.find("order_open").getBoolean(); |
| | |
| | | */ |
| | | public void open(ContractApplyOrder order) { |
| | | Item item = this.itemService.findBySymbol(order.getSymbol()); |
| | | //虚拟币新币 |
| | | if (!itemService.isContractTrading(item)) { |
| | | throw new YamiShopBindException("未开放合约交易"); |
| | | } |
| | | if (itemService.isSuspended(item)) { |
| | | throw new YamiShopBindException("停牌禁止交易"); |
| | | } |
| | | |
| | | List<ItemLeverageDTO> levers = itemLeverageService.findByItemId(item.getUuid()); |
| | | log.info("{} --- order --- {} --- {}", order.getSymbol(), item.getUuid(), levers.size()); |
| | | checkLever(order, levers); |
| | | |
| | | List<ContractOrder> contractOrderSubmitted = contractOrderService.findSubmitted(order.getPartyId(), |
| | | order.getSymbol(), order.getDirection()); |
| | | for (int i = 0; i < contractOrderSubmitted.size(); i++) { |
| | | BigDecimal sourceLeverRate = order.getLeverRate(); |
| | | sourceLeverRate = sourceLeverRate == null ? BigDecimal.ZERO : sourceLeverRate; |
| | | |
| | | BigDecimal targetLeverRate = contractOrderSubmitted.get(i).getLeverRate(); |
| | | targetLeverRate = targetLeverRate == null ? BigDecimal.ZERO : targetLeverRate; |
| | | if (sourceLeverRate.compareTo(targetLeverRate) != 0) { |
| | | throw new YamiShopBindException("存在不同杠杆的持仓单"); |
| | | } |
| | | } |
| | | List<ContractApplyOrder> applyOrderSubmittedList = this.findSubmitted(order.getPartyId().toString(), |
| | | order.getSymbol(), "open", order.getDirection()); |
| | | for (int i = 0; i < applyOrderSubmittedList.size(); i++) { |
| | | BigDecimal sourceLeverRate = order.getLeverRate(); |
| | | sourceLeverRate = sourceLeverRate == null ? BigDecimal.ZERO : sourceLeverRate; |
| | | BigDecimal targetLeverRate = applyOrderSubmittedList.get(i).getLeverRate(); |
| | | targetLeverRate = targetLeverRate == null ? BigDecimal.ZERO : targetLeverRate; |
| | | if (sourceLeverRate.compareTo(targetLeverRate) != 0) { |
| | | throw new YamiShopBindException("存在不同杠杆的持仓单"); |
| | | } |
| | | } |
| | | |
| | | |
| | | order.setOrderNo(DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(8)); |
| | | order.setUnitAmount(item.getUnitAmount()); |
| | | |
| | | order.setFee(item.getUnitFee().multiply(order.getVolume())); |
| | | order.setDeposit(item.getUnitAmount().multiply(order.getVolume())); |
| | | double number = strongLevelCalculationService.countSheets(order.getMoney().doubleValue(), order.getLeverRate().intValue(), 0.01, order.getPrice().doubleValue()); |
| | | |
| | | order.setVolumeOpen(new BigDecimal(number)); |
| | | order.setVolume(new BigDecimal(number)); |
| | | |
| | | BigDecimal unitAmount = order.getPrice().multiply(BigDecimal.valueOf(item.getFaceValue())); |
| | | unitAmount = unitAmount.setScale(4, RoundingMode.DOWN); |
| | | |
| | | order.setUnitAmount(unitAmount); |
| | | order.setDeposit(order.getMoney()); |
| | | |
| | | if (order.getLeverRate() != null) { |
| | | /** |
| | | * 加上杠杆 |
| | | */ |
| | | order.setVolume(order.getVolume().multiply(order.getLeverRate())); |
| | | order.setFee(order.getFee().multiply(order.getLeverRate())); |
| | | |
| | | BigDecimal fee = order.getDeposit().multiply(order.getLeverRate()).multiply(item.getUnitFee()); |
| | | fee = fee.setScale(4, RoundingMode.DOWN); // 保留两位小数 |
| | | order.setFee(fee); |
| | | } |
| | | order.setVolumeOpen(order.getVolume()); |
| | | |
| | | Wallet wallet = this.walletService.findByUserId(order.getPartyId()); |
| | | BigDecimal amountBefore = wallet.getMoney(); |
| | | BigDecimal totalAmountCost = order.getDeposit().add(order.getFee()); |
| | | |
| | | if (amountBefore.compareTo(totalAmountCost) < 0) { |
| | | throw new YamiShopBindException("余额不足"); |
| | | throw new YamiShopBindException("not sufficient funds"); |
| | | } |
| | | |
| | | walletService.updateMoney(order.getSymbol(), order.getPartyId(), BigDecimal.ZERO.subtract(order.getDeposit()), BigDecimal.ZERO |
| | | , Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_CONTRACT_OPEN, "委托单,订单号[" + order.getOrderNo() + "]" |
| | | ); |
| | | walletService.updateMoney(order.getSymbol(), order.getPartyId(), BigDecimal.ZERO.subtract(order.getFee()), BigDecimal.ZERO |
| | | , Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_FEE, "委托单,订单号[" + order.getOrderNo() + "]" |
| | | ); |
| | | save(order); |
| | | //如果是限价单先扣钱 |
| | | if(order.getOrderPriceType().equals("limit")){ |
| | | |
| | | walletService.updateMoney(order.getSymbol(), order.getPartyId(), BigDecimal.ZERO.subtract(order.getDeposit()), BigDecimal.ZERO |
| | | , Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_CONTRACT_OPEN, "委托单,订单号[" + order.getOrderNo() + "]" |
| | | ); |
| | | walletService.updateMoney(order.getSymbol(), order.getPartyId(), BigDecimal.ZERO.subtract(order.getFee()), BigDecimal.ZERO |
| | | , Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_FEE, "委托单,订单号[" + order.getOrderNo() + "]" |
| | | ); |
| | | } |
| | | save(order); |
| | | } |
| | | |
| | | /** |
| | |
| | | volume = volume.subtract(applyOrderSubmittedList.get(i).getVolume()); |
| | | } |
| | | if (order.getVolume().compareTo(volume) > 0) { |
| | | throw new YamiShopBindException("可平仓合约张数不足"); |
| | | throw new YamiShopBindException("可平仓合约数量不足"); |
| | | } |
| | | save(order); |
| | | |
| | |
| | | return; |
| | | } |
| | | order.setState("canceled"); |
| | | walletService.updateMoney(order.getSymbol(), |
| | | order.getPartyId(), |
| | | order.getDeposit().add(order.getFee()), |
| | | BigDecimal.ZERO, |
| | | Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_CONTRACT_CONCEL, "撤单,订单号[" + order.getOrderNo() + "]"); |
| | | if(order.getOrderPriceType().equals("limit")){ |
| | | walletService.updateMoney(order.getSymbol(), |
| | | order.getPartyId(), |
| | | order.getDeposit().add(order.getFee()), |
| | | BigDecimal.ZERO, |
| | | Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_CONTRACT_CONCEL, "撤单,订单号[" + order.getOrderNo() + "]"); |
| | | } |
| | | updateById(order); |
| | | } |
| | | |