| | |
| | | package com.nq.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | 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.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.nq.common.ServerResponse; |
| | | import com.nq.dao.*; |
| | | import com.nq.enums.EStockType; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.validation.constraints.Email; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.math.BigDecimal; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | |
| | | /** |
| | | * 用户资产 |
| | |
| | | return userAssetsMapper.updateById(userAssets)>1; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public ServerResponse transfer(Integer userId, String disbursementAccount, String depositAccount, String amt, HttpServletRequest request) { |
| | | // 参数校验 |
| | | ServerResponse paramCheckResponse = checkParameters(userId, disbursementAccount, depositAccount, amt, request); |
| | | if (paramCheckResponse != null) { |
| | | return paramCheckResponse; |
| | | } |
| | | |
| | | // 转账金额合法性检查 |
| | | BigDecimal transferAmount = validateTransferAmount(amt, request); |
| | | if (transferAmount == null) { |
| | | return ServerResponse.createByErrorMsg("无效的转账金额", request); |
| | | } |
| | | |
| | | // 查询转出账户和转入账户的资产信息 |
| | | UserAssets disbursement = getUserAssets(userId, disbursementAccount); |
| | | UserAssets deposit = getUserAssets(userId, depositAccount); |
| | | |
| | | // 账户检查 |
| | | if (disbursement == null) { |
| | | return ServerResponse.createByErrorMsg("转出账户不存在", request); |
| | | } |
| | | if (deposit == null) { |
| | | return ServerResponse.createByErrorMsg("转入账户不存在", request); |
| | | } |
| | | |
| | | // 检查转出账户余额是否足够 |
| | | if (isBalanceSufficient(disbursement.getAvailableBalance(), transferAmount)) { |
| | | return ServerResponse.createByErrorMsg("转出账户余额不足", request); |
| | | } |
| | | |
| | | // 执行转账操作 |
| | | try { |
| | | synchronized (userId){ |
| | | performTransfer(disbursement, deposit, transferAmount); |
| | | } |
| | | return ServerResponse.createBySuccess("划转成功", request); |
| | | } catch (Exception e) { |
| | | log.error("划转失败: " + e.getMessage(), e); |
| | | return ServerResponse.createByErrorMsg("划转失败", request); |
| | | } |
| | | } |
| | | |
| | | // 参数校验 |
| | | private ServerResponse checkParameters(Integer userId, String disbursementAccount, String depositAccount, String amt, HttpServletRequest request) { |
| | | if (userId == null || disbursementAccount == null || depositAccount == null || amt == null) { |
| | | return ServerResponse.createByErrorMsg("输入参数不能为空", request); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | // 转账金额校验 |
| | | private BigDecimal validateTransferAmount(String amt, HttpServletRequest request) { |
| | | try { |
| | | BigDecimal transferAmount = BigDecimal.valueOf(Double.valueOf(amt)); |
| | | if (transferAmount.compareTo(BigDecimal.ZERO) <= 0) { |
| | | ServerResponse.createByErrorMsg("转账金额必须大于零", request); |
| | | return null; |
| | | } |
| | | return transferAmount; |
| | | } catch (NumberFormatException e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | // 获取账户资产信息 |
| | | private UserAssets getUserAssets(Integer userId, String accountType) { |
| | | return userAssetsMapper.selectOne(new LambdaQueryWrapper<>(UserAssets.class) |
| | | .eq(UserAssets::getAccectType, accountType) |
| | | .eq(UserAssets::getUserId, userId)); |
| | | } |
| | | |
| | | // 判断余额是否足够 |
| | | private boolean isBalanceSufficient(BigDecimal balance, BigDecimal transferAmount) { |
| | | return balance.compareTo(transferAmount) < 0; |
| | | } |
| | | |
| | | // 执行转账操作 |
| | | private void performTransfer(UserAssets disbursement, UserAssets deposit, BigDecimal transferAmount) { |
| | | // 更新转出账户余额 |
| | | disbursement.setAvailableBalance(disbursement.getAvailableBalance().subtract(transferAmount)); |
| | | userAssetsMapper.updateById(disbursement); |
| | | |
| | | // 更新转入账户余额 |
| | | deposit.setAvailableBalance(deposit.getAvailableBalance().add(transferAmount)); |
| | | userAssetsMapper.updateById(deposit); |
| | | } |
| | | |
| | | //只要涉及到cumulativeProfitAndLoss变动重新设置状态 |
| | | private static void extracted(UserAssets userAssets) { |
| | | if(userAssets.getCumulativeProfitAndLoss().compareTo(BigDecimal.ZERO) >= 0){ |
| | |
| | | } |
| | | extracted(userAssets); |
| | | } |
| | | |
| | | @Override |
| | | public boolean saveBatch(Collection<UserAssets> entityList, int batchSize) { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public boolean saveOrUpdateBatch(Collection<UserAssets> entityList, int batchSize) { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public boolean updateBatchById(Collection<UserAssets> entityList, int batchSize) { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public boolean saveOrUpdate(UserAssets entity) { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public UserAssets getOne(Wrapper<UserAssets> queryWrapper, boolean throwEx) { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> getMap(Wrapper<UserAssets> queryWrapper) { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public <V> V getObj(Wrapper<UserAssets> queryWrapper, Function<? super Object, V> mapper) { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public BaseMapper<UserAssets> getBaseMapper() { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public Class<UserAssets> getEntityClass() { |
| | | return null; |
| | | } |
| | | } |