<?php
class shopcodesale extends netsDb {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function shopcodesale($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='shcstName';
		if(strlen($pTableDescriptAR[0]['Columns'][$tmpSTR]['Value'])<1 && !in_array($tmpSTR,$pExceptionAR2)){
			$this->sysLastError='提交内容错误:1.名称不能为空';
			return $this->sysErrorFuncID.'001';
		}
		
		return 0;
	}
	
	public function initialObject($pID){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$tmpHtml=new netsHtml();
		$this->TransStart();
		//-- ------ #Check cookie exist ------ --
		//Get cookie
		$tmpCookieName=$this->sysCookieHead.'Cookie_ShopCodeSale_'.$pID;
		$tmpAR0=$tmpHtml->AnalyerRequest($tmpCookieName,'',2);
		//-- ------ #Update data ------ --
		//-- #Update Discuss --
		$tmpCountConfigs=array();
		//#Info
		$tmpAR1=$this->sCountUpdateConfig_Template;
		$tmpAR1['Parent_TableName']=$this->sysTableHead.'shopcodesalecont';
		$tmpAR1['Parent_CountColumnName']='shcsctCount_Discuss';
		$tmpAR1['Parent_Condition']=' AND shcsctID='.$pID;
		$tmpAR1['Child_A_TableName']=$this->sysTableHead.'cmsaskdiscuss';
		$tmpAR1['Child_A_CountColumnName']='shcsdtID';
		$tmpAR1['Child_A_Condition']=' AND shcsdtRecycle=0 AND shcsdtChecked>1 AND shcsdtParid='.$pID;
		$tmpCountConfigs[]=$tmpAR1;
		//#Group
		$tmpSQL=' SELECT A.shcstID,A.shcstParid
			FROM {Nets.TableHead}shopcodesale AS A 
			WHERE 1=1 AND A.shcstRecycle=0 AND A.shcstChecked>1 AND A.shcstID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
		$tmpData0=$this->Search($tmpSTR);
		$tmpParid=0;
		if (count($tmpData0)>0) {
			$tmpParid=$tmpData0[0]['shcstParid'];
		}
		
		$tmpAR1=$this->sCountUpdateConfig_Template;
		$tmpAR1['Parent_TableName']=$this->sysTableHead.'shopcodesalegroupcont';
		$tmpAR1['Parent_CountColumnName']='shcsgctCount_Info';
		$tmpAR1['Parent_Condition']=' AND shcsgctID='.$tmpParid;
		$tmpAR1['Child_A_TableName']=$this->sysTableHead.'shopcodesale';
		$tmpAR1['Child_A_CountColumnName']='shcstID';
		$tmpAR1['Child_A_Condition']=' AND shcstRecycle=0 AND shcstChecked>1 AND shcstParid='.$tmpParid;
		$tmpCountConfigs[]=$tmpAR1;
		
		$tmpINT=$this->updateCount($tmpCountConfigs);
		//-- #Update Hit --
		if($tmpAR0[0]==''){
			$tmpSQL=' UPDATE {Nets.TableHead}cmsaskcont SET shcsctCount_Views=shcsctCount_Views+1 WHERE 1=1 AND shcsctIndexID={Nets.ID} ';
			$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
			$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
			$tmpINT=$this->Execute($tmpSTR);
			if($tmpINT!=0){
				$this->sysLastError='更新积分商品数据时出错，请稍后再试。';
				$this->TransRollback();
				return $tmpINT;
			}
			$this->writeCookie($this->sysNormalCookiePath,$tmpCookieName,1);
		}
		
		$this->TransCommit();
		return 0;
	}
	public function MakeOrder(&$pDbDescriptions){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$tmpHtml=new netsHtml();
		$this->TransStart();
		
		$tmpTotleCode=0;
		//-- ------ #Check shopcodesale ------ --
		$tmpAR0=explode($this->sysSPLIT2,$pDbDescriptions[1]['Columns']['shcsoctSales']['Value']);
		foreach ($tmpAR0 as $Row0){
			$tmpAR1=explode($this->sysSPLIT1,$Row0);
			if (count($tmpAR1)==2) {
				$tmpSQL=' SELECT A.shcstID,A.shcstName,A.shcstPrice,A.shcstCount
					FROM {Nets.TableHead}shopcodesale AS A
					INNER JOIN {Nets.TableHead}shopcodesalecont AS B ON B.shcsctIndexID=A.shcstID
					WHERE 1=1 AND A.shcstRecycle=0 AND A.shcstChecked>1 AND A.shcstID={Nets.ID} ';
				$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
				$tmpSTR=str_replace('{Nets.ID}',$tmpAR1[0],$tmpSTR);
				$tmpData_Codesale=$this->Search($tmpSTR);
				if (count($tmpData_Codesale)<=0) {
					$this->sysLastError='对不起，您兑换的商品不存在，请与客服人员联系。';
					$this->TransRollback();
					return $this->sysErrorFuncID.'001';
				}
				if ($tmpAR1[1]>$tmpData_Codesale[0]['shcstCount']){
					$this->sysLastError='对不起，您兑换的商品库存数量不足，请与客服人员联系。';
					$this->TransRollback();
					return $this->sysErrorFuncID.'001';
				}
				//Shopcodesale Count
				$tmpCount=$tmpData_Codesale[0]['shcstCount']-$tmpAR1[1];
				$tmpSQL=' UPDATE {Nets.TableHead}shopcodesale 
					SET A.shcstCount={Nets.Count}
					WHERE 1=1 AND A.shcstID={Nets.ID} ';
				$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
				$tmpSTR=str_replace('{Nets.Count}',$tmpCount,$tmpSTR);
				$tmpSTR=str_replace('{Nets.ID}',$tmpAR1[0],$tmpSTR);
				$tmpINT=$this->Execute($tmpSTR);
				if($tmpINT!=0){
					$this->sysLastError='更新积分商品信息时出错，请稍后再试。';
					$this->TransRollback();
					return $tmpINT;
				}
				
				$tmpTotleCode+=$tmpAR1[1]*$tmpData_Codesale[0]['shcstPrice'];
			}
		}
		$pDbDescriptions[0]['Columns']['shcsotPayyet']['Value']=$tmpTotleCode;
		$pDbDescriptions[0]['Columns']['shcsotTotalPrice']['Value']=$tmpTotleCode;
		//-- ------ #Userpay ------ --
		$tmpSQL=' SELECT A.uitID,A.uitName,A.uitCode
			FROM {Nets.TableHead}netsuserinfo AS A
			INNER JOIN {Nets.TableHead}netsuserinfocont 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}',$pDbDescriptions[0]['Columns']['shcsotCreateBy']['Value'],$tmpSTR);
		$tmpData_Userinfo=$this->Search($tmpSTR);
		if (count($tmpData_Userinfo)<=0) {
			$this->sysLastError='对不起，请登陆后才能兑换积分商品。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		if ($tmpTotleCode>$tmpData_Userinfo[0]['uitCode']) {
			$this->sysLastError='对不起，您的积分不足。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		
		$tmpUserCode=$tmpData_Userinfo[0]['uitCode']-$tmpTotleCode;
		$tmpSQL=' UPDATE {Nets.TableHead}netsuserinfo AS A
			SET A.uitCode={Nets.Code}
			WHERE 1=1 AND A.uitID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.Code}',$tmpUserCode,$tmpSTR);
		$tmpSTR=str_replace('{Nets.ID}',$pDbDescriptions[0]['Columns']['shcsotCreateBy']['Value'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			$this->sysLastError='更新用户信息时出错，请稍后再试。';
			$this->TransRollback();
			return $tmpINT;
		}
		//-- ------ #Make order ------ --
		//-- #ADD --
		//#Get numCode
		$tmpSTR='shcsotNumCode';
		$tmpDbDescriptions[0]['TableDescription']['SearchColumn']=$tmpSTR;
		$tmpDbDescriptions[0]['Columns'][$tmpSTR]['Value']='N_'.date('YmdHis');
		$this->numCode($tmpDbDescriptions[0]);
		//Set value
		$pDbDescriptions[0]['Columns']['shcsotName']['Value']=$pDbDescriptions[0]['Columns']['shcsotNumCode']['Value'];
		$pDbDescriptions[0]['Columns']['shcsotState']['Value']=2;
		$pDbDescriptions[0]['Columns']['shcsotCreateTime']['Value']=date($this->sysTimeFormat);
		//#Add
		$tmpINT=$this->Check($tmpDbDescriptions,array(),array());
		if($tmpINT!=0){
			$this->sysLastError='生成订单信息出错，请稍后再试。';
			$this->TransRollback();
			return $tmpINT;
		}
		$tmpDbDescriptions[$Coun0]['TableDescription']['SearchColumn']='shcsotNumCode,shcsotID';
		$tmpDbDescriptions[$Coun0]['TableDescription']['SearchColumn']='shcsoctIndexID';
		//Add
		$tmpINT=$this->add($pDbDescriptions);
		if($tmpINT!=0){
			$this->sysLastError='提交问题时出错，请稍后再试';
			$this->TransRollback();
			return $tmpINT;
		}
		
		echo ($this->sysSQL);
		exit();
		$this->TransCommit();
		return 0;
	}
	
}
?>