<?php
/**
  * @method 宜配接口
  * @author soul 2017/6/16
  */
class ypAction
{
	/**
	  * @method 号码自动完成
	  * @author soul 2017/6/22
	  */
	function ajaxAutoProNumber()
	{
		//必须已开通店铺、防止恶意采集
		$OthCheck = new OthCheck();
		$OthCheck->hasShop();
		$resArr = [];
		$Params = mvc::$URL_PARAMS;
		$formatNum = LibFc::FormatNum($Params['number']);
		if(strlen($formatNum) > 2)
		{
			$YpBase = new YpBase();
			$res = $YpBase->get('yp_number', ['display'], sprintf(" and format like '%s%%' group by format order by length(format), format limit 8", $formatNum));
			foreach($res['data'] as $v)
			{
				$resArr[] = $v['display'];
			}
		}
		LibFc::ajaxJsonEncode(['status'=>true, 'data'=>$resArr]);
	}
	
	
	
	/**
	  * @method 通过厂商 号码搜索产品
	  * @author soul 2017/6/22
	  */
	function ajaxGetYpProByNum()
	{
		//必须已开通店铺、防止恶意采集
		$OthCheck = new OthCheck();
		$OthCheck->hasShop();
		$returnArr = [];
		$Params = mvc::$URL_PARAMS;

		$where = '';
		$YpBase = new YpBase();
		$YpProduct = new YpProduct();
		$html = '';
		if(!empty($Params['pid']))
		{
			$pid = (int) $Params['pid'];
			$where = sprintf(" and p.pid =%d", $pid);
		}
		else
		{
			$formatNum = LibFc::FormatNum($Params['number']);
			if(strlen($formatNum) > 2)
			{
				//宜配配件
				$res = $YpBase->get('yp_number', ['pid'], sprintf(" and format ='%s' group by pid", $formatNum) );
				if(!empty($res['data']))
				{
					$tempIdArr = [];
					foreach($res['data'] as $v)
					{
						$tempIdArr[] = $v['pid'];
					}
					$where = sprintf(" and p.pid in (%s) group by p.pid", implode(',',$tempIdArr));
				}
				else
				{
					//TCD
					$LibYpApi = new LibYpApi();
					$res = $LibYpApi->get('tcd/getArtByNum', ['fac'=>$Params['fac'], 'number'=>$Params['number']]);
					if(!empty($res['data']))
					{
						LibTpl::Set('artArr', $res['data']);
						$html = LibTpl::Fetch('yp/anyTcdPro.php');
						LibFc::ajaxJsonEncode(['status'=>true, 'data'=>$html]);
						exit;
					}
				}
			}
		}

		if($where != '')
		{
			$table = 'yp_product as p 
			left join yp_files_pro_pic as pp on p.pro_pic_id=pp.fileid 
			left join yp_part as pa on p.partid=pa.partid
			left join yp_lng_des as ld on pa.desid = ld.desid and lngid=1
			left join yp_lng_text as lt on ld.txtid = lt.txtid';
			$res = $YpBase->get($table, ['p.pid', 'pa.partid','pa.show_para', 'p.remark as param', 'p.url', 'lt.txt as part', 'pp.path as fpath', 'pp.name as fname'], $where);
			$ypArr = [];
			$getParaPidArr = [];
			foreach($res['data'] as $v)
			{
				$v['url'] = mvc::$cfg['HOST']['yp'].$v['url'].'cn.html';
				$v['img'] = !empty($v['fname']) ? mvc::$cfg['HOST']['ypimg'].'ProFile/Image/'.$v['fpath'].'100/'.$v['fname'].'.jpg' : '';
				$v['nums'] = [];
				$v['paras'] = [];
				//取配件参数
				if($v['show_para'] == 1)
				{
					$temp = $YpProduct->getProductParas($v['pid'], $v['partid']);
					$v['paras'] = $temp['data'];
				}
				$ypArr[$v['pid']] = $v;
			}
			$where = sprintf(" and pid in (%s) and ismain=1", implode(',', array_keys($ypArr)));
			$res = $YpBase->get('yp_number as n left join yp_factory as f on f.fid=n.fid', ['n.pid', 'factory', 'display'], $where);
			foreach($res['data'] as $v)
			{
				$ypArr[$v['pid']]['nums'][] = ['factory'=>$v['factory'], 'display'=>$v['display']];
			}
			if(count($ypArr) == 1)
			{
				$ypArr = array_shift($ypArr);
				//只有一个，直接显示对应的配件信息和OE号码比较列表
				$conArr = $this->getOeContrast($ypArr['pid']);
				LibTpl::Set('conArr', $conArr);
				LibTpl::Set('ypArr', $ypArr);
				LibTpl::Set('pid', $pid);
				$html = LibTpl::Fetch('yp/oneYpPro.php');
				LibFc::ajaxJsonEncode(['status'=>true, 'data'=>$html]);
				exit;
			}
			else
			{
				//多个配件时
				LibTpl::Set('ypArr', $ypArr);
				$html = LibTpl::Fetch('yp/anyYpPro.php');
				LibFc::ajaxJsonEncode(['status'=>true, 'data'=>$html]);
				exit;
			}
		}
		LibFc::ajaxJsonEncode(['status'=>true, 'data'=>$html]);
	}



	/**
	  * @method OE比较
	  * @author soul 2017/6/22
	  */
	function ajaxGetComByNum()
	{
		//必须已开通店铺、防止恶意采集
		$OthCheck = new OthCheck();
		$OthCheck->hasShop();
		$returnArr = [];
		$Params = mvc::$URL_PARAMS;
		$conArr = $this->getOeContrast([], explode(',', $Params['oes']));
		LibTpl::Set('conArr', $conArr);
		$html = LibTpl::Fetch('yp/ajaxGetComByNum.php');
		LibFc::ajaxJsonEncode(['status'=>true, 'data'=>$html]);	
	}


	/**
	  * @method 获取OE号码对比的数据
	  * $pid 或者 $oeArr
	  * @author soul 2017/6/22
	  */
	private function getOeContrast($pid = 0, $oeArr = [])
	{
		$pid = (int) $pid;
		if($pid > 0)
		{
			$YpBase = new YpBase();
			$res = $YpBase->get('yp_number', ['display'], sprintf(" and pid=%d and fid=770", $pid));
			foreach($res['data'] as $v)
			{
				$oeArr[] = $v['display'];
			}
		}
		$conArr = [];
		if(count($oeArr) > 0)
		{
			$LibYpApi = new LibYpApi();
			$res = $LibYpApi->get('tcd/getNumContrast', [], ['oes'=>$oeArr]);
			$conArr = $res['data'];
		}
		return $conArr;
	}

	/**
	  * @method 通过宜配产品ID获取产品信息
	  * @author soul 2017/6/22
	  */
	function ajaxGetYpProInfo()
	{
		//必须已开通店铺、防止恶意采集
		$OthCheck = new OthCheck();
		$OthCheck->hasShop();
		$resArr = ['nums'=>[], 'brands'=>[], 'models'=>[]];
		$Params = mvc::$URL_PARAMS;
		$yppid = (int) $Params['yppid'];
		if($yppid > 0)
		{
			$YpBase = new YpBase();

			//宜配OE号
			$res = $YpBase->get('yp_number as n left join yp_oe_make as m on n.oe_make_id=m.oe_make_id', ['m.name as fac', 'display', 'format'], sprintf(" and pid=%d and fid=770", $yppid));
			if(!empty($res['data']))
			{
				$sortArr = [];
				foreach($res['data'] as $v)
				{
					$v['fac'] = empty($v['fac'])? 'OE': $v['fac'];
					$sortArr['fac'][] = $v['fac'];
					$sortArr['format'][] = $v['format'];
					$resArr['nums'][] = $v;
				}
				array_multisort($sortArr['fac'], SORT_ASC, $sortArr['format'], SORT_ASC, $resArr['nums']);
			}
		}
		LibFc::ajaxJsonEncode(['status'=>true, 'data'=>$resArr]);
	}


	/**
	  * @method 通过厂商 号码搜索产品
	  * @author soul 2017/6/22
	  */
	function ajaxGetTcdByNum()
	{
		//必须已开通店铺、防止恶意采集
		$OthCheck = new OthCheck();
		$OthCheck->hasShop();
		$html = '';
		$Params = mvc::$URL_PARAMS;
		$LibYpApi = new LibYpApi();
		$res = $LibYpApi->get('tcd/getArtContrast', ['artIds'=>$Params['artIds']]);
		LibTpl::Set('conArr', $res['data']);
		$html = LibTpl::Fetch();
		LibFc::ajaxJsonEncode(['status'=>true, 'data'=>$html]);
	}
}