<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 *
 * Model Parser_model
 *
 * This Model for ...
 *
 * @package        CodeIgniter
 * @category    Model
 * @author    Setiawan Jodi <jodisetiawan@fisip-untirta.ac.id>
 * @link      https://github.com/setdjod/myci-extension/
 * @param     ...
 * @return    ...
 *
 */

class Parser_model extends CI_Model
{

    // ------------------------------------------------------------------------

    public function __construct()
    {
        parent::__construct();
        $this->db->save_queries = false;
    }

    public function inicioParser()
    {
        $data = array(
            'cron_ejecucion' => 1,
            "fecha_ejecucion" => date("Y-m-d h:i:s"),
        );

        $this->db->where('id', 0);
        $this->db->update('config_parametros_cron', $data);
    }

    public function stopParser()
    {
        $data = array(
            'cron_ejecucion' => 0,
            "fecha_ejecucion" => date("Y-m-d h:i:s"),

        );

        $this->db->where('id', 0);
        $this->db->update('config_parametros_cron', $data);
    }

    public function existCronRun()
    {
        $sql = "SELECT * FROM config_parametros_cron";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->row();
        $datos['num_rows'] = $result->num_rows();
        $result->free_result();
        return $datos;
    }

    public function getUrlArchivos()
    {
        $sql = "SELECT * FROM config_tipo_licitacion_tablas WHERE activo = 1";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result();
        $datos['num_rows'] = $result->num_rows();
        $result->free_result();
        return $datos;
    }

    public function setInicioLicitacion($licitacionArr)
    {
        $this->db->insert('in_datos_iniciales', $licitacionArr);
        return $this->db->insert_id();
    }

    public function getMapaTablas()
    {
        $sql = "SELECT * FROM config_tablas_parser ORDER BY orden ASC";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result();
        $datos['num_rows'] = $result->num_rows();
        $result->free_result();
        return $datos;
    }

    public function truncateTables($tabla)
    {
        $this->db->truncate($tabla);
    }

    public function getDatosBrutos($tabla)
    {
        $sql = "SELECT * FROM $tabla";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result();
        $datos['num_rows'] = $result->num_rows();
        $result->free_result();
        return $datos;
    }

    public function getLicitacionesNuevasBruto()
    {
        $sql = "SELECT DISTINCT id_licitacion FROM in_datos_iniciales";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result();
        $datos['num_rows'] = $result->num_rows();
        $result->free_result();
        return $datos;
    }

    public function deleteCascadaNuevasLicitaciones()
    {
        $sql = "DELETE
                FROM global_datos_iniciales_licitacion
                WHERE id_licitacion IN(
                    SELECT DISTINCT id_licitacion FROM in_datos_iniciales
                )";
        $result = $this->db->query($sql);
        $datos['num_rows'] = $this->db->affected_rows();
        return $datos;
    }

    public function getMapaByTabla($tabla)
    {
        $sql = "SELECT * FROM $tabla";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result();
        $datos['num_rows'] = $result->num_rows();
        $result->free_result();
        return $datos;
    }

    public function setFechaNuevaImportacion($fechaImportacionLog)
    {
        $data = array(
            "fecha" => $fechaImportacionLog,
        );
        $this->db->insert('config_fecha_importaciones', $data);
        return $this->db->insert_id();
    }

    public function getDatosEmailEnvio()
    {
        $sql = "SELECT * FROM config_datos_emails";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->row();
        $datos['num_rows'] = $result->num_rows();
        return $datos;
    }
}

/* End of file Parser_model.php */
/* Location: ./application/models/Parser_model.php */
