From a0b6145467388f025c43a53a3d74c4e9741ec847 Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Tue, 02 Jun 2020 11:47:39 +0800 Subject: [PATCH] add target profitOrLess interface --- src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 110 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java b/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java index f911e60..8f2c8e0 100644 --- a/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java +++ b/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java @@ -1,10 +1,12 @@ 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; @@ -13,8 +15,9 @@ 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.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; @@ -25,6 +28,7 @@ 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; @@ -32,6 +36,7 @@ import javax.annotation.Resource; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -179,9 +184,9 @@ 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))); @@ -222,10 +227,10 @@ 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); } @@ -275,4 +280,102 @@ 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.toPlainString(), holdOrderEntity.getSymbol()); + // 开空止损 + } else { + model = new OrderModel(holdOrderEntity.getId(), RabbitPriceTypeEnum.CLOSE_LESS_STOP_LESS.getValue(), price.toPlainString(), holdOrderEntity.getSymbol()); + } + } + producer.sendPriceOperate(JSONObject.toJSONString(model)); + return Result.ok("设置成功"); + } + + return Result.fail("设置失败"); + } } -- Gitblit v1.9.1