package com.yami.trading.admin.controller.loan;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.yami.trading.admin.facade.PermissionFacade;
|
import com.yami.trading.bean.loan.LoanOrder;
|
import com.yami.trading.common.constants.Constants;
|
import com.yami.trading.common.domain.Result;
|
import com.yami.trading.common.util.StringUtils;
|
import com.yami.trading.security.common.util.SecurityUtils;
|
import com.yami.trading.service.loan.LoanOrderService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
@RestController
|
@CrossOrigin
|
@Slf4j
|
public class AdminLoanOrderController {
|
|
private final String action = "normal/loanadmin!";
|
|
@Autowired
|
private LoanOrderService loanOrderService;
|
@Autowired
|
private PermissionFacade permissionFacade;
|
|
private static final Map<Integer, String> STATE_LABEL = new HashMap<>();
|
private static final Map<Integer, String> REPAYMENT_LABEL = new HashMap<>();
|
|
static {
|
STATE_LABEL.put(1, "未审");
|
STATE_LABEL.put(2, "通过");
|
STATE_LABEL.put(3, "驳回");
|
STATE_LABEL.put(4, "逾期");
|
STATE_LABEL.put(5, "已还款");
|
REPAYMENT_LABEL.put(1, "到期一次还款");
|
}
|
|
@RequestMapping(action + "list.action")
|
public Result<Page<Map<String, Object>>> list(HttpServletRequest request) {
|
int current = parseInt(request.getParameter("current"), 1);
|
int size = parseInt(request.getParameter("size"), 10);
|
String status = normalizeStatusFilter(request.getParameter("status"));
|
String userName = firstNonBlank(request.getParameter("userName"), request.getParameter("user_name"));
|
String orderNo = request.getParameter("orderNo");
|
String rolename = request.getParameter("rolename");
|
List<String> children = permissionFacade.getOwnerUserIds();
|
Page<Map<String, Object>> page = loanOrderService.adminPagedQuery(current, size, status, userName, orderNo, rolename, children);
|
List<Map<String, Object>> records = new ArrayList<>();
|
for (Map<String, Object> row : page.getRecords()) {
|
records.add(formatAdminRow(row));
|
}
|
page.setRecords(records);
|
return Result.ok(page);
|
}
|
|
@RequestMapping(action + "change.action")
|
public Result<String> change(HttpServletRequest request) {
|
String orderId = firstNonBlank(request.getParameter("orderId"), request.getParameter("uuid"));
|
String statusStr = request.getParameter("statusStr");
|
String reason = request.getParameter("reason");
|
String operator = SecurityUtils.getSysUser().getUsername();
|
String err;
|
if ("2".equals(statusStr)) {
|
err = loanOrderService.approve(orderId, operator);
|
} else if ("3".equals(statusStr)) {
|
err = loanOrderService.reject(orderId, reason, operator);
|
} else if ("5".equals(statusStr)) {
|
err = loanOrderService.manualRepay(orderId, operator);
|
} else {
|
return Result.failed("Invalid status");
|
}
|
if (err != null) {
|
return Result.failed(err);
|
}
|
return Result.ok("Success");
|
}
|
|
@RequestMapping(action + "modify.action")
|
public Result<String> modify(HttpServletRequest request) {
|
LoanOrder order = new LoanOrder();
|
order.setUuid(firstNonBlank(request.getParameter("orderNo"), request.getParameter("uuid")));
|
order.setSymbol(request.getParameter("symbol"));
|
order.setQuota(parseDouble(request.getParameter("quota"), 0));
|
order.setIdFrontImg(firstNonBlank(request.getParameter("img_idimg_1"), request.getParameter("idFrontImg")));
|
order.setIdBackImg(firstNonBlank(request.getParameter("img_idimg_2"), request.getParameter("idBackImg")));
|
order.setHandheldImg(firstNonBlank(request.getParameter("img_idimg_3"), request.getParameter("handheldImg")));
|
loanOrderService.modifyOrder(order);
|
return Result.ok("Success");
|
}
|
|
private Map<String, Object> formatAdminRow(Map<String, Object> row) {
|
Map<String, Object> map = new HashMap<>(row);
|
int state = toInt(row.get("state"));
|
int repayment = toInt(row.get("repayment"));
|
String lendingInstitution = row.get("lendingInstitution") != null ? row.get("lendingInstitution").toString() : "1";
|
String lendingName = row.get("lendingName") != null ? row.get("lendingName").toString() : "";
|
map.put("state", new Object[]{state, STATE_LABEL.getOrDefault(state, String.valueOf(state))});
|
map.put("repayment", new Object[]{repayment, REPAYMENT_LABEL.getOrDefault(repayment, "到期一次还款")});
|
map.put("lendingInstitution", new Object[]{lendingInstitution, lendingName});
|
map.put("createTime", formatDate(row.get("createTime")));
|
map.put("houseImgs", buildHouseImgs(row));
|
map.put("userId", row.get("userId"));
|
map.put("userCode", row.get("userCode"));
|
return map;
|
}
|
|
private List<String> buildHouseImgs(Map<String, Object> row) {
|
List<String> imgs = new ArrayList<>();
|
imgs.add(fullImage(getStr(row, "idFrontImg")));
|
imgs.add(fullImage(getStr(row, "idBackImg")));
|
imgs.add(fullImage(getStr(row, "handheldImg")));
|
return imgs;
|
}
|
|
private String fullImage(String path) {
|
if (StringUtils.isNullOrEmpty(path)) {
|
return "";
|
}
|
if (path.startsWith("http")) {
|
return path;
|
}
|
return Constants.IMAGES_HTTP + path;
|
}
|
|
private String formatDate(Object val) {
|
if (val == null) {
|
return "";
|
}
|
if (val instanceof Date) {
|
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) val);
|
}
|
return val.toString();
|
}
|
|
private int toInt(Object val) {
|
if (val == null) {
|
return 0;
|
}
|
return Integer.parseInt(val.toString());
|
}
|
|
private String getStr(Map<String, Object> row, String key) {
|
Object val = row.get(key);
|
return val == null ? "" : val.toString();
|
}
|
|
private int parseInt(String val, int defaultVal) {
|
try {
|
return StringUtils.isNullOrEmpty(val) ? defaultVal : Integer.parseInt(val);
|
} catch (NumberFormatException e) {
|
return defaultVal;
|
}
|
}
|
|
private double parseDouble(String val, double defaultVal) {
|
try {
|
return StringUtils.isNullOrEmpty(val) ? defaultVal : Double.parseDouble(val);
|
} catch (NumberFormatException e) {
|
return defaultVal;
|
}
|
}
|
|
private String firstNonBlank(String... values) {
|
for (String v : values) {
|
if (!StringUtils.isNullOrEmpty(v)) {
|
return v;
|
}
|
}
|
return null;
|
}
|
|
/** 全部:不传 status 或传 0/空,均不按状态过滤 */
|
private String normalizeStatusFilter(String status) {
|
if (StringUtils.isNullOrEmpty(status)) {
|
return null;
|
}
|
String s = status.trim();
|
if ("0".equals(s) || "all".equalsIgnoreCase(s)) {
|
return null;
|
}
|
return s;
|
}
|
}
|