| | |
| | | package com.integrated.zyyt.util; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.http.HttpRequest; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.integrated.zyyt.ZyytConstant; |
| | | import com.integrated.zyyt.enetity.TokenResult; |
| | | import com.integrated.zyyt.enetity.ZyytDataResult; |
| | | import com.integrated.zyyt.enetity.business.Zyyt; |
| | | import com.integrated.zyyt.enetity.business.ZyytStationInfo; |
| | | import com.integrated.zyyt.enetity.business.ZyytTDjtjb; |
| | | import com.integrated.zyyt.enetity.business.ZyytTShkdrb; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.jdbc.core.JdbcTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @ClassName ZyytGetData |
| | |
| | | @Component |
| | | @Slf4j |
| | | public class ZyytUtil { |
| | | private static JdbcTemplate jdbcTemplate; |
| | | |
| | | @Autowired |
| | | public void setAlphaService(JdbcTemplate jdbcTemplate) { |
| | | this.jdbcTemplate = jdbcTemplate; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取批次号 |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 查询执行日期是否在执行 |
| | | * |
| | | * @param tableName |
| | | * @param date |
| | | */ |
| | | public static Map<String, Object> getLastRecord(String tableName, LocalDate date) { |
| | | String sql = "SELECT*FROM (SELECT*FROM ZYYT_RECORD WHERE BATCH_NO='" + getBatchNo(date) + "' AND TABLE_NAME='" + tableName + "' ORDER BY START_TIME DESC) tmp WHERE ROWNUM<=1"; |
| | | Map<String, Object> map = null; |
| | | try { |
| | | map = jdbcTemplate.queryForMap(sql); |
| | | } catch (Exception e) { |
| | | log.error("sql=={} 查询发生了异常,可能因为没有查询到数据", sql); |
| | | log.error(e.getMessage()); |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 查询指定批次是否有正在运行的任务 |
| | | * |
| | | * @param tableName |
| | | * @param date |
| | | * @return true 正在运行 false 没有运行 |
| | | */ |
| | | public static boolean isRunning(String tableName, LocalDate date) { |
| | | Map<String, Object> lastRecord = getLastRecord(tableName, date); |
| | | if (lastRecord != null) { |
| | | String isFinish = Convert.toStr(lastRecord.get("IS_FINISH")); |
| | | return !"1".equals(isFinish); |
| | | } else { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断指定日期是否需要重新执行 |
| | | * |
| | | * @param tableName |
| | | * @param date |
| | | * @return true 需要执行,false 不需要 |
| | | */ |
| | | public static boolean canGetAgain(String tableName, LocalDate date) { |
| | | Map<String, Object> map = getLastRecord(tableName, date); |
| | | if (map != null) { |
| | | String isFinish = Convert.toStr(map.get("IS_FINISH")); |
| | | if (!"1".equals(isFinish)) { |
| | | log.info("表:{} 日期: {},上次执行未完成,不执行拉取", tableName, date); |
| | | return false; |
| | | } |
| | | //下面 isFinish都是1 |
| | | String isError = Convert.toStr(map.get("IS_ERROR")); |
| | | if ("1".equals(isError)) { |
| | | log.info("表:{} 日期: {},上次执行有误,执行拉取", tableName, date); |
| | | return true; |
| | | } |
| | | |
| | | // 成功执行没有错 |
| | | String countNum = Convert.toStr(map.get("COUNT_NUM")); |
| | | // 同接口返回的最新业务数据条数比较,如果不相同则重新拉取 |
| | | Long apiTotalElements = getApiTotalElements(tableName, date); |
| | | if (apiTotalElements == null) { |
| | | log.error("API获取总数失败,不执行拉取"); |
| | | return false; |
| | | } |
| | | if (apiTotalElements.compareTo(Convert.toLong(countNum)) == 0) { |
| | | log.info("API返回总数 与 数据库条数相同,不执行拉取"); |
| | | return false; |
| | | } else { |
| | | log.info("API返回总数 与 数据库条数 不相同,执行拉取"); |
| | | return true; |
| | | } |
| | | } else { |
| | | // 该批次未执行 |
| | | log.info("表:{} 日期: {},未执行过", tableName, date); |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | |
| | | public static Zyyt getBusinessEntity(String tableName, LocalDate date) { |
| | | if ("STATIONINFO".equalsIgnoreCase(tableName)) { |
| | | new ZyytStationInfo( |
| | | ZyytConstant.URL_AUTHON, |
| | | ZyytConstant.URL_STATIONINFO_QUERY, |
| | | false, |
| | | ZyytConstant.CLIENT_STATIONINFO, |
| | | ZyytConstant.SECRET_STATIONINFO, |
| | | ZyytConstant.X_EOS_SOURCESYSKEY_STATIONINFO, |
| | | new JSONObject() |
| | | ); |
| | | } else { |
| | | JSONObject queryParams = new JSONObject(); |
| | | JSONObject criteria = new JSONObject(); |
| | | criteria.put("match", "and"); |
| | | |
| | | JSONArray matchChildren = new JSONArray(); |
| | | JSONObject children1 = new JSONObject(); |
| | | children1.put("name", "dRbrq"); |
| | | JSONArray child1C = new JSONArray(); |
| | | child1C.add(date + " 00:00:00"); |
| | | children1.put("value", child1C); |
| | | children1.put("match", "eq"); |
| | | matchChildren.add(children1); |
| | | |
| | | criteria.put("children", matchChildren); |
| | | |
| | | queryParams.put("criteria", criteria); |
| | | if ("YYZT_T_SHKDRB".equalsIgnoreCase(tableName)) { |
| | | |
| | | return new ZyytTShkdrb( |
| | | ZyytConstant.URL_AUTHON, |
| | | ZyytConstant.URL_SHKDRB_QUERY, |
| | | false, |
| | | ZyytConstant.CLIENT_SHKDRB, |
| | | ZyytConstant.SECRET_SHKDRB, |
| | | ZyytConstant.X_EOS_SOURCESYSKEY_SHKDRB, |
| | | queryParams |
| | | ); |
| | | } else if ("YYZT_T_DJTJB".equalsIgnoreCase(tableName)) { |
| | | return new ZyytTDjtjb( |
| | | ZyytConstant.URL_AUTHON, |
| | | ZyytConstant.URL_DJTJB_QUERY, |
| | | false, |
| | | ZyytConstant.CLIENT_DJTJB, |
| | | ZyytConstant.SECRET_DJTJB, |
| | | ZyytConstant.X_EOS_SOURCESYSKEY_DJTJB, |
| | | queryParams |
| | | ); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 获取API中返回的记录总数 |
| | | * |
| | | * @param tableName |
| | | * @param date |
| | | * @return |
| | | */ |
| | | public static Long getApiTotalElements(String tableName, LocalDate date) { |
| | | Zyyt businessEntity = getBusinessEntity(tableName, date); |
| | | ZyytDataResult zyytDataResult = businessEntity.getData(0, 1); |
| | | log.info("总共拉取到数据{}条", zyytDataResult.getTotalElements()); |
| | | return zyytDataResult.getTotalElements(); |
| | | } |
| | | |
| | | public static Long calcPages(Long totalElements, int pageSize) { |
| | | Long totalPages = 0L; |
| | | totalPages = totalElements / pageSize; |
| | | if (totalElements % pageSize != 0) { |
| | | totalPages++; |
| | | } |
| | | return totalPages; |
| | | } |
| | | |
| | | } |