diff --git a/zc-business/src/main/java/com/zc/business/controller/DeviceStatus.java b/zc-business/src/main/java/com/zc/business/controller/DeviceStatus.java index 44d9df34..85e57e8a 100644 --- a/zc-business/src/main/java/com/zc/business/controller/DeviceStatus.java +++ b/zc-business/src/main/java/com/zc/business/controller/DeviceStatus.java @@ -15,6 +15,7 @@ import com.zc.business.service.impl.StatusService; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.BufferedReader; @@ -94,6 +95,15 @@ public class DeviceStatus { executor.shutdown(); } + @Scheduled(cron = "0 30 23 * * ?") + //@Scheduled(cron = "0 30 * * * ?") + public void periodicDataClearing() { + StatusService statusService = SpringUtils.getBean(StatusService.class); + LocalDateTime startTime = LocalDateTime.now().minusMonths(UniversalEnum.THREE.getNumber()); + Status status = new Status(); + status.setStartTime(startTime); + statusService.delStatusByTime(status); + } private void extracted(StatusService statusService, DcDevice device, boolean reachable, String packetLossRate) { Status status = new Status(); status.setDeviceNo(device.getStakeMark()); diff --git a/zc-business/src/main/java/com/zc/business/controller/FTPDeletion.java b/zc-business/src/main/java/com/zc/business/controller/FTPDeletion.java index b828ba4a..e07e8af8 100644 --- a/zc-business/src/main/java/com/zc/business/controller/FTPDeletion.java +++ b/zc-business/src/main/java/com/zc/business/controller/FTPDeletion.java @@ -1,10 +1,17 @@ package com.zc.business.controller; +import com.ruoyi.common.core.redis.RedisCache; +import com.ruoyi.common.utils.spring.SpringUtils; +import com.ruoyi.system.service.ISysConfigService; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.IOException; import java.time.Instant; +import java.time.LocalDate; +import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; @@ -13,11 +20,13 @@ import org.apache.commons.net.ftp.FTPFile; * */ -@Component("FTPDeletion") +@Component() @Slf4j public class FTPDeletion { + ISysConfigService configService = SpringUtils.getBean(ISysConfigService.class); - private static int retentionDays = 60; // 默认保存天数为60天 +/* + private static int retentionDays = 90; // 默认保存天数为60天 public void deleteEventFile() { log.info("定时任务执行,当前保存天气设置天数:"+retentionDays+"当前时间:"+java.time.LocalTime.now()); new FTPDeletion().remoteFileDeletion(); @@ -30,28 +39,51 @@ public class FTPDeletion { //获取当前保存天数 public int getRetentionDays() { return retentionDays; - } + }*/ // 递归方法来处理文件和目录的删除 private void deleteDirectoryRecursively(FTPClient ftpClient, String parentDirPath) throws IOException { + int retentionDays= Integer.parseInt(configService.selectConfigByKey("EVENT-TIME"));//密钥 + + ftpClient.setControlEncoding("GBK"); FTPFile[] files = ftpClient.listFiles(parentDirPath); //选择要保留的天数 Instant thirtyDaysAgo = Instant.now().minus(retentionDays, ChronoUnit.DAYS); - log.info("当前设置的文件保存天数为:"+FTPDeletion.retentionDays+"当前时间:"+java.time.LocalTime.now()); + + // 转换为本地日期 + LocalDate localDate = thirtyDaysAgo.atZone(ZoneOffset.UTC).toLocalDate(); + + // 获取 30 天前当天的零点时间 + Instant thirtyDaysAgoStartOfDay = localDate.atStartOfDay(ZoneOffset.UTC).toInstant(); + log.info("当前设置的文件保存天数为:"+retentionDays+"当前时间:"+java.time.LocalTime.now()); for (FTPFile file : files) { String filePath = parentDirPath + "/" + file.getName(); if (file.isDirectory()) { + System.out.println("地址"+filePath); // 如果是目录,则递归调用 + // 删除30天前的文件 + Instant lastModifiedTime = file.getTimestamp().toInstant(); + String[] split = filePath.split("交通事件"); + if (split.length>1){ + if (!lastModifiedTime.isBefore(thirtyDaysAgoStartOfDay)){ + System.out.println("跳回天====="+filePath); + continue; + } + } + if (filePath.contains("ATTACHFILE")){ + continue; + } deleteDirectoryRecursively(ftpClient, filePath); + } else { // 排除包含特定关键词的文件名 if (!file.getName().contains("事故")) { // 删除30天前的文件 Instant lastModifiedTime = file.getTimestamp().toInstant(); - if (lastModifiedTime.isBefore(thirtyDaysAgo)) { + if (lastModifiedTime.isBefore(thirtyDaysAgoStartOfDay)) { boolean deleted = ftpClient.deleteFile(filePath); if (deleted) { log.info("已删除文件:"+filePath); @@ -60,6 +92,10 @@ public class FTPDeletion { log.info("无法删除文件:"+filePath); } + }else { + System.out.println("跳出循环-------------------------------------"); + continue; + } } else { log.info("文件名包含关键词'事故',跳过删除: :"+file.getName()); @@ -81,13 +117,21 @@ public class FTPDeletion { } } } - public void remoteFileDeletion() { - String server = "192.168.3.1"; - int port = 21; - String user = "1911390090@qq.com"; - String password = "989878wxl"; + //10.0.111.12 + String server= configService.selectConfigByKey("FTP-IP");//密钥 + //String server = "192.168.3.1"; + + // int port = 21; + int port= Integer.parseInt(configService.selectConfigByKey("FTP-PORT")); + + //ftpuser + //String user = "1911390090@qq.com"; + String user= configService.selectConfigByKey("FTP-USER");//密钥 + //Dxc123!@# + String password= configService.selectConfigByKey("FPT-PASSWORD");//密钥 + //String password = "989878wxl"; try { FTPClient ftpClient = new FTPClient(); ftpClient.setControlEncoding("GBK"); diff --git a/zc-business/src/main/java/com/zc/business/controller/StatusController.java b/zc-business/src/main/java/com/zc/business/controller/StatusController.java index 9ab72129..c66dc55d 100644 --- a/zc-business/src/main/java/com/zc/business/controller/StatusController.java +++ b/zc-business/src/main/java/com/zc/business/controller/StatusController.java @@ -32,7 +32,7 @@ import java.util.*; import java.util.stream.Collectors; -@Api(tags="设备状态") +@Api(tags = "设备状态") @RestController @RequestMapping("/system/status") public class StatusController extends BaseController { @@ -50,14 +50,16 @@ public class StatusController extends BaseController { //设备列表 @ApiOperation("设备状态列表按时间和类型") - @GetMapping ("/tablist") - public TableDataInfo getTabList(Status status) - { - if (status.getDeviceName()!=null) { - status.setDeviceName(status.getDeviceName().replaceAll(UniversalEnum.BLANK_SPACE.getValue(),UniversalEnum.PLUS_SIGN.getValue())); + @GetMapping("/tablist") + public TableDataInfo getTabList(Status status) { + if (status.getDeviceName() != null) { + status.setDeviceName(status.getDeviceName().replaceAll(UniversalEnum.BLANK_SPACE.getValue(), UniversalEnum.PLUS_SIGN.getValue())); } startPage(); - status.setTypes(status.getType().split(UniversalEnum.COMMA.getValue())); + String type = status.getType(); + if (type != null) { + status.setTypes(type.split(UniversalEnum.COMMA.getValue())); + } List listStatus = statusService.listStatusByTypes(status); return getDataTable(listStatus); } @@ -69,12 +71,14 @@ public class StatusController extends BaseController { @ApiOperation("设备状态导出") @Log(title = "【设备状态导出】", businessType = BusinessType.EXPORT) @PostMapping("/export") - public void export(HttpServletResponse response, Status status) - { - if (status.getDeviceName()!=null) { - status.setDeviceName(status.getDeviceName().replaceAll(UniversalEnum.BLANK_SPACE.getValue(),UniversalEnum.PLUS_SIGN.getValue())); + public void export(HttpServletResponse response, Status status) { + if (status.getDeviceName() != null) { + status.setDeviceName(status.getDeviceName().replaceAll(UniversalEnum.BLANK_SPACE.getValue(), UniversalEnum.PLUS_SIGN.getValue())); + } + String type = status.getType(); + if (type != null) { + status.setTypes(type.split(UniversalEnum.COMMA.getValue())); } - status.setTypes(status.getType().split(UniversalEnum.COMMA.getValue())); List listStatus = statusService.export(status); ExcelUtil util = new ExcelUtil<>(Status.class); util.exportExcel(response, listStatus, UniversalEnum.DEVICE_STATUS_LIST.getValue()); @@ -84,26 +88,33 @@ public class StatusController extends BaseController { @ApiOperation("设备状态柱状图按时间和类型") @GetMapping("/list") public AjaxResult getStatusList(Status status) { - if (status.getDeviceName()!=null) { - status.setDeviceName(status.getDeviceName().replaceAll(UniversalEnum.BLANK_SPACE.getValue(),UniversalEnum.PLUS_SIGN.getValue())); + if (status.getDeviceName() != null) { + status.setDeviceName(status.getDeviceName().replaceAll(UniversalEnum.BLANK_SPACE.getValue(), UniversalEnum.PLUS_SIGN.getValue())); } LocalDateTime todayStart = status.getStartTime().truncatedTo(ChronoUnit.DAYS); LocalDateTime currentTime = status.getTime().truncatedTo(ChronoUnit.DAYS).plusDays(UniversalEnum.ONE.getNumber()); status.setStartTime(todayStart); status.setTime(currentTime); - String[] types = status.getType().split(UniversalEnum.COMMA.getValue()); - status.setTypes(types); + String statusType = status.getType(); + String[] types = null; + if (statusType != null) { + + types = statusType.split(UniversalEnum.COMMA.getValue()); + status.setTypes(types); + } Map> maps = new HashMap<>(); List statuses = statusService.getStatusList(status); - for (String type : types) { - Map collect = statuses.stream().filter(item -> Objects.equals(item.getType(), type)) - .collect(Collectors.toMap( - item -> item.getTime().getYear() + UniversalEnum.SHORT_BAR.getValue() + item.getTime().getMonthValue() + UniversalEnum.SHORT_BAR.getValue() + item.getTime().getDayOfMonth(), - Status::getSuccessRate - )); - maps.put(type, collect); + if (types != null) { + for (String type : types) { + Map collect = statuses.stream().filter(item -> Objects.equals(item.getType(), type)) + .collect(Collectors.toMap( + item -> item.getTime().getYear() + UniversalEnum.SHORT_BAR.getValue() + item.getTime().getMonthValue() + UniversalEnum.SHORT_BAR.getValue() + item.getTime().getDayOfMonth(), + Status::getSuccessRate + )); + maps.put(type, collect); + } } return AjaxResult.success(maps); } @@ -111,10 +122,9 @@ public class StatusController extends BaseController { //按时间划分设备柱状图 @ApiOperation("更新缓存规则") - @GetMapping ("/rule") - public AjaxResult setRule(String rule) - { - redisCache.setCacheObject(ORDERRULE,rule); + @GetMapping("/rule") + public AjaxResult setRule(String rule) { + redisCache.setCacheObject(ORDERRULE, rule); return AjaxResult.success(); } @@ -158,9 +168,8 @@ public class StatusController extends BaseController { //按类型划分设备 @ApiOperation("设备状态列表按类型") - @GetMapping ("/type") - public AjaxResult getTypeList() - { + @GetMapping("/type") + public AjaxResult getTypeList() { //DcDevice dcDevice = new DcDevice(); //dcDevice.setUseState(1); //List dcDeviceList = dcDeviceService.numberOfDevicesByType(dcDevice); @@ -352,32 +361,32 @@ public class StatusController extends BaseController { // // } //} - Map maps=new HashMap<>(); + Map maps = new HashMap<>(); double lostRate = lastEntry.stream() .mapToDouble(Status -> Double.parseDouble(Status.getLostRate().replace(UniversalEnum.PER_CENT.getValue(), UniversalEnum.EMPTY_STRING.getValue()))) // 去掉%,并转换为double .average().getAsDouble(); double sucessRate = lastEntry.stream() .mapToDouble(Status -> Double.parseDouble(Status.getSuccessRate().replace(UniversalEnum.PER_CENT.getValue(), UniversalEnum.EMPTY_STRING.getValue()))) // 去掉%,并转换为double .average().getAsDouble(); - String failRate=String.format("%.2f", (UniversalEnum.ONE_HUNDRED.getNumber()-sucessRate))+UniversalEnum.PER_CENT.getValue(); + String failRate = String.format("%.2f", (UniversalEnum.ONE_HUNDRED.getNumber() - sucessRate)) + UniversalEnum.PER_CENT.getValue(); //丢包率 - maps.put("lostRate",String.format("%.2f", lostRate)+UniversalEnum.PER_CENT.getValue()); + maps.put("lostRate", String.format("%.2f", lostRate) + UniversalEnum.PER_CENT.getValue()); //在线率 - maps.put("sucessRate",String.format("%.2f", sucessRate)+UniversalEnum.PER_CENT.getValue()); + maps.put("sucessRate", String.format("%.2f", sucessRate) + UniversalEnum.PER_CENT.getValue()); //离线率 - maps.put("failRate",failRate); + maps.put("failRate", failRate); List list = dcDeviceService.list(); //已使用数量 - maps.put("sumUseState",String.valueOf(lastEntry.size())); + maps.put("sumUseState", String.valueOf(lastEntry.size())); //总数 - maps.put("sum",String.valueOf(list.size())); - subMap.put("全部设备",maps); - Map ruleMap=new HashMap<>(); - String orderRule=redisCache.getCacheObject(ORDERRULE); - if(StringUtil.isNotEmpty(orderRule)){ - ruleMap.put("rule",orderRule); - subMap.put("排序规则",ruleMap); - }else{ + maps.put("sum", String.valueOf(list.size())); + subMap.put("全部设备", maps); + Map ruleMap = new HashMap<>(); + String orderRule = redisCache.getCacheObject(ORDERRULE); + if (StringUtil.isNotEmpty(orderRule)) { + ruleMap.put("rule", orderRule); + subMap.put("排序规则", ruleMap); + } else { String[] rules = {UniversalEnum.ALL_EQUIPMENT.getValue(), UniversalEnum.HD_NETWORK_GUN_TYPE_FIXED_CAMERA.getValue(), UniversalEnum.HD_NETWORK_SPHERICAL_CAMERA.getValue(), @@ -400,9 +409,9 @@ public class StatusController extends BaseController { UniversalEnum.SOLAR_PANEL.getValue(), UniversalEnum.REMOTE_COMPUTER.getValue() }; - orderRule= Arrays.toString(rules).replace(UniversalEnum.LEFT_CENTER_BRACKET.getValue(),UniversalEnum.EMPTY_STRING.getValue()).replace(UniversalEnum.CLOSE_CENTER_BRACKET.getValue(),UniversalEnum.EMPTY_STRING.getValue()).replace(UniversalEnum.BLANK_SPACE.getValue(),UniversalEnum.EMPTY_STRING.getValue()); - ruleMap.put("rule",orderRule); - subMap.put("排序规则",ruleMap); + orderRule = Arrays.toString(rules).replace(UniversalEnum.LEFT_CENTER_BRACKET.getValue(), UniversalEnum.EMPTY_STRING.getValue()).replace(UniversalEnum.CLOSE_CENTER_BRACKET.getValue(), UniversalEnum.EMPTY_STRING.getValue()).replace(UniversalEnum.BLANK_SPACE.getValue(), UniversalEnum.EMPTY_STRING.getValue()); + ruleMap.put("rule", orderRule); + subMap.put("排序规则", ruleMap); } return AjaxResult.success(subMap); } diff --git a/zc-business/src/main/java/com/zc/business/mapper/StatusMapper.java b/zc-business/src/main/java/com/zc/business/mapper/StatusMapper.java index 290beffb..1eb83909 100644 --- a/zc-business/src/main/java/com/zc/business/mapper/StatusMapper.java +++ b/zc-business/src/main/java/com/zc/business/mapper/StatusMapper.java @@ -25,4 +25,5 @@ public interface StatusMapper { List listStatusByTypes(@Param("status")Status status); List getStatusList(@Param("status")Status status); + int delStatusByTime(Status status); } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java index 2c0ebaee..e6ec4a39 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java @@ -1,10 +1,10 @@ package com.zc.business.service.impl; - import cn.hutool.core.bean.BeanUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.google.gson.Gson; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.spring.SpringUtils; import com.zc.business.constant.RedisKeyConstants; @@ -17,14 +17,12 @@ import com.zc.business.enums.UniversalEnum; import com.zc.business.service.IDcFacilityService; import com.zc.business.service.IDcRoadSectionService; import com.zc.business.service.IDcTrafficStatisticsService; +import com.zc.business.utils.RetryInterceptorUtil; import com.zc.business.utils.StakeMarkUtils; import com.zc.common.core.httpclient.OkHttp; import com.zc.common.core.httpclient.exception.HttpException; import com.zc.common.core.httpclient.request.RequestParams; -import okhttp3.Call; -import okhttp3.Callback; -import okhttp3.Response; -import okhttp3.ResponseBody; +import okhttp3.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -637,6 +635,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi .data(requestParams) // 请求参数 .post(); // 请求方法 + ResponseBody body = response.body(); if (body != null) { String jsonString = body.string(); @@ -660,46 +659,98 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi */ @Override public JSONArray sectionHourlyTrafficFlow(String startDate, String endDate) throws HttpException, IOException { - OkHttp okHttp = new OkHttp(); - - RequestParams requestParams = new RequestParams(); +// +// +// OkHttp okHttp = new OkHttp(); +// +// RequestParams requestParams = new RequestParams(); +// +// requestParams.put("sysid", sysid); +// +// JSONObject parameters = new JSONObject() { +// { +// put("start_date", startDate); +// put("end_date", endDate); +// } +// }; +// +// requestParams.put("parameters", parameters.toJSONString()); +// +// Map headers = new HashMap<>(); +// headers.put("Authorization", getAccessToken()); +// try { +// Response response // 请求响应 +// = okHttp +// .headers(headers) +// .url(baseUrl + UniversalEnum.SECTION_HOURLY_TRAFFIC_FLOW_BY_VEHICLE_TYPE.getValue()) // 请求地址 +// .data(requestParams) // 请求参数 +// .post(); // 请求方法 +// +// ResponseBody body = response.body(); +// if (body != null) { +// String jsonString = body.string(); +// if (JSON.isValidArray(jsonString)) { +// return JSON.parseArray(jsonString); +// }else { +// return new JSONArray(); +// } +// } +// return new JSONArray(); +// } catch (IOException e) { +// // 处理异常 +// e.printStackTrace(); +// return new JSONArray(); +// } + +// 创建OkHttpClient.Builder实例 + OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); + +// 添加重试拦截器到OkHttpClient + httpClientBuilder.addInterceptor(new RetryInterceptorUtil()); + +// 构建最终的OkHttpClient实例 + OkHttpClient okHttpClient = httpClientBuilder.build(); + +// 现在使用带有重试机制的OkHttpClient发起请求 + Map requestParams = new HashMap<>(); requestParams.put("sysid", sysid); - JSONObject parameters = new JSONObject() { - { - put("start_date", startDate); - put("end_date", endDate); - } - }; + JSONObject parameters = new JSONObject(); + parameters.put("start_date", startDate); + parameters.put("end_date", endDate); - requestParams.put("parameters", parameters.toJSONString()); + requestParams.put("parameters", parameters); Map headers = new HashMap<>(); headers.put("Authorization", getAccessToken()); + Gson gson = new Gson(); + String requestParamsJson = gson.toJson(requestParams); +// 使用okHttpClient实例发起请求 try { - Response response // 请求响应 - = okHttp - .headers(headers) - .url(baseUrl + UniversalEnum.SECTION_HOURLY_TRAFFIC_FLOW_BY_VEHICLE_TYPE.getValue()) // 请求地址 - .data(requestParams) // 请求参数 - .post(); // 请求方法 + Request request = new Request.Builder() + .url(baseUrl + UniversalEnum.SECTION_HOURLY_TRAFFIC_FLOW_BY_VEHICLE_TYPE.getValue()) + .headers(Headers.of(headers)) + .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestParamsJson)) + .build(); - ResponseBody body = response.body(); - if (body != null) { - String jsonString = body.string(); - if (JSON.isValidArray(jsonString)) { - return JSON.parseArray(jsonString); - }else { - return new JSONArray(); + Response response = okHttpClient.newCall(request).execute(); + + ResponseBody body = response.body(); + if (body != null) { + String jsonString = body.string(); + if (JSON.isValidArray(jsonString)) { + return JSON.parseArray(jsonString); + } else { + return new JSONArray(); + } } - } - return new JSONArray(); + return new JSONArray(); } catch (IOException e) { - // 处理异常 e.printStackTrace(); return new JSONArray(); } + } /** @@ -707,39 +758,38 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi */ @Override public List> trafficFlowAtTollStationEntrance(String startDate, String endDate, String stationType) throws HttpException, IOException { - OkHttp okHttp = new OkHttp(); +// 创建OkHttpClient.Builder实例 + OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); - RequestParams requestParams = new RequestParams(); +// 添加重试拦截器到OkHttpClient + httpClientBuilder.addInterceptor(new RetryInterceptorUtil()); - requestParams.put("sysid", sysid); - - JSONObject parameters = new JSONObject() { - { - put("start_date", startDate); - put("end_date", endDate); - put("station_type", stationType); - } - }; +// 构建最终的OkHttpClient实例 + OkHttpClient okHttpClient = httpClientBuilder.build(); - requestParams.put("parameters", parameters.toJSONString()); +// 现在使用带有重试机制的OkHttpClient发起请求 + Map requestParams = new HashMap<>(); + requestParams.put("sysid", sysid); + JSONObject parameters = new JSONObject(); + parameters.put("start_date", startDate); + parameters.put("end_date", endDate); + parameters.put("station_type", stationType); + requestParams.put("parameters", parameters); Map headers = new HashMap<>(); - headers.put("Authorization", getAccessToken()); + Gson gson = new Gson(); + String requestParamsJson = gson.toJson(requestParams); try { - Response response = okHttp - .headers(headers) + Request request = new Request.Builder() .url(baseUrl + UniversalEnum.EACH_TOLL_STATION_ENTRANCE_BY_TYPE_OF_HOURLY_TRAFFIC_FLOW.getValue()) - .data(requestParams) - .post(); - // 确保响应成功 - if (!response.isSuccessful()) { - throw new IOException("请求不成功,HTTP代码:" + response.code()); - } + .headers(Headers.of(headers)) + .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestParamsJson)) + .build(); + Response response = okHttpClient.newCall(request).execute(); ResponseBody body = response.body(); if (body != null) { String jsonString = body.string(); - System.out.println(jsonString); if (JSON.isValidArray(jsonString)) { JSONArray jsonArray = JSON.parseArray(jsonString); Map sumByName = new LinkedHashMap<>(); @@ -1055,22 +1105,30 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi */ @Override public JSONArray getTheCurrentCongestedSection() throws HttpException, IOException { - OkHttp okHttp = new OkHttp(); + // 创建OkHttpClient.Builder实例 + OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); - RequestParams requestParams = new RequestParams(); +// 添加重试拦截器到OkHttpClient + httpClientBuilder.addInterceptor(new RetryInterceptorUtil()); - requestParams.put("sysid", sysid); +// 构建最终的OkHttpClient实例 + OkHttpClient okHttpClient = httpClientBuilder.build(); +// 现在使用带有重试机制的OkHttpClient发起请求 + Map requestParams = new HashMap<>(); + requestParams.put("sysid", sysid); Map headers = new HashMap<>(); headers.put("Authorization", getAccessToken()); + Gson gson = new Gson(); + String requestParamsJson = gson.toJson(requestParams); + //todo 获取路况信息 try { - Response response // 请求响应 - = okHttp - .headers(headers) - .url(baseUrl + UniversalEnum.GETS_THE_URI_OF_THE_CURRENT_CONGESTION_EVENT.getValue()) // 请求地址 - .data(requestParams) // 请求参数 - .post(); // 请求方法 - + Request request = new Request.Builder() + .url(baseUrl + UniversalEnum.GETS_THE_URI_OF_THE_CURRENT_CONGESTION_EVENT.getValue()) + .headers(Headers.of(headers)) + .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestParamsJson)) + .build(); + Response response = okHttpClient.newCall(request).execute(); ResponseBody body = response.body(); if (body != null) { String jsonString = body.string(); @@ -1189,32 +1247,36 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi } private Response getResponseTrafficFlowAtToll(String startDate, String stationType) throws HttpException, IOException { - OkHttp okHttp = new OkHttp(); - - RequestParams requestParams = new RequestParams(); + // 创建OkHttpClient.Builder实例 + OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); - requestParams.put("sysid", sysid); +// 添加重试拦截器到OkHttpClient + httpClientBuilder.addInterceptor(new RetryInterceptorUtil()); - JSONObject parameters = new JSONObject() { - { - put("start_date", startDate); - put("end_date", startDate); - put("station_type", stationType); - } - }; +// 构建最终的OkHttpClient实例 + OkHttpClient okHttpClient = httpClientBuilder.build(); - requestParams.put("parameters", parameters.toJSONString()); +// 现在使用带有重试机制的OkHttpClient发起请求 + Map requestParams = new HashMap<>(); + requestParams.put("sysid", sysid); + JSONObject parameters = new JSONObject(); + parameters.put("start_date", startDate); + parameters.put("end_date", startDate); + parameters.put("station_type", stationType); + requestParams.put("parameters", parameters); Map headers = new HashMap<>(); - headers.put("Authorization", getAccessToken()); + Gson gson = new Gson(); + String requestParamsJson = gson.toJson(requestParams); + + Request request = new Request.Builder() + .url(baseUrl + UniversalEnum.EACH_TOLL_STATION_ENTRANCE_BY_TYPE_OF_HOURLY_TRAFFIC_FLOW.getValue()) + .headers(Headers.of(headers)) + .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestParamsJson)) + .build(); + Response response = okHttpClient.newCall(request).execute(); - Response response // 请求响应 - = okHttp - .headers(headers) - .url(baseUrl + UniversalEnum.EACH_TOLL_STATION_ENTRANCE_BY_TYPE_OF_HOURLY_TRAFFIC_FLOW.getValue()) // 请求地址 - .data(requestParams) // 请求参数 - .post(); // 请求方法 return response; } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/StatusService.java b/zc-business/src/main/java/com/zc/business/service/impl/StatusService.java index 9afb97c9..4951accf 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/StatusService.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/StatusService.java @@ -53,4 +53,7 @@ public class StatusService { return list; } + public int delStatusByTime(Status status) { + return statusMapper.delStatusByTime(status); + } } diff --git a/zc-business/src/main/java/com/zc/business/utils/RetryInterceptorUtil.java b/zc-business/src/main/java/com/zc/business/utils/RetryInterceptorUtil.java new file mode 100644 index 00000000..0d627994 --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/utils/RetryInterceptorUtil.java @@ -0,0 +1,53 @@ +package com.zc.business.utils; + +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; + +import java.io.IOException; +import java.io.InterruptedIOException; +import java.net.SocketTimeoutException; +import java.rmi.UnknownHostException; + +/** + *重试拦截器 + */ + +public class RetryInterceptorUtil implements Interceptor { + + private static final int MAX_RETRIES = 3; // 最大重试次数 + private int retryCount = 0; + + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + while (retryCount <= MAX_RETRIES) { + try { + Response response = chain.proceed(request); + if (!response.isSuccessful() && retryCount < MAX_RETRIES) { + retryCount++; + System.out.println("请求失败的代码 " + response.code() + ". 重试 (" + retryCount + "/" + MAX_RETRIES + ")..."); + response.close(); + continue; + } else { + return response; + } + } catch (IOException e) { // 捕获所有IO异常,包括连接被拒绝 + if (++retryCount <= MAX_RETRIES) { + System.out.println("IOException。重试 (" + retryCount + "/" + MAX_RETRIES + ")..."); + // 可能需要短暂停顿后再重试,避免立即重试导致问题持续 + try { + Thread.sleep(1000); // 暂停1秒后重试 + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + throw new IOException("重试睡眠期间中断", ie); + } + } else { + throw e; // 超过最大重试次数,重新抛出异常 + } + } + } + return chain.proceed(request); // 如果没有异常或重试完毕,正常返回 + } + } + diff --git a/zc-business/src/main/resources/mapper/business/StatusMapper.xml b/zc-business/src/main/resources/mapper/business/StatusMapper.xml index cce1976e..6d9e9f7d 100644 --- a/zc-business/src/main/resources/mapper/business/StatusMapper.xml +++ b/zc-business/src/main/resources/mapper/business/StatusMapper.xml @@ -328,4 +328,14 @@ + + DELETE FROM status + + + AND status.time < #{startTime,jdbcType=DATE} + + + + +