package com.ruoyi.order.service.impl;
|
|
import com.alibaba.fastjson2.JSONObject;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.common.core.constant.Constants;
|
import com.ruoyi.common.core.constant.OrderConstants;
|
import com.ruoyi.common.core.constant.WechatConstants;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.exception.GlobalException;
|
import com.ruoyi.common.core.utils.SnowflakeIdWorker;
|
import com.ruoyi.common.core.utils.StringUtils;
|
import com.ruoyi.common.core.vo.UserDto;
|
import com.ruoyi.order.entity.Order;
|
import com.ruoyi.order.entity.Withdraw;
|
import com.ruoyi.order.entity.WithdrawalSetting;
|
import com.ruoyi.order.mapper.WithdrawMapper;
|
import com.ruoyi.order.request.WithdrawExportRequest;
|
import com.ruoyi.order.service.OrderService;
|
import com.ruoyi.order.service.WithdrawService;
|
import com.ruoyi.order.service.WithdrawalSettingService;
|
import com.ruoyi.order.vo.MoneyQueryRequest;
|
import com.ruoyi.order.vo.UserWithdrawRecordVO;
|
import com.ruoyi.order.vx.HttpUtil;
|
import com.ruoyi.user.api.feignClient.UserClient;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.time.LocalDate;
|
import java.time.Month;
|
import java.time.Year;
|
import java.time.temporal.TemporalAdjusters;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 用户提现申请记录表 服务实现类
|
* </p>
|
*
|
* @author hjl
|
* @since 2024-07-09
|
*/
|
@Service
|
public class WithdrawServiceImpl extends ServiceImpl<WithdrawMapper, Withdraw> implements WithdrawService {
|
|
@Resource
|
private WithdrawalSettingService withdrawalSettingService;
|
@Resource
|
private OrderService orderService;
|
|
/**
|
* 雪花算法类
|
*/
|
private static final SnowflakeIdWorker SNOW_FLAKE_ID_WORKER = new SnowflakeIdWorker(5, 5);
|
|
/**
|
* 小程序id
|
*/
|
@Value("wx.appid")
|
private String appId;
|
/**
|
* 转账名称
|
*/
|
@Value("wx.batchName")
|
private String batchName;
|
/**
|
* 商户号
|
*/
|
@Value("wx.mchId")
|
private String mchId;
|
/**
|
* 支付证书序列号
|
*/
|
@Value("wx.wechatPayserialNo")
|
private String wechatPayserialNo;
|
/**
|
* 转账备注
|
*/
|
@Value("wx.transferRemark")
|
private String transferRemark;
|
@Resource
|
private UserClient userClient;
|
|
@Override
|
public Page<UserWithdrawRecordVO> withdrawPage(String nickname, String userPhone, String applyForTime, Integer state, Page<UserWithdrawRecordVO> page) {
|
Page<UserWithdrawRecordVO> userWithdrawRecordVOPage = baseMapper.withdrawPage(nickname, userPhone, applyForTime, state, page);
|
|
return userWithdrawRecordVOPage;
|
}
|
|
@Override
|
public List<UserWithdrawRecordVO> excelExport(WithdrawExportRequest exportRequest) {
|
String nickname = exportRequest.getNickname();
|
String userPhone = exportRequest.getUserPhone();
|
String applyForTime = exportRequest.getApplyForTime();
|
Integer state = exportRequest.getState();
|
List<String> ids = exportRequest.getIdList();
|
// 结果封装
|
List<UserWithdrawRecordVO> list;
|
if (null != ids && !ids.isEmpty()) {
|
list = baseMapper.exportByIdList(ids);
|
} else {
|
list = baseMapper.exportList(nickname, userPhone, applyForTime, state);
|
}
|
return list;
|
}
|
|
@Override
|
public Boolean confirmWithdraw(String openId, Integer userId, Order order) {
|
// 校验提现
|
List<Withdraw> list = this.lambdaQuery().eq(Withdraw::getUserId, userId)
|
.eq(Withdraw::getOrderId, order.getId()).list();
|
List<Integer> stateList = list.stream().map(Withdraw::getState).collect(Collectors.toList());
|
if (stateList.contains(Constants.ONE)) {
|
throw new GlobalException("当前订单提现申请已通过!");
|
}
|
return weChatPay(order.getOrderMoney(), openId);
|
}
|
|
@Override
|
public BigDecimal withdrawalTotalMoney(MoneyQueryRequest request) {
|
List<String> cityList = request.getCityList();
|
String type = request.getType();
|
// 计算当前季度时间所包含时间
|
int currentYear = Year.now().getValue();
|
// 获取当前月份
|
Month currentMonth = LocalDate.now().getMonth();
|
// 计算当前季度的开始时间和结束时间
|
LocalDate startDate = LocalDate.of(currentYear, getStartMonthOfQuarter(currentMonth), 1);
|
LocalDate endDate = startDate.plusMonths(2).with(TemporalAdjusters.lastDayOfMonth());
|
String startDateStr = String.valueOf(startDate);
|
String endDateStr = String.valueOf(endDate);
|
BigDecimal withdrawalTotalMoney;
|
if (OrderConstants.QUARTER.equals(type)) {
|
// 用户提现总额
|
withdrawalTotalMoney = baseMapper.withdrawalTotalMoney(cityList, startDateStr, endDateStr);
|
} else if (OrderConstants.YEAR.equals(type)) {
|
// 用户提现总额
|
withdrawalTotalMoney = baseMapper.withdrawalTotalMoneyByYear(cityList);
|
} else if (OrderConstants.MONTH.equals(type)) {
|
// 用户提现总额
|
withdrawalTotalMoney = baseMapper.withdrawalTotalMoneyByMonth(cityList);
|
} else {
|
// 数量初始化
|
withdrawalTotalMoney = BigDecimal.ZERO;
|
}
|
return withdrawalTotalMoney;
|
}
|
|
@Override
|
public Boolean enableProcess(Integer enableProcess) {
|
Integer[] state = {0, 1};
|
boolean contains = Arrays.stream(state).collect(Collectors.toList()).contains(enableProcess);
|
if (!contains) {
|
throw new GlobalException("系统设置关闭/开启审核状态异常!");
|
}
|
return withdrawalSettingService.lambdaUpdate().set(WithdrawalSetting::getEnableProcess, enableProcess)
|
.update();
|
}
|
|
@Override
|
public WithdrawalSetting withdrawProcess() {
|
return withdrawalSettingService.lambdaQuery().one();
|
}
|
|
@Override
|
public Page<UserWithdrawRecordVO> withdrawList(Integer userId, Page<UserWithdrawRecordVO> page) {
|
Page<UserWithdrawRecordVO> userWithdrawRecordVOPage = baseMapper.withdrawList(userId, page);
|
for (UserWithdrawRecordVO record : userWithdrawRecordVOPage.getRecords()) {
|
R<UserDto> user = userClient.getUser(record.getUserId());
|
System.err.println("==="+user.getData());
|
record.setNickname(user.getData().getNickname());
|
record.setProfilePicture(user.getData().getProfilePicture());
|
record.setUserPhone(user.getData().getPhone());
|
}
|
return userWithdrawRecordVOPage;
|
}
|
|
@Override
|
public Boolean confirmWithdrawByUser(String orderId, Integer userId, String openId, String userPhone) {
|
Order order = orderService.lambdaQuery()
|
.eq(Order::getId, orderId)
|
.eq(Order::getIsDelete, 0).one();
|
if (null == order) {
|
throw new GlobalException("订单信息异常!");
|
}
|
// 校验提现
|
List<Withdraw> list = this.lambdaQuery().eq(Withdraw::getUserId, userId)
|
.eq(Withdraw::getOrderId, orderId).list();
|
List<Integer> stateList = list.stream().map(Withdraw::getState).collect(Collectors.toList());
|
if (stateList.contains(Constants.ZERO)) {
|
throw new GlobalException("当前订单已提交提现申请,请等待审核!");
|
} else if (stateList.contains(Constants.ONE)) {
|
throw new GlobalException("当前订单已完成提现,请勿重复提现!");
|
}
|
// 系统审核设置
|
WithdrawalSetting withdrawalSetting = withdrawalSettingService.lambdaQuery().one();
|
Withdraw withdraw = new Withdraw();
|
// 未开启全局 提现审核,则用户提现不需要后台审核
|
if (Constants.ZERO.equals(withdrawalSetting.getEnableProcess())) {
|
// 已通过
|
withdraw.setState(Constants.ONE);
|
// 商家微信打款至微信零钱
|
boolean update = weChatPay(order.getOrderMoney(), openId);
|
if (!update) {
|
throw new GlobalException("交易提现失败,请检查是否绑定微信!");
|
}
|
} else {
|
// 待审核
|
withdraw.setState(Constants.ZERO);
|
}
|
withdraw.setUserId(userId);
|
withdraw.setUserPhone(userPhone);
|
withdraw.setApplyForTime(new Date());
|
withdraw.setApplyForMoney(order.getOrderMoney());
|
withdraw.setOrderId(orderId);
|
withdraw.setCityCode(order.getCityCode());
|
return this.save(withdraw);
|
}
|
|
private boolean weChatPay(BigDecimal orderMoney, String openId) {
|
if (StringUtils.isBlank(openId)) {
|
return false;
|
}
|
Map<String, Object> postMap = new HashMap<>(8);
|
// 小程序 id
|
postMap.put(WechatConstants.APP_ID, appId);
|
postMap.put(WechatConstants.OUT_BATCH_NO, String.valueOf(UUID.randomUUID()).replaceAll("-", ""));
|
// 该笔批量转账的名称
|
postMap.put(WechatConstants.BATCH_NAME, batchName);
|
// 转账说明,UTF8编码,最多允许32个字符
|
postMap.put(WechatConstants.BATCH_REMARK, batchName);
|
// 转账金额单位为“分”。 总金额
|
postMap.put(WechatConstants.TOTAL_AMOUNT, orderMoney.multiply(new BigDecimal(Constants.ONE_HUNDRED)));
|
// 转账总笔数
|
postMap.put(WechatConstants.TOTAL_NUM, Constants.ONE);
|
List<Map<String, Object>> list = new ArrayList<>();
|
Map<String, Object> subMap = new HashMap<>(4);
|
// 商家明细单号 该商家下唯一
|
// subMap.put("out_detail_no", RandomUtil.randomString(32))
|
subMap.put(WechatConstants.OUT_DETAIL_NO, SNOW_FLAKE_ID_WORKER.nextId());
|
// 转账金额
|
subMap.put(WechatConstants.TRANSFER_AMOUNT, orderMoney);
|
// 转账备注
|
subMap.put(WechatConstants.TRANSFER_REMARK, transferRemark);
|
// 用户在直连商户应用下的用户标示
|
subMap.put(WechatConstants.OPEN_ID, openId);
|
// 大金额需要传入真实姓名
|
/*subMap.put("user_name",
|
RsaCryptoUtil.encryptOAEP(userName,WechatPayV3Util.getSaveCertificates(privatekeypath)))*/
|
list.add(subMap);
|
postMap.put(WechatConstants.TRANSFER_DETAIL_LIST, list);
|
// 使用类加载器获取资源 URL
|
ClassPathResource classPathResource = new ClassPathResource("vx/apiclient_key.pem");
|
/*// 获取 resources 目录下的文件路径,假设文件路径为 "resources/data/example.txt"
|
String filePath = "resources/data/vx/apiclient_key.pem";
|
File file = new File(filePath);
|
// 输出文件的绝对路径
|
String absolutePath = file.getAbsolutePath();*/
|
String result = HttpUtil.postTransBatRequest(
|
WechatConstants.WE_CHAT_URL_PRE,
|
JSONObject.toJSONString(postMap),
|
// 支付证书序列号
|
wechatPayserialNo,
|
// 商户号
|
mchId,
|
classPathResource.getPath(), WechatConstants.WE_CHAT_URL_SUF);
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
/*
|
* 成功示例
|
* {
|
* "out_batch_no": "plfk2020042013",
|
* "batch_id": "1030000071100999991182020050700019480001",
|
* "create_time": "2015-05-20T13:29:35.120+08:00"
|
* }
|
*/
|
if (null == jsonObject || null != jsonObject.get(WechatConstants.CREATE_TIME)) {
|
//转账成功
|
return Boolean.TRUE;
|
} else {
|
return Boolean.FALSE;
|
}
|
}
|
|
/**
|
* 根据当前月份获取当前季度的开始月份
|
*/
|
private static Month getStartMonthOfQuarter(Month currentMonth) {
|
if (currentMonth.compareTo(Month.JANUARY) >= 0 && currentMonth.compareTo(Month.MARCH) <= 0) {
|
return Month.JANUARY;
|
} else if (currentMonth.compareTo(Month.APRIL) >= 0 && currentMonth.compareTo(Month.JUNE) <= 0) {
|
return Month.APRIL;
|
} else if (currentMonth.compareTo(Month.JULY) >= 0 && currentMonth.compareTo(Month.SEPTEMBER) <= 0) {
|
return Month.JULY;
|
} else {
|
return Month.OCTOBER;
|
}
|
}
|
|
}
|