Browse Source

Merge remote-tracking branch 'origin/develop' into develop

develop
wangsixiang 8 months ago
parent
commit
181743df6f
  1. 125
      ruoyi-common/src/test/java/OkhttpTest.java
  2. 24
      zc-business/src/main/java/com/zc/business/controller/DCPerceivedEventsWarningController.java
  3. 60
      zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java
  4. 4
      zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java
  5. 3
      zc-business/src/main/java/com/zc/business/service/impl/DcNoStakeWarningTableServiceImpl.java
  6. 165
      zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

125
ruoyi-common/src/test/java/OkhttpTest.java

@ -1,4 +1,7 @@
import com.ruoyi.common.utils.uuid.IdUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zc.common.core.httpclient.OkHttp;
import com.zc.common.core.httpclient.exception.HttpException;
import com.zc.common.core.httpclient.request.RequestParams;
@ -7,8 +10,18 @@ import okhttp3.Callback;
import okhttp3.Response;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@ -19,6 +32,7 @@ public class OkhttpTest {
/**
* get 同步请求
*
* @throws HttpException
* @throws IOException
*/
@ -47,6 +61,7 @@ public class OkhttpTest {
/**
* get 异步请求
*
* @throws HttpException
* @throws IOException
*/
@ -61,16 +76,20 @@ public class OkhttpTest {
// 请求失败回调
@Override
public void onFailure(Call call, IOException e) { }
public void onFailure(Call call, IOException e) {
}
// 请求成功回调
@Override
public void onResponse(Call call, Response response) throws IOException { }
public void onResponse(Call call, Response response) throws IOException {
}
}); // 请求方法
}
/**
* post 同步请求
*
* @throws HttpException
* @throws IOException
*/
@ -98,6 +117,7 @@ public class OkhttpTest {
/**
* 文件 post 异步请求
*
* @throws HttpException
* @throws IOException
*/
@ -115,16 +135,20 @@ public class OkhttpTest {
.post(new Callback() {
// 请求失败回调
@Override
public void onFailure(Call call, IOException e) { }
public void onFailure(Call call, IOException e) {
}
// 请求成功回调
@Override
public void onResponse(Call call, Response response) throws IOException { }
public void onResponse(Call call, Response response) throws IOException {
}
}); // 请求方法
}
/**
* 文件 post 同步请求
*
* @throws HttpException
* @throws IOException
*/
@ -149,6 +173,7 @@ public class OkhttpTest {
/**
* post 异步请求
*
* @throws HttpException
* @throws IOException
*/
@ -165,18 +190,98 @@ public class OkhttpTest {
.filePost(new Callback() {
// 请求失败回调
@Override
public void onFailure(Call call, IOException e) { }
public void onFailure(Call call, IOException e) {
}
// 请求成功回调
@Override
public void onResponse(Call call, Response response) throws IOException { }
public void onResponse(Call call, Response response) throws IOException {
}
}); // 请求方法
}
@Test
public void aaa() {
String uuid = IdUtils.fastSimpleUUID();
System.out.println(uuid.length());
System.out.println(uuid);
try {
String apiKey = "00b26cbc2cb242283452ac5c842e81d1"; // 替换为你的API密钥
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 获取当前年份
int currentYear = currentDate.getYear();
String urlString = "https://apis.tianapi.com/jiejiari/index?key=" + apiKey + "&date=" + currentYear + "&type=1";
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
if (connection.getResponseCode() != 200) {
throw new RuntimeException("HTTP错误码:" + connection.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
String output;
StringBuilder response = new StringBuilder();
while ((output = br.readLine()) != null) {
response.append(output);
}
connection.disconnect();
JSONObject jsonResponse = JSONObject.parseObject(response.toString());
// 计算当前日期加上七天的日期
LocalDate dateAfterSevenDays = currentDate.plusDays(7);
String formatter = String.valueOf(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// 检查响应状态是否成功
if (jsonResponse.getInteger("code") == 200) {
// 获取result对象
JSONObject resultObj = jsonResponse.getJSONObject("result");
// 获取节假日列表JSONArray
JSONArray listArray = resultObj.getJSONArray("list");
// 遍历节假日列表
for (int i = 0; i < listArray.size(); i++) {
JSONObject holidayObj = listArray.getJSONObject(i);
String vacation = holidayObj.getString("vacation");
String name = holidayObj.getString("name");
String wage = holidayObj.getString("wage");
// Map<String, String> dateMap = splitDateString(vacation);
if (wage.equals(formatter)) {
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 分割日期字符串并构造Map
*
* @param dateStr 原始日期字符串 "|" 分隔
* @return 一个Map其中第一个日期为键剩余日期为值
*/
public static Map<String, String> splitDateString(String dateStr) {
String[] dates = dateStr.split("\\|");
if (dates.length > 0) {
Map<String, String> map = new HashMap<>();
String key = dates[0]; // 第一个日期作为键
map.put(key, dateStr);
return map;
} else {
return Collections.emptyMap(); // 如果原始字符串为空或只有分隔符,则返回空Map
}
}
@Test
public void ddd(){
// 获取当前季度
System.out.println();
}
}

24
zc-business/src/main/java/com/zc/business/controller/DCPerceivedEventsWarningController.java

@ -266,11 +266,11 @@ public class DCPerceivedEventsWarningController extends BaseController {
*/
@ApiOperation(value = "导出感知事件多发时段",tags = {"ECharts导出"})
@GetMapping("/exportManyTimesInterval")
public AjaxResult exportManyTimesInterval(HttpServletResponse response,DcWarning dcWarning){
public void exportManyTimesInterval(HttpServletResponse response,DcWarning dcWarning){
if (dcWarning==null||StringUtils.isBlank(dcWarning.getType())||dcWarning.getCurrently()==null||dcWarning.getLastYear()==null
||StringUtils.isBlank(dcWarning.getStartStakeMark())||StringUtils.isBlank(dcWarning.getEndStakeMark())
||StringUtils.isBlank(dcWarning.getDirection())){
return AjaxResult.error("参数错误");
return ;
}
HashMap<String,Object> map = perceivedEventsWarningService.selectManyTimesInterval(dcWarning);
List<ManyTimesInterval> list = new ArrayList<>();
@ -289,7 +289,6 @@ public class DCPerceivedEventsWarningController extends BaseController {
ExcelUtil<ManyTimesInterval> util = new ExcelUtil<>(ManyTimesInterval.class);
util.exportExcel(response, list, "感知事件多发时段");
return AjaxResult.success("导出感知事件多发时段成功");
}
//新-感知事件类型分析
@ -304,11 +303,11 @@ public class DCPerceivedEventsWarningController extends BaseController {
}
@ApiOperation(value = "导出感知事件类型分析",tags = {"ECharts导出"})
@GetMapping("/exportSelectWarningType")
public AjaxResult exportSelectWarningType(HttpServletResponse response,DcWarning dcWarning){
public void exportSelectWarningType(HttpServletResponse response,DcWarning dcWarning){
if (dcWarning==null||StringUtils.isBlank(dcWarning.getType())||dcWarning.getCurrently()==null||dcWarning.getLastYear()==null
||StringUtils.isBlank(dcWarning.getStartStakeMark())||StringUtils.isBlank(dcWarning.getEndStakeMark())
||StringUtils.isBlank(dcWarning.getDirection())){
return AjaxResult.error("参数错误");
return;
}
HashMap<String,Object> map = perceivedEventsWarningService.newSelectWarningType(dcWarning);
List<SelectWarningType> list = new ArrayList<>();
@ -330,7 +329,6 @@ public class DCPerceivedEventsWarningController extends BaseController {
ExcelUtil<SelectWarningType> util = new ExcelUtil<>(SelectWarningType.class);
util.exportExcel(response, list, "感知事件类型分析");
return AjaxResult.success("导出感知事件类型分析成功");
}
//新-感知事件桩号范围内事件分析
@PostMapping("/selectSection")
@ -345,11 +343,11 @@ public class DCPerceivedEventsWarningController extends BaseController {
@ApiOperation(value = "导出桩号范围内事件分析",tags = {"ECharts导出"})
@GetMapping("/exportSelectSection")
public AjaxResult exportSelectSection(HttpServletResponse response,DcWarning dcWarning){
public void exportSelectSection(HttpServletResponse response,DcWarning dcWarning){
if (dcWarning==null||StringUtils.isBlank(dcWarning.getType())||dcWarning.getCurrently()==null||dcWarning.getLastYear()==null
||StringUtils.isBlank(dcWarning.getStartStakeMark())||StringUtils.isBlank(dcWarning.getEndStakeMark())
||StringUtils.isBlank(dcWarning.getDirection())){
return AjaxResult.error("参数错误");
return;
}
HashMap<String,Object> map = perceivedEventsWarningService.newSelectSection(dcWarning);
List<SelectSection> list = new ArrayList<>();
@ -376,7 +374,6 @@ public class DCPerceivedEventsWarningController extends BaseController {
ExcelUtil<SelectSection> util = new ExcelUtil<>(SelectSection.class);
util.exportExcel(response, list, "桩号范围内事件分析");
return AjaxResult.success("导出桩号范围内事件分析成功");
}
//新-感知事件路段处置类型分析
@PostMapping("/selectStateType")
@ -388,9 +385,9 @@ public class DCPerceivedEventsWarningController extends BaseController {
}
@ApiOperation(value = "导出感知事件路段分析",tags = {"ECharts导出"})
@GetMapping("/exportSelectStateType")
public AjaxResult exportSelectStateType(HttpServletResponse response, DcWarning dcWarning){
public void exportSelectStateType(HttpServletResponse response, DcWarning dcWarning){
if (dcWarning==null||dcWarning.getCurrently()==null||dcWarning.getLastYear()==null){
return AjaxResult.error("参数错误");
return;
}
List<SelectStateType> list = new ArrayList<>();
@ -443,7 +440,6 @@ public class DCPerceivedEventsWarningController extends BaseController {
ExcelUtil<SelectStateType> util = new ExcelUtil<>(SelectStateType.class);
util.exportExcel(response, list, "感知事件路段分析");
return AjaxResult.success("导出感知事件路段分析成功");
}
//查询感知数量按照路段数量进行排名
@PostMapping("/sectionPerceivedList")
@ -453,7 +449,7 @@ public class DCPerceivedEventsWarningController extends BaseController {
@ApiOperation(value = "导出感知事件路段排名",tags = {"ECharts导出"})
@GetMapping("/exportSectionPerceivedList")
public AjaxResult exportSectionPerceivedList(HttpServletResponse response){
public void exportSectionPerceivedList(HttpServletResponse response){
List<HashMap<String,Object>> dataList = perceivedEventsWarningService.selectSectionPerceivedEventsList();
List<SectionPerceivedList> list = new ArrayList<>();
if (dataList != null && dataList.size() > 0){
@ -467,7 +463,5 @@ public class DCPerceivedEventsWarningController extends BaseController {
}
ExcelUtil<SectionPerceivedList> util = new ExcelUtil<>(SectionPerceivedList.class);
util.exportExcel(response, list, "感知事件路段排名");
return AjaxResult.success("导出感知事件路段排名成功");
}
}

60
zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java

@ -24,6 +24,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
@ -222,9 +223,9 @@ public AjaxResult realTimeTrafficFlow(String startDate, String direction,String
*/
@ApiOperation(value="导出全路段车流量状况分析",tags = {"ECharts导出"})
@GetMapping("/history/exportRealTimeTrafficFlow")
public AjaxResult exportRealTimeTrafficFlow(HttpServletResponse response,String startDate, String direction,String periodType ){
public void exportRealTimeTrafficFlow(HttpServletResponse response,String startDate, String direction,String periodType ){
if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(direction) || StringUtils.isEmpty(periodType)){
return AjaxResult.error("参数错误");
return;
}
String endDate = "";
if ("4".equals(periodType)){
@ -280,7 +281,6 @@ public AjaxResult realTimeTrafficFlow(String startDate, String direction,String
ExcelUtil<RealTimeTrafficFlow> util = new ExcelUtil<>(RealTimeTrafficFlow.class);
util.exportExcel(response, list, "全路段双向实时车流量");
// 将查询结果封装为成功响应并返回
return AjaxResult.success("导出成功");
}
/**
* 车流量时段分析
@ -304,9 +304,9 @@ public AjaxResult realTimeTrafficFlow(String startDate, String direction,String
*/
@ApiOperation(value="导出车流量时段分析",tags = {"ECharts导出"})
@GetMapping("/history/exportTrafficPeriodAnalysis")
public AjaxResult exportTrafficPeriodAnalysis(HttpServletResponse response,String startDate, String direction,String periodType ){
public void exportTrafficPeriodAnalysis(HttpServletResponse response,String startDate, String direction,String periodType ){
if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(direction) || StringUtils.isEmpty(periodType)){
return AjaxResult.error("参数错误");
return;
}
String endDate = "";
if ("4".equals(periodType)){
@ -361,8 +361,6 @@ public AjaxResult realTimeTrafficFlow(String startDate, String direction,String
ExcelUtil<TrafficPeriodAnalysis> util = new ExcelUtil<>(TrafficPeriodAnalysis.class);
util.exportExcel(response, list, "车流量时段分析");
// 将查询结果封装为成功响应并返回
return AjaxResult.success("导出成功");
}
@ApiOperation("断面车流量排名")
@ -383,7 +381,7 @@ public AjaxResult realTimeTrafficFlow(String startDate, String direction,String
*/
@ApiOperation(value="导出断面车流量排名",tags = {"ECharts导出"})
@GetMapping("/history/exportSectionTrafficRanking")
public AjaxResult exportSectionTrafficRanking(HttpServletResponse response,String startDate, String direction,String periodType ){
public void exportSectionTrafficRanking(HttpServletResponse response,String startDate, String direction,String periodType ){
List<SectionTrafficRanking> list = new ArrayList<>();
List<Map<String, String>> dcStatisticsData = dcGantryStatisticsDataService.sectionTrafficRanking(startDate,direction,periodType);
for (int i = 0; i < dcStatisticsData.size(); i++) {
@ -396,8 +394,7 @@ public AjaxResult realTimeTrafficFlow(String startDate, String direction,String
ExcelUtil<SectionTrafficRanking> util = new ExcelUtil<>(SectionTrafficRanking.class);
util.exportExcel(response, list, "断面车流量排名");
// 将查询结果封装为成功响应并返回
return AjaxResult.success("导出成功");
}
/**
@ -418,7 +415,7 @@ public AjaxResult realTimeTrafficFlow(String startDate, String direction,String
@ApiOperation(value="导出路段交通指标分析",tags = {"ECharts导出"})
@GetMapping("/history/exportSectionTrafficIndexAnalysis")
public AjaxResult exportSectionTrafficIndexAnalysis(HttpServletResponse response,String startDate, String direction,String periodType,Long ranking){
public void exportSectionTrafficIndexAnalysis(HttpServletResponse response,String startDate, String direction,String periodType,Long ranking){
List<SectionTrafficIndexAnalysis> list = new ArrayList<>();
String endDate = "";
@ -482,7 +479,6 @@ public AjaxResult realTimeTrafficFlow(String startDate, String direction,String
ExcelUtil<SectionTrafficIndexAnalysis> util = new ExcelUtil<>(SectionTrafficIndexAnalysis.class);
util.exportExcel(response, list, "路段交通指标分析");
// 将查询结果封装为成功响应并返回
return AjaxResult.success("导出成功");
}
@ApiOperation("通指标时间分布")
@ -641,8 +637,15 @@ public AjaxResult trafficFlowAtTollStationEntrance(String startDate, String endD
*/
@ApiOperation("全路段双向实时车流量")
@GetMapping("/history/realTimeTrafficFlowHour")
public AjaxResult realTimeTrafficFlowHour(String startDate,Long direction) throws HttpException, IOException{
List<Map<String,Object>> mapList = dcTrafficStatisticsService.realTimeTrafficFlowHour(startDate,direction);
public AjaxResult realTimeTrafficFlowHour() throws HttpException, IOException{
Map<String,List<Map<String, Object>>> mapList = dcTrafficStatisticsService.realTimeTrafficFlowHour();
// 将查询结果封装为成功响应并返回
return AjaxResult.success(mapList);
}
@ApiOperation("按照桩号查询门架数据 ")
@GetMapping("/history/queryTheGantryDataByPileNumber")
public AjaxResult queryTheGantryDataByPileNumber(String startDate,String stakeMark) throws HttpException, IOException{
List<Map<String,Object>> mapList = dcTrafficStatisticsService.queryTheGantryDataByPileNumber(startDate,stakeMark);
// 将查询结果封装为成功响应并返回
return AjaxResult.success(mapList);
}
@ -665,7 +668,13 @@ public AjaxResult trafficFlowAtTollStationEntrance(String startDate, String endD
*/
@ApiOperation(value = "导出全路段双向实时车流量",tags = {"ECharts导出"})
@GetMapping("/history/exportRealTimeTrafficFlowHour")
public AjaxResult exportRealTimeTrafficFlowHour(HttpServletResponse response) throws IOException, HttpException {
public void exportRealTimeTrafficFlowHour(HttpServletResponse response) throws IOException, HttpException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = "全路段双向实时车流量.xlsx";
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate currentDate = LocalDate.now();
@ -675,12 +684,14 @@ public AjaxResult trafficFlowAtTollStationEntrance(String startDate, String endD
LocalDate oneYearAgo = currentDate.minusYears(1);
String lastYear = oneYearAgo.format(formatter);
List<Map<String,Object>> thisYearHZ = dcTrafficStatisticsService.realTimeTrafficFlowHour(now,1L);
List<Map<String,Object>> thisYearJN = dcTrafficStatisticsService.realTimeTrafficFlowHour(now,3L);
List<Map<String,Object>> lastYearHZ = dcTrafficStatisticsService.realTimeTrafficFlowHour(lastYear,1L);
List<Map<String,Object>> lastYearJN = dcTrafficStatisticsService.realTimeTrafficFlowHour(lastYear,3L);
Map<String,List<Map<String,Object>>> map = dcTrafficStatisticsService.realTimeTrafficFlowHour();
if (map != null){
List<Map<String,Object>> thisYearHZ = map.get("1");
List<Map<String,Object>> thisYearJN = map.get("2");
List<Map<String,Object>> lastYearHZ = map.get("3");
List<Map<String,Object>> lastYearJN = map.get("4");
Workbook workbook = new XSSFWorkbook(); // 创建工作簿
XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作簿
Sheet sheet = workbook.createSheet("全路段双向实时车流量"); // 创建工作表
// 创建数据行样式
@ -781,15 +792,14 @@ public AjaxResult trafficFlowAtTollStationEntrance(String startDate, String endD
// 写入文件
try {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
workbook.write(response.getOutputStream());
try (ServletOutputStream outputStream = response.getOutputStream()){
workbook.write(outputStream);
} finally {
workbook.close();
}
return AjaxResult.success("导出感知事件多发时段成功");
}
}
}

4
zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java

@ -52,7 +52,7 @@ public interface IDcTrafficStatisticsService {
JSONArray trafficFlowAtTollStationEntranceHour(String startDate, String endDate, String stationType)throws HttpException, IOException;
List<Map<String, Object>> realTimeTrafficFlowHour(String startDate,Long direction) throws HttpException, IOException;
// JSONArray realTimeTrafficFlowHour2(String startDate,Long direction) throws HttpException, IOException;
Map<String,List<Map<String, Object>>> realTimeTrafficFlowHour() throws HttpException, IOException;
List<Map<String,Object>> queryTheGantryDataByPileNumber(String startDate,String stakeMark) throws HttpException, IOException;
}

3
zc-business/src/main/java/com/zc/business/service/impl/DcNoStakeWarningTableServiceImpl.java

@ -54,6 +54,9 @@ public class DcNoStakeWarningTableServiceImpl extends ServiceImpl<DcNoStakeWarni
if (endTime != null && startTime != null) {
queryWrapper.between(DcNoStakeWarningTable::getWarningTime, startTime, endTime);
}
queryWrapper.orderByDesc(DcNoStakeWarningTable::getWarningTime);
// 木桩
return queryWrapper;
}

165
zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

@ -31,7 +31,9 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
@ -632,35 +634,27 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
JSONArray jsonArray = JSON.parseArray(body.string());
// 使用HashMap来分组并求和
Map<String, Integer> sumByName = new HashMap<>();
Map<String, Integer> sumByName = new LinkedHashMap<>();
List<Map<String, String>> list = new ArrayList();
for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object
JSONObject jsonObject = (JSONObject) item;
// 获取当前时间
LocalTime now = LocalTime.now();
// 获取当前小时数(24小时制)
int currentHour = now.getHour();
if (jsonObject.getInteger("data_hour") == currentHour) {
String name = jsonObject.getString("ts_name"); // 更安全的取值方式
int totalFlow = jsonObject.getInteger("total_flow"); // 专门针对Integer类型
sumByName.put(name, totalFlow);
// sumByName.put(name, sumByName.getOrDefault(name, 0) + totalFlow);
}
}
// 输出结果
// 输出结果
// 正确创建新的映射对象并添加到list中
for (Map.Entry<String, Integer> entry : sumByName.entrySet()) {
Map<String, String> singleResult = new HashMap<>(); // 每次循环都创建一个新的映射
Map<String, String> singleResult = new LinkedHashMap<>(); // 每次循环都创建一个新的映射
singleResult.put("name", entry.getKey());
singleResult.put("value", entry.getValue().toString());
list.add(singleResult);
//System.out.println(entry.getKey() + " 的 total_flow 总和为: " + entry.getValue());
}
return list;
}
@ -705,35 +699,44 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
}
@Override
public List<Map<String, Object>> realTimeTrafficFlowHour(String startDate, Long direction) throws HttpException, IOException {
OkHttp okHttp = new OkHttp();
public Map<String,List<Map<String, Object>>> realTimeTrafficFlowHour() throws HttpException, IOException {
RequestParams requestParams = new RequestParams();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate currentDate = LocalDate.now();
String nowYear = currentDate.format(formatter);
//String nowYear =startDate;
// 获取一年前的日期
LocalDate oneYearAgo = currentDate.minusYears(1);
String lastYear = oneYearAgo.format(formatter);
OkHttp okHttp = new OkHttp();
RequestParams requestParams = new RequestParams();
requestParams.put("sysid", sysid);
//求情地址 startDate 时间
JSONArray body = getResponseBody(nowYear, requestParams, okHttp);
JSONObject parameters = new JSONObject() {
{
put("start_date", startDate);
put("end_date", startDate);
JSONArray bodylast = getResponseBody(lastYear, requestParams, okHttp);
Map<String,List<Map<String, Object>>> map = new HashMap<>();
if (body != null) {
List<Map<String, Object>> mapList = getMaps("1", body);
map.put("1",mapList);
List<Map<String, Object>> mapList2 = getMaps("3", body);
map.put("2",mapList2);
}
if (bodylast != null) {
List<Map<String, Object>> mapList = getMaps("1", bodylast);
map.put("3",mapList);
List<Map<String, Object>> mapList2 = getMaps("3", bodylast);
map.put("4",mapList2);
}
};
requestParams.put("parameters", parameters.toJSONString());
return map;
}
//处理 接口响应数据
private List<Map<String, Object>> getMaps(String direction, JSONArray jsonArray) throws IOException {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAccessToken());
Response response // 请求响应
= okHttp
.headers(headers)
.url(baseUrl + "/api/dc/query/gan_jihe_d_vehtypeflow") // 请求地址
.data(requestParams) // 请求参数
.post(); // 请求方法
ResponseBody body = response.body();
if (body != null) {
JSONArray jsonArray = JSON.parseArray(body.string());
// 获取当前时间
LocalTime now = LocalTime.now();
// 获取当前小时数(24小时制)
@ -755,7 +758,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
DcFacility dcFacilityAll = new DcFacility();
dcFacilityAll.setFacilityType(10);
dcFacilityAll.setDirection(String.valueOf(direction));
dcFacilityAll.setDirection(direction);
List<DcFacility> dcFacilityList = facilityService.listFacility(dcFacilityAll);
DcFacility dcFacility2 = facilityService.getfacilityCode(gantryId);
@ -786,10 +789,29 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
map.put("totalFlow", flowCounts.get(i));
mapList.add(map);
}
return mapList;
}
return null;
// 获取车流量接口 返回 jsonArray
private JSONArray getResponseBody(String startDate, RequestParams requestParams, OkHttp okHttp) throws HttpException, IOException {
JSONObject parameters = new JSONObject() {
{
put("start_date", startDate);
put("end_date", startDate);
}
};
requestParams.put("parameters", parameters.toJSONString());
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAccessToken());
Response response // 请求响应
= okHttp
.headers(headers)
.url(baseUrl + "/api/dc/query/gan_jihe_d_vehtypeflow") // 请求地址
.data(requestParams) // 请求参数
.post(); // 请求方法
ResponseBody body = response.body();
JSONArray jsonArray = JSON.parseArray(body.string());
return jsonArray;
}
@ -815,8 +837,19 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
}
return "";
}
/*
public JSONArray realTimeTrafficFlowHour2(String startDate,Long direction) throws HttpException, IOException {
public List<Map<String,Object>> queryTheGantryDataByPileNumber(String startDate,String stakeMark) throws HttpException, IOException {
if (stakeMark != null) {
//处理URl地址栏获取参数+号消失
stakeMark= stakeMark.replace(" ", "+");
}
List<Map<String,Object>> mapList =new ArrayList<>();
DcFacility dcFacilityAll = new DcFacility();
dcFacilityAll.setFacilityType(10);
List<DcFacility> dcFacilityList = facilityService.listFacility(dcFacilityAll);
DcFacility nearestFacility = findNearestFacility(dcFacilityList,stakeMark);
OkHttp okHttp = new OkHttp();
RequestParams requestParams = new RequestParams();
@ -845,41 +878,51 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
JSONArray jsonArray = JSON.parseArray(body.string());
// 使用HashMap来分组并求和
List<Map<String, Object>> list = new ArrayList();
for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object
System.out.println(jsonArray);
for (Object item : jsonArray) {
JSONObject jsonObject = (JSONObject) item;
// 获取当前时间
LocalTime now = LocalTime.now();
// 获取当前小时数(24小时制)
int currentHour = now.getHour();
if (jsonObject.getInteger("data_hour") == 16) {
if (jsonObject.getString("gantry_id").equals(nearestFacility.getFacilityCode())) {
Map<String, Object> map = new HashMap<>();
int totalFlow = jsonObject.getInteger("total_flow");
int data_hour = jsonObject.getInteger("data_hour");
map.put("hour",data_hour);
map.put("totalFlow",totalFlow);
mapList.add(map);
}
}
Map<String,Object> sumByName = new HashMap<>();
String name = jsonObject.getString("gantry_name"); // 更安全的取值方式
int totalFlow = jsonObject.getInteger("total_flow"); // 专门针对Integer类型
String gantryId = jsonObject.getString("gantry_id"); // 专门针对Integer类型
String data_hour = jsonObject.getString("data_hour"); // 专门针对Integer类型
// 输出结果
sumByName.put("naame", name);
sumByName.put("gantryId", gantryId);
sumByName.put("totalFlow", totalFlow);
sumByName.put("data_hour", data_hour);
list.add(sumByName);
return mapList;
}
return null;
}
public DcFacility findNearestFacility(List<DcFacility> dcFacilityList, String stakeMarkCode) {
// 将目标桩号转换为整数,以便比较
int targetCodeAsInt = Integer.parseInt(extract(stakeMarkCode));
DcFacility nearestFacility = null;
int minDistance = Integer.MAX_VALUE; // 初始化为最大整数值,确保任何实际距离都会小于这个值
// 输出结果
for (DcFacility facility : dcFacilityList) {
// 假设每个DcFacility对象中有一个方法或直接字段可以获取处理后的桩号字符串
String stakeMark = facility.getStakeMark(); // 请替换为实际的getter方法名
return jsonArray;
// 将当前设施的桩号转换为整数
int currentCodeAsInt = Integer.parseInt(extract(stakeMark));
// 计算与目标桩号的距离(差值的绝对值)
int distance = Math.abs(targetCodeAsInt - currentCodeAsInt);
// 如果当前桩号比之前记录的最近桩号更近,则更新最近桩号
if (distance < minDistance) {
minDistance = distance;
nearestFacility = facility;
}
}
return null;
return nearestFacility; // 返回最近的桩号信息
}
*/
}

Loading…
Cancel
Save