|  |  |  | 
|---|
|  |  |  | package com.xcong.excoin.modules.contract.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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.MemberWalletCoinEnum; | 
|---|
|  |  |  | import com.xcong.excoin.common.enumerates.RabbitPriceTypeEnum; | 
|---|
|  |  |  | import com.xcong.excoin.common.response.Result; | 
|---|
|  |  |  | import com.xcong.excoin.common.system.service.CommonService; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.dao.ContractEntrustOrderDao; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.entity.ContractOrderEntity; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.mapper.ContractEntrustOrderEntityMapper; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.parameter.dto.SubmitEntrustDto; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.parameter.vo.ContractEntrustVo; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.service.ContractEntrustOrderService; | 
|---|
|  |  |  | 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.entity.PlatformTradeSettingEntity; | 
|---|
|  |  |  | import com.xcong.excoin.rabbit.pricequeue.OrderModel; | 
|---|
|  |  |  | import com.xcong.excoin.rabbit.producer.OrderProducer; | 
|---|
|  |  |  | import com.xcong.excoin.utils.CacheSettingUtils; | 
|---|
|  |  |  | import com.xcong.excoin.utils.CoinTypeConvert; | 
|---|
|  |  |  | import com.xcong.excoin.utils.RedisUtils; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.math.RoundingMode; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author wzy | 
|---|
|  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private CommonService commonService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private OrderProducer producer; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Result addContractEntrustOrder(SubmitEntrustDto submitEntrustDto) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MemberWalletContractEntity walletContract = memberWalletContractDao.selectById(memberEntity.getId()); | 
|---|
|  |  |  | // 委托总额 | 
|---|
|  |  |  | BigDecimal entrustTotalAmount = submitEntrustDto.getEntrustPrice().multiply(BigDecimal.valueOf(submitEntrustDto.getSymbolCnt())).setScale(8, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(submitEntrustDto.getSymbol()); | 
|---|
|  |  |  | PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 保证金计算 -- 建仓价X规格X手数X(1/杠杆倍率) | 
|---|
|  |  |  | BigDecimal bondAmount = entrustTotalAmount.multiply(lotNumber).multiply((BigDecimal.ONE.divide(BigDecimal.valueOf(submitEntrustDto.getLeverRatio()), 8, BigDecimal.ROUND_DOWN))); | 
|---|
|  |  |  | BigDecimal bondAmount = submitEntrustDto.getEntrustPrice().multiply(lotNumber).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.add(bondAmount).compareTo(walletContract.getAvailableBalance()) > -1) { | 
|---|
|  |  |  | return Result.fail("可用余额不足"); | 
|---|
|  |  |  | 
|---|
|  |  |  | ContractEntrustOrderEntity entrustOrderEntity = convert.submitEntrustDtoToEntity(submitEntrustDto); | 
|---|
|  |  |  | entrustOrderEntity.setOrderNo(commonService.generateOrderNo(memberEntity.getId())); | 
|---|
|  |  |  | entrustOrderEntity.setMemberId(memberEntity.getId()); | 
|---|
|  |  |  | entrustOrderEntity.setBondAmount(bondAmount); | 
|---|
|  |  |  | entrustOrderEntity.setBondAmount(bondAmount.add(openFeePrice)); | 
|---|
|  |  |  | entrustOrderEntity.setSymbolSku(lotNumber); | 
|---|
|  |  |  | entrustOrderEntity.setEntrustAmount(entrustTotalAmount); | 
|---|
|  |  |  | // 暂默认逐仓 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int i = contractEntrustOrderDao.insert(entrustOrderEntity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | walletContract.setFrozenBalance(walletContract.getFrozenBalance().add(entrustTotalAmount.add(bondAmount))); | 
|---|
|  |  |  | walletContract.setAvailableBalance(walletContract.getAvailableBalance().subtract(entrustTotalAmount.add(bondAmount))); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | memberWalletContractDao.updateById(walletContract); | 
|---|
|  |  |  | 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)); | 
|---|
|  |  |  | } 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)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return Result.ok("委托成功"); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | return Result.fail("委托失败"); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Result findEntrustOrderList() { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | public Result findEntrustOrderList(String symbol) { | 
|---|
|  |  |  | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); | 
|---|
|  |  |  | List<ContractEntrustOrderEntity> list = contractEntrustOrderDao.selectEntrustOrderListByMemberIdAndSymbol(memberEntity.getId(), symbol); | 
|---|
|  |  |  | List<ContractEntrustVo> resultList = ContractEntrustOrderEntityMapper.INSTANCE.entityListToVoList(list); | 
|---|
|  |  |  | return Result.ok(resultList); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MemberWalletContractEntity walletContractEntity = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), MemberWalletCoinEnum.WALLETCOINCODE.getValue()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | BigDecimal total = entrustOrderEntity.getEntrustAmount().add(entrustOrderEntity.getBondAmount()); | 
|---|
|  |  |  | log.info("{}", total); | 
|---|
|  |  |  | log.info("{}", walletContractEntity.getAvailableBalance()); | 
|---|
|  |  |  | walletContractEntity.setAvailableBalance(walletContractEntity.getAvailableBalance().add(total)); | 
|---|
|  |  |  | walletContractEntity.setFrozenBalance(walletContractEntity.getFrozenBalance().subtract(total)); | 
|---|
|  |  |  | memberWalletContractDao.updateById(walletContractEntity); | 
|---|
|  |  |  | BigDecimal total = entrustOrderEntity.getEntrustAmount(); | 
|---|
|  |  |  | memberWalletContractDao.increaseWalletContractBalanceById(total, null, entrustOrderEntity.getBondAmount().negate(), walletContractEntity.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ContractOrderEntity orderEntity = ContractEntrustOrderEntityMapper.INSTANCE.entrustOrderToOrder(entrustOrderEntity); | 
|---|
|  |  |  | orderEntity.setTradeType(ContractOrderEntity.TRADE_TYPE_MARK_PRICE); | 
|---|
|  |  |  | orderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CANCEL); | 
|---|
|  |  |  | orderEntity.setOrderStatus(ContractOrderEntity.ORDER_STATUS_CANCEL); | 
|---|
|  |  |  | int i = contractOrderDao.insert(orderEntity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | contractEntrustOrderDao.deleteById(entrustOrderEntity.getId()); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return Result.fail("撤销失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<ContractEntrustOrderEntity> selectEntrustOrderListByIds(List<Long> list) { | 
|---|
|  |  |  | return contractEntrustOrderDao.selectEntrustOrderListByIds(list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|