hjl
2024-07-01 03c6572c7918beaa20365e69c9694a11f0d9948a
ruoyi-service/ruoyi-admin/src/main/java/com/ruoyi/admin/service/impl/WithdrawServiceImpl.java
@@ -4,20 +4,29 @@
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.admin.entity.Order;
import com.ruoyi.admin.entity.User;
import com.ruoyi.admin.entity.Withdraw;
import com.ruoyi.admin.entity.WithdrawalSetting;
import com.ruoyi.admin.mapper.WithdrawMapper;
import com.ruoyi.admin.request.WithdrawExportRequest;
import com.ruoyi.admin.service.WithdrawService;
import com.ruoyi.admin.service.WithdrawalSettingService;
import com.ruoyi.admin.vo.UserWithdrawRecordRequestVO;
import com.ruoyi.admin.utils.vx.HttpUtil;
import com.ruoyi.admin.vo.UserWithdrawRecordVO;
import com.ruoyi.common.core.constant.Constants;
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 org.apache.commons.codec.CharEncoding;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -27,8 +36,7 @@
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -45,21 +53,64 @@
    @Resource
    private WithdrawalSettingService withdrawalSettingService;
    /**
     * 雪花算法类
     */
    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;
    @Override
    public R<String> excelExport(List<String> ids, HttpServletResponse response) {
    public R<String> excelExport(WithdrawExportRequest exportRequest, HttpServletResponse response) {
        try {
            response.setCharacterEncoding(Constants.UTF8);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(Constants.EXCEL_WITHDRAWAL_FILE_NAME, CharEncoding.UTF_8) + ".xlsx");
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    URLEncoder.encode(Constants.EXCEL_WITHDRAWAL_FILE_NAME, CharEncoding.UTF_8) + ".xlsx");
        } catch (UnsupportedEncodingException e) {
            return R.fail("excel导出失败!");
        }
        try {
            List<Withdraw> list = lambdaQuery().in(Withdraw::getId, ids).eq(Withdraw::getIsDelete, 0).list();
            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);
            }
            // excel模板封装
            ExcelWriterBuilder excelWriterBuilder = EasyExcelFactory.write(response.getOutputStream());
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/" + Constants.EXCEL_WITHDRAWAL_FILE_NAME + ".xlsx");
            InputStream stream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("template/" + Constants.EXCEL_WITHDRAWAL_FILE_NAME + ".xlsx");
            // 自动释放资源
            try (ExcelWriter excelWriter = excelWriterBuilder.withTemplate(stream).build()) {
                WriteSheet writeSheet = EasyExcelFactory.writerSheet().build();
@@ -75,32 +126,23 @@
    }
    @Override
    public BigDecimal withdrawalTotalMoney(List<String> cityIdList) {
        return baseMapper.withdrawalTotalMoney(cityIdList);
    public BigDecimal withdrawalTotalMoney(List<String> cityList) {
        return baseMapper.withdrawalTotalMoney(cityList);
    }
    @Override
    public BigDecimal withdrawalTotalMoneyByYear(List<String> cityIdList) {
        return baseMapper.withdrawalTotalMoneyByYear(cityIdList);
    public BigDecimal withdrawalTotalMoneyByYear(List<String> cityList) {
        return baseMapper.withdrawalTotalMoneyByYear(cityList);
    }
    @Override
    public BigDecimal withdrawalTotalMoneyByMonth(List<String> cityIdList) {
        return baseMapper.withdrawalTotalMoneyByMonth(cityIdList);
    public BigDecimal withdrawalTotalMoneyByMonth(List<String> cityList) {
        return baseMapper.withdrawalTotalMoneyByMonth(cityList);
    }
    @Override
    public UserWithdrawRecordRequestVO withdrawList(Integer userId, Page<UserWithdrawRecordVO> page) {
        IPage<UserWithdrawRecordVO> record = baseMapper.withdrawList(userId, page);
        // 全局审核设置
        WithdrawalSetting setting = withdrawalSettingService.lambdaQuery().one();
        Integer enableProcess;
        if (null == setting) {
            enableProcess = 0;
        } else {
            enableProcess = setting.getEnableProcess();
        }
        return new UserWithdrawRecordRequestVO(record, enableProcess);
    public IPage<UserWithdrawRecordVO> withdrawList(Integer userId, Page<UserWithdrawRecordVO> page) {
        return baseMapper.withdrawList(userId, page);
    }
    @Override
@@ -117,4 +159,88 @@
    public WithdrawalSetting withdrawProcess() {
        return withdrawalSettingService.lambdaQuery().one();
    }
    @Override
    public IPage<UserWithdrawRecordVO> withdrawPage(String nickname, String userPhone, String applyForTime, Integer state, Page<UserWithdrawRecordVO> page) {
        return baseMapper.withdrawPage(nickname, userPhone, applyForTime, state, page);
    }
    @Override
    public Boolean confirmWithdraw(User user, Order order) {
        // 校验提现
        List<Withdraw> list = this.lambdaQuery().eq(Withdraw::getUserId, user.getId())
                .eq(Withdraw::getOrderId, order).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("当前订单已提现通过!");
        }
        return weChatPay(order.getOrderMoney(), user.getOpenId());
    }
    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;
        }
    }
}