package entities.dto;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

import static util.SQLDatabaseConnection.close;
import static util.SQLDatabaseConnection.getConnection;
import static view.CommonUse.CreateIndent;

public class Login {
    static Connection conn = null;
    static PreparedStatement ps = null;
    static ResultSet rs = null;

    public static boolean UserLogin(Long id) {
        boolean flag = false;
        String sql = "select password from Users where id = ?";
        Scanner reader = new Scanner(System.in);
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setLong(1, id);
            rs = ps.executeQuery();

            if (!rs.next()) {
                //TODO ID输入不正确后重新输入，但是这个函数调用的太乱了，有点偷懒了，有机会改一下
                System.out.println("查无此人");
                CreateIndent();//跳转到登录选项重新输入ID
            }

            System.out.println("请输入登录密码：");
            String password = reader.nextLine();
            flag = password.equals(rs.getString("password"));

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ps, rs);
        }
        return flag;
    }
}
