<?php
/**
* @method 收藏店铺
* @author 卢
* @copyright 2017/7/6
*/
class B2cCollShop extends B2cBase
{
	/**
	 * @method    获取店铺列表
	 * @param     [int]          $userId  [用户ID]
	 * @param     [string]          $where   [查询条件(' and shc_uid=1 and shop_status=0')]
	 * @param     [string]          $order   [排序方式]
	 * @param     [int]          $nowPage [当前页数]
	 * @param     [int]         $perPage [单页条数]
	 * @author 卢
	 * @copyright 2017-07-06
	 * @return array([status] => 1,[data] => Array([0] => Array([shc_shop_id] => 1 [shc_uid] => 149894[product] => Array([0] => Array( [pro_id] => 100031[pro_status] => 0)))
	 */
	function getUserCollShopList($userId,$where,$order,$nowPage,$perPage=2,$type='all')
	{
		if(LibFc::Int($userId)&&LibFc::Int($perPage)&&LibFc::Int($nowPage))
		{
			$table = 'us_shop_collection us left join sh_shop sh on shc_shop_id=shop_id';
			$total = $this->Get($table, ['count(*) count'],$where,true);
			$where .= sprintf(' order by %s limit %d,%d',$order, ($nowPage-1)*$perPage, $perPage);
			$data=$this->Get($table,['*'], $where);
			$data['data']=array_column($data['data'], null,'shc_shop_id');
			$shopArr=array_column($data['data'], 'shc_shop_id');
			if(count($shopArr))
			{
				$numArrData=$this->Get('us_shop_collection',['count(*) count','shc_shop_id'],sprintf(' and shc_shop_id in (%s) group by shc_shop_id',implode(',',$shopArr)));
				$numArr=array_column($numArrData['data'], null,'shc_shop_id');
				$limitArrData=$this->get('bc_product',['pro_id','pro_shop_id'],sprintf(' and pro_shop_id in (%s) and pro_status=1 and pro_count>0 order by pro_add_time desc limit 500',implode(',',$shopArr)));
				$limitArr=[];
				foreach ($limitArrData['data'] as $k => $v) 
				{
					$limitArr[$v['pro_shop_id']][]=$v['pro_id'];
				}
				foreach ($limitArr as $k => $v) 
				{
					$limitArr[$k]=array_slice($v, 0,20);
				}
				foreach ($data['data'] as $k => $v) 
				{
					if($v['shop_status']==1)
					{
						$where3=sprintf('and pro_shop_id =%d and pro_status=1 and pro_count>0',$k);
						if($type=='all')
						{
							$where3.=count($limitArr[$k])?sprintf(' and pro_id in (%s)',implode(',', $limitArr[$k])):'';
						}
						$productData=$this->getProductByShopId($where3,'pro_add_time desc',1,4);
						$data['data'][$k]['count']=$numArr[$k]['count'];
						$data['data'][$k]['product']=$productData['data'];
					}
				}
				$data['data']['totalAll']=$total['data']['count'];		
			}
			return $data;
		}
		return LibFc::ReturnData(false,'参数不正确');
	}
	/**
	 * @method    根据条件获取产品
	 * @param     [int]             $shopId  [店铺ID]
	 * @param     [string]             $where   [条件]
	 * @param     [string]             $order   [排序条件]
	 * @param     [int]             $nowPage [当前页]
	 * @param     [int]            $perPage [单页数]
	 * @author 卢
	 * @copyright 2017-07-07
	 * @return  array([status] => 1[data]=>array([0]=>array([pro_id] => 100031,....,pp_pro_id] => 100031)))
	 */
	function getProductByShopId($where,$order,$nowPage,$perPage=4)
	{
		if(LibFc::Int($nowPage)&&LibFc::Int($perPage))
		{
			$B2cProduct=new B2cProduct();
			$where .= sprintf(' order by %s limit %d,%d', $order,($nowPage-1)*$perPage, $perPage);
			$data=$this->Get('bc_product',['*'], $where);
			$proIdArr=array_column($data['data'], 'pro_id');
			if(count($proIdArr))
			{
				$picData=$B2cProduct->getProductPic($proIdArr);
			}
			foreach ($data['data'] as $k => $v) 
			{
				$data['data'][$k]=isset($picData['data'][$v['pro_id']][0])?array_merge($data['data'][$k],$picData['data'][$v['pro_id']][0]):$data['data'][$k];
			}
			//计算产品数量
			$totalData=$this->Get('bc_product',['count(*) count'],$where,true);
			$data['data']['totalCount']=$totalData['data']['count'];
			$data['data']['totalPage']=ceil($totalData['data']['count']/$perPage);
			// print_r($data['data']);
			return $data;	
		}
		return LibFc::ReturnData(false,'参数不正确');
	}
	/**
	 * @method    删除某用户的收藏店铺
	 * @param     [array]         $shopId [店铺ID array([0]=>1,[1]=>2)]
	 * @param     [int]         $userId [用户ID]
	 * @author 卢
	 * @copyright 2017-07-07
	 * @return array([status=>true,data=>1])
	 */
	function delUserCollShop($userId,$shopId)
	{
		if(LibFc::Int($userId)&&is_array($shopId))
		{
			$shopId=array_filter($shopId,'LibFc::Int');
			$shopIds=implode(',', $shopId);
			$where=sprintf(' and shc_shop_id in (%s) and shc_uid=%d',$shopIds,$userId);
			$res=$this->Del('us_shop_collection',$where);
			return LibFc::ReturnData(true,$res['data']);
		}
		return LibFc::ReturnData(false,'参数不正确');
	}
	/**
	 * @method    获取新品/热销数量
	 * @param     [int]        $shopId [description]
	 * @author 卢
	 * @copyright 2017-07-08
	 * @return array('newCount'=>10,'hotCount'=>20)
	 */
	function getProductCount($shopId)
	{
		if(!empty($shopId))
		{
			$shopArr=is_array($shopId)&&count($shopId)?$shopId:[(int)$shopId];
			$shopArr=array_filter($shopArr,'LibFc::Int');
			//新品数量
			$weekTime=time()-(7*24*60*60);
			$table='bc_product';
			$where=sprintf(' and pro_shop_id in (%s) and pro_set_time>=%d group by pro_shop_id',implode(',',$shopArr),$weekTime);
			$newData=$this->Get($table,['count(*) count','pro_shop_id'],$where);
			// print_r($newData);
			$newArr=array_column($newData['data'], null,'pro_shop_id');
			//热销
			$where=sprintf(" and pro_host='Y' and pro_shop_id in (%s) group by pro_shop_id",implode(',',$shopArr));
			$hotData=$this->Get($table,['count(*) count','pro_shop_id'],$where);
			$hotArr=array_column($hotData['data'], null,'pro_shop_id');
			$data=[];
			foreach ($shopArr as $v) 
			{
				$data[$v][]=array('newCount'=>$newArr[$v]['count'],'hotCount'=>$hotArr[$v]['count']);
			}
			// $data=array('newCount'=>$newData['data']['count'],'hotCount'=>$hotData['data']['count']);
			return LibFc::ReturnData(true,$data);		
		}
		return LibFc::ReturnData(false,'参数不正确');
	}
	/**
	 * @method    添加收藏店铺
	 * @param     [int]          $userId  [用户ID]
	 * @param     [int||array]          $shopArr [店铺ID]
	 * @author 卢
	 * @copyright 2017-07-10
	 * @return  array([0]=>1,[1]=>2) [已经存在的店铺ID]
	 */
	function addUserCollShop($userId,$shopArr)
	{
		if(LibFc::Int($userId) && !empty($shopArr))
		{

			$shopArr = is_array($shopArr)? $shopArr: [(int) $shopArr];
			foreach($shopArr as $k=>$v)
			{
				$shopArr[$k] = (int) $v;
			}
			$res =$this->getFieldUniVal('us_shop_collection', 'shc_shop_id', sprintf(' and shc_uid=%d and shc_shop_id in (%s)',$userId, implode(',', $shopArr)));
			$existId = $res['data'];
			$addArr =array_diff($shopArr, $existId);
			if(!empty($addArr))
			{
				$this->B2CDb->Begin();
				foreach ($addArr as $k => $v) 
				{
					$targetArr=array('shc_shop_id'=>$v,'shc_uid'=>$userId,'shc_add_time'=>time());
					$res=$this->Add('us_shop_collection',$targetArr);
				}
				$this->B2CDb->End();
			}
			return LibFc::ReturnData(true,$existId);
		}
		return LibFc::ReturnData(false,'参数不正确');
	}
}