8.3
luodangjia
2024-08-03 67157345fe1878681e39ec186ef37ff6b3b5c1fc
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/WithdrawServiceImpl.java
@@ -1,4 +1,6 @@
package com.ruoyi.order.service.impl;
import java.io.InputStream;
import java.util.Date;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -11,12 +13,15 @@
import com.ruoyi.common.core.utils.SnowflakeIdWorker;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.vo.UserDto;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.order.entity.Order;
import com.ruoyi.order.entity.Withdraw;
import com.ruoyi.order.entity.WithdrawDetail;
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.WithdrawDetailService;
import com.ruoyi.order.service.WithdrawService;
import com.ruoyi.order.service.WithdrawalSettingService;
import com.ruoyi.order.vo.MoneyQueryRequest;
@@ -29,11 +34,13 @@
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
@@ -51,7 +58,8 @@
    private WithdrawalSettingService withdrawalSettingService;
    @Resource
    private OrderService orderService;
    @Resource
    private RedisService redisService;
    /**
     * 雪花算法类
     */
@@ -86,8 +94,14 @@
    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);
    public Page<UserWithdrawRecordVO> withdrawPage(List<Integer> userIds,String nickname, String userPhone, String applyForTime, Integer state, Page<UserWithdrawRecordVO> page) {
        Page<UserWithdrawRecordVO> userWithdrawRecordVOPage = baseMapper.withdrawPage(userIds,nickname, userPhone, applyForTime, state, page);
        return userWithdrawRecordVOPage;
    }
    @Override
    public Page<UserWithdrawRecordVO> withdrawPage1(List<String> cityList,List<Integer> userIds,String nickname, String userPhone, String applyForTime, Integer state, Page<UserWithdrawRecordVO> page) {
        Page<UserWithdrawRecordVO> userWithdrawRecordVOPage = baseMapper.withdrawPage1(cityList,userIds,nickname, userPhone, applyForTime, state, page);
        return userWithdrawRecordVOPage;
    }
@@ -106,6 +120,11 @@
        } else {
            list = baseMapper.exportList(nickname, userPhone, applyForTime, state);
        }
        for (UserWithdrawRecordVO userWithdrawRecordVO : list) {
            R<UserDto> user = userClient.getUser(userWithdrawRecordVO.getUserId());
            userWithdrawRecordVO.setUserNo(user.getData().getUserNo());
            userWithdrawRecordVO.setNickname(user.getData().getNickname());
        }
        return list;
    }
@@ -118,7 +137,8 @@
        if (stateList.contains(Constants.ONE)) {
            throw new GlobalException("当前订单提现申请已通过!");
        }
        return weChatPay(order.getOrderMoney(), openId);
        return weChatPay(order.getOrderMoney(), openId,list.get(0).getId());
    }
    @Override
@@ -176,6 +196,21 @@
            record.setNickname(user.getData().getNickname());
            record.setProfilePicture(user.getData().getProfilePicture());
            record.setUserPhone(user.getData().getPhone());
            record.setUserNo(record.getUserNo());
        }
        return userWithdrawRecordVOPage;
    }
    @Override
    public Page<UserWithdrawRecordVO> withdrawList1(List<String> cityList,Integer userId, Page<UserWithdrawRecordVO> page) {
        Page<UserWithdrawRecordVO> userWithdrawRecordVOPage = baseMapper.withdrawList1(cityList,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());
            record.setUserNo(user.getData().getUserNo());
        }
        return userWithdrawRecordVOPage;
    }
@@ -200,12 +235,20 @@
        // 系统审核设置
        WithdrawalSetting withdrawalSetting = withdrawalSettingService.lambdaQuery().one();
        Withdraw withdraw = new Withdraw();
        withdraw.setUserId(userId);
        withdraw.setUserPhone(userPhone);
        withdraw.setApplyForTime(new Date());
        withdraw.setApplyForMoney(order.getOrderMoney());
        withdraw.setOrderId(orderId);
        withdraw.setCityCode(order.getCityCode());
        this.save(withdraw);
        // 未开启全局 提现审核,则用户提现不需要后台审核
        if (Constants.ZERO.equals(withdrawalSetting.getEnableProcess())) {
            // 已通过
            withdraw.setState(Constants.ONE);
            // 商家微信打款至微信零钱
            boolean update = weChatPay(order.getOrderMoney(), openId);
            boolean update = weChatPay(order.getOrderMoney(), openId,withdraw.getId());
            if (!update) {
                throw new GlobalException("交易提现失败,请检查是否绑定微信!");
            }
@@ -213,79 +256,91 @@
            // 待审核
            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) {
        order.setIsWithdrawal(1);
        orderService.updateById(order);
        redisService.deleteObject(orderId);
        return this.updateById(withdraw);
    }
    @Resource
    private WithdrawDetailService withdrawDetailService;
    private boolean weChatPay(BigDecimal orderMoney, String openId,String withdrawId) {
        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;
        BigDecimal maxTransferAmount = new BigDecimal("20000"); // 单次转账限额,单位为分
        int totalTransfers = orderMoney.multiply(new BigDecimal("100")).divide(maxTransferAmount, 0, RoundingMode.UP).intValue();
        boolean allTransfersSuccessful = true;
        for (int i = 0; i < totalTransfers; i++) {
            BigDecimal transferAmount;
            if (i < totalTransfers - 1) {
                transferAmount = maxTransferAmount;
            } else {
                // 最后一笔转账,金额为剩余金额
                transferAmount = orderMoney.multiply(new BigDecimal("100")).subtract(maxTransferAmount.multiply(new BigDecimal(i))).setScale(0, RoundingMode.DOWN);
            }
            Map<String, Object> postMap = new HashMap<>(8);
            postMap.put(WechatConstants.APP_ID, "wx98563d0ec9cf21c8");
            postMap.put(WechatConstants.OUT_BATCH_NO, String.valueOf(UUID.randomUUID()).replaceAll("-", ""));
            System.err.println("====="+postMap.get(WechatConstants.OUT_BATCH_NO));
            postMap.put(WechatConstants.BATCH_NAME, "二手回收提现");
            postMap.put(WechatConstants.BATCH_REMARK, "二手回收提现");
            postMap.put(WechatConstants.TOTAL_AMOUNT, transferAmount);
            postMap.put(WechatConstants.TOTAL_NUM, Constants.ONE);
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> subMap = new HashMap<>(4);
            subMap.put(WechatConstants.OUT_DETAIL_NO, String.valueOf(SNOW_FLAKE_ID_WORKER.nextId()));
            subMap.put(WechatConstants.TRANSFER_AMOUNT, transferAmount);
            subMap.put(WechatConstants.TRANSFER_REMARK, "二手回收提现到账");
            subMap.put(WechatConstants.OPEN_ID, openId);
            list.add(subMap);
            postMap.put(WechatConstants.TRANSFER_DETAIL_LIST, list);
            // 使用类加载器获取资源 URL
//            String path = ClassLoader.getSystemResource("/usr/local/vx/apiclient_key.pem").getPath();
//            String result = HttpUtil.postTransBatRequest(
//                    WechatConstants.WE_CHAT_URL_PRE,
//                    JSONObject.toJSONString(postMap),
//                    "7EEA04429B006E12AAA421C002EC48BBEED5BE94",
//                    "1665330417",
//                    "/usr/local/vx/apiclient_key.pem", WechatConstants.WE_CHAT_URL_SUF);
            String result = HttpUtil.postTransBatRequest(
                    WechatConstants.WE_CHAT_URL_PRE,
                    JSONObject.toJSONString(postMap),
                    "7EEA04429B006E12AAA421C002EC48BBEED5BE94",
                    "1665330417",
                    "D:\\study\\JiaDianHuiShou\\ruoyi-service\\ruoyi-order\\src\\main\\java\\com\\ruoyi\\order\\vx\\apiclient_key.pem", WechatConstants.WE_CHAT_URL_SUF);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.containsKey(WechatConstants.CREATE_TIME)) {
                // 转账成功
                //保存转账明细
                WithdrawDetail withdrawDetail = new WithdrawDetail();
                withdrawDetail.setWithdrawId(withdrawId);
                withdrawDetail.setMoney(transferAmount);
                withdrawDetail.setOutBatchNo((String) postMap.get(WechatConstants.OUT_BATCH_NO));
                withdrawDetailService.save(withdrawDetail);
            } else {
                allTransfersSuccessful = false;
                break;
            }
        }
        return allTransfersSuccessful;
    }
    /**
     * 根据当前月份获取当前季度的开始月份
     */