| | |
| | | package com.xcong.excoin.modules.contract.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.alibaba.druid.sql.visitor.functions.If; |
| | | 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.RabbitPriceTypeEnum; |
| | | 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.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.HoldOrderListDto; |
| | | import com.xcong.excoin.modules.contract.parameter.dto.ChangeBondDto; |
| | | import com.xcong.excoin.modules.contract.parameter.dto.ProfitOrLessDto; |
| | | import com.xcong.excoin.modules.contract.parameter.dto.SubmitOrderDto; |
| | | import com.xcong.excoin.modules.contract.parameter.vo.HoldOrderListVo; |
| | | import com.xcong.excoin.modules.contract.service.ContractHoldOrderService; |
| | | import com.xcong.excoin.modules.member.dao.MemberWalletContractDao; |
| | | import com.xcong.excoin.modules.member.entity.AgentReturnEntity; |
| | |
| | | import com.xcong.excoin.rabbit.producer.OrderProducer; |
| | | import com.xcong.excoin.utils.*; |
| | | import com.xcong.excoin.rabbit.pricequeue.OrderModel; |
| | | import jnr.a64asm.Mem; |
| | | 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.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | |
| | | List<ContractHoldOrderEntity> list = contractHoldOrderDao.selectHoldOrderListByMemberId(memberEntity.getId()); |
| | | if (CollUtil.isNotEmpty(list)) { |
| | | BigDecimal totalProfitOrLoss = BigDecimal.ZERO; |
| | | List<HoldOrderListDto> resultList = new ArrayList<>(); |
| | | List<HoldOrderListVo> resultList = new ArrayList<>(); |
| | | for (ContractHoldOrderEntity holdOrderEntity : list) { |
| | | HoldOrderListDto holdOrderListDto = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToDto(holdOrderEntity); |
| | | HoldOrderListVo holdOrderListVo = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToDto(holdOrderEntity); |
| | | String symbol = holdOrderEntity.getSymbol(); |
| | | // 获取最新价 |
| | | BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol))); |
| | |
| | | canAddMaxBond = BigDecimal.ZERO; |
| | | } |
| | | |
| | | holdOrderListDto.setCanAddMaxBond(canAddMaxBond); |
| | | holdOrderListDto.setReturnRate(returnRate); |
| | | holdOrderListDto.setProfitOrLoss(rewardRatio); |
| | | resultList.add(holdOrderListDto); |
| | | holdOrderListVo.setCanAddMaxBond(canAddMaxBond); |
| | | holdOrderListVo.setReturnRate(returnRate); |
| | | holdOrderListVo.setProfitOrLoss(rewardRatio); |
| | | resultList.add(holdOrderListVo); |
| | | totalProfitOrLoss = totalProfitOrLoss.add(rewardRatio); |
| | | } |
| | | |
| | |
| | | producer.sendCloseTrade(JSONObject.toJSONString(ids)); |
| | | return Result.ok("平仓成功"); |
| | | } |
| | | |
| | | @Override |
| | | public Result setTargetProfitOrLess(ProfitOrLessDto profitOrLessDto) { |
| | | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |
| | | ContractHoldOrderEntity holdOrderEntity = contractHoldOrderDao.selectHoldOrderByMemberIdAndId(memberEntity.getId(), profitOrLessDto.getId()); |
| | | if (holdOrderEntity == null) { |
| | | return Result.fail("订单不存在"); |
| | | } |
| | | |
| | | // 获取最新价 |
| | | BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(holdOrderEntity.getSymbol()))); |
| | | // 开仓价 |
| | | BigDecimal openPrice = holdOrderEntity.getOpeningPrice(); |
| | | // 设置的止盈止损价 |
| | | BigDecimal price = profitOrLessDto.getPrice(); |
| | | |
| | | // 开多 |
| | | if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { |
| | | if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) { |
| | | // 当前价大于开仓价 |
| | | if (newPrice.compareTo(openPrice) > 0) { |
| | | // 如果止盈价小于当前价 |
| | | if (price.compareTo(newPrice) < 0) { |
| | | return Result.fail("止盈价必须高于当前价"); |
| | | } |
| | | } else { |
| | | if (price.compareTo(openPrice) < 0) { |
| | | return Result.fail("止盈价必须高于开仓价"); |
| | | } |
| | | } |
| | | } else { |
| | | if (newPrice.compareTo(openPrice) > 0) { |
| | | if (price.compareTo(openPrice) > 0) { |
| | | return Result.fail("止损价必须低于开仓价"); |
| | | } |
| | | } else { |
| | | if (price.compareTo(newPrice) > 0) { |
| | | return Result.fail("止损价必须低于当前价"); |
| | | } |
| | | } |
| | | } |
| | | // 开空 |
| | | } else { |
| | | if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) { |
| | | if (newPrice.compareTo(openPrice) > 0) { |
| | | if (price.compareTo(openPrice) > 0) { |
| | | return Result.fail("止损价必须低于开仓价"); |
| | | } |
| | | } else { |
| | | if (price.compareTo(newPrice) > 0) { |
| | | return Result.fail("止损价必须低于当前价"); |
| | | } |
| | | } |
| | | } else { |
| | | if (newPrice.compareTo(openPrice) > 0) { |
| | | if (price.compareTo(newPrice) < 0) { |
| | | return Result.fail("止损价必须高于当前价"); |
| | | } |
| | | } else { |
| | | if (price.compareTo(openPrice) < 0) { |
| | | return Result.fail("止损价必须高于开仓价"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) { |
| | | holdOrderEntity.setStopProfitPrice(price); |
| | | } else { |
| | | holdOrderEntity.setStopLossPrice(price); |
| | | } |
| | | |
| | | int i = contractHoldOrderDao.updateById(holdOrderEntity); |
| | | if (i > 0) { |
| | | OrderModel model = null; |
| | | if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { |
| | | // 开多止盈 |
| | | if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) { |
| | | model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_STOP_PROFIT.getValue(), price.toPlainString(), holdOrderEntity.getSymbol()); |
| | | // 开多止损 |
| | | } else { |
| | | model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_STOP_LESS.getValue(), price.toPlainString(), holdOrderEntity.getSymbol()); |
| | | } |
| | | } else { |
| | | // 开空止盈 |
| | | if (ProfitOrLessDto.TYPE_PROFIT == profitOrLessDto.getType()) { |
| | | model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_PROFIT.getValue(), price.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); |
| | | // 开空止损 |
| | | } else { |
| | | model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_LESS.getValue(), price.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); |
| | | } |
| | | } |
| | | producer.sendPriceOperate(JSONObject.toJSONString(model)); |
| | | return Result.ok("设置成功"); |
| | | } |
| | | |
| | | return Result.fail("设置失败"); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public Result changeBond(ChangeBondDto changeBondDto) { |
| | | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |
| | | ContractHoldOrderEntity holdOrderEntity = contractHoldOrderDao.selectHoldOrderByMemberIdAndId(memberEntity.getId(), changeBondDto.getId()); |
| | | if (holdOrderEntity == null) { |
| | | return Result.fail("订单不存在"); |
| | | } |
| | | |
| | | MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), CoinTypeEnum.USDT.name()); |
| | | |
| | | // 增加保证金 |
| | | if (ChangeBondDto.TYPE_ADD == changeBondDto.getType()) { |
| | | if (changeBondDto.getAmount().compareTo(walletContract.getAvailableBalance()) > 0) { |
| | | return Result.fail("可用余额不足"); |
| | | } |
| | | walletContract.setAvailableBalance(walletContract.getAvailableBalance().subtract(changeBondDto.getAmount())); |
| | | walletContract.setFrozenBalance(walletContract.getFrozenBalance().add(changeBondDto.getAmount())); |
| | | holdOrderEntity.setBondAmount(holdOrderEntity.getBondAmount().add(changeBondDto.getAmount())); |
| | | // 减少保证金 |
| | | } else { |
| | | if (holdOrderEntity.getBondAmount().subtract(holdOrderEntity.getPrePaymentAmount()).subtract(changeBondDto.getAmount()).compareTo(BigDecimal.ZERO) < 0) { |
| | | return Result.fail("超出保证金最大减少金额"); |
| | | } |
| | | walletContract.setAvailableBalance(walletContract.getAvailableBalance().add(changeBondDto.getAmount())); |
| | | walletContract.setFrozenBalance(walletContract.getFrozenBalance().subtract(changeBondDto.getAmount())); |
| | | holdOrderEntity.setBondAmount(holdOrderEntity.getBondAmount().subtract(changeBondDto.getAmount())); |
| | | } |
| | | |
| | | BigDecimal forceClosingPrice = CalculateUtil.getForceSetPrice(holdOrderEntity.getBondAmount(), holdOrderEntity.getOpeningPrice(), holdOrderEntity.getSymbolCnt(), holdOrderEntity.getSymbolSku(), holdOrderEntity.getOpeningType(), memberEntity); |
| | | holdOrderEntity.setForceClosingPrice(forceClosingPrice); |
| | | holdOrderEntity.setOperateNo(holdOrderEntity.getOperateNo() + 1); |
| | | |
| | | int i = contractHoldOrderDao.updateById(holdOrderEntity); |
| | | int j = memberWalletContractDao.updateById(walletContract); |
| | | |
| | | OrderModel model = null; |
| | | // 开多 |
| | | if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) { |
| | | model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_MORE_BOMB.getValue(), forceClosingPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); |
| | | // 开空 |
| | | } else { |
| | | model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_BOMB.getValue(), forceClosingPrice.setScale(8, RoundingMode.HALF_UP).toPlainString(), holdOrderEntity.getSymbol()); |
| | | } |
| | | producer.sendPriceOperate(JSONObject.toJSONString(model)); |
| | | |
| | | if (i > 0 && j > 0) { |
| | | return Result.ok("调整成功"); |
| | | } |
| | | return Result.fail("调整失败"); |
| | | } |
| | | } |