|
@@ -0,0 +1,124 @@
|
|
|
+package com.nokia.finance.tasks.jobs.car.procedure;
|
|
|
+
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.reflect.TypeToken;
|
|
|
+import com.nokia.finance.tasks.dao.common.RequestLogDao;
|
|
|
+import com.nokia.finance.tasks.pojo.po.common.CarPhpRequestLogPo;
|
|
|
+import com.nokia.finance.tasks.pojo.po.common.RequestLogPo;
|
|
|
+import com.nokia.finance.tasks.utils.AESUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.util.UriComponents;
|
|
|
+import org.springframework.web.util.UriComponentsBuilder;
|
|
|
+
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+
|
|
|
+ * 车辆php页面请求日志入库定时任务
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class CarPhpRequestLogJob {
|
|
|
+ private final RequestLogDao requestLogDao;
|
|
|
+
|
|
|
+ public CarPhpRequestLogJob(RequestLogDao requestLogDao) {
|
|
|
+ this.requestLogDao = requestLogDao;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 执行任务
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "0 30 * * * ?")
|
|
|
+ public void runJob() {
|
|
|
+ log.info("执行车辆php页面请求日志入库定时任务");
|
|
|
+ try {
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ Gson gson = new Gson();
|
|
|
+
|
|
|
+
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ LocalDateTime startLocalDateTime = now.minusHours(1).withMinute(0).withSecond(0).withNano(0);
|
|
|
+ LocalDateTime endLocalDateTime = startLocalDateTime.plusHours(1);
|
|
|
+ Long startTime = startLocalDateTime.toEpochSecond(ZoneOffset.ofHours(8));
|
|
|
+ Long endTime = endLocalDateTime.toEpochSecond(ZoneOffset.ofHours(8));
|
|
|
+ List<CarPhpRequestLogPo> carPhpRequestLogPoList = requestLogDao.listCarPhpRequestLog(startTime, endTime);
|
|
|
+ List<RequestLogPo> list = new ArrayList<>();
|
|
|
+ for (CarPhpRequestLogPo carPhpRequestLogPo : carPhpRequestLogPoList) {
|
|
|
+ LocalDateTime requestTime = carPhpRequestLogPo.getRequestTime();
|
|
|
+ String queryString = carPhpRequestLogPo.getQueryString();
|
|
|
+ if (requestTime == null || !StringUtils.hasText(queryString)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ UriComponents uriComponents = UriComponentsBuilder.fromUriString("?" + queryString).build();
|
|
|
+ MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
|
|
|
+ Map<String, String> singleValueMap = queryParams.toSingleValueMap();
|
|
|
+ String data = singleValueMap.get("data");
|
|
|
+ if (!StringUtils.hasText(data)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String urlDecodeToken = URLDecoder.decode(data, StandardCharsets.UTF_8);
|
|
|
+ String decodeToken;
|
|
|
+ try {
|
|
|
+ decodeToken = AESUtil.decrypt(urlDecodeToken);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("token解密失败: {} -> {} -> {}", urlDecodeToken, queryString, e.getMessage(), e);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Map<String, String> tokenMap = gson.fromJson(decodeToken, new TypeToken<Map<String, String>>() {
|
|
|
+ }.getType());
|
|
|
+ String loginId = tokenMap.get("LOGIN_ID");
|
|
|
+ String pageUrl = tokenMap.get("REQUEST_URL");
|
|
|
+ String api = StringUtils.hasText(singleValueMap.get("type")) ? pageUrl + "?type=" + singleValueMap.get("type") : pageUrl;
|
|
|
+ Map<String, String> requestParameters = new HashMap<>();
|
|
|
+ requestParameters.put("request_parameters", gson.toJson(singleValueMap));
|
|
|
+ String appId = tokenMap.get("APP_ID");
|
|
|
+ RequestLogPo requestLogPo = new RequestLogPo();
|
|
|
+ requestLogPo.setRequestTime(carPhpRequestLogPo.getRequestTime());
|
|
|
+ requestLogPo.setLoginId(loginId);
|
|
|
+ requestLogPo.setPageUrl(pageUrl);
|
|
|
+ requestLogPo.setApi(api);
|
|
|
+ requestLogPo.setRequestParameters(gson.toJson(requestParameters));
|
|
|
+ requestLogPo.setAppId(appId);
|
|
|
+ setTimeStamp(tokenMap, requestLogPo);
|
|
|
+ requestLogPo.setToken(urlDecodeToken);
|
|
|
+ list.add(requestLogPo);
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ requestLogDao.insertBatch(list);
|
|
|
+ }).get(1, TimeUnit.MINUTES);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ log.error("线程中断: {}", e.getMessage(), e);
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setTimeStamp(Map<String, String> map, RequestLogPo requestLogPo) {
|
|
|
+ String timeStamp = map.get("TIME_STAMP");
|
|
|
+ if (StringUtils.hasText(timeStamp)) {
|
|
|
+ try {
|
|
|
+ requestLogPo.setTimeStamp(LocalDateTime.parse(map.get("TIME_STAMP"),
|
|
|
+ DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("时间戳解析失败: {}", timeStamp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|