<?php

class InfoSMS_model extends CI_Model
{
	var $insert_id = -1;
	var $top_num=false;
	var $order_by = false;
	var $m_object_type = false;
	var $m_object_id = false;
	var $m_readed = false;
	
    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }
	
	function init()
	{
		$this->top_num=false;
		$this->order_by=" ORDER BY i.m_id DESC ";
		$this->m_object_type = false;
		$this->m_object_id = false;
		$this->m_readed = false;
	}
	
	//获取短消息任务列表
	function get_task_sms($t_id)
	{
		$this->init();
		$this->m_object_type = " AND m_object_type='task' ";
		$this->m_object_id = " AND m_object_id= ".$this->HT->escape($t_id);
		$this->order_by=" ORDER BY i.m_id DESC ";
		return $this->get_list();
	}
	
	//设置已读
	function readed($m_id)
	{
		/* 不再使用注释-lmr
		$sql = "UPDATE infosms \n"
			 . "SET    m_readed = 1 \n"
			 . "WHERE  m_id = ?";
		return $this->HT->query($sql,array($m_id));
		*/
		return array();
	}
	function readed_for_info($m_object_id,$m_object_type='info')
	{
		/* 不再使用注释-lmr
		$sql = "UPDATE infosms \n"
			 . "SET    m_readed = 1 \n"
			 . "WHERE  m_object_type=? and m_object_id = ?";
		return $this->HT->query($sql,array($m_object_type,$m_object_id));
		*/
		return array();
	}
	
	//获取未读消息
	function unread_sms($m_object_type,$m_object_id)
	{
		$this->init();
		$this->m_object_type = " AND m_object_type=".$this->HT->escape($m_object_type);
		$this->m_object_id = " AND m_object_id= ".$this->HT->escape($m_object_id);
		$this->m_readed=" AND m_readed=0 ";
		return $this->get_list();
	}

	//获取作者管理平台所有的未读信息
	public function all_unread_sms($m_receiver,$m_object_type='task'){
		$sql = "SELECT	m_object_id,
						m_object_type,
						t_title 
				FROM 	infosms
						INNER JOIN infoTasks ON t_id=m_object_id AND t_delete=0
				WHERE	m_object_type=? 
						AND m_receiver= '$m_receiver' 
						AND m_readed=0
				ORDER BY m_datetime DESC";
		$query=$this->HT->query($sql,array($m_object_type));
		$result=$query->result();
		$sms=array();
		$num=count($result);
		if (!empty($result)) {
			foreach ($result as $v) {
				$sms[$v->m_object_id][]=$v;
			}
		}
		$unread_sms['sms_type']='task';//未读消息类型
		$unread_sms['sms_count']=$num;//未读消息数量
		$unread_sms['sms']=$sms;//未读消息列表
		return $unread_sms;
	}
	//信息平台的消息
	public function info_unread_sms($m_receiver,$m_object_type='info'){
		$sql = "SELECT	m_object_id,
						m_object_type,
						m_content,
						ic_url_title as t_title,
						ic_sitecode,
						is_id
				FROM 	infosms
						INNER JOIN infocontents ON ic_id=m_object_id
						LEFT JOIN infostructures ON is_ic_id=m_object_id
				WHERE	m_object_type=? 
						AND m_receiver= '$m_receiver' 
						AND m_readed=0
				ORDER BY m_datetime DESC";
		$query=$this->HT->query($sql,array($m_object_type));
		$result=$query->result();
		$sms=array();
		$num=count($result);
		if (!empty($result)) {
			foreach ($result as $v) {
				$sms[$v->m_object_id][]=$v;
			}
		}
		$unread_sms['sms_type']='info';//未读消息类型
		$unread_sms['sms_count']=$num;//未读消息数量
		$unread_sms['sms']=$sms;//未读消息列表
		return $unread_sms;
	}
	
    function add($m_object_type,$m_object_id,$m_receiver,$m_sender,$m_content)
    {
		$sql = "INSERT INTO infosms \n"
			 . "  ( \n"
			 . "    m_object_type, \n"
			 . "    m_object_id, \n"
			 . "    m_receiver, \n"
			 . "    m_sender, \n"
			 . "    m_content, \n"
			 . "    m_readed, \n"
			 . "    m_datetime \n"
			 . "  ) \n"
			 . "VALUES \n"
			 . "  ( \n"
			 . "    ?, \n"
			 . "    ?, \n"
			 . "    ?, \n"
			 . "    ?, \n"
			 . "    N?, \n"
			 . "    0, \n"
			 . "    GETDATE() \n"
			 . "  )";
        return $this->HT->query($sql, array($m_object_type,$m_object_id,$m_receiver,$m_sender,$m_content));
    }
	
	function get_list()
	{
		$this->top_num ? $sql = "SELECT TOP " . $this->top_num : $sql = "SELECT ";
		$sql .= "      i.m_id, \n"
			 . "       i.m_object_type, \n"
			 . "       i.m_object_id, \n"
			 . "       i.m_receiver, \n"
			 . "       i.m_sender, \n"
			 . "       i.m_readed, \n"
			 . "       i.m_content, \n"
			 . "       i.m_datetime \n"
			 . "FROM   infosms i \n"
			 . "WHERE  1 = 1";
		$this->m_object_type ? $sql.=$this->m_object_type : false;
		$this->m_object_id ? $sql.=$this->m_object_id : false;
		$this->m_readed ? $sql.=$this->m_readed : 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
		{
			return $query->result();
		}			 
	}
	
	    /*
     * 邮件发送
     */

    function send_mail($fromName, $fromEmail, $toName, $toEmail, $subject, $body,$frominfo='ChinaHighlights Online Writers Platform',$M_Web='info') {
        $sql = "INSERT INTO Email_AutomaticSend \n"
                . "  ( \n"
                . "	M_ReplyToName, M_ReplyToEmail, M_ToName, M_ToEmail, M_Title, M_Body, M_Web,  \n"
                . "	M_FromName, M_State \n"
                . "  ) \n"
                . "VALUES \n"
                . "  (  \n"
                . "	?, ?, ?, ?, ?, ?, ?, ?, 0 \n"
                . "  ) ";
        $query = $this->HT->query($sql, array($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $M_Web, $frominfo));
        return $query;
    }

    //获取邮件列表
    public function un_send_mail($top=1,$status=0){
    	$sql = "SELECT TOP $top
    						M_SN, 
    						M_ReplyToName, 
    						M_ReplyToEmail, 
    						M_ToName, 
    						M_ToEmail, 
    						M_Title, 
    						M_Body, 
    						M_Web,
    						M_FromName, 
    						M_AddTime,
    						M_State 
    				FROM 	Email_AutomaticSend
    				WHERE 	M_State=?
    				ORDER BY M_SN DESC";
        $query = $this->HT->query($sql,array($status));
        return $query->result();
    } 

    //设置邮件状态
    public function set_mail_status($M_SN,$status=1)
    {
    	$sql = "UPDATE Email_AutomaticSend SET M_State = ? WHERE  M_SN = ?";
		return $this->HT->query($sql,array($status,$M_SN));
    }

    //发送失败的邮件列表
    public function get_mail_log($top=50){
    	$sql = "SELECT TOP $top
    						M_SN, 
    						M_ReplyToName, 
    						M_ReplyToEmail, 
    						M_ToName, 
    						M_ToEmail,
    						M_CopyEmail,
    						M_Title, 
    						M_Body, 
    						M_Web,
    						M_FromName, 
    						M_State,
    						M_AddTime,
    						log_content
    				FROM 	Email_AutomaticSend INNER JOIN infoLogs ON log_res_id=M_SN
    				WHERE 	log_action='send_mail' AND M_State=1 order by M_SN DESC";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    //生日邮件发送失败/成功列表
    //$status:1 成功列表
    //$status:0 失败列表
    public function get_cusbirthdaycard_list($status,$istoday=false){
    	$map='';
    	if($status==0) {
    		$map= ' AND CBC_TryTimes=6 ';
    		if($istoday) {
    			$start_date=date('Y-m-d');
        		$end_date=date('Y-m-d',strtotime("tomorrow"));
    			$map.= " AND CBC_BirthDay BETWEEN $start_date AND $end_date ";
    		}
    	}else if($status==2){
    		$end_date=date('Y-m-d',strtotime("tomorrow"));
    		$map= " AND (CBC_TryTimes<6 OR CBC_TryTimes is null) AND CBC_BirthDay<'$end_date' ";
    		$status=0;
    	}
    	$sql="SELECT top 100 * 
    			FROM CS_CusBirthDayCard 
    		   WHERE CBC_SendState=? $map
    		   ORDER BY CBC_BirthDay DESC";
    	$query = $this->HT->query($sql,array($status));
        return $query->result();
    }

    //报价信,确认信供应商计划邮件发送失败/成功列表
    //$status:1 成功列表
    //$status:0 失败列表
    public function get_batchftp_list($status,$istoday=false){
    	$map='';
    	if($status==0) {
    		$map= ' AND BAT_TryTimes=6 ';
    		if($istoday) {
    			$start_date=date('Y-m-d');
        		$end_date=date('Y-m-d',strtotime("tomorrow"));
    			$map.= " AND LastEditDate BETWEEN $start_date AND $end_date ";
    		}
    	}else if($status==2){
    		$end_date=date('Y-m-d',strtotime("tomorrow"));
    		$map= " AND (BAT_TryTimes<6 OR BAT_TryTimes is null) AND LastEditDate<'$end_date' ";
    		$status=0;
    	}
    	$sql="SELECT top 100 * 
    			FROM BatchFTP 
    		   WHERE BAT_State=? $map
    		   ORDER BY LastEditDate DESC";
    	$query = $this->HT->query($sql,array($status));
        return $query->result();
    }

    //供应商平台邮件发送失败/成功列表
    //$status:1 成功列表
    //$status:0 失败列表
    public function get_strandedemail_list($status,$istoday=false){
    	$map='';
    	//发送失败列表
    	if($status==0) 
    	{
    		$map= ' AND M_TryTimes=6 ';
    		if($istoday) {
    			$start_date=date('Y-m-d');
        		$end_date=date('Y-m-d',strtotime("tomorrow"));
    			$map.= " AND M_AddTime BETWEEN $start_date AND $end_date ";
    		}
    	}
    	//待发送列表
    	else if($status==2)
    	{
    		$end_date=date('Y-m-d',strtotime("tomorrow"));
    		$map= " AND (M_TryTimes<6 OR M_TryTimes is null) AND M_AddTime<'$end_date' ";
    		$status=0;
    	}
    	$sql="SELECT top 100 * 
    			FROM CCP_StrandedEmail 
    		   WHERE M_Sign=? $map
    		   ORDER BY M_AddTime DESC";
    	$query = $this->HT->query($sql,array($status));
        return $query->result();
    }

    //获取当天HT需要发送/已经发送的邮件数目
	public function get_ht_mail_count($start_date,$end_date,$field='CS_CusBirthDayCard',$mapfield='CBC_BirthDay',$status=0){
    	$map='';
    	if($status1=0) $map=" AND $status=1 ";
    	$sql="SELECT count(0) AS num
    			FROM $field 
    		   WHERE ?<=$mapfield AND $mapfield<? $map";
    	$query = $this->HT->query($sql,array($start_date,$end_date));
    	$result=$query->result();
    	$count=0;
    	isset($result[0]->num) && $count=$result[0]->num;
        return $count;
	}

}