From be16ab024083da4ea682fe1c4c018b8076f7f5c5 Mon Sep 17 00:00:00 2001 From: hupeng Date: Thu, 1 Nov 2018 12:46:37 +0800 Subject: [PATCH] =?UTF-8?q?[=E7=B3=BB=E7=BB=9F][=E4=BF=AE=E6=94=B9]:?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E6=8B=89=E5=8F=96PVUV=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=92=8C=E9=80=BB=E8=BE=91=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iformall/schedule/WxAppVisitSchedule.java | 55 ++++++++++--------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/mallinkSchedule/src/main/java/com/iformall/schedule/WxAppVisitSchedule.java b/mallinkSchedule/src/main/java/com/iformall/schedule/WxAppVisitSchedule.java index 272ba4208..cf5637c7b 100644 --- a/mallinkSchedule/src/main/java/com/iformall/schedule/WxAppVisitSchedule.java +++ b/mallinkSchedule/src/main/java/com/iformall/schedule/WxAppVisitSchedule.java @@ -42,17 +42,13 @@ public class WxAppVisitSchedule { @Autowired private WxAppinfoService WxAppinfoService; - - -// @Scheduled(cron = "0 */1 * * * *?") - @Scheduled(cron = "0 0 9 * * ? ") + + + //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次 + @Scheduled(cron = "0 0 2,3,4 * * ? ") //2点,3点,4点各做一次 public void start() { try { - Calendar c =Calendar.getInstance(); - c.add(Calendar.DAY_OF_YEAR, -1); - Date time = c.getTime(); - String yesterday = new SimpleDateFormat("yyyyMMdd").format(time); -// "wx8eb8275b78db4ede", "76c43df01296998d8ce12383f213ac10"; + WxAppinfo appInfo = new WxAppinfo(); appInfo.setType(EnumAppType.C.getCode()); PageInfo page = WxAppinfoService.listAsPage(appInfo, 1, 10000); @@ -60,25 +56,38 @@ public class WxAppVisitSchedule { if(StringUtils.isBlank(w.getAppId())||StringUtils.isBlank(w.getSecret())) { continue; } - getData(yesterday,w.getAppId(),w.getSecret(),w.getTenantId()); + Calendar c = Calendar.getInstance(); + String date; + for(int i = 0; i < 7; i++) { //查找最近7天有没有漏掉的数据 + c.add(Calendar.DAY_OF_YEAR, -1); + date = new SimpleDateFormat("yyyyMMdd").format(c.getTime()); + if (!checkData(date ,w.getAppId(), w.getTenantId())) + getData(date,w.getAppId(),w.getSecret(),w.getTenantId()); + } } -// TODO hardcode appId -// String appId = "wx8eb8275b78db4ede"; -// String key ="76c43df01296998d8ce12383f213ac10"; -// String talentId ="456"; -// getData(yesterday,appId,key,talentId); }catch(Exception e) { logger.error("获取微信访问数据失败",e); } } + + private boolean checkData(String date,String appId, String tenantId) { + WxUserVisit wxUserVisit = new WxUserVisit(); + wxUserVisit.setAppId(appId); + wxUserVisit.setTenantId(tenantId); + wxUserVisit.setRefDate(date); + PageInfo page = wxUserVisitService.listAsPage(wxUserVisit, 1, 10000); + if (page.getList().size() > 0) + return true; + return false; + } - private void getData(String yesterday,String appId,String key,String talentId) throws Exception { + private void getData(String date,String appId,String key,String talentId) throws Exception { String accessToken = getAccessToken(appId, key); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); Map map = new HashMap(); - map.put("begin_date", yesterday); - map.put("end_date",yesterday); + map.put("begin_date", date); + map.put("end_date",date); RestTemplate restTemplate = new RestTemplate(); HttpEntity> entity = new HttpEntity>(map, headers); String reqUrl =visit+accessToken; @@ -89,21 +98,18 @@ public class WxAppVisitSchedule { Map maps = (Map)JSON.parse(body); JSONArray jSONArray = (JSONArray)maps.get("list"); if(jSONArray==null || jSONArray.isEmpty()) { - logger.info("获取失败"); + logger.error("获取数据失败:"+body); return; } JSONObject jsonObject = jSONArray.getJSONObject(0); Map itemMap = JSONObject.toJavaObject(jsonObject, Map.class); - //{"visit_uv":6,"stay_time_uv":1178.3333,"stay_time_session":115.9016,"ref_date":"20180828", - // "visit_depth":3.1311,"session_cnt":61,"visit_pv":645,"visit_uv_new":3} logger.info(JSON.toJSONString(itemMap)); WxUserVisit v = new WxUserVisit(); // TODO hardcode appId -// v.setAppId("wx8eb8275b78db4ede"); v.setAppId(appId); String time = itemMap.get("ref_date")+""; - Date date = new SimpleDateFormat("yyyyMMdd").parse(time); - v.setDayDate(date); + Date dayDate = new SimpleDateFormat("yyyyMMdd").parse(time); + v.setDayDate(dayDate); v.setRefDate(time); v.setSessionCnt(Integer.valueOf(itemMap.get("session_cnt")+"")); v.setVisitPv(Integer.valueOf(itemMap.get("visit_pv")+"")); @@ -119,7 +125,6 @@ public class WxAppVisitSchedule { } private String getAccessToken(String appId,String appSecret) { -// return "13_rBo3ajS3jjd8OXZ2MLd4HfLrmt78gvaCeRtu-Xme0iC0fhs_lNS47aLPEwI8kfZQIMKnWYshY5wpaf2IoSI7tgVBm7WwVrm_Bg96J31VPKi8pEp8yB6JiTpDcWkpwv5GngiH2vDkwz7VHOsPLBYcAGAZPM"; String token="https://api.weixin.qq.com/cgi-bin/token?"+ "grant_type=client_credential&appid=APPID&secret=APPSECRET"; String url = token.replace("APPID", appId).