| | |
| | | import com.xcong.excoin.rabbit.producer.OrderProducer; |
| | | import com.xcong.excoin.utils.CacheSettingUtils; |
| | | import com.xcong.excoin.utils.CalculateUtil; |
| | | import com.xcong.excoin.utils.ThreadPoolUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.collections.CollectionUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | |
| | | /** |
| | | * @author helius |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class OrderWebsocketServiceImpl { |
| | | |
| | |
| | | /** |
| | | * 开多止盈 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void dealForMoreStopPro(List<ContractHoldOrderEntity> orderList, List<OrderModel> list) { |
| | | if (CollectionUtils.isNotEmpty(orderList)) { |
| | | Map<Long, BigDecimal> modelMap = new HashMap<Long, BigDecimal>(); |
| | |
| | | System.out.println("执行操作"); |
| | | // 止盈价 |
| | | String symbol = order.getSymbol(); |
| | | // 本次需要退回的预付款 |
| | | BigDecimal prePrice = order.getPrePaymentAmount(); |
| | | // 本次需要退回的保证金 |
| | | BigDecimal prePrice = order.getBondAmount(); |
| | | Long memberId = order.getMemberId(); |
| | | MemberWalletContractEntity wallet = memberWalletContractService.findWalletContractByMemberIdAndSymbol(memberId, "USDT"); |
| | | if (wallet != null) { |
| | |
| | | // 盈亏额度= (当前的币种的平仓价-下单时的建仓价)*购买的手数/规格*倍率 |
| | | BigDecimal profitLossPrice = (closePrice |
| | | .subtract(order.getOpeningPrice())) |
| | | .multiply(new BigDecimal(currentFlat + "")) |
| | | .multiply(new BigDecimal(currentFlat)) |
| | | .multiply(symbolSku).setScale(8, BigDecimal.ROUND_DOWN); |
| | | MemberEntity memberEntity = memberService.getById(memberId); |
| | | |
| | |
| | | } |
| | | } |
| | | //回报率 |
| | | BigDecimal returnRate = profitLossPrice.divide((order.getPrePaymentAmount().subtract(contractOrderEntity.getClosingFeeAmount())), 8, BigDecimal.ROUND_DOWN); |
| | | BigDecimal returnRate = profitLossPrice.divide((order.getPrePaymentAmount().subtract(contractOrderEntity.getOpeningFeeAmount())), 8, BigDecimal.ROUND_DOWN); |
| | | contractOrderEntity.setRewardAmount(profitLossPrice); |
| | | contractOrderEntity.setRewardRatio(returnRate); |
| | | contractOrderEntity.setClosingFeeAmount(order.getOpeningFeeAmount()); |
| | |
| | | contractOrderEntity.setClosingType(6); |
| | | BigDecimal totalReturn = BigDecimal.ZERO; |
| | | contractOrderService.save(contractOrderEntity); |
| | | |
| | | contractEntrustOrderService.removeById(order.getId()); |
| | | // 将需要退回的减去手续费 |
| | | BigDecimal needReturn = prePrice.add(profitLossPrice); |
| | | //总退回金额=保证金+收益-手续费 |
| | | totalReturn = needReturn.subtract(contractOrderEntity.getClosingFeeAmount()); |
| | | // 更新钱包 |
| | | // 总的是收益-平仓手续费 |
| | | BigDecimal totalBalance = profitLossPrice.subtract(contractOrderEntity.getClosingFeeAmount()); |
| | | memberWalletContractService.increaseWalletContractBalanceById(totalBalance, totalReturn, null, wallet.getId()); |
| | |
| | | insertAccountFlow(order, wallet, profitLossPrice, "止盈平仓"); |
| | | |
| | | //返佣 |
| | | calYj(order.getMemberId(), order.getOpeningFeeAmount(), contractOrderEntity, 2); |
| | | ThreadPoolUtils.calReturnMoney(order.getMemberId(), order.getOpeningFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE); |
| | | } |
| | | } |
| | | } |
| | |
| | | contractHoldOrderEntity.setForceClosingPrice(forceSetPrice); |
| | | contractHoldOrderEntity.setLeverRatio(coinsCoinsOrder.getLeverRatio()); |
| | | contractHoldOrderEntity.setOpeningPrice(entrustPrice); |
| | | contractHoldOrderEntity.setTradeType(ContractHoldOrderEntity.TRADE_TYPE_LIMIT); |
| | | contractHoldOrderService.save(contractHoldOrderEntity); |
| | | |
| | | // 需要一个历史插入 |
| | | ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(contractHoldOrderEntity); |
| | | contractOrderEntity.setEntrustOpeningPrice(coinsCoinsOrder.getEntrustPrice()); |
| | | contractOrderEntity.setEntrustTime(coinsCoinsOrder.getCreateTime()); |
| | | contractOrderEntity.setOpeningTime(new Date()); |
| | | contractOrderEntity.setId(null); |
| | | contractOrderService.save(contractOrderEntity); |
| | | // 发送爆仓的队列 |
| | |
| | | producer.sendPriceOperate(JSONObject.toJSONString(model)); |
| | | } |
| | | // 扣除手续费 |
| | | BigDecimal totalBalance = wallet.getTotalBalance().subtract(openFeePrice); |
| | | |
| | | BigDecimal totalBalance = openFeePrice.negate(); |
| | | contractEntrustOrderService.removeById(coinsCoinsOrder.getId()); |
| | | memberWalletContractService.increaseWalletContractBalanceById(null, totalBalance, null, wallet.getId()); |
| | | // TODO 531 待写 |
| | | calYj(memId, openFeePrice, contractOrderEntity, 1); |