<?php
/**
	* @method 用户商城我的优惠
	* @author 许
	* @copyright 2017/8/3
	*/
class B2cUserFavourable extends B2cBase
{
	/**
	* @method 查询当前用户当前页数的店铺优惠-会员卡信息
	* @param  $PerPage 每页条数 int
	* @param  $NowPage 当前页码 int
	* @param  $userId 用户ID int
	* @author xu
	* @copyright 2017/08/03
	* @return  array('status'=>true,'data'=>'','code'=>)/array('status'=>true,'data'=>array(),'code'=>)
	*/
	public function getMemCardMsg($PerPage,$NowPage,$userId)
	{	
		//参数
		if(empty($PerPage) || !LibFc::Int($PerPage) || empty($NowPage) || !LibFc::Int($NowPage) || empty($userId) || !LibFc::Int($userId))
		{
			return LibFc::ReturnData(false,'参数错误！',0);
		}
		$table='sh_member_link left join sh_member on meml_mem_id=mem_id left join sh_shop on mem_shop_id=shop_id ';
		$where=' and meml_uid='.$userId.' and mem_status=1 and shop_status=1 order by meml_add_time desc';
		$res = $this->get($table, ['*'], $where.sprintf(" limit %d,%d",($NowPage-1)*$PerPage, $PerPage));

		$temp = $res['data'];
		// 店铺管理员名称
		$YpwUser = new YpwUser();
		foreach ($temp as $k => $v) {
			$rez =  $YpwUser->getUser(['u_nickname'],' and u_id='.$v['shop_uid'], true);
			$temp[$k]['u_nickname'] = !empty($rez['data']['u_nickname']) ? $rez['data']['u_nickname'] : "";
		}

		return LibFc::ReturnData(true,$temp);
	}
	/**
	 * @method    获取某店铺某用户id的会员信息
	 * @param     [int]         $uid    [用户ID]
	 * @param     [int|arr]         $shopId [店铺ID]
	 * @param     [arr]         $fields [字段]
	 * @return    [arr]                 [array([1]=>array('mem_discount'=>9.5))   以店铺id为键名]
	 * @author 卢
	 * @copyright 2017-08-05
	 */
	public function getMemMsgByUid($uid,$shopId,$fields)
	{
		if(LibFc::Int($uid)&&is_array($fields))
		{
			$data=array();
			$shopArr=is_array($shopId)? $shopId : [(int)$shopId];
			if(count($shopArr))
			{
				$fields=array_merge($fields,['meml_shop_id']);
				$table='sh_member_link left join sh_member on meml_shop_id=mem_shop_id and mem_id=meml_mem_id left join sh_member_level on mel_id=mem_mel_id';
				$where=sprintf(' and meml_shop_id in (%s) and meml_uid=%d and mem_status>0',implode(',',$shopArr),$uid);
				$res=$this->get($table,$fields,$where);
				$data=array_column($res['data'], null,'meml_shop_id');
				return LibFc::ReturnData(true,$data);
			}
		}
		return LibFc::ReturnData(false,'参数错误');
	}
	/**
	 * @method    获取店铺当前时间段满减信息
	 * @param     [int|arr]     $shopArr [店铺ID]
	 * @return    [arr]              ['status'=>true 'data'=>['123'=>['man_shop_id'=>123,'man_full_money'=>100.....]]]
	 * @author 卢
	 * @copyright 2017-08-30
	 */
	public function getManJian($shopArr)
	{
		if(!empty($shopArr))
		{
			$data=[];
			$shopId=is_array($shopArr)?$shopArr :[(int)$shopArr];
			$res=$this->get('sh_manjian',['*'],sprintf(' and man_shop_id in (%s) and man_btime<=%d and man_etime>=%d',implode(',',$shopId),time(),time()));
			if(count($res['data']))
			{
				foreach ($res['data'] as $k => $v) 
				{
					$data[$v['man_shop_id']][]=$v;
				}				
			}
			return LibFc::ReturnData(true,$data);
		}
		return LibFc::ReturnData(false,'参数错误');
	}
}