package br.com.ddns.grupotsergio;

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

/**
 *
 * @author Edson
 */
public class cidadeGS {
    public int chave = 0;
    public String cidade = null;
    public String uf = null;
    public String codigo = null;
    public String audit = null;
    private static PreparedStatement stm = null;
    private static ResultSet rs = null;
    private static Statement stmt;

    public cidadeGS(){
    }

    public String getAudit() {
        return audit;
    }

    public void setAudit(String audit) {
        this.audit = audit;
    }

    public int getChave() {
        return chave;
    }

    public void setChave(int chave) {
        this.chave = chave;
    }

    public String getCidade() {
        return cidade;
    }

    public void setCidade(String cidade) {
        this.cidade = cidade;
    }

    public String getCodigo() {
        return codigo;
    }

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

    public String getUf() {
        return uf;
    }

    public void setUf(String uf) {
        this.uf = uf;
    }

    public static boolean inserirCidade (cidadeGS cidade){
        try{
            stm = Conexao.conectar().prepareStatement("INSERT INTO cidades("
            + "idcidades,cidade,uf,codigo,auditoria)"
            + " values "
            + "(?,?,?,?,?)");
            stm.setInt(1, cidade.getChave());
            stm.setString(2, cidade.getCidade());
            stm.setString(3, cidade.getUf());
            stm.setString(4, cidade.getCodigo());
            stm.setString(5, cidade.getAudit());
            stm.executeUpdate();
            stm.close();
            return true;
        }catch(Exception e){
            System.out.println("Erro ao inserir");
            return false;
        }
    }

    public static boolean alterarCidade (cidadeGS cidade){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update cidades set "
                    + "CIDADE=?,UF=?,CODIGO=?,auditoria=? "
                    + "where IDCIDADES = ?");
            stm.setString(1, cidade.getCidade());
            stm.setString(2, cidade.getUf());
            stm.setString(3, cidade.getCodigo());
            stm.setString(4, cidade.getAudit());
            stm.setInt(5, cidade.getChave());
            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 excluiCidade (cidadeGS cidade){
        boolean testa = false;
        try {
            stm = Conexao.conectar().prepareStatement("delete from cidades "
                    + "where idcidades = ?");
            stm.setInt(1, cidade.getChave());
            int executeUpdate = stm.executeUpdate();

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

    public void selCidade(String Id) {
        try {
            cidadeGS scid = null;
            stmt = Conexao.conectar().createStatement();
            rs = stmt.executeQuery("Select * From cidades Where idcidades ='"+Id+"'");
            if (rs.next()) {
                scid = new cidadeGS();
                scid.setChave(rs.getInt("chave"));
                scid.setCidade(rs.getString("cidade"));
                scid.setUf(rs.getString("uf"));
                scid.setCodigo(rs.getString("codigo"));
                scid.setAudit(rs.getString("audit"));
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    public void listaCidades(){
        try{
            stmt = Conexao.conectar().createStatement();
            rs = stmt.executeQuery("select * from cidades order by cidade");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public ResultSet getResultado(){
        return rs;
    }
}
