<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &短信发送&记录类文件& By CC
 */

if (!defined('IS_RUN')) die('Forbsendidden');

class tj_sendsms extends tj_model {

    var $table = 'dbpre_sendsms';
	var $key = 'sendid';
	var $smsname;
	var $smspwd;
	
	
    function __construct() {
        parent::__construct();
		$this->S =  G_class('setsms');  //加载短信配置类
		$smsconfig = $this->S->read_one('1');//1为短信接口id
		$this->smsname = $smsconfig['smsname'];
		$this->smspwd = $smsconfig['smspwd'];
        $this->init_field();
    }

    function tj_sendsms() {
        $this->__construct();
    }

    function init_field() {
        $this->add_field('sendid,uid,mobile,content,dateline');
		$this->add_field_fun('mobile,content', '_T');
		$this->add_field_fun('mobile,content', 'trim');
		$this->add_field_fun('sendid,uid,dateline', 'intval');
    }
	

	 function read_all($type=null,$uid) {
        $this->db->from($this->table);
		if($type) $this->db->where_in('bid',$type);//获取下级部门短信记录
		if($uid) $this->db->where('uid',$uid);//获取自己短信记录
		$this->db->order_by('dateline','DESC');//必须为大写DESC或ASC
        $this->db->select('sendid,uid,mobile,content,dateline');
		
		$arr_sendsmss = $this->db->fetch_all_assoc();
		foreach($arr_sendsmss as $key=>$item){
			$arr_sendsms[] = $item;
			$arr_sendsms[$key]['user'] = G_class('user')->read($item['uid'],FALSE,'realname');
			$arr_sendsms[$key]['time'] =Ue_time($item['dateline']);
		}
        return $arr_sendsms;
    }


    function update(& $post) {
        if(!is_array($post)) return;
        foreach($post as $key => $val) {
			$p = array($key=>$val);
            $this->save($p, $post['sendid'], FALSE);
        }
    }

    function save($post,$variable=null,$uid=null) {
		 if(!$variable&&!$uid) {
			 global $_CO;
            $post['uid'] = $_CO['uid'];//发送人
            $post['dateline'] = $this->global['timestamp'];
        }else{
			$post['uid'] = $uid;//发送人
            $post['dateline'] = $this->global['timestamp'];
		}
        $variable = parent::save($post,$variable);
        return $variable;
    }
	
	function M_sendSMS($post,$variable=null) {
		
		$post['uid'] = $this->smsname;
		$post['pwd'] = $this->smspwd;
		global $_CO;
		$arr_user=G_class('user')->read($_CO['uid']);
		if($arr_user['sms_balance']>0){
		 //过滤手机号码
		   		$i=0;
				$array_mo = explode(",",$post['mobile']);
				$post['mobile'] ='';
				foreach ($array_mo as $key=>$var){
					if($var>0&&isphonenum($var)) {
						$post['mobile'].=$var.',';
						$i=$i+1;	
					}else{
						$false_mobile.= $var.',';
						}
				}
			$post['mobile'] = rtrim($post['mobile'], ",");
			
		   //过滤手机号码
	   if($arr_user['sms_balance']>=$i){}else{Note('您的账户短信余额不足'.$i.'条，请联系管理员！');}
		
		//加载创明短信平台接口类
       if(G_class('sendsms_sunnyh')->sendSMS($post)=='100'){

		   //短信发送记录处理
		   G_class('user')->update_smsnum($_CO['uid'],$arr_user['sms_balance']-$i); //更新剩余短信
		   G_class('user')->update_h_smsnum($_CO['uid'],$arr_user['h_send_sms']+$i);//更新已发短信
		   
		   $repost = array();
		   $repost['bid'] = $_CO['userbid'];
		   $repost['mobile'] = $post['mobile'];
		   $repost['content'] = $post['content'];
		   if($this->save($repost)) {
			   
			   if(intval($false_mobile)==0){
				     Note($i>1?'短信全部发送成功！':'短信发送成功！');
			   }else{
				    Note('短信部分发送成功，这些号码未发送成功：'.$false_mobile);
			   }

			   }else{Note('短信发送成功，但系统无法记录，请及时联系管理员！');}
	   }
	}else{
		Note('您的账户短信余额不足，请联系管理员！');
	}
    }
	
	function Ajax_send_verify($post,$uid) {
		
		$post['uid'] = $this->smsname;
		$post['pwd'] = $this->smspwd;
		
		//过滤手机号码
		   		$i=0;
				$array_mo = explode(",",$post['mobile']);
				$post['mobile'] ='';
				foreach ($array_mo as $key=>$var){
					if($var>0&&isphonenum($var)) {
						$post['mobile'].=$var.',';
						$i=$i+1;	
					}else{
						$false_mobile.= $var.',';
						}
				}
			$post['mobile'] = rtrim($post['mobile'], ",");
			
		   //过滤手机号码
		   if(G_class('sendsms_sunnyh')->sendSMS($post,null,1)=='100'){
		   $repost['bid'] = 1;
		   $repost['mobile'] = $post['mobile'];
		   $repost['content'] = $post['content'];
		   $this->save($repost,null,$uid);
		   return '100';
		   }
	}
	
	
	function Ajax_sendSMS($post,$variable=null) {
		
		$post['uid'] = $this->smsname;
		$post['pwd'] = $this->smspwd;
		global $_CO;
		$arr_user=G_class('user')->read($_CO['uid']);
		if($arr_user['sms_balance']>0){
		 //过滤手机号码
		   		$i=0;
				$array_mo = explode(",",$post['mobile']);
				$post['mobile'] ='';
				foreach ($array_mo as $key=>$var){
					if($var>0&&isphonenum($var)) {
						$post['mobile'].=$var.',';
						$i=$i+1;	
					}else{
						$false_mobile.= $var.',';
						}
				}
			$post['mobile'] = rtrim($post['mobile'], ",");
			
		   //过滤手机号码
		   if($arr_user['sms_balance']>=$i){
			//短信发送记录处理
		   G_class('user')->update_smsnum($_CO['uid'],$arr_user['sms_balance']-$i); //更新剩余短信
		   G_class('user')->update_h_smsnum($_CO['uid'],$arr_user['h_send_sms']+$i);//更新已发短信
		   
		   }else{
			   $json['status'] = 0;
			   $json['msg']='您的账户短信余额不足'.$i.'条，请联系管理员！';
		   }
		
		//加载创明短信平台接口类
       if(G_class('sendsms_sunnyh')->sendSMS($post,null,1)=='100'){
		   //短信发送记录处理
		   
		   $repost = array();
		   $repost['bid'] = $_CO['userbid'];
		   $repost['mobile'] = $post['mobile'];
		   $repost['content'] = $post['content'];
		   if($this->save($repost)) {
			   
			   if(intval($false_mobile)==0){
				   	$json['status'] = 1;
			   }else{
				   	 $json['status'] = 0;
				     $json['msg']='短信部分发送成功，这些号码未发送成功：'.$false_mobile;
			   }
			 
			   }else{ $json['status'] = 0;$json['msg']='短信发送成功，但系统无法记录，请及时联系管理员！';}
	   }
	}else{
		  $json['status'] = 0;
		  $json['msg']='您的账户短信余额不足，请联系管理员！';
	}
	return $json;
    }
	
	
	//获取接口剩余短信数
	function G_remainderSMS() {
		
		$post['uid'] = $this->smsname;
		$post['pwd'] = $this->smspwd;
		
		//加载创明短信平台接口类
       $re_r = G_class('sendsms_sunnyh')->remainderSMS($post);
	   if(intval($re_r)==0){
		  return '<a href="">重新获取</a>';
	   }elseif(intval($re_r)==1){
		   return '用户名或密码错误';
	   }
		 return $re_r;
	
    }
	//获取接口已发短信数
	function G_havesendSMS() {
		
		$post['uid'] = $this->smsname;
		$post['pwd'] = $this->smspwd;
		
		//加载创明短信平台接口类
	   $re_h = G_class('sendsms_sunnyh')->remainderSMS($post,'send');
	   if(intval($re_h)==0){
		  return '<a href="">重新获取</a>';
	   }elseif(intval($re_h)==1){
		   return '用户名或密码错误';
	   }
		 return $re_h;
	
    }
	
    function check_post(& $post, $edit = FALSE) {
        //if($edit && isset($post['groupname']) && !$post['groupname']) redirect('membercp_usergroup_empty_name');
      //  if(!$edit && !$post['groupname']) redirect('membercp_usergroup_empty_name');
       // if(!$edit && !in_array($post['grouptype'],array('member','special','system'))) redirect('membercp_usergroup_empty_type');
      //  if(!$edit && $post['grouptype']=='member' && !is_numeric($post['point'])) redirect('membercp_usergroup_empty_point');
    }
}
?>