Commit 0d1ad79a by sistem02 user

actualizando salidas

parent 0665992c
...@@ -13,6 +13,8 @@ import org.json.JSONObject; ...@@ -13,6 +13,8 @@ import org.json.JSONObject;
* @author sistem19user * @author sistem19user
*/ */
public interface SalidasDAO { public interface SalidasDAO {
public JSONArray obtenerHorariosSalida() throws Exception; public JSONArray obtenerHorariosSalida() throws Exception;
public JSONArray obtenerAsistencias(String horaProgramada) throws Exception;
public JSONObject actualizarSalida() throws Exception; public JSONObject actualizarSalida() throws Exception;
} }
...@@ -9,10 +9,9 @@ public class Main { ...@@ -9,10 +9,9 @@ public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Timer timer = new Timer(); Timer timer = new Timer();
// SalidasServices services = new SalidasServices(); SalidasServices services = new SalidasServices();
// System.out.println(services.obtenerHorariosSalida());
timer.schedule(new Temporizador(),0);
timer.schedule(new Temporizador(), 0, 3000);
} }
} }
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
*/ */
package salidasautomaticas.mysqldao; package salidasautomaticas.mysqldao;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSource;
import salidasautomaticas.dao.DAOFactory; import salidasautomaticas.dao.DAOFactory;
...@@ -18,47 +18,31 @@ import salidasautomaticas.dao.SalidasDAO; ...@@ -18,47 +18,31 @@ import salidasautomaticas.dao.SalidasDAO;
*/ */
public class MysqlDAOFactory extends DAOFactory { public class MysqlDAOFactory extends DAOFactory {
private static DataSource dataSourceMySQL;
static { static {
try { try {
createPoolConnectionsMySQL(); Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void createPoolConnectionsMySQL() { public static Connection obtenerConexion(String base) {
if (dataSourceMySQL == null) { Connection conexion = null;
BasicDataSource basicDataSource = new BasicDataSource(); if (base.equals("nuevo")) {
basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
basicDataSource.setUsername("billy");
basicDataSource.setPassword("billy");
basicDataSource.setUrl("jdbc:mysql://172.16.2.39:3306/nuevo");
basicDataSource.setMaxActive(30);
basicDataSource.setMaxIdle(30);
basicDataSource.setRemoveAbandoned(true);
basicDataSource.setRemoveAbandonedTimeout(5000);
basicDataSource.setMaxWait(6000);
dataSourceMySQL = basicDataSource;
}
}
public static Connection getConneccion(String bd) {
Connection connection = null;
if (bd.equals("nuevo")) {
try { try {
connection = dataSourceMySQL.getConnection(); conexion = DriverManager.getConnection(
"jdbc:mysql://172.16.2.39:3306/nuevo",
"root",
"mysql");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
return connection; return conexion;
} }
@Override @Override
public SalidasDAO getSalidasDAO() { public SalidasDAO getSalidasDAO() {
return new SalidasMysqlDAO(); return new SalidasMysqlDAO();
......
...@@ -7,6 +7,8 @@ package salidasautomaticas.mysqldao; ...@@ -7,6 +7,8 @@ package salidasautomaticas.mysqldao;
import java.sql.Connection; import java.sql.Connection;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Iterator;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import salidasautomaticas.dao.SalidasDAO; import salidasautomaticas.dao.SalidasDAO;
...@@ -20,18 +22,34 @@ public class SalidasMysqlDAO implements SalidasDAO { ...@@ -20,18 +22,34 @@ public class SalidasMysqlDAO implements SalidasDAO {
@Override @Override
public JSONArray obtenerHorariosSalida() throws Exception { public JSONArray obtenerHorariosSalida() throws Exception {
Connection cn = MysqlDAOFactory.getConneccion("nuevo"); Connection cn = MysqlDAOFactory.obtenerConexion("nuevo");
int diaSemana = obtenerDiaSemana(); int diaSemana = obtenerDiaSemana();
String query = "select ? as salida from personal inner join " String query = "SELECT ? as salida FROM (\n"
+ "cargo on (personal.codcargo=cargo.codcargo) where NULLIF(? , '') is NOT null and retirado=0 GROUP BY ?"; + "SELECT\n"
+ " p.salidaLV,\n"
+ " p.salidaS\n"
+ "FROM\n"
+ " asistencia a\n"
+ "INNER JOIN personal p ON a.codper = p.codper\n"
+ "INNER JOIN salidas_update su ON p.codcargo = su.codcargo\n"
+ "WHERE\n"
+ " a.fecha_asis BETWEEN '2018-04-03' AND '2018-04-05'\n"
+ "AND p.retirado = 0\n"
+ "AND p.horaing = su.horaLV\n"
+ "AND p.mining = su.minutoLV\n"
+ "AND p.horaingsabado = su.sabadoLV\n"
+ "AND p.miningsabado = su.sabadoS\n"
+ "AND NOT p.codlocal IN ('L035', 'L053', 'L038')\n"
+ "AND NOT p.pronoe = 1\n"
+ "AND NOT p.codcargo IN (71, 13)\n"
+ "AND NOT p.horaing > 19\n"
+ "AND NOT p.salidaS = '') as tabla WHERE NULLIF( ? , '') is NOT null GROUP BY ? ";
if (diaSemana >= 1 && diaSemana <= 5) { if (diaSemana >= 1 && diaSemana <= 5) {
query = query.replaceAll("\\?", "salidaLV"); query = query.replaceAll("\\?", "tabla.salidaLV");
} else if (diaSemana == 6) { } else if (diaSemana == 6) {
query = query.replaceAll("\\?", "salidaS"); query = query.replaceAll("\\?", "tabla.salidaS");
} }
JSONArray jsonArray = EstandarDAO.getInstance().consultar(cn, query); JSONArray jsonArray = EstandarDAO.getInstance().consultar(cn, query);
return jsonArray; return jsonArray;
...@@ -46,24 +64,95 @@ public class SalidasMysqlDAO implements SalidasDAO { ...@@ -46,24 +64,95 @@ public class SalidasMysqlDAO implements SalidasDAO {
} }
@Override @Override
public JSONArray obtenerAsistencias(String horaProgramada) throws Exception {
Connection cn = MysqlDAOFactory.obtenerConexion("nuevo");
int diaSemana = obtenerDiaSemana();
String query = "SELECT\n"
+ " asistencia.codasis,\n"
+ "asistencia.fecha_asis,\n"
+ "(WEEKDAY(asistencia.fecha_asis) + 1) AS numDia,\n"
+ "? \n"
+ "FROM\n"
+ " asistencia\n"
+ "INNER JOIN personal ON asistencia.codper = personal.codper\n"
+ "WHERE\n"
+ " asistencia.horasal = ''\n"
+ "AND asistencia.minsal = ''\n"
+ "AND ( ? <='*' AND asistencia.fecha_asis='2018-04-05')\n"
+ "OR (asistencia.fecha_asis BETWEEN '2018-04-03' AND '2018-04-04')\n"
+ "AND ? is not NULL \n"
+ "AND (WEEKDAY(asistencia.fecha_asis) + 1) !=7 AND personal.retirado=0 ";
if (diaSemana >= 1 && diaSemana <= 5) {
query = query.replaceAll("\\?", "personal.salidaLV");
} else if (diaSemana == 6) {
query = query.replaceAll("\\?", "personal.salidaS");
}
query = query.replaceAll("\\*", horaProgramada);
JSONArray jsonArray = EstandarDAO.getInstance().consultar(cn, query);
return jsonArray;
}
@Override
public JSONObject actualizarSalida() throws Exception { public JSONObject actualizarSalida() throws Exception {
JSONObject resultado = null; JSONObject resultado = null;
Connection cn = MysqlDAOFactory.getConneccion("nuevo"); Connection cn = MysqlDAOFactory.obtenerConexion("nuevo");
String queryPersonal = "select codper from personal where salidaLV='10:00' and retirado=0"; LocalTime horaSistema = LocalTime.now();
JSONArray jsonArray = EstandarDAO.getInstance().consultar(cn, queryPersonal); int cantMinSistema = horaSistema.getHour() * 60 + horaSistema.getMinute();
JSONArray listadoHoras = obtenerHorariosSalida();
for(int i=0; i<jsonArray.length(); i++){ System.out.println("listadoHoras " + listadoHoras);
JSONObject objJSON = (JSONObject) jsonArray.get(i); for (Iterator iterator = listadoHoras.iterator(); iterator.hasNext();) {
String codper = objJSON.getString("codper"); System.out.println("" + 1);
String query = "update asistencia set horasal=2, minsal=0 where codper=?"; int aleatorioHora = 0, aleatorioMin = 0;
JSONArray parametros = new JSONArray(); JSONObject objHora = (JSONObject) iterator.next();
parametros.put(codper); System.out.println("objHora " + objHora);
resultado = EstandarDAO.getInstance().ejecutar(cn, query); String[] splitHora = objHora.getString("salida").split(":");
int cantidadMin = Integer.parseInt(splitHora[0]) * 60 + Integer.parseInt(splitHora[1]);
if (cantidadMin > cantMinSistema) {
//Reprogramacion
System.out.println("Me reprogramo");
} else {
System.out.println("Programo");
JSONArray listadoProgramar = obtenerAsistencias(objHora.getString("salida"));
System.out.println("listadoProgrmar " + listadoProgramar);
for (Iterator iterator1 = listadoProgramar.iterator(); iterator1.hasNext();) {
JSONObject objPrograma = (JSONObject) iterator1.next();
String[] splitHoraConsulta = objPrograma.getString("salidaLV").split(":");
int min = Integer.parseInt(splitHoraConsulta[1]);
int hora = Integer.parseInt(splitHoraConsulta[0]);
System.out.println("obj " + objPrograma);
//Actualizacion de salidas
if (min == 0) {
aleatorioHora = (int) Math.floor(Math.random() * (hora - (hora - 1) + 1) + (hora - 1));
} else {
aleatorioHora = hora;
}
if (hora == aleatorioHora) {
if (min == 0) {
aleatorioMin = (int) Math.floor(Math.random() * (3 - 0 + 1) + 0);
} else {
aleatorioMin = (int) Math.floor(Math.random() * (33 - 27 + 1) + 27);
}
} else {
aleatorioMin = (int) Math.floor(Math.random() * (59 - 56 + 1) + 56);
}
String queryPersonal = "update asistencia set horasal= ?,minsal=? where codasis=? ";
JSONArray parametros = new JSONArray();
parametros.put(aleatorioHora);
parametros.put(aleatorioMin);
parametros.put(objPrograma.getString("codasis"));
// System.out.println("consulta "+queryPersonal+ " "+parametros);
resultado = EstandarDAO.getInstance().ejecutar(cn, queryPersonal, parametros);
}
}
} }
return resultado; return resultado;
} }
} }
...@@ -7,16 +7,19 @@ package salidasautomaticas.schedule; ...@@ -7,16 +7,19 @@ package salidasautomaticas.schedule;
import java.util.Date; import java.util.Date;
import java.util.TimerTask; import java.util.TimerTask;
import salidasautomaticas.services.SalidasServices;
/** /**
* *
* @author sistem19user * @author sistem19user
*/ */
public class Temporizador extends TimerTask{ public class Temporizador extends TimerTask{
SalidasServices service=new SalidasServices();
@Override @Override
public void run() { public void run() {
System.out.println(new Date()); service.actualizarSalidas();
System.out.println("Actualizar salida");
} }
} }
...@@ -26,4 +26,13 @@ public class SalidasServices { ...@@ -26,4 +26,13 @@ public class SalidasServices {
return listaHorarios; return listaHorarios;
} }
public JSONObject actualizarSalidas(){
JSONObject valor=new JSONObject();
try {
valor=salidadao.actualizarSalida();
} catch (Exception e) {
}
return valor;
}
} }
...@@ -77,6 +77,7 @@ public class EstandarDAO { ...@@ -77,6 +77,7 @@ public class EstandarDAO {
cont++; cont++;
} }
} }
// System.out.println("query "+ps);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
ResultSetMetaData rm = rs.getMetaData(); ResultSetMetaData rm = rs.getMetaData();
...@@ -90,8 +91,8 @@ public class EstandarDAO { ...@@ -90,8 +91,8 @@ public class EstandarDAO {
jsonArray.put(obj); jsonArray.put(obj);
} }
rs.close(); // rs.close();
cn.close(); // cn.close();
} }
} catch (Exception ex) { } catch (Exception ex) {
...@@ -130,13 +131,14 @@ public class EstandarDAO { ...@@ -130,13 +131,14 @@ public class EstandarDAO {
} }
} }
System.out.println("consulta ..... "+ps);
int filas = ps.executeUpdate(); int filas = ps.executeUpdate();
if (filas > 0) { if (filas > 0) {
obj.put("msg", filas); obj.put("msg", filas);
} else { } else {
obj.put("msg", 0); obj.put("msg", 0);
} }
cn.close(); // cn.close();
} }
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment