<?php

class infoImageReview_model extends CI_Model
{
    var $ir_ic_id = null;
    var $ir_is_id = null;
    var $ir_img_src = null;    //原图
    var $ir_img_replace = null;//替换图片
    var $ir_create_date = null;
    var $ir_update_date = null;
    var $ir_state = null;  //状态：0未替换，1完成替换保存，2替换未保存，-1无效图片
    var $ir_site_code = null;
    var $ir_ht_area_type = null;
    var $ir_ht_id = null;
    
    function __construct()
    {
        parent::__construct();
        $this->ht = $this->load->database('HT', TRUE);
    }
    
    /**
     * 
     * 检查参数
     * 
     */ 
    public function check_para() 
    {
        if ($this->ir_ic_id===null || $this->ir_is_id===null || $this->ir_img_src===null || $this->ir_state===null || $this->ir_site_code===null || $this->ir_ht_area_type===null || $this->ir_ht_area_id===null)
        {
            die('参数不全，请检查');
        }
    }

    /**
     * 
     * 插入数据
     * 
     */     
    public function insert() 
    {
        $this->check_para();
        
        $sql = "SELECT ir.ir_ic_id \n"
             . "FROM   InfoImageReview ir \n"
             . "WHERE  ir.ir_ic_id = ? \n"
             . "       AND ir.ir_img_src = ?";
        $query = $this->ht->query($sql, array($this->ir_ic_id,$this->ir_img_src));
        if (count($query->result())==0)
        {
            $sql = "INSERT INTO infoImageReview \n"
                 . "           (ir_ic_id \n"
                 . "           ,ir_is_id \n"
                 . "           ,ir_img_src \n"
                 . "           ,ir_img_replace \n"
                 . "           ,ir_update_date \n"
                 . "           ,ir_state \n"
                 . "           ,ir_ht_area_type \n"
                 . "           ,ir_ht_area_id \n"
                 . "           ,ir_site_code) \n"
                 . "     VALUES \n"
                 . "           (? \n"
                 . "           ,? \n"
                 . "           ,? \n"
                 . "           ,? \n"
                 . "           ,getdate() \n"
                 . "           ,? \n"
                 . "           ,? \n"
                 . "           ,? \n"
                 . "           ,?)";
            $query = $this->ht->query($sql, array(
                $this->ir_ic_id,
                $this->ir_is_id,
                $this->ir_img_src,
                $this->ir_img_replace,
                $this->ir_state,
                $this->ir_ht_area_type,
                $this->ir_ht_area_id,
                $this->ir_site_code
            ));
            return '保存'; //$this->ht->insert_id();
        }
        return '存在';
    }
	
    /**
     * 
     * 查找信息内容（infoContents.ic_content）有图片的信息
     * 
     */ 
    public function get_info_has_img()
    {
        $sql = "SELECT is1.is_id, \n"
             . "       ic.ic_id, \n"
             . "       ic.ic_title, \n"
             . "       ic.ic_sitecode, \n"
             . "       ic.ic_ht_area_type, \n"
             . "       ic.ic_ht_area_id, \n"
             . "       ic.ic_content, \n"
             . "       ic.ic_summary, \n"
             . "       ( \n"
             . "           SELECT TOP 1 ir.ir_id \n"
             . "           FROM   InfoImageReview ir \n"
             . "           WHERE  ir.ir_ic_id = ic.ic_id \n"
             . "                  AND ir.ir_is_id = is1.is_id \n"
             . "                  AND ir.ir_site_code = ic.ic_sitecode \n"
             . "       ) AS ir_id \n"
             . "FROM   infoStructures is1 \n"
             . "       INNER JOIN infoContents ic \n"
             . "            ON  is1.is_sitecode = ic.ic_sitecode \n"
             . "            AND is1.is_ic_id = ic.ic_id \n"
             . "WHERE  ic.ic_status = 1 \n"
             . "       AND ic.ic_sitecode = ? \n"
             . "       AND ic.ic_ht_area_type = ? \n"
             . "       AND ( \n"
             . "               CAST(ic.ic_content AS NVARCHAR(4000)) LIKE '%<img%' \n"
             . "               OR CAST(ic.ic_summary AS NVARCHAR(4000)) LIKE '%<img%' \n"
             . "           )";
        $query = $this->ht->query($sql, array($this->ir_site_code, $this->ir_ht_area_type));
        return $query->result();
    }
}