<?php
class oacontact extends netsDb {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function oacontact($pConnection){
		$this->netsDb($pConnection);
	}
	public function Check(&$pTableDescriptAR,$pExceptionAR1,$pExceptionAR2){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';

		if(count($pExceptionAR1)==0){
			$pExceptionAR1=array();
		}
		 parent::Check($pTableDescriptAR,$pExceptionAR1,$pExceptionAR2);
		//
		$tmpSTR='ocotName';
		if(strlen($pTableDescriptAR[0]['Columns'][$tmpSTR]['Value'])<1 && !in_array($tmpSTR,$pExceptionAR2)){
			$this->sysLastError='提交内容错误:1.名称不能为空';
			return $this->sysErrorFuncID.'001';
		}
		
		return 0;
	}
	
	public function AddContacts($pDescriptions,$pUserids){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$this->TransStart();
		//-- ------ #Check MyUserid ------ --
		$tmpSQL=' SELECT A.uitID,A.uitName,A.uitNickname,A.uitEmail,
			B.uictSourceR00
		
			FROM {Nets.TableHead}netsuserinfo AS A
			INNER JOIN {Nets.TableHead}netsuserinfocont AS B ON B.uictIndexID=A.uitID
			WHERE 1=1 AND A.uitRecycle=0 AND A.uitChecked>1 AND A.uitID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['ocotCreateBy']['Value'],$tmpSTR);
		$tmpData_My=$this->Search($tmpSTR);
		if (count($tmpData_My)==0) {
			$this->sysLastError='新增联系人失败:登陆后才能增加联系人';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//-- ------ #Check Userids ------ --
		$tmpSQL=' SELECT A.uitID,A.uitName,A.uitEmail,A.uitNickname,
			B.uictCo_Name,B.uictCo_Sex,B.uictCo_Tele,B.uictCo_Mobile,B.uictCo_Fax,B.uictCo_Software,
			B.uictCo_Website,B.uictCo_NumCode,B.uictCo_ZoneChoose,B.uictCo_ZoneDetail,B.uictCo_Address,B.uictCo_Company,
			B.uictCo_PersPosition,B.uictCo_PersSpical,B.uictInfo_Hometown,B.uictInfo_Birthday,B.uictSourceR00

			FROM {Nets.TableHead}netsuserinfo AS A
			INNER JOIN {Nets.TableHead}netsuserinfocont AS B ON B.uictIndexID=A.uitID
			WHERE 1=1 AND A.uitRecycle=0 AND A.uitChecked>1 AND (1=2 {Nets.IDS}) ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR_0='';
		foreach ($pUserids as $Row0){
			$tmpSTR_0.=' OR A.uitID='.$tmpCode->Int32($Row0,0).' ';
		}
		$tmpSTR=str_replace('{Nets.IDS}',$tmpSTR_0,$tmpSTR);
		$tmpData_User=$this->Search($tmpSTR);
		if (count($tmpData_User)==0) {
			$this->sysLastError='新增联系人失败:请选择至少一个联系人';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//-- ------ #Update exist contact ------ --
		//-- #GET --
		$tmpSQL=' SELECT A.ocotID,A.ocotName,A.ocotCreateBy,A.ocotUserid,A.ocotRecycle
			FROM {Nets.TableHead}oacontact AS A
			WHERE 1=1 AND
			A.ocotCreateBy={Nets.CreateBy} AND (1=2 {Nets.IDS}) ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.CreateBy}',$pDescriptions[0]['Columns']['ocotCreateBy']['Value'],$tmpSTR);
		$tmpSTR_0='';
		foreach ($pUserids as $Row0){
			$tmpSTR_0.=' OR A.ocotUserid='.$tmpCode->Int32($Row0,0).' ';
		}
		$tmpSTR=str_replace('{Nets.IDS}',$tmpSTR_0,$tmpSTR);
		$tmpData_Contact=$this->Search($tmpSTR);
		//-- #Filter and update --
		$tmpSQL_A=' UPDATE {Nets.TableHead}oacontact AS A
			SET A.ocotName=\'{Nets.Name}\',
			A.ocotRecycle=0
			WHERE 1=1 AND A.ocotID={Nets.ID} ';
		$tmpSQL_B=' UPDATE {Nets.TableHead}oacontactcont AS A
			SET A.ococtCoName=\'{Nets.CoName}\', 
			A.ococtCoEmail=\'{Nets.CoEmail}\', 
			A.ococtCoTele=\'{Nets.CoTele}\', 
			A.ococtCoMobile=\'{Nets.CoMobile}\', 
			A.ococtCoFax=\'{Nets.CoFax}\', 
			A.ococtCoSoftware=\'{Nets.CoSoftware}\', 
			A.ococtCoWebsite=\'{Nets.CoWebsite}\', 
			A.ococtCoNumCode=\'{Nets.CoNumCode}\', 
			A.ococtCoAddress=\'{Nets.CoAddress}\', 
			A.ococtCoCompany=\'{Nets.CoCompany}\', 
			A.ococtCoDepartment=\'{Nets.CoDepartment}\', 
			A.ococtCoPosition=\'{Nets.CoPosition}\', 
			A.ococtCoSpical=\'{Nets.CoSpical}\', 
			A.ococtSourceR00=\'{Nets.SourceR00}\'
			WHERE 1=1 AND A.ococtIndexID={Nets.ID} ';
		
		$tmpData0=$tmpData_User;
		$tmpData_User=array();
		foreach ($tmpData0 as $Row0){
			$tmpFlag=0;
			foreach ($tmpData_Contact as $Row1){
				if ($Row0['uitID']==$Row1['ocotUserid']) {
					$tmpFlag=1;
					
					$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL_A);
					$tmpSTR=str_replace('{Nets.Name}',$Row0['uitNickname'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.ID}',$Row1['ocotID'],$tmpSTR);
					$tmpINT=$this->Execute($tmpSTR);
					if($tmpINT!=0){
						$this->sysLastError='新增联系人失败:更新联系人状态时出错，请稍后再试';
						$this->TransRollback();
						return $tmpINT;
					}
					
					$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL_B);
					$tmpSTR=str_replace('{Nets.CoName}',$Row0['uictCo_Name'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoEmail}',$Row0['uitEmail'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoTele}',$Row0['uictCo_Tele'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoMobile}',$Row0['uictCo_Mobile'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoFax}',$Row0['uictCo_Fax'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoSoftware}',$Row0['uictCo_Software'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoWebsite}',$Row0['uictCo_Website'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoNumCode}',$Row0['uictCo_NumCode'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoAddress}',$Row0['uictCo_Address'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoCompany}',$Row0['uictCo_Company'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoPosition}',$Row0['uictCo_PersPosition'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.CoSpical}',$Row0['uictCo_PersSpical'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.SourceR00}',$Row0['uictSourceR00'],$tmpSTR);
					$tmpSTR=str_replace('{Nets.ID}',$Row1['ocotID'],$tmpSTR);
					$tmpINT=$this->Execute($tmpSTR);
					if($tmpINT!=0){
						$this->sysLastError='新增联系人失败:更新联系人状态时出错，请稍后再试';
						$this->TransRollback();
						return $tmpINT;
					}

				}
			}
			if ($tmpFlag==0) {
				$tmpData_User[]=$Row0;
			}
		}
		//-- ------ #Add contact ------ --
		foreach ($tmpData_User as $Row0) {
			//-- #Numcode --
			$tmpSTR='ocotNumCode';
			$pDescriptions[0]['TableDescription']['SearchColumn']=$tmpSTR;
			$pDescriptions[0]['Columns'][$tmpSTR]['Value']=$this->sysFileConfig['NumCodeHead_Normal'].date('YmdHis');
			$tmpINT=$this->numCode($pDescriptions[0]);
			
			$pDescriptions[0]['Columns']['ocotName']['Value']=$Row0['uitNickname'];
			$pDescriptions[0]['Columns']['ocotRecycle']['Value']=0;
			$pDescriptions[0]['Columns']['ocotCreateBy']['Value']=$tmpData_My[0]['uitID'];
			$pDescriptions[0]['Columns']['ocotCreateTime']['Value']=date($this->sysTimeFormat);
			$pDescriptions[0]['Columns']['ocotUserid']['Value']=$Row0['uitID'];

			$pDescriptions[1]['Columns']['ococtCoName']['Value']=$Row0['uictCo_Name'];
			$pDescriptions[1]['Columns']['ococtCoEmail']['Value']=$Row0['uitEmail'];
			$pDescriptions[1]['Columns']['ococtCoTele']['Value']=$Row0['uictCo_Tele'];
			$pDescriptions[1]['Columns']['ococtCoMobile']['Value']=$Row0['uictCo_Mobile'];
			$pDescriptions[1]['Columns']['ococtCoFax']['Value']=$Row0['uictCo_Fax'];
			$pDescriptions[1]['Columns']['ococtCoSoftware']['Value']=$Row0['uictCo_Software'];
			$pDescriptions[1]['Columns']['ococtCoWebsite']['Value']=$Row0['uictCo_Website'];
			$pDescriptions[1]['Columns']['ococtCoNumCode']['Value']=$Row0['uictCo_NumCode'];
			$pDescriptions[1]['Columns']['ococtCoAddress']['Value']=$Row0['uictCo_Address'];
			$pDescriptions[1]['Columns']['ococtCoCompany']['Value']=$Row0['uictCo_Company'];
			$pDescriptions[1]['Columns']['ococtCoDepartment']['Value']=$Row0['uitID'];
			$pDescriptions[1]['Columns']['ococtCoPosition']['Value']=$Row0['uictCo_PersPosition'];
			$pDescriptions[1]['Columns']['ococtCoSpical']['Value']=$Row0['uictCo_PersSpical'];
			$pDescriptions[1]['Columns']['ococtSourceR00']['Value']=$Row0['uictSourceR00'];
			
			
			$pDescriptions[0]['TableDescription']['SearchColumn']='ocotNumCode,ocotID';
			$pDescriptions[1]['TableDescription']['SearchColumn']='ococtIndexID';

			$tmpINT=$this->add($pDescriptions);
			if($tmpINT!=0){
				$this->sysLastError='新增联系人失败:新增联系人时出错，请稍后再试';
				$this->TransRollback();
				return $tmpINT;
			}
			
		}
		
		$this->TransCommit();
		return 0;
	}
	public function CancelContacts($pDescriptions,$pUserids){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$this->TransStart();
		//-- ------ #Check MyUserid ------ --
		$tmpSQL=' SELECT A.uitID,A.uitName,A.uitNickname,A.uitEmail
		
			FROM {Nets.TableHead}netsuserinfo AS A
			WHERE 1=1 AND A.uitRecycle=0 AND A.uitChecked>1 AND A.uitID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['ocotCreateBy']['Value'],$tmpSTR);
		$tmpData_My=$this->Search($tmpSTR);
		if (count($tmpData_My)==0) {
			$this->sysLastError='新增联系人失败:登陆后才能增加联系人';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//-- ------ #Check Userids ------ --
		$tmpSQL=' SELECT A.uitID,A.uitName,A.uitEmail,A.uitNickname
		
			FROM {Nets.TableHead}netsuserinfo AS A
			WHERE 1=1 AND A.uitRecycle=0 AND A.uitChecked>1 AND (1=2 {Nets.IDS}) ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR_0='';
		foreach ($pUserids as $Row0){
			$tmpSTR_0.=' OR A.uitID='.$tmpCode->Int32($Row0,0).' ';
		}
		$tmpSTR=str_replace('{Nets.IDS}',$tmpSTR_0,$tmpSTR);
		$tmpData_User=$this->Search($tmpSTR);
		if (count($tmpData_User)==0) {
			$this->sysLastError='新增联系人失败:请选择至少一个联系人';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//-- ------ #Update exist contact ------ --
		//-- #GET --
		$tmpSQL=' SELECT A.ocotID,A.ocotName,A.ocotCreateBy,A.ocotUserid,A.ocotRecycle
			FROM {Nets.TableHead}oacontact AS A
			WHERE 1=1 AND A.ocotRecycle=0 AND
			A.ocotCreateBy={Nets.CreateBy} AND (1=2 {Nets.IDS}) ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.CreateBy}',$pDescriptions[0]['Columns']['ocotCreateBy']['Value'],$tmpSTR);
		$tmpSTR_0='';
		foreach ($pUserids as $Row0){
			$tmpSTR_0.=' OR A.ocotUserid='.$tmpCode->Int32($Row0,0).' ';
		}
		$tmpSTR=str_replace('{Nets.IDS}',$tmpSTR_0,$tmpSTR);
		$tmpData_Contact=$this->Search($tmpSTR);
		
		//-- #Filter and update --
		$tmpSQL_A=' UPDATE {Nets.TableHead}oacontact AS A
			SET A.ocotRecycle=1
			WHERE 1=1 AND A.ocotID={Nets.ID} ';
		
		$tmpData0=$tmpData_User;
		$tmpData_User=array();
		foreach ($tmpData0 as $Row0){
			foreach ($tmpData_Contact as $Row1){
				if ($Row0['uitID']==$Row1['ocotUserid']) {
					$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL_A);
					$tmpSTR=str_replace('{Nets.ID}',$Row1['ocotID'],$tmpSTR);
					$tmpINT=$this->Execute($tmpSTR);
					if($tmpINT!=0){
						$this->sysLastError='新增联系人失败:更新联系人状态时出错，请稍后再试';
						$this->TransRollback();
						return $tmpINT;
					}
					
				}
			}
		}
		
		$this->TransCommit();
		return 0;
	}
	public function GetContactToArray($pUserid,&$pAR){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		//-- ------ #Search ------ --
		$tmpSQL=' SELECT A.ocotID,A.ocotName,A.ocotUserid
			FROM {Nets.TableHead}oacontact AS A
			WHERE 1=1 AND A.ocotRecycle=0 AND A.ocotCreateBy={Nets.Userid} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.Userid}',$pUserid,$tmpSTR);
		$tmpData0=$this->Search($tmpSTR);
		$tmpAR0=array();
		foreach ($tmpData0 as $Row0){
			$tmpAR0[]=$Row0['ocotUserid'];
		}
		
		$pAR=$tmpAR0;
		return 0;
	}
	public function getContact($pNameAR,$pParidAR,$pUserid,&$pData){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';

		$tmpSQL=' SELECT A.ocotID,A.ocotName,A.ocotNameStyle,A.ocotParid
			FROM {Nets.TableHead}oacontact AS A
			WHERE 1=1 AND (1=2 {Nets.Name} {Nets.Parid})
			ORDER BY A.ocotOrderBy DESC,A.ocotID DESC ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR_0='';
		foreach ($pNameAR as $Cell0){
			$tmpSTR_0.=' OR A.ocotName LIKE \''.$Cell0.'\' ';
		}
		$tmpSTR=str_replace('{Nets.Name}',$tmpSTR_0,$tmpSTR);
		$tmpSTR_0='';
		foreach ($pParidAR as $Cell0){
			$tmpSTR_0.=' OR A.ocotParid='.$Cell0.' ';
		}
		$tmpSTR=str_replace('{Nets.Parid}',$tmpSTR_0,$tmpSTR);
		$pData=$this->Search($tmpSTR);
		
		return 0;
	}
}
?>