API调用次数加上根据userID查

This commit is contained in:
majintao
2024-09-13 09:52:46 +08:00
parent 7cb237bf83
commit b363320ccb
@@ -134,55 +134,61 @@ public class UsersController {
return json.toString();
}
public Object surplusCallCountAction() throws IllegalAccessException {
public Object surplusCallCountAction(ApiUserModel model) throws IllegalAccessException {
RestfulModelAndView mav = new RestfulModelAndView();
ActionContext context = ActionContext.getContext();
HttpServletRequest request = context.getRequest();
AuthenticationBean instance = (AuthenticationBean) request.getAttribute(AuthenticationBean.ATTR_AUTHENTICATION_BEAN);
User user = null;
if (model.getUserId() != null && model.getUserId() > 0) {
user = userService.findById(model.getUserId());
}
if (null != instance) {
User user = instance.user;
List<OpenResource> openResourceList = openResourceService.findAll();
List<ApiMonitorInfo> list = new ArrayList<ApiMonitorInfo>();
long id = user.getId();
ApiUserAuthority authority = apiUserAuthorityService.findByUserId(id);
if (null != authority) {
// 用户有的uri
List<OpenResource> userResources = new ArrayList<OpenResource>();
for (OpenResource openResource : openResourceList) {
if (authority.getOpenResourseUris().contains(openResource.getUri())) {
userResources.add(openResource);
}
}
Map<String, Integer> countMap = authority.getTotalCountMap();
if (null != countMap) {
// 改成遍历用户有的uri
for (OpenResource openResource : userResources) {
String uri = openResource.getUri();
ApiMonitorInfo info = new ApiMonitorInfo();
info.setApiUri(uri);
info.setApiName(openResource.getName());
info.setUserId((int) id);
info.setUsername(user.getUserName());
if (null != countMap.get(uri) && countMap.get(uri) > 0) {
long curVistedCount = apiVisitCountService.findApiVisitCountByDay(id, uri, DateUtil.truncateDay(new Date()));
long historyCount = apiVisitCountService.findApiVisitAllCount(id, uri, DateUtil.truncateDay(new Date()));
Integer totalCount = countMap.get(uri);
info.setTotalCallCount(Long.valueOf(totalCount));
long usedCount = curVistedCount + historyCount;
info.setSurplusCallCount(totalCount - usedCount);
info.setUsedCallCount(usedCount);
}
list.add(info);
}
user = instance.user;
}
if (null == instance && user == null) {
mav.setModel(new BasicModel(HttpStatusCodeEnum.BAD_REQUEST_ERROR_KEY));
return mav;
}
List<OpenResource> openResourceList = openResourceService.findAll();
List<ApiMonitorInfo> list = new ArrayList<ApiMonitorInfo>();
long id = user.getId();
ApiUserAuthority authority = apiUserAuthorityService.findByUserId(id);
if (null != authority) {
// 用户有的uri
List<OpenResource> userResources = new ArrayList<OpenResource>();
for (OpenResource openResource : openResourceList) {
if (authority.getOpenResourseUris().contains(openResource.getUri())) {
userResources.add(openResource);
}
}
Map<String, Integer> countMap = authority.getTotalCountMap();
if (null != countMap) {
// 改成遍历用户有的uri
for (OpenResource openResource : userResources) {
String uri = openResource.getUri();
ApiMonitorInfo info = new ApiMonitorInfo();
info.setApiUri(uri);
info.setApiName(openResource.getName());
info.setUserId((int) id);
info.setUsername(user.getUserName());
if (null != countMap.get(uri) && countMap.get(uri) > 0) {
long curVistedCount = apiVisitCountService.findApiVisitCountByDay(id, uri, DateUtil.truncateDay(new Date()));
long historyCount = apiVisitCountService.findApiVisitAllCount(id, uri, DateUtil.truncateDay(new Date()));
Integer totalCount = countMap.get(uri);
info.setTotalCallCount(Long.valueOf(totalCount));
long usedCount = curVistedCount + historyCount;
info.setSurplusCallCount(totalCount - usedCount);
info.setUsedCallCount(usedCount);
}
list.add(info);
}
}
BasicModel basicModel = new BasicModel(200, "ok");
basicModel.setResult(list);
mav.setModel(basicModel);
} else {
mav.setModel(new BasicModel(HttpStatusCodeEnum.BAD_REQUEST_ERROR_KEY));
}
BasicModel basicModel = new BasicModel(200, "ok");
basicModel.setResult(list);
mav.setModel(basicModel);
return mav;
}