|
|
@ -1,11 +1,15 @@ |
|
|
|
package mx.gob.jumapacelaya.services; |
|
|
|
|
|
|
|
import mx.gob.jumapacelaya.models.*; |
|
|
|
import oracle.jdbc.OracleConnection; |
|
|
|
import oracle.jdbc.OraclePreparedStatement; |
|
|
|
import oracle.sql.CLOB; |
|
|
|
import org.apache.poi.ss.usermodel.*; |
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.StringReader; |
|
|
|
import java.sql.*; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.util.ArrayList; |
|
|
@ -15,8 +19,8 @@ public class DatabaseService { |
|
|
|
|
|
|
|
|
|
|
|
private Connection getMysqlConnection() throws SQLException { |
|
|
|
String url = "jdbc:mysql://185.196.20.240:33006/Mantenimientos"; |
|
|
|
String user = "root"; |
|
|
|
String url = "jdbc:oracle:thin:@//svrapps:1521/XEPDB1"; |
|
|
|
String user = "MANTENIMIENTOS"; |
|
|
|
String pass = "mantenimientos"; |
|
|
|
|
|
|
|
return DriverManager.getConnection(url, user, pass); |
|
|
@ -35,7 +39,7 @@ public class DatabaseService { |
|
|
|
// Método para obtener los tipos de mantenimientos |
|
|
|
public List<TiposMantenimiento> getTiposDeMantenimientos() { |
|
|
|
List<TiposMantenimiento> tiposDeMantenimientos = new ArrayList<>(); |
|
|
|
String query = "SELECT tipomantid, nombre FROM TIPOMANT"; |
|
|
|
String query = "SELECT tipomantid, nombre FROM MANTENIMIENTOS.TIPOMANT"; |
|
|
|
|
|
|
|
try (Connection connection = getMysqlConnection(); |
|
|
|
Statement statement = connection.createStatement(); |
|
|
@ -81,7 +85,7 @@ public class DatabaseService { |
|
|
|
public List<Usuario> getUsuarios() { |
|
|
|
List<Usuario> usuarios = new ArrayList<>(); |
|
|
|
String query = "SELECT u.* \n" + |
|
|
|
"FROM Mantenimientos.USUARIOS u;"; |
|
|
|
"FROM MANTENIMIENTOS.USUARIOS u"; |
|
|
|
|
|
|
|
try (Connection connection = getMysqlConnection(); |
|
|
|
Statement statement = connection.createStatement(); |
|
|
@ -91,7 +95,7 @@ public class DatabaseService { |
|
|
|
Usuario usuario = new Usuario( |
|
|
|
resultSet.getString("empleadoid"), |
|
|
|
resultSet.getString("nombre"), |
|
|
|
resultSet.getString("correo") |
|
|
|
resultSet.getString("email") |
|
|
|
); |
|
|
|
usuarios.add(usuario); |
|
|
|
} |
|
|
@ -105,7 +109,7 @@ public class DatabaseService { |
|
|
|
/* -------------- Metodo para obtener los departamentos ---------------- */ |
|
|
|
public List<DepartamentosModel> getDepartamentos() { |
|
|
|
List<DepartamentosModel> departamentos = new ArrayList<>(); |
|
|
|
String query = "select DEPARTAMENTOID, DESCRIPCION from DEPARTAMENTOS"; |
|
|
|
String query = "select DEPARTAMENTOID, DESCRIPCION from MANTENIMIENTOS.DEPARTAMENTOS"; |
|
|
|
|
|
|
|
try (Connection connection = getMysqlConnection(); |
|
|
|
Statement statement = connection.createStatement(); |
|
|
@ -128,7 +132,7 @@ public class DatabaseService { |
|
|
|
/* -------------- Metodo para obtener los tipos de hardware ---------------- */ |
|
|
|
public List<TiposHardware> getTiposHardware() { |
|
|
|
List<TiposHardware> tiposHardware = new ArrayList<>(); |
|
|
|
String query = "select TIPOHARDWAREID, NOMBRETIPO from TIPOSHARDWARE"; |
|
|
|
String query = "select TIPOHARDWAREID, DESCRIPCION from MANTENIMIENTOS.TIPOSHARDWARE"; |
|
|
|
|
|
|
|
try (Connection connection = getMysqlConnection(); |
|
|
|
Statement statement = connection.createStatement(); |
|
|
@ -137,7 +141,7 @@ public class DatabaseService { |
|
|
|
while (resultSet.next()) { |
|
|
|
TiposHardware tiposHardwareModel = new TiposHardware( |
|
|
|
resultSet.getString("tipohardwareid"), |
|
|
|
resultSet.getString("nombretipo") |
|
|
|
resultSet.getString("descripcion") |
|
|
|
); |
|
|
|
tiposHardware.add(tiposHardwareModel); |
|
|
|
} |
|
|
@ -152,12 +156,12 @@ public class DatabaseService { |
|
|
|
/* ----------------Obtener el Plan Anual de Mantenimiento ---------------- */ |
|
|
|
public List<PlanAnual> getPlanAnual() { |
|
|
|
List<PlanAnual> planAnualList = new ArrayList<>(); |
|
|
|
String query = "SELECT p.mttoprogramadoid, p.nomEquipo, p.departamento, " + |
|
|
|
String query = "SELECT p.plananualid, p.nomEquipo, p.area, " + |
|
|
|
"p.monitor, p.teclado, p.mouse, p.regulador, " + |
|
|
|
"p.cpu, p.impresora, p.miniPrint, p.laptop, p.escaner, " + |
|
|
|
"p.fechaprog, m.fecha AS fechaMantenimiento, p.tecnicosmt, p.estado " + |
|
|
|
"FROM MTTOPROGRAMADOS p " + |
|
|
|
"LEFT JOIN MANTENIMIENTOS m ON p.mttoprogramadoid = m.mttoprogramadoid"; |
|
|
|
"FROM MANTENIMIENTOS.PLANANUAL p " + |
|
|
|
"LEFT JOIN MANTENIMIENTOS.MANTENIMIENTOS m ON p.plananualid = m.plananualid"; |
|
|
|
|
|
|
|
try (Connection connection = getMysqlConnection(); |
|
|
|
Statement statement = connection.createStatement(); |
|
|
@ -165,9 +169,9 @@ public class DatabaseService { |
|
|
|
|
|
|
|
while (resultSet.next()) { |
|
|
|
PlanAnual planAnual = new PlanAnual( |
|
|
|
resultSet.getInt("mttoprogramadoid"), |
|
|
|
resultSet.getInt("plananualid"), |
|
|
|
resultSet.getString("nomEquipo"), |
|
|
|
resultSet.getString("departamento"), |
|
|
|
resultSet.getString("area"), |
|
|
|
resultSet.getBoolean("monitor"), |
|
|
|
resultSet.getBoolean("teclado"), |
|
|
|
resultSet.getBoolean("mouse"), |
|
|
@ -203,13 +207,12 @@ public class DatabaseService { |
|
|
|
String formaMant, String equipoId, String userSignatureBase64, |
|
|
|
String smtSignatureBase64, String gciaSignatureBase64, String planAnualId) { |
|
|
|
|
|
|
|
String query = "INSERT INTO MANTENIMIENTOS (fecha, tipoMantId, departamentoId, empleadoId, formaMant, nombreequipo," + |
|
|
|
" firmaUsuario, firmaSmt, firmaGcia, mttoprogramadoid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
|
|
|
String query = "INSERT INTO MANTENIMIENTOS.MANTENIMIENTOS (fecha, tipoMantId, departamentoId, empleadoId, formaMant, nombreequipo," + |
|
|
|
" firmaUsuario, firmaSmt, firmaGcia, planAnualId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING MANTENIMIENTOID INTO ?"; |
|
|
|
|
|
|
|
String updateStatusQuery = "UPDATE MTTOPROGRAMADOS SET estado = 'REALIZADO' WHERE mttoprogramadoid = ?"; |
|
|
|
String updateStatusQuery = "UPDATE MANTENIMIENTOS.PLANANUAL SET estado = 'REALIZADO' WHERE planAnualId = ?"; |
|
|
|
|
|
|
|
int nuevoId = -1; |
|
|
|
|
|
|
|
Connection connection = null; |
|
|
|
|
|
|
|
try { |
|
|
@ -219,29 +222,46 @@ public class DatabaseService { |
|
|
|
throw new SQLException("No se pudo establecer la conexion con la base de datos."); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
connection.setAutoCommit(false); |
|
|
|
|
|
|
|
// Paso 1: primero inserta el mantenimiento |
|
|
|
try (PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { |
|
|
|
try (OraclePreparedStatement preparedStatement = (OraclePreparedStatement) connection.prepareStatement(query)) { |
|
|
|
|
|
|
|
preparedStatement.setDate(1, Date.valueOf(fecha)); |
|
|
|
preparedStatement.setString(2, tipoMantId); |
|
|
|
preparedStatement.setLong(2, Long.parseLong(tipoMantId)); |
|
|
|
preparedStatement.setString(3, departamentoId); |
|
|
|
preparedStatement.setString(4, empleadoId); |
|
|
|
preparedStatement.setLong(4, Long.parseLong(empleadoId)); |
|
|
|
preparedStatement.setString(5, formaMant); |
|
|
|
preparedStatement.setString(6, equipoId); |
|
|
|
preparedStatement.setString(7, userSignatureBase64); |
|
|
|
preparedStatement.setString(8, smtSignatureBase64); |
|
|
|
preparedStatement.setString(9, gciaSignatureBase64); |
|
|
|
preparedStatement.setString(10, planAnualId); |
|
|
|
|
|
|
|
int rowsAffected = preparedStatement.executeUpdate(); |
|
|
|
if (rowsAffected > 0) { |
|
|
|
try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { |
|
|
|
if (generatedKeys.next()) { |
|
|
|
nuevoId = generatedKeys.getInt(1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (userSignatureBase64 != null) { |
|
|
|
preparedStatement.setString(7, userSignatureBase64); |
|
|
|
} else { |
|
|
|
preparedStatement.setNull(7, Types.VARCHAR); |
|
|
|
} |
|
|
|
|
|
|
|
if (smtSignatureBase64 != null) { |
|
|
|
preparedStatement.setString(8, smtSignatureBase64); |
|
|
|
} else { |
|
|
|
preparedStatement.setNull(8, Types.VARCHAR); |
|
|
|
} |
|
|
|
|
|
|
|
if (gciaSignatureBase64 != null) { |
|
|
|
preparedStatement.setString(9, gciaSignatureBase64); |
|
|
|
} else { |
|
|
|
preparedStatement.setNull(9, Types.VARCHAR); |
|
|
|
} |
|
|
|
|
|
|
|
preparedStatement.setLong(10, Long.parseLong(planAnualId)); |
|
|
|
|
|
|
|
preparedStatement.registerReturnParameter(11, Types.NUMERIC); |
|
|
|
|
|
|
|
preparedStatement.executeUpdate(); |
|
|
|
|
|
|
|
ResultSet rs = preparedStatement.getReturnResultSet(); |
|
|
|
if (rs != null && rs.next()) { |
|
|
|
nuevoId = rs.getInt(1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -258,12 +278,11 @@ public class DatabaseService { |
|
|
|
|
|
|
|
} catch (SQLException e) { |
|
|
|
System.err.println("Error al insertar mantenimiento: " + e.getMessage()); |
|
|
|
e.printStackTrace(); |
|
|
|
// En caso de error, se hace rollback |
|
|
|
if (connection != null) { |
|
|
|
try { |
|
|
|
if (connection != null) { |
|
|
|
connection.rollback(); |
|
|
|
} |
|
|
|
connection.rollback(); |
|
|
|
} catch (SQLException rollbackEx) { |
|
|
|
System.err.println("Error al hacer rollback: " + rollbackEx.getMessage()); |
|
|
|
} |
|
|
@ -286,13 +305,13 @@ public class DatabaseService { |
|
|
|
|
|
|
|
// INSERTAR EN LA TABLA: HARDWAREDET |
|
|
|
public boolean insertarHardware(String tipoHardwareId, String numSerie, String modelo, String placa, int mantenimientoId) { |
|
|
|
String query = "INSERT INTO HARDWAREDET (tipoHardwareId, numSerie, modelo, placa, mantenimientoId) VALUES (?, ?, ?, ?, ?)"; |
|
|
|
String query = "INSERT INTO MANTENIMIENTOS.HARDWAREDET (tipoHardwareId, numSerie, modelo, placa, mantenimientoId) VALUES (?, ?, ?, ?, ?)"; |
|
|
|
boolean isInserted = false; |
|
|
|
|
|
|
|
try (Connection connection = getMysqlConnection(); |
|
|
|
PreparedStatement preparedStatement = connection.prepareStatement(query)) { |
|
|
|
|
|
|
|
preparedStatement.setString(1, tipoHardwareId); |
|
|
|
preparedStatement.setInt(1, Integer.parseInt(tipoHardwareId)); |
|
|
|
preparedStatement.setString(2, numSerie); |
|
|
|
preparedStatement.setString(3, modelo); |
|
|
|
preparedStatement.setString(4, placa); |
|
|
@ -310,7 +329,7 @@ public class DatabaseService { |
|
|
|
public int getUltimoMantenimientoId() { |
|
|
|
int ultimoId = -1; |
|
|
|
try (Connection connection = getMysqlConnection()) { |
|
|
|
String query = "SELECT MAX(mantenimientoid) FROM MANTENIMIENTOS"; |
|
|
|
String query = "SELECT MAX(mantenimientoid) FROM MANTENIMIENTOS.MANTENIMIENTOS"; |
|
|
|
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { |
|
|
|
ResultSet resultSet = preparedStatement.executeQuery(); |
|
|
|
if (resultSet.next()) { |
|
|
@ -326,7 +345,7 @@ public class DatabaseService { |
|
|
|
|
|
|
|
// INSERTAR EN LA TABLA: ACTUALIZACIONESSEG |
|
|
|
public boolean insertActualizacionSeg(String descripcion, String otras, int mantenimientoId) { |
|
|
|
String query = "INSERT INTO ACTUALIZACIONESSEG (descripcion, otrasactualizaciones, mantenimientoid) VALUES (?, ?, ?)"; |
|
|
|
String query = "INSERT INTO MANTENIMIENTOS.ACTUALIZACIONESSEG (descripcion, otrasactualizaciones, mantenimientoid) VALUES (?, ?, ?)"; |
|
|
|
|
|
|
|
try (Connection connection = getMysqlConnection(); |
|
|
|
PreparedStatement preparedStatement = connection.prepareStatement(query)) { |
|
|
@ -345,7 +364,7 @@ public class DatabaseService { |
|
|
|
|
|
|
|
|
|
|
|
public void insertarDesdeExcel(InputStream inputStream) { |
|
|
|
String query = "INSERT INTO MTTOPROGRAMADOS (NOMEQUIPO, DEPARTAMENTO, MONITOR, TECLADO, MOUSE, " + |
|
|
|
String query = "INSERT INTO MANTENIMIENTOS.PLANANUAL (NOMEQUIPO, AREA, MONITOR, TECLADO, MOUSE, " + |
|
|
|
"REGULADOR, CPU, IMPRESORA, MINIPRINT, LAPTOP, ESCANER, FECHAPROG, TECNICOSMT, ESTADO) " + |
|
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
|
|
|
|
|
|
@ -361,7 +380,7 @@ public class DatabaseService { |
|
|
|
try { |
|
|
|
// Leer cada celda de la fila según el índice correcto |
|
|
|
String nomequipo = getStringCellValue(row.getCell(0)); // Columna 1 |
|
|
|
String departamento = getStringCellValue(row.getCell(1)); // Columna 2 |
|
|
|
String area = getStringCellValue(row.getCell(1)); // Columna 2 |
|
|
|
boolean monitor = getBooleanCellValue(row.getCell(2)); // Columna 3 |
|
|
|
boolean teclado = getBooleanCellValue(row.getCell(3)); // Columna 4 |
|
|
|
boolean mouse = getBooleanCellValue(row.getCell(4)); // Columna 5 |
|
|
@ -381,7 +400,7 @@ public class DatabaseService { |
|
|
|
// Insertar datos en la base de datos |
|
|
|
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { |
|
|
|
preparedStatement.setString(1, nomequipo); |
|
|
|
preparedStatement.setString(2, departamento); |
|
|
|
preparedStatement.setString(2, area); |
|
|
|
preparedStatement.setBoolean(3, monitor); |
|
|
|
preparedStatement.setBoolean(4, teclado); |
|
|
|
preparedStatement.setBoolean(5, mouse); |
|
|
@ -409,7 +428,7 @@ public class DatabaseService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
System.out.println("Datos insertados desde Excel con éxito en MTTOPROGRAMADOS."); |
|
|
|
System.out.println("Datos insertados desde Excel con éxito en PLANANUAL."); |
|
|
|
} catch (IOException e) { |
|
|
|
System.err.println("Error al leer el archivo Excel: " + e.getMessage()); |
|
|
|
} catch (SQLException e) { |
|
|
|