package Util;

import Store.Books;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class BasicOperation extends DataBaseProgress{
    // 用户
    public boolean loginCheck(String username,String password,int admin) {
        String enable = "select * from userDetails where username = ? and password = ? and admin = ?";
        try {
            this.preparedStatement = this.connection.prepareStatement(enable);
            this.preparedStatement.setString(1,username);
            this.preparedStatement.setString(2,password);
            this.preparedStatement.setInt(3,admin);

            ResultSet resultSet = this.preparedStatement.executeQuery();
            if (resultSet.next()) {
                System.out.println("登录成功");
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.end();
        }
        System.out.println("登录失败");
        return false;
    }

    public boolean registerCheck(String username,String password,int admin) throws SQLException {
        String enable = "insert into userDetails (username,password,admin) values ('"+username+"','"+password+"','"+admin+"')";
        try {
            int count = 0;
            Statement statement = connection.createStatement();
            count = statement.executeUpdate(enable);

            if (count == 1) {
                System.out.println("插入成功！");
                statement.close();
                connection.close();
                return true;
            } else {
                System.out.println("插入失败");
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 图书
    public List select() throws SQLException {
        List <Books> list = new ArrayList<>();

//        System.out.println("select");
        String enable = "select * from bookDetails";
        PreparedStatement preparedStatement1 = this.connection.prepareStatement(enable);
        ResultSet resultSet = preparedStatement1.executeQuery();

        while (resultSet.next()) {
//            System.out.println("A");
            Vector < Object > lines = new Vector<>();
            Books books = new Books(resultSet.getString("name"), resultSet.getString("author"),
                    resultSet.getString("type"),resultSet.getBoolean("isLent"));
            list.add(books);
        }
        return list;
    }

    // 添加书籍
    public boolean addBook(String name,String author,String type,Boolean isLent) {
        String enable = "insert into bookDetails (name,author,type,isLent) values ('"+name+"','"+author+"','"+type+"','"+isLent+"')";
        try {
            int count = 0;
            Statement statement = connection.createStatement();
            count = statement.executeUpdate(enable);

            if (count == 1) {
                statement.close();
                connection.close();
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 删除书籍
    public boolean deleteBook(String name) {
        String enable = "delete from bookDetails where name = ?";
        PreparedStatement preparedStatement1 = null;
        boolean flag = false;
        try {
            int count = 0;
            preparedStatement1 = connection.prepareStatement(enable);
            preparedStatement1.setString(1,name);
            if (preparedStatement1.executeUpdate() > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                preparedStatement1.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

    // 修改书籍
    public boolean changeBook(String name,Books books) {
        String enable = "update bookDetails set name = ?, author = ?, type = ?, isLent = ?";
        PreparedStatement preparedStatement1 = null;
        boolean flag = false;
        try {
            preparedStatement1 = connection.prepareStatement(enable);
            preparedStatement1.setString(1,books.getName());
            preparedStatement1.setString(2,books.getAuthor());
            preparedStatement1.setString(3,books.getType());
            preparedStatement1.setBoolean(4,books.isLent());
            if (preparedStatement1.executeUpdate() > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                preparedStatement1.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

    // 查询图书
    public boolean searchBook(String name) {
        String enable = "select name, author, type, isLent from bookDetails where name = ?";
        PreparedStatement preparedStatement1 = null;
        try {
            preparedStatement1 = connection.prepareStatement(enable);
            preparedStatement1.setString(1,name);

            ResultSet resultSet = preparedStatement1.executeQuery();
            while (resultSet.next()) {
                System.out.println("书名："+resultSet.getInt(1)+"\t"+"作者："+resultSet.getInt(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                preparedStatement1.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}
