<?php
Doo::loadCore ( 'db/DooModel' );
class staff extends DooModel {
	
	
	
	public $sid;
	public $username;
	public $passwork;
	public $staffManage;
	public $isadmin;
	public $cid;
	public $othercid;
	public $category;
	public $othercategory;
	public $gender;
	public $qq;
	public $phone;
	public $telephone;
	public $email;
	public $birthday;
	public $position;
	public $avatar;
	public $appDate;
	public $hiredate;
	public $hireBiasDate;
	public $wxid;
	public $wecat;
	public $nature;
	public $remittanceName;
	public $bankName;
	public $bankNumber;
	public $coupletNumber;
	public $remittanceBankType;
	
	public $living;
	public $nativePlace;
	public $emergencyContacts;
	
	public $did;
	public $didName;
	public $jobNumber;
	public $baseWage;
	public $postWage;
	public $achievementBonus;
	public $pendingApprovals;
	public $processApprovals;
	public $processCC;
	
	public $InductionDate;
	public $pendStatus;
	public $updateDate;
	
	public $probationaryDate;
	public $regularsDate;
	
	public $qualifications;
	public $marriage;
	
	public $salaryCard;
	public $salaryBank;
	
	public $pendingApprovalsLeave;
	public $processApprovalsLeave;
	public $processLeaveCC;
	public $staffManageLeave;
	public $dimissionDate;
	public $dimissionReason;
	public $seniorityFormula;
	
	public $practiceDate;
	
	public $cldAccess;
	
	public $IDcards;
	public $IDcardsImgA;
	public $certificate;
	public $diploma;
	public $professionalTitle;
	public $IDcardsImgB;
	
	public $nation;
	public $graduateInstitutions;
	public $major;
	public $education;
	public $graduationTime;
	public $registeredResidence;
	public $householdRegistrationType;
	public $IDcardsValidity;
	public $positionId;
	
	public $appliedPromote;
	public $departmentID;
	public $applyLeaveDate;
	
	public $_table = 'CLD_staff';
	public $_primarykey = 'sid';
	public $_fields = array (
			
			'nation',
			'graduateInstitutions',
			'major',
			'education',
			'graduationTime',
			'registeredResidence',
			'householdRegistrationType',
			'IDcardsValidity',
			
			'IDcards',
			'IDcardsImgA',
			'IDcardsImgB',
			'certificate',
			'diploma',
			'professionalTitle',
			
			'sid',
			'username',
			'staffManage',
			'birthday',
			'position',
			'passwork',
			'isadmin',
			'cid',
			'othercid',
			'appDate',
			'category',
			'othercategory',
			'gender',
			'qq',
			'phone',
			'telephone',
			'email',
			'avatar',
			'hiredate',
			'hireBiasDate',
			'wxid',
			'nature',
			'remittanceName',
			'bankName',
			'bankNumber',
			'remittanceBankType',
			'coupletNumber',
			'did',
			'jobNumber',
			'baseWage',
			'postWage',
			'achievementBonus',
			'pendingApprovals',
			'processApprovals',
			'processCC',
			
			
			'pendingApprovalsLeave',
			'processApprovalsLeave',
			'processLeaveCC',
			'staffManageLeave',
			
			'InductionDate',
			'pendStatus',
			'wecat',
			'updateDate',
			
			'dimissionReason',
			
			'salaryCard',
			'salaryBank',
			'qualifications',
			'marriage',
			
			'living',
			'nativePlace',
			'emergencyContacts',
			
			'practiceDate',
			'probationaryDate',
			'regularsDate',
			'dimissionDate',
			'seniorityFormula',
			'cldAccess',

			'departmentID',
			
			'didName',
			'positionId',
			'appliedPromote',
			
			'applyLeaveDate',
	);
	public function checkUser($uid, $passwork) {
		return $this->find ( array (
				'select' => 'position,birthday,sid,username,isadmin,cid,othercid,category,othercategory,gender,qq,phone,telephone,email,avatar,hiredate,nature',

				'where' => "username= '" . $uid . "' and passwork = '" . md5 ( $passwork ) . "' and nature!=4",
				'asArray' => TRUE 
		) );
	}
	public function getStaffByName($username = '') {
		
		if (empty($username))
			return array();
		$detail=$this->getOne ( array (
				'asc' => 'sid',
				'where' => "username= '" . $username . "'",
				'asArray' => TRUE 
		) );
		
		if (empty($detail))
			return array();
		Doo::loadModel ( 'department' );
		$department = new department ();
		$detail ['departmentName']=array();
		if($detail['departmentID']!=0){
			$detail ['departmentName'] =$department->getDepartmentByDid($detail['departmentID']);
		}
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		if (!empty($detail))
			$detail['sidKey'] = $XDeode->encode ( $detail ['sid'] );
		return $detail;
	}
	
	
	
	/**
	 * @return string
	 */
	public function getStaff($admin=false) {
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		Doo::loadModel ( 'department' );
		$department = new department ();
		Doo::loadModel ( 'position' );
		$position = new position ();
		
		$condition = array (
				'desc' => 'jobNumber',
				'asArray' => TRUE 
		);
		
		if ($admin)
			$condition += array (

					'where' => "username != 'admin' and nature !=4",
			);
		
		$list = $this->find ( $condition);
		foreach ( $list as $key => $value ) {
			$list [$key] ['departmentName'] ='';
			if($value['departmentID']!=0){
				 $list [$key] ['departmentName'] =$department->getDepartmentByDid($value['departmentID']);
			}
			
			$list [$key] ['sidKey'] = $XDeode->encode ( $value ['sid'] );
			$list [$key] ['cidKey'] = $XDeode->encode ( $value ['cid'] );
			$list [$key] ['didKey'] = '';
			if (!empty($value ['departmentID']))
				$list [$key] ['didKey'] = $XDeode->encode ( $value ['departmentID'] );
			
				$list [$key] ['position'] ='';
				$pdetail=$position->getPositionByPid($value['positionId']);
				if (!empty($pdetail))
					$list [$key] ['position'] =$pdetail['positionName'];
				
		}
		
		return $list;
	}
	
	/**
	 * 根据条件获得查询内容
	 * @param string $con
	 * @return string
	 */
	public function getStaffByCondition($con=''){
		if ( empty ( $con ))
			return array ();
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		Doo::loadModel ( 'department' );
		$department = new department ();
		
		Doo::loadModel ( 'position' );
		$position = new position ();
		
		$list = $this->find ( array (
				'where' => $con,
				'desc' => 'jobNumber',
				'asArray' => TRUE
		) );
		foreach ( $list as $key => $value ) {
			
			$list [$key] ['position'] ='';
			$pdetail=$position->getPositionByPid($value['positionId']);
			if (!empty($pdetail))
				$list [$key] ['position'] =$pdetail['positionName'];
			
			
			$list [$key] ['departmentName'] ='';
			if($value['departmentID']!=0){
				$list [$key] ['departmentName'] =$department->getDepartmentByDid($value['departmentID']);
			}
			
			$list [$key] ['didArray']=array();
			if (!empty($value['did']))
			$list [$key] ['didArray']=explode(',', $value['did']);
			
			$list [$key] ['cldAccessArray'] = json_decode($value['cldAccess']);
			$list [$key] ['sidKey'] = $XDeode->encode ( $value ['sid'] );
			$list [$key] ['cidKey'] = $XDeode->encode ( $value ['cid'] );
			$list [$key] ['didKey'] = '';
			if (!empty($value ['departmentID']))
				$list [$key] ['didKey'] = $XDeode->encode ( $value ['departmentID'] );
		}
		return $list;
	}
	
	public function getStaffInCid($cid = '') {
		if (empty ( $cid ))
			return array ();
		$list = $this->find ( array (
				'asc' => 'sid',
				'where' => "cid in(" . $cid . ")",
				'asArray' => TRUE 
		) );
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		foreach ( $list as $key => $value ) {
			$list [$key] ['sidKey'] = $XDeode->encode ( $value ['sid'] );
		}
		
		return $list;
	}
	
	/**
	 * 获得单位下所有的员工
	 * @param number $pid
	 */
	public function getStaffByPositionId($pid=0){
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		
		if (! is_numeric ( $pid ))
			$pid = $XDeode->decode ( $pid );
		
		$list = $this->find ( array (
				'where' => "positionId= '" . $pid . "' and username!='admin'",
				'asArray' => TRUE
		) );
// 		Doo::loadClass ( 'XDeode' );
// 		$XDeode = new XDeode ( 5 );
// 		foreach ( $list as $key => $value ) {
// 			$list [$key] ['sidKey'] = $XDeode->encode ( $value ['sid'] );
// 		}
		
		return $list;
	}
	
	public function getStaffByCid($cid = 0) {
		$list = $this->find ( array (
				'asc' => 'sid',
				'where' => "cid= '" . $cid . "' and nature !=4 ",
				'asArray' => TRUE
		) );
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		Doo::loadModel ( 'position' );
		$position = new position ();
		
		foreach ( $list as $key => $value ) {
			$list [$key] ['sidKey'] = $XDeode->encode ( $value ['sid'] );
			
			$list [$key] ['position'] ='';
			$pdetail=$position->getPositionByPid($value['positionId']);
			if (!empty($pdetail))
				$list [$key] ['position'] =$pdetail['positionName'];
			
		}
		return $list;
	}
	
	public function getStaffByCidAll($cid = 0){
		$list = $this->find ( array (
				'asc' => 'sid',
				'where' => "cid= '" . $cid . "' ",
				'asArray' => TRUE
		) );
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		Doo::loadModel ( 'position' );
		$position = new position ();
		
		foreach ( $list as $key => $value ) {
			$list [$key] ['sidKey'] = $XDeode->encode ( $value ['sid'] );
				
			$list [$key] ['position'] ='';
			$pdetail=$position->getPositionByPid($value['positionId']);
			if (!empty($pdetail))
				$list [$key] ['position'] =$pdetail['positionName'];
					
		}
		return $list;
	}
	
	/**
	 * 获得审批中员工
	 */
	public function getStaffByApplied($sid=0){
		$list = $this->find ( array (
				'asc' => 'sid',
			
				'where' => "(pendStatus= '2' or pendStatus='3' or pendStatus='5' or pendStatus='6' or pendStatus='7') and (pendingApprovals ='" . $sid . "' or pendingApprovalsLeave='" . $sid . "')",
				'asArray' => TRUE
		) );
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		Doo::loadModel ( 'position' );
		$position = new position ();
		foreach ( $list as $key => $value ) {
			$list [$key] ['sidKey'] = $XDeode->encode ( $value ['sid'] );
			
			$list [$key] ['position'] ='';
			$pdetail=$position->getPositionByPid($value['positionId']);
			if (!empty($pdetail))
				$list [$key] ['position'] =$pdetail['positionName'];
			
		}
		
		return $list;
	}
	
	/**
	 * 获得最近审批通过的员工
	 * @return string
	 */
	public function getStaffByApprovals(){
		$list = $this->find ( array (
				'asc' => 'InductionDate',
				'where' => "username!= 'admin' and pendStatus=0 and InductionDate!=0",
				'limit' => 10,
				'asArray' => TRUE
		) );
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		foreach ( $list as $key => $value ) {
			$list [$key] ['sidKey'] = $XDeode->encode ( $value ['sid'] );
		}
		
		return $list;
	}
	
	/**
	 * 获得最近更新的员工
	 */
	public function getStaffByUpdateDate(){
		$list = $this->find ( array (
				'asc' => 'updateDate',
				'where' => "username!= 'admin'",
				'limit' => 10,
				'asArray' => TRUE
		) );
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		foreach ( $list as $key => $value ) {
			$list [$key] ['sidKey'] = $XDeode->encode ( $value ['sid'] );
		}
		
		return $list;
	}
	
	public function getUserById($sid = 0) {
		
		$list =$this->find ( array (
				'asc' => 'sid',
				'where' => "sid= '" . $sid . "'",
				'asArray' => TRUE
		) );
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		foreach ( $list as $key => $value ) {
			$list [$key] ['sidKey'] = $XDeode->encode ( $value ['sid'] );
		}
		
		return $list;
	}
	public function getUserByIdList($puid) {
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		
		$puid = $XDeode->decode ( $puid );
		$list=$this->find ( array (
				'where' => "sid= '" . $puid . "'",
				'asArray' => TRUE
		) );
		
		foreach ($list as $key=>$value){
			$list[$key]['sidKey']=$XDeode->encode ( $value ['sid'] );
		}
		
		return $list;
	}
	
	/**
	 * 检查信息是否填写完整
	 * @param unknown $sid
	 * @return string
	 */
	public function checkStaffInfoIsComplete($sid){
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		
		if (!is_numeric($sid)){
			$sid = $XDeode->decode ( $sid );
		}
		
		$detail=$this->getOne ( array (//or wecat='' or email='' or coupletNumber=''
				'where' => "sid= '" . $sid . "' and (telephone='' or qq=''  or phone=''  or birthday='' or marriage='' or living='' or nativePlace='' or emergencyContacts='' or IDcards='' 
				or IDcardsImgA='' or IDcardsImgB='' or remittanceName=''  or bankName='' or bankNumber='' or salaryCard='' or salaryBank='' )",
				'asArray' => TRUE
		) );
		
		return $detail;
	}
	
	
	/**
	 * 根据参数字段更新相应字段（主键ID必须传）
	 * @param array $item 相关需要更新的字段信息
	 * @return number 返回员工ID
	 */
	public function setStaffByCondition($item = array()) {
		$lid = 0;
		if (is_array ( $item ) && ! empty ( $item )) {
			foreach ( $item as $key => $value ) {
				$this->$key = $value;
			}
			$lid = $this->update ();
		}
		return $lid;
	}
	
	/**
	 * 
	 * @param number $nature
	 */
	public function getStaffJobNumberByNature($nature=1){
		$detail=$this->getOne ( array (
				'where' => "nature= '" . $nature . "'",
				'desc' => 'jobNumber',
				'asArray' => TRUE
		) );
		
		return $detail;
	}
	
	/**
	 *
	 * @param number $nature
	 */
	public function getStaffJobNumberByInformal(){
		$detail=$this->getOne ( array (
				'where' => "nature= '2' or nature ='3'",
				'desc' => 'jobNumber',
				'asArray' => TRUE
		) );
	
		return $detail;
	}
	
	/**
	 * 根据用户ID获取相关信息
	 * @param number $sid 用户ID
	 */
	public function getStaffBySid($sid = 0) {
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		if (!is_numeric($sid)&&!empty($sid)){
			$sid = $XDeode->decode ( $sid );
		}
		
		$detail = array ();
		if (! empty ( $sid )){
			$detail = $this->getOne ( array (
					'where' => "sid= '" . $sid . "'",
					'asArray' => TRUE 
			) );
			
			$detail['sidKey']=$XDeode->encode ( $detail ['sid'] );
			
			Doo::loadModel ( 'department' );
			$department = new department ();
			$detail ['department'] =$department->getDepartmentByDid($detail['departmentID']);
			
			//获得岗位信息
			Doo::loadModel ( 'position' );
			$position = new position ();
			
			$positionIdKey=$XDeode->encode ( $detail ['positionId'] );
			$detail ['positionIdKey']=$positionIdKey;
			$positionDetail=$position->getPositionByPid($detail ['positionId']);
			//print_r('/'.$detail ['positionId'].'/');
			
			if(empty($positionDetail)){
				$positionDetail['baseWage']=0;
				$positionDetail['postWage']=0;
				$positionDetail['achievementBonus']=0;
				$positionDetail['positionName']='';
			}
			$detail ['positionDetail'] =$positionDetail;
			
			
			$d1 = strtotime($detail['hiredate']);//过去的某天，你来设定
			$d2 = 1 + ceil((time()-$d1)/60/60/24);
			$detail['workforce']=$d2;
			
			$d1 = strtotime($detail['dimissionDate']);//离职到今天
			$d2 = 1 + ceil((time()-$d1)/60/60/24);
			$detail['workforceDimission']=$d2;
			
			$detail ['cldAccessArray'] =array();
			if (!empty($detail['cldAccess']))
			 $detail ['cldAccessArray'] =json_decode($detail['cldAccess']);
			
			$detail['sidKey']=$XDeode->encode ( $detail ['sid'] );
			
			
			$webSite=WEB_SITE.'/upload/credentialInfo/';
			$detail['webSite']=$webSite;
			$detail['certificateArray']=array();
			if(!empty($detail['certificate'])){
				$certificate=json_decode($detail['certificate']);
				if(!empty($certificate)){
					foreach ($certificate as $key=>$value){
						$certificate[$key]=$value;
					}
					$detail['certificateArray']=$certificate;
				}
			}
			$detail['diplomaArray']=array();
			if(!empty($detail['diploma'])){
				$certificate=json_decode($detail['diploma']);
				if(!empty($certificate)){
				foreach ($certificate as $key=>$value){
					$certificate[$key]=$value;
				}
				$detail['diplomaArray']=$certificate;
				}
			}
			$detail['professionalTitleArray']=array();
			if(!empty($detail['professionalTitle'])){
				$certificate=json_decode($detail['professionalTitle']);
				if(!empty($certificate)){
				foreach ($certificate as $key=>$value){
					$certificate[$key]=$value;
				}
				$detail['professionalTitleArray']=$certificate;
				}
			}
			
		}
		return $detail;
	}
	
	public function getApprovalData($sid=0){
		$list = $this->find ( array (
				'asc' => 'sid',
				'where' => "(pendStatus= '2' or pendStatus='3' or pendStatus='5' or pendStatus='6') and ( pendingApprovalsLeave='" . $sid . "' or pendingApprovals='" . $sid . "') ",
				'asArray' => TRUE
		) );
		return $list;
	}
	
	public function getStaffByWxid($wxid = '') {
		// echo "wxid in ( " . $wxid . ")";
		if (empty ( $wxid ))
			return array ();
		return $this->find ( array (
				'select' => 'sid,wxid',
				'where' => "wxid in ( " . $wxid . ")",
				'asArray' => TRUE 
		) );
	}

	// 建筑获取办事处人员部分信息
	public function getStaffByCidOnBuilding($cid = 0) {
		$list = $this->find ( array (
			'select' => 'sid,username,cid,departmentID,category,qq,phone,telephone',
			'asc' => 'sid',
			'where' => "cid= '" . $cid . "'",
			'asArray' => TRUE
		) );

		return $list;
	}
	
	
	
}

?>