| | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | |
| | | |
| | | @Override |
| | | public UserAssets assetsByTypeAndUserId(String accetType, Integer userId) { |
| | | if(!accetType.equals(EStockType.US.getCode())){ |
| | | /*if(!accetType.equals(EStockType.US.getCode())){ |
| | | accetType = EStockType.US.getCode(); |
| | | } |
| | | }*/ |
| | | QueryWrapper<UserAssets> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("accect_type",accetType); |
| | | queryWrapper.eq("user_id",userId); |
| | |
| | | } |
| | | userAssets = userAssetsMapper.selectOne(queryWrapper); |
| | | return userAssets; |
| | | } |
| | | |
| | | @Override |
| | | public void addUserAssetsListByUserId(Integer userId) { |
| | | QueryWrapper<UserAssets> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("user_id",userId); |
| | | List<UserAssets> userAssetsList = userAssetsMapper.selectList(queryWrapper); |
| | | List<EStockType> stockTypes = Arrays.asList( |
| | | EStockType.US, |
| | | EStockType.HK, |
| | | EStockType.IN, |
| | | EStockType.TW |
| | | ); |
| | | for (EStockType stockType : stockTypes) { |
| | | //是否存在记录 |
| | | boolean isExist = false; |
| | | if (!userAssetsList.isEmpty()) { |
| | | boolean hasType = userAssetsList.stream() |
| | | .anyMatch(assets -> stockType.getCode().equals(assets.getAccectType())); |
| | | if (hasType) { |
| | | isExist = true; |
| | | } |
| | | } |
| | | if (!isExist) { //不存在账户新增 |
| | | UserAssets userAssets = new UserAssets(); |
| | | userAssets.setUserId(userId); |
| | | userAssets.setAccectType(stockType.getCode()); |
| | | userAssetsMapper.insert(userAssets); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public Boolean availablebalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount, String desc, String descType) throws Exception { |
| | | //查询账户 |
| | | UserAssets userAssets = assetsByTypeAndUserId(EStockType.getDefault().getCode(), userId); |
| | | /*UserAssets userAssets = assetsByTypeAndUserId(EStockType.getDefault().getCode(), userId); |
| | | //是否需要转换金额 |
| | | if (!accetType.equals(EStockType.getDefault().getCode())) { |
| | | amount = exchangeAmountByRate(accetType, amount); |
| | | } |
| | | |
| | | }*/ |
| | | UserAssets userAssets = assetsByTypeAndUserId(accetType, userId); |
| | | 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(); |
| | |
| | | |
| | | public Boolean aiAvailableBalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount) throws Exception { |
| | | //查询账户 |
| | | UserAssets userAssets = assetsByTypeAndUserId(EStockType.getDefault().getCode(), userId); |
| | | /*UserAssets userAssets = assetsByTypeAndUserId(EStockType.getDefault().getCode(), userId); |
| | | //是否需要转换金额 |
| | | if (!accetType.equals(EStockType.getDefault().getCode())) { |
| | | amount = exchangeAmountByRate(accetType, amount); |
| | | } |
| | | }*/ |
| | | UserAssets userAssets = assetsByTypeAndUserId(accetType, userId); |
| | | String before = userAssets.getAvailableBalance().toString(); |
| | | if (eUserAssets.getCode().equals(EUserAssets.BUY_AI.getCode())) { |
| | | //冻结金额 |
| | |
| | | } |
| | | |
| | | @Override |
| | | public BigDecimal exchangeAmountByRate(String accetType, BigDecimal amount) throws Exception { |
| | | EStockType stockType = EStockType.getEStockTypeByCode(accetType); |
| | | public BigDecimal exchangeAmountByRate(String fromType, String toType, BigDecimal amount) throws Exception { |
| | | EStockType stockType = EStockType.getEStockTypeByCode(fromType); |
| | | EStockType toStockType = EStockType.getEStockTypeByCode(toType); |
| | | ExchangeRate exchangeRate = exchangeRateRepository.findExchangeRateByCurrencyAndConversionCurrency( |
| | | stockType.getSymbol(), EStockType.getDefault().getSymbol()).orElse(null); |
| | | stockType.getSymbol(), toStockType.getSymbol()).orElse(null); |
| | | if (exchangeRate != null) { |
| | | //转换为默认货币 保留5位小数 |
| | | return amount.multiply(exchangeRate.getRata()).setScale(5, RoundingMode.HALF_UP); |
| | | } else { |
| | | log.error("exchangeAmountByRate is null:{}>>{}", stockType.getSymbol(), EStockType.getDefault().getSymbol()); |
| | | log.error("exchangeAmountByRate is null:{}>>{}", stockType.getSymbol(), toStockType.getSymbol()); |
| | | throw new Exception("货币转换汇率未设置"); |
| | | } |
| | | |