|  |  |  | 
|---|
|  |  |  | 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.dao.ContractOrderDao; | 
|---|
|  |  |  | import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity; | 
|---|
|  |  |  | 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 lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.math.RoundingMode; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author wzy | 
|---|
|  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private ContractEntrustOrderDao contractEntrustOrderDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private MemberWalletContractDao memberWalletContractDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private RedisUtils redisUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private CacheSettingUtils cacheSettingUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private ContractOrderDao contractOrderDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private CommonService commonService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private OrderProducer producer; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Result addContractEntrustOrder(SubmitEntrustDto submitEntrustDto) { | 
|---|
|  |  |  | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 获取最新价 | 
|---|
|  |  |  | BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(submitEntrustDto.getSymbol()))); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 委托开仓 | 
|---|
|  |  |  | if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_MORE || submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_LESS) { | 
|---|
|  |  |  | // 开多委托价不能大于当前价 | 
|---|
|  |  |  | if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_MORE) { | 
|---|
|  |  |  | if (submitEntrustDto.getEntrustPrice().compareTo(newPrice) > -1) { | 
|---|
|  |  |  | return Result.fail("委托价不能大于当前价"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 开空委托价不能小于当前价 | 
|---|
|  |  |  | if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_LESS) { | 
|---|
|  |  |  | if (submitEntrustDto.getEntrustPrice().compareTo(newPrice) < 1) { | 
|---|
|  |  |  | return Result.fail("委托价不能小于当前价"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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((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("可用余额不足"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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)); | 
|---|
|  |  |  | } 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("委托失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 委托平仓 | 
|---|
|  |  |  | // 委托平仓 (全仓模式) | 
|---|
|  |  |  | if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_CLOSE_MORE || submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_CLOSE_LESS) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return Result.fail("功能暂未开放,敬请期待"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return Result.fail("未知错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 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 | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public Result cancelEntrustOrder(Long id) { | 
|---|
|  |  |  | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 查询该委托单是否为该用户所有 | 
|---|
|  |  |  | ContractEntrustOrderEntity entrustOrderEntity = contractEntrustOrderDao.selectEntrustOrderByIdAndMemberId(id, memberEntity.getId()); | 
|---|
|  |  |  | if (entrustOrderEntity == null) { | 
|---|
|  |  |  | return Result.fail("该委托单不存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MemberWalletContractEntity walletContractEntity = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), MemberWalletCoinEnum.WALLETCOINCODE.getValue()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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.setOrderStatus(ContractOrderEntity.ORDER_STATUS_CANCEL); | 
|---|
|  |  |  | int i = contractOrderDao.insert(orderEntity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | contractEntrustOrderDao.deleteById(entrustOrderEntity.getId()); | 
|---|
|  |  |  | if (i > 0) { | 
|---|
|  |  |  | return Result.ok("撤销成功"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return Result.fail("撤销失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<ContractEntrustOrderEntity> selectEntrustOrderListByIds(List<Long> list) { | 
|---|
|  |  |  | return contractEntrustOrderDao.selectEntrustOrderListByIds(list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|