package com.casic.missiles.controller;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Test {
    public static void main(String[] args) throws IOException {
        //
        impJson();
//        System.out.println(System.currentTimeMillis());




    }

    public static void imp1Json() throws IOException {
        String json = "{ \n" +
                "\"dataType\":\"1\",\"thirdUserFieldList\":\n" +
                "[{\"text_field_name\":\"性别\",\"text_field\":\"sex\",\"field_type\":1},\n" +
                "{\"text_field_name\":\"姓名\",\n" +
                "\"text_field\":\"person_nick_name\",\"field_type\":1},\n" +
                "{\"text_field_name\":\"身份证号\",\n" +
                "\"text_field\":\"identity_card\",\"field_type\":1}],\n" +
                "\t\t\t\"personOrDeviceList\":\n" +
                "[" +
                "{\"identity_card\":\"371100197912110531\",\n" +
                "\"person_nick_name\":\"张三\",\n" +
                "\"sex\":1,\n" +
                "\"imei1\":123,\n" +
                "\"device_id\":123,\n" +
                "\"network_number\":456,\n" +
                "\"identity_card\":\"371100197912110532\",\n" +
                "\"organization_code\":\"110000000000\"\n" +
                "}" +
                "]," +
                "\"orgaList\":\n" +
                "[" +
                "{\"orgaCode\":\"110000000000\",\n" +
                "\"orgaName\":\"市公安局\",\t\t\n" +
                "\"superCode\":\"0\",\n" +
                "\"isSuper\":1\n" +
                "}" +
                "{\"orgaCode\":\"110101000000\",\n" +
                "\"orgaName\":\"东城分局\",\t\t\n" +
                "\"superCode\":\"110000000000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                "{\"orgaCode\":\"110102000000\",\n" +
                "\"orgaName\":\"西城分局\",\t\t\n" +
                "\"superCode\":\"110000000000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                "{\"orgaCode\":\"110091000000\",\n" +
                "\"orgaName\":\"天安门分局\",\t\t\n" +
                "\"superCode\":\"110000000000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                //西城分局
                "{\"orgaCode\":\"110102840000\",\n" +
                "\"orgaName\":\"国家大剧院派出所\",\t\t\n" +
                "\"superCode\":\"110102000000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                //西城分局
                "{\"orgaCode\":\"110102760000\",\n" +
                "\"orgaName\":\"府右街派出所\",\t\t\n" +
                "\"superCode\":\"110102000000\",\n" +
                "\"isSuper\":1\n" +
                "}," +

                "{\"orgaCode\":\"110102500000\",\n" +
                "\"orgaName\":\"西长安街派出所1\",\t\t\n" +
                "\"superCode\":\"110102000000\",\n" +
                "\"isSuper\":1\n" +
                "}" +

                "]\n" +
                "}";
        addJson("http://127.0.0.1:8083/sys/receiveSyncData", json, "utf-8");
    }

    public static void impJson() throws IOException {
        String json = "{ \n" +
                "\"dataType\":\"1\",\"thirdUserFieldList\":\n" +
                "[{\"text_field_name\":\"性别\",\"text_field\":\"sex\",\"field_type\":1},\n" +
                "{\"text_field_name\":\"姓名\",\n" +
                "\"text_field\":\"person_nick_name\",\"field_type\":1},\n" +
                "{\"text_field_name\":\"身份证号\",\n" +
                "\"text_field\":\"identity_card\",\"field_type\":1}],\n" +
                "\t\t\t\"personOrDeviceList\":\n" +
                "[" +
                "{\"identity_card\":\"371100197912110531\",\n" +
                "\"person_nick_name\":\"张三\",\n" +
                "\"sex\":1,\n" +
                "\"imei1\":123,\n" +
                "\"device_id\":123,\n" +
                "\"network_number\":456,\n" +
                "\"organization_code\":\"110000000000\"\n" +
                "}" +
                ",{" +
                "\"person_nick_name\":\"王五\",\n" +
                "\"sex\":1,\n" +
                "\"imei1\":678,\n" +
                "\"device_id\":678,\n" +
                "\"network_number\":4568,\n" +
                "\"identity_card\":\"371100197912110532\",\n" +
                "\"organization_code\":\"110102760500\"\n" +
                "}" +
                "],\"orgaList\":\n" +
                "[" +
                "{\"orgaCode\":\"110000000000\",\n" +
                "\"orgaName\":\"市公安局\",\t\t\n" +
                "\"superCode\":\"0\",\n" +
                "\"isSuper\":1\n" +
                "}" +
                "{\"orgaCode\":\"110101000000\",\n" +
                "\"orgaName\":\"东城分局\",\t\t\n" +
                "\"superCode\":\"110000000000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                "{\"orgaCode\":\"110102000000\",\n" +
                "\"orgaName\":\"西城分局\",\t\t\n" +
                "\"superCode\":\"110000000000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                "{\"orgaCode\":\"110091000000\",\n" +
                "\"orgaName\":\"天安门分局\",\t\t\n" +
                "\"superCode\":\"110000000000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                //西城分局
                "{\"orgaCode\":\"110102840000\",\n" +
                "\"orgaName\":\"国家大剧院派出所\",\t\t\n" +
                "\"superCode\":\"110102000000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                //西城分局
                "{\"orgaCode\":\"110102760000\",\n" +
                "\"orgaName\":\"府右街派出所\",\t\t\n" +
                "\"superCode\":\"110102000000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                "{\"orgaCode\":\"110102760200\",\n" +
                "\"orgaName\":\"一中队(综合队)\",\t\t\n" +
                "\"superCode\":\"110102760000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                "{\"orgaCode\":\"110102760400\",\n" +
                "\"orgaName\":\"二中队(治安审查队)\",\t\t\n" +
                "\"superCode\":\"110102760000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                "{\"orgaCode\":\"110102760500\",\n" +
                "\"orgaName\":\"三中队(新华门队)\",\t\t\n" +
                "\"superCode\":\"110102760000\",\n" +
                "\"isSuper\":1\n" +
                "}," +
                "{\"orgaCode\":\"110102500000\",\n" +
                "\"orgaName\":\"西长安街派出所1\",\t\t\n" +
                "\"superCode\":\"110102000000\",\n" +
                "\"isSuper\":1\n" +
                "}" +

                "]\n" +
                "}";
        addJson("http://127.0.0.1:8083/sys/receiveSyncData", json, "utf-8");
    }

    /**
     * uri 请求路径
     * objStr 推送的数据体
     * charset编码格式 UTF-8
     **/
    public static String addJson(String uri, String objStr, String charset) throws IOException {
        StringBuffer sb = new StringBuffer("");
        try {
            // 创建连接
            URL url = new URL(uri);
            System.out.println("uri=" + uri);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.connect();
            // POST请求
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            System.out.println("推送的数据：" + objStr);
            out.write(objStr.getBytes(charset));// 这样可以处理中文乱码问题
            out.flush();
            out.close();
            // 读取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String lines;
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), "utf-8");
                sb.append(lines);
            }
            System.out.println(sb);
            reader.close();
            // 断开连接
            connection.disconnect();
        } catch (MalformedURLException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sb.toString();
    }

}
