Helius
2020-09-24 3e32ab49044296b0147bf0a37370375e38df41a2
src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
@@ -9,6 +9,8 @@
import javax.annotation.Resource;
import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity;
import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -32,6 +34,10 @@
import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletCoinVo;
import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletContractInfoVo;
import com.xcong.excoin.modules.coin.service.CoinService;
import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao;
import com.xcong.excoin.modules.contract.entity.ContractHoldOrderEntity;
import com.xcong.excoin.modules.contract.mapper.ContractHoldOrderEntityMapper;
import com.xcong.excoin.modules.contract.parameter.vo.HoldOrderListVo;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.dao.MemberWalletAgentDao;
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
@@ -41,6 +47,7 @@
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity;
import com.xcong.excoin.modules.platform.dao.PlatformCnyUsdtExchangeDao;
import com.xcong.excoin.utils.CacheSettingUtils;
import com.xcong.excoin.utils.CoinTypeConvert;
import com.xcong.excoin.utils.MessageSourceUtils;
import com.xcong.excoin.utils.RedisUtils;
@@ -69,6 +76,10 @@
    MemberDao memberDao;
    @Resource
    RedisUtils redisUtils;
    @Resource
    private ContractHoldOrderDao contractHoldOrderDao;
    @Resource
    private CacheSettingUtils cacheSettingUtils;
    @Override
@@ -201,7 +212,7 @@
            memberWalletContractInfoVo.setAvailableBalance(memberWalletContractEntity.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
            memberWalletContractInfoVo.setTotalBalance(memberWalletContractEntity.getTotalBalance().setScale(4, BigDecimal.ROUND_DOWN));
            memberWalletContractInfoVo.setTotalRMBBalance(memberWalletContractEntity.getTotalBalance().multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
            memberWalletContractInfoVo.setWalletCode(memberWalletContractEntity.getWalletCode());
            memberWalletContractInfoVo.setWalletCode(memberWalletContractEntity.getWalletCode()+"/USDT");
            list.add(memberWalletContractInfoVo);
            totalCoin = totalCoin.add(memberWalletContractEntity.getTotalBalance());
         }
@@ -403,11 +414,44 @@
       }
       //获取用户ID
       Long memberId = LoginUserUtils.getAppLoginUser().getId();
       MemberEntity memberEntity = LoginUserUtils.getAppLoginUser();
       List<ContractHoldOrderEntity> list = contractHoldOrderDao.selectHoldOrderListByMemberIdAndSymbol(memberEntity.getId(), symbol, 1);
       BigDecimal totalProfitOrLoss = BigDecimal.ZERO;
        if (CollUtil.isNotEmpty(list)) {
            for (ContractHoldOrderEntity holdOrderEntity : list) {
                // 获取最新价
                BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(holdOrderEntity.getSymbol())));
                BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(holdOrderEntity.getSymbol());
                // 盈亏
                BigDecimal rewardRatio = BigDecimal.ZERO;
                // 开多
                if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
                    // (最新价-开仓价)*规格*张数
                    rewardRatio = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(lotNumber).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale()));
                    // 开空
                } else {
                    // (开仓价-最新价)*规格*张数
                    rewardRatio = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(lotNumber).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale()));
                }
                if (memberEntity.getIsProfit() == MemberEntity.IS_PROFIT_Y) {
                    PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting();
                    if (rewardRatio.compareTo(BigDecimal.ZERO) > -1) {
                        rewardRatio = rewardRatio.multiply(BigDecimal.ONE.subtract(tradeSettingEntity.getProfitParam()));
                    }
                }
                totalProfitOrLoss = totalProfitOrLoss.add(rewardRatio);
            }
        }
       
       String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
       MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, symbol);
       BigDecimal availableBalance = walletContract.getAvailableBalance();
       // 扣币
       if (totalProfitOrLoss.compareTo(BigDecimal.ZERO) < 0) {
          availableBalance = availableBalance.add(totalProfitOrLoss);
       }
       BigDecimal availableSubtract = availableBalance.subtract(balance);
       if (availableSubtract.compareTo(BigDecimal.ZERO) < 0) {
          return Result.fail(MessageSourceUtils.getString("member_service_0007"));
@@ -436,7 +480,6 @@
       }
       
       //更新合约全仓模式下的订单权益
        MemberEntity memberEntity = memberDao.selectById(memberId);
        String symbols = symbol+"/USDT";
        ThreadPoolUtils.sendWholeForceClosingPrice(symbols, memberEntity);