| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.xcong.excoin.common.LoginUserUtils; |
| | | import com.xcong.excoin.common.enumerates.CoinTypeEnum; |
| | | import com.xcong.excoin.common.enumerates.MemberWalletCoinEnum; |
| | | import com.xcong.excoin.common.enumerates.RabbitPriceTypeEnum; |
| | | import com.xcong.excoin.common.exception.GlobalException; |
| | | import com.xcong.excoin.common.response.Result; |
| | | import com.xcong.excoin.common.system.service.CommonService; |
| | | import com.xcong.excoin.modules.contract.dao.ContractEntrustOrderDao; |
| | |
| | | } |
| | | } |
| | | |
| | | MemberWalletContractEntity walletContract = memberWalletContractDao.selectById(memberEntity.getId()); |
| | | |
| | | BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(submitEntrustDto.getSymbol()); |
| | | PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); |
| | | |
| | | // 保证金计算 -- 建仓价X规格X手数X(1/杠杆倍率) |
| | | BigDecimal bondAmount = submitEntrustDto.getEntrustPrice().multiply(lotNumber).multiply(new BigDecimal(submitEntrustDto.getSymbolCnt())).multiply((BigDecimal.ONE.divide(BigDecimal.valueOf(submitEntrustDto.getLeverRatio()), 8, BigDecimal.ROUND_DOWN))); |
| | | |
| | | // 开仓手续费 建仓价*规格*手数*手续费率 |
| | | BigDecimal openFeePrice = submitEntrustDto.getEntrustPrice().multiply(lotNumber) |
| | | .multiply(new BigDecimal(submitEntrustDto.getSymbolCnt())) |
| | | .multiply(tradeSettingEntity.getFeeRatio().divide(new BigDecimal(100))) |
| | | .setScale(8, BigDecimal.ROUND_DOWN); |
| | | log.info("手续费:{}", openFeePrice); |
| | | |
| | | // 预付款 |
| | | BigDecimal entrustTotalAmount = bondAmount.add(openFeePrice).add(openFeePrice); |
| | | log.info("预付款:{}", entrustTotalAmount); |
| | | |
| | | if (entrustTotalAmount.compareTo(walletContract.getAvailableBalance()) > -1) { |
| | | return Result.fail(MessageSourceUtils.getString("member_service_0085")); |
| | | } |
| | | |
| | | ContractEntrustOrderEntityMapper convert = ContractEntrustOrderEntityMapper.INSTANCE; |
| | | ContractEntrustOrderEntity entrustOrderEntity = convert.submitEntrustDtoToEntity(submitEntrustDto); |
| | | entrustOrderEntity.setOrderNo(commonService.generateOrderNo(memberEntity.getId())); |
| | | entrustOrderEntity.setMemberId(memberEntity.getId()); |
| | | entrustOrderEntity.setBondAmount(bondAmount.add(openFeePrice)); |
| | | entrustOrderEntity.setSymbolSku(lotNumber); |
| | | entrustOrderEntity.setEntrustAmount(entrustTotalAmount); |
| | | // 暂默认逐仓 |
| | | entrustOrderEntity.setPositionType(ContractEntrustOrderEntity.POSITION_TYPE_ADD); |
| | | |
| | | int i = contractEntrustOrderDao.insert(entrustOrderEntity); |
| | | |
| | | memberWalletContractDao.increaseWalletContractBalanceById(entrustTotalAmount.negate(), null, entrustOrderEntity.getBondAmount(), walletContract.getId()); |
| | | if (i > 0) { |
| | | |
| | | // 发送委托单队列消息 |
| | | if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_MORE) { |
| | | OrderModel model = new OrderModel(entrustOrderEntity.getId(), RabbitPriceTypeEnum.ENTRUST_OPEN_MORE.getValue(), submitEntrustDto.getEntrustPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), submitEntrustDto.getSymbol()); |
| | | producer.sendPriceOperate(JSONObject.toJSONString(model)); |
| | | |
| | | LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), entrustTotalAmount, walletContract.getAvailableBalance().subtract(entrustTotalAmount), submitEntrustDto.getSymbol(), "委托买涨", "买涨:" + submitEntrustDto.getSymbol()); |
| | | } else { |
| | | OrderModel model = new OrderModel(entrustOrderEntity.getId(), RabbitPriceTypeEnum.ENTRUST_OPEN_LESS.getValue(), submitEntrustDto.getEntrustPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), submitEntrustDto.getSymbol()); |
| | | producer.sendPriceOperate(JSONObject.toJSONString(model)); |
| | | |
| | | LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), entrustTotalAmount, walletContract.getAvailableBalance().subtract(entrustTotalAmount), submitEntrustDto.getSymbol(), "委托买跌", "买跌:" + submitEntrustDto.getSymbol()); |
| | | } |
| | | |
| | | return Result.ok(MessageSourceUtils.getString("result_success_msg")); |
| | | // 逐仓委托 |
| | | if (ContractEntrustOrderEntity.POSITION_TYPE_ADD == memberEntity.getContractPositionType()) { |
| | | return entrustForAdd(submitEntrustDto, memberEntity); |
| | | // 全仓委托 |
| | | } else { |
| | | return Result.fail(MessageSourceUtils.getString("result_fail_msg")); |
| | | return entrustForWhole(submitEntrustDto, memberEntity); |
| | | } |
| | | } |
| | | |
| | | // 委托平仓 (全仓模式) |
| | | if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_CLOSE_MORE || submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_CLOSE_LESS) { |
| | | return Result.fail("功能暂未开放,敬请期待"); |
| | | } |
| | | |
| | | return Result.fail(MessageSourceUtils.getString("result_fail_msg")); |
| | | } |
| | | |
| | | private Result entrustForAdd(SubmitEntrustDto submitEntrustDto, MemberEntity memberEntity) { |
| | | BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(submitEntrustDto.getSymbol()); |
| | | PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); |
| | | |
| | | MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), CoinTypeEnum.USDT.name()); |
| | | |
| | | // 保证金计算 -- 建仓价X规格X手数X(1/杠杆倍率) |
| | | BigDecimal bondAmount = CalculateUtil.getBondAmount(submitEntrustDto.getEntrustPrice(), lotNumber, submitEntrustDto.getSymbolCnt(), submitEntrustDto.getLeverRatio()); |
| | | |
| | | // 开仓手续费 建仓价*规格*手数*手续费率 |
| | | BigDecimal openFeePrice = CalculateUtil.getOpenFeePrice(submitEntrustDto.getEntrustPrice(), lotNumber, submitEntrustDto.getSymbolCnt(), tradeSettingEntity.getFeeRatio()); |
| | | log.info("手续费:{}", openFeePrice); |
| | | |
| | | // 预付款 |
| | | BigDecimal entrustTotalAmount = bondAmount.add(openFeePrice).add(openFeePrice); |
| | | log.info("预付款:{}", entrustTotalAmount); |
| | | |
| | | if (entrustTotalAmount.compareTo(walletContract.getAvailableBalance()) > -1) { |
| | | throw new GlobalException(MessageSourceUtils.getString("member_service_0085")); |
| | | } |
| | | |
| | | ContractEntrustOrderEntityMapper convert = ContractEntrustOrderEntityMapper.INSTANCE; |
| | | ContractEntrustOrderEntity entrustOrderEntity = convert.submitEntrustDtoToEntity(submitEntrustDto); |
| | | entrustOrderEntity.setOrderNo(commonService.generateOrderNo(memberEntity.getId())); |
| | | entrustOrderEntity.setMemberId(memberEntity.getId()); |
| | | entrustOrderEntity.setBondAmount(bondAmount.add(openFeePrice)); |
| | | entrustOrderEntity.setSymbolSku(lotNumber); |
| | | entrustOrderEntity.setEntrustAmount(entrustTotalAmount); |
| | | entrustOrderEntity.setPositionType(ContractEntrustOrderEntity.POSITION_TYPE_ADD); |
| | | |
| | | int i = contractEntrustOrderDao.insert(entrustOrderEntity); |
| | | memberWalletContractDao.increaseWalletContractBalanceById(entrustTotalAmount.negate(), null, entrustOrderEntity.getBondAmount(), walletContract.getId()); |
| | | if (i > 0) { |
| | | |
| | | // 发送委托单队列消息 |
| | | if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_MORE) { |
| | | OrderModel model = new OrderModel(entrustOrderEntity.getId(), RabbitPriceTypeEnum.ENTRUST_OPEN_MORE.getValue(), submitEntrustDto.getEntrustPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), submitEntrustDto.getSymbol()); |
| | | producer.sendPriceOperate(JSONObject.toJSONString(model)); |
| | | |
| | | LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), entrustTotalAmount, walletContract.getAvailableBalance().subtract(entrustTotalAmount), submitEntrustDto.getSymbol(), "委托买涨", "买涨:" + submitEntrustDto.getSymbol()); |
| | | } else { |
| | | OrderModel model = new OrderModel(entrustOrderEntity.getId(), RabbitPriceTypeEnum.ENTRUST_OPEN_LESS.getValue(), submitEntrustDto.getEntrustPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), submitEntrustDto.getSymbol()); |
| | | producer.sendPriceOperate(JSONObject.toJSONString(model)); |
| | | |
| | | LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), entrustTotalAmount, walletContract.getAvailableBalance().subtract(entrustTotalAmount), submitEntrustDto.getSymbol(), "委托买跌", "买跌:" + submitEntrustDto.getSymbol()); |
| | | } |
| | | |
| | | return Result.ok(MessageSourceUtils.getString("result_success_msg")); |
| | | } else { |
| | | return Result.fail(MessageSourceUtils.getString("result_fail_msg")); |
| | | } |
| | | } |
| | | |
| | | private Result entrustForWhole(SubmitEntrustDto submitEntrustDto, MemberEntity memberEntity) { |
| | | BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(submitEntrustDto.getSymbol()); |
| | | PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); |
| | | |
| | | MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), CoinTypeConvert.convertContractTypeToCoin(submitEntrustDto.getSymbol())); |
| | | |
| | | ContractHoldOrderEntity holdOrderEntity = contractHoldOrderDao.selectWholeHoldOrderByOrderType(memberEntity.getId(), submitEntrustDto.getEntrustType()); |
| | | |
| | | BigDecimal bondAmount; |
| | | BigDecimal openFeePrice; |
| | | BigDecimal entrustTotalAmount; |
| | | |
| | | // 保证金计算 -- 建仓价X规格X手数X(1/杠杆倍率) |
| | | bondAmount = CalculateUtil.getBondAmount(submitEntrustDto.getEntrustPrice(), lotNumber, submitEntrustDto.getSymbolCnt(), submitEntrustDto.getLeverRatio()); |
| | | |
| | | // 开仓手续费 建仓价*规格*手数*手续费率 |
| | | openFeePrice = CalculateUtil.getOpenFeePrice(submitEntrustDto.getEntrustPrice(), lotNumber, submitEntrustDto.getSymbolCnt(), tradeSettingEntity.getFeeRatio()); |
| | | log.info("手续费:{}", openFeePrice); |
| | | |
| | | // 预付款 |
| | | entrustTotalAmount = bondAmount.add(openFeePrice).add(openFeePrice); |
| | | log.info("预付款:{}", entrustTotalAmount); |
| | | |
| | | if (entrustTotalAmount.compareTo(walletContract.getAvailableBalance()) > -1) { |
| | | throw new GlobalException(MessageSourceUtils.getString("member_service_0085")); |
| | | } |
| | | |
| | | ContractEntrustOrderEntityMapper convert = ContractEntrustOrderEntityMapper.INSTANCE; |
| | | ContractEntrustOrderEntity entrustOrderEntity = convert.submitEntrustDtoToEntity(submitEntrustDto); |
| | | entrustOrderEntity.setOrderNo(commonService.generateOrderNo(memberEntity.getId())); |
| | | entrustOrderEntity.setMemberId(memberEntity.getId()); |
| | | entrustOrderEntity.setBondAmount(bondAmount.add(openFeePrice)); |
| | | entrustOrderEntity.setSymbolSku(lotNumber); |
| | | entrustOrderEntity.setEntrustAmount(entrustTotalAmount); |
| | | entrustOrderEntity.setPositionType(ContractEntrustOrderEntity.POSITION_TYPE_ALL); |
| | | |
| | | int i = contractEntrustOrderDao.insert(entrustOrderEntity); |
| | | memberWalletContractDao.increaseWalletContractBalanceById(entrustTotalAmount.negate(), null, entrustOrderEntity.getBondAmount(), walletContract.getId()); |
| | | if (i > 0) { |
| | | |
| | | // 发送委托单队列消息 |
| | | if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_MORE) { |
| | | OrderModel model = new OrderModel(entrustOrderEntity.getId(), RabbitPriceTypeEnum.ENTRUST_OPEN_MORE.getValue(), submitEntrustDto.getEntrustPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), submitEntrustDto.getSymbol()); |
| | | producer.sendPriceOperate(JSONObject.toJSONString(model)); |
| | | |
| | | LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), entrustTotalAmount, walletContract.getAvailableBalance().subtract(entrustTotalAmount), submitEntrustDto.getSymbol(), "委托买涨", "买涨:" + submitEntrustDto.getSymbol()); |
| | | } else { |
| | | OrderModel model = new OrderModel(entrustOrderEntity.getId(), RabbitPriceTypeEnum.ENTRUST_OPEN_LESS.getValue(), submitEntrustDto.getEntrustPrice().setScale(8, RoundingMode.HALF_UP).toPlainString(), submitEntrustDto.getSymbol()); |
| | | producer.sendPriceOperate(JSONObject.toJSONString(model)); |
| | | |
| | | LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), entrustTotalAmount, walletContract.getAvailableBalance().subtract(entrustTotalAmount), submitEntrustDto.getSymbol(), "委托买跌", "买跌:" + submitEntrustDto.getSymbol()); |
| | | } |
| | | |
| | | return Result.ok(MessageSourceUtils.getString("result_success_msg")); |
| | | } else { |
| | | return Result.fail(MessageSourceUtils.getString("result_fail_msg")); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public Result findEntrustOrderList(String symbol) { |
| | | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |