| | |
| | | import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity; |
| | | import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity; |
| | | import com.xcong.excoin.modules.contract.entity.ContractOrderEntity; |
| | | import com.xcong.excoin.modules.contract.mapper.ContractEntrustOrderEntityMapper; |
| | | import com.xcong.excoin.modules.contract.mapper.ContractHoldOrderEntityMapper; |
| | | import com.xcong.excoin.modules.contract.service.ContractEntrustOrderService; |
| | | import com.xcong.excoin.modules.contract.service.ContractHoldOrderService; |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | |
| | | * @param |
| | | */ |
| | | public void dealLimitBuyOrder(List<ContractEntrustOrderEntity> orderList) { |
| | | |
| | | //List<CoinsCoinsOrder> orderList = orderMapper.selectOrderByBatchNo(batchno); |
| | | if (CollectionUtils.isNotEmpty(orderList)) { |
| | | ContractHoldOrderEntity contractHoldOrderEntity = null; |
| | |
| | | continue; |
| | | } |
| | | |
| | | contractHoldOrderEntity = new ContractHoldOrderEntity(); |
| | | // 委托单bean转换为持仓单bean |
| | | contractHoldOrderEntity = ContractEntrustOrderEntityMapper.INSTANCE.entrustOrderToHoldOrder(coinsCoinsOrder); |
| | | contractHoldOrderEntity.setId(null); |
| | | Long memId = coinsCoinsOrder.getMemberId(); |
| | | MemberEntity memberEntity = memberService.getById(memId); |
| | | BigDecimal entrustPrice = coinsCoinsOrder.getEntrustPrice(); |
| | | int symbolCnt = coinsCoinsOrder.getSymbolCnt(); |
| | | int type = coinsCoinsOrder.getEntrustType(); |
| | | //开仓价 |
| | | // Double openPrice = coinsCoinsOrder.getOpenPrice().doubleValue(); |
| | | //委托价 |
| | | // Double markPrice = coinsCoinsOrder.getMarkPrice(); |
| | | |
| | | if (type == 1) { |
| | | // 开多 |
| | | contractHoldOrderEntity.setOpeningType(ContractHoldOrderEntity.OPENING_TYPE_MORE); |
| | | } else { |
| | | // 开空 |
| | | contractHoldOrderEntity.setOpeningType(ContractHoldOrderEntity.OPENING_TYPE_LESS); |
| | | |
| | | } |
| | | |
| | | //持仓单赋值 |
| | |
| | | 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)); |
| | | } |
| | | // 扣除手续费 |
| | | //double totalBalance = wallet.getTotalBalance() - coinsCoinsOrder.getClosingPrice(); |
| | | BigDecimal totalBalance = openFeePrice.negate(); |
| | | |
| | | /** TradeSetting tradeSetting = tradeSettingCache.getTradeSetting(); |
| | | |
| | | Double totalPayPrice = new BigDecimal(coinsCoinsOrder.getPrePrice()).add(new BigDecimal(coinsCoinsOrder.getClosingPrice())).add(new BigDecimal(coinsCoinsOrder.getClosingPrice())) |
| | | .setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | totalBalance = totalBalance+(-totalPayPrice*(Double.valueOf(tradeSetting.getSpread())/10000));*/ |
| | | |
| | | contractEntrustOrderService.removeById(coinsCoinsOrder.getId()); |
| | | memberWalletContractService.increaseWalletContractBalanceById(null, totalBalance, null, wallet.getId()); |
| | | // TODO 531 待写 |
| | | calYj(memId, contractOrderEntity.getClosingFeeAmount(), contractOrderEntity, 1); |
| | | calYj(memId, openFeePrice, contractOrderEntity, 1); |
| | | } |
| | | } |
| | | } |