<?php
class lovesControl extends skymvc
{
	public $userInfo;
	public function __construct(){
		parent::__construct();
		$this->loadModel(array("loves","login","hates","feeds"));
		//$this->login->checklogin(1);
		$this->userInfo=$this->login->getUser();
	}
	
	public function onDefault(){
		
	}
	
	//喜欢产品
	public function  onProduct(){
		$this->loadModel(array("product"));
		if($this->userInfo){
			$userid=$this->userInfo['userid'];
			$id=intval(get('id'));
			if(!$id) exit(json_encode(array("error"=>1,"message"=>"参数出错")));
			$this->feeds->query("DELETE FROM ".table('feeds')." WHERE userid='$userid' AND object_id='$id' AND type_id=".PRODUCT_TYPE_ID."   ");
			if(!$this->loves->selectRow(array("where"=>"userid=".$this->userInfo['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' ");
				$data=array(
					"userid"=>$this->userInfo['userid'],
					"object_id"=>$id,
					"dateline"=>time(),
					"type_id"=>PRODUCT_TYPE_ID
				);
				$data['id']=$this->loves->setTable("loves_10")->insert($data);								
				$this->loves->setTable("loves",$this->userInfo['userid'],10)->insert($data);
				//处理讨厌
				if($this->hates->selectRow(array("where"=>"userid='$userid' AND object_id='$id' AND type_id=".PRODUCT_TYPE_ID." "))){
					$this->hates->delete(" 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' ");
				} 
				exit(json_encode(array("error"=>0,"message"=>"喜欢成功")));
			}
			
			exit(json_encode(array("error"=>1,"message"=>"你已经喜欢过了")));
		}else{
			exit(json_encode(array("error"=>2,"message"=>"请先登录")));
		}
	}
	
	//喜欢杂志
	public function onMagazine(){
		$this->loadModel(array("magazine"));
		if($this->userInfo){
			$userid=$this->userInfo;
			$id=intval(get('id'));
			if(!$this->loves->selectRow(array("where"=>"userid='".$this->userInfo['userid']." AND object_id='$id' AND type_id=".MAGAZINE_TYPE_ID." ")))
			{
				$this->magazine->query("UPDATE ".table('magazine')." SET fav_num=fav_num+1,grade=grade+1 WHERE id='$id' ");
				
				$data=array(
					"userid"=>$this->userInfo['userid'],
					"object_id"=>$id,
					"dateline"=>time(),
					"type_id"=>MAGAZINE_TYPE_ID
				);
				$data['id']=$this->loves->setTable("loves_10")->insert($data);								
				$this->loves->setTable("loves",$this->userInfo['userid'],10)->insert($data);
				
				exit(json_encode(array("error"=>0,"message"=>"关注成功")));
			}
			
			exit(json_encode(array("error"=>1,"message"=>"你已经关注过了")));
		}else{
			exit(json_encode(array("error"=>2,"message"=>"请先登录")));
		}
	}
	
	public function onShop(){
		$this->loadModel(array("shop"));
		if($this->userInfo){
			$userid=$this->userInfo;
			$id=get_post('shopid','i');
			if(!$id) exit(json_encode(array("error"=>3,"message"=>"参数出错")));
			$row=$this->loves->selectRow(array("where"=>"userid=".$this->userInfo['userid']." AND object_id='$id' AND type_id=".SHOP_TYPE_ID." "));
			 
			switch(get('op')){
				
				case 'unlove':
						if($row){
							$this->shop->query("UPDATE ".table('shop')." SET fav_num=fav_num-1,grade=grade-1 WHERE shopid='$id' ");
							$this->loves->delete("id=".$row['id']);
							 
						}
						exit(json_encode(array("error"=>0,"message"=>"取消关注成功x")));
					break;
				default:
						if(!$row)
						{
							$this->shop->query("UPDATE ".table('shop')." SET fav_num=fav_num+1,grade=grade+1 WHERE shopid='$id' ");
							
							$data=array(
								"userid"=>$this->userInfo['userid'],
								"object_id"=>$id,
								"dateline"=>time(),
								"type_id"=>SHOP_TYPE_ID
							);
							$data['id']=$this->loves->setTable("loves_10")->insert($data);								
							$this->loves->setTable("loves",$this->userInfo['userid'],10)->insert($data);
							
							exit(json_encode(array("error"=>0,"message"=>"关注成功")));
						}
						
						exit(json_encode(array("error"=>1,"message"=>"你已经关注过了")));
					break;
				
				
			}
			
		}else{
			exit(json_encode(array("error"=>2,"message"=>"请先登录")));
		}
	}
	
	
	 
	
}
?>