diff --git a/src/main/java/com/sqx/modules/job/task/TempCashOutTask.java b/src/main/java/com/sqx/modules/job/task/TempCashOutTask.java index 37b96385..84b91eb9 100644 --- a/src/main/java/com/sqx/modules/job/task/TempCashOutTask.java +++ b/src/main/java/com/sqx/modules/job/task/TempCashOutTask.java @@ -8,12 +8,19 @@ import com.sqx.modules.app.dao.UserMoneyDao; import com.sqx.modules.app.dao.UserMoneyDetailsDao; import com.sqx.modules.app.entity.UserMoney; import com.sqx.modules.app.entity.UserMoneyDetails; +import com.sqx.modules.app.service.UserMoneyDetailsService; +import com.sqx.modules.app.service.UserMoneyService; import com.sqx.modules.pay.dao.CashOutDao; import com.sqx.modules.pay.entity.CashOut; +import com.sqx.modules.pay.service.WuyouCallbackService; import com.sqx.modules.pay.wuyou.BaseResp; import com.sqx.modules.pay.wuyou.WuyouPay; import com.sqx.modules.sys.dao.SysUserMoneyDao; import com.sqx.modules.sys.entity.SysUserMoney; +import com.sqx.modules.sys.entity.SysUserMoneyDetails; +import com.sqx.modules.sys.service.SysUserMoneyDetailsService; +import com.sqx.modules.sys.service.SysUserMoneyService; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -23,81 +30,45 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.List; @Component @Profile({"prod"}) -public class TempCashOutTask{ +public class TempCashOutTask { @Resource private CashOutDao cashOutDao; @Resource private WuyouPay wuyouPay; + @Resource + private WuyouCallbackService wuyouCallbackService; private Logger logger = LoggerFactory.getLogger(getClass()); - @Autowired - private UserMoneyDao userMoneyDao; - @Autowired - private SysUserMoneyDao sysUserMoneyDao; - @Autowired - private UserMoneyDetailsDao userMoneyDetailsDao; + @Scheduled(cron = "0 0/8 * * * ? ") public void run() { - logger.info("提现开始"); List cashOuts = cashOutDao.selectTemp(DateUtil.offsetMinute(DateUtil.date(), 15)); - logger.info("待处理订单: {}", JSONObject.toJSONString(cashOuts)); + logger.info("定时查询提现订单 待处理订单: {}", cashOuts.size()); + + List sucOrderList = new ArrayList<>(); + List failOrderList = new ArrayList<>(); for (CashOut cashOut : cashOuts) { try { - BaseResp baseResp; -// if (DateUtil.parse(cashOut.getCreateAt()).compareTo(DateUtil.parse("2025-01-06 16:50:00")) > 0) { - baseResp = wuyouPay.queryExtractOrder(cashOut.getOrderNumber(), cashOut.getUserId(), cashOut.getUserType() != 2, cashOut.getMoney()); -// }else { -// baseResp = wuyouPay.queryExtractOrder(cashOut.getOrderNumber(), cashOut.getMoney()); -// } - logger.info("baseResp:{} ", JSONUtil.toJsonStr(baseResp)); - if (baseResp.getStatus() != null && (baseResp.getStatus().equals(2) || baseResp.getStatus().equals(10000))){ - logger.info("success:{} ", cashOut.getOrderNumber()); - cashOut.setState(1); - cashOut.setOutAt(DateUtil.now()); - cashOut.setRefund(null); - cashOut.setOutAt(DateUtil.now()); - cashOutDao.update(cashOut, new LambdaQueryWrapper().eq(CashOut::getUserId, cashOut.getUserId()).eq(CashOut::getId, cashOut.getId())); - }else if (baseResp.getStatus() != null && (baseResp.getStatus().equals(3) || baseResp.getStatus().equals(99999)) ){ - logger.info("提现失败, 返还余额, 用户: {}, {}, 金额: {}", cashOut.getUserId(), cashOut.getUserName(), cashOut.getMoney()); - if (cashOut.getUserType() != null && cashOut.getUserType().equals(2)) { - SysUserMoney sysUserMoney = sysUserMoneyDao.selectOne(new LambdaQueryWrapper().eq(SysUserMoney::getUserId, cashOut.getUserId())); - if (sysUserMoney != null) { - sysUserMoneyDao.incrMoney(cashOut.getMoney(), cashOut.getUserId()); - } - }else { - UserMoney userMoney = userMoneyDao.selectOne(new LambdaQueryWrapper().eq(UserMoney::getUserId, cashOut.getUserId())); - if (userMoney != null) { - userMoneyDao.incrMoney(cashOut.getMoney(), cashOut.getUserId()); - } - } - - cashOut.setOutAt(DateUtil.date().toString()); - cashOut.setState(2); - cashOut.setRefund(baseResp.getMsg()); - cashOutDao.update(cashOut, new LambdaQueryWrapper().eq(CashOut::getUserId, cashOut.getUserId()).eq(CashOut::getId, cashOut.getId())); - - UserMoneyDetails details = new UserMoneyDetails(); - details.setUserId(cashOut.getUserId()); - details.setTitle("提现失败存入余额" + cashOut.getMoney() + "元"); - details.setClassify(4); - details.setType(1); - details.setState(2); - details.setMoney(new BigDecimal(cashOut.getMoney())); - details.setContent("提现失败存入余额"); - details.setMoneyType(1); - details.setCreateTime(DateUtil.date().toString()); - userMoneyDetailsDao.insert(details); + BaseResp baseResp = wuyouPay.queryExtractOrder(cashOut.getOrderNumber(), cashOut.getUserId(), cashOut.getUserType() != 2, cashOut.getMoney()); + int i = wuyouCallbackService.executeExtractCallback(cashOut, baseResp); + if (i == 1) { + sucOrderList.add(cashOut.getOrderNumber()); + } else { + failOrderList.add(cashOut.getOrderNumber()); } - }catch (Exception e) { + } catch (Exception e) { logger.error("体现定时任务查询出错", e); } } - logger.info("提现结束"); + + logger.info("定时查询提现订单 提现结束, 成功:{}条, 失败:{}条", sucOrderList.size(), failOrderList.size()); + logger.info("定时查询提现订单 提现结束, 成功:{}, 失败:{}", JSONUtil.toJsonStr(sucOrderList), JSONUtil.toJsonStr(failOrderList)); } } diff --git a/src/main/java/com/sqx/modules/pay/controller/app/WuyouController.java b/src/main/java/com/sqx/modules/pay/controller/app/WuyouController.java index 227d4996..cf6ca044 100644 --- a/src/main/java/com/sqx/modules/pay/controller/app/WuyouController.java +++ b/src/main/java/com/sqx/modules/pay/controller/app/WuyouController.java @@ -31,6 +31,7 @@ import com.sqx.modules.pay.dao.CashOutDao; import com.sqx.modules.pay.dao.PayDetailsDao; import com.sqx.modules.pay.entity.CashOut; import com.sqx.modules.pay.entity.PayDetails; +import com.sqx.modules.pay.service.WuyouCallbackService; import com.sqx.modules.pay.wuyou.BaseResp; import com.sqx.modules.pay.wuyou.Encrypt; import com.sqx.modules.pay.wuyou.NotifyDto; @@ -47,6 +48,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; +import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.math.BigDecimal; import java.text.SimpleDateFormat; @@ -72,6 +74,8 @@ public class WuyouController { private final CashOutDao cashOutDao; private final WuyouPay wuyouPay; private final TempOrdersTask ordersTask; + @Resource + private WuyouCallbackService wuyouCallbackService; WuyouController(OrdersService ordersService, PayDetailsDao payDetailsDao, CashOutDao cashOutDao, UserMoneyService userMoneyService, UserMoneyDetailsService userMoneyDetailsService, TempOrdersTask ordersTask, @@ -264,8 +268,8 @@ public class WuyouController { return "success"; } - - private void updateCashAsync(NotifyDto notifyDto) { + @Transactional + public void updateCashAsync(NotifyDto notifyDto) { log.info("无忧支付提现回调, {}", notifyDto); Map params = new HashMap<>(); params.put("callbacks", notifyDto.getCallbacks()); @@ -277,7 +281,6 @@ public class WuyouController { String sign = Encrypt.getParamsSign(params); if (!sign.equals(notifyDto.getSign())) { log.error("无忧支付提现回调签名错误, 参数: {},签名结果:{}", JSONObject.toJSONString(notifyDto), sign); -// return "签名错误"; } String orderNo = notifyDto.getOut_trade_no(); @@ -303,24 +306,7 @@ public class WuyouController { CashOut cashOut = cashOutDao.selectOne(orderNumber); if (cashOut != null) { - if ("2".equals(notifyDto.getStatus())) { - cashOut.setState(1); - cashOut.setOutAt(DateUtil.now()); - } else if (!cashOut.getState().equals(2)) { - cashOut.setState(2); - cashOut.setRefund(notifyDto.getMsg()); - if (StringUtils.isNotBlank(notifyDto.getMsg()) && notifyDto.getMsg().contains("已驳回")) { - cashOut.setRefund("提现失败,请检查支付宝账号与收款人姓名后重试。"); - } - - UserMoneyDetails userMoneyDetails = new UserMoneyDetails( - cashOut.getUserId(), null, null, "提现失败", 4, 1, 1, - new BigDecimal(cashOut.getMoney()), "提现失败存入余额" + cashOut.getMoney() + "元", 1, cashOut.getId()); - //存入余额 钱 - userMoneyService.updateAmount(1, cashOut.getUserId(), Double.parseDouble(cashOut.getMoney())); - userMoneyDetailsService.save(userMoneyDetails); - } - cashOutDao.updateById(cashOut); + wuyouCallbackService.executeExtractCallback(cashOut, notifyDto); } } } diff --git a/src/main/java/com/sqx/modules/pay/service/WuyouCallbackService.java b/src/main/java/com/sqx/modules/pay/service/WuyouCallbackService.java new file mode 100644 index 00000000..1d26705c --- /dev/null +++ b/src/main/java/com/sqx/modules/pay/service/WuyouCallbackService.java @@ -0,0 +1,17 @@ +package com.sqx.modules.pay.service; + +import com.sqx.modules.pay.entity.CashOut; +import com.sqx.modules.pay.wuyou.BaseResp; +import com.sqx.modules.pay.wuyou.NotifyDto; + +/** + * @author GYJoker + */ +public interface WuyouCallbackService { + + int executeExtractCallback(CashOut cashOut, BaseResp resp); + + void executeExtractCallback(CashOut cashOut, NotifyDto notifyDto); + + int executeExtractCallback(CashOut cashOut, int status, String reason); +} diff --git a/src/main/java/com/sqx/modules/pay/service/impl/CashOutServiceImpl.java b/src/main/java/com/sqx/modules/pay/service/impl/CashOutServiceImpl.java index 61780a04..15483f6f 100644 --- a/src/main/java/com/sqx/modules/pay/service/impl/CashOutServiceImpl.java +++ b/src/main/java/com/sqx/modules/pay/service/impl/CashOutServiceImpl.java @@ -530,7 +530,7 @@ public class CashOutServiceImpl extends ServiceImpl impleme } if (!userInfo.getZhiFuBaoName().equals(userDetailInfo.getCertName())) { - return Result.error(500, "支付宝和实名姓名不太无法提现!"); + return Result.error(500, "支付宝和实名姓名不符无法提现!"); } alipayAccount = userInfo.getZhiFuBao(); alipayName = userInfo.getZhiFuBaoName(); diff --git a/src/main/java/com/sqx/modules/pay/service/impl/WuyouCallbackServiceImpl.java b/src/main/java/com/sqx/modules/pay/service/impl/WuyouCallbackServiceImpl.java new file mode 100644 index 00000000..b3c22b7d --- /dev/null +++ b/src/main/java/com/sqx/modules/pay/service/impl/WuyouCallbackServiceImpl.java @@ -0,0 +1,148 @@ +package com.sqx.modules.pay.service.impl; + +import cn.hutool.core.date.DateUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.sqx.modules.app.entity.UserMoneyDetails; +import com.sqx.modules.app.service.UserMoneyDetailsService; +import com.sqx.modules.app.service.UserMoneyService; +import com.sqx.modules.pay.dao.CashOutDao; +import com.sqx.modules.pay.entity.CashOut; +import com.sqx.modules.pay.service.WuyouCallbackService; +import com.sqx.modules.pay.wuyou.BaseResp; +import com.sqx.modules.pay.wuyou.NotifyDto; +import com.sqx.modules.sys.entity.SysUserMoneyDetails; +import com.sqx.modules.sys.service.SysUserMoneyDetailsService; +import com.sqx.modules.sys.service.SysUserMoneyService; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.math.BigDecimal; + +/** + * @author GYJoker + */ +@Service +public class WuyouCallbackServiceImpl implements WuyouCallbackService { + @Autowired + private UserMoneyService userMoneyService; + @Autowired + private SysUserMoneyService sysUserMoneyService; + @Autowired + private UserMoneyDetailsService userMoneyDetailsService; + @Autowired + private SysUserMoneyDetailsService sysUserMoneyDetailsService; + + @Resource + private CashOutDao cashOutDao; + + @Override + @Transactional + public int executeExtractCallback(CashOut cashOut, BaseResp resp) { + int status = 0; + if (resp.getStatus() != null && resp.getStatus().equals(2)) { + status = 1; + } else if (resp.getStatus() != null && (resp.getStatus().equals(3) || resp.getStatus().equals(99999))) { + status = 2; + } + + return executeExtractCallback(cashOut, status, resp.getMsg()); + } + + @Override + @Transactional + public void executeExtractCallback(CashOut cashOut, NotifyDto notifyDto) { + int status = 0; + if (notifyDto.getStatus() != null && "2".equals(notifyDto.getStatus())) { + status = 1; + } else if (notifyDto.getStatus() != null && "3".equals(notifyDto.getStatus())) { + status = 2; + } + + executeExtractCallback(cashOut, status, notifyDto.getMsg()); + } + + /** + * 处理提现结果 + * @param cashOut 提现实体类 + * @param status 提现 + * @param reason + * @return + */ + @Override + @Transactional + public int executeExtractCallback(CashOut cashOut, int status, String reason) { + if (StringUtils.isNotBlank(reason) && reason.contains("已驳回")) { + reason = "提现失败,请检查支付宝账号与收款人姓名后重试。"; + } + BigDecimal money = new BigDecimal(cashOut.getMoney()); + if (status == 1) { + cashOut.setState(1); + cashOut.setOutAt(DateUtil.now()); + cashOut.setRefund(null); + cashOut.setOutAt(DateUtil.now()); + cashOutDao.update(cashOut, new LambdaQueryWrapper().eq(CashOut::getUserId, cashOut.getUserId()).eq(CashOut::getId, cashOut.getId())); + + return 1; + } + + if (status == 2) { + cashOut.setOutAt(DateUtil.date().toString()); + cashOut.setState(2); + cashOut.setRefund(reason); + + if (cashOut.getUserType() != null && cashOut.getUserType().equals(2)) { + int count = sysUserMoneyDetailsService.count(new LambdaQueryWrapper() + .eq(SysUserMoneyDetails::getUserId, cashOut.getUserId()) + .eq(SysUserMoneyDetails::getClassify, 4) + .eq(SysUserMoneyDetails::getState, 2) + .eq(SysUserMoneyDetails::getMoneyType, 1) + .eq(SysUserMoneyDetails::getType, 1) + .eq(SysUserMoneyDetails::getSourceId, cashOut.getId()) + ); + if (count > 0) { + return 0; + } + + SysUserMoneyDetails details = new SysUserMoneyDetails( + cashOut.getUserId(), cashOut.getUserId(), null, "提现失败存入余额", 4, 1, 2, + money, "提现失败存入余额" + cashOut.getMoney() + "元", 1); + sysUserMoneyDetailsService.save(details); + + sysUserMoneyService.updateSysMoney(1, cashOut.getUserId(), money.doubleValue()); + } else { + int count = userMoneyDetailsService.count(new LambdaQueryWrapper() + .eq(UserMoneyDetails::getUserId, cashOut.getUserId()) + .eq(UserMoneyDetails::getClassify, 4) + .eq(UserMoneyDetails::getState, 2) + .eq(UserMoneyDetails::getMoneyType, 1) + .eq(UserMoneyDetails::getType, 1) + .eq(UserMoneyDetails::getSourceId, cashOut.getId()) + ); + if (count > 0) { + return 0; + } + + UserMoneyDetails details = new UserMoneyDetails(); + details.setUserId(cashOut.getUserId()); + details.setContent("提现失败存入余额" + cashOut.getMoney() + "元"); + details.setClassify(4); + details.setType(1); + details.setState(2); + details.setMoney(new BigDecimal(cashOut.getMoney())); + details.setTitle("提现失败存入余额"); + details.setMoneyType(1); + details.setCreateTime(DateUtil.date().toString()); + details.setSourceId(cashOut.getId()); + userMoneyDetailsService.save(details); + + userMoneyService.updateAmount(1, cashOut.getUserId(), money.doubleValue()); + } + + cashOutDao.update(cashOut, new LambdaQueryWrapper().eq(CashOut::getUserId, cashOut.getUserId()).eq(CashOut::getId, cashOut.getId())); + } + return 0; + } +}