204 lines
5.7 KiB
C#
204 lines
5.7 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using MLL;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DAL
|
|
{
|
|
public class DALEquipContratos
|
|
{
|
|
private readonly string connectionString;
|
|
|
|
public DALEquipContratos(string conexao)
|
|
{
|
|
connectionString = conexao;
|
|
}
|
|
public bool Inserir(ModeloEquipContratos modelo)
|
|
{
|
|
using SqlConnection conn = new(connectionString);
|
|
|
|
string sql = @"
|
|
INSERT INTO Equip_contratos
|
|
(
|
|
COD_EQUIP,
|
|
COD_CONTRATO
|
|
)
|
|
VALUES
|
|
(
|
|
@COD_EQUIP,
|
|
@COD_CONTRATO
|
|
)";
|
|
|
|
using SqlCommand cmd = new(sql, conn);
|
|
|
|
PreencherParametros(cmd, modelo);
|
|
|
|
conn.Open();
|
|
|
|
return cmd.ExecuteNonQuery() > 0;
|
|
}
|
|
public bool Alterar(ModeloEquipContratos modelo)
|
|
{
|
|
using SqlConnection conn = new(connectionString);
|
|
|
|
string sql = @"
|
|
UPDATE Equip_contratos
|
|
SET
|
|
COD_EQUIP=@COD_EQUIP,
|
|
COD_CONTRATO=@COD_CONTRATO
|
|
WHERE
|
|
ID_EQUIP_CONTRATOS=@ID_EQUIP_CONTRATOS";
|
|
|
|
using SqlCommand cmd = new(sql, conn);
|
|
|
|
PreencherParametros(cmd, modelo);
|
|
|
|
cmd.Parameters.AddWithValue(
|
|
"@ID_EQUIP_CONTRATOS",
|
|
modelo.ID_EQUIP_CONTRATOS);
|
|
|
|
conn.Open();
|
|
|
|
return cmd.ExecuteNonQuery() > 0;
|
|
}
|
|
public bool Excluir(int id)
|
|
{
|
|
using SqlConnection conn = new(connectionString);
|
|
|
|
string sql = @"DELETE FROM Equip_contratos WHERE ID_EQUIP_CONTRATOS=@ID";
|
|
|
|
using SqlCommand cmd = new(sql, conn);
|
|
|
|
cmd.Parameters.AddWithValue("@ID", id);
|
|
|
|
conn.Open();
|
|
|
|
return cmd.ExecuteNonQuery() > 0;
|
|
}
|
|
public ModeloEquipContratos? Carregar(int id)
|
|
{
|
|
using SqlConnection conn = new(connectionString);
|
|
|
|
string sql = @"
|
|
SELECT *
|
|
FROM Equip_contratos
|
|
WHERE ID_EQUIP_CONTRATOS=@ID";
|
|
|
|
using SqlCommand cmd = new(sql, conn);
|
|
|
|
cmd.Parameters.AddWithValue("@ID", id);
|
|
|
|
conn.Open();
|
|
|
|
using SqlDataReader dr = cmd.ExecuteReader();
|
|
|
|
if (dr.Read())
|
|
return PreencherModelo(dr);
|
|
|
|
return null;
|
|
}
|
|
public List<ModeloEquipContratos> Listar(string? codigo = null, string? codEquip = null, string? codContrato = null)
|
|
{
|
|
List<ModeloEquipContratos> lista = [];
|
|
|
|
using SqlConnection conn = new(connectionString);
|
|
|
|
string sql = @"
|
|
SELECT *
|
|
FROM Equip_contratos
|
|
WHERE 1=1";
|
|
|
|
using SqlCommand cmd = new();
|
|
|
|
if (!string.IsNullOrWhiteSpace(codigo))
|
|
{
|
|
sql += " AND CODIGO LIKE @CODIGO";
|
|
cmd.Parameters.AddWithValue("@CODIGO", "%" + codigo + "%");
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(codEquip))
|
|
{
|
|
sql += " AND COD_EQUIP LIKE @COD_EQUIP";
|
|
cmd.Parameters.AddWithValue("@COD_EQUIP", "%" + codEquip + "%");
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(codContrato))
|
|
{
|
|
sql += " AND COD_CONTRATO LIKE @COD_CONTRATO";
|
|
cmd.Parameters.AddWithValue("@COD_CONTRATO", "%" + codContrato + "%");
|
|
}
|
|
|
|
sql += " ORDER BY ID_EQUIP_CONTRATOS DESC";
|
|
|
|
cmd.Connection = conn;
|
|
cmd.CommandText = sql;
|
|
|
|
conn.Open();
|
|
|
|
using SqlDataReader dr = cmd.ExecuteReader();
|
|
|
|
while (dr.Read())
|
|
lista.Add(PreencherModelo(dr));
|
|
|
|
return lista;
|
|
}
|
|
private ModeloEquipContratos PreencherModelo(SqlDataReader dr)
|
|
{
|
|
return new ModeloEquipContratos
|
|
{
|
|
ID_EQUIP_CONTRATOS = Convert.ToInt32(dr["ID_EQUIP_CONTRATOS"]),
|
|
CODIGO = Convert.ToString(dr["CODIGO"]) ?? "",
|
|
COD_EQUIP = Convert.ToString(dr["COD_EQUIP"]) ?? "",
|
|
COD_CONTRATO = Convert.ToString(dr["COD_CONTRATO"]) ?? ""
|
|
};
|
|
}
|
|
private void PreencherParametros(SqlCommand cmd, ModeloEquipContratos modelo)
|
|
{
|
|
cmd.Parameters.AddWithValue(
|
|
"@COD_EQUIP",
|
|
modelo.COD_EQUIP);
|
|
|
|
cmd.Parameters.AddWithValue(
|
|
"@COD_CONTRATO",
|
|
modelo.COD_CONTRATO);
|
|
}
|
|
|
|
#region Métodos Específicos
|
|
public List<ModeloEquipContratos> ListarPorEquipamento(string codEquip)
|
|
{
|
|
List<ModeloEquipContratos> lista = [];
|
|
using SqlConnection conn = new(connectionString);
|
|
string sql = @"SELECT * FROM Equip_contratos WHERE COD_EQUIP=@COD_EQUIP";
|
|
|
|
using SqlCommand cmd = new(sql, conn);
|
|
|
|
cmd.Parameters.AddWithValue("@COD_EQUIP", codEquip);
|
|
|
|
conn.Open();
|
|
|
|
using SqlDataReader dr = cmd.ExecuteReader();
|
|
|
|
while (dr.Read())
|
|
lista.Add(PreencherModelo(dr));
|
|
|
|
return lista;
|
|
}
|
|
public List<ModeloEquipContratos> ListarPorContrato(string codContrato) {
|
|
List<ModeloEquipContratos> lista = [];
|
|
using SqlConnection conn = new(connectionString);
|
|
string sql = @"SELECT * FROM Equip_contratos WHERE COD_CONTRATO=@COD_CONTRATO";
|
|
using SqlCommand cmd = new(sql, conn);
|
|
cmd.Parameters.AddWithValue("@COD_CONTRATO", codContrato);
|
|
conn.Open();
|
|
using SqlDataReader dr = cmd.ExecuteReader();
|
|
while (dr.Read())
|
|
lista.Add(PreencherModelo(dr));
|
|
return lista;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |