<?php

class Infosharedata_model extends CI_Model {

    var $insert_id = -1;
    var $top_num = false;
    var $order_by = false;
    var $is_id = false;
    var $isd_id = false;
    var $search_title = false;
    var $search_week = false;
    var $isd_mail_id = false;

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

    function init() {
        $this->top_num = false;
        $this->isd_mail_id = false;
        $this->is_id = false;
        $this->isd_id = false;
        $this->search_title = false;
        $this->search_week = false;
        $this->order_by = " ORDER BY isd.isd_id DESC ";
    }

    function add($isd_mail_id, $isd_date, $isd_subject, $isd_fromname, $isd_html, $isd_title, $isd_memo, $isd_is_id) {
        $sql = "
			INSERT INTO infosharedata
				(isd_mail_id, isd_date, isd_subject, isd_fromname, isd_html, isd_title, isd_memo, isd_is_id, isd_datetime)
			VALUES
			(?, ?, N?, N?, N?, N?, N?,?, getdate())
		";
        $query = $this->HT->query($sql, array($isd_mail_id, $isd_date, $isd_subject, $isd_fromname, $isd_html, $isd_title, $isd_memo, $isd_is_id));
        $this->insert_id = $this->HT->last_id('infosharedata');
        return $this->insert_id;
    }

    //获取最大邮件ID
    function last_mailid() {
        $this->init();
        $this->top_num = 1;
        $this->isd_mail_id = ' AND isd_mail_id<>0 ';
        $mail = $this->get_list();
        if ($mail) {
            return $mail->isd_mail_id;
        } else {
            return 0;
        }
    }

    //获取结构根节点id
    function root_sid() {
        $this->init();
        $this->top_num = 1;
        $this->order_by = " ORDER BY isd.isd_id ASC ";
        $mail = $this->get_list();
        if ($mail) {
            return $mail->isd_is_id;
        } else {
            return 0;
        }
    }

    //结构数据
    function structures() {
        $sql = "SELECT   is1.is_id AS id,
                         is1.is_parent_id AS pId,
                         isd_title as name,
						 1 AS status
                FROM     infosharedata 
                         INNER JOIN infoStructures is1 ON isd_is_id = is1.is_id 
                WHERE    1=1
                ORDER BY is1.is_level ASC,
                         is1.is_sort ASC, 
                         is1.is_path ASC";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    function detail_by_id($isd_id) {
        $this->init();
        $this->isd_id = ' AND isd.isd_id= ' . $this->HT->escape($isd_id);
        $this->top_num = 1;
        $this->order_by = " ORDER BY isd.isd_id DESC ";
        return $this->get_list();
    }

    function search($keyword) {
        $this->init();
        $this->top_num = 24;
        $sql_keyword = '%' . $this->HT->escape_like_str($keyword) . '%';
        $this->search_title = "AND (isd_title like N'$sql_keyword' ) AND (isd_fromname<>'' OR isd_memo<>'' ) AND is1.is_parent_id<>240000129 ";
        return $this->get_list();
    }

    /* 搜索上周帖子列表 */

    function search_week() {
        $this->init();
        $this->search_week = " AND is1.is_parent_id<>240000129 AND (isd_fromname<>'' OR isd_memo<>'' ) AND (dateadd(day,-7,getdate())<=isd_datetime) ";
        //$this->order_by = " ORDER BY is1.is_path DESC ";
        return $this->get_list();
    }

    function detail($is_id) {
        $this->init();
        $this->is_id = ' AND isd.isd_is_id= ' . $this->HT->escape($is_id);
        $this->top_num = 1;
        $this->order_by = " ORDER BY isd.isd_id DESC ";
        return $this->get_list();
    }

    function get_list() {
        $this->top_num ? $sql = "SELECT TOP " . $this->top_num : $sql = "SELECT ";
        $sql .= "	 
				isd_id, isd_mail_id, isd_date, isd_subject, isd_fromname, isd_html, isd_title, isd_memo, isd_datetime, isd_is_id
				FROM infosharedata isd inner join infoStructures is1 on is1.is_id=isd_is_id
				WHERE 1 = 1
		";
        $this->isd_mail_id ? $sql.=$this->isd_mail_id : false;
        $this->search_title ? $sql.=$this->search_title : false;
        $this->search_week ? $sql.=$this->search_week : false;
        $this->is_id ? $sql.=$this->is_id : false;
        $this->isd_id ? $sql.=$this->isd_id : false;
        $this->order_by ? $sql.=$this->order_by : false;
        $query = $this->HT->query($sql);
        if ($this->top_num == 1) {
            if ($query->num_rows() > 0) {
                $row = $query->row();
                return $row;
            } else {
                return FALSE;
            }
        } else {
			if ($query->num_rows() > 0){
				return $query->result();
			}else{
				return FALSE;
			}
        }
    }

    function update($isd_id, $isd_title, $isd_memo, $isd_html) {
        $sql = "
			UPDATE infosharedata 
			 SET    isd_title = N?,
			     isd_memo = N?,
                             isd_html = N?,
			        isd_datetime = getdate() 
			 WHERE  isd_id= ?
					";
        $query = $this->HT->query($sql, array($isd_title, $isd_memo, $isd_html, $isd_id));
        return $query;
    }

    function delete_by_parent($is_id) {
        $sql = "
            delete from infosharedata where isd_is_id in  (
                    select is_id from infoStructures where is_path like '%,?,%' and is_id<>?
            )
            ";
        $this->HT->query($sql, array($is_id, $is_id));
        $sql = " delete from infoStructures where is_path like '%,?,%' and is_id<>?  ";
        $query = $this->HT->query($sql, array($is_id, $is_id));
        return $query;
    }
    
   

}
