<?php
class hatesControl extends skymvc
{
	public $userInfo;
	public function __construct(){
		parent::__construct();
		$this->loadModel(array("loves","login","hates","feeds"));
		$this->login->checklogin();
		$this->userInfo=$this->login->getUser();
	}
	
	public function onDefault(){
		
	}
	
	//喜欢
	public function  onProduct(){
		$this->loadModel(array("product"));
		if($_SESSION['ssuser']['userid']){
			$userid=$_SESSION['ssuser']['userid'];
			$id=intval(get('id'));
			$this->feeds->query("DELETE FROM ".table('feeds')." WHERE userid='$userid' AND object_id='$id' AND type_id=".PRODUCT_TYPE_ID."   ");
			if(!$this->hates->selectRow(array("where"=>"userid='$userid' AND object_id='$id' AND type_id=".PRODUCT_TYPE_ID." ")))
			{
				$this->product->query("UPDATE ".table('product')." SET hates=hates+1,grade=grade-1 WHERE id='$id' ");
				$data=array(
					"userid"=>$this->userInfo['userid'],
					"object_id"=>$id,
					"dateline"=>time(),
					"type_id"=>PRODUCT_TYPE_ID
				);
				$data['id']=$this->hates->setTable("hates_10")->insert($data);								
				$this->hates->setTable("hates",$this->userInfo['userid'],10)->insert($data);
				//处理喜欢
				if($this->loves->selectRow(array("where"=>"userid='$userid' AND object_id='$id' AND type_id=".PRODUCT_TYPE_ID." "))){
					$this->loves->delete(" userid='$userid' AND object_id='$id' AND type_id=".PRODUCT_TYPE_ID."  ");
					$this->product->query("UPDATE ".table('product')." SET loves=loves-1,grade=grade-1 WHERE id='$id' ");
				} 
				exit(json_encode(array("error"=>0,"message"=>"讨厌成功")));
			}
			
			exit(json_encode(array("error"=>1,"message"=>"你已经讨厌过了")));
		}else{
			exit(json_encode(array("error"=>2,"message"=>"请先登录")));
		}
	}
	
	
	 
	
}
?>