import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.util.date.DateUtil;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @ClassName: DataTest
 * @Description:
 * @Author: wanggeng
 * @Date: 2021/11/24 4:09 下午
 * @Version: 1.0
 */
public class DataTest {

    private String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/db_smart_city_usercenter?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=UTC&nullCatalogMeansCurrent=true";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void insertJson() throws Exception {
        Connection connection = DriverManager.getConnection(jdbcUrl, "root", "root");
        String jsonArrayFilePath = "/Users/wanggeng/Desktop/9+x-房屋-任务系统字典.txt";
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(jsonArrayFilePath)));
        StringBuilder jsonArraySB = new StringBuilder();
        for (String line; (line = bufferedReader.readLine()) != null; ) {
            jsonArraySB.append(line);
        }
        if (jsonArraySB.length() == 0) {
            return;
        }
        String gmtDate = DateUtil.getTime();
        String SQL = "INSERT INTO `data_data` (" +
                "`data_id`, " +
                "`data_parent_id`, " +
                "`data_name`, " +
                "`data_summary`, " +
                "`data_code`, " +
                "`data_sort`, " +
                "`creator`, " +
                "`gmt_create`, " +
                "`modifier`, " +
                "`gmt_modified`, " +
                "`is_delete`) " +
                "VALUES (?, ? ,? ,? ,? ,? ,'1' ,'" + gmtDate + "', '1', '" + gmtDate + "', '0');";
        try (PreparedStatement preparedStatement = connection.prepareStatement(SQL)) {
            JSONArray jsonArray = JSONArray.parseArray(jsonArraySB.toString());
            insertDatas(jsonArray, 53, "", preparedStatement);
        }
    }

    /**
     * 插入数据
     *
     * @param jsonArray
     * @param parentCode
     * @param preparedStatement
     * @throws SQLException
     */
    public void insertDatas(JSONArray jsonArray, int lastIndex, String parentCode, PreparedStatement preparedStatement) throws SQLException {
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            int dataSort = lastIndex + (i + 1);
            String dataCode = String.format("%s%04d", parentCode, dataSort);
            preparedStatement.setString(1, jsonObject.getString("dataId"));
            preparedStatement.setString(2, jsonObject.getString("dataParentId"));
            preparedStatement.setString(3, jsonObject.getString("dataName"));
            preparedStatement.setString(4, jsonObject.getString("dataSummary"));
            preparedStatement.setString(5, dataCode);
            preparedStatement.setInt(6, dataSort);
            preparedStatement.executeUpdate();
            JSONArray subDatas = jsonObject.getJSONArray("subData");
            if (!subDatas.isEmpty()) {
                insertDatas(subDatas, 0, dataCode, preparedStatement);
            }
        }
    }

}
