| | |
| | | |
| | | 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.response.Result; |
| | | import com.xcong.excoin.common.system.service.CommonService; |
| | | import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao; |
| | | import com.xcong.excoin.modules.contract.dao.ContractOrderDao; |
| | | import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity; |
| | | import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity; |
| | | import com.xcong.excoin.modules.contract.entity.ContractOrderEntity; |
| | | import com.xcong.excoin.modules.contract.mapper.ContractHoldOrderEntityMapper; |
| | | import com.xcong.excoin.modules.contract.parameter.dto.SubmitOrderDto; |
| | | import com.xcong.excoin.modules.contract.service.ContractHoldOrderService; |
| | | import com.xcong.excoin.modules.member.dao.MemberWalletContractDao; |
| | | import com.xcong.excoin.modules.member.entity.MemberEntity; |
| | | import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity; |
| | | import com.xcong.excoin.modules.platform.dao.TradeSettingDao; |
| | | import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity; |
| | | import com.xcong.excoin.utils.CacheSettingUtils; |
| | | import com.xcong.excoin.utils.CalculateUtil; |
| | | import com.xcong.excoin.utils.CoinTypeConvert; |
| | | import com.xcong.excoin.utils.RedisUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | |
| | | * @author wzy |
| | | * @date 2020-05-27 |
| | | **/ |
| | | @Slf4j |
| | | @Service |
| | | public class ContractHoldOrderServiceImpl extends ServiceImpl<ContractHoldOrderDao, ContractHoldOrderEntity> implements ContractHoldOrderService { |
| | | |
| | | @Resource |
| | |
| | | |
| | | @Resource |
| | | private ContractOrderDao contractOrderDao; |
| | | |
| | | @Resource |
| | | private CommonService commonService; |
| | | |
| | | @Resource |
| | | private MemberWalletContractDao memberWalletContractDao; |
| | | |
| | | @Resource |
| | | private CacheSettingUtils cacheSettingUtils; |
| | | |
| | | @Resource |
| | | private RedisUtils redisUtils; |
| | |
| | | |
| | | // 获取最新价 |
| | | BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(submitOrderDto.getSymbol()))); |
| | | return null; |
| | | |
| | | MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), CoinTypeEnum.USDT.name()); |
| | | |
| | | PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); |
| | | |
| | | // 规格 |
| | | BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(submitOrderDto.getSymbol()); |
| | | |
| | | // 开仓价 |
| | | BigDecimal openingPrice = BigDecimal.ZERO; |
| | | |
| | | // 开多 |
| | | if (submitOrderDto.getOrderType() == ContractHoldOrderEntity.OPENING_TYPE_MORE) { |
| | | // 市场价*(1 + (点差/10000)) |
| | | openingPrice = newPrice.multiply(BigDecimal.ONE.add(tradeSettingEntity.getSpread().divide(new BigDecimal(10000)))).setScale(8, BigDecimal.ROUND_DOWN); |
| | | |
| | | // 开空 |
| | | } else if (submitOrderDto.getOrderType() == ContractHoldOrderEntity.OPENING_TYPE_LESS) { |
| | | // 市场价*(1 - (点差/10000)) |
| | | openingPrice = newPrice.multiply(BigDecimal.ONE.subtract(tradeSettingEntity.getSpread().divide(new BigDecimal(10000)))).setScale(8, BigDecimal.ROUND_DOWN); |
| | | } else { |
| | | return Result.fail("未知类型"); |
| | | } |
| | | |
| | | log.info("开仓价:{}", openingPrice); |
| | | // 开仓手续费 建仓价*规格*手数*手续费率 |
| | | BigDecimal openFeePrice = openingPrice.multiply(lotNumber) |
| | | .multiply(new BigDecimal(submitOrderDto.getSymbolCnt())) |
| | | .multiply(tradeSettingEntity.getFeeRatio().divide(new BigDecimal(100))) |
| | | .setScale(8, BigDecimal.ROUND_DOWN); |
| | | log.info("开仓手续费:{}", openFeePrice); |
| | | |
| | | // 保证金 建仓价*规格*手数*(1/杠杆倍率) |
| | | BigDecimal bondAmount = openingPrice.multiply(lotNumber).multiply(new BigDecimal(submitOrderDto.getSymbolCnt())) |
| | | .multiply(BigDecimal.ONE.divide(new BigDecimal(submitOrderDto.getLeverRatio()))) |
| | | .setScale(8, BigDecimal.ROUND_DOWN); |
| | | log.info("保证金:{}", bondAmount); |
| | | |
| | | // 预付款为 --> 保证金+开仓手续费+平仓手续费 (开仓平仓手续费相同) |
| | | BigDecimal prePaymentAmount = bondAmount.add(openFeePrice).add(openFeePrice); |
| | | log.info("预付款:{}", prePaymentAmount); |
| | | |
| | | if (prePaymentAmount.compareTo(walletContract.getAvailableBalance()) > -1) { |
| | | return Result.fail("可用金额不足"); |
| | | } |
| | | |
| | | // 预估强平价 |
| | | BigDecimal forceClosingPrice = CalculateUtil.getForceSetPrice(bondAmount, openingPrice, submitOrderDto.getSymbolCnt(), lotNumber, submitOrderDto.getOrderType(), memberEntity); |
| | | log.info("强平价:{}", forceClosingPrice); |
| | | |
| | | ContractHoldOrderEntity holdOrderEntity = new ContractHoldOrderEntity(); |
| | | holdOrderEntity.setMemberId(memberEntity.getId()); |
| | | holdOrderEntity.setOrderNo(commonService.generateOrderNo(memberEntity.getId())); |
| | | holdOrderEntity.setPotionType(ContractEntrustOrderEntity.POSITION_TYPE_ADD); |
| | | holdOrderEntity.setTradeType(ContractHoldOrderEntity.TRADE_TYPE_MARK); |
| | | holdOrderEntity.setSymbol(submitOrderDto.getSymbol()); |
| | | holdOrderEntity.setSymbolCnt(submitOrderDto.getSymbolCnt()); |
| | | holdOrderEntity.setSymbolSku(lotNumber); |
| | | holdOrderEntity.setLeverRatio(submitOrderDto.getLeverRatio()); |
| | | holdOrderEntity.setForceClosingPrice(forceClosingPrice); |
| | | holdOrderEntity.setOpeningFeeAmount(openFeePrice); |
| | | holdOrderEntity.setOpeningPrice(openingPrice); |
| | | holdOrderEntity.setOpeningType(submitOrderDto.getOrderType()); |
| | | holdOrderEntity.setMarkPrice(newPrice); |
| | | holdOrderEntity.setIsCanClosing(ContractHoldOrderEntity.ORDER_CAN_CLOSING_Y); |
| | | holdOrderEntity.setPrePaymentAmount(prePaymentAmount); |
| | | holdOrderEntity.setBondAmount(bondAmount); |
| | | |
| | | ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(holdOrderEntity); |
| | | |
| | | contractHoldOrderDao.insert(holdOrderEntity); |
| | | int i = contractOrderDao.insert(contractOrderEntity); |
| | | walletContract.setAvailableBalance(walletContract.getAvailableBalance().subtract(prePaymentAmount)); |
| | | walletContract.setFrozenBalance(walletContract.getFrozenBalance().add(prePaymentAmount)); |
| | | memberWalletContractDao.updateById(walletContract); |
| | | if (i > 0) { |
| | | return Result.ok("success"); |
| | | } |
| | | return Result.fail("fail"); |
| | | } |
| | | } |