355 lines
14 KiB
Java
355 lines
14 KiB
Java
package com.ruoyi.system.rental;
|
|
|
|
import static com.ruoyi.system.rental.RentalValues.*;
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
import com.ruoyi.common.utils.SecurityUtils;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.*;
|
|
|
|
/** 登录业务入口。输入仅提取命令字段,不把客户端对象直接映射为数据库实体。 */
|
|
@RestController
|
|
@RequestMapping("/rental")
|
|
public class RentalController {
|
|
private final RentalStore store;
|
|
private final RentalProvisioning provisioning;
|
|
private final RentalIdentity identity;
|
|
private final RentalOrders orders;
|
|
private final RentalFunds funds;
|
|
private final RentalReviews reviews;
|
|
private final RentalTasks tasks;
|
|
private final RentalChain chain;
|
|
private final RentalQueries queries;
|
|
|
|
@Value("${rental.deposit-address.nile:}")
|
|
private String nileDeposit;
|
|
|
|
@Value("${rental.deposit-address.mainnet:}")
|
|
private String mainDeposit;
|
|
|
|
@org.springframework.beans.factory.annotation.Autowired
|
|
private RentalReconciliation reconciliation;
|
|
|
|
public RentalController(
|
|
RentalStore store,
|
|
RentalProvisioning provisioning,
|
|
RentalIdentity identity,
|
|
RentalOrders orders,
|
|
RentalFunds funds,
|
|
RentalReviews reviews,
|
|
RentalTasks tasks,
|
|
RentalChain chain,
|
|
RentalQueries queries) {
|
|
this.store = store;
|
|
this.provisioning = provisioning;
|
|
this.identity = identity;
|
|
this.orders = orders;
|
|
this.funds = funds;
|
|
this.reviews = reviews;
|
|
this.tasks = tasks;
|
|
this.chain = chain;
|
|
this.queries = queries;
|
|
}
|
|
|
|
private long user() {
|
|
Long user = SecurityUtils.getUserId();
|
|
require(user != null, "请先登录");
|
|
return user;
|
|
}
|
|
|
|
@GetMapping("/mine/{resource}")
|
|
@PreAuthorize("@ss.hasPermi('rent:portal:view')")
|
|
public AjaxResult mine(
|
|
@PathVariable String resource, @RequestParam(defaultValue = "1") int page) {
|
|
return AjaxResult.success(queries.list(resource, user(), page));
|
|
}
|
|
|
|
@GetMapping("/manage/{resource}")
|
|
@PreAuthorize("@ss.hasPermi('rent:manage:list')")
|
|
public AjaxResult manage(
|
|
@PathVariable String resource, @RequestParam(defaultValue = "1") int page) {
|
|
return AjaxResult.success(queries.list(resource, null, page));
|
|
}
|
|
|
|
@PostMapping("/register/{resource}")
|
|
@PreAuthorize("@ss.hasPermi('rent:portal:register')")
|
|
public AjaxResult register(
|
|
@PathVariable String resource, @RequestBody Map<String, Object> body) {
|
|
return AjaxResult.success(provisioning.create(resource, body, user()));
|
|
}
|
|
|
|
@PostMapping("/credential/issue")
|
|
@PreAuthorize("@ss.hasPermi('rent:credential:issue')")
|
|
public AjaxResult issue(@RequestBody Map<String, Object> body) {
|
|
return AjaxResult.success(
|
|
identity.issue(input(body, "owner_type", 8), number(body, "owner_id"), user()));
|
|
}
|
|
|
|
@PostMapping("/credential/{id}/revoke")
|
|
@PreAuthorize("@ss.hasPermi('rent:credential:issue')")
|
|
public AjaxResult revoke(@PathVariable long id) {
|
|
identity.revoke(id, user());
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/wallet/{id}/challenge")
|
|
@PreAuthorize("@ss.hasPermi('rent:wallet:bind')")
|
|
public AjaxResult challenge(@PathVariable long id) {
|
|
return AjaxResult.success(provisioning.challenge(id, user()));
|
|
}
|
|
|
|
@PostMapping("/wallet/{id}/prove")
|
|
@PreAuthorize("@ss.hasPermi('rent:wallet:bind')")
|
|
public AjaxResult prove(@PathVariable long id, @RequestBody Map<String, Object> body) {
|
|
provisioning.prove(id, input(body, "nonce", 64), input(body, "signature", 256), user());
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/wallet/{id}/policy")
|
|
@PreAuthorize("@ss.hasPermi('rent:wallet:bind')")
|
|
public AjaxResult policy(@PathVariable long id, @RequestBody Map<String, Object> body) {
|
|
provisioning.policy(id, body, user());
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/package")
|
|
@PreAuthorize("@ss.hasPermi('rent:package:edit')")
|
|
public AjaxResult product(@RequestBody Map<String, Object> body) {
|
|
return AjaxResult.success(provisioning.product(body, user()));
|
|
}
|
|
|
|
@PostMapping("/status/{resource}/{id}")
|
|
@PreAuthorize("@ss.hasPermi('rent:manage:audit')")
|
|
public AjaxResult status(
|
|
@PathVariable String resource,
|
|
@PathVariable long id,
|
|
@RequestBody Map<String, Object> body) {
|
|
provisioning.status(
|
|
resource, id, input(body, "status", 32), user(), input(body, "reason", 512));
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/order")
|
|
@PreAuthorize("@ss.hasPermi('rent:order:create')")
|
|
public AjaxResult order(@RequestBody Map<String, Object> body) {
|
|
Map<String, Object> order = orders.create(number(body, "app_id"), user(), body);
|
|
return AjaxResult.success(
|
|
map(
|
|
"id",
|
|
order.get("id"),
|
|
"order_no",
|
|
order.get("order_no"),
|
|
"status",
|
|
order.get("status")));
|
|
}
|
|
|
|
@PostMapping("/order/{id}/cancel")
|
|
@PreAuthorize("@ss.hasPermi('rent:order:create')")
|
|
public AjaxResult cancel(@PathVariable long id) {
|
|
orders.cancel(id, user());
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/account/open")
|
|
@PreAuthorize("@ss.hasPermi('rent:portal:register')")
|
|
public AjaxResult account(@RequestBody Map<String, Object> body) {
|
|
return AjaxResult.success(funds.account(user(), input(body, "network", 16)));
|
|
}
|
|
|
|
@PostMapping("/account/{id}/topup")
|
|
@PreAuthorize("@ss.hasPermi('rent:account:topup')")
|
|
public AjaxResult topup(@PathVariable long id, @RequestBody Map<String, Object> body) {
|
|
Map<String, Object> account = store.get("account", id, false);
|
|
String network = text(account, "network");
|
|
String deposit = "NILE".equals(network) ? nileDeposit : mainDeposit;
|
|
require(!deposit.isEmpty(), "尚未配置该网络租赁充值收款地址");
|
|
java.math.BigDecimal amount = money(body.get("amount"));
|
|
require(amount.signum() > 0, "充值金额必须大于0");
|
|
Map<String, Object> proof =
|
|
chain.transfer(
|
|
network,
|
|
input(body, "tx_id", 64),
|
|
(int) number(body, "log_index"),
|
|
deposit,
|
|
amount);
|
|
String reason = input(body, "reason", 512);
|
|
long actor = user();
|
|
tasks.tx(
|
|
() -> {
|
|
funds.post(
|
|
id,
|
|
"TOPUP:" + text(proof, "reference"),
|
|
"TOPUP",
|
|
amount,
|
|
ZERO,
|
|
ZERO,
|
|
ZERO,
|
|
null,
|
|
null,
|
|
null,
|
|
actor,
|
|
text(proof, "reference"),
|
|
reason);
|
|
store.event("ACCOUNT", id, "TOPUP", actor, reason);
|
|
return null;
|
|
});
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/order/{id}/refund")
|
|
@PreAuthorize("@ss.hasPermi('rent:refund:create')")
|
|
public AjaxResult refund(@PathVariable long id, @RequestBody Map<String, Object> body) {
|
|
return AjaxResult.success(reviews.refund(id, body, user()));
|
|
}
|
|
|
|
@PostMapping("/refund/{id}/review")
|
|
@PreAuthorize("@ss.hasPermi('rent:refund:approve')")
|
|
public AjaxResult reviewRefund(@PathVariable long id, @RequestBody Map<String, Object> body) {
|
|
require(body.get("approve") instanceof Boolean, "必须明确审批结果");
|
|
reviews.approveRefund(id, Boolean.TRUE.equals(body.get("approve")), user());
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/settlement")
|
|
@PreAuthorize("@ss.hasPermi('rent:settlement:create')")
|
|
public AjaxResult settlement(@RequestBody Map<String, Object> body) {
|
|
require(body.get("order_ids") instanceof List, "缺少来源订单");
|
|
List<Long> ids = new ArrayList<>();
|
|
for (Object value : (List<?>) body.get("order_ids")) {
|
|
ids.add(Long.valueOf(value.toString()));
|
|
}
|
|
return AjaxResult.success(reviews.settlement(ids, user()));
|
|
}
|
|
|
|
@PostMapping("/settlement/{id}/{action}")
|
|
@PreAuthorize("@ss.hasPermi('rent:settlement:approve')")
|
|
public AjaxResult settlementAction(
|
|
@PathVariable long id,
|
|
@PathVariable String action,
|
|
@RequestBody Map<String, Object> body) {
|
|
String reason = input(body, "reason", 512);
|
|
long actor = user();
|
|
if ("confirm".equals(action)) {
|
|
Map<String, Object> row = store.get("settlement", id, false),
|
|
account = store.get("account", number(row, "account_id"), false);
|
|
Map<String, Object> authorization;
|
|
try {
|
|
authorization =
|
|
new com.fasterxml.jackson.databind.ObjectMapper()
|
|
.readValue(text(row, "payment_evidence"), Map.class);
|
|
} catch (Exception ex) {
|
|
throw new IllegalArgumentException("缺少原付款授权收款地址");
|
|
}
|
|
Map<String, Object> proof =
|
|
chain.transfer(
|
|
text(account, "network"),
|
|
input(body, "tx_id", 64),
|
|
(int) number(body, "log_index"),
|
|
text(authorization, "receiver_address"),
|
|
amount(row, "amount"));
|
|
reviews.paid(id, text(proof, "reference"), orders.encode(proof), actor);
|
|
return AjaxResult.success();
|
|
}
|
|
return AjaxResult.success(
|
|
tasks.tx(
|
|
() -> {
|
|
Map<String, Object> row = reviews.transition(id, action, actor, reason);
|
|
if ("pay".equals(action) && row.get("payment_evidence") == null) {
|
|
String receiver = input(body, "receiver_address", 64);
|
|
RentalOrders.address(receiver);
|
|
store.update(
|
|
"settlement",
|
|
id,
|
|
map(
|
|
"payment_evidence",
|
|
orders.encode(
|
|
map(
|
|
"receiver_address",
|
|
receiver,
|
|
"payout_no",
|
|
text(row, "payout_no")))));
|
|
row = store.get("settlement", id, false);
|
|
}
|
|
return row;
|
|
}));
|
|
}
|
|
|
|
@PostMapping("/wallet/{id}/refresh")
|
|
@PreAuthorize("@ss.hasPermi('rent:manage:audit')")
|
|
public AjaxResult refresh(@PathVariable long id) {
|
|
tasks.refresh(id);
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/task/{id}/verify")
|
|
@PreAuthorize("@ss.hasPermi('rent:manage:audit')")
|
|
public AjaxResult verify(@PathVariable long id) {
|
|
tasks.verify(id);
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/task/{id}/attach")
|
|
@PreAuthorize("@ss.hasPermi('rent:manage:audit')")
|
|
public AjaxResult attach(@PathVariable long id, @RequestBody Map<String, Object> body) {
|
|
tasks.attach(id, input(body, "tx_id", 64), user(), input(body, "reason", 512));
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/order/{id}/resolve")
|
|
@PreAuthorize("@ss.hasPermi('rent:manage:audit')")
|
|
public AjaxResult resolve(@PathVariable long id, @RequestBody Map<String, Object> body) {
|
|
tasks.resolveRecovered(id, user(), input(body, "reason", 512));
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@PostMapping("/webhook/{id}/retry")
|
|
@PreAuthorize("@ss.hasPermi('rent:manage:audit')")
|
|
public AjaxResult retryWebhook(@PathVariable long id, @RequestBody Map<String, Object> body) {
|
|
String reason = input(body, "reason", 512);
|
|
long actor = user();
|
|
tasks.tx(
|
|
() -> {
|
|
Map<String, Object> row = store.get("webhook", id, true);
|
|
require("DEAD".equals(text(row, "status")), "只允许人工重新投递已终止的事件");
|
|
store.event(
|
|
"ORDER",
|
|
number(row, "order_id"),
|
|
"RETRY_WEBHOOK",
|
|
actor,
|
|
reason + ";原重试次数=" + text(row, "attempt_count"));
|
|
store.update(
|
|
"webhook",
|
|
id,
|
|
map(
|
|
"status",
|
|
"PENDING",
|
|
"attempt_count",
|
|
0,
|
|
"next_attempt_at",
|
|
now(),
|
|
"lock_owner",
|
|
null,
|
|
"lock_until",
|
|
null));
|
|
return null;
|
|
});
|
|
return AjaxResult.success();
|
|
}
|
|
|
|
@GetMapping("/reconcile")
|
|
@PreAuthorize("@ss.hasPermi('rent:manage:audit')")
|
|
public AjaxResult reconcile(@RequestParam(defaultValue = "0") long afterId) {
|
|
require(afterId >= 0, "核对游标不能为负");
|
|
return AjaxResult.success(
|
|
map(
|
|
"accounts",
|
|
reconciliation.check(afterId),
|
|
"reservations",
|
|
reconciliation.reservations()));
|
|
}
|
|
}
|