新版仿ok交易所-后端
1
zj
2026-06-03 1004f3d16011f69894196bfd180ea539b76ba4e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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;
    }
}