From ffdc01ef7492a69635ba8cb634e63429a9e69ade Mon Sep 17 00:00:00 2001 From: haungweixiong Date: Mon, 28 Nov 2022 16:45:23 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AB=98=E5=BE=B7=E7=83=AD=E5=8A=9B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/GaodeController.java | 168 ++++++++++++++++-- .../monitormanage/entity/CrowdFlow.java | 4 +- src/main/resources/application.properties | 6 +- 3 files changed, 156 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/hisense/monitormanage/controller/GaodeController.java b/src/main/java/com/hisense/monitormanage/controller/GaodeController.java index 7032d5b..50342a0 100644 --- a/src/main/java/com/hisense/monitormanage/controller/GaodeController.java +++ b/src/main/java/com/hisense/monitormanage/controller/GaodeController.java @@ -1,8 +1,11 @@ package com.hisense.monitormanage.controller; +import cn.hutool.core.date.DateField; +import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.crypto.SecureUtil; +import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.OrderItem; @@ -23,6 +26,9 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; @RestController @RequestMapping("api/gaode") @@ -58,7 +64,7 @@ public class GaodeController { } Page flowPage = new Page<>(); - flowPage.addOrder(OrderItem.asc("id")); + flowPage.addOrder(OrderItem.desc("id")); if (page != null && page > 0) { flowPage.setCurrent(page); } @@ -67,22 +73,75 @@ public class GaodeController { } Page result = crowdFlowMapper.selectPage(flowPage, queryWrapper); - return Result.success(result.getRecords()); + Result success = Result.success(result.getRecords()); + success.setCount((int) result.getTotal()); + return success; } - @GetMapping("fetchGaodeData") - public void fetchGaodeDataByHand() { - fetchGaodeData(); - } - - /** - * 从0开始每隔15分钟执行一次,也就是0,15,30,45 + * 手动刷一条数据 + * @param date 格式yyyyMMddHHmm 202211261700 */ - @Scheduled(cron = "0 0/15 * * * ?") - public void fetchGaodeData() { - + @GetMapping("fetchGaodeData") + public void fetchGaodeDataByHand(String date) { Date currentDate = new Date(); + if (date != null) { + currentDate = DateUtil.parse(date, "yyyyMMddHHmm"); + } + Integer cnt = _fetchGaodeData(currentDate, "total_flow"); + Integer visitCnt = _fetchGaodeData(currentDate, "visit_flow"); + + String total_flow = _fetchGaodeHotData(currentDate, "total_flow"); + System.out.println(total_flow); + LambdaQueryWrapper queryWrapper = new QueryWrapper().lambda() + .eq(CrowdFlow::getCaptureTime, currentDate); + CrowdFlow entity = crowdFlowMapper.selectOne(queryWrapper); + if (entity == null){ + CrowdFlow crowdFlow = new CrowdFlow(); + crowdFlow.setCaptureTime(currentDate); + crowdFlow.setCnt(cnt); + crowdFlow.setVisitCnt(visitCnt); + crowdFlowMapper.insert(crowdFlow); + }else if (cnt != entity.getCnt() || visitCnt != entity.getVisitCnt()){ + entity.setCnt(cnt); + entity.setVisitCnt(visitCnt); + crowdFlowMapper.updateById(entity); + } + } + + /** + * 刷一遍所有数据 + * + */ + @GetMapping("fixGaodeData") + public void fixGaodeDataByHand() throws InterruptedException { + + Page flowPage = new Page<>(); + flowPage.addOrder(OrderItem.asc("id")); + Page crowdFlowPage = null; + do { + + crowdFlowPage = crowdFlowMapper.selectPage(flowPage, null); + List crowdFlows = crowdFlowPage.getRecords(); + for (CrowdFlow crowdFlow : crowdFlows) { + Integer cnt = _fetchGaodeData(crowdFlow.getCaptureTime(), "total_flow"); + Integer visitCnt = _fetchGaodeData(crowdFlow.getCaptureTime(), "visit_flow"); + //接口有限流,调一会就不通了,延时一会 + Thread.sleep(4 * 1000); + if (crowdFlow.getVisitCnt() != visitCnt) { + crowdFlow.setVisitCnt(visitCnt); + } + if (crowdFlow.getCnt() != cnt) { + crowdFlow.setCnt(cnt); + crowdFlowMapper.updateById(crowdFlow); + } + } + flowPage.setCurrent(flowPage.getCurrent()+1); + + }while (crowdFlowPage.hasNext()); + } + + public Integer _fetchGaodeData(Date currentDate, String flowType) { StringBuilder url = new StringBuilder(gaodeUrl + "/v4/biolap/abapi/query"); url.append('?'); TreeMap parame = new TreeMap<>(); @@ -92,8 +151,9 @@ public class GaodeController { parame.put("apiid","490"); parame.put("areaId","WY00017X7U"); parame.put("timeType","15m"); - parame.put("flowType","total_flow"); - parame.put("ds", DateUtil.format(currentDate, "yyyyMMddhhmmss")); + parame.put("flowType", flowType); + String ds = DateUtil.format(currentDate, "yyyyMMddHHmm"); + parame.put("ds", ds); parame.put("timestamp", new Date().getTime() / 1000); StringBuilder builder = new StringBuilder(); parame.forEach((key, value) -> { @@ -105,20 +165,92 @@ public class GaodeController { parame.put("token", token); url.append("token={token}"); HashMap forObject = restTemplate.getForObject(url.toString(), HashMap.class, parame); + System.out.println(JSON.toJSONString(forObject)); if ((Integer) forObject.get("errcode") == 0) { Map data = (Map) forObject.get("data"); if (data != null) { Map indexData = (Map) data.get("index_data"); List query = (List) indexData.get("query"); if (query != null && query.size() == 1) { - CrowdFlow crowdFlow = new CrowdFlow(); - crowdFlow.setCaptureTime(currentDate); - crowdFlow.setCnt((Integer) query.get(0).get("index")); - crowdFlowMapper.insert(crowdFlow); + return (Integer) query.get(0).get("index"); } } } + + return null; + } + + public String _fetchGaodeHotData(Date currentDate, String flowType) { + StringBuilder url = new StringBuilder(gaodeUrl + "/v4/biolap/abapi/query"); + url.append('?'); + TreeMap parame = new TreeMap<>(); + parame.put("key","a7c2748d8af3393b6fa0531023d2b8c8"); + + parame.put("appname","dz-qingdao-chengguan"); + parame.put("apiid","484"); + parame.put("areaId","WY00017X7U"); + parame.put("timeType","15m"); + parame.put("flowType", flowType); + String ds = DateUtil.format(currentDate, "yyyyMMddHHmm"); + parame.put("ds", ds); + parame.put("geoLength", 10); + parame.put("timestamp", new Date().getTime() / 1000); + StringBuilder builder = new StringBuilder(); + parame.forEach((key, value) -> { + builder.append(value + ";"); + url.append(key + "={" + key + "}&"); + }); + String secretkey = "4c32a89db1f20b222e08919b29567368"; + String token = SecureUtil.md5(builder.toString() + secretkey); + parame.put("token", token); + url.append("token={token}"); + HashMap forObject = restTemplate.getForObject(url.toString(), HashMap.class, parame); + System.out.println(JSON.toJSONString(forObject)); + if ((Integer) forObject.get("errcode") == 0) { + Map data = (Map) forObject.get("data"); + if (data != null) { + Map indexData = (Map) data.get("index_data"); + Map analysis = (Map) indexData.get("analysis"); + if (analysis != null && analysis.get("area_data") != null) { + return JSON.toJSONString(analysis.get("area_data")); + } + } + + + } + + return null; + } + + + + /** + * 从0开始每隔15分钟执行一次,也就是0,15,30,45 + * 为了查询准确需要查上一个15分钟 + */ + @Scheduled(cron = "0 0/15 * * * ?") + public void fetchGaodeData() { + + System.out.println(Thread.currentThread().getName()); + DateTime dateTime = DateUtil.date().setField(DateField.MILLISECOND, 0); + DateTime currentDate = DateUtil.offsetMinute(dateTime, -15); + + Integer cnt = _fetchGaodeData(currentDate, "total_flow"); + if (cnt == null){ + cnt = 0; + } + + Integer visitCnt = _fetchGaodeData(currentDate, "visit_flow"); + if (visitCnt == null) { + visitCnt = 0; + } + CrowdFlow crowdFlow = new CrowdFlow(); + crowdFlow.setCaptureTime(currentDate); + crowdFlow.setCnt(cnt); + crowdFlow.setVisitCnt(visitCnt); + crowdFlowMapper.insert(crowdFlow); + } } diff --git a/src/main/java/com/hisense/monitormanage/entity/CrowdFlow.java b/src/main/java/com/hisense/monitormanage/entity/CrowdFlow.java index ef44e76..a86dbb5 100644 --- a/src/main/java/com/hisense/monitormanage/entity/CrowdFlow.java +++ b/src/main/java/com/hisense/monitormanage/entity/CrowdFlow.java @@ -16,11 +16,13 @@ import java.util.Date; public class CrowdFlow { @TableId - private long id; + private Long id; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date captureTime; private Integer cnt; + private Integer visitCnt; + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c7816e1..1a7cba3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,11 +9,11 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.name=defaultDataSource # 数据库连接地址 #spring.datasource.url=jdbc:mysql://15.72.183.91:3306/monitor_manage?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 -spring.datasource.url=jdbc:mysql://15.72.183.91:3306/monitor_manage?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=true&serverTimezone=GMT%2B8 +spring.datasource.url=jdbc:mysql://127.0.0.1:3306/monitor_manage?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=true&serverTimezone=GMT%2B8 # 数据库用户名&密码: spring.datasource.username=root -spring.datasource.password=w@CmM1mBVQkPhdrc -#spring.datasource.password=123456 +#spring.datasource.password=w@CmM1mBVQkPhdrc +spring.datasource.password=123456 hwx.file.work-path=D:/tupian/ hwx.file.pic-host=http://127.0.0.1:7009 spring.resources.static-locations=classpath:/static,classpath:/public,file:${hwx.file.work-path}