package com.stylefeng.guns.modular.system.service.impl;
|
|
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
import com.stylefeng.guns.modular.system.dao.WithdrawalMapper;
|
import com.stylefeng.guns.modular.system.model.Driver;
|
import com.stylefeng.guns.modular.system.model.UserInfo;
|
import com.stylefeng.guns.modular.system.model.Withdrawal;
|
import com.stylefeng.guns.modular.system.service.IUserInfoService;
|
import com.stylefeng.guns.modular.system.service.IWithdrawalService;
|
//import com.stylefeng.guns.modular.system.util.ICBCPayUtil;
|
import com.stylefeng.guns.modular.system.util.PayMoneyUtil;
|
import com.stylefeng.guns.modular.system.util.ResultUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Isolation;
|
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.util.*;
|
|
|
@Service
|
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
|
public class WithdrawalServiceImpl extends ServiceImpl<WithdrawalMapper, Withdrawal> implements IWithdrawalService {
|
|
@Resource
|
private WithdrawalMapper withdrawalMapper;
|
|
@Autowired
|
private IUserInfoService userInfoService;
|
|
// @Autowired
|
// private ICBCPayUtil icbcPayUtil;
|
|
@Autowired
|
private PayMoneyUtil payMoneyUtil;
|
|
private Map<String, Timer> timerMap = new HashMap<>();
|
|
|
|
|
/**
|
* 提现操作
|
* @param money
|
* @param code
|
* @param name
|
* @param uid
|
* @throws Exception
|
*/
|
@Override
|
public ResultUtil withdrawal(Double money, String code, String name, Integer uid) throws Exception {
|
if(money.compareTo(0D) <= 0){
|
return ResultUtil.error("提现金额必须大于0");
|
}
|
UserInfo userInfo = userInfoService.selectById(uid);
|
if(null == userInfo.getBalance()){
|
return ResultUtil.error("账户没有余额,不能提现");
|
}
|
if(userInfo.getBalance().compareTo(money) < 0){
|
return ResultUtil.error("提现金额必须小于账户余额");
|
}
|
|
Withdrawal withdrawal = new Withdrawal();
|
withdrawal.setBalance(userInfo.getBalance());
|
withdrawal.setCode(code);
|
withdrawal.setFlag(1);
|
withdrawal.setInsertTime(new Date());
|
withdrawal.setMoney(money);
|
withdrawal.setName(name);
|
withdrawal.setState(1);
|
withdrawal.setUserId(uid);
|
withdrawal.setUserType(1);
|
|
// ResultUtil<String> transfer = icbcPayUtil.transfer(Double.valueOf(withdrawal.getMoney() * 100).longValue(), withdrawal.getCode(), withdrawal.getName());
|
// if(transfer.getCode() != 200){
|
// return transfer;
|
// }
|
// withdrawal.setSerialNo(transfer.getData());
|
this.insert(withdrawal);
|
|
double v = new BigDecimal(userInfo.getBalance()).subtract(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
|
userInfo.setBalance(v);
|
userInfoService.updateById(userInfo);
|
return ResultUtil.success();
|
}
|
|
|
/**
|
* 获取历史提交数
|
* @param uid
|
* @param pageNum
|
* @param size
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public List<Map<String, Object>> queryWithdrawal(Integer uid, Integer pageNum, Integer size) throws Exception {
|
pageNum = (pageNum - 1) * size;
|
return withdrawalMapper.queryWithdrawal(uid, 1, pageNum, size);
|
}
|
|
|
/**
|
* 提现审核处理
|
* @param id
|
* @param state
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public ResultUtil withdrawalAudit(Integer id, Integer state) throws Exception {
|
Withdrawal withdrawal = this.selectById(id);
|
// if(withdrawal.getState() == 2){
|
// return ResultUtil.error("申请已审核通过,不能重复提交");
|
// }
|
// if(withdrawal.getState() == 3){
|
// return ResultUtil.error("申请已审核拒绝,不能重复提交");
|
// }
|
if(state == 1){//审核通过
|
TimerTask timerTask = new TimerTask() {
|
@Override
|
public void run() {
|
try {
|
Map<String, String> map = payMoneyUtil.wxPayBank("用户提现", withdrawal.getMoney().toString(), withdrawal.getId().toString(), withdrawal.getCode(), withdrawal.getName(), "");
|
if("SUCCESS".equals(map.get("return_code"))){
|
withdrawal.setSerialNo(map.get("payment_no"));
|
withdrawal.setState(2);
|
WithdrawalServiceImpl.this.updateById(withdrawal);
|
Timer timer = timerMap.get(withdrawal.getSerialNo());
|
timer.cancel();
|
}else{
|
UserInfo userInfo = userInfoService.selectById(withdrawal.getUserId());
|
userInfo.setBalance(new BigDecimal(userInfo.getBalance()).add(new BigDecimal(withdrawal.getMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
|
userInfoService.updateById(userInfo);
|
withdrawal.setState(3);
|
WithdrawalServiceImpl.this.updateById(withdrawal);
|
|
Timer timer = timerMap.get(withdrawal.getSerialNo());
|
timer.cancel();
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}
|
};
|
Timer timer = new Timer();
|
timer.schedule(timerTask, 1000, 10000);//1秒钟后间隔10秒钟查询交易结果
|
timerMap.put(withdrawal.getSerialNo(), timer);
|
}
|
if(state == 2){//审核拒绝
|
withdrawal.setState(3);
|
this.updateById(withdrawal);
|
}
|
return ResultUtil.success();
|
}
|
}
|