From 655b29c16a6bcaf8f3064d3f10bf11a3a6d0abea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=85=B4=E7=90=B3?= <1911390090@qq.com> Date: Fri, 5 Jul 2024 17:37:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E8=B4=B9=E7=AB=99=E6=97=A5=E7=B4=AF?= =?UTF-8?q?=E8=AE=A1=E8=BD=A6=E6=B5=81=E8=BE=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DcTrafficStatisticsController.java | 11 +++ .../service/IDcTrafficStatisticsService.java | 6 ++ .../impl/DcTrafficStatisticsServiceImpl.java | 70 ++++++++++++++++++- 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java b/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java index 8986f5bb..f7fd5497 100644 --- a/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java +++ b/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java @@ -803,6 +803,17 @@ public AjaxResult trafficFlowAtTollStationEntranceHour(String startDate, String // 将获取到的交通指标数据封装为成功的结果并返回 return AjaxResult.success(jsonArray); } + /** + * 各收费站入口分车型车流量 + */ + @ApiOperation("收费站日累计车流辆") + @GetMapping("/history/trafficFlowAtToll") + public AjaxResult trafficFlowAtToll() throws HttpException, IOException { + // 调用服务层方法,获取当前交通指标数据 + int a = dcTrafficStatisticsService.trafficFlowAtToll(); + // 将获取到的交通指标数据封装为成功的结果并返回 + return AjaxResult.success(a); + } /** * 导出收费站出入口实时车流量 diff --git a/zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java b/zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java index b9f99d05..70e828c3 100644 --- a/zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java +++ b/zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java @@ -58,4 +58,10 @@ public interface IDcTrafficStatisticsService { JSONArray getTheCurrentCongestedSection()throws HttpException, IOException; + /** + * 各收费站日累计车流辆 + * @return + */ + int trafficFlowAtToll()throws HttpException, IOException; + } 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 f69f2428..ea658dff 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 @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.TypeFactory; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.spring.SpringUtils; +import com.sun.xml.bind.v2.TODO; import com.zc.business.constant.RedisKeyConstants; import com.zc.business.domain.*; import com.zc.business.enums.StakeMarkRange; @@ -683,9 +684,13 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi if (body != null) { JSONArray jsonArray = JSON.parseArray(body.string()); Map sumByName = new LinkedHashMap<>(); + Integer totalFlow1=0; List> list = new ArrayList(); for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object JSONObject jsonObject = (JSONObject) item; + + totalFlow1=totalFlow1+ jsonObject.getInteger("total_flow"); + // 获取当前时间 LocalTime now = LocalTime.now(); // 获取当前小时数(24小时制) @@ -693,10 +698,11 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi if (jsonObject.getInteger("data_hour") == currentHour) { String name = jsonObject.getString("ts_name"); // 更安全的取值方式 int totalFlow = jsonObject.getInteger("total_flow"); // 专门针对Integer类型 + sumByName.put(name, totalFlow); } } - + System.out.println("总数===:"+totalFlow1); // 正确创建新的映射对象并添加到list中 for (Map.Entry entry : sumByName.entrySet()) { Map singleResult = new LinkedHashMap<>(); // 每次循环都创建一个新的映射 @@ -1036,6 +1042,68 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi return new JSONArray(); } + /** + * 各收费站日累计车流辆 + * @return + */ + @Override + public int trafficFlowAtToll()throws HttpException, IOException { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(UniversalEnum.TIME_FORMAT_YEARS_MONTH_DAY.getValue()); + LocalDate currentDate = LocalDate.now(); + String startDate = currentDate.format(formatter); + Response response = getResponseTrafficFlowAtToll(startDate,UniversalEnum.ONE.getValue()); + Response responseTwo = getResponseTrafficFlowAtToll(startDate,UniversalEnum.TWO.getValue()); + + ResponseBody body = response.body(); + ResponseBody bodyTwo = responseTwo.body(); + Integer totalFlow1=UniversalEnum.ZERO.getNumber(); + Integer totalFlow2=UniversalEnum.ZERO.getNumber(); + if (body != null) { + JSONArray jsonArray = JSON.parseArray(body.string()); + for (Object item : jsonArray) { + JSONObject jsonObject = (JSONObject) item; + totalFlow1=totalFlow1+ jsonObject.getInteger("total_flow"); + } + } + if (bodyTwo != null) { + JSONArray jsonArray = JSON.parseArray(bodyTwo.string()); + for (Object item : jsonArray) { + JSONObject jsonObject = (JSONObject) item; + totalFlow2=totalFlow2+ jsonObject.getInteger("total_flow"); + } + } + return totalFlow1+totalFlow2; + } + + private Response getResponseTrafficFlowAtToll(String startDate,String stationType) throws HttpException, IOException { + OkHttp okHttp = new OkHttp(); + + RequestParams requestParams = new RequestParams(); + + requestParams.put("sysid", sysid); + + JSONObject parameters = new JSONObject() { + { + put("start_date", startDate); + put("end_date", startDate); + put("station_type", stationType); + } + }; + + requestParams.put("parameters", parameters.toJSONString()); + + Map headers = new HashMap<>(); + + headers.put("Authorization", getAccessToken()); + + Response response // 请求响应 + = okHttp + .headers(headers) + .url(baseUrl + UniversalEnum.EACH_TOLL_STATION_ENTRANCE_BY_TYPE_OF_HOURLY_TRAFFIC_FLOW.getValue()) // 请求地址 + .data(requestParams) // 请求参数 + .post(); // 请求方法 + return response; + } public DcFacility findNearestFacility(List dcFacilityList, String stakeMarkCode) { // 将目标桩号转换为整数