<?php
class netsuserunit extends netsDb {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function netsuserunit($pConnection){
		$this->netsDb($pConnection);
	}
	public function delete_Clear($pDescriptions,$pIds){
		$this->sysErrorFuncID=$this->sysErrorClassID.'01';
		//-- ------ #Self ------ --
		$tmpINT=parent::delete($pDescriptions,$pIds);
		if($tmpINT!=0){
			return $tmpINT;
		}
		//-- ------ #Department ------ --
		$tmpSTR='';
		$tmpAR0=array('udeptUnitid','netsuserdepartment','udept','udepct',);
		$tmpINT=$this->deleteChild($pDescriptions,$tmpAR0,$pIds,$tmpSTR);
		if($tmpINT!=0){
			$this->sysLastError='部门删除错误，请与管理员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//-- ------ #User ------ --
		$tmpNextIds='';
		$tmpAR0=array('uitUnitid','netsuserinfo','uit','uict',);
		$tmpINT=$this->deleteChild($pDescriptions,$tmpAR0,$pIds,$tmpNextIds);
		if($tmpINT!=0){
			$this->sysLastError='用户删除错误，请与管理员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//-- ------ #Delete Unit info group ------ --
		$tmpIDS=$tmpNextIds;
		$tmpAR0=array(
			array('ddugtCreateBy','downdownusergroup','ddugt','ddugct',),
			array('shsugtCreateBy','shopsaleusergroup','shsugt','shsugct',),
			array('cdugtCreateBy','cmsdocusergroup','cdugt','ddugct',),
			);
		foreach ($tmpAR0 as $Cell){
			$tmpSTR='';
			$tmpINT=$this->deleteChild($pDescriptions,$Cell,$tmpIDS,$tmpSTR,' AND '.$Cell[2].'PublicType=1 ');
			if($tmpINT!=0){
				$this->sysLastError='用户文档分类及用户私有信息删除错误，请与管理员联系。';
				$this->TransRollback();
				return $this->sysErrorFuncID.'001';
			}
		}
		$tmpIDS=$tmpNextIds;
		$tmpAR0=array(
			array('cftCreateBy','cmsfriend','cft','cfct',),
			);
		foreach ($tmpAR0 as $Cell){
			$tmpSTR='';
			$tmpINT=$this->deleteChild($pDescriptions,$Cell,$tmpIDS,$tmpSTR,' AND '.$Cell[2].'IsUPublic=2 ');
			if($tmpINT!=0){
				$this->sysLastError='用户文档分类及用户私有信息删除错误，请与管理员联系。';
				$this->TransRollback();
				return $this->sysErrorFuncID.'001';
			}
		}
		//-- ------ #Delete User info group ------ --
		$tmpIDS=$tmpNextIds;
		$tmpAR0=array(
			array('ddugtCreateBy','downdownusergroup','ddugt','ddugct',),
			array('shsugtCreateBy','shopsaleusergroup','shsugt','shsugct',),
			array('cdugtCreateBy','cmsdocusergroup','cdugt','ddugct',),
			);
		foreach ($tmpAR0 as $Cell){
			$tmpSTR='';
			$tmpINT=$this->deleteChild($pDescriptions,$Cell,$tmpIDS,$tmpSTR,' AND '.$Cell[2].'PublicType=0 ');
			if($tmpINT!=0){
				$this->sysLastError='用户文档分类及用户私有信息删除错误，请与管理员联系。';
				$this->TransRollback();
				return $this->sysErrorFuncID.'001';
			}
		}
		$tmpIDS=$tmpNextIds;
		$tmpAR0=array(
			array('cftCreateBy','cmsfriend','cft','cfct',),
			);
		foreach ($tmpAR0 as $Cell){
			$tmpSTR='';
			$tmpINT=$this->deleteChild($pDescriptions,$Cell,$tmpIDS,$tmpSTR,' AND '.$Cell[2].'IsUPublic=1 ');
			if($tmpINT!=0){
				$this->sysLastError='用户文档分类及用户私有信息删除错误，请与管理员联系。';
				$this->TransRollback();
				return $this->sysErrorFuncID.'001';
			}
		}
		
		return 0;
	}
	public function Check(&$pTableDescriptAR,$pExceptionAR1,$pExceptionAR2){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';

		if(count($pExceptionAR1)==0){
			$pExceptionAR1=array();
		}
		 parent::Check($pTableDescriptAR,$pExceptionAR1,$pExceptionAR2);
		//
		$tmpSTR='uutName';
		if(strlen($pTableDescriptAR[0]['Columns'][$tmpSTR]['Value'])<1 && !in_array($tmpSTR,$pExceptionAR2)){
			$this->sysLastError='提交内容错误:1.名称不能为空';
			return $this->sysErrorFuncID.'001';
		}
		
		return 0;
	}
	
	/**
	 * 
	 */
	public function getConfirm($pDescriptions,&$pData){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		//-- ------ # ------ --
		//
		$tmpSQL=' SELECT 
			A.uutID,A.uutName,
			B.uuctComName,B.uuctComIdCode,B.uuctComIdCodeSource,B.uuctComIdCodeChecked,
			B.uuctComContact,B.uuctComContactTele,B.uuctComContactTeleChecked,
			B.uuctComContactEmail,B.uuctEmailChecked,
			B.uuctPayBankCode,B.uuctPayBankName,B.uuctPayBankUName,B.uuctPayBankAddress,B.uuctPayBankCountry,B.uuctPayBankCountryCode,B.uuctPayBankChecked
			FROM '.$this->sysTableHead.'netsuserunit AS A
			INNER JOIN '.$this->sysTableHead.'netsuserunitcont AS B ON B.uuctIndexID=A.uutID
			INNER JOIN '.$this->sysTableHead.'netsuserinfo AS C ON C.uitUnitid=A.uutID
			WHERE 1=1 AND C.uitID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['uutCreateBy']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		//Recheck states
		if($tmpData0[0]['uuctEmailChecked']==0 && strlen($tmpData0[0]['uuctComContactEmail'])>4){
			$tmpData0[0]['uuctEmailChecked']=1;
		}elseif (strlen($tmpData0[0]['uuctComContactEmail'])<=4){
			$tmpData0[0]['uuctEmailChecked']=0;
		}
		if($tmpData0[0]['uuctComContactTeleChecked']==0 && 
			(strlen($tmpData0[0]['uuctComContact'])>1 && strlen($tmpData0[0]['uuctComContactTele'])>4)){
			$tmpData0[0]['uuctComContactTeleChecked']=1;
		}elseif (strlen($tmpData0[0]['uuctComContact'])<=1 || strlen($tmpData0[0]['uuctComContactTele'])<=4){
			$tmpData0[0]['uuctComContactTeleChecked']=0;
		}
		if($tmpData0[0]['uuctPayBankChecked']==0 &&
			(strlen($tmpData0[0]['uuctPayBankCode'])>5 && strlen($tmpData0[0]['uuctPayBankName'])>2 &&
			strlen($tmpData0[0]['uuctPayBankUName'])>2 && strlen($tmpData0[0]['uuctPayBankAddress'])>3)){
			$tmpData0[0]['uuctPayBankChecked']=1;
		}elseif (strlen($tmpData0[0]['uuctPayBankCode'])<=5 || strlen($tmpData0[0]['uuctPayBankName'])<2 ||
			strlen($tmpData0[0]['uuctPayBankUName'])<2 || strlen($tmpData0[0]['uuctPayBankAddress'])<=3){
			$tmpData0[0]['uuctPayBankChecked']=0;
		}
		if($tmpData0[0]['uuctComIdCodeChecked']==0 &&
			(strlen($tmpData0[0]['uuctComName'])>5 && strlen($tmpData0[0]['uuctComIdCode'])>3 &&
			strlen($tmpData0[0]['uuctComIdCodeSource'])>5)){
			$tmpData0[0]['uuctComIdCodeChecked']=1;
		}elseif (strlen($tmpData0[0]['uuctComName'])<=5 || strlen($tmpData0[0]['uuctComIdCode'])<=3 ||
			strlen($tmpData0[0]['uuctComIdCodeSource'])<=5){
			$tmpData0[0]['uuctComIdCodeChecked']=0;
		}
		$tmpSQL=' UPDATE netsuserunitcont SET 
			uuctEmailChecked={Nets.Checked_Email},
			uuctComContactTeleChecked={Nets.Checked_Mobile},
			uuctPayBankChecked={Nets.Checked_PayBank},
			uuctComIdCodeChecked={Nets.Checked_Postcode}
			WHERE uuctIndexID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.Checked_Email}',$tmpData0[0]['uuctEmailChecked'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.Checked_Mobile}',$tmpData0[0]['uuctComContactTeleChecked'],$tmpSTR);
		$tmpSTR=str_replace('{Nets.Checked_PayBank}',$tmpData0[0]['uuctPayBankChecked'],$tmpSTR);
		$tmpSTR=str_replace('{Nets.Checked_Postcode}',$tmpData0[0]['uuctComIdCodeChecked'],$tmpSTR);
		$tmpSTR=str_replace('{Nets.ID}',$tmpData0[0]['uutID'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			return $tmpINT;
		}
		
		$pData=$tmpData0;
		return 0;
	}
	public function setConfirm($pDescriptions,$pType){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$this->TransStart();

		$tmpDescriptions=$pDescriptions;
		//-- ------ #Get ------ --
		$tmpSQL=' SELECT * FROM '.$this->sysTableHead.'netsuserunit AS A
			INNER JOIN '.$this->sysTableHead.'netsuserunitcont AS B ON B.uuctIndexID=A.uutID
			INNER JOIN '.$this->sysTableHead.'netsuserinfo AS C ON C.uitUnitid=A.uutID
			WHERE 1=1 AND C.uitID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['uutCreateBy']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)==0){
			$this->sysLastError='请登陆后再申请认证。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		$tmpINT=$this->getDescriptionFromData($tmpDescriptions,$tmpData0);
		//-- ------ #Checked ------ --
		if($pType=='uuctEmailChecked'){
			//uuctEmailChecked
			$tmpDescriptions[1]['Columns']['uuctComContactEmail']['Value']=$pDescriptions[1]['Columns']['uuctComContactEmail']['Value'];
			if($tmpDescriptions[1]['Columns']['uuctEmailChecked']['Value']==0 && 
				(strlen($pDescriptions[1]['Columns']['uuctComContactEmail']['Value'])>4)){
				$tmpDescriptions[1]['Columns']['uuctEmailChecked']['Value']=1;
			}elseif ($tmpDescriptions[1]['Columns']['uuctEmailChecked']['Value']==2 && 
				($tmpDescriptions[1]['Columns']['uuctComContactEmail']['Value']!=$pDescriptions[1]['Columns']['uuctComContactEmail']['Value'])){
				$tmpDescriptions[1]['Columns']['uuctEmailChecked']['Value']=1;
			}
			if(strlen($pDescriptions[1]['Columns']['uuctComContactEmail']['Value'])<=4){
				$tmpDescriptions[1]['Columns']['uuctEmailChecked']['Value']=0;
			}
			if($tmpDescriptions[1]['Columns']['uuctEmailChecked']['Value']==1){
				//Send mail
				$tmpCode=new netsCode();
				$tmpSTR='ActionType=UnitConfirmEmail_callback&UnitConfirmEmail_callback='.$tmpCode->SamplePxd(
					$tmpData0[0]['uitID'].$this->sysSPLIT1.
					$tmpDescriptions[0]['Columns']['uutID']['Value'].$this->sysSPLIT1.
					$tmpDescriptions[0]['Columns']['uutName']['Value'].$this->sysSPLIT1.
					$tmpData0[0]['uitPxd'].$this->sysSPLIT1.
					$tmpDescriptions[1]['Columns']['uuctComContactEmail']['Value'].$this->sysSPLIT1.
					$tmpDescriptions[1]['Columns']['uuctEmailChecked']['Value']).$this->sysSPLIT1.
					$tmpDescriptions[0]['Columns']['uutID']['Value'];
				$tmpContent=str_replace('{Nets.Charset}',$this->sysCharset,$this->sysFileConfig['EmailConfirmTemplate']);
				$tmpContent=str_replace('{Nets.Name}',$tmpData0[0]['uitName'].'['.$tmpDescriptions[0]['Columns']['uutName']['Value'].']',$tmpContent);
				$tmpContent=str_replace('{Nets.Encryption}',$tmpSTR,$tmpContent);
				$this->sysLastMessage=$tmpContent;
			}
		}elseif ($pType=='uuctEmailChecked_callback'){
			//uuctEmailChecked_callback
			$tmpDescriptions[1]['Columns']['uuctEmailChecked']['Value']=2;
		}elseif ($pType=='uuctComContactTeleChecked'){
			//uuctComContactTeleChecked
			$tmpDescriptions[1]['Columns']['uuctComContact']['Value']=$pDescriptions[1]['Columns']['uuctComContact']['Value'];
			$tmpDescriptions[1]['Columns']['uuctComContactTele']['Value']=$pDescriptions[1]['Columns']['uuctComContactTele']['Value'];
			if($tmpDescriptions[1]['Columns']['uuctComContactTeleChecked']['Value']==0 && 
				(strlen($pDescriptions[1]['Columns']['uuctComContact']['Value'])>1 && strlen($pDescriptions[1]['Columns']['uuctComContactTele']['Value'])>4)){
				$tmpDescriptions[1]['Columns']['uuctComContactTeleChecked']['Value']=1;
			}elseif ($tmpDescriptions[1]['Columns']['uuctComContactTeleChecked']['Value']==2 &&
				($tmpDescriptions[1]['Columns']['uuctComContact']['Value']!=$pDescriptions[1]['Columns']['uuctComContact']['Value'] ||
				$tmpDescriptions[1]['Columns']['uuctComContactTele']['Value']!=$pDescriptions[1]['Columns']['uuctComContactTele']['Value'])){
				//$tmpDescriptions[1]['Columns']['uuctComContactTeleChecked']['Value']=1;
			}
			if(strlen($pDescriptions[1]['Columns']['uuctComContact']['Value'])<=1 || strlen($pDescriptions[1]['Columns']['uuctComContactTele']['Value'])<=4){
				$tmpDescriptions[1]['Columns']['uuctComContactTeleChecked']['Value']=0;
			}
		}elseif ($pType=='uuctComContactTeleChecked_callback'){
			//uuctComContactTeleChecked_callback
			$tmpDescriptions[1]['Columns']['uuctComContactTeleChecked']['Value']=2;
		}elseif ($pType=='uuctPayBankChecked'){
			//uuctPayBankChecked
			$tmpDescriptions[1]['Columns']['uuctPayBankCode']['Value']=$pDescriptions[1]['Columns']['uuctPayBankCode']['Value'];
			$tmpDescriptions[1]['Columns']['uuctPayBankName']['Value']=$pDescriptions[1]['Columns']['uuctPayBankName']['Value'];
			$tmpDescriptions[1]['Columns']['uuctPayBankUName']['Value']=$pDescriptions[1]['Columns']['uuctPayBankUName']['Value'];
			$tmpDescriptions[1]['Columns']['uuctPayBankAddress']['Value']=$pDescriptions[1]['Columns']['uuctPayBankAddress']['Value'];
			if($tmpDescriptions[1]['Columns']['uuctPayBankChecked']['Value']==0 && 
				(strlen($pDescriptions[1]['Columns']['uuctPayBankCode']['Value'])>5 && strlen($pDescriptions[1]['Columns']['uuctPayBankName']['Value'])>2 &&
				strlen($pDescriptions[1]['Columns']['uuctPayBankUName']['Value'])>2 && strlen($pDescriptions[1]['Columns']['uuctPayBankAddress']['Value'])>3)){
				$tmpDescriptions[1]['Columns']['uuctPayBankChecked']['Value']=1;
				$tmpSTR='0.'.rand(0,40);
				$tmpDescriptions[1]['Columns']['uuctPayBankCheckedNum']['Value']=$tmpSTR;
				$tmpDescriptions[1]['Columns']['uuctPayBankCheckedTime']['Value']=date($this->sysTimeFormat);
			}elseif ($tmpDescriptions[1]['Columns']['uuctPayBankChecked']['Value']==2 && 
				($tmpDescriptions[1]['Columns']['uuctPayBankCode']['Value']!=$pDescriptions[1]['Columns']['uuctPayBankCode']['Value'] || 
				$tmpDescriptions[1]['Columns']['uictPayBankName']['Value']!=$pDescriptions[1]['Columns']['uuctPayBankName']['Value'] || 
				$tmpDescriptions[1]['Columns']['uuctPayBankUName']['Value']!=$pDescriptions[1]['Columns']['uuctPayBankUName']['Value'] || 
				$tmpDescriptions[1]['Columns']['uictPayBankAddress']['Value']!=$pDescriptions[1]['Columns']['uuctPayBankAddress']['Value'])){
				$tmpDescriptions[1]['Columns']['uuctPayBankChecked']['Value']=1;
				$tmpSTR='0.'.rand(0,40);
				$tmpDescriptions[1]['Columns']['uuctPayBankCheckedNum']['Value']=$tmpSTR;
				$tmpDescriptions[1]['Columns']['uuctPayBankCheckedTime']['Value']=date($this->sysTimeFormat);
			}
			if (strlen($tmpDescriptions[1]['Columns']['uuctPayBankCode']['Value'])<=5 || strlen($tmpDescriptions[1]['Columns']['uuctPayBankName']['Value'])<=1 ||
				strlen($tmpDescriptions[1]['Columns']['uuctPayBankUName']['Value'])<=1 || strlen($tmpDescriptions[1]['Columns']['uuctPayBankAddress']['Value'])<=3){
				$tmpDescriptions[1]['Columns']['uuctPayBankChecked']['Value']=0;
				$tmpDescriptions[1]['Columns']['uuctPayBankCheckedNum']['Value']=0.00;
				$tmpDescriptions[1]['Columns']['uuctPayBankCheckedTime']['Value']=date($this->sysTimeFormat);
			}
		}elseif ($pType=='uuctPayBankChecked_callback'){
			//uuctPayBankChecked_callback
			if($tmpDescriptions[1]['Columns']['uuctPayBankCheckedNum']['Value']==$pDescriptions[1]['Columns']['uuctPayBankCheckedNum']['Value']){
				$tmpDescriptions[1]['Columns']['uuctPayBankChecked']['Value']=2;
			}else{
				$this->sysLastError='您填写的打款金额错误。';
				$this->TransRollback();
				return $this->sysErrorFuncID.'001';
			}
		}elseif ($pType=='uuctComIdCodeChecked'){
			//uuctComIdCodeChecked
			$tmpDescriptions[1]['Columns']['uuctComName']['Value']=$pDescriptions[1]['Columns']['uuctComName']['Value'];
			$tmpDescriptions[1]['Columns']['uuctComIdCode']['Value']=$pDescriptions[1]['Columns']['uuctComIdCode']['Value'];
			$tmpDescriptions[1]['Columns']['uuctComIdCodeSource']['Value']=$pDescriptions[1]['Columns']['uuctComIdCodeSource']['Value'];
			if($tmpDescriptions[1]['Columns']['uictPostcodeChecked']['Value']==0 && 
				(strlen($pDescriptions[1]['Columns']['uuctComName']['Value'])>5 && strlen($pDescriptions[1]['Columns']['uuctComName']['Value'])>3 &&
				strlen($pDescriptions[1]['Columns']['uuctComIdCodeSource']['Value'])>5)){
				$tmpDescriptions[1]['Columns']['uuctComIdCodeChecked']['Value']=1;
			}elseif ($tmpDescriptions[1]['Columns']['uuctComIdCodeChecked']['Value']==2 && 
				($tmpDescriptions[1]['Columns']['uuctComName']['Value']!=$pDescriptions[1]['Columns']['uuctComName']['Value'] ||
				$tmpDescriptions[1]['Columns']['uuctComIdCode']['Value']!=$pDescriptions[1]['Columns']['uuctComIdCode']['Value'] ||
				$tmpDescriptions[1]['Columns']['uuctComIdCodeSource']['Value']!=$pDescriptions[1]['Columns']['uuctComIdCodeSource']['Value'])){
				$tmpDescriptions[1]['Columns']['uuctComIdCodeChecked']['Value']=1;
			}
			if (strlen($tmpDescriptions[1]['Columns']['uuctComName']['Value'])<=5 || strlen($tmpDescriptions[1]['Columns']['uuctComIdCode'])<=3 ||
				strlen($tmpDescriptions[1]['Columns']['uuctComIdCodeSource']['Value'])<=5){
				$tmpDescriptions[1]['Columns']['uuctComIdCodeChecked']['Value']=0;
			}
		}elseif ($pType=='uuctComIdCodeChecked_callback'){
			//uuctComIdCodeChecked_callback
			$tmpDescriptions[1]['Columns']['uuctComIdCodeChecked']['Value']=2;
		}
		//-- ------ #Modify ------ --
		$tmpINT=$this->modify($tmpDescriptions);
		if($tmpINT!=0){
			$this->sysLastError='更新用户认证状态错误，请与管理员联系。';
			return $this->sysErrorFuncID.'002';
		}
		
		$this->TransCommit();
		return 0;
	}
}
?>