Helius
2020-08-04 ea8eb9c441bb4452d153579b456467b2f5e958e4
src/main/java/com/xcong/excoin/modules/contract/service/impl/RabbitOrderServiceImpl.java
@@ -10,16 +10,16 @@
import com.xcong.excoin.modules.contract.entity.ContractOrderEntity;
import com.xcong.excoin.modules.contract.mapper.ContractHoldOrderEntityMapper;
import com.xcong.excoin.modules.contract.service.RabbitOrderService;
import com.xcong.excoin.modules.documentary.dao.FollowFollowerOrderRelationDao;
import com.xcong.excoin.modules.documentary.entity.FollowFollowerOrderRelationEntity;
import com.xcong.excoin.modules.documentary.service.FollowOrderOperationService;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.dao.MemberWalletContractDao;
import com.xcong.excoin.modules.member.entity.AgentReturnEntity;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity;
import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity;
import com.xcong.excoin.utils.CacheSettingUtils;
import com.xcong.excoin.utils.CoinTypeConvert;
import com.xcong.excoin.utils.RedisUtils;
import com.xcong.excoin.utils.ThreadPoolUtils;
import com.xcong.excoin.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -61,6 +61,11 @@
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private FollowFollowerOrderRelationDao followFollowerOrderRelationDao;
    @Resource
    private FollowOrderOperationService followOrderOperationService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void cancelHoldOrder(List<Long> ids) {
@@ -79,6 +84,7 @@
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public void cancelHoldOrderMethod(ContractHoldOrderEntity holdOrderEntity) {
        String symbol = holdOrderEntity.getSymbol();
        // 获取最新价
@@ -98,14 +104,14 @@
            Integer closingType = null;
            // 开多
            if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
                newPrice = newPrice.multiply(BigDecimal.ONE.subtract(memberEntity.getSpread().divide(BigDecimal.valueOf(10000), 4, BigDecimal.ROUND_DOWN)));
                newPrice = newPrice.multiply(BigDecimal.ONE.subtract(memberEntity.getClosingSpread().divide(BigDecimal.valueOf(10000), 4, BigDecimal.ROUND_DOWN)));
                // (最新价-开仓价)*规格*张数
                profitOrLoss = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(lotNumber).multiply(new BigDecimal(holdOrderEntity.getSymbolCnt()));
                orderType = ContractOrderEntity.ORDER_TYPE_CLOSE_MORE;
                closingType = OrderClosingTypeEnum.CLOSE_MORE.getValue();
                // 开空
            } else {
                newPrice = newPrice.multiply(BigDecimal.ONE.add(memberEntity.getSpread().divide(BigDecimal.valueOf(10000), 4, BigDecimal.ROUND_DOWN)));
                newPrice = newPrice.multiply(BigDecimal.ONE.add(memberEntity.getClosingSpread().divide(BigDecimal.valueOf(10000), 4, BigDecimal.ROUND_DOWN)));
                // (开仓价-最新价)*规格*张数
                profitOrLoss = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(lotNumber).multiply(new BigDecimal(holdOrderEntity.getSymbolCnt()));
                orderType = ContractOrderEntity.ORDER_TYPE_CLOSE_LESS;
@@ -138,9 +144,38 @@
            // 计算盈利或亏损后可用金额和总金额应该增加或减少的
            BigDecimal addMoney = holdOrderEntity.getBondAmount().subtract(holdOrderEntity.getOpeningFeeAmount()).add(profitOrLoss);
            memberWalletContractDao.increaseWalletContractBalanceById(addMoney, profitOrLoss.subtract(contractOrderEntity.getOpeningFeeAmount()), null, walletContract.getId());
            // 流水
            LogRecordUtils.insertMemberAccountFlow(memberEntity.getId(), addMoney, walletContract.getAvailableBalance().add(addMoney), holdOrderEntity.getSymbol(), "平仓", "平仓");
            // 计算佣金
            ThreadPoolUtils.calReturnMoney(memberEntity.getId(), contractOrderEntity.getClosingFeeAmount(), contractOrderEntity, AgentReturnEntity.ORDER_TYPE_CLOSE);
            // 判断当前持仓是否为跟单订单
            if (ContractOrderEntity.CONTRACTTYPE_DOCUMENTARY == holdOrderEntity.getContractType()) {
                updateFollowOrderRelation(holdOrderEntity.getId(), contractOrderEntity.getId());
                // 若为交易员,则平仓跟随者订单
                if (MemberEntity.IS_TRADER_Y.equals(memberEntity.getIsTrader())) {
                    followOrderOperationService.closingFollowOrders(holdOrderEntity.getOrderNo());
                }
            }
        }
    }
    /**
     * 更新跟单订单关系
     *
     * @param oldOrderId 当前持仓ID
     * @param newOrderId 历史订单ID
     */
    public void updateFollowOrderRelation(Long oldOrderId, Long newOrderId) {
        FollowFollowerOrderRelationEntity orderRelationEntity = followFollowerOrderRelationDao.selectNowOneByorderId(oldOrderId);
        if (orderRelationEntity != null) {
            orderRelationEntity.setOrderId(newOrderId);
            orderRelationEntity.setOrderType(FollowFollowerOrderRelationEntity.ORDER_TYPE_HISTORY);
            followFollowerOrderRelationDao.updateById(orderRelationEntity);
        }
    }
}