<?php
/**
	* @method 用户商城浏览历史
	* @author 许
	* @copyright 2017/7/5
	*/
class B2cHistory extends B2cBase
{
	/**
	* @method 查询浏览历史
	* @param  $partId 分类ID int
	* @param  $PerPage 每页条数 int
	* @param  $NowPage 当前页码 int
	* @param  $userId 用户ID int
	* @author xu
	* @copyright 2017/07/05
	* @return  array('status'=>true,'data'=>'','code'=>)/array('status'=>true,'data'=>array(),'code'=>)
	*/
	public function getProHistory($headPartIds,$PerPage,$NowPage,$userId,$partId) 
	{	
		//参数判断
		if(!LibFc::Int($PerPage)||!LibFc::Int($NowPage)||!LibFc::Int($userId)) return LibFc::ReturnData(false,'参数错误！',0);

		$headPartIds = array_filter($headPartIds,'LibFc::Int');

		$table='us_product_history left join bc_product on prh_pro_id=pro_id left join sh_shop on shop_id=pro_shop_id';
		$where = sprintf("and prh_uid=%d and shop_status=1 and shop_uid!=%d ",$userId,$userId);

		// 分类参数
		if(LibFc::Int($partId)) $where.=sprintf(' and pro_part_id=%d ',$partId);
		if($partId==='other') $where.=empty($headPartIds)?'':sprintf(" and pro_part_id not in (%s) ",implode(',', $headPartIds));
		$where .= ' order by prh_ltime desc';

		// 分页
		$res = $this->get($table, ['*'], $where.sprintf(" limit %d, %d",($NowPage-1)*$PerPage, $PerPage));
		$data = $res['data'];
		if(empty($data)) return LibFc::ReturnData(true,$data);

		// 图片路由
		$B2cProduct = new B2cProduct();
		$res = $B2cProduct->getProductPic(array_column($data,'pro_id'));
		$proPic = $res['data'];
		$data = array_map(function($v)use($proPic){return empty($proPic[$v['pro_id']])?$v:array_merge($v,current($proPic[$v['pro_id']]));},$data);

		//按日期分组
		$res = array();
		foreach ($data as $value)
		{
			$res[date("Ymd",$value['prh_ltime'])][] = $value;
		}
		return LibFc::ReturnData(true,$res);
	}

	/**
		* @method 删除浏览历史
		* @param  $uid 用户ID int
		* @param  $proIdArr 商品ID array (1,2,3...)
		* @author xu
		* @copyright 2017/07/05
		* @return 成功:array('status'=>true,'data'=>'success','code'=>0);失败:array('status'=>false,'data'=>'error','code'=>0)
	*/
	public function delUserProHistory($uid, $proIdArr) 
	{	
		//过滤
		if(!is_array($proIdArr)||!LibFc::Int($uid)||empty($proIdArr)) return LibFc::ReturnData(false,'参数错误！');
		$proIdArr = array_filter($proIdArr,'LibFc::Int');
		if(empty($proIdArr)) return LibFc::ReturnData(false,'参数错误！');
		// 删除
		$this->del('us_product_history',sprintf(" and prh_uid=%d and prh_pro_id in (%s)",$uid,implode(',', $proIdArr)));
		return LibFc::ReturnData(true,true);
	}


	/**
		* @method 删除一整天的浏览历史
		* @param  $uid 用户ID int
		* @param  $time 日期
		* @param  $partId 分类ID
		* @author xu
		* @copyright 2018/03/24
		* @return 成功:array('status'=>true,'data'=>'success','code'=>0);失败:array('status'=>false,'data'=>'error','code'=>0)
	*/
	public function delDayProHistory($uid,$time,$partId=0) 
	{	
		//过滤
		if(empty($uid)||!LibFc::Int($uid)||empty($time)||!strtotime($time)) return LibFc::ReturnData(false,'参数错误！');
		// 所有proId
		$time = strtotime($time);
		$table = "us_product_history left join bc_product on pro_id=prh_pro_id";
		$where = sprintf(" and prh_uid=%d and prh_ltime between %d and %d", $uid, $time,$time+24*60*60);
		if(LibFc::Int($partId)) $where.=sprintf(" and pro_part_id=%d ",$partId);
		if($partId==='other')
		{
			$where = sprintf("and prh_uid=%d group by pro_part_id order by num desc limit 5",$uid);
			$res = $this->get($table, ['count(*) num,pro_part_id'], $where);
			$res = array_column($res['data'],'pro_part_id');
			if(!empty($res)) $where.= sprintf(" and pro_part_id not in (%s) ",implode(',', array_filter($res)));
		}
		$res = $this->get($table,['prh_pro_id'],$where);
		$prhProId = array_column($res['data'],'prh_pro_id');
		
		if(empty($prhProId)) return LibFc::ReturnData(true,true);
		// 删除
		$this->del('us_product_history',sprintf(" and prh_uid=%d and prh_pro_id in (%s)",$uid,implode(',', $prhProId)));
		
		return LibFc::ReturnData(true,true);
	}




	/**
		* @method 添加浏览历史
		* @param  $uid 用户ID int
		* @param  $proId (商品ID)
		* @author xu
		* @copyright 2017/07/10
		* @return 成功:array('status'=>true,'data'=>'success','code'=>0);失败:array('status'=>false,'data'=>'error','code'=>0)
	*/
	public function addUserProHistory($uid, $proId,$shopId=0) 
	{	
		if(!LibFc::Int($uid)||!LibFc::Int($proId)) return LibFc::ReturnData(true,true);

		$B2cProduct = new B2cProduct();
		$res = $B2cProduct->getProduct(['*'], sprintf(" and pro_id = %d", $proId), true);
		$res = $res['data'];
		// print_r($res);
		if( !empty($shopId) && ($res['pro_shop_id']==$shopId) ) return LibFc::ReturnData(true,true);
		// 售价
		$price = (($res['pro_promotion_btime']<time()) && ($res['pro_promotion_etime']>time()) && !empty($res['pro_promotion_price']))? $res['pro_promotion_price'] : $res['pro_sale_price'];
		// 入库
		$temp = ['prh_price' => $price,'prh_uid' => $uid,'prh_ltime' => time(),'prh_pro_id' => $proId];
		$this->B2CDb->Begin();
		$this->del('us_product_history',sprintf(" and prh_uid=%d and prh_pro_id = %d",$uid,$proId));
		$this->add('us_product_history',$temp);
		$this->B2CDb->End();

		return LibFc::ReturnData(true,true);
	}
	/**
	 * @method    获取猜你喜欢列表
	 * @param     [int]         $uid     [用户id]
	 * @param     [int]         $perPage [每页数量]
	 * @param     [int]         $nowPage [当前页]
	 * @param     [str]         $where   [条件]
	 * @param     [array]       $FieldArr[查询字段]
	 * @param     [bool]        $rand    [是否随机,仅限第一页]
	 * @author     廖
	 * @copyright 2017-11-01
	 * @return    array('status'=>true,'data'=>[0=>['proid'=>xxx,'proName'=>xxx]],'code'=>0)
	 */
	public function getGuessLovely($uid,$perPage,$nowPage,$where='',$FieldArr=[],$rand=false)
	{
		if(LibFc::Int($uid) && LibFc::Int($perPage) && LibFc::Int($nowPage))
		{
			$lastTime = time()-604800;
			//获取分类的id
			$partData = $this->get('us_product_history right join bc_product on prh_pro_id=pro_id right join sh_shop on pro_shop_id=shop_id',['pro_part_id'],sprintf(" and prh_uid=%d and prh_ltime>%d and pro_status>0 and pro_count>0 and shop_status=1 order by prh_ltime desc",$uid,$lastTime));
			$partArr = array_unique(array_column($partData['data'], 'pro_part_id'));
			$partWhere = !empty($partArr)?sprintf(' and pro_part_id in (%s)',implode(',',$partArr)):"";
			//检测全部的数量
			$order = $rand?' order by pro_promoted desc, rand()':' order by pro_promoted desc,pro_id desc';
			$res = $this->get('bc_product left join sh_shop on pro_shop_id=shop_id',['pro_id'],$partWhere.$where.$order.' limit 100');
			$allYProId = array_column($res['data'], 'pro_id');
			$selectData = ['pro_id','pro_name','pro_sale_price','pro_promotion_price','pro_promotion_btime','pro_promotion_etime'];
			$selectData = array_merge($selectData,$FieldArr);
			//计算偏移量
			$start = ($nowPage-1)*$perPage+1;
			$end = ($nowPage-1)*$perPage+$perPage;
			$partWhere = !empty($partArr)?sprintf(' and pro_part_id not in (%s)',implode(',',$partArr)):"";
			//已取出的配件
			$proWhere = !empty($allYProId)?sprintf(' and pro_id not in (%s) ',implode(',',$allYProId)):"";
			if(count($allYProId)>($start-1))
			{
				if($rand) shuffle($allYProId);
				$sproIdArr = array_slice($allYProId,$start-1,$perPage,true);
				$data = $this->get('bc_product left join sh_shop on pro_shop_id=shop_id',$selectData,sprintf('and pro_id in (%s) %s',implode(',',$sproIdArr),$where.$order));
				$data = $data['data'];
				$count = $perPage-count($data);
				//如果不够
				if($count>0)
				{
					$twhere = $partWhere.$proWhere.$where.$order.sprintf(" limit %d", $count);
					$Ndata = $this->get('bc_product RIGHT join sh_shop on pro_shop_id=shop_id', $selectData, $twhere);
					$data = array_merge($data,$Ndata['data']);
				}
			}else
			{
				$twhere = $partWhere.$proWhere.$where.$order.sprintf(" limit %d,%d",($nowPage-1)*$perPage, $perPage);
				$Ndata = $this->get('bc_product RIGHT join sh_shop on pro_shop_id=shop_id', $selectData, $limit);
				$data = $Ndata['data'];
			}
			$sproIdArr = array_column($data, 'pro_id');
			//获取配件图片		
			if(!empty($sproIdArr))
			{
				$B2cProduct = new B2cProduct();
				$res = $B2cProduct->getProductPic($sproIdArr);
				foreach ($data as $key => $value) 
				{
					$data[$key] = isset($res['data'][$value['pro_id']][0])?array_merge($data[$key],$res['data'][$value['pro_id']][0]):$data[$key];
				}
			}
			return LibFc::ReturnData(true,$data);
		}
		return LibFc::ReturnData(false,'参数不正确');
	}	


	/**
	 * @method    获取相似宝贝列表
	 * @param     [int] $partId [配件分类]
	 * @author     xu 
	 * @copyright 2017-11-13
	 * @return    array('status'=>true,'data'=>[0=>['proid'=>xxx,'proName'=>xxx]],'code'=>0)
	 */
	public function showThisLikeGodWrap($partId,$proId,$perPage,$nowPage,$where='')
	{
		// 参数
		if(!LibFc::Int($partId) || !LibFc::Int($perPage) || !LibFc::Int($nowPage) || !LibFc::Int($proId))
			return LibFc::ReturnData(false,'参数不正确');
		$select = ['pro_id','pro_name','pro_sale_price','pro_sold_count','pro_promotion_price','pro_promotion_btime','pro_promotion_etime'];
		$where = sprintf(' and pro_status>0 and pro_part_id =%d and pro_count>0 and pro_id != %d and shop_status=1 order by pro_sold_count desc limit %d,%d',$partId,$proId, ($nowPage-1)*$perPage, $perPage);
		$res = $this->get('bc_product left join sh_shop on pro_shop_id=shop_id', $select, $where);
		if(empty($res['data']))
			return LibFc::ReturnData(true,[]);
		$data = $res['data'];
		$proId = array_column($data, 'pro_id');

		//主图
		$B2cProduct = new B2cProduct();
		$res = $B2cProduct->getProductPic($proId);
		foreach ($data as $key => $value) 
		{
			$data[$key] =isset($res['data'][$value['pro_id']][0])? array_merge($data[$key],$res['data'][$value['pro_id']][0]):$data[$key];
		}
		return LibFc::ReturnData(true,$data);	
	}
}