| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.nq.Repository.ExchangeRateRepository; |
| | | import com.nq.common.ServerResponse; |
| | | import com.nq.dao.*; |
| | | import com.nq.enums.EStockType; |
| | |
| | | import javax.annotation.Resource; |
| | | import javax.validation.constraints.Email; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | |
| | | |
| | | @Autowired |
| | | UserPositionMapper userPositionMapper; |
| | | |
| | | @Autowired |
| | | ExchangeRateRepository exchangeRateRepository; |
| | | |
| | | |
| | | @Override |
| | | public UserAssets assetsByTypeAndUserId(String accetType, Integer userId) { |
| | |
| | | |
| | | @Override |
| | | public Boolean availablebalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount, String desc, String descType) { |
| | | |
| | | UserAssets userAssets = assetsByTypeAndUserId(accetType,userId); |
| | | //UserAssets userAssets = assetsByTypeAndUserId(accetType,userId); |
| | | //查询墨西哥账户 |
| | | UserAssets userAssets = assetsByTypeAndUserId(EStockType.MX.getCode(), userId); |
| | | //如果不是墨西哥币需要转换金额 |
| | | if (!accetType.equals(EStockType.MX.getCode())) { |
| | | EStockType stockType = EStockType.getEStockTypeByCode(accetType); |
| | | ExchangeRate exchangeRate = exchangeRateRepository.findExchangeRateByCurrencyAndConversionCurrency( |
| | | stockType.getSymbol(), EStockType.MX.getSymbol()).orElse(null); |
| | | if (exchangeRate != null) { |
| | | //转换为墨西哥币 |
| | | amount = exchangeAmountByRate(amount, exchangeRate.getRata()); |
| | | } |
| | | } |
| | | UserPosition userPosition = userPositionMapper.selectOne(new LambdaQueryWrapper<UserPosition>().gt(UserPosition::getAmountToBeCovered, BigDecimal.ZERO).eq(UserPosition::getUserId,userAssets.getUserId())); |
| | | String type = eUserAssets.getDesc(); |
| | | String before = userAssets.getAvailableBalance().toString(); |
| | |
| | | } |
| | | extracted(userAssets); |
| | | } |
| | | |
| | | |
| | | public Boolean aiAvailableBalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount, BigDecimal buyAmount) { |
| | | //查询墨西哥账户 |
| | | UserAssets userAssets = assetsByTypeAndUserId(EStockType.MX.getCode(), userId); |
| | | //如果不是墨西哥币需要转换金额 |
| | | if (!accetType.equals(EStockType.MX.getCode())) { |
| | | EStockType stockType = EStockType.getEStockTypeByCode(accetType); |
| | | ExchangeRate exchangeRate = exchangeRateRepository.findExchangeRateByCurrencyAndConversionCurrency( |
| | | stockType.getSymbol(), EStockType.MX.getSymbol()).orElse(null); |
| | | if (exchangeRate != null) { |
| | | //转换为墨西哥币 |
| | | amount = exchangeAmountByRate(amount, exchangeRate.getRata()); |
| | | buyAmount = exchangeAmountByRate(buyAmount, exchangeRate.getRata()); |
| | | } else { |
| | | return false; |
| | | } |
| | | } |
| | | String before = userAssets.getAvailableBalance().toString(); |
| | | if (eUserAssets.getCode().equals(EUserAssets.BUY_AI.getCode())) { |
| | | //冻结金额 |
| | | userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(amount)); |
| | | //扣除可用金额 |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount.negate())); |
| | | } else if (eUserAssets.getCode().equals(EUserAssets.BUY_AI_REJECT.getCode())) { |
| | | //解除冻结金额 |
| | | userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(amount.negate())); |
| | | //归还可用金额 |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount)); |
| | | } else if (eUserAssets.getCode().equals(EUserAssets.AI_SETTLEMENT.getCode())) { |
| | | //解除购买冻结金额 |
| | | userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(buyAmount.negate())); |
| | | //归还可用金额(购买金额+收益) |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount)); |
| | | } |
| | | String after = userAssets.getAvailableBalance().toString(); |
| | | MoneyLog moneyLog = new MoneyLog(); |
| | | moneyLog.setDescs(eUserAssets.getDesc()); |
| | | moneyLog.setBeFore(before); |
| | | moneyLog.setAfter(after); |
| | | moneyLog.setAmount(amount.toString()); |
| | | moneyLog.setAccectType(userAssets.getAccectType()); |
| | | moneyLog.setType(eUserAssets.getCode()); |
| | | moneyLog.setUserId(userAssets.getId()+""); |
| | | moneyLog.setSymbol(EStockType.getEStockTypeByCode(userAssets.getAccectType()).getSymbol()); |
| | | moneyLog.setCreateTime(new Date()); |
| | | moneyLogMapper.insert(moneyLog); |
| | | return userAssetsMapper.updateById(userAssets)>1; |
| | | } |
| | | |
| | | @Override |
| | | public BigDecimal exchangeAmountByRate(BigDecimal amount, BigDecimal rate) { |
| | | //保留5位小数 |
| | | return amount.multiply(rate).setScale(5, RoundingMode.HALF_UP); |
| | | } |
| | | } |