新版仿ok交易所-后端
1
zj
2026-06-04 bf362d7ee6ab091cd26d69be9095440e5000928c
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.yami.trading.admin.controller.loan;
 
import com.yami.trading.bean.loan.LoanConfig;
import com.yami.trading.bean.loan.LoanOrder;
import com.yami.trading.common.constants.Constants;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.common.web.ResultObject;
import com.yami.trading.security.common.util.SecurityUtils;
import com.yami.trading.service.loan.LoanConfigService;
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.util.*;
import java.util.concurrent.TimeUnit;
 
@RestController
@CrossOrigin
@Slf4j
public class LoanController {
 
    private final String action = "/api/loan!";
 
    @Autowired
    private LoanConfigService loanConfigService;
    @Autowired
    private LoanOrderService loanOrderService;
 
    @RequestMapping(action + "getLoanParamList.action")
    public Object getLoanParamList() {
        ResultObject result = new ResultObject();
        try {
            List<Map<String, Object>> list = new ArrayList<>();
            for (LoanConfig config : loanConfigService.listEnabled()) {
                list.add(buildConfigMap(config));
            }
            result.setData(list);
            result.setCode("0");
        } catch (BusinessException e) {
            result.setCode("1");
            result.setMsg(e.getMessage());
        } catch (Exception e) {
            result.setCode("1");
            result.setMsg("Program error");
            log.error("getLoanParamList error", e);
        }
        return result;
    }
 
    @RequestMapping(action + "apply.action")
    public Object apply(HttpServletRequest request) {
        ResultObject result = new ResultObject();
        try {
            String userId = SecurityUtils.getCurrentUserId();
            if (StringUtils.isNullOrEmpty(userId)) {
                result.setCode("403");
                result.setMsg("Please login first");
                return result;
            }
            String configId = firstNonBlank(request.getParameter("uuid"), request.getParameter("id"));
            double quota = parseDouble(request.getParameter("quota"), 0);
            String symbol = request.getParameter("symbol");
            String front = firstNonBlank(request.getParameter("frontFile"), request.getParameter("idFrontImg"));
            String back = firstNonBlank(request.getParameter("reverseFile"), request.getParameter("idBackImg"));
            String handheld = firstNonBlank(request.getParameter("fileList"), request.getParameter("handheldImg"));
            LoanOrder order = loanOrderService.apply(userId, configId, quota, symbol, front, back, handheld);
            result.setData(buildOrderMap(order));
            result.setCode("0");
        } catch (BusinessException e) {
            result.setCode("1");
            result.setMsg(e.getMessage());
        } catch (Exception e) {
            result.setCode("1");
            result.setMsg(e.getMessage());
            log.error("loan apply error", e);
        }
        return result;
    }
 
    @RequestMapping(action + "getOrders.action")
    public Object getOrders() {
        ResultObject result = new ResultObject();
        try {
            String userId = SecurityUtils.getCurrentUserId();
            if (StringUtils.isNullOrEmpty(userId)) {
                result.setData(Collections.emptyList());
                result.setCode("0");
                return result;
            }
            List<Map<String, Object>> list = new ArrayList<>();
            for (LoanOrder order : loanOrderService.listByUser(userId)) {
                list.add(buildOrderMap(order));
            }
            result.setData(list);
            result.setCode("0");
        } catch (Exception e) {
            result.setCode("1");
            result.setMsg("Program error");
            log.error("getOrders error", e);
        }
        return result;
    }
 
    @RequestMapping(action + "getOrderDetail.action")
    public Object getOrderDetail(HttpServletRequest request) {
        ResultObject result = new ResultObject();
        try {
            String userId = SecurityUtils.getCurrentUserId();
            String orderNo = request.getParameter("orderNo");
            String id = request.getParameter("id");
            LoanOrder order = null;
            if (!StringUtils.isNullOrEmpty(orderNo)) {
                order = loanOrderService.findByOrderNo(orderNo);
            }
            if (order == null && !StringUtils.isNullOrEmpty(id)) {
                order = loanOrderService.findById(id);
            }
            if (order == null || !Objects.equals(order.getUserId(), userId)) {
                result.setCode("1");
                result.setMsg("Order not found");
                return result;
            }
            result.setData(buildOrderMap(order));
            result.setCode("0");
        } catch (Exception e) {
            result.setCode("1");
            result.setMsg("Program error");
            log.error("getOrderDetail error", e);
        }
        return result;
    }
 
    private Map<String, Object> buildConfigMap(LoanConfig config) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", config.getUuid());
        map.put("uuid", config.getUuid());
        map.put("term", config.getTerm());
        map.put("dailyRate", config.getDailyRate());
        map.put("maxQuota", config.getMaxQuota());
        map.put("lendingInstitution", config.getLendingInstitution());
        map.put("lendingName", config.getLendingName());
        map.put("repayment", config.getRepayment());
        map.put("repayCycle", config.getRepayCycle() != null ? config.getRepayCycle() : config.getTerm());
        return map;
    }
 
    private Map<String, Object> buildOrderMap(LoanOrder order) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", order.getUuid());
        map.put("uuid", order.getUuid());
        map.put("orderNo", order.getOrderNo());
        map.put("quota", order.getQuota());
        map.put("symbol", order.getSymbol());
        map.put("term", order.getTerm());
        map.put("dailyRate", order.getDailyRate());
        map.put("totalInterest", order.getTotalInterest());
        map.put("repayCycle", order.getRepayCycle());
        map.put("repayment", order.getRepayment());
        map.put("lendingInstitution", order.getLendingInstitution());
        map.put("lendingName", order.getLendingName());
        map.put("state", order.getState());
        map.put("status", order.getState());
        map.put("reason", order.getReason());
        map.put("createTime", order.getCreateTime() != null ? order.getCreateTime().getTime() : null);
        map.put("dueTime", order.getDueTime());
        map.put("remainQuota", calcRemainDays(order));
        map.put("idFrontImg", fullImage(order.getIdFrontImg()));
        map.put("idBackImg", fullImage(order.getIdBackImg()));
        map.put("handheldImg", fullImage(order.getHandheldImg()));
        return map;
    }
 
    private int calcRemainDays(LoanOrder order) {
        if (order.getDueTime() == null) {
            return order.getTerm();
        }
        long diff = order.getDueTime().getTime() - System.currentTimeMillis();
        return (int) Math.max(0, TimeUnit.MILLISECONDS.toDays(diff));
    }
 
    private String fullImage(String path) {
        if (StringUtils.isNullOrEmpty(path)) {
            return path;
        }
        if (path.startsWith("http")) {
            return path;
        }
        return Constants.IMAGES_HTTP + path;
    }
 
    private String firstNonBlank(String... values) {
        if (values == null) {
            return null;
        }
        for (String v : values) {
            if (!StringUtils.isNullOrEmpty(v)) {
                return v;
            }
        }
        return null;
    }
 
    private double parseDouble(String val, double defaultVal) {
        try {
            return StringUtils.isNullOrEmpty(val) ? defaultVal : Double.parseDouble(val);
        } catch (NumberFormatException e) {
            return defaultVal;
        }
    }
}