package dao.specification;

import dao.userAbstractDAO;
import entities.Users;
import org.jetbrains.annotations.NotNull;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static dao.specification.abstractDAO.Delete;
import static util.SQLDatabaseConnection.getConnection;
import static util.SQLDatabaseConnection.close;

public class userDAO implements userAbstractDAO {
    private Connection connection = null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;

    //新建用户
    @Override
    public int insert(@NotNull Users users) {
        int flag;
        try {
            connection = getConnection();
            String insert_sql = "insert into Users(name, phoneNumber, address, password) " +
                    "values (?,?,?,?)";
            preparedStatement = connection.prepareStatement(insert_sql);

            preparedStatement.setString(1, users.getName());
            preparedStatement.setString(2, users.getPhoneNumber());
            preparedStatement.setString(3, users.getAddress());
            preparedStatement.setString(4, users.getPassword());

            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(connection, preparedStatement, resultSet);
        }
        return flag;
    }

    //删除
    @Override
    public int delete(@NotNull Users user) {
        String delete_sql = "delete from Users where id=?";

        return Delete
                (connection, preparedStatement, resultSet, delete_sql, user.getId());
    }

    //更新用户个人信息
    //@TODO 只能更新所有信息，不能选择性更新
    @Override
    public int update(@NotNull Users users) {
        int flag;
        String update_sql = "update Users " +
                "set name = ?, phoneNumber = ?, address = ? " +
                "where id = ?";

        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(update_sql);

            preparedStatement.setString(1, users.getName());
            preparedStatement.setString(2, users.getPhoneNumber());
            preparedStatement.setString(3, users.getAddress());
            preparedStatement.setLong(4, users.getId());

            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    //查询所有用户
    @Override
    public List<Users> search(Users user) {
        List<Users> list = new ArrayList<>();
        String selectAll_sql = "select * from Users";

        try {
            connection = getConnection();

            preparedStatement = connection.prepareStatement(selectAll_sql);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                user = new Users();
                SetAttribute(user, resultSet);
                list.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(connection, preparedStatement, resultSet);
        }
        return list;
    }

    //用ID查询用户
    @Override
    public Users searchID(Long id) {
        Users user = null;
        String selectID_sql = "select * from Users where id = ?";
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(selectID_sql);
            preparedStatement.setLong(1, id);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                user = new Users();
                SetAttribute(user, resultSet);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(connection, preparedStatement, resultSet);
        }
        return user;
    }

    private void SetAttribute(@NotNull Users user, @NotNull ResultSet rs) throws SQLException {
        user.setId(rs.getLong("id"));
        user.setName(rs.getString("name"));
        user.setPhoneNumber(rs.getString("phoneNumber"));
        user.setAddress(rs.getString("address"));
    }
}
