package com.lutao.cloud.admin.mq;


import com.google.gson.Gson;
import com.google.gson.JsonArray;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: vincent.zhao
 * @create: 2023-07-28 11:58
 **/
public class JsonToSqlTest {

    private static final String PATH = "D:/work/810/应急管理部-静态数据-json/专业设备.json";

    //D:\work\810\应急管理部-静态数据-json

    private static final String filename = "专业设备";
    String tablename = "";

    public static void main(String[] args) throws Exception {

        StringBuilder sb = new StringBuilder();
        try {
            InputStream input = new FileInputStream(PATH);

            byte[] buffer = new byte[1024];
            int length = 0;
            length = input.read(buffer);

            while (length != -1) {
                sb.append(new String(buffer, 0, length));
                length = input.read(buffer);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("========== JSON ---> 转换成 SQL 开始 ==========");
        jsonToExcel(sb.toString());
        System.out.println("========== JSON ---> 转换成 SQL 结束 ==========");
    }

    private static void jsonToExcel(String str) throws Exception {


        Gson gson = new Gson();
        JsonArray jsonArray = gson.fromJson(str, JsonArray.class);
        StringBuffer ret = new StringBuffer();

        for (int i = 0; i < jsonArray.size(); i++) {

            Map<String, Object> map = gson.fromJson(jsonArray.get(i), Map.class);
            List<Object> list = map.values().stream().collect(Collectors.toList());

            StringBuffer sb = new StringBuffer("('zhonghaiyou',");
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j) instanceof String) {
                    sb.append("'");
                    sb.append(list.get(j) + "'");
                    if (j != list.size() -1) {
                        sb.append(",");
                    }
                } else {
                    if (j != list.size() -1) {
                        sb.append(list.get(j) + ",");
                    }
                }
            }

            sb.toString().substring(0, sb.toString().length() - 1);
            if (i == jsonArray.size() -1) {
                sb.append(")");
            } else {
                sb.append("),");
            }
            System.out.println(sb.toString());
            ret.append(sb);
            ret.append("\n");
        }
        System.out.println(jsonArray.size());
        String filePath = "D:/work/应急管理数据对接动态及静态数据sql" + filename  + ".sql";
        FileWriter fwriter = null;
        try {
            // true表示不覆盖原来的内容，而是加到文件的后面。若要覆盖原来的内容，直接省略这个参数就好
            fwriter = new FileWriter(filePath, true);
            fwriter.write(ret.toString());
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                fwriter.flush();
                fwriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
