1. 分账测试

This commit is contained in:
邱贞招 2024-06-01 18:39:48 +08:00
parent 13397d5c78
commit 5172cb184b
2 changed files with 101 additions and 84 deletions
electripper-system/src/main
java/com/ruoyi/system/service/impl
resources/mapper/system

View File

@ -19,6 +19,7 @@ import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.onenet.Token;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.AttachVo;
import com.ruoyi.system.mapper.AsDeviceMapper;
import com.ruoyi.system.mapper.AsUserMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.*;
@ -45,6 +46,8 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
@ -94,15 +97,22 @@ public class CallbackServiceImpl implements CallbackService {
@Resource
private AsUserMapper asUserMapper;
@Resource
private AsDeviceMapper asDeviceMapper;
@Autowired
private ScheduledExecutorService scheduledExecutorService;
/**
* 微信支付回调
*/
@Override
@SneakyThrows
@Transactional
public void weChat(HttpServletRequest request) {
String body = HttpUtils.getBody(request);
logger.info("【微信支付回调】接收对象 : " + JSON.toJSONString(body));
logger.info("【微信支付回调】接收对象(未验签) : " + JSON.toJSONString(body));
// 解析通知数据
Notification notification = JSON.parseObject(body, Notification.class);
String outTradeNo;
@ -113,6 +123,8 @@ public class CallbackServiceImpl implements CallbackService {
Transaction transaction = checkAndParse(request, body, Transaction.class);
if (Transaction.TradeStateEnum.SUCCESS.equals(transaction.getTradeState())) {
// 充值成功后的业务处理
logger.info("【微信支付回调】交易对象(验签后) : " + JSON.toJSONString(transaction));
String transactionId = transaction.getTransactionId();
AttachVo attachVo = JSONObject.parseObject(transaction.getAttach(),AttachVo.class);
logger.info("【微信支付回调】附加信息 : " + JSON.toJSONString(attachVo));
outTradeNo = transaction.getOutTradeNo();
@ -123,7 +135,7 @@ public class CallbackServiceImpl implements CallbackService {
/** 支付回调逻辑 1. 处理预约还是开锁 电压 */
AsDevice asDevice = null;
if(StrUtil.isNotBlank(order.getSn())){
asDevice = asDeviceService.selectAsDeviceBySn(order.getSn());
asDevice = asDeviceMapper.selectAsDeviceBySn(order.getSn());
}
//先判断是骑行订单还是押金如果是骑行订单
// 还要区分是取消预约支付
@ -148,92 +160,92 @@ public class CallbackServiceImpl implements CallbackService {
Integer autoRefundDeposit = rule.getAutoRefundDeposit();
EtOperatingArea area = etOperatingAreaService.selectEtOperatingAreaByAreaId(order.getAreaId());
if(autoRefundDeposit!=null && autoRefundDeposit>0){
//创建一个定时器TimerTask计算出退还时间后执行退款操作
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
// 退款
Long userId = order.getUserId();
EtOrder etOrder = new EtOrder();
etOrder.setUserId(userId);
etOrder.setPaid(ServiceConstants.ORDER_PAY_STATUS_PAID);
etOrder.setType(ServiceConstants.ORDER_TYPE_DEPOSIT);
etOrder.setStatus(ServiceConstants.ORDER_STATUS_ORDER_END);
List<EtOrder> etOrders = orderService.selectEtOrderList(etOrder);
if (etOrders.size() == 0 || ObjectUtil.isNull(etOrders) ) {
throw new ServiceException("押金充值记录不存在");
}
Optional<EtOrder> latestOrder = etOrders.stream()
.max(Comparator.comparing(EtOrder::getPayTime));
if (latestOrder.isPresent()) {
EtOrder newestOrder = latestOrder.get();
// 处理找到的最新支付时间的订单
String deposit = area.getDeposit();
if(newestOrder.getTotalFee().compareTo(new BigDecimal(deposit))!=0){
throw new ServiceException("押金充值记录与当前运营区的押金不同");
}
Refund refund = wxPayService.refund(newestOrder,autoRefundDeposit+"个小时后自动退款", newestOrder.getTotalFee());
EtRefund refund1= orderService.createRefund(etOrder, newestOrder.getTotalFee(), null, null, null, null, refund,ServiceConstants.REFUND_TYPE_DEPOSIT);
int i = etRefundService.insertEtRefund(refund1);
if(i>0){
logger.info("【自动退款】保存退款对象成功");
}
// 新增资金流水记录
capitalFlowRecords(newestOrder,ServiceConstants.FLOW_TYPE_DISBURSE,ServiceConstants.ORDER_TYPE_DEPOSIT_REFUND);
} else {
throw new ServiceException("没有找到押金充值记录");
}
}
};
//创建一个定时器计算出退还时间后执行退款操作
// 往后推autoRefundDeposit小时执行
Date refundTime = DateUtils.getTimeAfterXHours(order.getPayTime(), autoRefundDeposit);
timer.schedule(task, refundTime);
}
// 一分钟后请求分账
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
logger.info("【微信支付回调】开始请求分账------------");
// 请求分账
List<CreateOrderReceiver> receivers = new ArrayList<>();
// 获取到合伙人的openid
SysUser sysUser = new SysUser();
sysUser.setUserType("03");
sysUser.setAreaId(area.getAreaId());
List<SysUser> sysUsers = userMapper.selectUserList(sysUser);
BigDecimal dividendAmount = BigDecimal.ZERO;
for (SysUser sysUser1 : sysUsers){
AsUser asUser1 = userService.selectUserById(sysUser1.getAppUserId());
if(asUser1!=null && asUser1.getWxopenid()!=null){
CreateOrderReceiver receiver = new CreateOrderReceiver();
receiver.setType(ReceiverType.PERSONAL_OPENID.name());
receiver.setAccount(asUser1.getWxopenid());
String dividendItem = sysUser1.getDividendItem();
if(dividendItem.contains("1")){
dividendAmount.add(order.getRidingFee().add(order.getAppointmentFee()));//1-骑行费骑行费+预约费
}else if(dividendItem.contains("2")){
dividendAmount.add(order.getManageFee().add(order.getManageFee()));//2-调度费调度费+管理费
}
BigDecimal multiply = dividendAmount.multiply(new BigDecimal(sysUser1.getDividendProportion()).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(100));
logger.info(sysUser1.getUserName()+"分账比例:"+sysUser1.getDividendProportion()+"%,分账金额:"+multiply);
receiver.setAmount(multiply.longValue());
receiver.setDescription("系统自动分账");
receivers.add(receiver);
}
scheduledExecutorService.schedule(() -> {
// 退款
Long userId = order.getUserId();
EtOrder etOrder = new EtOrder();
etOrder.setUserId(userId);
etOrder.setPaid(ServiceConstants.ORDER_PAY_STATUS_PAID);
etOrder.setType(ServiceConstants.ORDER_TYPE_DEPOSIT);
etOrder.setStatus(ServiceConstants.ORDER_STATUS_ORDER_END);
List<EtOrder> etOrders = orderService.selectEtOrderList(etOrder);
if (etOrders.size() == 0 || ObjectUtil.isNull(etOrders) ) {
throw new ServiceException("押金充值记录不存在");
}
OrdersEntity ordersEntity = wxPayService.createOrder(outTradeNo,receivers);
if(ordersEntity!=null){
logger.info("【微信支付回调】发起分账响应:【{}】",JSON.toJSON(ordersEntity));
}else{
logger.info("【微信支付回调】发起分账失败");
throw new ServiceException("发起分账失败");
Optional<EtOrder> latestOrder = etOrders.stream()
.max(Comparator.comparing(EtOrder::getPayTime));
if (latestOrder.isPresent()) {
EtOrder newestOrder = latestOrder.get();
// 处理找到的最新支付时间的订单
String deposit = area.getDeposit();
if(newestOrder.getTotalFee().compareTo(new BigDecimal(deposit))!=0){
throw new ServiceException("押金充值记录与当前运营区的押金不同");
}
Refund refund = wxPayService.refund(newestOrder,autoRefundDeposit+"个小时后自动退款", newestOrder.getTotalFee());
EtRefund refund1= orderService.createRefund(etOrder, newestOrder.getTotalFee(), null, null, null, null, refund,ServiceConstants.REFUND_TYPE_DEPOSIT);
int i = etRefundService.insertEtRefund(refund1);
if(i>0){
logger.info("【自动退款】保存退款对象成功");
}
// 新增资金流水记录
capitalFlowRecords(newestOrder,ServiceConstants.FLOW_TYPE_DISBURSE,ServiceConstants.ORDER_TYPE_DEPOSIT_REFUND);
} else {
throw new ServiceException("没有找到押金充值记录");
}
}, autoRefundDeposit, TimeUnit.HOURS);
}
logger.info("=================【微信支付回调】10秒后开始请求分账==================");
// 30秒后请求分账
scheduledExecutorService.schedule(() -> {
logger.info("=================【微信支付回调】开始请求分账==================");
logger.info("区域对象====="+JSON.toJSONString(area));
logger.info("订单对象====="+JSON.toJSONString(order));
// 请求分账
List<CreateOrderReceiver> receivers = new ArrayList<>();
// 获取到合伙人的openid
SysUser sysUser = new SysUser();
sysUser.setUserType("03");
sysUser.setAreaId(area.getAreaId());
List<SysUser> sysUsers = userMapper.selectUserList(sysUser);
for (SysUser sysUser1 : sysUsers){
AsUser asUser1 = asUserMapper.selectUserById(sysUser1.getAppUserId());
if(asUser1!=null && asUser1.getWxopenid()!=null){
BigDecimal dividendAmount = BigDecimal.ZERO;
logger.info("=============系统用户sysUser1============"+JSON.toJSONString(sysUser1));
CreateOrderReceiver receiver = new CreateOrderReceiver();
receiver.setType(ReceiverType.PERSONAL_OPENID.name());
receiver.setAccount(asUser1.getWxopenid());
String dividendItem = sysUser1.getDividendItem();
logger.info("=================分账项目dividendItem=================="+dividendItem);
if(dividendItem.contains("1")){
logger.info("=================骑行费(骑行费+预约费)==================");
dividendAmount = dividendAmount.add(order.getRidingFee().add(order.getAppointmentFee()));//1-骑行费骑行费+预约费
}
if(dividendItem.contains("2")){
logger.info("=================调度费(调度费+管理费)==================");
dividendAmount = dividendAmount.add(order.getManageFee().add(order.getDispatchFee()));//2-调度费调度费+管理费
}
logger.info("=================分账金额dividendAmount=================="+dividendAmount);
BigDecimal divide = new BigDecimal(sysUser1.getDividendProportion()).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
logger.info("=================分账比例%=================="+divide);
BigDecimal multiply = dividendAmount.multiply(divide);
logger.info(sysUser1.getUserName()+"分账比例:"+sysUser1.getDividendProportion()+"%,分账金额:"+multiply);
receiver.setAmount(multiply.multiply(new BigDecimal(100)).longValue());
receiver.setDescription("系统自动分账");
receivers.add(receiver);
}
}
};
timer.schedule(task, new Date(System.currentTimeMillis() + 30000));// 30秒后发起分账
OrdersEntity ordersEntity = wxPayService.createOrder(transactionId,receivers);
if(ordersEntity!=null){
logger.info("【微信支付回调】发起分账响应:【{}】",JSON.toJSON(ordersEntity));
}else{
logger.info("【微信支付回调】发起分账失败");
throw new ServiceException("发起分账失败");
}
}, 10, TimeUnit.SECONDS);
}else if(attachVo.getType().equals(ServiceConstants.BUSINESS_TYPE_APPOINTMENT)){
logger.info("【微信支付回调】取消预约支付");
// 2-取消预约支付
@ -268,11 +280,13 @@ public class CallbackServiceImpl implements CallbackService {
throw new ServiceException("【微信支付回调】更新车辆状态失败");
}
}
logger.info("=================【微信支付回调】开始更新订单信息==================");
int updateEtOrder = orderService.updateEtOrder(order);
if(updateEtOrder==0){
logger.error("【微信支付回调】更新订单信息失败");
throw new ServiceException("【微信支付回调】更新订单信息失败");
}
logger.info("=================【微信支付回调】开始更新用户信息==================");
int updateUser = userService.updateUser(asUser);
if(updateUser==0){
logger.error("【微信支付回调】更新用户押金失败");

View File

@ -74,6 +74,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="userType != null and userType != ''">
AND u.user_type = #{userType}
</if>
<if test="areaId != null">
AND u.area_id = #{areaId}
</if>
<if test="userName != null and userName != ''">
AND u.user_name like concat('%', #{userName}, '%')
</if>