<?php
class shopsalecar extends netsDb {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function shopsalecar($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='shsctName';
		if(strlen($pTableDescriptAR[0]['Columns'][$tmpSTR]['Value'])<1 && !in_array($tmpSTR,$pExceptionAR2)){
			$this->sysLastError='提交内容错误:1.名称不能为空';
			return $this->sysErrorFuncID.'001';
		}
		
		return 0;
	}
	
	public function addCar(&$pDbDescriptions,$pUserinfo){
		$this->sysErrorFuncID=$this->sysErrorClassID.'03';
		//-- ------ #Get sale ------ --
		if($pDbDescriptions[0]['Columns']['shsctParid']['Value']==0){
			$this->sysLastError='您所要购买的商品并不存在。';
			return $this->sysErrorFuncID.'001';
		}
		//Get sale
		$tmpSQL=' SELECT
			A.shstID,A.shstName,A.shstPrice
			FROM '.$this->sysTableHead.'shopsale AS A
			INNER JOIN '.$this->sysTableHead.'shopsalecont AS B ON B.shsctIndexID=A.shstID
			WHERE 1=1 AND A.shstRecycle=0 AND A.shstChecked>1
			AND A.shstID='.$pDbDescriptions[0]['Columns']['shsctParid']['Value'].' ';
		$tmpData0=$this->Search($tmpSQL);
		if(count($tmpData0)==0){
			$this->sysLastError='您所要购买的商品并不存在。';
			return $this->sysErrorFuncID.'002';
		}
		//Select salecar
		$tmpSQL=' SELECT 
			A.shsctID,A.shsctParid,A.shsctState,A.shsctCount,A.shsctTotalPrice
			FROM '.$this->sysTableHead.'shopsalecar AS A
			WHERE 1=1 AND A.shsctParid='.$pDbDescriptions[0]['Columns']['shsctParid']['Value'].'
			AND A.shsctCreateBy='.$pUserinfo[0]['Columns']['uitID']['Value'].' ';
		$tmpData1=$this->Search($tmpSQL);
		if(count($tmpData1)>0){
			if($tmpData1[0]['shsctState']==0){
				$tmpSQL=' UPDATE '.$this->sysTableHead.'shopsalecar 
					SET shsctCount=shsctCount+1,shsctTotalPrice=shsctTotalPrice+'.$tmpData0[0]['shstPrice'].' 
					WHERE 1=1 AND shsctID='.$tmpData1[0]['shsctID'].' ';
				$tmpINT=$this->Execute($tmpSQL);
			}else {
				$tmpSQL=' UPDATE '.$this->sysTableHead.'shopsalecar 
					SET shsctState=0,shsctCount=1,shsctTotalPrice='.$tmpData0[0]['shstPrice'].' 
					WHERE 1=1 AND shsctID='.$tmpData1[0]['shsctID'].' ';
				$tmpINT=$this->Execute($tmpSQL);
			}
			return 0;
		}
		//-- ------ #Add ------ --
		//#Get numCode
		$tmpSTR='shsctNumCode';
		$pDbDescriptions[0]['TableDescription']['SearchColumn']=$tmpSTR;
		$pDbDescriptions[0]['Columns'][$tmpSTR]['Value']='N_'.date('YmdHis');
		$this->numCode($pDbDescriptions[0]);
		
		$tmpSTR='shsctName';
		$pDbDescriptions[0]['Columns'][$tmpSTR]['Value']=$tmpData0[0]['shstName'];
		$tmpSTR='shsctParid';
		$pDbDescriptions[0]['Columns'][$tmpSTR]['Value']=$tmpData0[0]['shstID'];
		$tmpSTR='shsctState';
		$pDbDescriptions[0]['Columns'][$tmpSTR]['Value']=0;
		$tmpSTR='shsctCreateBy';
		$pDbDescriptions[0]['Columns'][$tmpSTR]['Value']=$pUserinfo[0]['Columns']['uitID']['Value'];
		$tmpSTR='shsctCreateTime';
		$pDbDescriptions[0]['Columns'][$tmpSTR]['Value']=date($this->sysTimeFormat);
		$tmpSTR='shsctCount';
		$pDbDescriptions[0]['Columns'][$tmpSTR]['Value']=1;
		$tmpSTR='shsctTotalPrice';
		$pDbDescriptions[0]['Columns'][$tmpSTR]['Value']=$tmpData0[0]['shstPrice'];
		
		//#Add
		$tmpINT=$this->Check($pDbDescriptions,array(),array());
		if($tmpINT!=0){
			return $tmpINT;
		}
		$pDbDescriptions[0]['TableDescription']['SearchColumn']='shsctNumCode,shsctID';
		$pDbDescriptions[1]['TableDescription']['SearchColumn']='shscctIndexID';
		$tmpINT=$this->add($pDbDescriptions);
		if($tmpINT!=0){
			return $tmpINT;
		}
		
		return 0;
	}
}
?>