|  | @@ -1,10 +1,21 @@
 | 
	
		
			
				|  |  |  package com.ym.mec.biz.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import java.math.BigDecimal;
 | 
	
		
			
				|  |  | +import java.util.Date;
 | 
	
		
			
				|  |  | +import java.util.List;
 | 
	
		
			
				|  |  | +import java.util.Objects;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import org.slf4j.Logger;
 | 
	
		
			
				|  |  | +import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | +import org.springframework.beans.BeanUtils;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | +import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | +import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  import com.google.common.collect.Lists;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.StudentWithdrawDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.SysUserCashAccountDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dto.CashAccountDetail;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.dto.WithdrawDto;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dto.WithdrawInfoDto;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.entity.StudentWithdraw;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.entity.SysUserCashAccount;
 | 
	
	
		
			
				|  | @@ -21,24 +32,9 @@ import com.ym.mec.common.exception.BizException;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.page.PageInfo;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.service.impl.BaseServiceImpl;
 | 
	
		
			
				|  |  |  import com.ym.mec.util.date.DateUtil;
 | 
	
		
			
				|  |  | -import com.ym.mec.util.string.IdWorker;
 | 
	
		
			
				|  |  | -import org.apache.commons.lang3.RandomStringUtils;
 | 
	
		
			
				|  |  | -import org.slf4j.Logger;
 | 
	
		
			
				|  |  | -import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | -import org.springframework.beans.BeanUtils;
 | 
	
		
			
				|  |  | -import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | -import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | -import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -import java.math.BigDecimal;
 | 
	
		
			
				|  |  | -import java.util.Date;
 | 
	
		
			
				|  |  | -import java.util.List;
 | 
	
		
			
				|  |  | -import java.util.Objects;
 | 
	
		
			
				|  |  | -import java.util.concurrent.locks.Lock;
 | 
	
		
			
				|  |  | -import java.util.concurrent.locks.ReentrantLock;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  | -public class StudentWithdrawServiceImpl extends BaseServiceImpl<String, StudentWithdraw>  implements StudentWithdrawService {
 | 
	
		
			
				|  |  | +public class StudentWithdrawServiceImpl extends BaseServiceImpl<Integer, StudentWithdraw> implements StudentWithdrawService {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Autowired
 | 
	
		
			
				|  |  |  	private StudentWithdrawDao studentWithdrawDao;
 | 
	
	
		
			
				|  | @@ -46,7 +42,7 @@ public class StudentWithdrawServiceImpl extends BaseServiceImpl<String, StudentW
 | 
	
		
			
				|  |  |  	private SysUserCashAccountDao sysUserCashAccountDao;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  | -	public BaseDAO<String, StudentWithdraw> getDAO() {
 | 
	
		
			
				|  |  | +	public BaseDAO<Integer, StudentWithdraw> getDAO() {
 | 
	
		
			
				|  |  |  		return studentWithdrawDao;
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -61,50 +57,62 @@ public class StudentWithdrawServiceImpl extends BaseServiceImpl<String, StudentW
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	@Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | -	public boolean confirmWithdraw(WithdrawDto withdrawDto) {
 | 
	
		
			
				|  |  | +	public boolean confirmWithdraw(Integer id, DealStatusEnum status, String memo, String transNo, TransTypeEnum transType) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		StudentWithdraw studentWithdraw = get(id);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if (studentWithdraw == null) {
 | 
	
		
			
				|  |  | +			throw new BizException("提现申请记录不存在");
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		Date date = new Date();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		Integer userId = studentWithdraw.getUserId();
 | 
	
		
			
				|  |  | +		BigDecimal amount = studentWithdraw.getAmount();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		studentWithdraw.setComment(memo);
 | 
	
		
			
				|  |  | +		studentWithdraw.setModifyTime(date);
 | 
	
		
			
				|  |  | +		studentWithdraw.setStatus(status);
 | 
	
		
			
				|  |  | +		studentWithdraw.setTransNo(transNo);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		studentWithdrawDao.update(studentWithdraw);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  		try {
 | 
	
		
			
				|  |  | -			SysUserCashAccount sysUserCashAccount = cashAccountService.get(withdrawDto.getUserId().intValue());
 | 
	
		
			
				|  |  | -			if (Objects.isNull(sysUserCashAccount)){
 | 
	
		
			
				|  |  | +			SysUserCashAccount sysUserCashAccount = cashAccountService.get(userId);
 | 
	
		
			
				|  |  | +			if (Objects.isNull(sysUserCashAccount)) {
 | 
	
		
			
				|  |  |  				throw new BizException("账户不存在!");
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  | -			//校验银行账户余额
 | 
	
		
			
				|  |  | -			if (sysUserCashAccount.getBalance().compareTo(withdrawDto.getAmount()) < 0) {
 | 
	
		
			
				|  |  | -				throw new BizException("余额不足,提现失败!");
 | 
	
		
			
				|  |  | +			// 校验银行账户余额
 | 
	
		
			
				|  |  | +			if (sysUserCashAccount.getFrozenAmount().compareTo(amount) < 0) {
 | 
	
		
			
				|  |  | +				throw new BizException("账户冻结金额异常");
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  | -			//第三方提现到用户银行卡
 | 
	
		
			
				|  |  | -			//用户个人虚拟账户金额减少
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -			//生成提现记录(缺少银行流水号,交易状态,用户承担的费用,平台承担的费用)
 | 
	
		
			
				|  |  | -			Integer withdrawId = saveStudentWithdraw(withdrawDto);
 | 
	
		
			
				|  |  | +			sysUserCashAccount.setFrozenAmount(sysUserCashAccount.getFrozenAmount().subtract(amount));
 | 
	
		
			
				|  |  | +			
 | 
	
		
			
				|  |  | +			if(status == DealStatusEnum.FAilED){
 | 
	
		
			
				|  |  | +				sysUserCashAccount.setBalance(sysUserCashAccount.getBalance().add(amount));
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			sysUserCashAccount.setUpdateTime(date);
 | 
	
		
			
				|  |  | +			cashAccountService.update(sysUserCashAccount);
 | 
	
		
			
				|  |  | +			
 | 
	
		
			
				|  |  | +			SysUserCashAccountDetail sysUserCashAccountDetail = new SysUserCashAccountDetail();
 | 
	
		
			
				|  |  | +			sysUserCashAccountDetail.setUserId(userId);
 | 
	
		
			
				|  |  | +			sysUserCashAccountDetail.setType(PlatformCashAccountDetailTypeEnum.WITHDRAW);
 | 
	
		
			
				|  |  | +			sysUserCashAccountDetail.setStatus(status);
 | 
	
		
			
				|  |  | +			sysUserCashAccountDetail.setAmount(amount);
 | 
	
		
			
				|  |  | +			sysUserCashAccountDetail.setAttribute(id + "");
 | 
	
		
			
				|  |  | +			sysUserCashAccountDetail.setComment(memo);
 | 
	
		
			
				|  |  | +			sysUserCashAccountDetail.setTransNo(transNo);
 | 
	
		
			
				|  |  | +			sysUserCashAccountDetail.setTransType(transType);
 | 
	
		
			
				|  |  | +			sysUserCashAccountDetail.setBalance(sysUserCashAccount.getBalance());
 | 
	
		
			
				|  |  | +			
 | 
	
		
			
				|  |  | +			cashAccountDetailService.insert(sysUserCashAccountDetail);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -			accountDetailService.addCashAccountDetail(withdrawDto.getUserId(),withdrawDto.getAmount(),withdrawId +"","",
 | 
	
		
			
				|  |  | -					PlatformCashAccountDetailTypeEnum.WITHDRAW,TransTypeEnum.FAST_PAYMENT, DealStatusEnum.ING,"");
 | 
	
		
			
				|  |  |  		} catch (Exception e) {
 | 
	
		
			
				|  |  |  			log.error("用户提现出现异常 {}", e.getMessage(), e);
 | 
	
		
			
				|  |  |  			throw new BizException(e.getMessage());
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  		return true;
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | -	/**
 | 
	
		
			
				|  |  | -	 * 修改用户账户余额,保存账户明细
 | 
	
		
			
				|  |  | -	 *
 | 
	
		
			
				|  |  | -	 * @param withdrawDto
 | 
	
		
			
				|  |  | -	 */
 | 
	
		
			
				|  |  | -	private void updateCashAccount(SysUserCashAccount sysUserCashAccount, WithdrawDto withdrawDto) {
 | 
	
		
			
				|  |  | -		BigDecimal balance = sysUserCashAccount.getBalance().subtract(withdrawDto.getAmount());
 | 
	
		
			
				|  |  | -		SysUserCashAccount account = new SysUserCashAccount();
 | 
	
		
			
				|  |  | -		account.setUserId(sysUserCashAccount.getUserId());
 | 
	
		
			
				|  |  | -		account.setBalance(balance);
 | 
	
		
			
				|  |  | -		account.setUpdateTime(new Date());
 | 
	
		
			
				|  |  | -		cashAccountService.update(account);
 | 
	
		
			
				|  |  | -		SysUserCashAccountDetail cashAccountDetail = new SysUserCashAccountDetail();
 | 
	
		
			
				|  |  | -		cashAccountDetail.setUserId(withdrawDto.getUserId());
 | 
	
		
			
				|  |  | -		cashAccountDetail.setType(PlatformCashAccountDetailTypeEnum.WITHDRAW);
 | 
	
		
			
				|  |  | -		cashAccountDetail.setAmount(withdrawDto.getAmount());
 | 
	
		
			
				|  |  | -		cashAccountDetail.setBalance(balance);
 | 
	
		
			
				|  |  | -		cashAccountDetail.setCreateTime(new Date());
 | 
	
		
			
				|  |  | -		cashAccountDetailService.insert(cashAccountDetail);
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	public PageInfo<WithdrawInfoDto> queryWithdrawPage(CashAccountDetail cashAccountDetail) {
 | 
	
	
		
			
				|  | @@ -112,7 +120,7 @@ public class StudentWithdrawServiceImpl extends BaseServiceImpl<String, StudentW
 | 
	
		
			
				|  |  |  		cashAccountDetail.setType(PlatformCashAccountDetailTypeEnum.WITHDRAW.getCode());
 | 
	
		
			
				|  |  |  		PageInfo<SysUserCashAccountDetail> accountDetailPageInfo = accountDetailService.queryPage(cashAccountDetail);
 | 
	
		
			
				|  |  |  		List<WithdrawInfoDto> dataList = Lists.newArrayList();
 | 
	
		
			
				|  |  | -		accountDetailPageInfo.getRows().forEach(data ->{
 | 
	
		
			
				|  |  | +		accountDetailPageInfo.getRows().forEach(data -> {
 | 
	
		
			
				|  |  |  			WithdrawInfoDto withdrawInfoDto = new WithdrawInfoDto();
 | 
	
		
			
				|  |  |  			StudentWithdraw studentWithdraw = studentWithdrawDao.getByUserId(data.getUserId().longValue());
 | 
	
		
			
				|  |  |  			withdrawInfoDto.setWithdrawId(studentWithdraw.getWithdrawNo());
 | 
	
	
		
			
				|  | @@ -122,9 +130,9 @@ public class StudentWithdrawServiceImpl extends BaseServiceImpl<String, StudentW
 | 
	
		
			
				|  |  |  			withdrawInfoDto.setTransTypeName(TransTypeEnum.getDescriptionByCode(data.getTransType().getCode()));
 | 
	
		
			
				|  |  |  			withdrawInfoDto.setAccountNo(studentWithdraw.getBankCardNo());
 | 
	
		
			
				|  |  |  			withdrawInfoDto.setWithdrawStatus(studentWithdraw.getStatus().getMsg());
 | 
	
		
			
				|  |  | -			//支付账号
 | 
	
		
			
				|  |  | -			dataList.add(withdrawInfoDto);
 | 
	
		
			
				|  |  | -		});
 | 
	
		
			
				|  |  | +			// 支付账号
 | 
	
		
			
				|  |  | +				dataList.add(withdrawInfoDto);
 | 
	
		
			
				|  |  | +			});
 | 
	
		
			
				|  |  |  		BeanUtils.copyProperties(accountDetailPageInfo, result);
 | 
	
		
			
				|  |  |  		result.setRows(dataList);
 | 
	
		
			
				|  |  |  		return result;
 | 
	
	
		
			
				|  | @@ -132,53 +140,47 @@ public class StudentWithdrawServiceImpl extends BaseServiceImpl<String, StudentW
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	@Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | -	public void add(StudentWithdraw withdraw) throws Exception {
 | 
	
		
			
				|  |  | -		SysUserCashAccount account = sysUserCashAccountDao.get(withdraw.getUserId());
 | 
	
		
			
				|  |  | -		//账户状态正常
 | 
	
		
			
				|  |  | -		if(account != null && account.getStatus() == PlatformCashAccountStatusEnum.NORMAL){
 | 
	
		
			
				|  |  | -			//提现金额不大于账户可用余额
 | 
	
		
			
				|  |  | -			if(withdraw.getAmount().compareTo(account.getBalance()) < 1){
 | 
	
		
			
				|  |  | -				//修改账户余额
 | 
	
		
			
				|  |  | -				BigDecimal subtract = account.getBalance().subtract(withdraw.getAmount());
 | 
	
		
			
				|  |  | -				account.setBalance(subtract);
 | 
	
		
			
				|  |  | -				account.setFrozenAmount(account.getFrozenAmount().add(withdraw.getAmount()));
 | 
	
		
			
				|  |  | +	public void apply(Integer userId, String bankCardNo, BigDecimal amount) throws Exception {
 | 
	
		
			
				|  |  | +		SysUserCashAccount account = sysUserCashAccountDao.getLocked(userId);
 | 
	
		
			
				|  |  | +		// 账户状态正常
 | 
	
		
			
				|  |  | +		if (account != null && account.getStatus() == PlatformCashAccountStatusEnum.NORMAL) {
 | 
	
		
			
				|  |  | +			// 提现金额不大于账户可用余额
 | 
	
		
			
				|  |  | +			if (amount.compareTo(account.getBalance()) < 1) {
 | 
	
		
			
				|  |  |  				Date date = new Date();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				StudentWithdraw withdraw = new StudentWithdraw();
 | 
	
		
			
				|  |  | +				withdraw.setAmount(amount);
 | 
	
		
			
				|  |  | +				withdraw.setBankCardNo(bankCardNo);
 | 
	
		
			
				|  |  | +				withdraw.setCreateTime(date);
 | 
	
		
			
				|  |  | +				withdraw.setModifyTime(date);
 | 
	
		
			
				|  |  | +				withdraw.setStatus(DealStatusEnum.ING);
 | 
	
		
			
				|  |  | +				withdraw.setUserId(userId);
 | 
	
		
			
				|  |  | +				withdraw.setWithdrawNo(System.currentTimeMillis() + "");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				// 修改账户余额
 | 
	
		
			
				|  |  | +				BigDecimal subtract = account.getBalance().subtract(amount);
 | 
	
		
			
				|  |  | +				account.setBalance(subtract);
 | 
	
		
			
				|  |  | +				account.setFrozenAmount(account.getFrozenAmount().add(amount));
 | 
	
		
			
				|  |  |  				account.setUpdateTime(date);
 | 
	
		
			
				|  |  |  				sysUserCashAccountDao.update(account);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				withdraw.setBalance(subtract);
 | 
	
		
			
				|  |  | +				studentWithdrawDao.insert(withdraw);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  				SysUserCashAccountDetail cashAccountDetail = new SysUserCashAccountDetail();
 | 
	
		
			
				|  |  |  				cashAccountDetail.setUserId(account.getUserId());
 | 
	
		
			
				|  |  |  				cashAccountDetail.setType(PlatformCashAccountDetailTypeEnum.WITHDRAW);
 | 
	
		
			
				|  |  | -				cashAccountDetail.setAmount(withdraw.getAmount());
 | 
	
		
			
				|  |  | +				cashAccountDetail.setAmount(amount);
 | 
	
		
			
				|  |  |  				cashAccountDetail.setBalance(subtract);
 | 
	
		
			
				|  |  |  				cashAccountDetail.setCreateTime(date);
 | 
	
		
			
				|  |  | +				cashAccountDetail.setStatus(DealStatusEnum.ING);
 | 
	
		
			
				|  |  |  				cashAccountDetailService.insert(cashAccountDetail);
 | 
	
		
			
				|  |  | -				withdraw.setWithdrawNo(RandomStringUtils.random(8));
 | 
	
		
			
				|  |  | -				studentWithdrawDao.insert(withdraw);
 | 
	
		
			
				|  |  | -			}else {
 | 
	
		
			
				|  |  | +			} else {
 | 
	
		
			
				|  |  |  				throw new Exception("账户余额不足");
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  | -		}else {
 | 
	
		
			
				|  |  | +		} else {
 | 
	
		
			
				|  |  |  			throw new Exception("账户状态异常");
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	/**
 | 
	
		
			
				|  |  | -	 * 保存提现记录
 | 
	
		
			
				|  |  | -	 *
 | 
	
		
			
				|  |  | -	 * @param withdrawDto
 | 
	
		
			
				|  |  | -	 */
 | 
	
		
			
				|  |  | -	private Integer saveStudentWithdraw(WithdrawDto withdrawDto) {
 | 
	
		
			
				|  |  | -		IdWorker idWorker = new IdWorker(0, 0);
 | 
	
		
			
				|  |  | -		StudentWithdraw studentWithdraw = new StudentWithdraw();
 | 
	
		
			
				|  |  | -		studentWithdraw.setWithdrawNo(idWorker.nextId());
 | 
	
		
			
				|  |  | -		studentWithdraw.setUserId(withdrawDto.getUserId());
 | 
	
		
			
				|  |  | -		studentWithdraw.setBankCardNo(withdrawDto.getBankCardNo());
 | 
	
		
			
				|  |  | -		studentWithdraw.setAmount(withdrawDto.getAmount());
 | 
	
		
			
				|  |  | -		studentWithdraw.setCreateTime(new Date());
 | 
	
		
			
				|  |  | -		insert(studentWithdraw);
 | 
	
		
			
				|  |  | -		return studentWithdraw.getId();
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  }
 |