优化代码 public List<Boolean> machineAuditCallbackBusiness(List<MachineAuditCallbackRequest> request) { log.info("信息 请求参数 {}", JSON.toJSON(request)); List<Boolean> result = new ArrayList<>(); request.forEach(temp -> result.add(machineAuditCallbackBusiness(temp)) ); return result; }
时间: 2023-05-16 21:05:19 浏览: 84
这段代码可以进行进一步的优化,可以使用 Java 8 的 Stream API 来简化代码,如下所示:
public List<Boolean> machineAuditCallbackBusiness(List<MachineAuditCallbackRequest> request) {
log.info("信息 请求参数 {}", JSON.toJSON(request));
return request.stream().map(this::machineAuditCallbackBusiness).collect(Collectors.toList());
}
相关问题
@PostMapping("/appUpdate") @ApiOperation(value = "获取版本更新") public Response<AppUpdateResponseVO> appUpdate(@RequestBody @Valid Request<AppUpdateRequestVO> request) { AppUpdateRequestDTO appUpdateRequestDto = WalletVoMapper.INSTANCE.toDto(request.getParam()); // todo 因为 app 版本定义错误现在应该是1.xx.xx 变为了 3.xx.xx 需要将3开头的改为1开头, 如果版本更新到了3.xx.xx 需要将代码删掉 String appVersion = request.getAppVersion(); if (request.getAppVersion() != null && request.getAppVersion().startsWith("3") && Boolean.TRUE.toString().equalsIgnoreCase(SpringUtil.getProperty("app-update-fix-switch","true"))) { appVersion = StringUtils.overlay(appVersion, "1", 0, 1); } AppUpdateResponseDTO appUpdateByVersion = appUpdateService.getAppUpdateByVersion(appUpdateRequestDto, appVersion, request.getAppType(), request.getPlatform(), request.getClientIp()); AppUpdateResponseVO appUpdateResponseVo = WalletVoMapper.INSTANCE.toVo(appUpdateByVersion); PlatformEnum platform = request.getPlatform() == PlatformEnum.WEB_H5 ? request.getParam().getPlatform() : request.getPlatform(); boolean downloadUrlStatus = SpringUtil.getProperty("s3.download.status", boolean.class, true); if (downloadUrlStatus) { String downloadUrlS3 = SpringUtil.getProperty(MessageFormat.format("{0}.s3.download.url", platform.name().toLowerCase())); log.info("appUpdate downloadUrlS3 {}", downloadUrlS3); appUpdateResponseVo.setDownloadUrl(downloadUrlS3); appUpdateResponseVo.setDownloadUrls(List.of(downloadUrlS3)); } if (appUpdateResponseVo != null && appUpdateResponseVo.getVersion() != null && appUpdateResponseVo.getVersion().startsWith("3") && Boolean.TRUE.toString().equalsIgnoreCase(SpringUtil.getProperty("app-update-fix-switch","true"))) { log.info("appUpdate response {}", appUpdateResponseVo); appUpdateResponseVo = new AppUpdateResponseVO(); } if(log.isInfoEnabled()){ log.info("appUpdate request {} response {}", JacksonUtils.toJson(request),JacksonUtils.toJson(appUpdateResponseVo)); } return Response.succeed(appUpdateResponseVo); }在这段代码中加入一个下载次数限制每天下载2000次 用代码展示
可以在该方法中增加一个计数器,表示当天已下载的次数,每次请求时进行判断,如果已下载次数小于2000,则进行下载,并将计数器加1,否则返回下载次数已达上限的提示信息。具体代码如下:
```
private static final int MAX_DOWNLOAD_COUNT = 2000;
private static Map<String, Integer> downloadCountMap = new ConcurrentHashMap<>();
@PostMapping("/appUpdate")
@ApiOperation(value = "获取版本更新")
public Response<AppUpdateResponseVO> appUpdate(@RequestBody @Valid Request<AppUpdateRequestVO> request) {
AppUpdateRequestDTO appUpdateRequestDto = WalletVoMapper.INSTANCE.toDto(request.getParam());
// todo 因为 app 版本定义错误现在应该是1.xx.xx 变为了 3.xx.xx 需要将3开头的改为1开头, 如果版本更新到了3.xx.xx 需要将代码删掉
String appVersion = request.getAppVersion();
if (request.getAppVersion() != null && request.getAppVersion().startsWith("3")
&& Boolean.TRUE.toString().equalsIgnoreCase(SpringUtil.getProperty("app-update-fix-switch", "true"))) {
appVersion = StringUtils.overlay(appVersion, "1", 0, 1);
}
AppUpdateResponseDTO appUpdateByVersion = appUpdateService.getAppUpdateByVersion(appUpdateRequestDto, appVersion, request.getAppType(), request.getPlatform(), request.getClientIp());
AppUpdateResponseVO appUpdateResponseVo = WalletVoMapper.INSTANCE.toVo(appUpdateByVersion);
PlatformEnum platform = request.getPlatform() == PlatformEnum.WEB_H5 ? request.getParam().getPlatform() : request.getPlatform();
boolean downloadUrlStatus = SpringUtil.getProperty("s3.download.status", boolean.class, true);
if (downloadUrlStatus) {
String downloadUrlS3 = SpringUtil.getProperty(MessageFormat.format("{0}.s3.download.url", platform.name().toLowerCase()));
log.info("appUpdate downloadUrlS3 {}", downloadUrlS3);
appUpdateResponseVo.setDownloadUrl(downloadUrlS3);
appUpdateResponseVo.setDownloadUrls(List.of(downloadUrlS3));
}
if (appUpdateResponseVo != null && appUpdateResponseVo.getVersion() != null && appUpdateResponseVo.getVersion().startsWith("3")
&& Boolean.TRUE.toString().equalsIgnoreCase(SpringUtil.getProperty("app-update-fix-switch", "true"))) {
log.info("appUpdate response {}", appUpdateResponseVo);
appUpdateResponseVo = new AppUpdateResponseVO();
}
if (log.isInfoEnabled()) {
log.info("appUpdate request {} response {}", JacksonUtils.toJson(request), JacksonUtils.toJson(appUpdateResponseVo));
}
// 判断下载次数是否超过限制
String appVersionKey = request.getAppVersion() + "_" + platform.name();
int downloadCount = downloadCountMap.getOrDefault(appVersionKey, 0);
if (downloadCount < MAX_DOWNLOAD_COUNT) {
downloadCountMap.put(appVersionKey, downloadCount + 1);
} else {
return Response.fail("当前版本下载次数已达上限");
}
return Response.succeed(appUpdateResponseVo);
}
```
阅读全文