Files
gxtyzx-java/modules/common/system/controller/PaymentController.java
T
2024-08-13 17:51:18 +08:00

247 lines
9.4 KiB
Java

package com.gxwebsoft.common.system.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.utils.RequestUtil;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.*;
import com.gxwebsoft.common.system.service.PaymentService;
import com.gxwebsoft.common.system.param.PaymentParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.service.UserBalanceLogService;
import com.gxwebsoft.common.system.service.UserService;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
import com.wechat.pay.java.service.partnerpayments.model.TransactionAmount;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static com.gxwebsoft.common.core.constants.BalanceConstants.BALANCE_ADMIN;
import static com.gxwebsoft.common.core.constants.BalanceConstants.BALANCE_USE;
/**
* 支付方式控制器
*
* @author 科技小王子
* @since 2024-05-11 12:39:11
*/
@Api(tags = "支付方式管理")
@RestController
@RequestMapping("/api/system/payment")
public class PaymentController extends BaseController {
@Resource
private PaymentService paymentService;
@Resource
private UserService userService;
@Resource
private UserBalanceLogService userBalanceLogService;
@Resource
private RedisUtil redisUtil;
@Resource
private RequestUtil requestUtil;
@ApiOperation("余额支付接口")
@PostMapping("/balancePay")
public ApiResult<?> balancePay(@RequestBody Order order) {
System.out.println("使用余额支付 >>> 订单信息 " + order);
// 查询购买者信息
final User buyer = userService.getById(order.getUserId());
if (buyer.getBalance().compareTo(order.getPayPrice()) < 0) {
return fail("余额不足");
}
// 扣除余额
final BigDecimal subtract = buyer.getBalance().subtract(order.getTotalPrice());
buyer.setBalance(subtract);
final boolean updateUser = userService.updateUser(buyer);
System.out.println("updateUser = " + updateUser);
// 记录余额明细
UserBalanceLog userBalanceLog = new UserBalanceLog();
userBalanceLog.setUserId(buyer.getUserId());
userBalanceLog.setScene(BALANCE_USE);
userBalanceLog.setMoney(order.getPayPrice());
BigDecimal balance = buyer.getBalance().add(order.getPayPrice());
userBalanceLog.setBalance(balance);
userBalanceLog.setComments(order.getMerchantName());
userBalanceLog.setTransactionId(UUID.randomUUID().toString());
userBalanceLog.setOrderNo(order.getOrderNo());
final boolean save = userBalanceLogService.save(userBalanceLog);
System.out.println("save = " + save);
// 推送微信官方支付结果(携带租户ID的POST请求)
final Transaction transaction = new Transaction();
transaction.setOutTradeNo(order.getOrderNo());
transaction.setTransactionId(order.getOrderNo());
final TransactionAmount amount = new TransactionAmount();
// 计算金额
// BigDecimal decimal = order.getTotalPrice();
// final BigDecimal multiply = decimal.multiply(new BigDecimal(100));
// // 将 BigDecimal 转换为 Integer
// Integer money = multiply.intValue();
amount.setTotal(order.getPayPrice().intValue());
amount.setCurrency("CNY");
transaction.setAmount(amount);
// 获取支付配置信息用于解密
String key = "Payment:0:".concat(order.getTenantId().toString());
System.out.println("key = " + key);
final Payment payment = redisUtil.get(key, Payment.class);
requestUtil.pushBalancePayNotify(transaction, payment);
return success("支付成功",order.getOrderNo());
}
@ApiOperation("选择支付方式")
@GetMapping("/select")
public ApiResult<?> select(PaymentParam param) {
String key = "SelectPayment:".concat(getTenantId().toString());
final String string = redisUtil.get(key);
final List<Payment> paymentList = JSONObject.parseArray(string, Payment.class);
if (!CollectionUtils.isEmpty(paymentList)) {
return success(paymentList);
}
// 使用关联查询
final List<Payment> list = paymentService.list(new LambdaUpdateWrapper<Payment>().eq(Payment::getStatus, true));
if (!CollectionUtils.isEmpty(list)) {
list.forEach(d -> {
d.setApiKey(null);
d.setApiclientCert(null);
d.setApiclientKey(null);
d.setMerchantSerialNumber(null);
});
}
redisUtil.set(key,list,1L, TimeUnit.DAYS);
return success(list);
}
@PreAuthorize("hasAuthority('sys:payment:list')")
@OperationLog
@ApiOperation("分页查询支付方式")
@GetMapping("/page")
public ApiResult<PageResult<Payment>> page(PaymentParam param) {
PageParam<Payment, PaymentParam> page = new PageParam<>(param);
page.setDefaultOrder("sort_number asc, create_time asc");
return success(paymentService.page(page, page.getWrapper()));
// 使用关联查询
// return success(paymentService.pageRel(param));
}
@PreAuthorize("hasAuthority('sys:payment:list')")
@OperationLog
@ApiOperation("查询全部支付方式")
@GetMapping()
public ApiResult<List<Payment>> list(PaymentParam param) {
PageParam<Payment, PaymentParam> page = new PageParam<>(param);
page.setDefaultOrder("sort_number asc, create_time asc");
return success(paymentService.list(page.getOrderWrapper()));
// 使用关联查询
//return success(paymentService.listRel(param));
}
@PreAuthorize("hasAuthority('sys:payment:list')")
@OperationLog
@ApiOperation("根据id查询支付方式")
@GetMapping("/{id}")
public ApiResult<Payment> get(@PathVariable("id") Integer id) {
return success(paymentService.getById(id));
// 使用关联查询
//return success(paymentService.getByIdRel(id));
}
@PreAuthorize("hasAuthority('sys:payment:save')")
@OperationLog
@ApiOperation("添加支付方式")
@PostMapping()
public ApiResult<?> save(@RequestBody Payment payment) {
if (paymentService.count(new LambdaQueryWrapper<Payment>().eq(Payment::getCode,payment.getCode())) > 0) {
return fail(payment.getName() + "已存在");
}
if (paymentService.save(payment)) {
String key = "Payment:" + payment.getCode() + ":" + getTenantId();
redisUtil.set(key,payment);
return success("添加成功");
}
return fail("添加失败");
}
@PreAuthorize("hasAuthority('sys:payment:update')")
@OperationLog
@ApiOperation("修改支付方式")
@PutMapping()
public ApiResult<?> update(@RequestBody Payment payment) {
if (paymentService.updateById(payment)) {
String key = "Payment:" + payment.getCode() + ":" + getTenantId();
redisUtil.set(key,payment);
return success("修改成功");
}
return fail("修改失败");
}
@PreAuthorize("hasAuthority('sys:payment:remove')")
@OperationLog
@ApiOperation("删除支付方式")
@DeleteMapping("/{id}")
public ApiResult<?> remove(@PathVariable("id") Integer id) {
final Payment payment = paymentService.getById(id);
System.out.println("payment = " + payment);
String key = "Payment:" + payment.getCode() + ":" + getTenantId();
redisUtil.delete(key);
if (paymentService.removeById(id)) {
return success("删除成功");
}
return fail("删除失败");
}
@PreAuthorize("hasAuthority('sys:payment:save')")
@OperationLog
@ApiOperation("批量添加支付方式")
@PostMapping("/batch")
public ApiResult<?> saveBatch(@RequestBody List<Payment> list) {
if (paymentService.saveBatch(list)) {
return success("添加成功");
}
return fail("添加失败");
}
@PreAuthorize("hasAuthority('sys:payment:update')")
@OperationLog
@ApiOperation("批量修改支付方式")
@PutMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody BatchParam<Payment> batchParam) {
if (batchParam.update(paymentService, "id")) {
return success("修改成功");
}
return fail("修改失败");
}
@PreAuthorize("hasAuthority('sys:payment:remove')")
@OperationLog
@ApiOperation("批量删除支付方式")
@DeleteMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
if (paymentService.removeByIds(ids)) {
return success("删除成功");
}
return fail("删除失败");
}
}