<?php
/**
*号码搜索
*/
class numberAction extends baseAction
{
	/**
	 * @method 号码检测
	 * @http  POST
	 * @param token      string [必填]
	 * @param numbers    string [必填] 多个逗号分隔 GDB900,GDB901....
	 * @author soul 2018/10/30
	 * @return
	 	{"status":true,"data":[{"num":"GDB900","type":"yp"},{"num":"GDB1322","type":"yp"},{"num":"GDB212154511","type":""},{"num":333123,"type":"yp"}]}
	 */
	public function checkNumber()
	{
		$arr = [];
		if(!empty($_POST['numbers']))
		{
			foreach(explode(',', $_POST['numbers']) as $v)
			{
				$arr[LibFc::FormatNum($v)] = '';
			}
			$typeArr = ['yp', 'tcd', 'aaia'];
			foreach($typeArr as $type)
			{
				$snum = [];
				foreach($arr as $num=>$getType)
				{
					if(empty($getType)) $snum[] = $num;
				}
				if(empty($snum)) break;
				switch($type)
				{
					case 'yp':
						$YpsBase = new YpsBase();
						$where = sprintf(" and format in ('%s') group by format", implode("','", $snum));
						$res = $YpsBase->get('yp_number', ['format'], $where);
						foreach($res['data'] as $nv)
						{
							$arr[LibFc::FormatNum($nv['format'])] = $type;
						}
					break;
					case 'tcd':
						$TcdmArt = new TcdmArt();
						$res = $TcdmArt->get('tof_article_number', ['arl_format'], sprintf(" and arl_format in ('%s') group by arl_format", implode("','", $snum)));
						foreach($res['data'] as $nv)
						{
							$arr[$nv['arl_format']] = $type;
						}
					break;
					case 'aaia':
						$AaiaBase = new AaiaBase();
						$where = sprintf(" and format in ('%s') group by format", implode("','", $snum));
						$res = $AaiaBase->get('yp_number', ['format'], $where);
						foreach($res['data'] as $nv)
						{
							$arr[LibFc::FormatNum($nv['format'])] = $type;
						}
					break;
				}
			}
		}
		$tarr = [];
		foreach($arr as $k=>$v)
		{
			$tarr[] = ['num'=>$k, 'type'=>$v];
		}
		LibFc::EchoJson(['status'=>true, 'data'=>$tarr]);
	}
	
	/**
	  * @method get  号码补全
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  word			   string [必填] 号码
	  * @param  type               array  [选填] 默认按顺序 ['tcd', 'etk', 'yp', 'oth', 'pjd']
	  * @param  count              int    [选填] 10  数量
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function fix()
	{
		
		$numArr = [];
		$formatNum = LibFc::FormatNum($_POST['word']);
		$numArr = [];
		$count = $_POST['count'] > 0 &&  $_POST['count'] < 50? (int) $_POST['count']: 20;
		if(strlen($formatNum) > 2)
		{
			$typeArr = empty($_POST['type']) ? ['tcd', 'etk', 'yp', 'oth', 'pjd'] : $_POST['type'];
			foreach($typeArr as $type)
			{
				if(!empty($numArr)) break;
				switch($type)
				{
					case 'etk':
						$EtkBasic = new EtkBasic();
						$res = $EtkBasic->Get('data_product_search', ['src_realoem'], sprintf(" and src_format like '%s%%' group by src_format limit %d", LibFc::Escape($formatNum), $count));
						$numArr = array_column($res['data'], 'src_realoem');
					break;
					case 'pjd':
						$B2cBase = new B2cBase();
						$table = 'bc_number join bc_product on pro_id = num_pro_id join sh_shop on pro_shop_id=shop_id and shop_status = 1 and shop_istest != 1';
						$res = $B2cBase->get($table, ['num_display'], sprintf(" and pro_status=1 and num_format like '%s%%' group by num_format limit %d", LibFc::Escape($formatNum), $count));
						$numArr = array_column($res['data'], 'num_display');
					break;
					case 'yp':
						$YpsBase = new YpsBase();
						$where = sprintf(" and format like '%s%%' group by format limit %d", LibFc::Escape($formatNum),  $count);
						$res = $YpsBase->get('yp_number', ['display'], $where);
						$numArr = array_column($res['data'], 'display');
					case 'tcd':
						$TcdmArt = new TcdmArt();
						$res = $TcdmArt->get('tof_article_number', ['arl_number'], sprintf(" and arl_format LIKE '%s%%' group by arl_format ORDER BY length(arl_format), arl_format limit %d", LibFc::Escape($formatNum), $count));
						$numArr = array_column($res['data'], 'arl_number');
					break;
				}
			}
		}
		echo json_encode(['status'=>true, 'data'=>$numArr]);
	}
	
	
	
	
	/**
	  * @method get  号码搜索
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  word			   string [必填] 号码
	  * @param  type               string [选填] 数据源  默认所有 多个逗号分隔  etk,pjd,super,yp,tcd,aaia
	  * @param  dtype              string [选填] para...
	  * @param  count              int    [选填] TCD最大数量 默认10
	  * @param  uid                int [选填] 用户ID
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function search()
	{
		$Params = mvc::$URL_PARAMS;
		$OthNumber = new OthNumber();
		$res = $OthNumber->search($Params);
		LibFc::EchoJson($res);
	}



	/**
	  * @method get 配件详情信息
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  type               string [必填] etk|pjd|yp|tcd|super
	  * @param  proId 产品ID
	  * @param  etkId  [必填]type = etk
	  * @param  grp2Id [必填]子零件ID type = etk
	  * @param  lnkId [可选] epcModelPartItem 返回的lnk_id type = etk
	  * @param  mod3_id            int [选填] 宜配车型ID
	  * @param  tcd_mod3_id        int [选填] TCD车型ID
	  * @param  uid                int [选填] 用户ID
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data":{"pro_id":"61055","pro_url":"http:\/\/www.yiparts.com\/zastava,bonnet_cable,46616224,cn.html","pro_name":"机盖拉线","para":[{"name":"长度","val":"1690","unit":"mm"}],"parastr":"","pic":[],"num":[{"factory":"OE","display":"466162.24"}],"pro_price":{"title":"销售价","price":"0.00"},"models":{"ypc":[],"tcd":{"model3_id":0,"model_des":1},"aaia":[]},"nums":{"oe":[{"oe":"1","factory":"ZASTAVA","display":"466162.24"}],"ref":[{"oe":"0","factory":"KAGER","display":"19-4079"}]}},"code":null,"encry":0}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function itemDes()
	{
		$Params = mvc::$URL_PARAMS;
		$proArr = [];
		$proId = (int) $_GET['proId'];
		$type = $_GET['type'];
		$mod3Id = 0;
		if(isset($_GET['mod3_id']) && !empty($_GET['mod3_id'])) $mod3Id = (int) $_GET['mod3_id'];
		else if(isset($_GET['mod3Id']) && !empty($_GET['mod3Id'])) $mod3Id = (int) $_GET['mod3Id'];
		$tcd_mod3_id = isset($_GET['tcd_mod3_id'])? (int) $_GET['tcd_mod3_id']:0;
		if($proId > 0 && !empty($type))
		{
			$Ypc2Model = new Ypc2Model();
			switch($type)
			{
				case 'etk':
					$etkId = (int) $_GET['etkId'];
					$grp2Id = (int) $_GET['grp2Id'];
					$lnkId = isset($_GET['lnkId']) ? (int) $_GET['lnkId']: 0;
					if($etkId < 1 && $mod3Id > 0)
					{
						$YpcModel = new YpcModel();
						$res = $YpcModel->getModel(3, ['b_etk_id'], sprintf(" and m3_id=%d and b_etk_id > 0", $mod3Id), true);
						if(empty($res['data'])) LibFc::ReturnJson(true, $arr);
						$etkId = $res['data']['b_etk_id'];
					}
					if(!empty(mvc::$cfg['DB']['EPC'][$etkId]))
					{
						$EpcItem = new EpcItem($etkId);
						$res = $EpcItem->getItem($proId, $grp2Id, $lnkId);
						$proArr = $res['data'];
						if(empty($proArr)) 
							LibFc::EchoJson(['status'=>true, 'data'=>$proArr]);
						//车型样例
						$proArr['models'] = ['ypc'=>[], 'tcd'=>[], 'aaia'=>[]];
						$res = $EpcItem->getItemMod3Id($proId, $grp2Id);
						$ypcMod3IdArr = $res['data'];
						
						$display = $etkId == 7? ltrim($proArr['display'], 'A'): $proArr['display'];

						//号码样例
						$proArr['nums'] = ['oe'=>[], 'ref'=>[]];
						$EtkNum = new EtkNum();
						$res = $EtkNum->getReplaceNumber($proArr['display']);
						$proArr['nums']['oe'] = $res['data'];

						$B2cProduct = new B2cProduct();
						$res = $B2cProduct->getItemByNumber($display, ' and pro_show_pjl=1', $Params['uid']);
						$proArr['pjd'] = array_values($res['data']);
					}
				break;
				case 'pjd':
					$B2cProduct = new B2cProduct();
					$res = $B2cProduct->getAnyProductInfo([$proId], $Params['uid'], ['para','numMain', 'picAll', 'desc'], $mod3Id);
					if(!empty($res['data']))
					{
						$proArr = $res['data'][$proId];
						$proArr['models'] = ['ypc'=>[], 'tcd'=>[], 'aaia'=>[]];
						$res = $B2cProduct->get('bc_product_model3_link', ['distinct pml3_mod3_id'], sprintf(" and pml3_pro_id =%d", $proId));
						$ypcMod3IdArr = array_column($res['data'], 'pml3_mod3_id');
						//是否显示互换号
						if(!empty($proArr['shop_srefnum']))
						{
							$proArr['nums'] = ['oe'=>[], 'ref'=>[]];
							foreach($proArr['nums'] as $numtype=>$vv)
							{
								$res = $B2cProduct->getItemNumber($proId, $numtype, 1, 1);
								$proArr['nums'][$numtype] = $res['data'];
							}
						}
					}
				break;
				case 'super':
					$B2cProduct = new B2cProduct();
					$res = $B2cProduct->getSuperProductInfo([$proId], $Params['uid'], ['para', 'numMain', 'picAll', 'desc'], $mod3Id);
					if(!empty($res['data']))
					{
						$proArr = $res['data'][$proId];
						$proArr['models'] = ['ypc'=>[], 'tcd'=>[], 'aaia'=>[]];
						$res = $B2cProduct->get('bc_product_model3_link', ['distinct pml3_mod3_id'], sprintf(" and pml3_pro_id =%d", $proId));
						$ypcMod3IdArr = array_column($res['data'], 'pml3_mod3_id');
						//是否显示互换号
						if(!empty($proArr['shop_srefnum']))
						{
							$proArr['nums'] = ['oe'=>[], 'ref'=>[]];
							foreach($proArr['nums'] as $numtype=>$vv)
							{
								$res = $B2cProduct->getItemNumber($proId, $numtype, 1, 1);
								$proArr['nums'][$numtype] = $res['data'];
							}
						}
					}
				break;
				case 'yp':
					$YpsProduct = new YpsProduct();
					$res = $YpsProduct->getAnyProductInfo([$proId], ['para', 'picAll']);
					if(!empty($res['data']))
					{
						$proArr = $res['data'][$proId];
						$proArr['models'] = ['ypc'=>[], 'tcd'=>[], 'aaia'=>[]];

						$res = $YpsProduct->getYpcModelId($proId);
						$ypcMod3IdArr = $res['data'];
						
						$table = 'yp_tcd_model3_pro_link as m3pl
						join yp_tcd_model3 as m3 on m3pl.model3_id=m3.model3_id
						join yp_make as m on m.make_id=m3.make_id';
						$res = $YpsProduct->get($table, ['m3pl.model3_id AS mod3_id, 0 as model_des, brand_id_ypc'], sprintf(" and pid=%d order by brand_id_ypc desc limit 1", $proId), true);
						$proArr['models']['tcd'] = $res['data'];
						if(empty($proArr['models']['tcd']))
						{
							$res = $YpsProduct->get('yp_product', ['model_des'], sprintf(" and pid=%d limit 1", $proId), true);
							if(!empty($res['data']))
							{
								$proArr['models']['tcd'] = ['model3_id'=>0, 'model_des'=>1];
							}
						}
						
						//主号码
						$res = $YpsProduct->get('yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid', ['f.factory, n.format'], sprintf(" and n.pid = %d and n.ismain=1 limit 1", $proId), true);
						$mainNum = ['factory'=>$res['data']['factory'], 'display'=>$res['data']['format']];

						$proArr['nums'] = ['oe'=>[], 'ref'=>[]];
						foreach($proArr['nums'] as $numtype=>$vv)
						{
							$res = $YpsProduct->getItemNumber($proId, $numtype, 1, 1);
							$proArr['nums'][$numtype] = $res['data'];
						}
					}
				break;
				case 'tcdcn':
				case 'tcd':
					$TcdmArt = new TcdmArt();
					$res = $TcdmArt->getProductInfo([$proId], ['para', 'picAll'], $mod3Id, [$tcd_mod3_id]);
					if(!empty($res['data']))
					{
						$proArr = $res['data'][$proId];
						$proArr['models'] = ['ypc'=>[], 'tcd'=>[], 'aaia'=>[]];

						$res = $TcdmArt->getYpcModelId($proId);
						$ypcMod3IdArr = $res['data'];

						$res = $TcdmArt->get('tof_articles join tof_suppliers on sup_id = art_sup_id', ['art_number display, sup_brand factory'], sprintf(" and art_id = %d", $proId), true);
						$mainNum = ['factory'=>$res['data']['factory'], 'display'=>$res['data']['display']];

						$proArr['nums'] = ['oe'=>[], 'ref'=>[]];
						foreach($proArr['nums'] as $numtype=>$vv)
						{
							$res = $TcdmArt->getItemNumber($proId, $numtype, 1, 1);
							$proArr['nums'][$numtype] = $res['data'];
						}
					}
				break;
				case 'aaia':
					$AaiaProduct = new AaiaProduct();
					$res = $AaiaProduct->getAnyProductInfo([$proId], ['para','picAll']);
					if(!empty($res['data']))
					{
						$proArr = $res['data'][$proId];
						$proArr['models'] = ['ypc'=>[], 'tcd'=>[], 'aaia'=>[]];
						
						$res = $AaiaProduct->get('yp_number as n join yp_factory as f on n.fid=f.fid', ['factory, display'], sprintf(" and n.pid = %d and f.fid=770", $proId));
						$oeNumArr = $res['data'];

						$res = $AaiaProduct->get('yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid', ['f.factory, n.format'], sprintf(" and n.pid = %d and n.ismain=1 limit 1", $proId), true);
						$mainNum = ['factory'=>$res['data']['factory'] == 'OEM'? 'OE': $res['data']['factory'], 'display'=>$res['data']['format']];

						$proArr['nums'] = ['oe'=>[], 'ref'=>[]];
						foreach($proArr['nums'] as $numtype=>$vv)
						{
							$res = $AaiaProduct->getItemNumber($proId, $numtype, 1, 1);
							$proArr['nums'][$numtype] = $res['data'];
						}
					}
				break;
				case 'zhipei':
					$OthProduct = new OthProduct();
					$res = $OthProduct->getZhipeiAnyProductInfo([$proId], ['para', 'picAll']);
					if(!empty($res['data']))
					{
						$proArr = $res['data'][$proId];
					}
				break;
				default:
					if(stripos($type, 'oth') === 0)
					{
						$OthProduct = new OthProduct();
						$res = $OthProduct->getOthSiteAnyProductInfo($type, [$proId], ['para', 'picAll']);
						if(!empty($res['data']))
						{
							$proArr = $res['data'][$proId];
						}
					}
				break;
			}
		}

		if(!empty($proArr))
		{
			$proArr['type'] = $type;
			$proArr['brand_id_ypc'] = 0;
			//ypc国内车型
			if(empty($ypcMod3IdArr) && !empty($oeNumArr))
			{
				$YpcModel = new YpcModel();
				$res = $YpcModel->getM3idByNum($oeNumArr);
				$ypcMod3IdArr = $res['data'];
			}
			if(!empty($ypcMod3IdArr))
			{
				$Ypc2Model = new Ypc2Model();
				$where = sprintf(" and mod3_id in (%s) order by brand_initial, brand_name, make_seq, make_name, mod2_name, mod2_start_year desc, mod3_name, mod3_start_year desc limit 1", implode(',', $ypcMod3IdArr));
				$res = $Ypc2Model->getModel(3, ['brand_id', 'brand_etk_id', 'mod3_id'], $where, true);
				$proArr['models']['ypc'] = $res['data'];
			}

			//tcd 
			if($type != 'yp')
			{
				$tcdProId = $proId;
				if(stripos($type, 'tcd') === false)
				{
					$tcdProId = 0;
					if(!empty($mainNum['factory']) && !empty($mainNum['format']))
					{
						$TcdmArt = new TcdmArt();
						$res = $TcdmArt->getArtIdByFacNumber($mainNum['format'], $mainNum['factory']);
						if(!empty($res['data']['pro_id'])) $tcdProId = $res['data']['pro_id'];
					}
				}
				$TcdmArt = new TcdmArt();
				$table = 'tof_article_types 
					join tof_types on typ_id = lat_typ_id 
					join tof_manufacturers on mfa_id = typ_mfa_id';
				$where = sprintf(" and lat_art_id =%d order by mfa_brand_id_ypc desc", $tcdProId);
				$filed = ['typ_id mod3_id, mfa_brand_id_ypc brand_id_ypc'];
				$res = $TcdmArt->get($table, $filed, $where, true);
				if(!empty($res['data'])) $proArr['models']['tcd'] = $res['data'];
			}

			//找到产品ID
			$aaiaProId = $proId;
			$AaiaBase = new AaiaBase();
			if($type != 'aaia')
			{
				$aaiaProId = 0;
				if(!empty($mainNum['factory']) && !empty($mainNum['format']))
				{
					$where = sprintf(" and f.factory = '%s' and n.format='%s' and n.ismain=1 limit 1", LibFc::Escape($mainNum['factory']), LibFc::Escape(LibFc::FormatNum($mainNum['format'])));
					$res = $AaiaBase->get('yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid', ['pid'], $where, true);
					if(!empty($res['data'])) $aaiaProId = $res['data']['pid'];
				}
			}
			if($aaiaProId > 0)
			{
				$table = 'yp_aaia_model3_pro_link as m3pl
				join yp_aaia_model3 as m3 on m3pl.model3_id=m3.model3_id
				join yp_make as m on m.make_id=m3.make_id';
				$res = $AaiaBase->get($table, ['m3.model3_id AS mod3_id, brand_id_ypc'], sprintf(" and pid=%d order by brand_id_ypc desc limit 1", $aaiaProId), true);
				$proArr['models']['aaia'] = $res['data'];
			}
			
			foreach($proArr['models'] as $k=>$v)
			{
				if($proArr['brand_id_ypc'] > 0) break;
				if(!empty($v['brand_id_ypc']))
					$proArr['brand_id_ypc'] = $v['brand_id_ypc'];
				else if($k == 'ypc' && !empty($v['brand_id']))
					$proArr['brand_id_ypc'] = $v['brand_id'];
			}

			if(empty($proArr['brand_id_ypc']))
			{
				switch($type)
				{
					case 'yp':
						$YpsProduct = new YpsProduct();
						$table = 'yp_make_pro_link as mpl join yp_make as m on m.make_id=mpl.make_id';
						$res = $YpsProduct->get($table, ['brand_id_ypc'], sprintf(" and pid=%d order by brand_id_ypc desc limit 1", $proId), true);
						if(!empty($res['data'])) $proArr['brand_id_ypc'] = $res['data']['brand_id_ypc']; 
					break;
					case 'aaia':
						$table = 'yp_make_pro_link as mpl join yp_make as m on m.make_id=mpl.make_id';
						$res = $AaiaBase->get($table, ['brand_id_ypc'], sprintf(" and pid=%d order by brand_id_ypc desc limit 1", $aaiaProId), true);
						if(!empty($res['data'])) $proArr['brand_id_ypc'] = $res['data']['brand_id_ypc']; 
					break;
				}
			}

			if(is_array($proArr['pic']) && !empty($proArr['pic']))
			{
				//统一返回大图
				$smallArr = ['/small/'=>'/', '/400/'=>'/800/', '/150/'=>'/', '/320/'=>'/', '/100/'=>'/'];
				foreach($proArr['pic'] as $k=>$v)
				{
					foreach($smallArr as $kk=>$vv)
					{
						if(stripos($v, $kk) !== false)
						{
							$proArr['pic'][$k] = str_replace($kk, $vv, $v);
							break;
						}
					}
				}
			}
		}

		if($type == 'super')
		{
			unset($proArr['models']['aaia']);
			unset($proArr['models']['tcd']);
		}
		LibFc::EchoJson(['status'=>true, 'data'=>$proArr]);
	}



	/**
	  * @method get 配件的互换号
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  etkId			   int [必填] etkId
	  * @param  proId			   int [必填] 产品ID
	  * @param  type               string [必填] etk|pjd|yp|tcd|super
	  * @param  numtype            string [选填] oe|ref|all
	  * @param  page               int [选填] 第几页
	  * @param  per				   int [选填] 每页N条数据
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	 {"status":true,"data":[{"factory":"ATE","display":"13.0460-2969.2"},{"factory":"ATE","display":"602969"},{"factory":"DELPHI","display":"LP560"},{"factory":"FERODO","display":"FDB546"},{"factory":"HONEYWELL","display":"571410"},{"factory":"ICER","display":"180698"},{"factory":"ICER","display":"180698-700"},{"factory":"LPR","display":"05P301"},{"factory":"MINTEX","display":"MDB1403"},{"factory":"NECTO","display":"FD0461A"},{"factory":"OE","display":"0060 750 413"},{"factory":"OE","display":"0060 750 838"},{"factory":"OE","display":"0060 777 264"},{"factory":"OE","display":"0060 795 402"},{"factory":"PAGID","display":"T5033"},{"factory":"REMSA","display":"0259.02"},{"factory":"ROADHOUSE","display":"2259.02"},{"factory":"TEXTAR","display":"2116501"},{"factory":"TEXTAR","display":"21165 165 2 5 T4047"},{"factory":"TRW","display":"GDB900"},{"factory":"VALEO","display":"598008"},{"factory":"WVA","display":"21164"},{"factory":"WVA","display":"21165"},{"factory":"WVA","display":"21166"},{"factory":"WVA","display":"21167"}],"code":null}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function itemNumber()
	{
		$numArr = [];
		$proId = (int) $_GET['proId'];
		$etkId = isset($_GET['etkId'])? (int) $_GET['etkId']: 0;
		$grp2Id = isset($_GET['grp2Id'])? (int) $_GET['grp2Id']: 0;
		$numtype = empty($_GET['numtype']) ? 'all': $_GET['numtype'];
		$per = empty($_GET['per']) || $_GET['per'] < 1 ? 200: (int) $_GET['per'];
		$page = empty($_GET['page']) || $_GET['page'] < 1 ? 1: (int) $_GET['page'];
		$type = $_GET['type'];
		if($proId != '' && !empty($type))
		{
			switch($type)
			{
				case 'etk':
					if($numtype != 'ref' && $page < 2){
						$lnkId = isset($_GET['lnkId']) ? (int) $_GET['lnkId']: 0;
						if($etkId < 1 && $mod3Id > 0){
							$YpcModel = new YpcModel();
							$res = $YpcModel->getModel(3, ['b_etk_id'], sprintf(" and m3_id=%d and b_etk_id > 0", $mod3Id), true);
							if(empty($res['data'])) LibFc::ReturnJson(true, $arr);
							$etkId = $res['data']['b_etk_id'];
						}
						if(!empty(mvc::$cfg['DB']['EPC'][$etkId])){
							$EpcItem = new EpcItem($etkId);
							$res = $EpcItem->getItem($proId, $grp2Id, $lnkId);
							$proArr = $res['data'];
							if(!empty($proArr)){
								$EtkNum = new EtkNum();
								$res = $EtkNum->getReplaceNumber($proArr['display']);
								$numArr = $res['data'];
							}
						}
					}
				break;
				case 'pjd':
				case 'super':
					$B2cProduct = new B2cProduct();
					$res = $B2cProduct->getItemNumber($proId, $numtype, $page, $per);
					$numArr = $res['data'];
				break;
				case 'yp':
					$YpsProduct = new YpsProduct();
					$res = $YpsProduct->getItemNumber($proId, $numtype, $page, $per);
					$numArr = $res['data'];
				break;
				case 'tcdcn':
				case 'tcd':
					$TcdmArt = new TcdmArt();
					$res = $TcdmArt->getItemNumber($proId, $numtype, $page, $per);
					$numArr = $res['data'];
				break;
				case 'aaia':
					$AaiaProduct = new AaiaProduct();
					$res = $AaiaProduct->getItemNumber($proId, $numtype, $page, $per);
					$numArr = $res['data'];
				break;
				default:
					if(stripos($type, 'oth') === 0)
					{
						$OthProduct = new OthProduct();
						$res = $OthProduct->getOthSiteItemNumber($type, $proId, $numtype, $page, $per);
						$numArr = $res['data'];
					}
				break;
			}
		}
		echo json_encode(['status'=>true, 'data'=>$numArr]);
	}
	

	/**
	  * @method 获取宜配车型的描叙车型
	  * @http  get
	  * @param  proId 产品ID
	  * @author soul 
	  * @copyright 2018/7/26
	  * @return json
		{"status":true,"data":{"model_des":"ZASTAVA&nbsp; YUGO FLORIDA 1.1 1989\/08\/01&nbsp; \r\n\r\nZASTAVA&nbsp; YUGO FLORIDA 1.3 (103 A) 1991\/03\/01&nbsp; \r\n\r\nZASTAVA&nbsp; YUGO FLORIDA 1.4 1988\/10\/01-1995\/06\/01 \r\n\r\nZASTAVA&nbsp; YUGO FLORIDA 1.4 CAT 1988\/10\/01&nbsp; \r\n\r\nZASTAVA&nbsp; YUGO FLORIDA 1.6 1989\/08\/01-1995\/06\/01 \r\n"},"code":0,"encry":0}
	  {"status":false,"data":'失败原因',"code":''}
	*/
	public function itemYpModelDes()
	{
		$proId = (int) $_GET['proId'];
		$YpsProduct = new YpsProduct();			
		$res = $YpsProduct->get('yp_product', ['model_des'], sprintf(" and pid=%d limit 1", $proId), true);
		$re = ['<br>', '<br/>', '<br />'];
		if(!empty($res['data']))
			$res['data']['model_des'] =  strip_tags(str_replace($re, "\r\n", $res['data']['model_des']));
		echo json_encode($res);
	}


	/**
	  * @method 配件适用车型(树形)
	  * @http  get
	  * @param  type  产品类型etk|pjd|yp|tcd|super|tcdcn
	  * @param  proId 产品ID
	  * @param  etkId  [可选]type = etk
	  * @param  grp2Id [可选]子零件ID type = etk
	  * @param  mod_type [必选] 车型类型 ypc|tcd|aaia
	  * @param  mod_level [必选] 车型级别 
									ypc|tcd 品牌：0  车系：1  车型：2 明细：3
									aaia  品牌：0  车系：1  车型：2 年份：3  明细：4
	  * @param  make_id [可选] 车系：1 
	  * @param  brand_id [可选] 车系：1  ypc车型需要提供
	  * @param  mod1_id [可选] 车系ID
	  * @param  mod2_id [可选] 车型ID
	  * @param  mod3_id [可选] 年份ID|| 明细ID
	  * @author soul 
	  * @copyright 2018/7/26
	  * @return json
		ypc  http://192.168.1.115/paojieling/api/index.php/data/itemModelTree?mod3Id=7240&grp2Id=135080&proId=785205&etkId=7&from=PJLH5&devcode=ujhniais26imf4euu15o4mkg74&mod_level=0&type=etk&mod_type=ypc
		   mod_level=0 (make_show_brand = 1 时显示品牌名称用brand_name， 否则用make_name)
				{"status":true,"data":[{"brand_id":"3","make_show_brand":0,"brand_name":"奔驰","make_id":"1","make_name":"北京奔驰"},{"brand_id":"3","make_show_brand":0,"brand_name":"奔驰","make_id":"149","make_name":"奔驰(进口)"}],"code":null,"encry":0}
	  	   mod_level=1 brand_id=3 make_id=1
				{"status":true,"data":[{"mod2_id":"2015","mod2_name":"E级 (W213)","mod2_start_year":"2017","mod2_end_year":"0"},{"mod2_id":"1914","mod2_name":"E级 (W213) 加长版","mod2_start_year":"2017","mod2_end_year":"0"},{"mod2_id":"1035","mod2_name":"GLC级 (X253)","mod2_start_year":"2016","mod2_end_year":"0"}],"code":null,"encry":0}
		   mod_level=2 mod2_id=2015
		   		{"status":true,"data":[{"mod3_id":"1790","mod3_name":"1.8 TFSI","mod3_start_year":"2009","mod3_end_year":"2016","mod3_engine":"CCUA","mod3_cc":"1798","mod3_kw":"118","mod3_transmission":"8CVT"},...],"code":null,"encry":0}
		   mod_level=3 mod3_id=7243
		  		{"status":true,"data":{"brand_id":"3","brand_etk_id":"7","brand_code":"BC","brand_name":"奔驰","make_name":"北京奔驰","mod1_name":"E级","mod2_name":"E级 (W213)","mod3_id":"7243","mod3_name":"E300","mod3_start_year":"2017","mod3_end_year":"0","mod3_kw":"180","mod3_cc":"1991","mod3_engine":"274.920","mod3_transmission":"9A\/MT","mod3_fuel":"汽油","mod3_body":"4门5座三厢车","mod3_front_brake":"通风盘式","mod3_rear_brake":"通风盘式","mod3_parking_brake":"电子驻车","mod3_drive":"前置后驱","mod3_cc_name":"2.0"},"code":null,"encry":0}

		tcd  http://192.168.1.115/paojieling/api/index.php/data/itemModelTree?proId=104020249290301&from=PJLH5&devcode=ujhniais26imf4euu15o4mkg74&mod_level=0&type=tcd&mod_type=tcd
		注意：年份有可能是 201605 到月份

			mod_level=0 
				{"status":true,"data":[{"make_id":"25","make_name":"DAIHATSU"},{"make_id":"107","make_name":"SUBARU"},{"make_id":"111","make_name":"TOYOTA"}],"code":null,"encry":0}
			mod_level=1 make_id=111
				{"status":true,"data":[{"mod2_id":"10002","mod2_name":"PASSO (_NGC3_, _KGC3_)","mod2_start_year":"2010","mod2_end_year":0}],"code":null,"encry":0}
			mod_level=2 mod2_id=10002
				{"status":true,"data":[{"mod3_id":"13938","mod3_name":"1.0","mod3_engine":"1NR-FE","mod3_kw":"51","mod3_cc":"998","mod3_cylinder":"3","mod3_start_year":"2010","mod3_end_year":0},...],"code":null,"encry":0}
			 mod_level=3 mod3_id=13938
			 	{"status":true,"data":{"make_id":"111","make_name":"TOYOTA","mod2_name":"PASSO (_NGC3_, _KGC3_)","mod3_id":"13938","mod3_name":"1.0","mod3_engine":"1KR-FE","mod3_kw":"51","mod3_cc":"998","mod3_cylinder":"3","mod3_body":"Hatchback","mod3_start_year":"2010","mod3_end_year":0},"code":null,"encry":0}
		
		aaia  http://192.168.1.115/paojieling/api/index.php/data/itemModelTree?proId=2174&from=PJLH5&devcode=ujhniais26imf4euu15o4mkg74&mod_level=0&type=aaia&mod_type=aaia
			mod_level=0
				{"status":true,"data":[{"make_id":"33","make_name":"AUDI"},{"make_id":"31","make_name":"VOLKSWAGEN"}],"code":null,"encry":0}
			mod_level=1 make_id=31
			 	{"status":true,"data":[{"mod1_id":"3361","mod1_name":"CC"},{"mod1_id":"3372","mod1_name":"EOS"},{"mod1_id":"3403","mod1_name":"PASSAT"},{"mod1_id":"3405","mod1_name":"PASSAT CC"},{"mod1_id":"3414","mod1_name":"R32"}],"code":null,"encry":0}
			mod_level=2 mod1_id=3403
			 	{"status":true,"data":[{"mod2_id":"13730","mod2_name":"3.6L V6"}],"code":null,"encry":0}
		    mod_level=3 mod2_id=13730
			 	{"status":true,"data":[{"mod3_id":"1505282","mod3_name":"2013"},{"mod3_id":"1502732","mod3_name":"2012"},{"mod3_id":"1447326","mod3_name":"2010"},{"mod3_id":"1501767","mod3_name":"2009"},{"mod3_id":"1442024","mod3_name":"2008"},{"mod3_id":"1433381","mod3_name":"2007"},{"mod3_id":"1433189","mod3_name":"2006"}],"code":null,"encry":0}
			mod_level=4 mod3_id=1505282
				{"status":true,"data":{"mod3_id":"1505282","mod3_name":"2013","partid":"6956","mod3_param":"{#19193105} \r\n Lower\r\n Upper"},"code":null,"encry":0}
	  */
	public function itemModelTree()
	{
		$Params = mvc::$URL_PARAMS;
		$type = $Params['type'];
		$mod_type = $Params['mod_type'];
		$level = (int) $Params['mod_level'];
		$proId = (int) $Params['proId'];
		$etkId = isset($Params['etkId'])? (int) $Params['etkId']: 0;
		$grp2Id = isset($Params['grp2Id'])? (int) $Params['grp2Id']: 0;
		$grp2Id = 0;
		$arr = [];
		if($proId != '' && !empty($type))
		{
			switch($type)
			{
				case 'etk':
					if(!empty(mvc::$cfg['DB']['EPC'][$etkId]) && $mod_type == 'ypc')
					{
						$EpcItem = new EpcItem($etkId);
						$res = $EpcItem->getItemMod3Id($proId, $grp2Id);
						$ypcMod3IdArr = $res['data'];
					}
				break;
				case 'pjd':
				case 'super':
					if($mod_type == 'ypc')
					{
						$B2cBase = new B2cBase();
						$res = $B2cBase->get('bc_product_model3_link', ['pml3_mod3_id', 'pml3_remark'], sprintf(" and pml3_pro_id = %d", $proId));
						$ypcMod3IdArr = array_column($res['data'], 'pml3_mod3_id');
					}
				break;
				case 'yp':
					$YpsProduct = new YpsProduct();
					if($mod_type == 'ypc')
					{
						$res = $YpsProduct->getYpcModelId($proId);
						$ypcMod3IdArr = $res['data'];
					}
					else if($mod_type == 'tcd')
					{
						switch($level)
						{
							case 0:
								$table = 'yp_tcd_model3_pro_link AS m3p
								JOIN yp_tcd_model3 AS m3 ON m3p.model3_id = m3.model3_id
								JOIN yp_make AS m ON m.make_id=m3.make_id';
								$where = sprintf(" and m3p.pid= %d GROUP BY m.make_id order by m.name", $proId);
								$mres = $YpsProduct->get($table, ['m.make_id, m.desid as mdesid, m.name AS make_name'], $where);
								if(!empty($mres['data']))
								{
									$desidArr = array_unique(array_column($mres['data'], 'mdesid'));
									$lres = $YpsProduct->getLang($desidArr, 1);
									$langArr = $lres['data'];
									foreach($mres['data'] as $v)
									{
										$v['make_name'] = !empty($langArr[$v['mdesid']])? $langArr[$v['mdesid']]: $v['make_name'];
										unset($v['mdesid']);
										$arr[] = $v;
									}
								}
							break;
							case 1:
								$table = 'yp_tcd_model3_pro_link AS m3p
								JOIN yp_tcd_model2 AS m2 ON m2.model2_id= m3p.model2_id';
								$where = sprintf(" and m3p.pid= %d and m2.make_id=%d GROUP BY m2.model2_id order by m2.name, m2.start_year desc", $proId, $Params['make_id']);
								$mres = $YpsProduct->get($table, ['m2.model2_id AS mod2_id, m2.desid as m2desid, m2.name AS mod2_name, m2.start_year as mod2_start_year, m2.end_year as mod2_end_year'], $where);
								if(!empty($mres['data']))
								{
									$desidArr = array_unique(array_column($mres['data'], 'm2desid'));
									$lres = $YpsProduct->getLang($desidArr, 1);
									$langArr = $lres['data'];
									foreach($mres['data'] as $v)
									{
										$v['mod2_name'] = !empty($langArr[$v['m2desid']])? $langArr[$v['m2desid']]: $v['make_name'];
										unset($v['m2desid']);
										$arr[] = $v;
									}
								}
							break;
							case 2:
								$table = 'yp_tcd_model3_pro_link AS m3p 
								JOIN yp_tcd_model_engine_link AS mel ON m3p.model3_id=mel.model_last_id  
								JOIN yp_tcd_engine2 AS e2 ON e2.engine2_id=mel.engine_last_id';
								$res = $YpsProduct->get($table, ['m3p.model3_id, e2.name'], sprintf(" and m3p.pid= %d and m3p.model2_id=%d order by e2.name", $proId, $Params['mod2_id']));
								$TypeEng = [];
								foreach($res['data'] as $v)
								{
									$TypeEng[$v['model3_id']][$v['name']] = $v['name'];
								}
								$table = 'yp_tcd_model3_pro_link AS m3p
								JOIN yp_tcd_model3 AS m3 ON m3p.model3_id = m3.model3_id
								LEFT JOIN yp_tcd_model3_info as m3i on m3.model3_id=m3i.model3_id';
								$where = sprintf(" and m3p.pid= %d and m3p.model2_id=%d order by m3.name desc, m3.start_year desc", $proId, $Params['mod2_id']);
								$mres = $YpsProduct->get($table, ['m3.model3_id AS mod3_id, m3.short_name AS mod3_name,"" as mod3_engine, kw as mod3_kw,cylinder as mod3_cylinder,cc as mod3_cc, m3.start_year as mod3_start_year, m3.end_year as mod3_end_year'], $where);
								$arr = $mres['data'];
								foreach($arr as $k=>$v)
								{
									$arr[$k]['mod3_engine'] = !empty($TypeEng[$v['mod3_id']])? implode(';', $TypeEng[$v['mod3_id']]): '';
								}
							break;
							case 3:
								$table = 'yp_tcd_model3_pro_link AS m3p 
								JOIN yp_tcd_model_engine_link AS mel ON m3p.model3_id=mel.model_last_id  
								JOIN yp_tcd_engine2 AS e2 ON e2.engine2_id=mel.engine_last_id';
								$res = $YpsProduct->get($table, ['distinct e2.name'], sprintf(" and m3p.pid= %d and m3p.model3_id=%d order by e2.name", $proId, $Params['mod3_id']));
								$engines = implode(';', array_column($res['data'], 'name'));
								$table = 'yp_tcd_model3_pro_link AS m3p
								JOIN yp_tcd_model3 AS m3 ON m3p.model3_id = m3.model3_id
								LEFT JOIN yp_tcd_model3_info as m3i on m3.model3_id=m3i.model3_id
								join yp_tcd_model2 as m2 on m2.model2_id= m3.model2_id
								join yp_make as m on m.make_id=m3.make_id';
								$where = sprintf(" and m3p.pid= %d and m3p.model3_id=%d", $proId, $Params['mod3_id']);
								$mres = $YpsProduct->get($table, ['m.make_id, m.desid as mdesid, m2.desid as m2desid, m3.model3_id AS mod3_id, m3.short_name AS mod3_name,"" as mod3_engine, kw as mod3_kw,cylinder as mod3_cylinder,cc as mod3_cc,m3.style as mod3_body, m3.start_year as mod3_start_year, m3.end_year as mod3_end_year'], $where, true);
								$arr = $mres['data'];
								if(!empty($arr))
								{
									$desidArr[] = $arr['mdesid'];
									$desidArr[] = $arr['m2desid'];
									$lres = $YpsProduct->getLang($desidArr, 1);
									$langArr = $lres['data'];
									$arr['make_name'] = $langArr[$arr['mdesid']];
									$arr['mod2_name'] = $langArr[$arr['m2desid']];
									unset($arr['mdesid']);unset($arr['m2desid']);
									$arr['mod3_engine'] = $engines;
								}
							break;
						}
						
					}
					else if($mod_type == 'aaia')
					{
						$res = $YpsProduct->get('yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid', ['f.factory, n.format'], sprintf(" and n.pid = %d and n.ismain=1 limit 1", $proId), true);
						$mainNum = ['factory'=>$res['data']['factory'], 'display'=>$res['data']['format']];
					}
				break;
				case 'tcdcn':
				case 'tcd':
					$TcdmArt = new TcdmArt();
					if($mod_type == 'ypc')
					{
						$res = $TcdmArt->getYpcModelId($proId);
						$ypcMod3IdArr = $res['data'];
					}
					else if($mod_type == 'aaia')
					{
						$res = $TcdmArt->get('tof_articles join tof_suppliers on sup_id = art_sup_id', ['art_number display, sup_brand factory'], sprintf(" and art_id = %d", $proId), true);
						$mainNum = $res['data'];
					}
				break;
				case 'aaia':
					$AaiaProduct = new AaiaProduct();
					if($mod_type == 'ypc')
					{
						$res = $AaiaProduct->get('yp_number as n join yp_factory as f on n.fid=f.fid', ['factory, display'], sprintf(" and pid = %d and f.fid=770", $proId));
						$oeNumArr = array_column($res['data'], 'format');
					}
					else if($mod_type == 'tcd')
					{
						$res = $AaiaProduct->get('yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid', ['f.factory, n.format'], sprintf(" and n.pid = %d and n.ismain=1 limit 1", $proId), true);
						$mainNum = ['factory'=>$res['data']['factory'] == 'OEM'? 'OE': $res['data']['factory'], 'display'=>$res['data']['format']];
					}
				break;
			}

			//ypc国内车型
			if($mod_type == 'ypc' && empty($ypcMod3IdArr) && !empty($oeNumArr))
			{
				$YpcModel = new YpcModel();
				$res = $YpcModel->getM3idByNum($oeNumArr);
				$ypcMod3IdArr = $res['data'];
			}
			if($mod_type == 'ypc' && !empty($ypcMod3IdArr))
			{
				$Ypc2Base = new Ypc2Base();
				switch($level)
				{
					case 0:
						$where = sprintf(" and mod3_id in (%s) group by brand_id, make_id order by brand_seq, brand_initial, make_seq, make_initial", implode(',', $ypcMod3IdArr));
						$res = $Ypc2Base->get('bc_model3 join bc_make on make_id=mod3_make_id join bc_brand on mod3_brand_id=brand_id', ['brand_id', 'make_show_brand', 'brand_name', 'make_id', 'make_name'], $where);
						$arr = $res['data'];
					break;
					case 1:
						$where = sprintf(" and mod2_id in (select distinct mod3_mod2_id from bc_model3 where mod3_id in (%s) and mod3_brand_id=%d and mod3_make_id=%d) order by mod2_name, mod2_start_year desc", implode(',', $ypcMod3IdArr), $Params['brand_id'], $Params['make_id']);
						$res = $Ypc2Base->get('bc_model2', ['mod2_id, mod2_name, mod2_start_year, mod2_end_year'], $where);
						$arr = $res['data'];
					break;
					case 2:
						$where = sprintf(" and mod3_id in (%s) and mod3_mod2_id=%d order by mod3_name, mod3_start_year desc", implode(',', $ypcMod3IdArr), $Params['mod2_id']);
						$res = $Ypc2Base->get('bc_model3', ['mod3_id', 'mod3_name', 'mod3_start_year', 'mod3_end_year', 'mod3_engine', 'mod3_kw', 'mod3_cc', 'mod3_transmission', 'mod3_fuel', 'mod3_drive','mod3_body','mod3_front_brake','mod3_rear_brake','mod3_parking_brake', 'mod3_start_stop', 'mod3_chassis', "'' as mod3_para"], $where);
						$arr = array_column($res['data'], null, 'mod3_id');
						//产品的车型参数
						if(in_array($type, ['pjd','super']) && $arr)
						{
							$modelPara = [];
							$where = sprintf(" and pml3_pro_id = %d and pml3_mod3_id in (%s) and pml3_remark != '' group by pml3_mod3_id, pml3_remark", $proId, implode(',', array_keys($arr)));
							$res = $B2cBase->get('bc_product_model3_link', ['pml3_mod3_id', 'pml3_remark'], $where);
							foreach($res['data'] as $pv)
							{
								$modelPara[$pv['pml3_mod3_id']][] = $pv['pml3_remark'];
							}

							$where = sprintf(" and pmp_pro_id = %d and pmp_mod3_id in (%s) and pmp_info != '' group by pmp_mod3_id, pmp_info", $proId, implode(',', array_keys($arr)));
							$res = $B2cBase->get('bc_product_model3_para', ['pmp_mod3_id', 'pmp_info'], $where);
							foreach($res['data'] as $pv)
							{
								$modelPara[$pv['pmp_mod3_id']][] = $pv['pmp_info'];
							}
							foreach($modelPara as $pk=>$pv)
							{
								$arr[$pk]['mod3_para'] = implode(';', array_unique($pv));
							}
						}
						$arr = array_values($arr);
					break;
					case 3:
						$Ypc2Model = new Ypc2Model();
						$where = sprintf(" and mod3_id=%d", $Params['mod3_id']);
						$res = $Ypc2Model->getModel(3, ["brand_id, brand_etk_id, brand_code, brand_name, make_show_brand, make_name, mod1_name, mod2_name, bc_model3.*, '' as mod3_para"], $where, true);
						$arr = $res['data'];

						//产品的车型参数
						if(in_array($type, ['pjd','super']) && $arr)
						{
							$where = sprintf(" and pml3_pro_id =%d and pml3_mod3_id =%d and pml3_remark != '' group by pml3_remark", $proId, $Params['mod3_id']);
							$res = $B2cBase->get('bc_product_model3_link', ['pml3_remark'], $where);
							$modelPara = array_column($res['data'], 'pml3_remark');

							$where = sprintf(" and pmp_pro_id =%d and pmp_mod3_id =%d and pmp_info != '' group by pmp_info", $proId, $Params['mod3_id']);
							$res = $B2cBase->get('bc_product_model3_para', ['pmp_pro_id', 'pmp_info'], $where);
							$modelPara = array_merge($modelPara, array_column($res['data'], 'pml3_remark'));
							$arr['mod3_para'] = implode(';', array_unique($modelPara));
						}
						unset($arr['mod3_keyword']);
						unset($arr['mod3_brand_id']);
						unset($arr['mod3_make_id']);
						unset($arr['mod3_mod2_id']);
						unset($arr['mod3_mod1_id']);
						unset($arr['mod3_engine_format']);
						unset($arr['mod3_imgs']);
						unset($arr['mod3_drive_short']);
					break;
				}
			}

			//tcd 
			if($mod_type == 'tcd' && $type != 'yp')
			{
				$tcdProId = $proId;
				if(stripos($type, 'tcd') === false)
				{
					$tcdProId = 0;
					if(!empty($mainNum['factory']) && !empty($mainNum['format']))
					{
						$TcdmArt = new TcdmArt();
						$res = $TcdmArt->getArtIdByFacNumber($mainNum['format'], $mainNum['factory']);
						if(!empty($res['data']['pro_id'])) $tcdProId = $res['data']['pro_id'];
					}
				}
				$TcdmArt = new TcdmArt();
				switch($level)
				{
					case 0:
						$table = 'tof_article_types 
						join tof_types on typ_id = lat_typ_id
						join tof_manufacturers on mfa_id = typ_mfa_id';
						$where = sprintf(" and lat_art_id=%d group by mfa_id order by mfa_brand", $tcdProId);
						$res = $TcdmArt->get($table, ['mfa_brand, mfa_brand_cn, mfa_id'], $where);
						foreach($res['data'] as $v)
						{
							$arr[] = ['make_id'=>$v['mfa_id'], 'make_name'=>mvc::$cfg['dLang'] != 'en'? $v['mfa_brand_cn']: $v['mfa_brand']];
						}
					break;
					case 1:
						$table = 'tof_article_types 
						join tof_types on typ_id = lat_typ_id
						join tof_models on mod_id = typ_mod_id';
						$where = sprintf(" and lat_art_id=%d and typ_mfa_id=%d group by mod_id order by mod_name, mod_start desc", $tcdProId, $Params['make_id']);
						$res = $TcdmArt->get($table, ['mod_id, mod_name, mod_start, mod_end'], $where);
						foreach($res['data'] as $v)
						{
							$arr[] = [
								'mod2_id'=>$v['mod_id'], 
								'mod2_name'=>$v['mod_name'],
								'mod2_start_year'=>$v['mod_start'],
								'mod2_end_year'=>$v['mod_end']
							];
						}
					break;
					case 2:
						$table = 'tof_article_types 
						join tof_types on typ_id = lat_typ_id';
						$where = sprintf(" and lat_art_id=%d and typ_mod_id=%d order by typ_name, typ_start desc", $tcdProId, $Params['mod2_id']);
						$res = $TcdmArt->get($table, ["typ_id, typ_name, typ_cc, typ_kw, typ_body, typ_cylinder, typ_start, typ_end, typ_engine"], $where);
						foreach($res['data'] as $v)
						{
							$arr[] = [
								'mod3_id'=>$v['typ_id'], 
								'mod3_name'=>$v['typ_name'],
								'mod3_engine'=>$v['typ_engine'],
								'mod3_kw'=>$v['typ_kw'],
								'mod3_cc'=>$v['typ_cc'],
								'mod3_cylinder'=>$v['typ_cylinder'],
								'mod3_start_year'=>$v['typ_start'],
								'mod3_end_year'=>$v['typ_end']
							];
						}
					break;
					case 3:
						$table = 'tof_article_types 
						join tof_types on typ_id = lat_typ_id
						join tof_models on mod_id = typ_mod_id
						join tof_manufacturers on mfa_id = mod_mfa_id
						left join tof_types_country on tco_typ_id=typ_id
						left join tof_country on tco_cou_id=cou_id and cou_isarea = 0';
						$where = sprintf(" and lat_art_id =%d and lat_typ_id=%d", $tcdProId, $Params['mod3_id']);
						$filed = ["mfa_brand, mfa_id, mod_name,typ_id, typ_name, typ_cc, typ_kw, typ_body, typ_cylinder, typ_start, typ_end, typ_engine,typ_drive,typ_fuel,typ_hp,typ_type,typ_id_org,group_concat(distinct cou_code) cou_codes"];
						$res = $TcdmArt->get($table, $filed, $where, true);
						if(!empty($res['data']))
						{
							$v = $res['data'];
							$arr = [
								'make_id'=>$v['mfa_id'],
								'make_name'=>$v['mfa_brand'],
								'mod2_name'=>$v['mod_name'],
								'mod3_id'=>$v['typ_id'], 
								'mod3_name'=>$v['typ_name'],
								'mod3_engine'=>$v['typ_engine'],
								'mod3_kw'=>$v['typ_kw'],
								'mod3_cc'=>$v['typ_cc'],
								'mod3_cylinder'=>$v['typ_cylinder'],
								'mod3_body'=>$v['typ_body'],
								'mod3_drive'=>$v['typ_drive'],
								'mod3_fuel'=>$v['typ_fuel'],
								'mod3_hp'=>$v['typ_hp'],
								'mod3_start_year'=>$v['typ_start'],
								'mod3_end_year'=>$v['typ_end'],
								'mod3_typ_type'=>$v['typ_type'],
								'mod3_typ_id_org'=>$v['typ_id_org'],
								'mod3_cou_codes'=>$v['cou_codes'],
							];
						}
					break;
				}
			}

			//aaia
			if($mod_type == 'aaia')
			{
				//找到产品ID
				$aaiaProId = $proId;
				$AaiaBase = new AaiaBase();
				if($type != 'aaia')
				{
					$aaiaProId = 0;
					if(!empty($mainNum['factory']) && !empty($mainNum['format']))
					{
						$where = sprintf(" and f.factory = '%s' and n.format='%s' and n.ismain=1 limit 1", LibFc::Escape($mainNum['factory']), LibFc::Escape(LibFc::FormatNum($mainNum['format'])));
						$res = $AaiaBase->get('yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid', ['pid'], $where, true);
						if(!empty($res['data'])) $aaiaProId = $res['data']['pid'];
					}
				}
				if($aaiaProId > 0)
				{
					switch($level)
					{
						case 0:
							$table = 'yp_aaia_model3_pro_link AS m3p
							JOIN yp_aaia_model3 AS m3 ON m3p.model3_id = m3.model3_id
							JOIN yp_make AS m ON m.make_id=m3.make_id';
							$where = sprintf(" and m3p.pid= %d GROUP BY m.make_id order by m.name", $aaiaProId);
							$res = $AaiaBase->get($table, ['m.make_id, m.name AS make_name'], $where);
							$arr = $res['data'];
						break;
						case 1:
							$table = 'yp_aaia_model3_pro_link AS m3p
							JOIN yp_aaia_model1 AS m1 ON m1.model1_id= m3p.model1_id';
							$where = sprintf(" and m3p.pid= %d and m1.make_id=%d GROUP BY m1.model1_id order by m1.name, m1.start_year desc", $aaiaProId, $Params['make_id']);
							$res = $AaiaBase->get($table, ['m1.model1_id AS mod1_id, m1.name AS mod1_name'], $where);
							$arr = $res['data'];
						break;
						case 2:
							$table = 'yp_aaia_model3_pro_link AS m3p
							JOIN yp_aaia_model2 AS m2 ON m2.model2_id= m3p.model2_id';
							$where = sprintf(" and m3p.pid= %d and m3p.model1_id=%d GROUP BY m2.model2_id order by m2.name, m2.start_year desc", $aaiaProId, $Params['mod1_id']);
							$res = $AaiaBase->get($table, ['m2.model2_id AS mod2_id, m2.name AS mod2_name'], $where);
							$arr = $res['data'];
						break;
						case 3:
							$table = 'yp_aaia_model3_pro_link AS m3p
							JOIN yp_aaia_model3 AS m3 ON m3p.model3_id = m3.model3_id';
							$where = sprintf(" and m3p.pid= %d and m3p.model2_id=%d order by m3.name desc", $aaiaProId, $Params['mod2_id']);
							$res = $AaiaBase->get($table, ['m3.model3_id AS mod3_id, m3.name AS mod3_name'], $where);
							$arr = $res['data'];
						break;
						case 4:
							$res = $AaiaBase->get('yp_aaia_model3_pro_link AS m3p JOIN yp_aaia_model3 AS m3 ON m3p.model3_id = m3.model3_id', ['m3.model3_id AS mod3_id, m3.name AS mod3_name, m3p.partid'], sprintf(" and m3p.pid=%d and m3p.model3_id= %d limit 1", $aaiaProId, $Params['mod3_id']), true);
							$arr = $res['data'];
							if(!empty($arr))
								$arr['mod3_param'] = '';
							if(!empty($arr['partid']))
							{
								$res = $AaiaBase->get('ypdb_aaia_param.param_'.$arr['partid'], ['param as mod3_param'], sprintf(" and pid=%d and carno= %d", $aaiaProId, $arr['mod3_id']), true);
								$arr = array_merge($arr, $res['data']);
							}
							$re = ['<br>', '<br/>', '<br />'];
							$arr['mod3_param'] = strip_tags(str_replace($re, "\r\n", $arr['mod3_param']));
						break;
					}
				}
			}
			
		}
		echo json_encode(['status'=>true, 'data'=>$arr]);
	}
	

	/**
	  * @method 配件适用车型（分页）
	  * @http  get
	  * @param  type  类型etk|pjd|yp|tcd|super
	  * @param  proId 产品ID
	  * @param  etkId  [可选]type = etk
	  * @param  grp2Id [可选]子零件ID type = etk
	  * @author soul 
	  * @copyright 2018/7/26
	  * @return json
			 {"status":true,"data":{
				 "ypc":[{"mod3_id":"10","brand_id":"3","brand_name":"奔驰","make_name":"北京奔驰","mod2_name":"C级 (W204)","mod3_name":"C280","mod3_start_year":"2007","mod3_end_year":"2009"},...],
				 "tcd":[ {"make_id": "553", "make_name": "MERCEDES-BENZ", "mod2_name": "C-CLASS (W204)", "mod3_name": "C 350 CGI (204.065)", "mod3_start_year": "200809", "mod3_end_year": "201401"},...],
				 "aaia":[]},"code":null}
			 {"status":false, "data": 错误信息, "code":"错误代码"}
	  */
	public function itemModel()
	{
		$modelArr = ['ypc'=>[], 'tcd'=>[], 'aaia'=>[]];
		$proId = $_GET['proId'];
		$etkId = (int) $_GET['etkId'];
		$grp2Id = (int) $_GET['grp2Id'];
		$grp2Id = 0;
		if($proId != '' && !empty($_GET['type']))
		{
			switch($_GET['type'])
			{
				case 'etk':
					$proId = (int) $proId;
					if(!empty(mvc::$cfg['DB']['EPC'][$etkId]))
					{
						$EpcItem = new EpcItem($etkId);
						$res = $EpcItem->getItemMod3Id($proId, $grp2Id);
						$cofArr = ['type'=>'page', 'page'=>1, 'per'=>500];
						$Ypc2Model = new Ypc2Model();
						$res = $Ypc2Model->getModelListByMod3Ids($res['data'], $cofArr);
						$modelArr['ypc'] = $res['data'];
					}
				break;
				case 'pjd':
				case 'super':
					$B2cBase = new B2cBase();
					$res = $B2cBase->get('bc_product_model3_link', ['pml3_mod3_id', 'pml3_pro_id'], sprintf(" and pml3_pro_id = %d", $proId));
					$mod3IdArr = array_column($res['data'], 'pml3_mod3_id');
				break;
				case 'yp':
					$YpsProduct = new YpsProduct();
					$res = $YpsProduct->getYpcModelId($proId);
					$mod3IdArr = $res['data'];

					$res = $YpsProduct->getItemModel($proId);
					$modelArr['tcd'] = $res['data'];
					
					//主号码 查询tcd、aaia
					$res = $YpsProduct->get('yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid', ['f.factory, n.format'], sprintf(" and n.pid = %d and n.ismain=1 limit 1", $proId), true);
					if(!empty($res['data']))
					{
						$mainNum = $res['data'];
						if(empty($modelArr['tcd']))
						{
							$TcdmArt = new TcdmArt();
							$res = $TcdmArt->getModelByFacNumber($mainNum['format'], $mainNum['factory']);
							$modelArr['tcd'] = $res['data'];
						}
						//aaia
						$AaiaProduct = new AaiaProduct();
						$where = sprintf(" and f.factory = '%s' and n.format='%s' and n.ismain=1 limit 1", LibFc::Escape($mainNum['factory']), LibFc::Escape($mainNum['format']));
						$res = $AaiaProduct->get('yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid', ['pid'], $where, true);
						if(!empty($res['data']))
						{
							$res = $AaiaProduct->getItemModel($res['data']['pid']);
							$modelArr['aaia'] = $res['data'];
						}
					}
				break;
				case 'tcd':
					$TcdmArt = new TcdmArt();
					$res = $TcdmArt->getYpcModelId($proId);
					$mod3IdArr = $res['data'];

					$res = $TcdmArt->getItemModel($proId);
					$modelArr['tcd'] = $res['data'];

					$res = $TcdmArt->get('tof_articles join tof_suppliers on sup_id = art_sup_id', ['art_number, sup_brand'], sprintf(" and art_id = %d", $proId), true);
					$mainNum = $res['data'];
					if(!empty($mainNum))
					{
						$AaiaProduct = new AaiaProduct();
						$where = sprintf(" and f.factory = '%s' and n.format='%s' and n.ismain=1 limit 1", LibFc::Escape($mainNum['sup_brand']), LibFc::FormatNum($mainNum['art_number']));
						$res = $AaiaProduct->get('yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid', ['pid'], $where, true);
						if(!empty($res['data']))
						{
							$res = $AaiaProduct->getItemModel($res['data']['pid']);
							$modelArr['aaia'] = $res['data'];
						}
					}
				break;
				case 'aaia':
					$AaiaProduct = new AaiaProduct();
					$res = $AaiaProduct->get('yp_number', ['format'], sprintf(" and pid = %d and fid=770", $proId));
					if(!empty($res['data']))
					{
						$YpcModel = new YpcModel();
						$res = $YpcModel->getM3idByNum(array_column($res['data'], 'format'));
						$mod3IdArr = $res['data'];
					}

					//主号码 查询tcd
					$res = $AaiaProduct->get('yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid', ['f.factory, n.format'], sprintf(" and n.pid = %d and n.ismain=1 limit 1", $proId), true);
					if(!empty($res['data']))
					{
						$mainNum = $res['data'];
						$mainNum['factory'] = $mainNum['factory'] == 'OEM'? 'OE': $mainNum['factory'];
						$TcdmArt = new TcdmArt();
						$res = $TcdmArt->getModelByFacNumber($mainNum['format'], $mainNum['factory']);
						$modelArr['tcd'] = $res['data'];
					}

					$res = $AaiaProduct->getItemModel($proId);
					$modelArr['aaia'] = $res['data'];
				break;
			}

			if(!empty($mod3IdArr))
			{
				$Ypc2Model = new Ypc2Model();
				$where = sprintf(" and mod3_id in (%s) order by brand_seq, brand_initial, make_seq, make_initial, mod1_name, mod2_name, mod3_name limit 500", implode(',', $mod3IdArr));
				$filed = ['brand_id, brand_name, make_name, mod2_name, mod2_start_year, mod2_end_year, mod3_id, mod3_name, mod3_start_year, mod3_end_year, mod3_engine, mod3_kw, mod3_cc, mod3_transmission, mod3_fuel, mod3_body, mod3_front_brake, mod3_rear_brake, mod3_parking_brake, mod3_start_stop, mod3_chassis, mod3_drive'];
				$res = $Ypc2Model->getModel(3, $filed, $where);
				$modelArr['ypc'] = $res['data'];
			}
		}
		echo json_encode(['status'=>true, 'data'=>$modelArr]);
	}



	/**
	  * @method 配件适用发动机
	  * @http  get
	  * @param  type  类型 pjd|tcd|super
	  * @param  proId 产品ID
	  * @author soul 
	  * @copyright 2019/6/28
	  * @return  
	  pjd
		{"status":true,"data":[{"brand_name":"\u5965\u8fea","engine":"CADA","fuel":"\u6c7d\u6cb9","kw":"155","cc":"1984"},{"brand_name":"\u5965\u8fea","engine":"CAEB","fuel":"\u6c7d\u6cb9","kw":"155","cc":"1984"},{"brand_name":"\u5965\u8fea","engine":"CAKA","fuel":"\u6c7d\u6cb9","kw":"245","cc":"2995"},{"brand_name":"\u5965\u8fea","engine":"CALA","fuel":"\u6c7d\u6cb9","kw":"195","cc":"3197"},{"brand_name":"\u5965\u8fea","engine":"CALB","fuel":"\u6c7d\u6cb9","kw":"199","cc":"3197"},{"brand_name":"\u5965\u8fea","engine":"CAUA","fuel":"\u6c7d\u6cb9","kw":"260","cc":"4163"},{"brand_name":"\u5965\u8fea","engine":"CDNB","fuel":"\u6c7d\u6cb9","kw":"132","cc":"1984"},{"brand_name":"\u5965\u8fea","engine":"CDNC","fuel":"\u6c7d\u6cb9","kw":"155","cc":"1984"},{"brand_name":"\u5965\u8fea","engine":"CDTA","fuel":"\u6c7d\u6cb9","kw":"245","cc":"2995"},{"brand_name":"\u5965\u8fea","engine":"CDZA","fuel":"\u6c7d\u6cb9","kw":"132","cc":"1984"},{"brand_name":"\u5965\u8fea","engine":"CGWC","fuel":"\u6c7d\u6cb9","kw":"245","cc":"2995"},{"brand_name":"\u5965\u8fea","engine":"CHJA","fuel":"\u6cb9\u7535\u6df7\u5408","kw":"155","cc":"1984"},{"brand_name":"\u5965\u8fea","engine":"CHMB","fuel":"\u6c7d\u6cb9","kw":"200","cc":"2995"},{"brand_name":"\u5965\u8fea","engine":"CLXA","fuel":"\u6c7d\u6cb9","kw":"140","cc":"2498"},{"brand_name":"\u5965\u8fea","engine":"CLXB","fuel":"\u6c7d\u6cb9","kw":"150","cc":"2498"},{"brand_name":"\u5965\u8fea","engine":"CMUA","fuel":"\u6c7d\u6cb9","kw":"200","cc":"2995"},{"brand_name":"\u5965\u8fea","engine":"CNCD","fuel":"\u6c7d\u6cb9","kw":"165","cc":"1984"},{"brand_name":"\u5965\u8fea","engine":"CNCE","fuel":"\u6c7d\u6cb9","kw":"169","cc":"1984"},{"brand_name":"\u5965\u8fea","engine":"CNYB","fuel":"\u6c7d\u6cb9","kw":"162","cc":"2773"},{"brand_name":"\u5965\u8fea","engine":"CTDA","fuel":"\u6c7d\u6cb9","kw":"245","cc":"2995"},{"brand_name":"\u5965\u8fea","engine":"CUHA","fuel":"\u6c7d\u6cb9","kw":"165","cc":"1984"},{"brand_name":"\u5965\u8fea","engine":"CYYA","fuel":"\u6c7d\u6cb9","kw":"140","cc":"1798"}]}
	tcd
	  {"status":true,"data":[{"brand_name":"BMW","engine":"M10 B16 (164VA)","cylinders":"4","kw":"55~77","hp":"75~105","cc":"1573"},{"brand_name":"BMW","engine":"M10 B16 (164VB)","cylinders":"4","kw":"55~77","hp":"75~105","cc":"1573"},{"brand_name":"BMW","engine":"M10 B18 (184EB)","cylinders":"4","kw":"66~77","hp":"90~105","cc":"1766"},{"brand_name":"BMW","engine":"M10 B18 (184VA)","cylinders":"4","kw":"66~77","hp":"90~105","cc":"1766"},{"brand_name":"BMW","engine":"M10 B18 (184VB)","cylinders":"4","kw":"66~77","hp":"90~105","cc":"1766"},{"brand_name":"BMW","engine":"M10 B18 (184VC)","cylinders":"4","kw":"66~77","hp":"90~105","cc":"1766"},{"brand_name":"BMW","engine":"M10 B20","cylinders":"4","kw":"74~101","hp":"101~137","cc":"1990"},{"brand_name":"BMW","engine":"M20 B20 (206VA)","cylinders":"6","kw":"90~110","hp":"122~150","cc":"1990"},{"brand_name":"BMW","engine":"M20 B23 (236EA)","cylinders":"6","kw":"102~110","hp":"139~150","cc":"2316"}]}
	  */
	public function itemEngine()
	{
		$arr = [];
		$proId = $_GET['proId'];
		if($proId != '' && !empty($_GET['type']))
		{
			switch($_GET['type'])
			{
				case 'super':
				case 'pjd':
					$B2cBase = new B2cBase();
					$res = $B2cBase->get('bc_product_model3_link', ['pml3_mod3_id'], sprintf(" and pml3_pro_id = %d", $proId));
					if(!empty($res['data']))
					{
						$Ypc2Model = new Ypc2Model();
						$mod3IdArr = array_column($res['data'], 'pml3_mod3_id');
						$where = sprintf(" and mod3_id in(%s) group by mod3_engine order by max(brand_initial), mod3_engine", implode(',', array_column($res['data'], 'pml3_mod3_id')));
						$res = $Ypc2Model->getModel(3, ['max(brand_name) brand_name', 'mod3_engine engine', 'max(mod3_fuel) fuel', 'max(mod3_kw) kw','max(mod3_cc) cc'], $where);
						$arr = $res['data'];
					}
				break;
				case 'tcd':
					$TcdmArt = new TcdmArt();
					$where = sprintf(" and eng_id in (select distinct lte_eng_id from tof_article_types join tof_types_engines on lat_typ_id = lte_typ_id where lat_art_id = %d
					) order by mfa_brand, eng_code_format", $proId);
					$filed = ['eng_id, mfa_brand, eng_code, eng_cylinders, eng_kw_from, eng_kw_upto,eng_hp_from, eng_hp_upto, eng_ccm_from, eng_ccm_upto'];
					$res = $TcdmArt->get('tof_engines join tof_manufacturers on mfa_id = eng_mfa_id', $filed, $where);
					foreach($res['data'] as $v)
					{
						$arr[] = [
							'brand_name'=>$v['mfa_brand'],
							'engine'=>$v['eng_code'],
							'cylinders'=>$v['eng_cylinders'],
							'kw'=>!empty($v['eng_kw_upto']) && $v['eng_kw_from'] != $v['eng_kw_upto']? $v['eng_kw_from'].'~'.$v['eng_kw_upto']: $v['eng_kw_from'],
							'hp'=>!empty($v['eng_hp_upto']) && $v['eng_hp_from'] != $v['eng_hp_upto']? $v['eng_hp_from'].'~'.$v['eng_hp_upto']: $v['eng_hp_from'],
							'cc'=>!empty($v['eng_ccm_upto']) &&  $v['eng_ccm_from'] != $v['eng_ccm_upto']? $v['eng_ccm_from'].'-'.$v['eng_ccm_upto']: $v['eng_ccm_from'],
						];
					}
				break;
			}
		}
		echo json_encode(['status'=>true, 'data'=>$arr]);
	}



	/**
	  * @method get  通过OE号码获取价格
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  word			   string [必填] 号码 多个逗号分隔
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data":[{"pri_id":"1760921","pri_brand":"\u5954\u9a70","pri_number":"2129000319","pri_name":"CONTROL UNIT","pri_remark":"","price":[{"pie_date":"2018-01-20","pie_name":"\u5efa\u8bae\u96f6\u552e\u4ef7","pie_money":"32691.40","pie_unit":""},{"pie_date":"2018-01-20","pie_name":"\u7ecf\u9500\u5546\u4ef7\u683c","pie_money":"10461.20","pie_unit":""}]},{"pri_id":"1760923","pri_brand":"\u5954\u9a70","pri_number":"46368063039045","pri_name":"INSTRUMENT PANEL","pri_remark":"","price":[{"pie_date":"2018-01-20","pie_name":"\u5efa\u8bae\u96f6\u552e\u4ef7","pie_money":"108427.00","pie_unit":""},{"pie_date":"2018-01-20","pie_name":"\u7ecf\u9500\u5546\u4ef7\u683c","pie_money":"41635.90","pie_unit":""}]}]}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function getPriceByNumber()
	{
		$arr = [];
		$Params = mvc::$URL_PARAMS;
		$numArr = [];
		foreach(explode(',', $Params['word']) as $v)
		{
			$t = LibFc::FormatNum($v);
			if(strlen($t) > 2)
			{
				$reNum = ['I'=>'1', 'O'=>'0', 'i'=>'1', 'o'=>'0'];
				$v = strtr(LibFc::FormatNum($v), $reNum);
				$numArr[$v] = $v;
				if(substr($v, 0, 1) == 'A')
				{
					$v = substr($v, 1);
					$numArr[$v] = $v;
				}
			}
		}
		if(!empty($numArr))
		{
			$EtkBasic = new EtkBasic();
			$where  = sprintf(" and pri_format in ('%s') AND pie_money >= 0.1 group by pri_id, pie_name, pie_money", implode("','", $numArr));
			$filed = ["pri_id, pri_brand, pri_number, pri_name, pri_remark, pie_date, pie_name, pie_money, pie_unit, brand_id_ypc"];
			$res = $EtkBasic->get('pri_product join pri_price on pie_pri_id=pri_id', $filed, $where);
			foreach($res['data'] as $v)
			{
				if(!isset($arr[$v['pri_id']]))
				{
					$arr[$v['pri_id']] = [
						'pri_id'=>$v['pri_id'],	
						'pri_brand'=>$v['pri_brand'],	
						'brand_id_ypc'=>$v['brand_id_ypc'],	
						'pri_number'=>$v['pri_number'],
						'pri_name'=>$v['pri_name'],
						'pri_remark'=>$v['pri_remark']
					];
				}
				$arr[$v['pri_id']]['price'][] = [
					'pie_date'=>!empty($v['pie_date'])? date('Y-m', strtotime($v['pie_date'])): '',
					'pie_name'=>$v['pie_name'],
					'pie_money'=>sprintf('%.2f', $v['pie_money']),
					'pie_unit'=>$v['pie_unit']
				];
			}
			$arr = array_values($arr);
		}
		echo json_encode(['status'=>true, 'data'=>$arr]);
	}


	/**
	  * @method get  搜索车型库的号码
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  nums			   array [必填] [['num_id'=>1, part_id=>34, 'fac_id'=>1, 'num_format'=>''], ....]
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function ypcNumberSearch()
	{
		$proArr = [];
		$YpcBase = new YpcBase();
		
		$mod3Id = empty($_POST['mod3Id']) ? (int) $_POST['mod3_id']: (int) $_POST['mod3Id'];
		$typeProArr = [];
		$tempProArr = [];
		foreach($_POST['nums'] as $v)
		{
			if(empty($v['num_id']) || empty($v['part_id']) || empty($v['fac_id']) || empty($v['num_format'])) continue;
			$numId = (int) $v['num_id'];
			$where = sprintf(" and nm_num_fac_id=%d and nm_num_format='%s' and nm_num_part_id=%d order by nm_seq",$v['fac_id'], LibFc::FormatNum($v['num_format']), $v['part_id']);
			$res = $YpcBase->Get('yc_number_match', ['nm_type', 'nm_pro_id'], $where);
			if(!empty($res['data']))
			{
				$tempProArr[$numId] = $res['data'];
				foreach($res['data'] as $vv)
				{
					$typeProArr[$vv['nm_type']][$vv['nm_pro_id']] = $vv['nm_pro_id'];
				}
			}
		}
		
		$allProArr = [];
		$dataType = ['para', 'picAll'];
		foreach($typeProArr as $type=>$proIdArr)
		{
			switch($type)
			{
				case 'yp':
					$YpsProduct = new YpsProduct();
					$res = $YpsProduct->getAnyProductInfo($proIdArr, $dataType);
					$allProArr[$type] = $res['data'];
				break;
				case 'tcd':
					$TcdmArt = new TcdmArt();
					$res = $TcdmArt->getProductInfo($proIdArr, $dataType, $mod3Id);
					$allProArr[$type] = $res['data'];
				break;
				case 'aaia':
					$AaiaProduct = new AaiaProduct();
					$res = $AaiaProduct->getAnyProductInfo($proIdArr, $dataType);
					$allProArr[$type] = $res['data'];
				break;
				case 'zhipei':
					$OthProduct = new OthProduct();
					$res = $OthProduct->getZhipeiAnyProductInfo($proIdArr, $dataType);
					$allProArr[$type] = $res['data'];
				break;
				default:
					if(stripos($type, 'oth') === 0)
					{
						$OthProduct = new OthProduct();
						$res = $OthProduct->getOthSiteAnyProductInfo($type, $proIdArr, $dataType);
						$allProArr[$type] = $res['data'];
					}
				break;
			}
		}

		foreach($tempProArr as $numId=>$tv)
		{
			foreach($tv as $tvv)
			{
				if(!empty($allProArr[$tvv['nm_type']][$tvv['nm_pro_id']]))
				{
					$tpro = $allProArr[$tvv['nm_type']][$tvv['nm_pro_id']];
					$tpro['type'] = $tvv['nm_type'];
					$proArr[$numId][] = $tpro;
				}
			}
		}
		LibFc::ReturnJson(true, $proArr);
	}


	/**
	  * @method 第三方数据和宜配某产品的号码比较，返回相同的OE
	  * @http   get
	  * @param  token      string [必填] 调用接口凭证 (post|get)
	  * @param  type	   string [必填] tcd|aaia|zhipei|...
	  * @param  proId      string [必填] 第三方数据产品ID
	  * @param  ypProId    string [必填] 宜配产品ID
	  * @author soul
	  * @copyright 2019/5/27
	  * @return 
	  */
	public function getOthToYpSameOe()
	{
		$arr = [];
		$Params = mvc::$URL_PARAMS;
		$ypProId = (int) $Params['ypProId'];
		if($ypProId < 1 || empty($Params['type']) || empty($Params['proId'])) LibFc::ReturnJson(true, $arr);
		$proId = (int) $Params['proId'];
		$type = $Params['type'];

		//获取宜配的所有OE
		$YpsBase = new YpsBase();
		$res = $YpsBase->get('yp_number', ['format', 'display'], sprintf(" and fid=770 and pid=%d", $ypProId));
		if(empty($res['data'])) LibFc::ReturnJson(true, $arr);

		$ypOeArr = array_column($res['data'], 'display', 'format');
		$othOeArr = [];
		switch($type)
		{
			case 'tcdcn':
			case 'tcd':
				$TcdmArt = new TcdmArt();
				$res = $TcdmArt->get('tof_article_number', ['distinct arl_format'], sprintf(" and arl_art_id = %d and arl_kind =3", $proId));
				$othOeArr = array_column($res['data'], 'arl_format');
			break;
			case 'aaia':
				$AaiaBase = new AaiaBase();
				$res = $AaiaBase->get('yp_number', ['distinct format'], sprintf(" and fid=770 and pid = %d", $proId));
				$othOeArr = array_column($res['data'], 'format');
			break;
			default:
				if(stripos($type, 'oth') === 0)
				{
					$siteName = substr($type, 3);
					$BaseDb = new BaseDb(mvc::$cfg['DB']['site'][$siteName]);
					$res = $BaseDb->get('fac_search', ['sea_format'], sprintf(" and sea_type=2 and sea_pro_id = %d", $proId));
					$othOeArr = array_column($res['data'], 'sea_format');
				}
			break;
		}
		foreach($othOeArr as $v)
		{
			if(!empty($ypOeArr[$v])) $arr[] = ['factory'=>'OE', 'display'=>$ypOeArr[$v]];
		}
		LibFc::ReturnJson(true, $arr);
	}



	/**
	  * @method get  目录后台号码搜索
	  * @http   get
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  word			   string [必填] 号码
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function superSearch()
	{
		$Params = mvc::$URL_PARAMS;
		$proArr = [];
		$formatNum = LibFc::FormatNum($Params['word']);
		if(strlen($formatNum) > 2)
		{
			$dtypeArr = ['para', 'picAll'];
			$count = 10;
			$typeArr = ['etk', 'yp', 'tcd'];

			$formatNumArr = LibFc::SearchNum($formatNum);
			foreach($typeArr as $type)
			{
				switch($type)
				{
					case 'etk':
						$tnums = $formatNumArr;
						$resfrom = '';
						$EtkBasic = new EtkBasic();
						$nres = $EtkBasic->Get('data_product_search', ['distinct src_etk_id'], sprintf(" and src_format in ('%s')", implode("','", $tnums)));
						//epc或者宜配数据加追EPC替换号
						if(empty($nres['data'])){
							if($epcReNum === null){
								$EtkNum = new EtkNum();
								$res = $EtkNum->getSearchNum($formatNum);
								$epcReNum = $res['data'];
								if($epcReNum['hasreplace']){
									$res = $EtkNum->getReplaceNumber($formatNum);
									$epcReNum['replace'] = $res['data'];
								}
							}
							if(!empty($epcReNum['nums']) && $epcReNum['hasreplace']){
								$tnums = $epcReNum['nums'];
								$resfrom = 'reepcnum';
								$nres = $EtkBasic->Get('data_product_search', ['distinct src_etk_id'], sprintf(" and src_format in ('%s')", implode("','", $tnums)));
							}
						}
						foreach($nres['data'] as $v)
						{
							$etkId = $v['src_etk_id'];
							if(empty(mvc::$cfg['DB']['EPC'][$etkId])) continue;
							$EpcItem = new EpcItem($etkId);
							$pres = $EpcItem->getSearchNumber($tnums, $islike);
							foreach($pres['data'] as $vv)
							{
								//替换号中找出搜索号码
								if($resfrom == 'reepcnum' && !empty($epcReNum['replace'])){
									$hasnums = [];
									foreach($vv['num'] as $tnum){
										$hasnums[] = LibFc::FormatNum($tnum['display']);
									}
									foreach($epcReNum['replace'] as $enum){
										if($formatNum == $enum['format'] && !in_array($enum['format'], $hasnums)){
											$vv['num'][] = ['factory'=>$enum['factory'], 'display'=>$enum['display']];
										}
									}
								}
								$vv['pro_name'] = strip_tags($vv['pro_name']);
								$res = $EpcItem->getItem($vv['pro_id'], $vv['grp2_id']);
								$vv = $vv = array_merge($res['data'], $vv);
	
								$proArr[$type][$etkId.$vv['pro_id']] = $vv;
							}
							$proArr[$type] = array_values($proArr[$type]);
						}
					break;
					case 'yp':
						$YpsBase = new YpsBase();
						$where = sprintf(" and n.format in ('%s') group by n.pid", implode("','", $formatNumArr));
						$table = 'yp_number AS n JOIN yp_factory AS fac ON fac.fid = n.fid';
						$filed = ['n.pid, n.display, factory'];
						$res = $YpsBase->get($table, $filed, $where);
						if(!empty($res['data']))
						{
							$proNum = array_column($res['data'], null, 'pid');
							$YpsProduct = new YpsProduct();
							$tres = $YpsProduct->getAnyProductInfo(array_column($res['data'], 'pid'), $dtypeArr);
							foreach($tres['data'] as $proId=>$pv)
							{
								$tnum = [];
								foreach($pv['num'] as $tv)
								{
									$tnum[$tv['factory'].'@'.$tv['display']] = $tv;
								}
								$tnum[$proNum[$proId]['factory'].'@'.$proNum[$proId]['display']] = [
									'factory'=>$proNum[$proId]['factory'],	
									'display'=>$proNum[$proId]['display']	
								];
								$pv['num'] = array_values($tnum);
								$proArr[$type][] = $pv;
							}
						}
					break;
					case 'tcd':
						$TcdmArt = new TcdmArt();
						$ignoreSupId = [0, 11404];
						$table = 'tof_article_number
						join tof_articles on  arl_art_id = art_id
						join tof_brands on bra_id = arl_bra_id 
						join tof_suppliers on sup_id = art_sup_id 
						join tof_generic_articles on ga_id = art_ga_id 
						left join ms_ga_sup_seq on gss_sup_id=art_sup_id and art_ga_id = gss_ga_id';
						$where = sprintf(" and arl_format in ('%s') and art_sup_id not in (%s) group by art_id,gss_seq, sup_brand order by gss_seq, sup_brand limit %d",
								implode("','", $formatNumArr), implode(',', $ignoreSupId), $count);
						$res = $TcdmArt->get($table, ['art_id, bra_brand, arl_number'], $where);
						if(!empty($res['data']))
						{
							$proNum = array_column($res['data'], null, 'art_id');
							$tres = $TcdmArt->getProductInfo(array_column($res['data'], 'art_id'), $dtypeArr);
							foreach($tres['data'] as $proId=>$pv)
							{
								$tnum = [];
								foreach($pv['num'] as $tv)
								{
									$tnum[$tv['factory'].'@'.$tv['display']] = $tv;
								}
								$tnum[$proNum[$proId]['bra_brand'].'@'.$proNum[$proId]['arl_number']] = [
									'factory'=>$proNum[$proId]['bra_brand'],	
									'display'=>$proNum[$proId]['arl_number']	
								];
								$pv['num'] = array_values($tnum);
								$proArr[$type][] = $pv;
							}
						}
					break;
				}
			}
			
			//如果找不到参考数据，展示智配数据
			if(empty($proArr))
			{
				$type = 'zhipei';
				$BaseDb = new BaseDb(mvc::$cfg['DB']['ZHIPEI']);
				$where = sprintf("and OemNO in ('%s') group by p.OemId", implode("','", $formatNumArr));
				$res = $BaseDb->Get('2_value as p join 2_param as pa on p.OemId=pa.OemId', ['p.OemId as pro_id'], $where);
				if(!empty($res['data']))
				{
					$OthProduct = new OthProduct();
					$res = $OthProduct->getZhipeiAnyProductInfo(array_column($res['data'], 'pro_id'), $dtypeArr);
					$proArr[$type] = array_values($res['data']);
				}
			}
		}
		echo json_encode(['status'=>true, 'data'=>$proArr]);
	}


	/**
	  * @method get  号码匹配全车件模式车型
	  * @http  get
	  * @param  number			   string [必填] 号码 11138616793,11 41 8 612 615 多个号码用英文逗号分隔
	  * @author soul
	  * @copyright 2023/7/12
	  * @return {"status":true,"data":["4189","21007","6288"],"code":"","runtime":97}
	  */
	public function getModelByNumber()
	{
		$Params = mvc::$URL_PARAMS;
		$EtkNum = new EtkNum();
		$res = $EtkNum->getModelByNumber($Params['number'], 'all');
		LibFc::EchoJson($res);
	}


	/**
	  * @method get 获取无产品的号码记录
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  btime			   int [选填] 开始时间戳
	  * @param  etime			   int [选填] 结束时间戳
	  * @param  page               int [选填] 第几页
	  * @param  per				   int [选填] 每页N条数据 默认200
	  * @author soul
	  * @copyright 2026/6/3
	  * @return  {"status":true,"data":[{"ysn_id":"1","ysn_number":"904508H308","ysn_format":"904508H308","ysn_count":"4","ysn_chkstatus":"0","ysn_chktime":"1780472120","ysn_atime":"1780472120","ysn_from":"PJLPC"},{"ysn_id":"2","ysn_number":"24256525","ysn_format":"24256525","ysn_count":"1","ysn_chkstatus":"0","ysn_chktime":"1780472126","ysn_atime":"1780472126","ysn_from":"PJLPC"}],"code":0,"runtime":71}
		ysn_number 原始号码
		ysn_format 格式化号码
		ysn_countint 查询次数
		ysn_chkstatus 检查结果 0无 1已有
		ysn_chktime 检测时间
		ysn_atimeint L首次加入时间
		ysn_from 首次查询源
	  */
	public function getNumListByNoItem()
	{
		$Params = mvc::$URL_PARAMS;
		$arr = [];
		$per = empty($Params['per']) || $Params['per'] < 1 ? 200: (int) $Params['per'];
		$page = empty($Params['page']) || $Params['page'] < 1 ? 1: (int) $Params['page'];
		$YpwBase = new YpwBase();
		$where = '';
		if($Params['btime']) $where = sprintf(" and ysn_atime >=%d", $btime);
		if($Params['etime']) $where = sprintf(" and ysn_atime <=%d", $etime);
		$res = $YpwBase->get('yp_search_numnull', ['*'], $where.sprintf(" order by ysn_id limit %d, %d", $page-1, $per));
		LibFc::EchoJson($res);
	}


	/**
	  * @method get 哪些数据源有此号码
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  word			   string [必填] 号码
	  * @author soul
	  * @copyright 2026/6/3
	  * @return  {"status":true,"data":["yp","tcd"],"code":"","runtime":481}
	  */
	public function checkNumItem()
	{
		$Params = mvc::$URL_PARAMS;
		$OthNumber = new OthNumber();
		$Params['onlycheck'] = 1;
		$res = $OthNumber->search($Params);
		LibFc::ReturnJson(true, array_keys($res['data']));
	}

	/**
	  * @method get 增加无号码记录
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  word			   string [必填] 号码
	  * @param  from			   string [必填] 来源
	  * @author soul
	  * @copyright 2026/6/3
	  * @return  
	  */
	public function addNumNullLog()
	{
		$Params = mvc::$URL_PARAMS;
		$OthNumber = new OthNumber();
		$res = $OthNumber->addNumNullLog($Params['word'], $Params['from']);
		LibFc::EchoJson($res);
	}
}