<?php

class PjlProduct extends PjlBase
{
	/**
	 * @method    添加|修改配件
	 * @param     [int]         $shopId  [店铺id]
	 * @param     [arr]         $dataArr [字段数组]
	 * @param     [int]        $proId   [配件id 不为空则为修改]
	 * @return   ['status'=>true,'data'=>xx]  返回配件id
	 * @author lu
	 * @copyright 2018-12-27
	 */
	function setShopProduct($shopId,$dataArr,$proId=0)
	{
		if(LibFc::Int($shopId)&&!empty($dataArr)&&is_array($dataArr))
		{
			$data=[
				'pro_shop_id'=>$shopId,
				'pro_status'=>(isset($dataArr['pro_status'])?$dataArr['pro_status']:0),
				'pro_display'=>$dataArr['pro_display'],
				'pro_format'=>LibFc::FormatNum($dataArr['pro_display']),
				'pro_name'=>$dataArr['pro_name'],
				'pro_ischeck'=>(isset($dataArr['pro_ischeck'])?$dataArr['pro_ischeck']:0),
				'pro_price'=>$dataArr['pro_price'],
				'pro_quality'=>$dataArr['pro_quality'],
				'pro_model'=>$dataArr['pro_model'],
				'pro_quality_des'=>(!empty($dataArr['pro_quality_des'])?$dataArr['pro_quality_des']:''),
				'pro_etime'=>time(),
			];
			if(empty($proId))
			{
				$data['pro_atime']=time();
				$res=$this->add('pl_product',$data);
			}
			else
			{
				$res=$this->set('pl_product',$data,sprintf(' and pro_shop_id=%d and pro_id=%d',$shopId,$proId));
				if($res['status'])
				{
					$res['data']=$proId;
				}
			}
			return LibFc::ReturnData($res['status'], $res['data']);
		}
		return LibFc::ReturnData(false, 'setShopProduct 参数错误');
	}
	/**
	 * @method    清空店铺所有配件
	 * @param     [int]     $shopId [店铺id]
	 * @return    [type]             [description]
	 * @author lu
	 * @copyright 2018-12-27
	 */
	function clearAll($shopId)
	{
		if(LibFc::Int($shopId))
		{
			$res=$this->del('pl_product',sprintf(' and pro_shop_id=%d',$shopId));
			return LibFc::ReturnData($res['status'], $res['data']);
		}
		return LibFc::ReturnData(false, '店铺Id不能为空');
	}
	/**
	 * @method    删除店铺配件
	 * @param     [int]     $shopId [店铺id]
	 * @param     [int|arr]     $proArr [配件id]
	 * @return    [type]             [description]
	 * @author lu
	 * @copyright 2018-12-27
	 */
	function delPro($shopId,$proArr)
	{
		if(!empty($proArr))
		{
			$proArr=is_array($proArr)?array_filter($proArr,'LibFc::Int'):[(int)$proArr];
			if(!empty($proArr))
			{
				$res=$this->del('pl_product',sprintf(' and pro_shop_id=%d and pro_id in(%s)',$shopId,implode(',',$proArr)));
				return LibFc::ReturnData($res['status'], $res['data']);
			}
		}
		return LibFc::ReturnData(false, '配件Id不能为空');
	}
	/**
	 * @method    根据号码和地区获取配件列表
	 * @param     [str]     $number   [号码]
	 * @param     [str]     $pctIds   [地区 逗号分隔]
	 * @param     string     $dataType [类型 【返回字段不一样】  base|all]
	 * @param     integer    $page     [当前页码]
	 * @param     integer    $perPage  [每页条数]
	 * @return    [type]               [description]
	 * @author lu
	 * @copyright 2018-12-27
	 */
	function getShopPro($number, $pctIds, $dataType = 'base', $page=1, $perPage= 20)
	{
		if(empty($number))
		{
			return LibFc::ReturnData(false, 'OE号不能为空');
		}
		if(empty($pctIds))
		{
			return LibFc::ReturnData(false, '地区不能为空');
		}
		$where=' and pro_status=1 and pro_ischeck=1 and shop_status=2';
		$where.=sprintf(" and pro_format='%s'",LibFc::FormatNum($number));
		$where.=(" and concat(',',shop_pct_ids,',') like ',".LibFc::Escape($pctIds).",%'");
		if($dataType=='base')
		{
			$fields=['shop_name','shop_id','pro_id','pro_name','shop_pct_ids'];
		}
		else
		{
			$fields=['shop_name','shop_id','pro_id','pro_display','pro_name','pro_model','pro_price','pro_quality','pro_quality_des','pro_atime','pro_etime','shop_pct_ids'];
		}
		$data=[];
		$res=$this->get('pl_product join pl_shop on shop_id=pro_shop_id',['count(1) count'],$where,true);
		$data['total']=$res['data']['count'];
		if($data['total']>0)
		{
			$where.=sprintf(' order by pro_etime desc limit %d,%d',($page-1)*$perPage,$perPage);
			$res=$this->get('pl_product join pl_shop on shop_id=pro_shop_id',$fields,$where);
			$PjlUser=new PjlUser();
			if(!empty($res['data']))
			{
				foreach ($res['data'] as $k => $v) 
				{
					if(!empty($v['shop_pct_ids']))
					{
						$address=$PjlUser->getAddrByIds($v['shop_pct_ids']);
						$v['address']=array_values($address['data']);
						$res['data'][$k]=$v;
					}
				}
			}
			$data['lists']=$res['data'];
		}
		return LibFc::ReturnData(true, $data);
	}
	/**
	 * @method    根据配件id获取配件详情
	 * @param     [int]         $proId [配件id]
	 * @return    [type]                [description]
	 * @author lu
	 * @copyright 2018-12-27
	 */
	function getShopProInfo($proId)
	{
		if(!LibFc::Int($proId))
		{
			return LibFc::ReturnData(false, '配件Id不能为空');
		}
		$where=sprintf(' and pro_id=%d',$proId);
		$fields=['shop_name','shop_logo','shop_id','shop_style','pro_id','pro_status','pro_display','pro_name','pro_model','pro_price','pro_quality','pro_quality_des','pro_atime','pro_etime','shop_pct_ids','pro_ischeck','pro_check'];
		$res=$this->get('pl_product join pl_shop on shop_id=pro_shop_id',$fields,$where,true);
		if(!empty($res['data']['shop_logo']))
		{
			$res['data']['shop_logo_url']=mvc::$cfg['FILE_CENTER']['pjl']['url_get'].'&c=shop&f='.$res['data']['shop_logo'].mvc::$cfg['FILE_CENTER']['pjl']['get_uri'];
		}
		return $res;
	}
	/**
	 * @method    获取店铺配件列表
	 * @param     [int]          $shopId  [店铺id]
	 * @param     integer         $page    [当前页码]
	 * @param     integer         $perPage [每页条数]
	 * @param     string          $orderby [排序]
	 * @param     string          $where   [条件]
	 * @return    [type]                   [description]
	 * @author lu
	 * @copyright 2018-12-27
	 */
	function getShopProlists($page=1,$perPage=20,$orderby='pro_etime desc',$where='')
	{
		$where.=!empty($orderby)?sprintf(' order by %s',$orderby):' order by pro_etime desc';
		$where.=sprintf(' limit %d,%d',($page-1)*$perPage,$perPage);
		$fields=['shop_name','shop_id','shop_style','pro_id','pro_status','pro_display','pro_name','pro_model','pro_price','pro_quality','pro_quality_des','pro_atime','pro_etime','pro_ischeck'];
		$res=$this->get('pl_product join pl_shop on shop_id=pro_shop_id',$fields,$where);
		return LibFc::ReturnData(true, $res['data']);
	}

	/**
	 * @method    修改附近谁有的地址
	 * @author soul
	 * @copyright 2019/2/19
	 */
	public function setShopAddr($shopId, $addrArr)
	{
		$shopId = (int) $shopId;
		if($shopId < 1 || empty($addrArr)) return LibFc::ReturnData(false, 'setShopAddr 参数不正确');
		$this->PjlDb->Begin();

		$newIdArr = array_keys($addrArr);
		$this->del('pl_shop_addr', sprintf(" and sha_id not in (%s) and sha_shop_id=%d and sha_main != 1", implode(',', $newIdArr), $shopId));
		foreach($addrArr as $shaId=>$val)
		{
			if(empty($val['sha_tel']) || empty($val['sha_pct_ids']) || empty($val['sha_addr'])) continue;
			$val['sha_etime'] = time();
			if($shaId > 0)
			{
				$this->set('pl_shop_addr', $val, sprintf(" and sha_id =%d and sha_shop_id=%d", $shaId, $shopId));
			}
			else
			{
				$val['sha_shop_id'] = $shopId;
				$val['sha_atime'] = time();
				$this->add('pl_shop_addr', $val);
			}
		}
		$this->PjlDb->End();

		$LibYpApi = new LibYpApi();
		$LibYpApi->Get('pjl/updateShopAddr',['shop_id'=>$shopId]);
		return LibFc::ReturnData(true);
	}


	/**
	 * @method    初始化附近谁有的主地址
	 * @author soul
	 * @copyright 2019/2/19
	 */
	public function initShopAddr($shopId)
	{
		$LibYpApi = new LibYpApi();
		return $LibYpApi->Get('pjl/updateShopAddr',['shop_id'=>$shopId]);
	}
}