无关风月
2025-04-09 4e30491ff75e23e4cce21713a79c03421addd3f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
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;
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.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;
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.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;
 
/**
 * <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;
    @Resource
    private RedisService redisService;
    /**
     * 雪花算法类
     */
    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(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,List<String> siteIds) {
        Page<UserWithdrawRecordVO> userWithdrawRecordVOPage = baseMapper.withdrawPage1(cityList,userIds,nickname, userPhone, applyForTime, state, page,siteIds);
 
        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);
        }
        for (UserWithdrawRecordVO userWithdrawRecordVO : list) {
            R<UserDto> user = userClient.getUser(userWithdrawRecordVO.getUserId());
            userWithdrawRecordVO.setUserNo(user.getData().getUserNo());
            userWithdrawRecordVO.setNickname(user.getData().getNickname());
        }
        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,list.get(0).getId(),order.getServeName());
    }
 
    @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 (request.getSiteIds()==null){
            request.setSiteIds(new ArrayList<>());
        }
        if (OrderConstants.QUARTER.equals(type)) {
            // 用户提现总额
            withdrawalTotalMoney = baseMapper.withdrawalTotalMoney(cityList, startDateStr, endDateStr,request.getSiteIds());
        } else if (OrderConstants.YEAR.equals(type)) {
            // 用户提现总额
            withdrawalTotalMoney = baseMapper.withdrawalTotalMoneyByYear(cityList,request.getSiteIds());
        } else if (OrderConstants.MONTH.equals(type)) {
            // 用户提现总额
            withdrawalTotalMoney = baseMapper.withdrawalTotalMoneyByMonth(cityList,request.getSiteIds());
        } 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());
            record.setUserNo(record.getUserNo());
        }
        return userWithdrawRecordVOPage;
    }
 
    @Override
    public Page<UserWithdrawRecordVO> withdrawList1(List<String> cityList,Integer userId, Page<UserWithdrawRecordVO> page,List<String> siteIds) {
        Page<UserWithdrawRecordVO> userWithdrawRecordVOPage = baseMapper.withdrawList1(cityList,userId, page,siteIds);
        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;
    }
 
    @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)) {
            return  false;
//            throw new GlobalException("当前订单已提交提现申请,请等待审核!");
        } else if (stateList.contains(Constants.ONE)) {
            throw new GlobalException("当前订单已完成提现,请勿重复提现!");
        }
        // 系统审核设置
        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,withdraw.getId(),order.getServerName());
//            if (!update) {
//                throw new GlobalException("交易提现失败,请检查是否绑定微信!");
//            }
        } else {
            // 待审核
            withdraw.setState(Constants.ZERO);
        }
 
 
 
        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;
//        }
//
//        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);
//
//            WithdrawDetail withdrawDetail = new WithdrawDetail();
//            withdrawDetail.setWithdrawId(withdrawId);
//            withdrawDetail.setMoney(transferAmount);
//            withdrawDetail.setOutBatchNo((String) postMap.get(WechatConstants.OUT_BATCH_NO));
//            withdrawDetailService.save(withdrawDetail);
//
//            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;
//    }
 
    // 上架打款到用户零钱接口已弃用 用新版商家转账接口
    private boolean weChatPay(BigDecimal orderMoney, String openId,String withdrawId,String serverName) {
        if (StringUtils.isBlank(openId)) {
            return false;
        }
        BigDecimal maxTransferAmount = new BigDecimal("200000"); // 单次转账限额,单位为分
        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");
            // 订单号
            String s = String.valueOf(UUID.randomUUID()).replaceAll("-", "");
            postMap.put("out_bill_no", s);
            System.err.println("====="+postMap.get(WechatConstants.OUT_BATCH_NO));
            postMap.put(WechatConstants.OPEN_ID, openId);
            // 转账金额
            postMap.put("transfer_amount", transferAmount);
            // 转账备注
            postMap.put("transfer_remark", transferAmount);
            // 回调地址
            postMap.put("notify_url", transferAmount);
            // 转账场景报备信息
            Map<String, Object> info = new HashMap<>();
            info.put("info_type","回收商品名称");
            info.put("info_content",serverName);
            postMap.put("transfer_scene_report_infos", JSONObject.toJSONString(info));
            String result = HttpUtil.postTransBatRequest(
                    WechatConstants.WE_CHAT_PAY_URL_PRE,
                    JSONObject.toJSONString(postMap),
                    "7EEA04429B006E12AAA421C002EC48BBEED5BE94",
                    "1665330417",
                    "E:\\ershoucert\\apiclient_key.pem", WechatConstants.WE_CHAT_URL_SUF);
            JSONObject jsonObject = JSONObject.parseObject(result);
//            WithdrawDetail withdrawDetail = new WithdrawDetail();
//            withdrawDetail.setWithdrawId(withdrawId);
//            withdrawDetail.setMoney(transferAmount);
//            withdrawDetail.setOutBatchNo((String) postMap.get(WechatConstants.OUT_BATCH_NO));
//            withdrawDetailService.save(withdrawDetail);
            if (jsonObject.containsKey(WechatConstants.CREATE_TIME)) {
                String string = jsonObject.getString("package_info");
                Withdraw withdraw = this.getById(withdrawId);
 
                Order order = orderService.getById(withdraw.getOrderId());
                order.setPackageInfo(string);
                orderService.updateById(order);
                // 转账成功
                //保存转账明细
                WithdrawDetail withdrawDetail = new WithdrawDetail();
                withdrawDetail.setWithdrawId(withdrawId);
                withdrawDetail.setMoney(transferAmount);
                withdrawDetail.setOutBatchNo(s);
                withdrawDetailService.save(withdrawDetail);
 
 
            } else {
                allTransfersSuccessful = false;
                break;
            }
 
        }
 
        return allTransfersSuccessful;
    }
 
 
    /**
     * 根据当前月份获取当前季度的开始月份
     */
    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;
        }
    }
 
}