<?php

class InfoMetas_model extends CI_Model
{

    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('INFORMATION', TRUE);
    }

    function add($im_ic_id, $im_key, $im_value)
    {
        $sql = "INSERT INTO infoMetas \n"
            . "  ( \n"
            . "	im_ic_id, im_key, im_value \n"
            . "  ) \n"
            . "VALUES \n"
            . "  (  \n"
            . "	?, ?, N? \n"
            . "  )";
        return $this->HT->query($sql, array($im_ic_id, $im_key, (string) $im_value));
    }

    function get_list($im_ic_id, $im_key)
    {
        $sql = "
            SELECT im.im_id
                  ,im.im_ic_id
                  ,im.im_key
                  ,im.im_value
            FROM   infoMetas im
            WHERE  im.im_ic_id = ?
                   AND im.im_key = ?
                   ORDER BY im.im_id ASC
                ";
        $query = $this->HT->query($sql, array($im_ic_id, $im_key));
        return $query->result();
    }

    function get_list_by_key($im_ic_id, $im_key)
    {
        $sql = "
            SELECT im.im_id
                  ,im.im_ic_id
                  ,im.im_key
                  ,im.im_value
            FROM   infoMetas im
            WHERE  im.im_ic_id = ?
                   AND im.im_key = ?
                   ORDER BY im.im_id ASC
                ";
        $query = $this->HT->query($sql, array($im_ic_id, $im_key));
        return $query->result();
    }

    //列表所有含AMPmeta的信息。
    public function list_amp($site_code)
    {
        $sql = "
            SELECT im.im_id
                  ,im.im_ic_id
                  ,c.ic_url
                  ,c.ic_id
                  ,empty_tpl = case when c.ic_template in ('r_tpl_empty', 'r_empty_container', 'r_empty2', 'r_tpl_empty_2017', 'tpl_empty', 'tpl_empty_h1', 'tpl_empty_navi_h1') then 'yes' else 'no' end
            FROM   infoMetas im
                   INNER JOIN infoContents c
                    ON c.ic_id = im.im_ic_id
                   LEFT JOIN infoMetas im2
                    ON im2.im_ic_id = im.im_ic_id AND im2.im_key = 'AMP_STATUS'
                   LEFT JOIN infoMetas im3
                    ON im3.im_ic_id = im.im_ic_id AND im2.im_key = 'AMP_SCHEMA'
            WHERE  im.im_key = 'AMP_JSON'
                   AND c.ic_sitecode = ?
                   AND c.ic_status = 1
                   AND CONVERT(varchar, im2.im_value) = '1'
        ";
        $query = $this->HT->query($sql, array($site_code));
        return $query->result();
    }

    //列表所有不含AMPmeta的信息。
    public function list_no_amp($site_code)
    {
        $sql = "
            SELECT c0.ic_id, c0.ic_url
            FROM infoContents c0 
            WHERE c0.ic_id not in (
                SELECT c.ic_id
                FROM   infoMetas im
                       LEFT JOIN infoContents c
                        ON c.ic_id = im.im_ic_id
                       LEFT JOIN infoMetas im2
                        ON im2.im_ic_id = im.im_ic_id AND im2.im_key = 'AMP_STATUS'
                       LEFT JOIN infoMetas im3
                        ON im3.im_ic_id = im.im_ic_id AND im2.im_key = 'AMP_SCHEMA'
                WHERE  im.im_key = 'AMP_JSON'
                       AND c.ic_sitecode = ?
                       AND c.ic_status = 1
                       AND CONVERT(varchar, im2.im_value) = '1'
                ) 
            AND c0.ic_sitecode = ? 
            AND c0.ic_status = 1 
            AND c0.ic_url <> '' 
            AND datalength(c0.ic_content) > 0
            AND CHARINDEX('search', c0.ic_url) = 0
            AND CHARINDEX('faq', c0.ic_url) = 0
            AND CHARINDEX('feedback', c0.ic_url) = 0
            AND CHARINDEX('test', c0.ic_url) = 0
        ";
        $query = $this->HT->query($sql, array($site_code, $site_code));
        return $query->result();
    }

    //按节点列表信息（含meta数据）
    function list_info_by_path_with_meta($is_path, $site_code)
    {
        $sql = "SELECT  is1.is_id, \n"
            . "       is1.is_path, \n"
            . "       ic.ic_id, \n"
            . "       ic.ic_url, \n"
            . "       ic.ic_status, \n"
            . "       ic.ic_template, \n"
            . "       ic.ic_sitecode, \n"
            . "       ic.ic_content, \n"
            . "       ic.ic_photo, \n"
            . "       ic.ic_title, \n"
            . "       ic.ic_type, \n"
            . "       im.im_id \n"
            . "       FROM   infoStructures is1 \n"
            . "       INNER JOIN infoContents ic ON ic.ic_id = is1.is_ic_id \n"
            . "            AND ic.ic_sitecode = is1.is_sitecode \n"
            . "       LEFT JOIN infoMetas im ON im.im_ic_id = ic.ic_id AND im.im_key=?"
            . " WHERE  is1.is_path LIKE '$is_path%' AND is_sitecode = ? AND ic_status = 1"
            . " ORDER BY is1.is_level, is1.is_sort, is1.is_path";
        $query = $this->HT->query($sql, array('AMP_JSON', $site_code));
        //print_r($this->HT->queries);
        return $query->result();
    }

    function get($im_ic_id, $im_key)
    {
        $sql = "SELECT im.im_value \n"
            . "FROM   infoMetas im \n"
            . "WHERE  im.im_ic_id = ? \n"
            . "       AND im.im_key = ?";
        $query = $this->HT->query($sql, array($im_ic_id, $im_key));
        if ($query->num_rows() > 0) {
            return $query->row()->im_value;
        } else {
            return false;
        }
    }

    function get_gm($im_ic_id, $im_key)
    {
        $sql = "SELECT im.im_value \n"
            . "FROM   information_gm.dbo.infoMetas im \n"
            . "WHERE  im.im_ic_id = ? \n"
            . "       AND im.im_key = ?";
        $query = $this->HT->query($sql, array($im_ic_id, $im_key));
        if ($query->num_rows() > 0) {
            return $query->row()->im_value;
        } else {
            return false;
        }
    }

    function detail($im_ic_id, $im_key)
    {
        $sql = "SELECT im.im_value \n"
            . "FROM   infoMetas im \n"
            . "WHERE  im.im_ic_id = ? \n"
            . "       AND im.im_key = ? ORDER BY im.im_id asc";
        $query = $this->HT->query($sql, array($im_ic_id, $im_key));
        return $query->result();
    }

    //获取未收录的信息
    public function get_unembody_content($datetime, $top_num = 1)
    {
        $sql = "  SELECT TOP $top_num 
                            im_ic_id,
                            im_value,
                            ic_url,
                            ic_sitecode,
                            log_ht_usercode AS ic_author
                FROM        infoMetas 
                            INNER JOIN infoStructures ON is_ic_id=im_ic_id
                            INNER JOIN infoContents ON ic_id=im_ic_id
                            INNER JOIN infologs ON log_res_id=is_id
                WHERE       im_key = 'meta_embody' 
                            AND convert(varchar(500),im_value) != '1' 
                            AND convert(varchar(500),im_value)<N?
                ORDER BY im_id DESC";
        $query = $this->HT->query($sql, array($datetime));
        return $query->result();
    }

    function update($im_ic_id, $im_key, $im_value)
    {
        $sql = "UPDATE infoMetas \n"
            . "SET    im_value    = N? \n"
            . "WHERE  im_ic_id    = ? \n"
            . "       AND im_key  = ?";
        return $this->HT->query($sql, array((string) $im_value, $im_ic_id, $im_key));
    }

    function delete($im_ic_id, $im_key)
    {
        $sql = "DELETE  \n"
            . "FROM   infoMetas \n"
            . "WHERE  im_ic_id = ? \n"
            . "       AND im_key = ?";
        return $this->HT->query($sql, array($im_ic_id, $im_key));
    }

    function update_by_id($im_id, $im_value)
    {
        $sql = "UPDATE infoMetas \n"
            . "SET    im_value    = N? \n"
            . "WHERE  im_id    = ? \n";
        return $this->HT->query($sql, array($im_value, $im_id));
    }

    function delete_by_id($im_id)
    {
        $sql = "DELETE  \n"
            . "FROM   infoMetas \n"
            . "WHERE  im_id = ? \n";
        return $this->HT->query($sql, array($im_id));
    }

}
