1
zj
2024-10-21 a54e43c6d57c82660d46b24b0720175314960d78
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package project.monitor.loan.internal;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
import org.springframework.cglib.beans.BeanMap;
import org.springframework.jdbc.core.JdbcTemplate;
 
import kernel.exception.BusinessException;
import kernel.web.ApplicationUtil;
import kernel.web.Page;
import project.monitor.loan.LoanParam;
import project.monitor.loan.LoanService;
import project.monitor.loan.SimpleLoanOrder;
import project.party.model.Party;
 
/**
 * @author JORGE
 * @description 借贷服务接口实现类
 */
@SuppressWarnings("unchecked")
public class LoanServiceImpl implements LoanService{
    /**
     * 借贷状态字典
     */
    private static HashMap<Integer,String> stateMap;
    
    /**
     * 付款方式字典
     */
    private static HashMap<Integer,String> repayments;
    
    /**
     * 逗号正则表达式
     */
    private static Pattern commaPattern=Pattern.compile(",");
    
    /**
     * 冒号正则表达式
     */
    private static Pattern colonPattern=Pattern.compile(":");
    
    /**
     * 放款机构字典
     */
    private static HashMap<Integer,String> lendingInstitutions;
    
    public LoanServiceImpl() {
        stateMap=new HashMap<Integer,String>();
        repayments=new HashMap<Integer,String>();
        lendingInstitutions=new HashMap<Integer,String>();
        
        stateMap.put(1, "未审");
        stateMap.put(2, "通过");
        stateMap.put(3, "驳回");
        
        repayments.put(1,"到期还本息");
        repayments.put(2,"到期还本金");
        repayments.put(3,"到期还利息");
        
        lendingInstitutions.put(1,"LOAN1");
        lendingInstitutions.put(2,"LOAN2");
        lendingInstitutions.put(3,"LOAN3");
    }
 
    @Override
    public Boolean addLoanOrder(SimpleLoanOrder simpleLoanOrder) {
        Object primaryKey=ApplicationUtil.executeInsert(simpleLoanOrder);
        return null!=primaryKey;
    }
 
    @Override
    public HashMap<String,Object> getLoanParams(String uuid) {
        LoanParam loanParam=null;
        if(null==uuid || (uuid=uuid.trim()).isEmpty()) {
            List<LoanParam> loanParamList=ApplicationUtil.executeSelect(LoanParam.class);
            loanParam=(null==loanParamList || loanParamList.isEmpty())?null:loanParamList.get(0);
        }else {
            loanParam=ApplicationUtil.executeGet(uuid, LoanParam.class);
        }
        
        if(null==loanParam) return null;
        
        HashMap<String,Object> paramMap=new HashMap<String,Object>(BeanMap.create(loanParam));
        String term=(String)paramMap.get("term");
        if(null==term || (term=term.trim()).isEmpty()) {
            paramMap.put("termList",new Object[0]);
        }else {
            paramMap.put("termList",commaPattern.splitAsStream(term).map(tr->new String[] {tr,tr}).collect(Collectors.toList()));
        }
        
        String repayCycle=(String)paramMap.get("repayCycle");
        if(null==repayCycle || (repayCycle=repayCycle.trim()).isEmpty()) {
            paramMap.put("repayCycleList",new Object[0]);
        }else {
            paramMap.put("repayCycleList",commaPattern.splitAsStream(repayCycle).map(rc->new String[] {rc,rc}).collect(Collectors.toList()));
        }
        
        String dailyRate=(String)paramMap.get("dailyRate");
        if(null==dailyRate || (dailyRate=dailyRate.trim()).isEmpty()) {
            paramMap.put("dailyRateList",new Object[0]);
        }else {
            paramMap.put("dailyRateList",commaPattern.splitAsStream(dailyRate).map(dr->new String[] {dr,dr}).collect(Collectors.toList()));
        }
        
        String repayment=(String)paramMap.get("repayment");
        if(null==repayment || (repayment=repayment.trim()).isEmpty()) {
            paramMap.put("repaymentList",new Object[0]);
        }else {
            paramMap.put("repaymentList",commaPattern.splitAsStream(repayment).map(rep->colonPattern.split(rep)).collect(Collectors.toList()));
        }
        
        String lendingInstitution=(String)paramMap.get("lendingInstitution");
        if(null==lendingInstitution || (lendingInstitution=lendingInstitution.trim()).isEmpty()) {
            paramMap.put("lendingList",new Object[0]);
        }else {
            paramMap.put("lendingList",commaPattern.splitAsStream(lendingInstitution).map(led->colonPattern.split(led)).collect(Collectors.toList()));
        }
        
        paramMap.put("stateList",commaPattern.splitAsStream("1:未审,2:通过,3:驳回").map(stat->colonPattern.split(stat)).collect(Collectors.toList()));
        
        return paramMap;
    }
 
    @Override
    public Page pagedQuery(Map<String, Object> queryParams) {
        int pageSize=(Integer)queryParams.get("pageSize");
        int pageNo=(Integer)queryParams.get("pageNo");
        if (pageNo <= 0) pageNo = 1;
        
        Page page = new Page(pageNo, pageSize, Integer.MAX_VALUE);
        String userName=(String)queryParams.get("userName");
        String orderNo=(String)queryParams.get("orderNo");
        int status=(Integer)queryParams.get("status");
        
        ArrayList<Object> paramList=new ArrayList<Object>();
        StringBuilder whereStatement=new StringBuilder("WHERE 1=1 ");
        
        if(0!=status) {
            whereStatement.append("AND STATE=? ");
            paramList.add(status);
        }
        
        if(null!=orderNo && !(orderNo=orderNo.trim()).isEmpty()) {
            whereStatement.append("AND UUID=? ");
            paramList.add(orderNo);
        }
        
        if(null!=userName && !(userName=userName.trim()).isEmpty()) {
            whereStatement.append("AND PARTY_ID=(SELECT UUID FROM PAT_PARTY WHERE USERNAME=?) ");
            paramList.add(userName);
        }
        
        whereStatement.append("LIMIT ?,?");
        paramList.add(page.getFirstElementNumber());
        paramList.add(pageSize);
        
        List<SimpleLoanOrder> orderList=ApplicationUtil.executeSelect(SimpleLoanOrder.class,whereStatement.toString(),paramList.toArray(new Object[paramList.size()]));
        if(null==orderList || orderList.isEmpty()) return page;
        
        String partyIds=orderList.stream().map(order->order.getPartyId()).collect(Collectors.joining(","));
        List<Party> partyList=ApplicationUtil.executeSelect(Party.class,"WHERE FIND_IN_SET(UUID,?)",new Object[] {partyIds});
        Map<?,?> idToNameMap=partyList.stream().collect(Collectors.toMap(party->party.getId(),party->party.getUsername(),(oldname,newname)->newname));
        
        List<HashMap<String,Object>> transferList=orderList.stream().map(order->{
            Integer state=order.getState();
            Integer rep=order.getRepayment();
            String houseImgs=order.getHouseImgs();
            Integer led=order.getLendingInstitution();
            houseImgs=null==houseImgs?null:(houseImgs=houseImgs.trim()).isEmpty()?null:houseImgs;
            HashMap<String,Object> transferMap=new HashMap<String,Object>(BeanMap.create(order));
            transferMap.put("state", new Object[] {state,stateMap.get(state)});
            transferMap.put("userName", idToNameMap.get(order.getPartyId()));
            transferMap.put("repayment", new Object[] {rep,repayments.get(rep)});
            transferMap.put("houseImgs", null==houseImgs?null:commaPattern.split(houseImgs)[0]);
            transferMap.put("lendingInstitution", new Object[] {led,lendingInstitutions.get(led)});
            return transferMap;
        }).collect(Collectors.toList());
        
        page.setElements(transferList);
        
        return page;
    }
 
    @Override
    public boolean deleteLoanOrder(String orderId) {
        if(null==orderId || (orderId=orderId.trim()).isEmpty()) {
            throw new BusinessException("申请单ID不能为空!");
        }
        
        int count=ApplicationUtil.executeDel(orderId,SimpleLoanOrder.class);
        if(1!=count) {
            throw new BusinessException("根据申请单ID删除了多条记录,说明申请单ID重复!");
        }
        
        return count==1?true:false;
    }
 
    @Override
    public boolean updateLoanOrderState(String orderId, String status) {
        if(null==orderId || (orderId=orderId.trim()).isEmpty()) {
            throw new BusinessException("申请单ID不能为空!");
        }
        
        if(null==status || (status=status.trim()).isEmpty()) {
            throw new BusinessException("审核状态不能为空!");
        }
        
        int state="true".equalsIgnoreCase(status)?2:3;
        int count=ApplicationUtil.getBean(JdbcTemplate.class).update("UPDATE T_SIMPLE_LOAN_ORDER SET state=? WHERE UUID=?",state,orderId);
        if(1!=count) {
            throw new BusinessException("根据申请单ID修改了多条记录,说明申请单ID重复!");
        }
        
        return true;
    }
 
    @Override
    public Boolean modLoanOrder(SimpleLoanOrder simpleLoanOrder) {
        Serializable id=simpleLoanOrder.getId();
        if(null==id) {
            throw new BusinessException("申请单ID不能为空!");
        }
        
        Set<String> excludeFields=new HashSet<String>();
        excludeFields.add("id");
        excludeFields.add("partyId");
        excludeFields.add("state");
        excludeFields.add("houseImgs");
        excludeFields.add("incomeImg");
        
        int count=ApplicationUtil.executeUpdate(simpleLoanOrder,excludeFields);
        if(1!=count) {
            throw new BusinessException("根据申请单ID修改了多条记录,说明申请单ID重复!");
        }
        
        return true;
    }
    
    @Override
    public SimpleLoanOrder getLoanOrder(String orderId) {
        if(null==orderId || (orderId=orderId.trim()).isEmpty()) {
            throw new BusinessException("申请单ID不能为空!");
        }
        return ApplicationUtil.executeGet(orderId,SimpleLoanOrder.class);
    }
}