Files
gxtyzx-java/src/main/java/com/gxwebsoft/think/controller/ThinkRechargeLogController.java
T
2025-04-09 09:58:39 +08:00

181 lines
7.0 KiB
Java

package com.gxwebsoft.think.controller;
import cn.hutool.core.date.DateUtil;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.think.entity.*;
import com.gxwebsoft.think.service.ThinkRechargeLogService;
import com.gxwebsoft.think.param.ThinkRechargeLogParam;
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.entity.User;
import com.gxwebsoft.think.service.ThinkUsersVipService;
import com.gxwebsoft.think.service.ThinkVipService;
import com.gxwebsoft.think.service.WechatConfigService;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import com.wechat.pay.java.service.refund.model.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
* 控制器
*
* @author 科技小王子
* @since 2024-08-11 20:50:59
*/
@Api(tags = "管理")
@RestController
@RequestMapping("/api/think/think-recharge-log")
public class ThinkRechargeLogController extends BaseController {
@Resource
private ThinkRechargeLogService thinkRechargeLogService;
@Resource
private WechatConfigService wechatConfigService;
@Resource
private ThinkUsersVipService thinkUsersVipService;
@Resource
private ThinkVipService thinkVipService;
@PostMapping("/refund-notify")
public ApiResult<?> refundNotify(@RequestBody String body, @RequestHeader Map<String, Object> header) {
System.out.println("recharge refund body = " + body);
com.wechat.pay.java.core.notification.RequestParam requestParam = new RequestParam.Builder()
.serialNumber(header.get("wechatpay-serial").toString())
.nonce(header.get("wechatpay-nonce").toString())
.signature(header.get("wechatpay-signature").toString())
.timestamp(header.get("wechatpay-timestamp").toString())
.body(body)
.build();
NotificationConfig config = wechatConfigService.getNotificationConfig();
// 初始化 NotificationParser
NotificationParser parser = new NotificationParser(config);
RefundNotification notification = parser.parse(requestParam, RefundNotification.class);
System.out.println("notification = " + notification);
String refundNo = notification.getOutRefundNo();
if (notification.getRefundStatus().equals(Status.SUCCESS)) {
ThinkRechargeLog thinkRechargeLog = thinkRechargeLogService.getByRefundNumber(refundNo);
if (thinkRechargeLog != null) {
thinkRechargeLog.setStatus(3);
thinkRechargeLogService.updateById(thinkRechargeLog);
ThinkUsersVip thinkUsersVip = thinkUsersVipService.getById(thinkRechargeLog.getVid());
if (thinkUsersVip != null) {
if (thinkUsersVip.getType().equals(4) || thinkUsersVip.getType().equals(5)) {
BigDecimal remain = thinkUsersVip.getRemainingMoney().subtract(thinkUsersVip.getPrice());
if (remain.compareTo(BigDecimal.ZERO) < 1) thinkUsersVip.setStatus(3);
else thinkUsersVip.setRemainingMoney(remain);
}
else {
int num = thinkUsersVip.getNum() - thinkUsersVip.getCount();
if (num <= 0) thinkUsersVip.setStatus(3);
else thinkUsersVip.setNum(num);
}
thinkUsersVipService.updateById(thinkUsersVip);
}
}
}
return success();
}
@ApiOperation("退款")
@PostMapping("/refund")
public ApiResult<?> refund(@RequestBody ThinkRechargeLog thinkRechargeLog) {
ThinkRechargeLog thinkRechargeLog1 = thinkRechargeLogService.getById(thinkRechargeLog.getId());
thinkRechargeLog1.setRefundNo("R" + thinkRechargeLog1.getOrderNum());
thinkRechargeLogService.updateById(thinkRechargeLog1);
thinkRechargeLogService.refund(thinkRechargeLog1);
return success();
}
@ApiOperation("分页查询")
@GetMapping("/page")
public ApiResult<PageResult<ThinkRechargeLog>> page(ThinkRechargeLogParam param) {
// 使用关联查询
return success(thinkRechargeLogService.pageRel(param));
}
@ApiOperation("查询全部")
@GetMapping()
public ApiResult<List<ThinkRechargeLog>> list(ThinkRechargeLogParam param) {
// 使用关联查询
return success(thinkRechargeLogService.listRel(param));
}
@PreAuthorize("hasAuthority('think:thinkRechargeLog:list')")
@ApiOperation("根据id查询")
@GetMapping("/{id}")
public ApiResult<ThinkRechargeLog> get(@PathVariable("id") Integer id) {
// 使用关联查询
return success(thinkRechargeLogService.getByIdRel(id));
}
@ApiOperation("添加")
@PostMapping()
public ApiResult<?> save(@RequestBody ThinkRechargeLog thinkRechargeLog) {
if (thinkRechargeLogService.save(thinkRechargeLog)) {
return success("添加成功");
}
return fail("添加失败");
}
@ApiOperation("修改")
@PutMapping()
public ApiResult<?> update(@RequestBody ThinkRechargeLog thinkRechargeLog) {
if (thinkRechargeLogService.updateById(thinkRechargeLog)) {
return success("修改成功");
}
return fail("修改失败");
}
@ApiOperation("删除")
@DeleteMapping("/{id}")
public ApiResult<?> remove(@PathVariable("id") Integer id) {
if (thinkRechargeLogService.removeById(id)) {
return success("删除成功");
}
return fail("删除失败");
}
@ApiOperation("批量添加")
@PostMapping("/batch")
public ApiResult<?> saveBatch(@RequestBody List<ThinkRechargeLog> list) {
if (thinkRechargeLogService.saveBatch(list)) {
return success("添加成功");
}
return fail("添加失败");
}
@ApiOperation("批量修改")
@PutMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody BatchParam<ThinkRechargeLog> batchParam) {
if (batchParam.update(thinkRechargeLogService, "id")) {
return success("修改成功");
}
return fail("修改失败");
}
@ApiOperation("批量删除")
@DeleteMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
if (thinkRechargeLogService.removeByIds(ids)) {
return success("删除成功");
}
return fail("删除失败");
}
}