From 66ae2020f8ea1e1f2573b46c668874180a483f3d Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期一, 13 一月 2025 16:19:14 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'
---
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WalletController.java | 198 +++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 174 insertions(+), 24 deletions(-)
diff --git a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WalletController.java b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WalletController.java
index 291de10..999eeff 100644
--- a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WalletController.java
+++ b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WalletController.java
@@ -1,25 +1,42 @@
package com.ruoyi.account.controller;
+import cn.hutool.core.collection.CollectionUtil;
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.api.model.BalanceChangeRecord;
-import com.ruoyi.account.service.BalanceChangeRecordService;
-import com.ruoyi.account.service.WalletService;
+import com.ruoyi.account.api.model.BalancePayment;
+import com.ruoyi.account.service.*;
+import com.ruoyi.account.util.payment.PaymentUtil;
+import com.ruoyi.account.util.payment.model.UniPayCallbackResult;
+import com.ruoyi.account.util.payment.model.UniPayResult;
import com.ruoyi.account.vo.WalletVO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
+import com.ruoyi.order.feignClient.RemoteOrderGoodsClient;
+import com.ruoyi.order.model.Order;
+import com.ruoyi.other.api.domain.VipSetting;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import io.swagger.annotations.ApiParam;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.format.annotation.DateTimeFormat;
+import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.math.BigDecimal;
+import java.time.*;
import java.util.List;
+import java.util.stream.Collectors;
-@Api("钱包")
+@Api(tags = "小程序-个人中心-我的钱包")
@RestController
@RequestMapping("wallet")
public class WalletController extends BaseController {
@@ -29,41 +46,174 @@
private TokenService tokenService;
@Resource
private WalletService walletService;
+ @Resource
+ private RemoteOrderGoodsClient remoteOrderGoodsClient;
+ @Resource
+ private AppUserService appUserService;
+ @Resource
+ private VipSettingService vipSettingService;
+ @Resource
+ private BalancePaymentService balancePaymentService;
+ @Resource
+ private RedisTemplate redisTemplate;
+
+
/**
* 钱包详情
*/
@GetMapping("detail")
- @ApiOperation(value = "钱包详情", notes = "钱包详情", tags = {"小程序-个人中心-我的钱包-钱包详情"})
+ @ApiOperation(value = "钱包详情", notes = "钱包详情")
public R<WalletVO> detail() {
LoginUser loginUserApplet = tokenService.getLoginUserApplet();
+ VipSetting vipSetting = vipSettingService.getVipSettingByUserId(loginUserApplet.getUserid());
WalletVO walletVO = walletService.getWalletByUserId(loginUserApplet.getUserid());
+ walletVO.setVipWithdrawalFee(vipSetting.getVipWithdrawalFee());
return R.ok(walletVO);
}
/**
* 变更明细
*/
- @ApiOperation(value = "变更明细", notes = "变更明细", tags = {"小程序-个人中心-我的钱包-变更记录"})
+ @ApiOperation(value = "变更明细", notes = "变更明细")
@GetMapping("change")
- public R<List<BalanceChangeRecord>> change() {
+ public R<List<BalanceChangeRecord>> change(@ApiParam(value = "变更类型")
+ @RequestParam(required = false) Integer changeType,
+ @ApiParam(value = "创建时间")
+ @RequestParam(required = false)
+ @DateTimeFormat(pattern = "yyyy-MM-dd")
+ LocalDate createTime) {
Long userId = SecurityUtils.getUserId();
- return R.ok(balanceChangeRecordService.list(new LambdaQueryWrapper<BalanceChangeRecord>()
- .eq(BalanceChangeRecord::getAppUserId, userId)));
+
+ LocalDateTime startTime = null;
+ LocalDateTime endTime = null;
+ if (createTime != null) {
+ // 将 createTime 设置为当天的开始时间 (00:00)
+ startTime = createTime.atStartOfDay();
+
+ // 使用 YearMonth 来获取该月的最后一天
+ YearMonth yearMonth = YearMonth.from(createTime);
+ LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
+
+ // 将最后一天转换为 LocalDateTime,并设置为当天的最后一秒 (23:59:59.999)
+ endTime = lastDayOfMonth.atTime(LocalTime.MAX);
+ }
+
+ List<BalanceChangeRecord> list = balanceChangeRecordService.list(new LambdaQueryWrapper<BalanceChangeRecord>()
+ .eq(changeType != null, BalanceChangeRecord::getChangeType, changeType)
+ .between(startTime != null, BalanceChangeRecord::getCreateTime, startTime, endTime)
+ .eq(BalanceChangeRecord::getAppUserId, userId).orderByDesc(BalanceChangeRecord::getCreateTime));
+
+ List<Long> orderIds = list.stream().map(BalanceChangeRecord::getOrderId).collect(Collectors.toList());
+ if (CollectionUtil.isNotEmpty(orderIds)){
+ R<List<Order>> r = remoteOrderGoodsClient.getOrderListByIds(orderIds);
+ if (!R.isSuccess(r)){
+ return R.fail(r.getMsg());
+ }
+ List<Order> orderList = r.getData();
+ list.forEach(bc -> {
+ orderList.stream().filter(o -> o.getId().equals(bc.getOrderId())).findFirst().ifPresent(o -> {
+ Long appUserId = o.getAppUserId();
+ AppUser appUser = appUserService.getById(appUserId);
+ bc.setUserName(appUser.getName());
+ bc.setAmount(o.getPaymentAmount());
+ });
+ });
+ }
+ return R.ok(list);
}
-// /**
-// * 充值
-// */
-// @GetMapping("recharge")
-// @ApiOperation(value = "充值", notes = "钱包充值")
-// public AjaxResult recharge(@ApiParam(value = "充值金额", required = true) @RequestParam BigDecimal amount) {
-// Long userId = SecurityUtils.getUserId();
-// String openId;
-// // 商户号
-// String partnerTradeNo;
-// // TODO 充值
-// return AjaxResult.success();
-// }
+ /**
+ * 充值
+ */
+ @GetMapping("recharge")
+ @ApiOperation(value = "充值", notes = "钱包充值")
+ public R<String> recharge(@ApiParam(value = "充值金额", required = true) @RequestParam BigDecimal amount) {
+ LoginUser loginUserApplet = tokenService.getLoginUserApplet();
+ AppUser appUser = appUserService.getById(loginUserApplet.getUserid());
+ BalancePayment balancePayment = new BalancePayment();
+ balancePayment.setAppUserId(appUser.getId());
+ balancePayment.setMoney(amount);
+ balancePayment.setStatus(1);
+ balancePayment.setCreateTime(LocalDateTime.now());
+ balancePaymentService.save(balancePayment);
+ //调起支付
+ UniPayResult uniPayResult = PaymentUtil.uniPay(balancePayment.getId().toString(), amount.doubleValue(), "余额充值", "余额充值", "",
+ "/account/wallet/rechargeCallback", appUser.getWxOpenid(), null);
+ if(null == uniPayResult || !"100".equals(uniPayResult.getRa_Code())){
+ return R.fail(null == uniPayResult ? "支付失败" : uniPayResult.getRb_CodeMsg());
+ }
+ String rc_result = uniPayResult.getRc_Result();
+ JSONObject jsonObject = JSON.parseObject(rc_result);
+ //将支付数据添加到redis队列中,便于定时任务去校验是否完成支付,没有完成支付支付,15分钟后关闭订单。
+ long second = LocalDateTime.now().plusMinutes(15).toEpochSecond(ZoneOffset.UTC);
+ redisTemplate.opsForZSet().add("BalanceRecharge", balancePayment.getId(), second);
+ return R.ok(jsonObject.toJSONString());
+ }
+
+ /**
+ * 充值支付回调
+ * @param callback
+ * @param response
+ */
+ @ResponseBody
+ @GetMapping("/rechargeCallback")
+ public void rechargeCallback(UniPayCallbackResult callback, HttpServletResponse response){
+ String r2_orderNo = callback.getR2_OrderNo();
+ BalancePayment balancePayment = balancePaymentService.getById(r2_orderNo);
+ if(null == balancePayment || balancePayment.getStatus() != 1){
+ response.setStatus(200);
+ PrintWriter out = null;
+ try {
+ out = response.getWriter();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ out.println("success");
+ out.flush();
+ out.close();
+ return;
+ }
+ if("100".equals(callback.getR6_Status())){
+ //添加账户变动流水
+ BigDecimal money = balancePayment.getMoney();
+ AppUser appUser = appUserService.getById(balancePayment.getAppUserId());
+ BigDecimal balance = appUser.getBalance();
+ appUser.setBalance(appUser.getBalance().add(money));
+ appUser.setWithdrawableAmount(appUser.getWithdrawableAmount().add(money));
+ appUser.setTotalRechargeAmount(appUser.getTotalRechargeAmount().add(money));
+ appUserService.updateById(appUser);
+ //流水
+ BalanceChangeRecord balanceChangeRecord = new BalanceChangeRecord();
+ balanceChangeRecord.setAppUserId(appUser.getId());
+ balanceChangeRecord.setOrderId(balanceChangeRecord.getId());
+ balanceChangeRecord.setChangeType(1);
+ balanceChangeRecord.setBeforeAmount(balance);
+ balanceChangeRecord.setChangeAmount(money);
+ balanceChangeRecord.setAfterAmount(appUser.getBalance());
+ balanceChangeRecord.setDelFlag(0);
+ balanceChangeRecord.setCreateTime(LocalDateTime.now());
+ balanceChangeRecordService.save(balanceChangeRecord);
+
+ balancePayment.setStatus(2);
+ balancePayment.setPaymentTime(LocalDateTime.now());
+ balancePayment.setSerialNumber(callback.getR8_BankOrderNo());
+ balancePayment.setBalanceChangeRecordId(balanceChangeRecord.getId());
+ balancePaymentService.updateById(balancePayment);
+
+ response.setStatus(200);
+ PrintWriter out = null;
+ try {
+ out = response.getWriter();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ out.println("success");
+ out.flush();
+ out.close();
+ }
+
+
+ }
}
--
Gitblit v1.7.1