package com.rf.psychological.utils;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;

@Slf4j
public class Data {
	

	public static void main(String[] args) throws InterruptedException, SQLException {

        String className = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://49.232.26.44:3306/psychological_112";
        String userName = "root";
        String password = "Mysql@.2020";
        Connection con = null;

        log.info("初始化数据库连接");
        try{
            Class.forName(className);
            con = DriverManager.getConnection(url,userName,password);
        } catch (ClassNotFoundException | SQLException e) {
            log.error("初始化数据库连接出错：");
            e.printStackTrace();
            return;
        }
        log.info("初始化数据库完成");
        Thread.sleep(3000);
        log.info("step1:修改表结构——开始");
        PreparedStatement stat = null;
        try{

            //        修改表结构 增加角色字段
            String alterTable = "alter table t_user_info add column role_type varchar(2) default '1' comment '用户角色 1、普通用户 2、机构管理员 3、机构超级管理员'";
            stat = con.prepareStatement(alterTable);
            stat.execute();
        } catch (SQLException e) {
            log.error("step1:修改表结构——失败");
            e.printStackTrace();
            return;
        }finally {
            if(stat != null){
                stat.close();
            }
        }
        log.info("step1：修改表结构——完成");

        log.info("机构管理员合并");
        PreparedStatement stat1 = null;
        ResultSet rs = null;
        try{
            String institution_user_sql = "select * from t_institution_user ";
            stat1 = con.prepareStatement(institution_user_sql);
            rs = stat1.executeQuery();
            while (rs.next()){
                try {
                    System.out.println("id  " + rs.getString("id"));
                    System.out.println("institution_name  " + rs.getString("institution_name"));
                    System.out.println("institution_no  " + rs.getString("institution_no"));
                    System.out.println("password  " + rs.getString("password"));
                    System.out.println("gender  " + rs.getString("gender"));
                    System.out.println("pet_name  " + rs.getString("pet_name"));
                    System.out.println("phone  " + rs.getString("phone"));
                    System.out.println("birthday" + rs.getString("birthday"));
                    System.out.println("profession" + rs.getString("profession"));
                    System.out.println("addition_info" + rs.getString("addition_info"));
                    System.out.println("user_status" + rs.getString("user_status"));

                    String insertSql = "INSERT INTO t_user_info (id, gender, password, pet_name, phone, birthday, profession, addition_info, institution_name, institution_no, user_status, g_id, model_phone, role_type) \r\n"
                            + "VALUES \r\n"
                            + "(?, ?, ?,?, ?, ?, ?,?, ?, ?, ?,?, ?, ?)\r\n";
                    PreparedStatement statement = con.prepareStatement(insertSql);
                    statement.setString(1, rs.getString("id"));
                    statement.setString(2,  rs.getString("gender"));
                    statement.setString(3, rs.getString("password"));
                    statement.setString(4, rs.getString("pet_name"));
                    statement.setString(5, rs.getString("phone"));
                    statement.setString(6, rs.getString("birthday"));
                    statement.setString(7, rs.getString("profession"));
                    statement.setString(8, rs.getString("addition_info"));
                    statement.setString(9, rs.getString("institution_name"));
                    statement.setString(10, rs.getString("institution_no"));
                    statement.setString(11, rs.getString("user_status"));
                    statement.setString(12, "");
                    statement.setString(13, "0");
                    statement.setString(14, "2");

                    int i = statement.executeUpdate();
                    statement.close();
                    System.out.println("---------"+i+"----------");
                }catch (Exception e){
                    System.out.println(rs.getString("institution_no")+"----------i---------"+rs.getString("phone"));
                }

            }
        } catch (SQLException e) {
            log.error("机构管理员合并——失败");
            e.printStackTrace();
        }finally {
            if(rs != null){
                rs.close();
            }
            if(stat1 != null ){
                stat1.close();
            }
        }


//        机构超级管理员
        log.info("机构超级管理员合并");
        PreparedStatement stat2  = null;
        ResultSet rs2 = null;
        try{
            String sql = "select * from t_institution_info ";
            stat2 = con.prepareStatement(sql);
            rs2  = stat2.executeQuery();
            while (rs2.next()){
                try {
                    System.out.println("id  " + rs2.getString("id"));
                    System.out.println("institution_name  " + rs2.getString("institution_name"));
                    System.out.println("institution_no  " + rs2.getString("institution_no"));
                    System.out.println("password  " + rs2.getString("password"));
                    System.out.println("login_name  " + rs2.getString("login_name"));
                    String insertSql = "INSERT INTO t_user_info (id, gender, password, pet_name, phone, birthday, profession, addition_info, institution_name, institution_no, user_status, g_id, model_phone, role_type) \r\n"
                            + "VALUES \r\n"
                            + "(?, ?, ?,?, ?, ?, ?,?, ?, ?, ?,?, ?, ?)\r\n";
                    PreparedStatement statement = con.prepareStatement(insertSql);
                    statement.setString(1, rs2.getString("id"));
                    statement.setString(2,  "0");
                    statement.setString(3, rs2.getString("password"));
                    statement.setString(4, rs2.getString("institution_name"));
                    statement.setString(5, rs2.getString("login_name"));
                    statement.setString(6, "");
                    statement.setString(7, "");
                    statement.setString(8, "");
                    statement.setString(9, rs2.getString("institution_name"));
                    statement.setString(10, rs2.getString("institution_no"));
                    statement.setString(11, "2");
                    statement.setString(12, "");
                    statement.setString(13, "0");
                    statement.setString(14, "3");

                    int i = statement.executeUpdate();
                    System.out.println();
                    statement.close();
                    System.out.println("----------i---------");
                }catch (Exception e ){
                    System.out.println(rs2.getString("institution_no")+"----------i---------"+rs2.getString("login_name"));
                }

            }
        } catch (SQLException e) {
            log.error("机构超级管理员合并失败");
            e.printStackTrace();
        }finally {
            if(rs2 != null){
                rs2.close();
            }
            if(stat2 != null){
                stat2.close();
            }
        }
        log.error("机构超级管理员合并完成");

        Thread.sleep(1000);
        log.error("用户合并完成");

    }
}
