<?php
class cmsask extends netsDb {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function cmsask($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='castName';
		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_CmsAsk_'.$pID;
		$tmpAR0=$tmpHtml->AnalyerRequest($tmpCookieName,'',2);
		//-- ------ #Update data ------ --
		//-- #Update Discuss --
		$tmpCountConfigs=array();
		//#Info
		$tmpAR1=$this->sCountUpdateConfig_Template;
		$tmpAR1['Parent_TableName']=$this->sysTableHead.'cmsaskcont';
		$tmpAR1['Parent_CountColumnName']='casctCount_Discuss';
		$tmpAR1['Parent_Condition']=' AND casctID='.$pID;
		$tmpAR1['Child_A_TableName']=$this->sysTableHead.'cmsaskdiscuss';
		$tmpAR1['Child_A_CountColumnName']='casdtID';
		$tmpAR1['Child_A_Condition']=' AND casdtRecycle=0 AND casdtChecked>1 AND casdtParid='.$pID;
		$tmpCountConfigs[]=$tmpAR1;
		//#Group
		$tmpSQL=' SELECT A.castID,A.castParid
			FROM {Nets.TableHead}cmsask AS A 
			WHERE 1=1 AND A.castRecycle=0 AND A.castChecked>1 AND A.castID={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]['castParid'];
		}
		
		$tmpAR1=$this->sCountUpdateConfig_Template;
		$tmpAR1['Parent_TableName']=$this->sysTableHead.'cmsaskgroupcont';
		$tmpAR1['Parent_CountColumnName']='casgctCount_Info';
		$tmpAR1['Parent_Condition']=' AND casgctID='.$tmpParid;
		$tmpAR1['Child_A_TableName']=$this->sysTableHead.'cmsaskgroup';
		$tmpAR1['Child_A_CountColumnName']='castID';
		$tmpAR1['Child_A_Condition']=' AND castRecycle=0 AND castChecked>1 AND castParid='.$tmpParid;
		$tmpCountConfigs[]=$tmpAR1;
		
		$tmpINT=$this->updateCount($tmpCountConfigs);
		//-- #Update Hit --
		if($tmpAR0[0]==''){
			$tmpSQL=' UPDATE {Nets.TableHead}cmsaskcont SET casctCount_Views=casctCount_Views+1 WHERE 1=1 AND casctIndexID={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 add_Question(&$pDbDescriptions){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$this->TransStart();
		//-- ------ #Sub code ------ --
		//Check
		$tmpSQL=' SELECT A.uitID,A.uitName,A.uitCode 
			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}',$pDbDescriptions[0]['Columns']['castCreateBy']['Value'],$tmpSTR);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)<1){
			$tmpData0[0]=array(
				'uitID'=>0,
				'uitCode'=>0,
				);
				
		}
		$tmpUitCode=$pDbDescriptions[0]['Columns']['castCode']['Value']-$this->sysFileConfig['QuestionsInitialCode'];
		if ($tmpUitCode<0) {
			$tmpUitCode=0;
		}
		if($tmpUitCode>$tmpData0[0]['uitCode'] && $tmpData0[0]['uitID']>0){
			$this->sysLastError='对不起，您的积分不足以支付本问题所需积分数。除去系统为您支付的'.$this->sysFileConfig['QuestionsInitialCode'].'积分，您还必须支付'.$tmpUitCode.'积分。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}else if($tmpUitCode>$tmpData0[0]['uitCode'] && $tmpData0[0]['uitID']<=0){
			$this->sysLastError='对不起，只有在登陆后，才能发布悬赏问题。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//Sub
		$tmpSQL=' UPDATE {Nets.TableHead}netsuserinfo 
			SET uitCode=uitCode-{Nets.Code} 
			WHERE 1=1 AND uitID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.Code}',$tmpUitCode,$tmpSTR);
		$tmpSTR=str_replace('{Nets.ID}',$tmpData0[0]['uitID'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			$this->sysLastError='扣除积分时出错，请稍后再试。';
			$this->TransRollback();
			return $tmpINT;
		}
		
		//-- ------ #ADD ------ --
		//Set value
		$pDbDescriptions[0]['Columns']['castChecked']['Value']=2;
		$pDbDescriptions[0]['Columns']['castState']['Value']=0;
		$pDbDescriptions[0]['Columns']['castCreateTime']['Value']=date($this->sysTimeFormat);
		//Add
		$tmpINT=$this->add($pDbDescriptions);
		if($tmpINT!=0){
			$this->sysLastError='提交问题时出错，请稍后再试';
			$this->TransRollback();
			return $tmpINT;
		}
		
		$this->TransCommit();
		return 0;
	}
	public function OnPositive($pID,&$pCount){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$tmpHtml=new netsHtml();
		$this->TransStart();
		//-- ------ #Update ------ --
		//Get cookie
		$tmpCookieName=$this->sysCookieHead.'Cookie_CmsAsk_Vote_'.$pID;
		$tmpAR0=$tmpHtml->AnalyerRequest($tmpCookieName,'',2);
		$tmpCookieValue=$tmpAR0[0];
		if ($tmpCookieValue!='') {
			$this->sysLastError='谢谢，您已经投过票了。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//SQL
		$tmpSQL=' UPDATE {Nets.TableHead}cmsaskcont AS A 
			SET A.casctCode_Positive=A.casctCode_Positive+1
			WHERE 1=1 AND A.casctIndexID={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 $this->sysErrorFuncID.'001';
		}
		//Get count
		$tmpSQL=' SELECT A.casctID,casctCode_Positive
			FROM {Nets.TableHead}cmsaskcont AS A
			WHERE 1=1 AND A.casctIndexID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
		$tmpData0=$this->Search($tmpSTR);
		if (count($tmpData0)<=0) {
			$this->sysLastError='对不起，获取投票数失败，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		$pCount=$tmpData0[0]['casctCode_Positive'];
		//Set cookie
		$this->writeCookie($this->sysNormalCookiePath,$tmpCookieName,1);
		
		$this->TransCommit();
		return 0;
	}
	public function OnNegative($pID,&$pCount){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$tmpHtml=new netsHtml();
		$this->TransStart();
		//-- ------ #Update ------ --
		//Get cookie
		$tmpCookieName=$this->sysCookieHead.'Cookie_CmsAsk_Vote_'.$pID;
		$tmpAR0=$tmpHtml->AnalyerRequest($tmpCookieName,'',2);
		$tmpCookieValue=$tmpAR0[0];
		if ($tmpCookieValue!='') {
			$this->sysLastError='谢谢，您已经投过票了。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//SQL
		$tmpSQL=' UPDATE {Nets.TableHead}cmsaskcont AS A 
			SET A.casctCode_Negative=A.casctCode_Negative+1
			WHERE 1=1 AND A.casctIndexID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
		$tmpINT=$this->sDb->Execute($tmpSTR);
		if ($tmpINT!=0) {
			$this->sysLastError='对不起，投票失败，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//Get count
		$tmpSQL=' SELECT A.casctID,casctCode_Negative
			FROM {Nets.TableHead}cmsaskcont AS A
			WHERE 1=1 AND A.casctIndexID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
		$tmpData0=$this->sDb->Search($tmpSTR);
		if (count($tmpData0)<=0) {
			$this->sysLastError='对不起，获取投票数失败，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		$pCount=$tmpData0[0]['casctCode_Negative'];
		//Set cookie
		$this->writeCookie($this->sysNormalCookiePath,$tmpCookieName,1);
		
		$this->TransCommit();
		return 0;
	}
	public function OnBest($pID,$pDID){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$tmpHtml=new netsHtml();
		$this->TransStart();
		//-- ------ #Update ------ --
		//-- ------ #Discuss ------ --
		$tmpSQL=' UPDATE {Nets.TableHead}cmsaskdiscuss AS A 
			SET A.casdtBest=1
			WHERE 1=1 AND A.casdtID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDID,$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if ($tmpINT!=0) {
			$this->sysLastError='对不起，更新评论状态失败，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//-- ------ #Userinfo ------ --
		//#Get ask
		$tmpSQL=' SELECT A.castID,A.castName,A.castState,A.castCode
			FROM {Nets.TableHead}cmsask AS A
			WHERE 1=1 AND A.castRecycle=0 AND A.castChecked>1 AND A.castID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
		$tmpData_Ask=$this->Search($tmpSTR);
		if (count($tmpData_Ask)<=0) {
			$this->sysLastError='对不起，您设置的问题应该不存在，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		if ($tmpData_Ask[0]['castState']>0) {
			$this->sysLastError='对不起，此问题已经过期。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//#Get askdiscuss
		$tmpSQL=' SELECT A.casdtID,A.casdtName,A.casdtCreateBy
			FROM {Nets.TableHead}cmsaskdiscuss AS A
			WHERE 1=1 AND A.casdtRecycle=0 AND A.casdtChecked>1 AND A.casdtID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDID,$tmpSTR);
		$tmpData_Discuss=$this->Search($tmpSTR);
		if (count($tmpData_Discuss)<=0) {
			$this->sysLastError='对不起，您设置的问题应该不存在，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//#Update
		$tmpSQL=' UPDATE {Nets.TableHead}netsuserinfo AS A
			SET A.uitCode=A.uitCode+{Nets.Code}
			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.Code}',$tmpData_Ask[0]['castCode'],$tmpSTR);
		$tmpSTR=str_replace('{Nets.ID}',$tmpData_Discuss[0]['casdtCreateBy'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if ($tmpINT!=0) {
			$this->sysLastError='对不起，更新用户信息失败，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//-- ------ #Ask ------ --
		$tmpSQL=' UPDATE {Nets.TableHead}cmsask AS A
			SET A.castState={Nets.State}
			WHERE 1=1 AND A.castID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.State}',1,$tmpSTR);
		$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if ($tmpINT!=0) {
			$this->sysLastError='对不起，更新问题状态失败，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		
		$this->TransCommit();
		return 0;
	}
	public function OnPositive_Discuss($pID,$pDID,&$pCount){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$tmpHtml=new netsHtml();
		$this->TransStart();
		//-- ------ #Update ------ --
		//Get cookie
		$tmpCookieName=$this->sysCookieHead.'Cookie_CmsAsk_Vote_'.$pID.'_'.$pDID;
		$tmpAR0=$tmpHtml->AnalyerRequest($tmpCookieName,'',2);
		$tmpCookieValue=$tmpAR0[0];
		if ($tmpCookieValue!='') {
			$this->sysLastError='谢谢，您已经投过票了。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//SQL
		$tmpSQL=' UPDATE {Nets.TableHead}cmsaskdiscusscont AS A 
			SET A.casdctCode_BePositive=A.casdctCode_BePositive+1
			WHERE 1=1 AND A.casdctIndexID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDID,$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if ($tmpINT!=0) {
			$this->sysLastError='对不起，投票失败，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//Get count
		$tmpSQL=' SELECT A.casdctID,casdctCode_BePositive
			FROM {Nets.TableHead}cmsaskdiscusscont AS A
			WHERE 1=1 AND A.casdctIndexID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDID,$tmpSTR);
		$tmpData0=$this->Search($tmpSTR);
		if (count($tmpData0)<=0) {
			$this->sysLastError='对不起，获取投票数失败，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		$pCount=$tmpData0[0]['casdctCode_BePositive'];
		//Set cookie
		$this->writeCookie($this->sysNormalCookiePath,$tmpCookieName,1);
		
		$this->TransCommit();
		return 0;
	}
	public function OnNegative_Discuss($pID,$pDID,&$pCount){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$tmpHtml=new netsHtml();
		$this->TransStart();
		//-- ------ #Update ------ --
		//Get cookie
		$tmpCookieName=$this->sysCookieHead.'Cookie_CmsAsk_Vote_'.$pID.'_'.$pDID;
		$tmpAR0=$tmpHtml->AnalyerRequest($tmpCookieName,'',2);
		$tmpCookieValue=$tmpAR0[0];
		if ($tmpCookieValue!='') {
			$this->sysLastError='谢谢，您已经投过票了。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//SQL
		$tmpSQL=' UPDATE {Nets.TableHead}cmsaskdiscusscont AS A 
			SET A.casdctCode_BeNegative=A.casdctCode_BeNegative+1
			WHERE 1=1 AND A.casdctIndexID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDID,$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if ($tmpINT!=0) {
			$this->sysLastError='对不起，投票失败，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//Get count
		$tmpSQL=' SELECT A.casdctID,casdctCode_BeNegative
			FROM {Nets.TableHead}cmsaskdiscusscont AS A
			WHERE 1=1 AND A.casdctIndexID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDID,$tmpSTR);
		$tmpData0=$this->Search($tmpSTR);
		if (count($tmpData0)<=0) {
			$this->sysLastError='对不起，获取投票数失败，请与客服人员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		$pCount=$tmpData0[0]['casdctCode_BeNegative'];
		//Set cookie
		$this->writeCookie($this->sysNormalCookiePath,$tmpCookieName,1);
		
		$this->TransCommit();
		return 0;
	}
	
}
?>