|
|
|
@ -2,6 +2,7 @@ package com.ruoyi.applet.service.impl; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker; |
|
|
|
import com.ruoyi.applet.transfer.TransferToUser; |
|
|
|
import com.ruoyi.common.core.sms.AliyunSmsUtils; |
|
|
|
import com.ruoyi.model.domain.AppUsers; |
|
|
|
import com.ruoyi.model.domain.AppletAmountLog; |
|
|
|
import com.ruoyi.model.service.IAppUsersService; |
|
|
|
@ -278,6 +279,8 @@ public class AppletAmountService { |
|
|
|
appletAmountLogService.updateById(appletAmountLog); |
|
|
|
|
|
|
|
// 3. 执行实际的微信提现操作 |
|
|
|
boolean wechatCallFailed = false; |
|
|
|
String failReason = null; |
|
|
|
try { |
|
|
|
TransferToUser.TransferToUserResponse response = this.cashOut(appletAmountLog); |
|
|
|
|
|
|
|
@ -297,8 +300,10 @@ public class AppletAmountService { |
|
|
|
appletAmountLog.getUserId(), appletAmountLog.getAmount(), response.state); |
|
|
|
break; |
|
|
|
case FAIL: |
|
|
|
appletAmountLog.setState(2); // 2-失败 |
|
|
|
log.error("提现审核通过但微信提现失败,用户ID: {}, 金额: {}", appletAmountLog.getUserId(), appletAmountLog.getAmount()); |
|
|
|
// 标记失败,准备退回金额与记录错误信息 |
|
|
|
wechatCallFailed = true; |
|
|
|
failReason = response.packageInfo != null ? ("微信接口失败:" + response.packageInfo) : "微信接口失败"; |
|
|
|
log.error("微信提现失败,用户ID: {}, 金额: {}, 原因: {}", appletAmountLog.getUserId(), appletAmountLog.getAmount(), failReason); |
|
|
|
break; |
|
|
|
default: |
|
|
|
appletAmountLog.setState(0); // 0-处理中 |
|
|
|
@ -310,19 +315,31 @@ public class AppletAmountService { |
|
|
|
appletAmountLog.setPackageInfo(response.packageInfo); |
|
|
|
} |
|
|
|
} else { |
|
|
|
appletAmountLog.setState(2); // 2-失败 |
|
|
|
log.error("提现审核通过但微信提现返回空结果,用户ID: {}, 金额: {}", appletAmountLog.getUserId(), appletAmountLog.getAmount()); |
|
|
|
wechatCallFailed = true; |
|
|
|
failReason = "微信提现返回空结果"; |
|
|
|
log.error("微信提现返回空结果,用户ID: {}, 金额: {}", appletAmountLog.getUserId(), appletAmountLog.getAmount()); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
wechatCallFailed = true; |
|
|
|
failReason = "微信提现执行异常:" + e.getMessage(); |
|
|
|
log.error("微信提现执行异常,用户ID: {}, 金额: {}", appletAmountLog.getUserId(), appletAmountLog.getAmount(), e); |
|
|
|
appletAmountLog.setState(2); // 2-失败 |
|
|
|
} |
|
|
|
|
|
|
|
// 失败时:回退金额、记录错误信息、生成退款流水并标记审核不通过 |
|
|
|
if (wechatCallFailed) { |
|
|
|
handleWithdrawFail(appletAmountLog, auditRemark, failReason); |
|
|
|
} |
|
|
|
|
|
|
|
appletAmountLogService.updateById(appletAmountLog); |
|
|
|
|
|
|
|
log.info("审核通过处理完成,用户ID: {}, 金额: {}, 审核备注: {}", |
|
|
|
appletAmountLog.getUserId(), appletAmountLog.getAmount(), auditRemark); |
|
|
|
|
|
|
|
|
|
|
|
// 只有在非失败情况下发送审核通过通知 |
|
|
|
if (appletAmountLog.getAuditStatus() != null && appletAmountLog.getAuditStatus() == 1 && appletAmountLog.getState() != null && appletAmountLog.getState() != 2) { |
|
|
|
AliyunSmsUtils.sendWithdrawalApprovalNotice(appUsersService.getById(appletAmountLog.getUserId()).getUserTelephone()); |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("审核通过处理失败,用户ID: {}, 金额: {}", appletAmountLog.getUserId(), appletAmountLog.getAmount(), e); |
|
|
|
@ -330,4 +347,76 @@ public class AppletAmountService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 微信接口失败时退回金额、记录错误并生成退款流水 |
|
|
|
*/ |
|
|
|
private void handleWithdrawFail(AppletAmountLog appletAmountLog, String auditRemark, String errorInfo) { |
|
|
|
try { |
|
|
|
AppUsers user = appUsersService.selectAppUsersByUserId(appletAmountLog.getUserId()); |
|
|
|
if (user == null) { |
|
|
|
log.error("退款失败:用户不存在,userId: {}", appletAmountLog.getUserId()); |
|
|
|
// 标记原记录为审核不通过 |
|
|
|
appletAmountLog.setAuditStatus(2); |
|
|
|
appletAmountLog.setState(2); |
|
|
|
appletAmountLog.setRemark((auditRemark == null ? "" : auditRemark) + ";退款失败:用户不存在"); |
|
|
|
appletAmountLog.setErrorInfo(errorInfo); |
|
|
|
appletAmountLogService.updateById(appletAmountLog); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
BigDecimal refundAmount = appletAmountLog.getAmount() == null ? BigDecimal.ZERO : appletAmountLog.getAmount(); |
|
|
|
if (appletAmountLog.getMoneyType() == 0) { |
|
|
|
BigDecimal original = user.getMoney() == null ? BigDecimal.ZERO : user.getMoney(); |
|
|
|
BigDecimal updated = original.add(refundAmount); |
|
|
|
user.setMoney(updated); |
|
|
|
log.info("合伙人钱包退款:{},用户ID: {},原余额: {},新余额: {}", refundAmount, user.getUserId(), original, updated); |
|
|
|
} else if (appletAmountLog.getMoneyType() == 1) { |
|
|
|
BigDecimal original = user.getPrice() == null ? BigDecimal.ZERO : user.getPrice(); |
|
|
|
BigDecimal updated = original.add(refundAmount); |
|
|
|
user.setPrice(updated); |
|
|
|
log.info("伴宠师钱包退款:{},用户ID: {},原余额: {},新余额: {}", refundAmount, user.getUserId(), original, updated); |
|
|
|
} else if (appletAmountLog.getMoneyType() == 2) { |
|
|
|
BigDecimal original = user.getBaoPrice() == null ? BigDecimal.ZERO : user.getBaoPrice(); |
|
|
|
BigDecimal updated = original.subtract(refundAmount); |
|
|
|
user.setBaoPrice(updated); |
|
|
|
log.info("保证金退款:{},用户ID: {},原余额: {},新余额: {}", refundAmount, user.getUserId(), original, updated); |
|
|
|
} |
|
|
|
|
|
|
|
appUsersService.updateAppUsers(user); |
|
|
|
|
|
|
|
// 更新原提现记录 |
|
|
|
appletAmountLog.setAuditStatus(2); // 审核不通过 |
|
|
|
appletAmountLog.setState(2); // 失败/已退回 |
|
|
|
String baseRemark = (auditRemark == null ? "" : auditRemark); |
|
|
|
String fail = (errorInfo == null ? "未知错误" : errorInfo); |
|
|
|
String remark = "如需帮助请联系客服"; |
|
|
|
appletAmountLog.setRemark(remark); |
|
|
|
appletAmountLog.setErrorInfo(errorInfo); |
|
|
|
appletAmountLogService.updateById(appletAmountLog); |
|
|
|
|
|
|
|
// 记录退款流水 |
|
|
|
AppletAmountLog refundLog = new AppletAmountLog(); |
|
|
|
refundLog.setUserId(user.getUserId()); |
|
|
|
if (appletAmountLog.getMoneyType() == 0) { |
|
|
|
refundLog.setTitle("合伙人钱包提现审核不通过退款"); |
|
|
|
} else if (appletAmountLog.getMoneyType() == 1) { |
|
|
|
refundLog.setTitle("伴宠师钱包提现审核不通过退款"); |
|
|
|
} else if (appletAmountLog.getMoneyType() == 2) { |
|
|
|
refundLog.setTitle("保证金提现审核不通过退款"); |
|
|
|
} |
|
|
|
refundLog.setAmount(refundAmount); |
|
|
|
refundLog.setType(0); // 收入(退款) |
|
|
|
refundLog.setState(1); // 成功 |
|
|
|
refundLog.setCreateTime(LocalDateTime.now()); |
|
|
|
refundLog.setMoneyType(appletAmountLog.getMoneyType()); |
|
|
|
refundLog.setNameValue(appletAmountLog.getNameValue()); |
|
|
|
refundLog.setAuditStatus(1); // 退款记录自动通过 |
|
|
|
appletAmountLogService.save(refundLog); |
|
|
|
|
|
|
|
log.info("微信失败已退款,用户ID: {},金额: {},原因: {}", user.getUserId(), refundAmount, errorInfo); |
|
|
|
} catch (Exception ex) { |
|
|
|
log.error("处理微信失败退款异常,用户ID: {},金额: {}", appletAmountLog.getUserId(), appletAmountLog.getAmount(), ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |