| | |
| | | import com.xcong.excoin.common.enumerates.CoinTypeEnum; |
| | | import com.xcong.excoin.common.enumerates.OrderClosingTypeEnum; |
| | | import com.xcong.excoin.common.system.service.CommonService; |
| | | import com.xcong.excoin.modules.contract.dao.ContractEntrustOrderDao; |
| | | import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao; |
| | | import com.xcong.excoin.modules.contract.dao.ContractOrderDao; |
| | | import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity; |
| | |
| | | import com.xcong.excoin.modules.member.entity.MemberSettingEntity; |
| | | 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.utils.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | |
| | | |
| | | @Resource |
| | | private MemberDao memberDao; |
| | | |
| | | @Resource |
| | | private OrderWebsocketServiceImpl orderWebsocketService; |
| | | |
| | | @Resource |
| | | private ContractHoldOrderDao contractHoldOrderDao; |
| | | |
| | | @Resource |
| | | private ContractOrderDao contractOrderDao; |
| | | |
| | | @Resource |
| | | private CommonService commonService; |
| | | |
| | | @Resource |
| | | private MemberWalletContractDao memberWalletContractDao; |
| | | |
| | | @Resource |
| | | private CacheSettingUtils cacheSettingUtils; |
| | | |
| | | @Resource |
| | | private RedisUtils redisUtils; |
| | | @Resource |
| | | private MemberSettingDao memberSettingDao; |
| | | |
| | | @Resource |
| | | private FollowFollowerOrderRelationDao followFollowerOrderRelationDao; |
| | | @Resource |
| | |
| | | private FollowTraderProfitDetailDao followTraderProfitDetailDao; |
| | | @Resource |
| | | private FollowFollowerProfitDao followFollowerProfitDao; |
| | | @Resource |
| | | private ContractEntrustOrderDao contractEntrustOrderDao; |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | |
| | | |
| | | MemberEntity memberEntity = memberDao.selectById(holdOrderEntity.getMemberId()); |
| | | |
| | | MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(holdOrderEntity.getMemberId(), CoinTypeEnum.USDT.name()); |
| | | MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(holdOrderEntity.getMemberId(), CoinTypeConvert.convertContractTypeToCoin(symbol)); |
| | | if (walletContract != null) { |
| | | BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(symbol); |
| | | // 盈亏 |
| | |
| | | BigDecimal bondAmount = CalculateUtil.getBondAmount(holdOrderEntity.getOpeningPrice(), lotNumber, closeCnt, holdOrderEntity.getLeverRatio()); |
| | | |
| | | log.info("bondAmount:{}", bondAmount); |
| | | // 平仓手续费 |
| | | BigDecimal fee = holdOrderEntity.getOpeningFeeAmount().divide(BigDecimal.valueOf(holdOrderEntity.getSymbolCnt()), 8, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(closeCnt)); |
| | | // 平仓手续费 TODO 可能需要修复手续费 |
| | | BigDecimal fee = BigDecimal.ZERO; |
| | | |
| | | if (holdOrderEntity.getSymbolCnt() != 0) { |
| | | fee = holdOrderEntity.getOpeningFeeAmount().divide(BigDecimal.valueOf(holdOrderEntity.getSymbolCnt()), 8, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(closeCnt)); |
| | | } else { |
| | | fee = holdOrderEntity.getOpeningFeeAmount(); |
| | | } |
| | | log.info("fee:{}", fee); |
| | | ContractOrderEntity contractOrderEntity = ContractHoldOrderEntityMapper.INSTANCE.holdOrderToOrder(holdOrderEntity); |
| | | contractOrderEntity.setId(null); |
| | |
| | | contractOrderEntity.setClosingFeeAmount(fee); |
| | | contractOrderDao.insert(contractOrderEntity); |
| | | |
| | | if (holdOrderEntity.getSymbolCntSale() - closeCnt != 0) { |
| | | holdOrderEntity.setSymbolCntSale(holdOrderEntity.getSymbolCntSale() - closeCnt); |
| | | if (holdOrderEntity.getSymbolCntSale() != 0) { |
| | | holdOrderEntity.setOpeningFeeAmount(holdOrderEntity.getOpeningFeeAmount().subtract(fee)); |
| | | holdOrderEntity.setBondAmount(holdOrderEntity.getBondAmount().subtract(bondAmount)); |
| | | contractHoldOrderDao.updateById(holdOrderEntity); |
| | | } else { |
| | | contractHoldOrderDao.deleteById(holdOrderEntity.getId()); |
| | | } |
| | | |
| | | BigDecimal changeAmount = profitOrLoss.add(bondAmount).subtract(fee); |
| | | BigDecimal changeAmount = profitOrLoss.add(bondAmount); |
| | | log.info("changeAmount : {}", changeAmount); |
| | | |
| | | memberWalletContractDao.increaseWalletContractBalanceById(changeAmount, profitOrLoss.subtract(fee), null, walletContract.getId()); |
| | | BigDecimal total = profitOrLoss.subtract(fee); |
| | | log.info("totalMoney : {}", total); |
| | | |
| | | memberWalletContractDao.increaseWalletContractBalanceById(changeAmount, total, null, walletContract.getId()); |
| | | |
| | | ThreadPoolUtils.sendWholeForceClosingPrice(holdOrderEntity.getSymbol(), memberEntity); |
| | | // 计算佣金 |
| | |
| | | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void entrustCloseOrder(List<OrderModel> list) { |
| | | if (CollUtil.isNotEmpty(list)) { |
| | | List<Long> ids = new ArrayList<>(); |
| | | list.forEach(model -> ids.add(model.getOrderId())); |
| | | List<ContractEntrustOrderEntity> contractEntrustOrderEntities = contractEntrustOrderDao.selectEntrustOrderListByIds(ids); |
| | | |
| | | if (CollUtil.isNotEmpty(contractEntrustOrderEntities)) { |
| | | for (ContractEntrustOrderEntity entrustOrderEntity : contractEntrustOrderEntities) { |
| | | closeOrder(entrustOrderEntity); |
| | | } |
| | | } else { |
| | | log.info("平仓委托单未找到"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void closeOrder(ContractEntrustOrderEntity entrustOrder) { |
| | | log.info("执行平仓委托"); |
| | | } |
| | | } |