| | |
| | | |
| | | 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 com.nq.pojo.*; |
| | | import com.nq.service.IUserAssetsServices; |
| | | import com.nq.utils.KeyUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | |
| | | /** |
| | | * 用户资产 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class UserAssetsServices implements IUserAssetsServices { |
| | | |
| | |
| | | @Autowired |
| | | UserRechargeServiceImpl userRechargeService; |
| | | |
| | | @Autowired |
| | | UserWithdrawServiceImpl withdrawService; |
| | | |
| | | @Resource |
| | | UserRechargeMapper userRechargeMapper; |
| | | |
| | |
| | | @Autowired |
| | | UserPositionMapper userPositionMapper; |
| | | |
| | | @Autowired |
| | | ExchangeRateRepository exchangeRateRepository; |
| | | |
| | | |
| | | @Override |
| | | public UserAssets assetsByTypeAndUserId(String accetType, Integer userId) { |
| | | if(accetType.equals("SZHB")){ |
| | | /*if(accetType.equals("SZHB")){ |
| | | accetType = "US"; |
| | | }*/ |
| | | if(accetType.equals(EStockType.US.getCode()) || accetType.equals("IN")){ |
| | | accetType = EStockType.MX.getCode(); |
| | | } |
| | | QueryWrapper<UserAssets> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("accect_type",accetType); |
| | |
| | | if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){ |
| | | BigDecimal availableBalance = bigAmt.subtract(userAssets.getAmountToBeCovered()); |
| | | availableBalance = availableBalance.subtract(userAssets.getHandlingChargeWritten()); |
| | | if(availableBalance.compareTo(BigDecimal.ZERO) > 0){ |
| | | if(availableBalance.compareTo(BigDecimal.ZERO) >= 0){ |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(availableBalance)); |
| | | userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(userAssets.getAmountToBeCovered())); |
| | | userAssets.setAmountToBeCovered(BigDecimal.ZERO); |
| | |
| | | |
| | | |
| | | @Override |
| | | public Boolean availablebalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount, String desc, String descType) { |
| | | |
| | | UserAssets userAssets = assetsByTypeAndUserId(accetType,userId); |
| | | public Boolean availablebalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount, String desc, String descType) throws Exception { |
| | | //UserAssets userAssets = assetsByTypeAndUserId(accetType,userId); |
| | | //查询墨西哥账户 |
| | | UserAssets userAssets = assetsByTypeAndUserId(EStockType.MX.getCode(), userId); |
| | | //如果不是墨西哥币需要转换金额 |
| | | if (!accetType.equals(EStockType.MX.getCode())) { |
| | | amount = exchangeAmountByRate(accetType, amount); |
| | | } |
| | | 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(); |
| | |
| | | if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){ |
| | | BigDecimal availableBalance = amount.subtract(userAssets.getAmountToBeCovered()); |
| | | availableBalance = availableBalance.subtract(userAssets.getHandlingChargeWritten()); |
| | | if(availableBalance.compareTo(BigDecimal.ZERO) > 0){ |
| | | if(availableBalance.compareTo(BigDecimal.ZERO) >= 0){ |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(availableBalance)); |
| | | userAssets.setAmountToBeCovered(BigDecimal.ZERO); |
| | | userAssets.setHandlingChargeWritten(BigDecimal.ZERO); |
| | |
| | | if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){ |
| | | BigDecimal availableBalance = amount.subtract(userAssets.getAmountToBeCovered()); |
| | | availableBalance = availableBalance.subtract(userAssets.getHandlingChargeWritten()); |
| | | if(availableBalance.compareTo(BigDecimal.ZERO) > 0){ |
| | | if(availableBalance.compareTo(BigDecimal.ZERO) >= 0){ |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(availableBalance)); |
| | | userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(userAssets.getAmountToBeCovered())); |
| | | userAssets.setAmountToBeCovered(BigDecimal.ZERO); |
| | | userAssets.setHandlingChargeWritten(BigDecimal.ZERO); |
| | | }else{ |
| | | BigDecimal amountToBeCovered = userAssets.getAmountToBeCovered().subtract(amount); |
| | | userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(amount)); |
| | | userAssets.setAmountToBeCovered(amountToBeCovered); |
| | | userAssets.setHandlingChargeWritten(BigDecimal.ZERO); |
| | | userAssets.setAmountToBeCovered(availableBalance.negate()); |
| | | } |
| | | }else{ |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount)); |
| | |
| | | } |
| | | |
| | | if(null != userPosition){ |
| | | userPosition.setAmountToBeCovered(userAssets.getAmountToBeCovered()); |
| | | userPosition.setAmountToBeCovered(userAssets.getAmountToBeCovered().add(userAssets.getHandlingChargeWritten())); |
| | | userPositionMapper.updateById(userPosition); |
| | | } |
| | | String after = userAssets.getAvailableBalance().toString(); |
| | |
| | | } |
| | | extracted(userAssets); |
| | | } |
| | | |
| | | |
| | | public Boolean aiAvailableBalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount) throws Exception { |
| | | //查询墨西哥账户 |
| | | UserAssets userAssets = assetsByTypeAndUserId(EStockType.MX.getCode(), userId); |
| | | //如果不是墨西哥币需要转换金额 |
| | | if (!accetType.equals(EStockType.MX.getCode())) { |
| | | amount = exchangeAmountByRate(accetType, amount); |
| | | } |
| | | String before = userAssets.getAvailableBalance().toString(); |
| | | if (eUserAssets.getCode().equals(EUserAssets.BUY_AI.getCode())) { |
| | | //冻结金额 |
| | | userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(amount)); |
| | | //扣除可用金额 |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().subtract(amount)); |
| | | amount = amount.negate(); |
| | | } else if (eUserAssets.getCode().equals(EUserAssets.BUY_AI_REJECT.getCode())) { |
| | | //解除冻结金额 |
| | | userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(amount)); |
| | | //归还可用金额 |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount)); |
| | | } else if (eUserAssets.getCode().equals(EUserAssets.AI_SETTLEMENT.getCode())) { |
| | | //解除购买冻结金额 |
| | | userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(amount)); |
| | | //归还购买金额 |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount)); |
| | | } else if (eUserAssets.getCode().equals(EUserAssets.AI_SETTLEMENT_INT.getCode())) { |
| | | //收益 |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount)); |
| | | } else if(eUserAssets.getCode().equals(EUserAssets.DK.getCode())) { |
| | | //发放贷款金额 |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount)); |
| | | } else if(eUserAssets.getCode().equals(EUserAssets.RT_DK.getCode())) { |
| | | //归还贷款 |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().subtract(amount)); |
| | | amount = amount.negate(); |
| | | } else if(eUserAssets.getCode().equals(EUserAssets.RT_DK_INT.getCode())) { |
| | | //贷款利息 |
| | | userAssets.setAvailableBalance(userAssets.getAvailableBalance().subtract(amount)); |
| | | amount = amount.negate(); |
| | | } |
| | | |
| | | 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.getUserId()+""); |
| | | moneyLog.setSymbol(EStockType.getEStockTypeByCode(userAssets.getAccectType()).getSymbol()); |
| | | moneyLog.setCreateTime(new Date()); |
| | | moneyLogMapper.insert(moneyLog); |
| | | return userAssetsMapper.updateById(userAssets)>1; |
| | | } |
| | | |
| | | @Override |
| | | public BigDecimal exchangeAmountByRate(String accetType, BigDecimal amount) throws Exception { |
| | | EStockType stockType = EStockType.getEStockTypeByCode(accetType); |
| | | ExchangeRate exchangeRate = exchangeRateRepository.findExchangeRateByCurrencyAndConversionCurrency( |
| | | stockType.getSymbol(), EStockType.MX.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.MX.getSymbol()); |
| | | throw new Exception("货币转换汇率未设置"); |
| | | } |
| | | |
| | | |
| | | } |
| | | } |