|
|
@@ -8,19 +8,15 @@ import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import space.anyi.serve.entity.Response;
|
|
|
import space.anyi.serve.entity.auth.JwtUserDetails;
|
|
|
-import space.anyi.serve.entity.wallet.Wallet;
|
|
|
-import space.anyi.serve.entity.wallet.WalletTransaction;
|
|
|
-import space.anyi.serve.entity.wallet.WalletTransactionVo;
|
|
|
+import space.anyi.serve.entity.wallet.*;
|
|
|
import space.anyi.serve.service.WalletService;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
@Tag(name = "WalletController", description = "钱包管理")
|
|
|
@RestController
|
|
|
-@RequestMapping("api/wallet")
|
|
|
+@RequestMapping("wallet")
|
|
|
public class WalletController {
|
|
|
|
|
|
private final WalletService walletService;
|
|
|
@@ -32,53 +28,47 @@ public class WalletController {
|
|
|
@Operation(summary = "查询余额")
|
|
|
@PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')")
|
|
|
@GetMapping("balance")
|
|
|
- public Response<Map<String, Object>> getBalance(Authentication authentication) {
|
|
|
+ public Response<WalletVo> getBalance(Authentication authentication) {
|
|
|
JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
|
|
|
Long userId = details.getUser().getId();
|
|
|
Wallet wallet = walletService.getByUserId(userId);
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("balance", wallet != null ? wallet.getBalance() : BigDecimal.ZERO);
|
|
|
- return Response.ok(result);
|
|
|
+ return Response.ok(new WalletVo(wallet != null ? wallet.getBalance() : BigDecimal.ZERO));
|
|
|
}
|
|
|
|
|
|
@Operation(summary = "充值(预留接口,待对接支付平台)")
|
|
|
@PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')")
|
|
|
@PostMapping("recharge")
|
|
|
- public Response<Map<String, Object>> recharge(@RequestBody Map<String, BigDecimal> body, Authentication authentication) {
|
|
|
- JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
|
|
|
- Long userId = details.getUser().getId();
|
|
|
- BigDecimal amount = body.get("amount");
|
|
|
- if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ public Response<String> recharge(@Valid @RequestBody RechargeDto dto, Authentication authentication) {
|
|
|
+ if (dto.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
return Response.error("充值金额必须大于0");
|
|
|
}
|
|
|
- walletService.addBalance(userId, amount, "recharge", "用户充值");
|
|
|
- return Response.ok(Map.of("message", "充值成功"));
|
|
|
+ JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
|
|
|
+ Long userId = details.getUser().getId();
|
|
|
+ walletService.addBalance(userId, dto.getAmount(), "recharge", "用户充值");
|
|
|
+ return Response.ok("充值成功");
|
|
|
}
|
|
|
|
|
|
@Operation(summary = "管理员代充值")
|
|
|
@PreAuthorize("hasRole('ROLE_admin')")
|
|
|
@PostMapping("admin-recharge")
|
|
|
- public Response<Map<String, Object>> adminRecharge(@RequestBody Map<String, Object> body) {
|
|
|
- Long userId = Long.valueOf(body.get("userId").toString());
|
|
|
- BigDecimal amount = new BigDecimal(body.get("amount").toString());
|
|
|
- String remark = (String) body.getOrDefault("remark", "管理员代充值");
|
|
|
- if (amount.compareTo(BigDecimal.ZERO) <= 0) return Response.error("金额必须大于0");
|
|
|
- walletService.addBalance(userId, amount, "admin_adjust", remark);
|
|
|
- return Response.ok(Map.of("message", "充值成功"));
|
|
|
+ public Response<String> adminRecharge(@Valid @RequestBody AdminRechargeDto dto) {
|
|
|
+ if (dto.getAmount().compareTo(BigDecimal.ZERO) <= 0) return Response.error("金额必须大于0");
|
|
|
+ String remark = dto.getRemark() != null ? dto.getRemark() : "管理员代充值";
|
|
|
+ walletService.addBalance(dto.getUserId(), dto.getAmount(), "admin_adjust", remark);
|
|
|
+ return Response.ok("充值成功");
|
|
|
}
|
|
|
|
|
|
@Operation(summary = "提现申请")
|
|
|
@PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')")
|
|
|
@PostMapping("withdraw")
|
|
|
- public Response<Map<String, Object>> applyWithdraw(@RequestBody Map<String, BigDecimal> body, Authentication authentication) {
|
|
|
- JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
|
|
|
- Long userId = details.getUser().getId();
|
|
|
- BigDecimal amount = body.get("amount");
|
|
|
- if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ public Response<WithdrawApplyVo> applyWithdraw(@Valid @RequestBody WithdrawDto dto, Authentication authentication) {
|
|
|
+ if (dto.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
return Response.error("提现金额必须大于0");
|
|
|
}
|
|
|
- WalletTransaction tx = walletService.applyWithdraw(userId, amount);
|
|
|
- return Response.ok(Map.of("transactionId", tx.getId().toString(), "status", tx.getStatus()));
|
|
|
+ JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
|
|
|
+ Long userId = details.getUser().getId();
|
|
|
+ WalletTransaction tx = walletService.applyWithdraw(userId, dto.getAmount());
|
|
|
+ return Response.ok(new WithdrawApplyVo(tx.getId().toString(), tx.getStatus(), tx.getAmount()));
|
|
|
}
|
|
|
|
|
|
@Operation(summary = "获取待审核提现列表(管理员)")
|
|
|
@@ -94,28 +84,21 @@ public class WalletController {
|
|
|
@Operation(summary = "审核提现(管理员)")
|
|
|
@PreAuthorize("hasRole('ROLE_admin')")
|
|
|
@PutMapping("withdraw/{id}/review")
|
|
|
- public Response<Void> reviewWithdraw(@PathVariable Long id, @RequestBody Map<String, Object> body) {
|
|
|
- Boolean approved = (Boolean) body.get("approved");
|
|
|
- String rejectReason = (String) body.get("rejectReason");
|
|
|
- if (approved == null) return Response.error("请指定审核结果");
|
|
|
- walletService.reviewWithdraw(id, approved, rejectReason);
|
|
|
+ public Response<Void> reviewWithdraw(@PathVariable Long id, @Valid @RequestBody WithdrawReviewDto dto) {
|
|
|
+ walletService.reviewWithdraw(id, dto.getApproved(), dto.getRejectReason());
|
|
|
return Response.ok();
|
|
|
}
|
|
|
|
|
|
@Operation(summary = "资金明细")
|
|
|
@PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')")
|
|
|
@GetMapping("transactions")
|
|
|
- public Response<Map<String, Object>> getTransactions(
|
|
|
+ public Response<List<WalletTransactionVo>> getTransactions(
|
|
|
@RequestParam(defaultValue = "1") int pageNum,
|
|
|
@RequestParam(defaultValue = "10") int pageSize,
|
|
|
Authentication authentication) {
|
|
|
JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
|
|
|
Long userId = details.getUser().getId();
|
|
|
List<WalletTransaction> list = walletService.getTransactions(userId, pageNum, pageSize);
|
|
|
- List<WalletTransactionVo> vos = WalletTransactionVo.from(list);
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("list", vos);
|
|
|
- result.put("total", vos.size());
|
|
|
- return Response.ok(result);
|
|
|
+ return Response.ok(WalletTransactionVo.from(list));
|
|
|
}
|
|
|
}
|