路泰机电科技体检——数据平台后端
赵文轩
2024-06-04 01d883058b2fba499dd898abff39c8b843597e6f
src/main/java/com/example/utils/DictionaryUtil.java
@@ -26,7 +26,8 @@
@Component
public class DictionaryUtil {
//    private static final String CONFIG_PATH = "/Users/chacca/开发相关/代码/ltkj_peis_sjpt/src/main/resources/config.properties";
    private static final String CONFIG_PATH = "/Users/chacca/开发相关/代码/ltkj_peis_sjpt/src/main/resources/ltkj.properties";
//    private static final String CONFIG_PATH = "/Users/chacca/开发相关/代码/ltkj_peis_sjpt/src/main/resources/ltkj.properties";
    private static final String CONFIG_PATH = "D:\\ltkjprojectconf\\config.properties";
    private static String BASE_API_URL = "";
    // 请求代码总集合
    private static List<String> methods = new ArrayList<>();
@@ -42,6 +43,9 @@
// 分页每次1000条
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {}
        methods.add("JGDM");
        methods.add("ZDZYZ");
        methods.add("YQZD");
@@ -158,7 +162,16 @@
        return jsonObject;
    }
    public void execSync(){
    }
    /**
     * 单线程执行
     * @throws IOException
     */
    public void exec1() throws IOException {
        log.info("开始执行同步");
        int size = 1000;
        FileInputStream inputStream = new FileInputStream(CONFIG_PATH);
        Properties props = new Properties();
@@ -193,6 +206,11 @@
            assert entries != null;
            JSONObject response = entries.getJSONObject("Response");
            if ("0".equals(response.getStr("ResultCode"))) {
                try {
                    dropTable(tabName,connection);
                } catch (SQLException e) {
                    log.error("删除表异常 ->{}",e.getMessage());
                }
                if (isLimit) {
                    maxPage = LimitInsertData(connection, tabName, response,name,page);
                    log.info("计算页码为 ->{}", maxPage);
@@ -226,6 +244,7 @@
                log.error("{}请求失败 - 请求状态码不为0,请求参数 ->{},请求返回 ->{}", method,object.toString(),response.toString());
            }
        });
        log.info("执行同步完毕");
    }
    /**
@@ -290,11 +309,11 @@
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Accept", "application/json");
//            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Type", "application/json;utf-8");
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            connection.setDoOutput(true);
            OutputStream stream = connection.getOutputStream();
            stream.write(params.getBytes(StandardCharsets.UTF_8));
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),StandardCharsets.UTF_8));
            StringBuilder builder = new StringBuilder();
            String str;
            while ((str = reader.readLine()) != null) {
@@ -389,6 +408,8 @@
        insertSqlBuilder.append("insert into ").append(tabName).append(" (");
        selectSqlBuilder.append("select count(1) as count from ").append(tabName).append(" where ");
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            if (entry.getValue().toString().equals("null"))
                continue;
            insertSqlBuilder.append(entry.getKey()).append(", ");
            valueBuilder.append("'").append(entry.getValue().toString().replaceAll("'","‘")).append("', ");
            selectSqlBuilder.append(entry.getKey()).append(" = '").append(entry.getValue().toString().replaceAll("'","‘")).append("' and ");
@@ -457,4 +478,16 @@
        Statement statement = connection.createStatement();
        statement.execute(sql.toString());
    }
    /**
     * 删除表
     * @param tabName
     * @param connection
     * @throws SQLException
     */
    private void dropTable(String tabName,Connection connection) throws SQLException {
        String sql = "DROP TABLE IF EXISTS " + tabName;
        Statement statement = connection.createStatement();
        statement.executeUpdate(sql);
    }
}