<?php

Doo::loadCore('db/DooModel');

class client_staff extends DooModel {

    public $csid;
    public $sid;
    public $cid;
   
    public $_table = 'CLD_client_staff';
    public $_primarykey = 'csid';
    public $_fields = array('csid', 'sid', 'cid');


    function getClientByCid($cid){
    	$sql = "select * from " . $this->_table . " as a left join CLD_staff as b on (a.sid=b.sid)  where a.cid= '".$cid."' " ;
		
    	$query = Doo::db ()->query ( $sql );
		
		$result = $query->fetch ();
		
		return $result;
    }
    
	function getClientByStaff($condition="",$limit,$staffid="",$sid,$search,$tooltip,$cateid,$tagSQL=''){
		if (!empty($staffid)){
			//$staffid="and a.sid= '".$staffid."'";
			//a.cid,a.gender,a.clientname,a.keynum,a.companyid,a.companyname,a.updatetime,a.servicetime,a.department,a.position  left join CLD_tag_client as c on (b.cid=c.client) " . $this->_table . " as a ".$staffid."
    		$sql = "select b.landmarks,b.stay,b.local,b.ride,b.address,b.district,b.office,b.fax,b.email,b.qq,b.phone,b.mark,b.nicename,b.cid,b.gender,b.clientname,b.keynum,b.companyid,b.companyname,b.updatetime,b.servicetime,b.department,b.position,b.priority,b.telephone from   CLD_client as b  left join CLD_tag_client as c on (b.cid=c.client and c.sid=".$staffid.") where 1 ".$condition."  ".$limit ;
		}else{//
			if(strlen($condition)==1||empty($condition))
				$sql = "select  b.landmarks,b.stay,b.local,b.ride,b.address,b.district,b.office,b.fax,b.email,b.qq,b.phone,b.mark,b.nicename,b.cid,b.gender,b.clientname,b.keynum,b.companyid,b.companyname,b.updatetime,b.servicetime,b.department,b.position,b.priority,b.telephone from  CLD_client as b ".$limit ;
			else//left join CLD_tag_client as c on (a.cid=c.client)  where 1 ".$condition." 
				$sql = "select  a.landmarks,a.stay,a.local,a.ride,a.address,a.district,a.office,a.fax,a.email,a.qq,a.phone,a.mark,a.nicename,a.cid,a.gender,a.clientname,a.keynum,a.companyid,a.companyname,a.updatetime,a.servicetime,a.department,a.position,a.priority,a.telephone from CLD_client  as a left join CLD_tag_client as c on (c.client=a.cid and c.sid=".$sid." )  where 1 ".$condition."  ".$limit ;	
		}
		
		if(!empty($search))
			$sql = "select  b.landmarks,b.stay,b.local,b.ride,b.address,b.district,b.office,b.fax,b.email,b.qq,b.phone,b.mark,b.nicename,b.cid,b.gender,b.clientname,b.keynum,b.companyid,b.companyname,b.updatetime,b.servicetime,b.department,b.position,b.priority,b.telephone from  CLD_client as b where 1 ".$condition." ".$limit ;
	
		if(!empty($tooltip)){	
			$condition=str_replace("a.","c.",$condition);  
			if ($tagSQL)
			$sql = "select b.address,b.district, b.office,b.fax,b.email,b.qq,b.phone,b.mark,b.nicename, b.cid,b.gender,b.clientname,b.keynum,b.companyid,b.companyname,b.updatetime,b.servicetime,b.department,b.position,b.priority,b.telephone from CLD_C_tooltip as a left join  CLD_tag_client as c on (a.cid=c.client) left join CLD_client as b on (a.cid=b.cid )  where  a.name like '%".$tooltip."%' and a.cateid = ".$cateid." ".$condition."  ".$limit ;
			else
			$sql = "select b.address,b.district, b.office,b.fax,b.email,b.qq,b.phone,b.mark,b.nicename, b.cid,b.gender,b.clientname,b.keynum,b.companyid,b.companyname,b.updatetime,b.servicetime,b.department,b.position,b.priority,b.telephone from CLD_C_tooltip as a  left join CLD_client as b on (a.cid=b.cid )  where  a.name like '%".$tooltip."%' and a.cateid = ".$cateid." ".$condition."  ".$limit ;
		}
		//	echo $sql;//die;
    	$query = Doo::db ()->query ( $sql );
		$result = $query->fetchAll ();
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		foreach ($result as $key=>$value){
			$result[$key]['companyidKey'] = $XDeode->encode( $value['companyid'] );
		}
		
		
		return $result;
    	
    }
    
    public function synClient($sid ,$district){
    	
    	$sql="select b.cid,b.sid,a.district from CLD_client  as a right join " . $this->_table . " as b on (b.cid=a.cid and b.sid !=".$sid." ) where a.district LIKE  '".$district."%' ";
    	
    	$query = Doo::db ()->query ( $sql );
		
		$result = $query->fetchAll ();
		
		return $result;
    }
    
    public function getProject(){
    	
    	return $this->find ( array ('asc' => 'pid', 'asArray' => TRUE ) );
    }
    
    public function getProjectByIdList($projectid){
    	return $this->find ( array ('where' => "pid= '".$projectid."'", 'asArray' => TRUE ) );
    }
    
}

?>