<?php

class Translate_model extends CI_Model
{
	//初始化数据库连接
    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
        $this->adminsession = $this->session->userdata('session_admin');
    }
    
	
	//生成密码
	function randomkeys($length){
		$output='';
		for($a=0;$a<$length;$a++){
			
			$charIndes=mt_rand(48,122);//生成php随机数
			if($charIndes==94||$charIndes==95||$charIndes==96){//排除几个字符
				$length++;
			} else {
				$output.=chr(mt_rand(48,122));//生成php随机数
			}
		}
		return $output;
	}
	
    public  function getuserList(){
    	$sql='SELECT tlui_loginname,tlui_englishname,tlui_chinesename,tlui_id,
    	     tlui_email,tlui_status FROM TranslateUserInfo';
    	$query = $this->HT->query($sql);
    	return $query->result();
    }
	
	

    
    public function addTranslate($infotmp){
    	$info=array();
    	//'tli_opi_sn','tli_dateline','tli_status'
    	$files=array('tli_content','tli_tagetlgc','tli_otherquest','tli_title','tli_userid','tli_deadline','tli_ic_id');
    	$info['tli_ic_id'] = 0;
		foreach($infotmp as $k=>$v){
    		if(in_array($k,$files)){
    			$info[$k]=$v;
    		}
    	}
    	$info['tli_opi_sn']   =$this->adminsession["OPI_SN"];
    	$info['tli_dateline'] =date('Y-m-d H:i:s',time());
    	$info['tli_status']   =0;    	
    	$this->HT->insert('TranslateInfo', $info);
    	return $this->HT->insert_id();
    }
    
    
    //列表
    public function getList($key,$opiid=0,$userid=0,$status='',&$recordcount,$page,$per_page){
    	$result = array();
    	$searchwhere = '';
    	if(is_array($key)){
	    	foreach($key as $k=>$v){
	    		if(in_array($k,array('tli_userid','tli_opi_sn'))){
	    			$searchwhere .= " and $k = $v ";
	    		} elseif (in_array($k,array('tli_title'))){
	    			$searchwhere .= " and $k like '%$v%' ";
	    		} elseif ($k=='tli_checktime_f'){
					if($v){
						$searchwhere .= " and tli_checktime>'$v' ";
					}
				} elseif ($k=='tli_checktime_t'){
					if($v){
						$searchwhere .= " and tli_checktime<'$v' ";
					}
				}
	    	}
    	}
    	if( in_array($status,array('0','1','2','3','4','5','6','4-1','4-0'))){
    		if($status == '4-1'){
				$sqlwhere = "where  tli_status in(4,5,6) and tli_charge>0 ";
			} else if($status == '4-0'){
				$sqlwhere = "where  tli_status in(4,5,6) and tli_charge=0 ";
			} else {
				$sqlwhere = "where  tli_status=$status";
			}
    	} else {
    		$sqlwhere = "where 1=1 ";
    	}

    	$sqlwhere .= $searchwhere;
    	$sql = "select count(0) as num from TranslateInfo ".$sqlwhere;
    	$query = $this->HT->query($sql);
    	$recordcount = $query->first_row()->num; //获取总记录数量
    	$opiNameSql = ',(select OPI_Name from OperatorInfo where OPI_SN=TranslateInfo.tli_opi_sn) as OPI_Name,
		                (select tlui_chinesename+\'(\'+tlui_loginname+\')\' from TranslateUserInfo where tlui_id=tli_userid) as GET_Name';
    	$sql = "select * $opiNameSql from TranslateInfo ".$sqlwhere." order by tli_id desc";

    	$query = $this->HT->query($sql);
    	$lastpagenum = $recordcount-$page;
    	for($i=0;$i<$per_page && $i < $recordcount && $i < $lastpagenum ;$i++){

			$result[$i] = $query->row($page+$i);
			$result[$i]->infoLink = $this->getIsIdLink($result[$i]->tli_ic_id);
    	}
    	return $result;
    }
	
    public function deletetranslation($tli_id){
    	return $this->HT->delete('TranslateInfo', array('tli_id' => $tli_id));
    }
    
    public function getOpiList(){
    	$sql='SELECT OPI_SN,OPI_Name FROM OperatorInfo where DeleteFlag=0';
    	$query = $this->HT->query($sql);
    	return $query->result();
    } 
    
    public function updatetranslat($info,$tid){
		$info['tli_new'] = 1;
    	$result = $this->HT->update('TranslateInfo', $info, array('tli_id'=>$tid));
    	if($result){
    		return  array($tid);
    	} else {
    		return false;
    	}
    }
    
    public function checktranslate($ic_id){
		$sql = "select * from TranslateInfo where  tli_ic_id=$ic_id ";
		$q = $this->HT->query($sql);
		if($q->num_rows() >0){
			return true;
		} else {
			return false;
		}
	}
    //读取单条内容
    public function getTranslateInfo($tli_id=0,$ic_id=0){
		if($tli_id){
    		$sql = "select * from TranslateInfo where  tli_id=$tli_id ";
		}else if($ic_id){
			$sql = "select * from TranslateInfo where  tli_ic_id=$ic_id ";
		} else {
			return false;
		}
    	$query = $this->HT->query($sql);
    	if ($query->num_rows() > 0){
    		$row = $query->row_array();
    		$q = $this->HT->query("select OPI2_Name from OperatorInfo2 where OPI2_LGC=2 and OPI2_OPI_SN=$row[tli_opi_sn]");
    		$r = $q->row_array();
    		if($r){
    			$row['opi_name'] = $r['OPI2_Name'];
    		} else {
    			$row['opi_name'] = 'Unkown';
    		}   			
    		return $row;
    	} else {
    		return false;
    	}
    }
    
    /*以上部分为翻译内容*/
    /*以下部分为用户管理部分*/
    //用户管理部分以小写u开头
    //
	public function uList(&$recordcount,$page,$per_page){
		$result = array();
		$sql = "select count(0) as num from TranslateUserInfo ";
		$query = $this->HT->query($sql);
		$recordcount = $query->first_row()->num; //获取总记录数量
		$sql = "select * from TranslateUserInfo order by tlui_id desc";
		$query = $this->HT->query($sql);
		$lastpagenum = $recordcount-$page;
		for($i=0;$i<$per_page && $i < $recordcount && $i < $lastpagenum ;$i++){
			$result[$i] = $query->row($page+$i);
		}
		return $result;
	}
	//获取用户信息
	public function getUserInfo($userid){
		$sql = "select * from TranslateUserInfo where tlui_id='".$userid."'";
		$query = $this->HT->query($sql);
		return $query->first_row();
	}
	//更新用户
	public function update_user($userinfo,$userid)
	{
		$result = $this->HT->update('TranslateUserInfo', $userinfo, array('tlui_id'=>$userid));
		if($result){
			return  array($userid);
		}
	}
	
	public function deleteuser($uid){
		return $this->HT->delete('TranslateUserInfo', array('tlui_id' => $uid));
	}
	
	//公告部分
	public function getAnnouncList($per_page,$page=0){
		
		$sql = "select count(0) as num FROM TranslateMessage ";
		$query = $this->HT->query($sql);
		$recordcount = $query->first_row()->num; //获取总记录数量
		$result = array();
		
		$sql = "select tlm_id,tlm_title,tlm_content,tlm_dateline,tlm_hits FROM TranslateMessage ";
		$query = $this->HT->query($sql);
		$lastpagenum = $recordcount-$page;
		for($i=0;$i<$per_page && $i < $recordcount && $i < $lastpagenum ;$i++){
			$result[$i] = $query->row($page+$i);
		}
		return $result;
	}
	public function getAnnounc($id){
		$sql = "select * from TranslateMessage where tlm_id='".$id."'";
		$query = $this->HT->query($sql);
		return $query->first_row();
	}
	public function inertAnnounc($message){
		if($this->HT->insert('TranslateMessage', $message)){
			return $this->HT->insert_id();
		}else{
			return false;
		}
	}
	public function updateAnnounc($message,$id){
		$result = $this->HT->update('TranslateMessage', $message, array('tlm_id'=>$id));
		if($result){
			return  array($id);
		} else{
			return false;
		}
	}
	public function deleteAnnounc($id){
		return $this->HT->delete('TranslateMessage', array('tlm_id' => $id));
	}
	
	public function getIsIdLink($is_id){
		if(!is_numeric($is_id)) return '';
		$sql = "select ic_url FROM infoStructures inner join infoContents ON is_ic_id=ic_id where is_id=$is_id";
		$query = $this->HT->query($sql);
		$row = $query->first_row();
		if($row){
			return $row->ic_url;
		} else {
			return '';
		}
	}
}

