/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.ddns.grupotsergio;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;

/**
 *
 * @author Edson
 */
public class locaisGS {
    int idlocais = 0;
    String local = null;
    String codigo = null;
    String auditoria = null;
    String obs = null;

    private static PreparedStatement stm = null;
    private static ResultSet rs = null;
    private static Statement stmt;

    public locaisGS(){
    }

    public String getAuditoria() {
        return auditoria;
    }

    public void setAuditoria(String auditoria) {
        this.auditoria = auditoria;
    }

    public String getCodigo() {
        return codigo;
    }

    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }

    public int getIdlocais() {
        return idlocais;
    }

    public void setIdlocais(int idlocais) {
        this.idlocais = idlocais;
    }

    public String getLocal() {
        return local;
    }

    public void setLocal(String local) {
        this.local = local;
    }

    public String getObs() {
        return obs;
    }

    public void setObs(String obs) {
        this.obs = obs;
    }

    public static boolean inserirLocal (locaisGS grupo){
        try{
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("INSERT INTO locais("
                    + "idlocais,"
                    + "local,"
                    + "codigo,"
                    + "auditoria,"
                    + "obs) VALUES"
                    + "(?,?,?,?,?)");
            stm.setInt(1, grupo.getIdlocais());
            stm.setString(2, grupo.getLocal());
            stm.setString(3, grupo.getCodigo());
            stm.setString(4, grupo.getAuditoria());
            stm.setString(5, grupo.getObs());
            stm.executeUpdate();
            stm.close();
            return true;
        }catch(Exception e){
            System.out.println("Erro ao inserir");
            return false;
        }
    }

    public static boolean altLocalLocal (locaisGS grupo){
        boolean testa = false;
        try{
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("update locais set "
                    + "local=? "
                    + "where idlocais=?");
            stm.setString(1, grupo.getLocal());
            stm.setInt(2, grupo.getIdlocais());

            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        }catch(Exception e){
            System.out.println("Erro ao alterar");
        }
        return testa;
    }

    public static boolean altLocalCod (locaisGS grupo){
        boolean testa = false;
        try{
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("update locais set "
                    + "codigo=? "
                    + "where idlocais=?");
            stm.setString(1, grupo.getCodigo());
            stm.setInt(2, grupo.getIdlocais());

            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        }catch(Exception e){
            System.out.println("Erro ao alterar");
        }
        return testa;
    }

    public static boolean altLocalAud (locaisGS grupo){
        boolean testa = false;
        try{
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("update locais set "
                    + "auditoria=? "
                    + "where idlocais=?");
            stm.setString(1, grupo.getAuditoria());
            stm.setInt(2, grupo.getIdlocais());

            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        }catch(Exception e){
            System.out.println("Erro ao alterar");
        }
        return testa;
    }

    public static boolean altLocalObs (locaisGS grupo){
        boolean testa = false;
        try{
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("update locais set "
                    + "obs=? "
                    + "where idlocais=?");
            stm.setString(1, grupo.getObs());
            stm.setInt(2, grupo.getIdlocais());

            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        }catch(Exception e){
            System.out.println("Erro ao alterar");
        }
        return testa;
    }

    public static boolean excluiLocal (locaisGS grupo){
        boolean testa = false;
        try {
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("delete from locais "
                    + "where idlocais = ?");
            stm.setInt(1, grupo.getIdlocais());
            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return testa;
    }

    public void selLocal(String Id) {
        try {
            grupoGS scid = null;
            stmt = (Statement) Conexao.conectar().createStatement();
            rs = stmt.executeQuery("Select * From locais Where idlocais ='"+Id+"'");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getAutoInc(String Generator) {
        int Codigo = 0;
        try {
            PreparedStatement pstmt = (PreparedStatement) Conexao.conectar().prepareStatement(
                    "Select max(idlocais) From locais");
            ResultSet rss = pstmt.executeQuery();
            rss.next();
            Codigo = rss.getInt(1) + 1;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return Codigo;
    }

    public void listaLocais(){
        try{
            stmt = (Statement) Conexao.conectar().createStatement();
            rs = stmt.executeQuery("select * from locais order by local");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public ResultSet getResultado(){
        return rs;
    }

}
