load->library('session');
		$this->load->_model('Model_user','user');
		$this->load->_model('Model_notice','notice');
		$this->load->_model('Model_noticeemail','noticeemail');
		$this->load->_model('Model_shop','shop');
		$this->load->_model('Model_typeclass','typeclass');
		$this->load->_model('Model_fullorder','fullorder');
		$this->load->_model('Model_fullordertt','fullordertt');
		$this->load->_model('Model_fullordersmt','fullordersmt');
		$this->load->_model('Model_express','express');
		$this->load->_model('Model_emaildata','emaildata');
		$this->load->_model('Model_als','als');
		$this->load->_model('Model_apitt','apitt');
		$this->load->_model('Model_producttitle','producttitle');
	}
	//定义方法的调用规则 获取URI第二段值
    public function _remap($arg,$arg_array)
    {
		if($arg == 'add')//添加
        {
             $this->_add($arg_array);
        }
		else if($arg == 'edit')//修改
        {
             $this->_edit($arg_array);
        }
		else if($arg == 'del')//修改
        {
             $this->_del();
        }
		else if($arg == 'rows')//数据
        {
             $this->_rows();
        }
		else if($arg == 'email')
        {
             $this->_email();
        }
		else if($arg == 'addemail')
        {
             $this->_addemail();
        }
		else if($arg == 'emailfs')
        {
             $this->_emailfs();
        }
		else if($arg == 'testfs')
        {
             $this->_testfs();
        }
		else
		{
			 $this->_index($arg_array);
		}
    }
	//管理
	public function _index($arg_array)
	{
		if(isset($_SESSION['api']))
		{
			$user = $this->user->get_api($_SESSION['api']);
		    $fgshop = "";$sid = "";
		    $user = explode('|',trim($user['shop'],'|'));
			foreach ($user as $value) 
		    {
				$fgshop .= " shop = ".$value." or";
				$sid .= " id = ".$value." or";
			}
		}
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['page']))  
		{
		    $page = $this->input->post('page',true);
		    $perpage = $this->input->post('perpage',true);
			$shop = $this->input->post('shop',true);
			$type = $this->input->post('type',true);
			$ordertatus = $this->input->post('ordertatus',true);
			$arg = $this->input->post('arg',true);
			$logisticstatus = $this->input->post('logisticstatus',true);
			$where = "1=1 and (".rtrim($fgshop,'or').")";
			if($shop)
            {
                $where  .= " and shop = '$shop'";
            }
			if($type)
            {
                $where  .= " and type = '$type'";
            }
			if($ordertatus)
            {
                $where  .= " and ordertatus = '$ordertatus'";
            }
			if($logisticstatus)
            {
                $where  .= " and logisticstatus = '$logisticstatus'";
            }
            //数据排序
            $order_str = "id asc";
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
            //取得信息列表
            $info_list = $this->notice->find_all($where,'id,shop,title,type,message,email,ordertatus,logisticstatus,ktime,jtime,state',$order_str,$start,$perpage);
			 //格式化数据
            foreach ($info_list as $key=>$value) 
		    {
				$shop = $this->shop->read($value['shop']);
				$info_list[$key]['shop'] = $shop['shopname'];
				$email = $this->emaildata->read($value['email']);
				$info_list[$key]['email'] = $email['smtp_user'];
				$info_list[$key]['ktime'] = date('Y-m-d',$value['ktime']);
				$info_list[$key]['jtime'] = date('Y-m-d',$value['jtime']);
		    	if($value['type'] == 1)
				{
					$info_list[$key]['type'] = '自动发送';
				}
				else if($value['type'] == 2)
				{
					$info_list[$key]['type'] = '物流追踪';
				}
				else if($value['type'] == 3)
				{
					$info_list[$key]['type'] = '营销';
				}
				else if($value['type'] == 4)
				{
					$info_list[$key]['type'] = '运单号通知';
				}
				if($value['message'] == 1)
				{
					$info_list[$key]['message'] = '发送邮件';
				}
				//订单状态开始
				$typeclass = $this->typeclass->read($value['ordertatus']);
				$info_list[$key]['ordertatus'] = $typeclass?(($arg==1)?$typeclass['title']:$typeclass['spare']):'';
				//订单状态结束
				if($value['logisticstatus'] == 0)
				{
					$info_list[$key]['logisticstatus'] = "无信息";
				}
				else if($value['logisticstatus'] == 2)
				{
					$info_list[$key]['logisticstatus'] = "已取件";
				}
				else if($value['logisticstatus'] == 3)
				{
					$info_list[$key]['logisticstatus'] = "在途中";
				}
				/**
				else if($value['logisticstatus'] == 4)
				{
					$info_list[$key]['logisticstatus'] = "到达目的地国";
				}
				**/
				else if($value['logisticstatus'] == 4 || $value['logisticstatus'] == 5)
				{
					$info_list[$key]['logisticstatus'] = "即将派送";
				}
				else if($value['logisticstatus'] == 6)
				{
					$info_list[$key]['logisticstatus'] = "已签收";
				}
				else if($value['logisticstatus'] == 1)
				{
					$info_list[$key]['logisticstatus'] = "派送异常";
				}
				if($value['message'] == 1)
				{
					$info_list[$key]['message'] = '发送邮件';
				}
				else if($value['message'] == 2)
				{
					$info_list[$key]['message'] = 'AE站内信';
				}
				else if($value['message'] == 3)
				{
					$info_list[$key]['message'] = 'TT会话';
				}
				if($value['state'] == 1)
				{
					$info_list[$key]['state'] = '启用中';
				}
				else if($value['state'] == 2)
				{
					$info_list[$key]['state'] = '未启用';
				}
            }
		    $total = $this->notice->find_count($where);
		    $pagenum = ceil($total/$perpage);
		    $over = $total-($start+$perpage);
		    $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
		    echo json_encode($rows);exit;
		}
		$wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or'));
		$this->data['wlshop'] = $wlshop;
		$mb = 2;
		if(isset($arg_array[0]))
		{
			if($arg_array[0]=='smt')
			{
				$mb = 1;
			}
			else if($arg_array[0]=='dlz')
			{
				$mb = 2;
			}
			else if($arg_array[0]=='tt')
			{
				$mb = 3;
			}
		}
		$this->data['arg'] = $mb;
		$this->_Template('notice',$this->data);
	}
	//添加
	public function _add($arg_array)
	{
		if(isset($_SESSION['api']))
		{
			$user = $this->user->get_api($_SESSION['api']);
		    $fgshop = "";$sid = "";
		    $user = explode('|',trim($user['shop'],'|'));
			foreach ($user as $value) 
		    {
				$fgshop .= " shop = ".$value." or";
				$sid .= " id = ".$value." or";
			}
		}
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['title']))
		{
			$post['title'] = $this->input->post('title',true);
			$post['shop'] = $this->input->post('shop',true);
			$post['message'] = $this->input->post('message',true);
			$post['type'] = $this->input->post('type',true);
			$post['repeat'] = $this->input->post('repeat',true);
			$ktime = $this->input->post('ktime',true);
			$post['ktime'] = strtotime($ktime);
			$jtime = $this->input->post('jtime',true);
			$post['jtime'] = strtotime($jtime);
			$post['content'] = $this->input->post('content');
			$emailtitle = $this->input->post('emailtitle');
			$post['js'] = '';
			if($emailtitle)
			{
				$post['emailtitle'] = $emailtitle;
			}
			if($post['type'] == 1)
			{
				$post['ordertatus'] = $this->input->post('ordertatus',true);
				if($post['ordertatus'] == 2 || $post['ordertatus'] == 3)
				{
					$post['sending'] = $this->input->post('sending',true);
					$post['timeinterval'] = $this->input->post('timeinterval',true);
				}
			}
			else if($post['type'] == 2)
			{
				$post['logisticstatus'] = $this->input->post('logisticstatus',true);
			}
			else if($post['type'] == 3)
			{
				$marketing = $this->input->post('marketing',true);
			    $post['marketing'] = strtotime($marketing);
			}
			else if($post['type'] == 4)
			{
				$post['js'] = $this->input->post('js',true);
				if($post['js'] != '')
				{
					$post['js'] = ','.$post['js'];
				}
				$post['express'] = $this->input->post('express',true);
				if($post['express'] != '')
				{
					$post['express'] = ','.$post['express'];
				}
			}
			$post['state'] = $this->input->post('state',true);
			if($post['state'] == 1)
			{
				$post['statetime'] = time();
			}
			
			if($this->notice->insert($post))
        	{
         		echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
       	 	}
       		else
        	{
           		echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
        	}
		}
		$wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or'));
		$this->data['wlshop'] = $wlshop;
		$email = $this->emaildata->find_all('1=1 and '.rtrim($fgshop,'or'));
		$this->data['email'] = $email;
		$mb = 2;
		if(isset($arg_array[0]))
		{
			if($arg_array[0]=='smt')
			{
				$mb = 1;
			}
			else if($arg_array[0]=='dlz')
			{
				$mb = 2;
			}
			else if($arg_array[0]=='tt')
			{
				$mb = 3;
			}
		}
		$this->data['arg'] = $mb;
		$this->_Template('notice_add',$this->data);
	}
	//修改
	public function _edit($arg_array)
	{
		if(isset($_SESSION['api']))
		{
			$user = $this->user->get_api($_SESSION['api']);
		    $fgshop = "";$sid = "";
		    $user = explode('|',trim($user['shop'],'|'));
			foreach ($user as $value) 
		    {
				$fgshop .= " shop = ".$value." or";
				$sid .= " id = ".$value." or";
			}
		}
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['id']))
		{
			$id = $this->input->post('id',true);
			$post['title'] = $this->input->post('title',true);
			$post['shop'] = $this->input->post('shop',true);
			$post['message'] = $this->input->post('message',true);
			$post['type'] = $this->input->post('type',true);
			$post['repeat'] = $this->input->post('repeat',true);
			$ktime = $this->input->post('ktime',true);
			$post['ktime'] = strtotime($ktime);
			$jtime = $this->input->post('jtime',true);
			$post['jtime'] = strtotime($jtime);
			$post['content'] = $this->input->post('content');
			$post['emailtitle'] = $this->input->post('emailtitle');
			$post['js'] = '';
			if($post['type'] == 1)
			{
				$post['ordertatus'] = $this->input->post('ordertatus',true);
				if($post['ordertatus'] == 2 || $post['ordertatus'] == 3)
				{
					$post['sending'] = $this->input->post('sending',true);
					$post['timeinterval'] = $this->input->post('timeinterval',true);
				}
				$post['logisticstatus'] = 0;
			}
			else if($post['type'] == 2)
			{
				$post['ordertatus'] = 0;//非订单状态写入:未获取
				$post['logisticstatus'] = $this->input->post('logisticstatus',true);
			}
			else if($post['type'] == 3)
			{
				$post['ordertatus'] = 0;//非订单状态写入:未获取
				$marketing = $this->input->post('marketing',true);
			    $post['marketing'] = strtotime($marketing);
			}
			else if($post['type'] == 4)
			{
				$post['js'] = $this->input->post('js',true);
				if($post['js'] != '')
				{
					$post['js'] = ','.$post['js'];
				}
				$post['express'] = $this->input->post('express',true);
				if($post['express'] != '')
				{
					$post['express'] = ','.$post['express'];
				}
			}
			$post['state'] = $this->input->post('state',true);
			if($post['state'] == 1)
			{
				$post['statetime'] = time();
			}
			if($this->notice->save($post,$id))
        	{
         		echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
       	 	}
       		else
        	{
           		echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
        	}
		}
		$notice = $this->notice->read($arg_array[0]);
		$this->data['notice'] = $notice;
		$wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or'));
		$this->data['wlshop'] = $wlshop;
		$email = $this->emaildata->find_all('1=1 and '.rtrim($fgshop,'or'));
		$this->data['email'] = $email;
		$mb = 2;
		if(isset($arg_array[0]))
		{
			if($arg_array[0]=='smt')
			{
				$mb = 1;
			}
			else if($arg_array[0]=='dlz')
			{
				$mb = 2;
			}
			else if($arg_array[0]=='tt')
			{
				$mb = 3;
			}
		}
		$this->data['arg'] = $mb;
		$this->_Template('notice_edit',$this->data);
	}
	//删除
	public function _del()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['s']))  
		{
            $id_arr = $this->input->post('s');
            $id_arr =  explode(',',$id_arr);
            if(!$id_arr)
            {
                echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
            }
            //循环删除记录
            foreach ($id_arr as $v) 
		    {
                $this->notice->remove($v);
            }
            echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
		}
    }
	
	public function _email()
	{
		if(isset($_SESSION['api']))
		{
			$user = $this->user->get_api($_SESSION['api']);
		    $fgshop = "";$sid = "";
		    $user = explode('|',trim($user['shop'],'|'));
			foreach ($user as $value) 
		    {
				$fgshop .= " shop = ".$value." or";
				$sid .= " id = ".$value." or";
			}
		}
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['page']))  
		{
		    $page = $this->input->post('page',true);
		    $perpage = $this->input->post('perpage',true);
			$shop = $this->input->post('shop',true);
			$email = $this->input->post('email',true);
			$notice = $this->input->post('notice',true);
			$xztime = $this->input->post('xztime',true);
			$timetk = $this->input->post('timetk',true);
			$timetj = $this->input->post('timetj',true);
			$timetk = strtotime($timetk);
			$timetj = strtotime($timetj);
			$where = "1=1 and (".rtrim($fgshop,'or').")";
			if($shop)
            {
                $where  .= " and shop = '$shop'";
            }
			if($email)
            {
                $where  .= " and email like '%$email%'";
            }
			if($notice)
            {
                $where  .= " and notice = '$notice'";
            }
			if($timetk && $timetj)
            {
                $where  .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
            }
            //数据排序
            $order_str = "id desc";
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
            //取得信息列表
            $info_list = $this->noticeemail->find_all($where,'id,shop,email,notice,addtime,time',$order_str,$start,$perpage);
			 //格式化数据
            foreach ($info_list as $key=>$value) 
		    {
				$data = $this->noticeemail->read($value['id']);
				$shop = $this->shop->read($value['shop']);
				$info_list[$key]['shop'] = $shop['shopname'];
				$info_list[$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
				if(is_numeric($value['time']) &&  $value['time'] > 0)
				{
				    $info_list[$key]['time'] = date('Y-m-d H:i:s',$value['time']);
				}
				else
				{
					$info_list[$key]['time'] = '';
					if($data['error'] != '')
					{
						$info_list[$key]['time'] = $data['error'];
					}
				}
				$notice = $this->notice->read($value['notice']);
				$info_list[$key]['notice'] = $notice['title'];
            }
		    $total = $this->noticeemail->find_count($where);
		    $pagenum = ceil($total/$perpage);
		    $over = $total-($start+$perpage);
		    $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
		    echo json_encode($rows);exit;
		}
		$wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or'));
		$this->data['wlshop'] = $wlshop;
		$notice = $this->notice->find_all("message = 1 and type = '3' and (".rtrim($fgshop,'or').")");
		$this->data['notice'] = $notice;
		$this->_Template('notice_email',$this->data);
	}
	
	public function _addemail()
	{
		if(isset($_SESSION['api']))
		{
			$user = $this->user->get_api($_SESSION['api']);
		    $fgshop = "";$sid = "";
		    $user = explode('|',trim($user['shop'],'|'));
			foreach ($user as $value) 
		    {
				$fgshop .= " shop = ".$value." or";
				$sid .= " id = ".$value." or";
			}
		}
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['email']))  
		{
			$time = time();$cw = '';$i = 0;$fs = '';
		    $notice = $this->input->post('notice',true);
			$n = $this->notice->read($notice);
			$email = $this->input->post('email',true);
			$email = str_replace(PHP_EOL, ',', $email);
            $email =  explode(',',trim($email,','));
			$this->db->trans_begin();
			foreach ($email as $v) 
		    {
				$f = $this->fullorder->find_all("email = '$v' and shop = '".$n['shop']."'",'*','id desc');
				if(isset($f[0]['email']))
				{
					$tid = $this->noticeemail->insert(array('shop'=>$f[0]['shop'],'email'=>$v,'notice'=>$notice,'addtime'=>$time));
					$fs .= $f[0]['id'].'-'.$tid.',';
				}
				else
				{
					$i++;
					$cw .= $v.',';
				}
			}
			if(count($email) == $i)
			{
				echo json_encode(array('msg'=>'Email地址全部错误,请排查!','success'=>false));exit;
			}
			if($this->db->trans_status() === TRUE)
			{
				$this->db->trans_commit();
				$ch = curl_init();
				$url = 'http://a'.rand(10,50).'.wepolicy.cn/notice/emailfs';
				curl_setopt($ch,CURLOPT_URL,$url);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
				curl_setopt($ch,CURLOPT_HEADER,0);
				curl_setopt($ch,CURLOPT_POST, 1);
				curl_setopt($ch,CURLOPT_TIMEOUT,1);
        		//设置post数据
				$post = array();
				$post['notice'] = $notice;
				$post['fs'] = trim($fs,',');
        		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
				$res = curl_exec($ch);
				curl_close($ch);
				if($i > 0)
				{
					echo json_encode(array('msg'=>'有错误Email地址,请排查!其它正常发送中。
错误Email:
'.trim($cw,',').'
','success'=>true));exit;
				}
				else
				{
					echo json_encode(array('n'=>0,'msg'=>'添加成功,邮件逐条发送中','success'=>true));exit;
			    }
				
			}
			else
			{
			    $this->db->trans_rollback();
				echo json_encode(array('msg'=>'系统错误,请重试!!','success'=>false));exit;
		    }
		}
		$notice = $this->notice->find_all("message = 1 and type = '3' and (".rtrim($fgshop,'or').")");
		$this->data['notice'] = $notice;
		$this->_Template('notice_addemail',$this->data);
	}
	
	public function _emailfs()
	{
		$time = time();
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['notice']))  
		{
			$notice = $this->input->post('notice');
			$notice = $this->notice->read($notice);
			$notice['email'] = $this->emaildata->read($notice['email']);
			$fs = $this->input->post('fs');
			$fs =  explode(',',$fs);
			foreach ($fs as $v) 
		    {
				$v = explode('-',$v);
				$data = $this->fullorder->read($v[0]);
				$shop = $this->shop->read($data['shop']);
				$express = $this->express->read($data['express']);
				$this->als->save(array('cs'=>json_encode(array($data,$shop,$express,$notice))),1);
				$go = $this->notice->get_god($data,$shop,$express,$notice);
				if($go == 1)
		        {
					$this->noticeemail->save(array('time'=>time()),$v[1]);
				}
				else
				{
					$this->noticeemail->save(array('error'=>$go),$v[1]);
				}
				sleep(50);
			}
		}
	}
	
	public function _testfs()
	{
		$time = time();
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['id']))  
		{
			$id = $this->input->post('id');
			$email = $this->input->post('email');
			$number = $this->input->post('number');
			$data = $this->fullorder->get_number($number);
			if(!$data)
			{
				$data = $this->fullordersmt->get_number($number);
				if(!$data)
				{
					$data = $this->fullordertt->get_number($number);
					if(!$data)
					{
						echo json_encode(array('msg'=>'编号无效!','success'=>false));exit;
					}
				}
			}
			else
			{
				if(!$email)
				{
					echo json_encode(array('msg'=>'请输入邮箱!','success'=>false));exit;
				}
			}
			$data['email'] = $email;
			$shop = $this->shop->read($data['shop']);
			$express = $this->express->read($data['express']);
			$id_arr =  explode(',',trim($id,','));$i = 0;
			foreach ($id_arr as $v) 
		    {
				$notice = $this->notice->read($v);
				if($notice)
				{
					$notice['email'] = $this->emaildata->read($notice['email']);
					$go = $this->notice->get_god($data,$shop,$express,$notice);
					if($go == 1)
		        	{
						$i++;
					}
				}
			}
			if($i == count($id_arr))
			{
				echo json_encode(array('msg'=>'发送成功','success'=>false));exit;
			}
			else
			{
				echo json_encode(array('msg'=>'有部分发送失败'.$go,'success'=>false));exit;
			}
		}
	}
}