<?php

class Area_model extends CI_Model
{

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

    function get_city($city_id)
    {
        $sql = "SELECT TOP 1 ci.CII_SN, \n"
            . "       ci.CII_Code, \n"
            . "       ci2.CII2_Name \n"
            . "FROM   CItyInfo ci \n"
            . "       INNER JOIN CItyInfo2 ci2 ON ci2.CII2_CII_SN = ci.CII_SN \n"
            . "WHERE  ci2.CII2_LGC = ? \n"
            . "       AND ci.CII_IsTrue = 1 \n"
            . "       AND ci2.CII2_Name IS NOT NULL \n"
            . "       AND ci2.CII2_Name <> '' \n"
            . "       AND ci.CII_Code IS NOT NULL \n"
            . "       AND ci.CII_Code <> '' \n"
            . "       AND ci.CII_SN =? \n"
            . "ORDER BY \n"
            . "       ci.CII_Code ASC";
        $query = $this->HT->query($sql, array($this->config->item('site_lgc'), $city_id));
        //print_r($this->HT->queries);
        if ($query->result()) {
            $row = $query->row();
            return $row;
        } else {
            return FALSE;
        }
    }


    function get_city_list($PRI_SN)
    {
        $sql = "SELECT ci.CII_SN, \n"
            . "       UPPER(ci.CII_Code) AS CII_Code, \n"
            . "       ci2.CII2_Name \n"
            . "FROM   CItyInfo ci \n"
            . "       INNER JOIN CItyInfo2 ci2 ON ci2.CII2_CII_SN = ci.CII_SN \n"
            . "WHERE  ci2.CII2_LGC = ? \n"
            . "       AND ci.CII_IsTrue = 1 \n"
            . "       AND ci2.CII2_Name IS NOT NULL \n"
            . "       AND ci2.CII2_Name <> '' \n"
            . "       AND ci.CII_Code IS NOT NULL \n"
            . "       AND ci.CII_Code <> '' \n"
            . "       AND ci.CII_PRI_SN = ? \n"
            . "ORDER BY \n"
            . "       ci.CII_Code ASC";
        $query = $this->HT->query($sql, array($this->config->item('site_lgc'), $PRI_SN));
        return $query->result();
    }

    //获取直辖市和特区列表
    function get_municipalities_list()
    {
        $sql = "SELECT ci.CII_SN, \n"
            . "       UPPER(ci.CII_Code) AS CII_Code, \n"
            . "       ci2.CII2_Name \n"
            . "FROM   CItyInfo ci \n"
            . "       INNER JOIN CItyInfo2 ci2 ON ci2.CII2_CII_SN = ci.CII_SN \n"
            . "WHERE  ci2.CII2_LGC = ? \n"
            . "       AND ci.CII_IsTrue = 1 \n"
            . "       AND ci2.CII2_Name IS NOT NULL \n"
            . "       AND ci2.CII2_Name <> '' \n"
            . "       AND ci.CII_Code IS NOT NULL \n"
            . "       AND ci.CII_Code <> '' \n"
            . "       AND ci.CII_SN IN (1,3,4,5,177,178) \n"
            . "ORDER BY \n"
            . "       ci.CII_Code ASC";
        $query = $this->HT->query($sql, array($this->config->item('site_lgc')));
        return $query->result();
    }

    function get_province($PRI_SN)
    {
        $sql = "SELECT TOP 1 pi1.PRI_SN, \n"
            . "       pi2.PRI2_Name \n"
            . "FROM   PRovinceInfo pi1 \n"
            . "       INNER JOIN PRovinceInfo2 pi2 ON pi2.PRI2_PRI_SN = pi1.PRI_SN \n"
            . "WHERE  (pi1.PRI_Country = 1 or pi1.PRI_Country = 10) \n"
            . "       AND pi1.PRI_SN NOT IN(1,2,3,4) \n"
            . "       AND pi2.PRI2_LGC = ? \n"
            . "       AND pi1.PRI_SN = ? ";
        $query = $this->HT->query($sql, array($this->config->item('site_lgc'), $PRI_SN));
        if ($query->result()) {
            $row = $query->row();
            return $row;
        } else {
            return FALSE;
        }
    }

    function get_province_list()
    {
        $sql = "SELECT pi1.PRI_SN, \n"
            . "       pi2.PRI2_Name \n"
            . "FROM   PRovinceInfo pi1 \n"
            . "       INNER JOIN PRovinceInfo2 pi2 ON pi2.PRI2_PRI_SN = pi1.PRI_SN \n"
            . "WHERE  (pi1.PRI_Country = 1 or pi1.PRI_Country = 10) \n"
            . "       AND pi1.PRI_SN NOT IN(1,2,3,4,176,192) \n"
            . "       AND pi2.PRI2_LGC = ? ";
        $query = $this->HT->query($sql, array($this->config->item('site_lgc')));
        return $query->result();
    }

    function get_country($COI_SN)
    {
        $sql = "SELECT TOP 1 ci.COI_SN, \n"
            . "       ci2.COI2_Country \n"
            . "FROM   COuntryInfo ci \n"
            . "       INNER JOIN COuntryInfo2 ci2 ON ci2.COI2_COI_SN = ci.COI_SN \n"
            . "WHERE  ci2.COI2_LGC = ? \n"
            . "       AND ci.COI_SN = ? ";
        $query = $this->HT->query($sql, array($this->config->item('site_lgc'), $COI_SN));
        if ($query->result()) {
            $row = $query->row();
            return $row;
        } else {
            return FALSE;
        }
    }

    function get_country_list()
    {
        $sql = "SELECT ci.COI_SN, \n"
            . "       ci2.COI2_Country \n"
            . "FROM   COuntryInfo ci \n"
            . "       INNER JOIN COuntryInfo2 ci2 ON ci2.COI2_COI_SN = ci.COI_SN \n"
            . "WHERE  ci2.COI2_LGC = ? \n"
            . "       AND ci.COI_SN in(1, 2, 19, 22, 24, 27, 28, 29, 30, 31, 35, 562, 591, 517)";
        $query = $this->HT->query($sql, array($this->config->item('site_lgc')));
        return $query->result();
    }
    function get_country_list_for_site($country_list)
    {
        $sql = "SELECT ci.COI_SN, \n"
            . "       ci2.COI2_Country \n"
            . "FROM   COuntryInfo ci \n"
            . "       INNER JOIN COuntryInfo2 ci2 ON ci2.COI2_COI_SN = ci.COI_SN \n"
            . "WHERE  ci2.COI2_LGC = ? \n"
            . "       AND ci.COI_SN in($country_list)";
        $query = $this->HT->query($sql, array($this->config->item('site_lgc')));
        return $query->result();
    }

    //根据区域类型和ID获取区域名称
    function get_area_name($ht_area_type, $ht_area_id)
    {
        switch ($ht_area_type) {
            case 'c': //城市
                $area = $this->get_city($ht_area_id);
                return $area ? $area->CII2_Name : false;
                break;
            case 'p': //省份
                $area = $this->get_province($ht_area_id);
                return $area ? $area->PRI2_Name : false;
                break;
            case 'n': //国家
                $area = $this->get_country($ht_area_id);
                return $area ? $area->COI2_Country : false;
                break;
            case 't': //特殊区域
                break;
            case 's': //大洲
                break;
            case 'z': //公民游
                break;
            default:
                break;
        }
        return false;
    }

    //获取最后编辑信息的50个城市
    function get_last_edit_city()
    {
        $sql = "SELECT DISTINCT TOP 30 ci2.CII2_CII_SN, \n"
            . "       ci2.CII2_Name \n"
            . "FROM   CItyInfo2 ci2 \n"
            . "WHERE  ci2.CII2_LGC = ? \n"
            . "       AND ci2.CII2_CII_SN IN (SELECT TOP 1000 ic.ic_ht_area_id \n"
            . "                               FROM   infoContents ic \n"
            . "                               WHERE  ic_ht_area_type = 'c' \n"
            . "                               ORDER BY \n"
            . "                                      ic.ic_datetime DESC) \n"
            . "ORDER BY \n"
            . "       ci2.CII2_CII_SN ASC";
        $query = $this->HT->query($sql, array($this->config->item('site_lgc')));
        return $query->result();
    }
}
