package net.micode.notes.mysql;

import org.json.JSONObject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;

public class ConnectRDS {
    /**
     * 此处为您的连接地址、端口、账号、密码等信息。
     */
    private static final String url = "jdbc:mysql://rm-wz9acr6k0s55gtz1z7o.mysql.rds.aliyuncs.com:3306?useSSL=false&ampserverTimezone=GMT";
    private static final String user = "m_note";
    private static final String password = "Mj243306";

    /**
     * 此处为小米便签用户账号id、用户名、密码、同步数据等信息
     */
    private static int user_id;
    private static String user_account;
    private static String user_pwd;
    private static String user_info;

    public static boolean login(String account, String pwd){
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql;
        try {
            /**
             * 连接到阿里云端服务器
             */
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,user,password);
            System.out.println("connect successfully");

            /**
             * 查找用户是否存在
             */
            sql = "select * from mnote.mnote";
            preparedStatement = conn.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                user_id = resultSet.getInt("user_id");
                user_account = resultSet.getString("account");
                user_pwd = resultSet.getString("pwd");
                if (user_account.equals(account) && user_pwd.equals(pwd)){
                    return true;
                }
            }
            //捕捉异常。
        } catch (SQLException e) {
            System.out.println("MySQL操作错误");
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(conn != null){
                    conn.close();
                }
                if(preparedStatement != null){
                    preparedStatement.close();
                }
                if(resultSet != null){
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        user_id = 0;
        user_account = "";
        user_pwd = "";
        return false;
    }

    public static String addAccount(String newaccount, String pwd) throws Exception {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql;
        try {
            /**
             * 连接到阿里云端服务器
             */
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,user,password);
            System.out.println("connect successfully");

            sql = "select * from mnote.mnote";
            preparedStatement = conn.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                if (resultSet.getString("account").equals(newaccount)){
                    return "existed";
                }
            }

            sql = "insert into mnote.mnote(account,pwd) values(?,?)";
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, newaccount);
            preparedStatement.setString(2, pwd);
            int result = preparedStatement.executeUpdate();
            if (result != 0) {
                System.out.println("注册成功");
                return "success";
            }
        } catch (ClassNotFoundException var11) {
            var11.printStackTrace();
        } finally {
            try {
                if(conn != null){
                    conn.close();
                }
                if(preparedStatement != null){
                    preparedStatement.close();
                }
                if(resultSet != null){
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return "failed";
    }

    public static boolean uploadInfo(String JSONInfo) throws Exception {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            /**
             * 连接到阿里云端服务器
             */
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,user,password);
            System.out.println("connect successfully");

            String sql = "REPLACE INTO mnote.mnote(info,account,user_id,pwd) values(?,?,?,?) ";
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, JSONInfo);
            preparedStatement.setString(2, user_account);
            preparedStatement.setString(3, String.valueOf(user_id));
            preparedStatement.setString(4, user_pwd);

            int result = preparedStatement.executeUpdate();
            if (result != 0) {
                System.out.println("上传成功！");
                return true;
            }
        } catch (ClassNotFoundException var11) {
            var11.printStackTrace();
        } finally {
            try {
                if(conn != null){
                    conn.close();
                }
                if(preparedStatement != null){
                    preparedStatement.close();
                }
                if(resultSet != null){
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static String downloadInfo(String account, String pwd) throws Exception {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql;
        try {
            /**
             * 连接到阿里云端服务器
             */
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,user,password);
            System.out.println("connect successfully");

            sql = "select * from mnote.mnote";
            preparedStatement = conn.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                user_id = resultSet.getInt("user_id");
                user_account = resultSet.getString("account");
                user_pwd = resultSet.getString("pwd");
                user_info = resultSet.getString("info");
                if (user_account.equals(account) && user_pwd.equals(pwd)){
                    return user_info;
                }
            }

            int result = preparedStatement.executeUpdate();
            if (result != 0) {
                System.out.println("下载成功！");
            }

        } catch (ClassNotFoundException var11) {
            var11.printStackTrace();
        } finally {
            try {
                if(conn != null){
                    conn.close();
                }
                if(preparedStatement != null){
                    preparedStatement.close();
                }
                if(resultSet != null){
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return "failed";
    }
}
