<?php

class Information_model extends CI_Model
{

  var $topNum = false;
  var $orderBy = false;
  var $search_title = false;
  var $search_url = false;
  var $search = false;
  var $path = false;
  var $level = false;
  var $is_parent_id = false;
  var $is_id_array = false;
  var $ic_url_is_id = false;
  var $ic_ht_area_type = false;
  var $ic_ht_area_id = false;
  var $ic_type = false;
  var $ic_id = false;

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

  function init()
  {
    $this->topNum = false;
    $this->orderBy = " ORDER BY  ic_datetime DESC  ";
    $this->search_title = false;
    $this->search_url = false;
    $this->search = false;
    $this->path = false;
    $this->level = false;
    $this->is_parent_id = false;
    $this->is_id_array = false;
    $this->ic_url_is_id = false;
    $this->ic_ht_area_type = false;
    $this->ic_ht_area_id = false;
    $this->ic_id = false;
  }

  function get_last_edit_list($topNum = 24)
  {
    $this->init();
    $this->topNum = $topNum;
    return $this->GetList();
  }

  function search($keyword)
  {
    $this->init();
    $this->topNum = 48;
    if (is_numeric($keyword)) {
      $this->search_title = "AND ic_id=" . $this->HT->escape($keyword);
    } else {
      $sql_keyword = '%' . $this->HT->escape_like_str($keyword) . '%';
      $this->search_title = "AND (ic_url_title like N'$sql_keyword' OR ic_title like N'$sql_keyword' )";
    }
    return $this->GetList();
  }

  function search_all_text($keyword, $topnum = 24)
  {
    $this->init();
    $this->topNum = $topnum;
    $sql_keyword = '%' . $this->HT->escape_like_str($keyword) . '%';
    $this->search = " AND  (ic_content like N'$sql_keyword' 
                                or ic.ic_photo like N'$sql_keyword'
                                or map.im_value like N'$sql_keyword'
                                or mapm.im_value like N'$sql_keyword')";
    return $this->GetList();
  }

  //根据关键词来搜索内容
  function search_by_words($url, array $words, $exclude_ids)
  {
    $this->init();
    $this->topNum = 1;
    $sql_keyword = ' AND ( 1=1 ';
    foreach ($words as $item) {
      $sql_keyword .= " AND ic_title like '%" . $this->HT->escape_like_str(trim($item)) . "%' ";
    }
    $sql_keyword .= ' ) ';
    $this->search = " AND ic_status=1 AND ic_url LIKE '$url%' " . $sql_keyword . ' AND is_id NOT IN(' . implode(',', $exclude_ids) . ',0)';
    $this->orderBy = " ORDER BY is1.is_level ASC, is1.is_sort ASC,ic_datetime DESC ";
    return $this->GetList('ic_id, is_id, ic_url,ic_url_title,ic_title,is_path,is_level,ic_status,ic.ic_photo');
  }

  //在当前节点下搜索关键词
  function search_by_words_2($path, array $words, $exclude_ids)
  {
    $this->init();
    $this->topNum = 1;
    $sql_keyword = ' AND ( 1=1 ';
    foreach ($words as $item) {
      $sql_keyword .= " AND ic_title like '%" . $this->HT->escape_like_str(trim($item)) . "%' ";
    }
    $sql_keyword .= ' ) ';
    $this->path = " AND is1.is_path LIKE '$path%' ";
    $this->search = ' AND ic_status=1  AND is_id NOT IN(' . implode(',', $exclude_ids) . ',0) ' . $sql_keyword;
    $this->orderBy = " ORDER BY is1.is_level ASC, is1.is_sort ASC,ic_datetime DESC ";
    return $this->GetList('ic_id, is_id, ic_url,ic_url_title,ic_title,is_path,is_level,ic_status,ic.ic_photo');
  }

  //在同级别下，随机获取N条数据，并且排除某些id
  function random($topnum, $is_path, array $exclude_ids)
  {
    $this->init();
    $this->topNum = $topnum;
    $this->search = " AND is1.is_path LIKE '$is_path%' ";
    //$this->search = " AND is_parent_id =". $this->HT->escape($is_parent_id);
    $exclude_ids_string = implode(',', $exclude_ids);
    $this->search .= " AND ic_status=1  AND is_id NOT in ($exclude_ids_string,0)";
    $this->orderBy = " ORDER BY  NewID()  ";
    return $this->GetList('ic_id, is_id, ic_url,ic_url_title,ic_title,is_path,is_level,ic_status,ic.ic_photo');
  }

  //在urls列表里面，随机获取N条数据，并且排除某些id
  function random_range($topnum, array $url_array, array $exclude_ids)
  {
    $this->init();
    $this->topNum = $topnum;
    $this->search = ' AND ( 1=2 ';
    foreach ($url_array as $item) {
      $this->search .= ' OR ic_url= ' . $this->HT->escape(trim($item));
    }
    $this->search .= ' )';
    $exclude_ids_string = implode(',', $exclude_ids);
    $this->search .= " AND ic_status=1  AND is_id NOT in ($exclude_ids_string,0)";
    $this->orderBy = " ORDER BY  NewID()  ";
    return $this->GetList('ic_id, is_id, ic_url,ic_url_title,ic_title,is_path,is_level,ic_status,ic.ic_photo');
  }

  //根据url搜索信息
  function search_url($url)
  {
    $this->init();
    $this->topNum = 24;
    $this->search_url = ' AND ic_url= ' . $this->HT->escape($url);
    return $this->GetList();
  }

  //根据根节点路径获取子节点列表
  function get_list_by_path($path, $level = false, $site_code = false, $filed = false)
  {
    $this->init();
    if ($level !== false) {
      $this->level = " AND is1.is_level= '$level' ";
    }
    $this->path = " AND is1.is_path LIKE '$path%' ";
    $this->orderBy = ' ORDER BY is1.is_level ASC, is1.is_sort ASC,is1.is_path ASC ';
    return $this->GetList($filed, $site_code);
  }

  //根据根节点路径获取子节点列表
  function get_list_by_path_gm($path, $level = false, $site_code = false, $filed = false)
  {
    $this->init();
    if ($level !== false) {
      $this->level = " AND is1.is_level= '$level' ";
    }
    $this->path = " AND is1.is_path LIKE '$path%' ";
    $this->orderBy = ' ORDER BY is1.is_level ASC, is1.is_sort ASC,is1.is_path ASC ';
    return $this->GetList_gm($filed, $site_code);
  }

  /**
   * 获取当前页面的面包屑，不包括当前节点
   */
  function get_path_exclude_self($is_id, $path)
  {
    $site_code = $this->config->item('site_code');
    $path_query = $this->HT->query("select 
        ic_id, is_id, ic_url,ic_url_title,ic_title,is_path,is_level,ic_status
        from infoStructures a inner join infoContents b on a.is_ic_id=b.ic_id 
        where  is_id in ($path 0) and is_id <> ? and is_siteCode = ? order by is_level asc", array($is_id, $site_code));
    $path_result = $path_query->result();
    $path_list = [];

    $group_map = [
      278008010 => [
        'ic_url' => '/travelguide/culture/',
        'ic_url_title' => 'Chinese Culture'
      ],
      278008011 => [
        'ic_url' => '/travelguide/',
        'ic_url_title' => 'Travel Guide'
      ],
      278008012 => [
        'ic_url' => '/aboutus/',
        'ic_url_title' => 'About Us'
      ],
      278008013 => [
        'ic_url' => '/citytour/',
        'ic_url_title' => 'City Tours'
      ],
      278008014 => [
        'ic_url' => '/tour/',
        'ic_url_title' => 'China Tours'
      ]
    ];

    foreach ($path_result as $path_row) {

      $ic_url = $path_row->ic_url;
      $ic_url_title = $path_row->ic_url_title;

      if ($path_row->is_level === 0) {
        if (array_key_exists($path_row->is_id, $group_map)) {
          $top_group = $group_map[$path_row->is_id];
          $path_array = [
            'ic_id' => $path_row->ic_id,
            'is_id' => $path_row->is_id,
            'ic_title' => $path_row->ic_title,
            'ic_url' => $top_group['ic_url'],
            'ic_url_title' => $top_group['ic_url_title']
          ];
          $path_list[] = $path_array;
        }
      } else if ($path_row->ic_status === 1) {

        $path_array = [
          'ic_id' => $path_row->ic_id,
          'is_id' => $path_row->is_id,
          'ic_title' => $path_row->ic_title,
          'ic_url' => $path_row->ic_url,
          'ic_url_title' => $path_row->ic_url_title
        ];
        $path_list[] = $path_array;
      }
    }

    return $path_list;
  }

  /**
   * 获取当前页面的面包屑，不包括当前节点
   */
  function get_breadcrumb_data($is_id, $path, $group_map)
  {
    $path_query = $this->HT->query("select 
        ic_id, is_id, ic_url,ic_url_title,ic_title,is_path,is_level,ic_status
        from infoStructures a inner join infoContents b on a.is_ic_id=b.ic_id 
        where  is_id in ($path 0) and is_id <> ? order by is_level asc", array($is_id));
    $path_result = $path_query->result();
    $path_list = [];

    foreach ($path_result as $path_row) {

      $ic_url = $path_row->ic_url;
      $ic_url_title = $path_row->ic_url_title;

      if ($path_row->is_level === 0) {
        if (array_key_exists($path_row->is_id, $group_map)) {
          $top_group = $group_map[$path_row->is_id];
          $path_array = [
            'ic_id' => $path_row->ic_id,
            'is_id' => $path_row->is_id,
            'ic_title' => $path_row->ic_title,
            'ic_url' => $top_group['ic_url'],
            'ic_url_title' => $top_group['ic_url_title']
          ];
          $path_list[] = $path_array;
        }
      } else if ($path_row->ic_status === 1) {

        $path_array = [
          'ic_id' => $path_row->ic_id,
          'is_id' => $path_row->is_id,
          'ic_title' => $path_row->ic_title,
          'ic_url' => $path_row->ic_url,
          'ic_url_title' => $path_row->ic_url_title
        ];
        $path_list[] = $path_array;
      }
    }

    return $path_list;
  }

  //根据路径获取某一级别节点详细页
  function get_detail_by_path($path, $level)
  {
    $this->init();
    $this->topNum = 1;
    $this->level = " AND is1.is_level= '$level' ";
    $this->is_id_array = " AND is1.is_id IN  ($path 0) ";
    $this->orderBy = ' ORDER BY is1.is_level ASC, is1.is_sort ASC,is1.is_path ASC ';
    return $this->GetList();
  }
  //根据节点ID列表获取信息
  function get_detail_by_ids($is_ids)
  {
    $this->init();
    $this->is_id_array = " AND is1.is_id IN  ($is_ids) ";
    $this->orderBy = ' ORDER BY is1.is_level ASC, is1.is_sort ASC,is1.is_path ASC ';
    return $this->GetList();
  }

  //根据根节点ID获取子节点列表
  function get_list_by_id($is_parent_id, $level = false)
  {
    $this->init();
    if ($level !== false) {
      $this->level = " AND is1.is_level= '$level' ";
    }
    $this->is_parent_id = " AND is1.is_parent_id = '$is_parent_id' ";
    $this->orderBy = ' ORDER BY is1.is_level ASC, is1.is_sort ASC,is1.is_path ASC ';
    return $this->GetList('is1.is_id,  ic.ic_id, ic.ic_url, ic.ic_url_title, ic.ic_title, ic.ic_datetime, ic.ic_author');
  }


  function Detail($ic_url_is_id, $filed = '', $site_code = '')
  {
    if (empty($ic_url_is_id)) {
      return false;
    }
    $this->init();
    $this->topNum = 1;
    if (is_numeric($ic_url_is_id)) {
      $this->ic_url_is_id = " AND is1.is_id=  " . $this->HT->escape($ic_url_is_id);
    } else {
      $this->ic_url_is_id = " AND ic.ic_url =  " . $this->HT->escape($ic_url_is_id);
    }
    return $this->GetList($filed, $site_code);
  }

  function detail_by_ic_id($ic_id)
  {
    if (empty($ic_id)) {
      return false;
    }
    $this->init();
    $this->topNum = 1;
    $this->ic_url_is_id = " AND ic.ic_id =  " . $this->HT->escape($ic_id);
    return $this->GetList();
  }

  function GetList($filed = "", $site_code = "")
  {
    $this->topNum ? $sql = "SELECT TOP " . $this->topNum : $sql = "SELECT ";
    if (empty($filed)) {
      $sql .= "         is1.is_id, \n"
        . "       is1.is_parent_id, \n"
        . "       is1.is_path, \n"
        . "       is1.is_level, \n"
        . "       is1.is_sort, \n"
        . "       is1.is_sitecode, \n"
        . "       is1.is_datetime, \n"
        . "       is1.is_ic_id, \n"
        . "       ic.ic_id, \n"
        . "       ic.ic_url, \n"
        . "       ic.ic_url_title, \n"
        . "       ic.ic_type, \n"
        . "       ic.ic_title, \n"
        . "       ic.ic_content, \n"
        . "       ic.ic_summary, \n"
        . "       ic.ic_seo_title, \n"
        . "       ic.ic_seo_description, \n"
        . "       ic.ic_seo_keywords, \n"
        . "       ic.ic_show_bread_crumbs, \n"
        . "       ic.ic_status, \n"
        . "       ic.ic_template, \n"
        . "       ic.ic_photo, \n"
        . "       ic.ic_photo_width, \n"
        . "       ic.ic_photo_height, \n"
        . "       ic.ic_sitecode, \n"
        . "       ic.ic_recommend_tours, \n"
        . "       ic.ic_recommend_packages, \n"
        . "       ic.ic_datetime, \n"
        . "       ic.ic_ht_area_id, \n"
        . "       ic.ic_ht_area_type, \n"
        . "       ic.ic_ht_product_id, \n"
        . "       ic.ic_ht_product_type, \n"
        . "       ic.SRMS_SIC_Code, \n"
        . "       ic.SRMS_SIIT_Code, \n"
        . "       ic.ic_author \n";
    } else {
      $sql .= " $filed ";
    }
    //添加查询AMP发布状态
    //$sql.=",isnull((select top 1 CONVERT(varchar, im_value) from infoMetas where im_ic_id=ic_id and im_key='AMP_STATUS'),0) as amp_status ";
    $sql .= ",0 as amp_status ";
    $sql .= "FROM   infoStructures is1 \n"
      . "       INNER JOIN infoContents ic ON ic.ic_id = is1.is_ic_id \n"
      . "       left JOIN infoMetas map on ic.ic_id = map.im_ic_id AND map.im_key = 'meta_addon_picture' \n"
      . "       left JOIN infoMetas mapm on ic.ic_id = mapm.im_ic_id AND mapm.im_key = 'meta_addon_picture_mobile' \n"
      . "            AND ic.ic_sitecode = is1.is_sitecode \n"
      . "WHERE  is1.is_sitecode = ? ";
    $this->search_title ? $sql .= $this->search_title : false;
    $this->search_url ? $sql .= $this->search_url : false;
    $this->search ? $sql .= $this->search : false;
    $this->path ? $sql .= $this->path : false;
    $this->level ? $sql .= $this->level : false;
    $this->is_parent_id ? $sql .= $this->is_parent_id : false;
    $this->is_id_array ? $sql .= $this->is_id_array : false;
    $this->ic_url_is_id ? $sql .= $this->ic_url_is_id : false;
    $this->ic_id ? $sql .= $this->ic_id : false;
    $this->ic_type ? $sql .= $this->ic_type : false;
    $this->ic_ht_area_type ? $sql .= $this->ic_ht_area_type : false;
    $this->ic_ht_area_id ? $sql .= $this->ic_ht_area_id : false;

    $this->orderBy ? $sql .= $this->orderBy : false;

    $query = $this->HT->query($sql, array($site_code ? $site_code : $this->config->item('site_code')));
    //print_r($this->HT->queries);
    if ($this->topNum === 1) {
      if ($query->num_rows() > 0) {
        $row = $query->row();
        return $row;
      } else {
        return FALSE;
      }
    } else {
      return $query->result();
    }
  }

  function GetList_gm($filed = "", $site_code = "")
  {
    $this->topNum ? $sql = "SELECT TOP " . $this->topNum : $sql = "SELECT ";
    if (empty($filed)) {
      $sql .= "         is1.is_id, \n"
        . "       is1.is_parent_id, \n"
        . "       is1.is_path, \n"
        . "       is1.is_level, \n"
        . "       is1.is_sort, \n"
        . "       is1.is_sitecode, \n"
        . "       is1.is_datetime, \n"
        . "       is1.is_ic_id, \n"
        . "       ic.ic_id, \n"
        . "       ic.ic_url, \n"
        . "       ic.ic_url_title, \n"
        . "       ic.ic_type, \n"
        . "       ic.ic_title, \n"
        . "       ic.ic_content, \n"
        . "       ic.ic_summary, \n"
        . "       ic.ic_seo_title, \n"
        . "       ic.ic_seo_description, \n"
        . "       ic.ic_seo_keywords, \n"
        . "       ic.ic_show_bread_crumbs, \n"
        . "       ic.ic_status, \n"
        . "       ic.ic_template, \n"
        . "       ic.ic_photo, \n"
        . "       ic.ic_photo_width, \n"
        . "       ic.ic_photo_height, \n"
        . "       ic.ic_sitecode, \n"
        . "       ic.ic_recommend_tours, \n"
        . "       ic.ic_recommend_packages, \n"
        . "       ic.ic_datetime, \n"
        . "       ic.ic_ht_area_id, \n"
        . "       ic.ic_ht_area_type, \n"
        . "       ic.ic_ht_product_id, \n"
        . "       ic.ic_ht_product_type, \n"
        . "       ic.SRMS_SIC_Code, \n"
        . "       ic.SRMS_SIIT_Code, \n"
        . "       ic.ic_author \n";
    } else {
      $sql .= " $filed ";
    }
    //添加查询AMP发布状态
    //$sql.=",isnull((select top 1 CONVERT(varchar, im_value) from infoMetas where im_ic_id=ic_id and im_key='AMP_STATUS'),0) as amp_status ";
    $sql .= ",0 as amp_status ";
    $sql .= "FROM   information_gm.dbo.infoStructures is1 \n"
      . "       INNER JOIN information_gm.dbo.infoContents ic ON ic.ic_id = is1.is_ic_id \n"
      . "       left JOIN information_gm.dbo.infoMetas map on ic.ic_id = map.im_ic_id AND map.im_key = 'meta_addon_picture' \n"
      . "       left JOIN information_gm.dbo.infoMetas mapm on ic.ic_id = mapm.im_ic_id AND mapm.im_key = 'meta_addon_picture_mobile' \n"
      . "            AND ic.ic_sitecode = is1.is_sitecode \n"
      . "WHERE  is1.is_sitecode = ? ";
    $this->search_title ? $sql .= $this->search_title : false;
    $this->search_url ? $sql .= $this->search_url : false;
    $this->search ? $sql .= $this->search : false;
    $this->path ? $sql .= $this->path : false;
    $this->level ? $sql .= $this->level : false;
    $this->is_parent_id ? $sql .= $this->is_parent_id : false;
    $this->is_id_array ? $sql .= $this->is_id_array : false;
    $this->ic_url_is_id ? $sql .= $this->ic_url_is_id : false;
    $this->ic_id ? $sql .= $this->ic_id : false;
    $this->ic_type ? $sql .= $this->ic_type : false;
    $this->ic_ht_area_type ? $sql .= $this->ic_ht_area_type : false;
    $this->ic_ht_area_id ? $sql .= $this->ic_ht_area_id : false;

    $this->orderBy ? $sql .= $this->orderBy : false;

    $query = $this->HT->query($sql, array($site_code ? $site_code : $this->config->item('site_code')));
    //print_r($this->HT->queries);
    if ($this->topNum === 1) {
      if ($query->num_rows() > 0) {
        $row = $query->row();
        return $row;
      } else {
        return FALSE;
      }
    } else {
      return $query->result();
    }
  }

  //根据区域信息获取根节点
  function GetRoot($ic_ht_area_type, $ic_ht_area_id)
  {
    $sql = "SELECT TOP 1 is1.is_id, \n"
      . "       is1.is_parent_id, \n"
      . "       is1.is_path, \n"
      . "       is1.is_level, \n"
      . "       ic.ic_url_title \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"
      . "WHERE  is1.is_sitecode = ? \n"
      . "       AND ic.ic_ht_area_type = ? \n"
      . "       AND ic.ic_ht_area_id = ? \n"
      . "ORDER BY \n"
      . "       is1.is_level ASC, \n"
      . "       is1.is_sort ASC, \n"
      . "       is1.is_path ASC \n";
    $query = $this->HT->query($sql, array($this->config->item('site_code'), $ic_ht_area_type, $ic_ht_area_id));
    if ($query->result()) {
      $row = $query->row();
      return $row;
    } else {
      return FALSE;
    }
  }

  //根据信息树id获取省份代号
  public function get_province_by_isid($is_id)
  {
    $sql = "  SELECT  top 1  CII_PRI_SN 
                FROM    infoStructures is1 
                        INNER JOIN infoContents ic ON ic.ic_id = is1.is_ic_id 
                        INNER JOIN CItyInfo ON ic.ic_ht_area_id=CII_SN
                WHERE   is1.is_id=?";
    $query = $this->HT->query($sql, array($is_id));
    $result = $query->result();
    if (!empty($result)) {
      return $result[0]->CII_PRI_SN;
    } else {
      return false;
    }
  }

  //获取结构列表
  function StructureList($is_id)
  {
    $sql = "SELECT is1.is_id AS id, \n"
      . "       is1.is_parent_id AS pId, \n"
      . "       ISNULL(ic.ic_url_title,'New Information') AS name , \n"
      . "       ISNULL(ic.ic_status,0) AS status, \n"
      . "       is1.is_path, \n"
      . "       is1.is_id \n"
      . "FROM   infoStructures is1 \n"
      . "       INNER JOIN infoContents ic ON ic.ic_id = is1.is_ic_id \n"
      . "WHERE  is1.is_id = ? \n"
      . "       OR  ',' + is1.is_path LIKE '%,$is_id,%' \n"
      . "ORDER BY \n"
      . "       is1.is_level ASC, \n"
      . "       is1.is_sort ASC, \n"
      . "       is1.is_path ASC \n";
    $query = $this->HT->query($sql, array($is_id));
    //print_r($this->HT->queries);
    return $query->result();
  }

  //检测链接是否重复
  function URLcheck($is_id, $ic_url)
  {
    $sql = "SELECT TOP 1 is1.is_id  \n"
      . "FROM   infoStructures is1 \n"
      . "       INNER JOIN infoContents ic ON ic.ic_id = is1.is_ic_id \n"
      . "WHERE  is1.is_id <> ? \n "
      . "       AND ic.ic_url = ? \n"
      . "       AND ic.ic_sitecode=? ";
    $query = $this->HT->query($sql, array($is_id, $ic_url, $this->config->item('site_code')));
    //print_r($this->HT->queries);
    if ($query->num_rows() > 0) {
      return false;
    } else {
      return true;
    }
  }

  //获取没有绑定的景点列表
  function get_unlink_landscape_list($city_id)
  {
    $sql = "SELECT TOP 8 lsi.LSI_SN, \n"
      . "       lsi.LSI_City, \n"
      . "       lsi2.LSI2_Name, \n"
      . "       ci2.CII2_Name \n"
      . "FROM   tourmanager.dbo.LandScapeInfo lsi \n"
      . "       INNER JOIN tourmanager.dbo.LandScapeInfo2 lsi2 ON lsi2.LSI2_LSI_SN = lsi.LSI_SN \n"
      . "            AND lsi2.LSI2_LGC = ? \n"
      . "       INNER JOIN tourmanager.dbo.CItyInfo2 ci2 ON ci2.CII2_CII_SN = lsi.LSI_City \n"
      . "            AND ci2.CII2_LGC = ? \n"
      . "WHERE  lsi.LSI_Publish = 1 \n"
      . "       AND (lsi.LSI_ParentSN IS NULL OR lsi.LSI_ParentSN = 0) \n"
      . "       AND lsi.LSI_City = ? \n"
      . "       AND NOT EXISTS( \n"
      . "               SELECT TOP 1 1 \n"
      . "               FROM   infoContents ic \n"
      . "               WHERE  ic.ic_ht_product_type = 't' \n"
      . "                      AND ic.ic_ht_product_id = lsi.LSI_SN \n"
      . "           ) \n"
      . "ORDER BY \n"
      . "       lsi2.LSI2_SN DESC";
    $query = $this->HT229->query($sql, array($this->config->item('site_lgc'), $this->config->item('site_lgc'), $city_id));
    return $query->result();
  }

  //获取根节点列表
  function root_type_list()
  {
    $this->init();
    $this->orderBy = " ORDER BY ic.ic_url_title ASC ";
    $this->level = " AND is1.is_level=1 ";
    $this->is_parent_id = " AND is1.is_parent_id=0 ";
    $this->ic_type = " AND ic.ic_type='root' ";
    return $this->GetList();
  }

  //获取某个信息类型详情
  function get_type_detail($type_name, $area_type, $area_id)
  {
    $this->init();
    $this->topNum = 1;
    $this->orderBy = " ORDER BY is1.is_level ASC ";
    $this->ic_ht_area_type = " AND ic.ic_ht_area_type= " . $this->HT->escape($area_type);
    $this->ic_ht_area_id = " AND ic.ic_ht_area_id=" . $this->HT->escape($area_id);
    $this->ic_type = " AND ic.ic_type= " . $this->HT->escape($type_name);
    return $this->GetList();
  }

  //获取3年未更新的信息
  function get_oldest_info($yeardiff = 3, $has_no_pub = true, $empty_info = true)
  {
    $has_no_pub ? $sql_no_pub = '' : $sql_no_pub = "AND ic_status = 1";
    $empty_info ? $sql_empty_info = '' : $sql_empty_info = "AND datalength(ic_content) > 0";
    //sql
    $sql = "SELECT 
            ic_id,
            ic_url,
            ic_sitecode,
            ic_title, 
            ic_url_title,
            ic_datetime,
            ic_type,
            ic_author,
            ic_status,
            ic_view,
            is_id
            FROM infoContents
            LEFT JOIN infoStructures ON ic_id = is_ic_id
            WHERE YEAR(GETDATE()) - YEAR(ic_datetime) >= ?
            AND ic_sitecode = ? 
            " . $sql_no_pub . "
            " . $sql_empty_info . "
            AND ic_url <> ''
            ORDER BY ic_datetime desc";
    $query = $this->HT->query($sql, array($yeardiff, $this->config->item('site_code')));
    return $query->result();
  }


  //ct
  function ct_get_by_url($ic_url, $webcode)
  {
    $this->ic_url_is_id = " AND ic.ic_url =  N" . $this->HT->escape($ic_url);
    $sql = "SELECT is1.is_id, \n"
      . " is1.is_parent_id, \n"
      . " is1.is_path, \n"
      . " is1.is_level, \n"
      . " is1.is_sort, \n"
      . " is1.is_sitecode, \n"
      . " is1.is_datetime, \n"
      . " is1.is_ic_id, \n"
      . " ic.ic_id, \n"
      . " ic.ic_url, \n"
      . " ic.ic_url_title, \n"
      . " ic.ic_type, \n"
      . " ic.ic_title, \n"
      . " ic.ic_content, \n"
      . " ic.ic_summary, \n"
      . " ic.ic_seo_title, \n"
      . " ic.ic_seo_description, \n"
      . " ic.ic_seo_keywords, \n"
      . " ic.ic_show_bread_crumbs, \n"
      . " ic.ic_status, \n"
      . " ic.ic_template, \n"
      . " ic.ic_photo, \n"
      . " ic.ic_photo_width, \n"
      . " ic.ic_photo_height, \n"
      . " ic.ic_sitecode, \n"
      . " ic.ic_recommend_tours, \n"
      . " ic.ic_recommend_packages, \n"
      . " ic.ic_datetime, \n"
      . " ic.ic_ht_area_id, \n"
      . " ic.ic_ht_area_type, \n"
      . " ic.ic_ht_product_id, \n"
      . " ic.ic_ht_product_type, \n"
      . " ic.ic_author \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"
      . " WHERE   is1.is_sitecode = ? ";
    $sql .= $this->ic_url_is_id;
    $query = $this->HT->query($sql, $webcode);
    // print_r($this->HT->queries);

    return $query->row();
  }

  //获取分组列表
  function group_list()
  {
    $this->init();
    $this->search = ' AND is_level<=1 ';
    $this->orderBy = ' ORDER BY is1.is_level ASC, is1.is_sort ASC,ic.ic_title ASC,is1.is_path ASC ';
    return $this->GetList();
  }

  /**
   *  CT 获取当前页面的面包屑，不包括当前节点,去除链接为空及不发布的节点
   */
  function get_path_exclude_ct($is_id, $path)
  {
    $site_code = $this->config->item('site_code');
    $path_query = $this->HT->query("select 
        ic_id, is_id, ic_url,ic_url_title,ic_title,is_path,is_level,ic_status
        from infoStructures a inner join infoContents b on a.is_ic_id=b.ic_id 
        where  is_id in ($path 0) and is_id <> ? and is_siteCode = ?  order by is_level asc", array($is_id, $site_code));
    $path_result = $path_query->result();
    $path_list = [];

    $group_map = [
      278013869 => [
        'ic_url' => '/tour',
        'ic_url_title' => 'China Tours'
      ],
      278014282 => [
        'ic_url' => '/citytour',
        'ic_url_title' => 'China City Tours'
      ],
      278013862 => [
        'ic_url' => '/daytrip',
        'ic_url_title' => 'China Day Trip'
      ],
      278014609 => [
        'ic_url' => '/china-trains',
        'ic_url_title' => 'China Trains'
      ],
      278014608 => [
        'ic_url' => '/china-flights',
        'ic_url_title' => 'China Flights'
      ]
    ];

    foreach ($path_result as $path_row) {

      if (array_key_exists($path_row->is_id, $group_map)) {
        $top_group = $group_map[$path_row->is_id];
        $path_array = [
          'ic_id' => $path_row->ic_id,
          'is_id' => $path_row->is_id,
          'ic_title' => $path_row->ic_title,
          'ic_url' => $top_group['ic_url'],
          'ic_url_title' => $top_group['ic_url_title']
        ];
        $path_list[] = $path_array;
      } else if ($path_row->ic_status === 1) {
        $path_array = [
          'ic_id' => $path_row->ic_id,
          'is_id' => $path_row->is_id,
          'ic_title' => $path_row->ic_title,
          'ic_url' => $path_row->ic_url,
          'ic_url_title' => $path_row->ic_url_title
        ];
        $path_list[] = $path_array;
      }
    }

    return $path_list;
  }

  /**
   * intl 根据keywords获取信息。
   */
  function list_by_keywords($site, $keywords)
  {
    $this->init();
    $keysql = '';
    foreach ($keywords as $key) {
      $keysql .= ' OR ic_content like \'%' . $key . '%\'';
      $keysql .= ' OR ic_url like \'%' . $key . '%\'';
      $keysql .= ' OR m1.im_value like \'%' . $key . '%\'';
      $keysql .= ' OR m2.im_value like \'%' . $key . '%\'';
    }
    $search = ' AND (1=0 ' . $keysql . ')';
    $sql = "
      SELECT is_id, ic_id, ic_url, ic_content, m1.im_value AS amp_json, m2.im_value AS amp_body
      FROM infoStructures is1
        INNER JOIN infoContents ic
        ON ic.ic_id = is1.is_ic_id
          AND ic.ic_sitecode = is1.is_sitecode
        LEFT JOIN infoMetas m1
        ON m1.im_ic_id = ic.ic_id
          AND m1.im_key = 'AMP_JSON'
        LEFT JOIN infoMetas m2
        ON m2.im_ic_id = ic.ic_id
          AND m2.im_key = 'AMP_BODY_PC'
      WHERE ic_status = 1
        AND is1.is_sitecode = ?
        AND ic_status = 1
        " . $search . "
      ORDER BY ic_datetime DESC
    ";
    $query = $this->HT->query($sql, array($site));
    // print_r($this->HT->queries);
    return $query->result();
  }
}
