<?php
/**
 * @method 店铺分类的管理
 * @author 廖
 * @copyright 2017/7/7
 */
class B2cCollPro extends B2cBase
{
	/**
	 * @method    取出收藏商品中失效的商品信息
	 * @param     [string]     $where       查询条件(' and prc_uid=1 and pro_status=1)
	 * @param     [int]        $nowPage     当前的页数
	 * @param     [int]        $perPage     每页获取的数量
	 * @param     [array]      $FieldData   要获取的字段
	 * @return    [array]      [["status"]=> bool(true) ["data"]=> [array['pro_id'=>XXX....]]=> int(0) ]
	 * @author  廖
	 * @copyright 2017-07-07
	 * @return
	 */
	function getUserCollProList($where,$nowPage,$perPage,$FieldData=[])
	{
		if(LibFc::Int($perPage) && LibFc::Int($nowPage))
		{
			//计算偏移量
			$start = +1;
			$end = $nowPage*$perPage;
			$defaultData = ['prc_uid','prc_pro_id','pro_id','pro_shop_id','pro_status','prc_price','pro_name','pro_sale_price','pro_count','pro_promotion_price','pro_promotion_btime','pro_promotion_etime','shop_id','shop_status','shop_domain'];
			$FieldData = array_merge($defaultData,$FieldData);

			$table = 'us_product_collection left join bc_product on prc_pro_id=pro_id left join sh_shop on pro_shop_id=shop_id';
			$where .= sprintf(' limit %d,%d',($nowPage-1)*$perPage, $perPage);
			$data = $this->get($table, $FieldData, $where);
			//配件id arr
			$proIdArr = array_column($data['data'],'pro_id');
			//获取配件的图片
			if(!empty($proIdArr))
			{
				$B2cProduct = new B2cProduct();
				$res = $B2cProduct->getProductPic($proIdArr);
			}
			if(!empty($data['data']))
			{
				foreach ($data['data'] as $k=>$v)
				{
					if(isset($res['data'][$v['pro_id']][0])&&!empty($res['data'][$v['pro_id']][0]))
					{
						$data['data'][$k] = array_merge($data['data'][$k],$res['data'][$v['pro_id']][0]);
					}
				}
			}
			return $data;
		}
		return LibFc::ReturnData(false,'参数不正确');
	}

	/**
	 * @method    删除收藏的商品
	 * @param     [int]          $uid      当前用户的id
	 * @param     [array]        $proIdArr 收藏的产品id(1,2,3...) 的数组
	 * @author    廖
	 * @copyright 2017-07-11
	 * @return    array([status=>true,data=>1])
	 */
	function delUserCollPro($uid,$proIdArr)
	{
		if(LibFc::Int($uid) && !empty($proIdArr))
		{	
			$proIdArr = is_array($proIdArr)? $proIdArr: [(int) $proIdArr];
			$proIdArr = array_filter($proIdArr,'LibFc::Int');
			if(!empty($proIdArr))
			{
				$this->B2CDb->Begin();
				$resCount = $this->get('bc_product',['pro_id','pro_coll_count'],sprintf(' and pro_id in (%s)',implode(',',$proIdArr)));
				$resCount = array_column($resCount['data'],'pro_coll_count','pro_id');
				foreach($proIdArr as $k=>$v)
				{
					$newCount = $resCount[$v]>0?((int)$resCount[$v]-1):0;
					$this->Set('bc_product',['pro_coll_count'=>$newCount],' and pro_id='.$v);
				}
				$where = sprintf(' and prc_pro_id in (%s) and prc_uid=%d',implode(',',$proIdArr),$uid);
				$res = $this->Del('us_product_collection',$where);
				//获取配件收藏总数
				$this->B2CDb->End();
				return LibFc::ReturnData(true,$res['data']);
			}
		}
		return LibFc::ReturnData(false,'参数不正确');
	}
	/**
	 * @method    添加收藏
	 * @param     [int]         $uid      当前用户id
	 * @param     [array]       $proIdArr 收藏产品id
	 * @author     廖
	 * @copyright 2017-07-11
	 * @return  array([0]=>1,[1]=>2) [已经存在的收藏产品ID]
	 */
	function addUserCollPro($uid,$proIdArr)
	{
		if(LibFc::Int($uid) && !empty($proIdArr))
		{
			$proIdArr = is_array($proIdArr)? $proIdArr: [(int) $proIdArr];
			$proIdArr = array_filter($proIdArr,'LibFc::Int');
			if(!empty($proIdArr))
			{
				//查看是不是已经收藏
				$res =$this->getFieldUniVal('us_product_collection', 'prc_pro_id', sprintf(' and prc_uid=%d and prc_pro_id in (%s)',$uid, implode(',', $proIdArr)));
				$existId = $res['data'];
				$addArr =array_diff($proIdArr, $existId);
				if(!empty($addArr))
				{
					$this->B2CDb->Begin();
					$res = $this->get('bc_product',['pro_id','pro_sale_price','pro_coll_count'],sprintf(' and pro_id in (%s)',implode(',',$addArr)));
					$priceArr = array_column($res['data'], 'pro_sale_price','pro_id');
					$countArr = array_column($res['data'], 'pro_coll_count','pro_id');
					foreach ($addArr as $k => $v) 
					{
						$targetArr = array('prc_pro_id'=>$v,'prc_uid'=>$uid,'prc_add_time'=>time(),'prc_price'=>$priceArr[$v]);
						$this->Add('us_product_collection',$targetArr);
						$newCount = $countArr[$v] || $countArr[$v]>0?((int)$countArr[$v]+1):1;
						$this->Set('bc_product',['pro_coll_count'=>$newCount],' and pro_id='.$v);
					}
					$this->B2CDb->End();
				}
				return LibFc::ReturnData(true,$existId);	
			}
		}
		return LibFc::ReturnData(false,'参数不正确');
	}

	/**
	 * @method    取消收藏
	 * @param     [int]         $uid      当前用户id
	 * @param     [array]       $proIdArr 收藏产品id
	 * @author     廖
	 * @copyright 2017-07-11
	 * @return  array([0]=>1,[1]=>2) [已经存在的收藏产品ID]
	 */
	function cancelCollPro($uid,$proIdArr)
	{
		if(LibFc::Int($uid) && !empty($proIdArr))
		{
			$proIdArr = is_array($proIdArr)? $proIdArr: [(int) $proIdArr];
			$proIdArr = array_filter($proIdArr,'LibFc::Int');
			if(!empty($proIdArr))
			{
				$this->B2CDb->Begin();
				$this->del('us_product_collection',sprintf(' and prc_uid=%d and prc_pro_id in (%s)',$uid,implode(',',$proIdArr)));
				$res = $this->get('bc_product',['pro_id','pro_coll_count'],sprintf(' and pro_id in (%s)',implode(',',$proIdArr)));
				$countArr = array_column($res['data'], 'pro_coll_count','pro_id');
				foreach ($countArr as $k => $v) 
				{
					$newCount = $v>0?$v-1:0;
					$this->Set('bc_product',['pro_coll_count'=>$newCount],' and pro_id='.$k);
				}
				$this->B2CDb->End();
				return LibFc::ReturnData(true,$proIdArr);	
			}
		}
		return LibFc::ReturnData(false,'参数不正确');
	}

}
