OrderCountCheckAspect.java
14.7 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.lhcredit.framework.aspectj;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.lhcredit.common.utils.JsonUtils;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.framework.aspectj.lang.annotation.OrderCountCheck;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.project.business.bCreditReport.domain.BCreditReport;
import com.lhcredit.project.business.bCreditReport.service.BCreditReportServiceImpl;
import com.lhcredit.project.business.frontUser.domain.FrontUser;
import com.lhcredit.project.business.frontUser.domain.FrontUserMon;
import com.lhcredit.project.business.frontUser.domain.TempConf;
import com.lhcredit.project.business.frontUser.service.FrontUserServiceImpl;
import com.lhcredit.project.business.frontUser.service.TokenManager;
import com.lhcredit.project.business.templateConfigurationParameters.domain.TemplateConfigurationParameters;
import com.lhcredit.project.business.templateConfigurationParameters.service.TemplateConfigurationParametersServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Aspect
@Component
public class OrderCountCheckAspect {
@Autowired
private TokenManager tokenManager;
@Autowired
private BCreditReportServiceImpl bCreditReportService;
@Autowired
private FrontUserServiceImpl frontUserService;
@Autowired
private TemplateConfigurationParametersServiceImpl templateConfigurationParametersService;
@Pointcut("@annotation(com.lhcredit.framework.aspectj.lang.annotation.OrderCountCheck)")
public void targetAnnot(){}
@Around("targetAnnot()")
public Object checkOrderCount(ProceedingJoinPoint joinPoint) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("authorization");
//获取 目标方法参数
Object[] objects = joinPoint.getArgs();
BCreditReport bCreditReport = (BCreditReport)objects[0];
//设置报告类型参数 并 查询用户已经下单的 报告数量
/*ArrayList<String> typeList = new ArrayList<>();
typeList.add(bCreditReport.getReportType());
List<BCreditReport> reportCountlist = bCreditReportService.getReportCountByOrg(userMon.getOrgId(), typeList);*/
//获取用户 报告上线值
/*List<TempConf> tempConfList = userMon.getTempConfList();
Map<String, TempConf> numMap = tempConfList.stream().collect(Collectors.toMap(TempConf::getValue, Function.identity()));
TempConf tempConf = numMap.get(bCreditReport.getReportType());*/
//定义用户模板
TempConf tempConf;
//获取目标方法的 注解值
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
OrderCountCheck countCheck = signature.getMethod().getAnnotation(OrderCountCheck.class);
Integer totalReport = -1;
//根据注解值 判断 对应的目标方法
switch (countCheck.reportType()){
//后台批量下单
case "backgroundBatchOrder":
MultipartFile file = bCreditReport.getFile();
if (file.isEmpty()
|| (!file.getOriginalFilename().endsWith("xls") && !file.getOriginalFilename().endsWith("xlsx"))){
return AjaxResult.error("参数有误");
}
//根据userId查询用户配置模板信息
tempConf = getBackgroundUserTempConf(bCreditReport);
if (tempConf == null || StringUtils.isEmpty(tempConf.getNum()))
return AjaxResult.error("用户无此权限");
/*FrontUser user = frontUserService.getFrontUserById(bCreditReport.getUserId());
//reports 是为用户配置的报告上限数据,为空则暂时不拦截
if (StringUtils.isEmpty(user.getReports()))
return joinPoint.proceed();
List<TempConf> tempConfList = JsonUtils.fromJson(user.getReports(), new TypeReference<List<TempConf>>() {});
TempConf backgroundTemConf = getTempConf(tempConfList, bCreditReport);
if (backgroundTemConf == null){
// return AjaxResult.error("用户无此模板");
return joinPoint.proceed();
}*/
//限制数量 为 -1则 不做校验
if (Integer.parseInt(tempConf.getNum()) < 0)
return joinPoint.proceed();
//查询用户的下单数量
List<BCreditReport> reportCountlist = getbCreditReports(bCreditReport);
//定义用户下单的个数
Integer orderedNum;
if (CollectionUtils.isEmpty(reportCountlist)){
orderedNum = 0;
}else {
orderedNum = reportCountlist.get(0).getReportCount();
}
//获取 批量下单的企业数量
ExcelReader reader = ExcelUtil.getReader(bCreditReport.getFile().getInputStream());
List<Map<String,Object>> fileInfo = reader.readAll();
//计算 用户报告数量总和 = 当前单下单的报告数量 + 已下单的报告数量
totalReport = fileInfo.size() + orderedNum;
//用户的报告数量上限
if (totalReport > Integer.parseInt("".equals(tempConf.getNum()) ? "0" : tempConf.getNum()))
return AjaxResult.error(tempConf.getName() + "下单次数已达上限");
return joinPoint.proceed();
//后台 单个下单
case "backgroundOrder" :
//获取用户的报告上限数量
tempConf = getBackgroundUserTempConf(bCreditReport);
if (tempConf == null || StringUtils.isEmpty(tempConf.getNum()))
return AjaxResult.error("用户无此权限");
/*FrontUser frontUser = frontUserService.getFrontUserById(bCreditReport.getUserId());
//reports 是为用户配置的报告上限数据,为空则暂时不拦截
if (StringUtils.isEmpty(frontUser.getReports()))
return joinPoint.proceed();
List<TempConf> backTempConfList = JsonUtils.fromJson(frontUser.getReports(), new TypeReference<List<TempConf>>() {});
TempConf backgroudTemConfOne = getTempConf(backTempConfList, bCreditReport);
if (backgroudTemConfOne == null){
// return AjaxResult.error("用户无此模板");
return joinPoint.proceed();
}*/
//限制数量 为 -1则 不做校验
if (Integer.parseInt(tempConf.getNum()) < 0)
return joinPoint.proceed();
//查询 目前已经下单的数量
List<BCreditReport> reports1 = getbCreditReports(bCreditReport);
//定义用户下单的个数
Integer numOrder;
if (CollectionUtils.isEmpty(reports1)){
numOrder = 0;
}else {
numOrder = reports1.get(0).getReportCount();
}
//计算 用户已经下的单量
Integer totalNum = numOrder + 1;
//用户的报告数量上限
if (totalNum > Integer.parseInt("".equals(tempConf.getNum()) ? "0" : tempConf.getNum()))
return AjaxResult.error(tempConf.getName() + "下单次数已达上限");
return joinPoint.proceed();
//前台 单条/批量下单
case "frontBatchOrder" :
//获取用户的报告上限数量 (前台用户登录才可下单,所以user肯定存在)
FrontUserMon userMon = tokenManager.getUserInfoByToken(token);
List<TempConf> tempConfFrontList = userMon.getTempConfList();
// 未配置 报告模板下单上限,直接返回
if (CollectionUtils.isEmpty(tempConfFrontList))
return AjaxResult.error("用户无此权限");
tempConf = getTempConf(tempConfFrontList, bCreditReport);
if (tempConf == null || StringUtils.isEmpty(tempConf.getNum())){
return AjaxResult.error("用户无此权限");
}
//限制数量 为 -1则 不做校验
if (Integer.parseInt(tempConf.getNum()) < 0)
return joinPoint.proceed();
//查询用户的下单数量
List<BCreditReport> bCreditReports = getbCreditReports(bCreditReport,userMon);
//定义用户下单的个数
Integer frontOrderedNum;
if (CollectionUtils.isEmpty(bCreditReports)){
frontOrderedNum = 0;
}else {
frontOrderedNum = bCreditReports.get(0).getReportCount();
}
//获取 批量下单的企业数量 与 已下单的报告数量 之和
Integer frontOrderCount = bCreditReport.getEnterpriseList().size() + frontOrderedNum;
//用户的报告数量上限
if (frontOrderCount > Integer.parseInt("".equals(tempConf.getNum()) ? "0" : tempConf.getNum()))
return AjaxResult.error(tempConf.getName() + "下单次数已达上限");
return joinPoint.proceed();
//单条 海外下单
case "overSeaOrder" :
//获取用户的报告上限数量 (前台用户登录才可下单,所以user肯定存在)
FrontUserMon userMon2 = tokenManager.getUserInfoByToken(token);
List<TempConf> overSeaConfFrontList = userMon2.getTempConfList();
// 未配置 报告模板下单上限,直接返回
if (CollectionUtils.isEmpty(overSeaConfFrontList))
return AjaxResult.error("用户无此权限");
tempConf = getTempConf(overSeaConfFrontList, bCreditReport);
if (tempConf == null || StringUtils.isEmpty(tempConf.getNum())){
return AjaxResult.error("用户无此权限");
}
//限制数量 为 -1则 不做校验
if (Integer.parseInt(tempConf.getNum()) < 0)
return joinPoint.proceed();
//查询 目前已经下单的数量
List<BCreditReport> reports2 = getbCreditReports(bCreditReport, userMon2);
//定义用户下单的个数
Integer numOrderO;
if (CollectionUtils.isEmpty(reports2)){
numOrderO = 0;
}else {
numOrderO = reports2.get(0).getReportCount();
}
//计算 用户已经下的单量
Integer totalNumO = numOrderO + 1;
//用户的报告数量上限
if (totalNumO > Integer.parseInt("".equals(tempConf.getNum()) ? "0" : tempConf.getNum()))
return AjaxResult.error(tempConf.getName() + "下单次数已达上限");
return joinPoint.proceed();
default:
return AjaxResult.error("无此用户信息");
}
}
/**
* 后台用户 -- 获取配置模板
* @param bCreditReport
* @return
*/
private TempConf getBackgroundUserTempConf(BCreditReport bCreditReport){
//获取用户的报告上限数量
FrontUser frontUser = frontUserService.getFrontUserById(bCreditReport.getUserId());
//reports 是为用户配置的报告上限数据,为空则暂时不拦截
if (frontUser == null || StringUtils.isEmpty(frontUser.getReports()))
return null;
List<TempConf> backTempConfList = JsonUtils.fromJson(frontUser.getReports(), new TypeReference<List<TempConf>>() {});
return getTempConf(backTempConfList, bCreditReport);
}
/**
* 根据报告类型 和 orgId 查询用户的下单数量
* @param bCreditReport
* @return
*/
private List<BCreditReport> getbCreditReports(BCreditReport bCreditReport) {
ArrayList<String> typeList = new ArrayList<>();
typeList.add(bCreditReport.getReportType());
List<BCreditReport> reportCountlist = bCreditReportService.getReportCountByUserId(bCreditReport.getUserId(), typeList);
return reportCountlist;
}
private List<BCreditReport> getbCreditReports(BCreditReport bCreditReport,FrontUserMon userMon) {
ArrayList<String> typeList = new ArrayList<>();
typeList.add(bCreditReport.getReportType());
List<BCreditReport> reportCountlist = bCreditReportService.getReportCountByUserId(userMon.getId(), typeList);
return reportCountlist;
}
/**
* 获取用户报告类型的上限
* @param tempConfList
* @param bCreditReport
* @return
*/
private TempConf getTempConf(List<TempConf> tempConfList, BCreditReport bCreditReport) {
//去掉 tempType =3 的数据
Map<String, TempConf> tempConfMap = tempConfList.stream().filter(te -> !te.getTempType().equals(3)).collect(Collectors.toMap(TempConf::getValue, Function.identity()));
TempConf backgroundTemConf = tempConfMap.get(bCreditReport.getReportType());
//报告类型为 -11时,为定制报告,需要单独查询clientID 区分报告模板
if (bCreditReport.getReportType().equals("-11")){
TemplateConfigurationParameters templateParam = templateConfigurationParametersService.selectTemplateConfigurationParametersById(bCreditReport.getTemplateParamId());
for (TempConf tef : tempConfList) {
//定制模板
if (tef.getTempType() == 2 && tef.getValue().equals(templateParam.getClientId().toString())){
return tef;
}
}
}
return backgroundTemConf;
}
}