<?php

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

    //获取结构列表
    public function get_structure_list(){
        $sql = "SELECT   is1.is_id AS id,
                         is1.is_parent_id AS pId,
                         ad_title as name,
                         ad_status AS status,
                         ad_id
                FROM     infoAdvertise 
                         INNER JOIN infoStructures is1 ON ad_is_id = is1.is_id 
                WHERE    ad_sitecode = ?
                ORDER BY is1.is_level ASC,is1.is_sort ASC,is1.is_path ASC";
        $query = $this->HT->query($sql, array($this->config->item('site_code')));
        return $query->result();
    }

    //广告列表
    public function get_ad_list($ad_sitecode,$ad_expire=false,$ad_expire_end=false)
    {
        $mapsql='';
        if ($ad_expire) {
            $mapsql=" AND ad_expire<$ad_expire ";
        }
        if ($ad_expire_end) {
            $mapsql=" AND ad_expire > $ad_expire AND ad_expire < $ad_expire_end ";
        }
        $sql = "SELECT TOP 30 ad_id,
                             ad_is_id,
                             ad_title,
                             ad_content,
                             ad_type,
                             ad_expire,
                             ad_place,
                             ad_sitecode,
                             ad_status,
                             ad_createtime,
                             ad_updatetime
                  FROM    infoAdvertise
                 WHERE    ad_sitecode=? $mapsql
                 ORDER BY ad_updatetime DESC";
        $query = $this->HT->query($sql,array($ad_sitecode));
        $result = $query->result();
        return $result;
    }

    //根据广告ID获取指定广告的内容
    public function get_advertise($ad_id)
    {
        $sql = "SELECT TOP 1 ad_id,
                             ad_is_id,
                             ad_title,
                             ad_content,
                             ad_type,
                             ad_expire,
                             ad_place,
                             ad_sitecode,
                             ad_status,
                             ad_createtime
                FROM     infoAdvertise
                WHERE    ad_id=?";
        $query = $this->HT->query($sql,array($ad_id));
        $result = $query->result();
        if (!empty($result)) {
            $result=$result[0];
        }
        return $result;
    }

    //根据广告树节点ID获取指定广告的内容
    public function get_advertise_treeid($ad_is_id)
    {
        $sql = "SELECT TOP 1 ad_id,
                             ad_is_id,
                             ad_title,
                             ad_content,
                             ad_type,
                             ad_expire,
                             ad_place,
                             ad_sitecode,
                             ad_status,
                             ad_createtime
                FROM     infoAdvertise
                WHERE    ad_is_id=?";
        $query = $this->HT->query($sql,array($ad_is_id));
        $result = $query->result();
        if (!empty($result)) {
            $result=$result[0];
        }
        return $result;
    }

    //根据广告树节点ID获取关联的网页信息
    public function get_advertise_page($ad_is_id)
    {
        $sql = "SELECT  adp_id,
                        adp_ad_id,
                        adp_ic_url,
                        adp_forself,
                        adp_sitecode,
                        adp_status,
                        adp_createtime
                FROM    infoAdvertisePage LEFT JOIN infoAdvertise ON adp_ad_id=ad_id
                WHERE   ad_is_id=?";
        $query = $this->HT->query($sql,array($ad_is_id));
        $result = $query->result();
        return $result;
    }

    public function get_click_rate($ad_id)
    {
        $sql = "SELECT  sum(adp_click) as adp_click
                FROM    infoAdvertisePage 
                WHERE   adp_ad_id=?";
        $query = $this->HT->query($sql,array($ad_id));
        $result = $query->result();
        if ($result) {
            $result=$result[0]->adp_click;
        }
        return $result;
    }

    //更新广告
    public function update($ad_title,$ad_content,$ad_type,$ad_expire,$ad_place,$ad_status,$ad_id)
    {	
		$ad_content=str_replace('http:'.$this->config->item('site_image_url'), $this->config->item('site_image_url'),$ad_content);
        $ad_content=str_replace($this->config->item('media_image_url_remote2'), $this->config->item('site_image_url'),$ad_content);
        $ad_content=str_replace($this->config->item('media_image_url_org'), $this->config->item('site_image_url'),$ad_content);
        $ad_content=str_replace($this->config->item('media_image_url'), $this->config->item('site_image_url'),$ad_content);
        $ad_content=str_replace($this->config->item('media_image_url2'), $this->config->item('site_image_url'),$ad_content);   
        $ad_content=str_replace($this->config->item('media_image_url_remote'), $this->config->item('site_image_url'),$ad_content);
		
        $sql = "UPDATE  infoAdvertise
                   SET  ad_title=N?,
                        ad_content=N?,
                        ad_type=N?,
                        ad_expire=?,
                        ad_place=N?,
                        ad_status=?,
                        ad_updatetime=?
                 WHERE  ad_id = ?";
        $result = $this->HT->query($sql, array($ad_title,$ad_content,$ad_type,$ad_expire,$ad_place,$ad_status,time(),$ad_id));
        return $result;
    }

    //更新关联信息
    public function update_page($adp_ic_url,$adp_forself,$adp_status,$adp_id,$adp_ad_id)
    {
        $sql = "UPDATE  infoAdvertisePage
                   SET  adp_ic_url=N?,
                        adp_forself=?,
                        adp_status=?
                 WHERE  adp_id=?";
        $result = $this->HT->query($sql, array($adp_ic_url,$adp_forself,$adp_status,$adp_id));
        
        $this->update_adtime($adp_ad_id);

        return $result;
    }

    //关联信息页面
    public function add_page($adp_ad_id,$adp_ic_url,$adp_forself,$adp_sitecode)
    {
        $sql = "INSERT INTO infoAdvertisePage (
                             adp_ad_id,
                             adp_ic_url,
                             adp_forself,
                             adp_sitecode,
                             adp_createtime
                    ) VALUES (?,N?,?,?,?)";
        $query = $this->HT->query($sql, array($adp_ad_id,$adp_ic_url,$adp_forself,$adp_sitecode,time()));
        
        $this->update_adtime($adp_ad_id);

        return $this->HT->last_id('infoAdvertisePage');
    }

    public function update_adtime($ad_id)
    {
        $sql = "UPDATE  infoAdvertise
                   SET  ad_updatetime=?
                 WHERE  ad_id = ?";
        $this->HT->query($sql, array(time(),$ad_id));
    }

    //添加新广告节点
    public function add_advertise($ad_is_id,$ad_title,$ad_sitecode,$ad_createtime){
        $sql = "INSERT INTO infoAdvertise (
                             ad_is_id,
                             ad_title,
                             ad_sitecode,
                             ad_createtime,
                             ad_updatetime,
                             ad_expire
                    ) VALUES (?,N?,?,?,?,?)";
        $query = $this->HT->query($sql, array($ad_is_id,$ad_title,$ad_sitecode,$ad_createtime,time(),time()+100*24*60*60));
        return $this->HT->last_id('infoAdvertise');
    }

    //删除广告内容
    public function delete($ad_id)
    {
        $sql = "DELETE FROM infoAdvertise WHERE  ad_id = ?";
        $result=$this->HT->query($sql, array($ad_id));
        return $result;
    }

    //删除广告关联的信息
    public function delete_page($ad_id)
    {
        $sql = "DELETE FROM infoAdvertisePage WHERE  adp_ad_id = ?";
        $result=$this->HT->query($sql, array($ad_id));
        return $result;
    }

    //根据关联ID删除关联信息
    public function delete_page_by_adpid($adp_id)
    {
        $sql = "DELETE FROM infoAdvertisePage WHERE  adp_id = ?";
        $result=$this->HT->query($sql, array($adp_id));
        return $result;
    }
}