CountReportAspect.java
10.6 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
package com.lhcredit.framework.aspectj;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lhcredit.common.utils.ServletUtils;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.common.utils.http.HttpUtils;
import com.lhcredit.framework.aspectj.lang.annotation.ReportCount;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.project.business.apiFinance.service.IApiFinanceService;
import com.lhcredit.project.business.bCreditReport.service.BCreditReportServiceImpl;
import com.lhcredit.project.business.financeInfo.service.FinanceInfoServiceImpl;
import com.lhcredit.project.business.frontDept.domain.FrontDept;
import com.lhcredit.project.business.frontDept.service.FrontDeptServiceImpl;
import com.lhcredit.project.business.frontUser.domain.FrontUserMon;
import com.lhcredit.project.business.frontUser.domain.TempConf;
import com.lhcredit.project.business.frontUser.service.TokenManager;
import com.lhcredit.project.business.monitorCompany.service.MonitorCompanyServiceImpl;
import com.lhcredit.project.business.statisticsFinancePageselect.service.IStatisticsFinancePageselectService;
import com.lhcredit.project.business.userDownloadCountLog.domain.UserDownloadCountLog;
import com.lhcredit.project.business.userDownloadCountLog.service.UserDownloadCountLogServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
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.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
@Aspect
@Component
public class CountReportAspect {
@Autowired
private TokenManager tokenManager;
@Autowired
private FrontDeptServiceImpl frontDeptService;
@Autowired
private IApiFinanceService apiFinanceService;
@Autowired
private MonitorCompanyServiceImpl monitorCompanyService;
@Autowired
private FinanceInfoServiceImpl financeInfoService;
@Autowired
private IStatisticsFinancePageselectService financePageselectService;
@Autowired
private UserDownloadCountLogServiceImpl userDownloadCountLogService;
@Pointcut("@annotation(com.lhcredit.framework.aspectj.lang.annotation.ReportCount)")
public void countReportMethods() {}
@Around("countReportMethods()")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
//获取注解
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
ReportCount reportCount = method.getAnnotation(ReportCount.class);
//注解的 reportType 无值 则直接返回
if ("".equals(reportCount.reportType())) {
return AjaxResult.error("reportType属性不能为空");
}
//获取用户得总公司部门
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("authorization");
FrontUserMon user = tokenManager.getUserInfoByToken(token);
if (user == null || CollectionUtils.isEmpty(user.getTempConfList())){
setErrorRespons("用户暂无此项权限");
return null;
// return AjaxResult.error("用户暂无此项权限");
}
Map<String, TempConf> tempConfMap = user.getTempConfList().stream().filter(te -> te.getTempType().equals(3)).collect(Collectors.toMap(TempConf::getValue, Function.identity()));
if (MapUtils.isEmpty(tempConfMap)){
setErrorRespons("用户暂无此项权限");
return null;
// return AjaxResult.error("用户暂无此权限");
}
// 用户配置的目标对象
TempConf tempConf ;
//根据 headId 查询 各种报告的限查询数量
// FrontDept dept = frontDeptService.getFrontDeptById(Long.parseLong(user.getHeadId()));
// FrontDept dept = frontDeptService.getFrontDeptById(23l);
switch (reportCount.reportType()) {
//信用报告
// case "creditReport" :
// //查询信用报告的次数:
// Long credtiReportCount = bCreditReportService.getCreditSearchCountByOrgId(user.getHeadId());
//// Long credtiReportCount = bCreditReportService.getCreditSearchCountByOrgId("23");
// int reportNum = dept.getReportNum();
// if (credtiReportCount > reportNum) {
// return AjaxResult.error("报告查询次数已达上限");
// }else {
// return joinPoint.proceed();
// }
//监控企业
case "monitorCompany" :
// Long monitorCompanyCount = monitorCompanyService.getMonitorCompanySearchCount("23");
tempConf = tempConfMap.get("0");
if (Objects.isNull(tempConf) || StringUtils.isEmpty(tempConf.getNum())){
return AjaxResult.error("用户暂无此项权限");
}
int monitorNum = Integer.parseInt(tempConf.getNum());
// -1 不校验
if(monitorNum == -1){
return joinPoint.proceed();
}
Long monitorCompanyCount = monitorCompanyService.getMonitorCompanySearchCount(user.getId().toString());
if (monitorCompanyCount >= monitorNum) {
return AjaxResult.error("公司监控查询次数已达上限");
}
return joinPoint.proceed();
//财务次数监控
case "financeInfo" :
// Long financeInfoCount = financeInfoService.getFinanceInfoSearchCount("23");
tempConf = tempConfMap.get("1");
if (Objects.isNull(tempConf) || StringUtils.isEmpty(tempConf.getNum())){
return AjaxResult.error("用户暂无此项权限");
}
// -1 不校验
int financeNum = Integer.parseInt(tempConf.getNum());
if(financeNum == -1){
return joinPoint.proceed();
}
Long financeInfoCount = financeInfoService.getFinanceInfoSearchCount(user.getId().toString());
Integer countval23=financePageselectService.selectCountByOrgId(user.getId().toString());
if (null!=countval23)financeInfoCount=financeInfoCount+countval23;
if (financeInfoCount >= financeNum) {
return AjaxResult.error("财务信息查询次数已达上限");
}
return joinPoint.proceed();
//司法导出数据
case "sFAssist":
tempConf = tempConfMap.get("2");
if (Objects.isNull(tempConf) || StringUtils.isEmpty(tempConf.getNum())){
// return AjaxResult.error("用户暂无此项权限");
setErrorRespons("用户暂无此权限");
return null;
}
int sFAssistNum = Integer.parseInt(tempConf.getNum());
// -1 不做校验
if (sFAssistNum == -1){
return joinPoint.proceed();
}
//查询用户下载司法数据次数
UserDownloadCountLog userSFAssitDataNum = userDownloadCountLogService.getUserSFAssitDataNum(user.getId());
//没查到: 一次没下载过
if (userSFAssitDataNum == null)
return joinPoint.proceed();
//超过限制返回 错误信息
if (userSFAssitDataNum.getSfAssistNum() > sFAssistNum){
//获取 response
/*HttpServletResponse response = null;
Object[] objects = joinPoint.getArgs();
for (Object arg : objects){
if (arg instanceof HttpServletResponse){
response = (HttpServletResponse)arg;
break;
}
}
//目标方法为void 手动修改返回值
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
//序列化 AjaxResult对象为json,并写入response流
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(response.getWriter(),AjaxResult.error("司法数据下载次数已达到上限"));*/
setErrorRespons("司法数据下载次数已达到上限");
return null;
// return AjaxResult.error("司法数据下载次数已达到上限");
}
return joinPoint.proceed();
//税务信息
case "taxInfo":
tempConf = tempConfMap.get("3");
if (Objects.isNull(tempConf) || StringUtils.isEmpty(tempConf.getNum())){
return AjaxResult.error("用户暂无此项权限");
}
int taxCount = Integer.parseInt(tempConf.getNum());
// -1 不校验
if(taxCount == -1){
return joinPoint.proceed();
}
//查询用户实际开票条数
Long taxNum = apiFinanceService.getFinanceCountByUser(user.getId().toString());
if (taxNum > taxCount)
return AjaxResult.error("用户开票次数以达到上限");
return joinPoint.proceed();
default:
return null;
}
}
/**
* response 设置返回错误信息
* @param msg
* @throws Exception
*/
private void setErrorRespons(String msg) throws Exception{
HttpServletResponse response = ServletUtils.getResponse();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(response.getWriter(),AjaxResult.error(msg));
}
}