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

package br.com.ddns.grupotsergio;

import java.sql.*;

/**
 *
 * @author Edson
 */
public class empresaGS {
    public int chave = 0;
    public String razao_social = null;
    public String nome_fantasia = null;
    public String audit = null;
    public Blob logo = null;

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

    public empresaGS(){
    }

    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 getNome_fantasia() {
        return nome_fantasia;
    }

    public void setNome_fantasia(String nome_fantasia) {
        this.nome_fantasia = nome_fantasia;
    }

    public String getRazao_social() {
        return razao_social;
    }

    public void setRazao_social(String razao_social) {
        this.razao_social = razao_social;
    }

    public Blob getLogo() {
        return logo;
    }

    public void setLogo(Blob logo) {
        this.logo = logo;
    }

    public static boolean inserirEmpresa (empresaGS empresa){
        try{
            stm = Conexao.conectar().prepareStatement("insert into empresa(idempresa, razao_social, "
                    + "nome_fantasia, auditoria) values (?,?,?,?)");
            stm.setInt(1, empresa.getChave());
            stm.setString(2, empresa.getRazao_social());
            stm.setString(3, empresa.getNome_fantasia());
            stm.setString(4, empresa.getAudit());
            stm.executeUpdate();
            stm.close();
            return true;
        }catch(Exception e){
            System.out.println("Erro ao inserir");
            return false;
        }
    }

    public static boolean alterarEmpresa (empresaGS empresa){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update empresa set "
                    + "razao_social = ?, nome_fantasia = ?, auditoria = ?, "
                    + "logo = ? where idempresa = ?");
            stm.setString(1, empresa.getRazao_social());
            stm.setString(2, empresa.getNome_fantasia());
            stm.setString(3, empresa.getAudit());
            stm.setInt(4, empresa.getChave());
            stm.setBlob(5, empresa.getLogo());
            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 excluiEmpresa (empresaGS empresa){
        boolean testa = false;
        try {
            stm = Conexao.conectar().prepareStatement("delete from empresa "
                    + "where idempresa = ?");
            stm.setInt(1, empresa.getChave());
            int executeUpdate = stm.executeUpdate();

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

    public void selEmpresa(String Id) {
        try {
            empresaGS scid = null;
            stmt = Conexao.conectar().createStatement();
            rs = stmt.executeQuery("Select * From empresa Where idempresa ='"+Id+"'");
            if (rs.next()) {
                scid = new empresaGS();
                scid.setChave(rs.getInt("chave"));
                scid.setRazao_social(rs.getString("razao_social"));
                scid.setNome_fantasia(rs.getString("nome_fantasia"));
                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(idempresa) From empresa");
            ResultSet rss = pstmt.executeQuery();
            rss.next();
            Codigo = rss.getInt(1) + 1;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return Codigo;
    }

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

    public ResultSet getResultado(){
        return rs;
    }
    
}
