/*
 * 0-t ha eggyik sem
 * 1-et ad vissza ha Vendeg
 * 2-t ha Szakacs
 * 3-at ha Szállodavezető
 * 4-et ha Recepciós
 * 5-öt ha Takarító
 */
package com.HotelServices.model.dto.impl;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class Bejelentkezik {
    public int felhasznalo(String nev, String jelszo){    
    Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hotelservices", "root", "admin");

            //System.out.println("kajshkjashd");
            PreparedStatement stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM vendeg where FelhasznaloNev = ? and Jelszo = ?");
            stmt.setString(1, nev);
            stmt.setString(2, jelszo);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return 1;
            }

            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM szakacs where FelhasznaloNev = ? and Jelszo = ?");
            stmt.setString(1, nev);
            stmt.setString(2, jelszo);
            rs = stmt.executeQuery();
            if (rs.next()) {
                return 2;
            }
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM szallodavezeto where FelhasznaloNev = ? and Jelszo = ?");
            stmt.setString(1, nev);
            stmt.setString(2, jelszo);
            rs = stmt.executeQuery();
            if (rs.next()) {
                return 3;
            }
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM recepcios where FelhasznaloNev = ? and Jelszo = ?");
            stmt.setString(1, nev);
            stmt.setString(2, jelszo);
            rs = stmt.executeQuery();
            if (rs.next()) {
                return 4;
            }
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM takarito where FelhasznaloNev = ? and Jelszo = ?");
            stmt.setString(1, nev);
            stmt.setString(2, jelszo);
            rs = stmt.executeQuery();
            if (rs.next()) {
                return 5;
            }
            
        } catch (java.lang.Exception ex) {
            ex.printStackTrace();
        }
    return 0;   
        }
    /*
     * 100-199-ig felhasználó
     * 200-299-ig takarító
     * 300-399-ig szállodavezető
     * 400-499-ig szakács
     * 499-500-ig portás 
     */    
    
    
        public int userAzon(String nev) {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hotelservices", "root", "admin");
            PreparedStatement stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM vendeg WHERE FelhasznaloNev = ?");
            stmt.setString(1, nev);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getInt(1)+100;
            }        
            
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM takarito WHERE FelhasznaloNev = ?");
            stmt.setString(1, nev);
            rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getInt(1)+200;
            }
            
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM szallodavezeto WHERE FelhasznaloNev = ?");
            stmt.setString(1, nev);
            rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getInt(1)+300;
            }
            
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM szakacs WHERE FelhasznaloNev = ?");
            stmt.setString(1, nev);
            rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getInt(1)+400;
            }
            
            stmt = (PreparedStatement) con.prepareStatement("SELECT * FROM recepcios WHERE FelhasznaloNev = ?");
            stmt.setString(1, nev);
            rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getInt(1)+500;
            }
            
            stmt.close();
            con.close();
            return -1;
         
        } catch (java.lang.Exception ex) {
            ex.printStackTrace();
            }
    return -1;
    }
    
}

