<?php
/**
*TCD
*/
class tcdAction extends baseAction
{

	/**
	 * @method 号码搜索 
	 * @http  get
	 * @param  number   string [必填] 号码
	 * @author hzq
	 * @copyright 2026/8/1
	 * @return 
		{"status":true,"data":[{"pro_id":"2708912","pro_code":"brake_pad_set_disc_brake,trw,gdb900","pro_name":"Brake Pad Set, disc brake","pro_yppartid":["160","13"],"num":[{"factory":"TRW","display":"GDB900"}],"pic":["http:\/\/3thpics.yiparts.com\/tpic\/30\/13\/small\/1357489.jpg"],"para":[{"name":"for manufacturer","val":"TRW","unit":""},{"name":"Wear Warning Contact","val":"incl. wear warning contact","unit":""},{"name":"Height","val":"48.5","unit":"mm"},{"name":"Length","val":"95","unit":"mm"},{"name":"Thickness\/Strength","val":"16.4","unit":"mm"}]}],"code":0}
		{"status":false,"data":'失败原因',"code":''}
	 */
	public function getNameByOE()
	{
		$TcdmArt = new TcdmArt();
		$res = $TcdmArt->getNameByOE($_GET['number']);
		echo json_encode($res);
	}

	/**
	 * @method 号码搜索 
	 * @http  get
	 * @param  number   string [必填] 号码
	 * @param  maxCount string [选填] 最大数量 默认20
	 * @author soul
	 * @copyright 2017/4/13
	 * @return 
		{"status":true,"data":[{"pro_id":"2708912","pro_code":"brake_pad_set_disc_brake,trw,gdb900","pro_name":"Brake Pad Set, disc brake","pro_yppartid":["160","13"],"num":[{"factory":"TRW","display":"GDB900"}],"pic":["http:\/\/3thpics.yiparts.com\/tpic\/30\/13\/small\/1357489.jpg"],"para":[{"name":"for manufacturer","val":"TRW","unit":""},{"name":"Wear Warning Contact","val":"incl. wear warning contact","unit":""},{"name":"Height","val":"48.5","unit":"mm"},{"name":"Length","val":"95","unit":"mm"},{"name":"Thickness\/Strength","val":"16.4","unit":"mm"}]}],"code":0}
		{"status":false,"data":'失败原因',"code":''}
	 */
	public function searchNumber()
	{
		$maxCount = empty($_GET['maxCount']) ? 20 : $_GET['maxCount'];
		$TcdmArt = new TcdmArt();
		$res = $TcdmArt->searchNumberAll($_GET['number'], $maxCount, ['para']);
		echo json_encode($res);
	}

	/**
	 * @method 配件详情
	 * @http  get
	 * @param  pro_id   string [必填] 旧版为数字，新版为字符串402_206_37101
	 * @author soul
	 * @copyright 2017/4/13
	 * @return 
		{"status":true,"data":{"pro_id":"2707154","pro_name":"Brake Pad Set, disc brake",
		"para":[{"name":"for manufacturer","val":"BREMBO","unit":""}...],
		"pic":["http:\/\/3thpics.yiparts.com\/tpic\/49\/13\/small\/1355379.jpg"],
		"num":[{"factory":"TRW","display":"GDB1816"}],"nums":[{"oe":0,"factory":"A.B.S.","display":"37742"}...],
		"model":[{"mod3_id":"6208","brand_id":"6","brand_name":"\u5965\u8fea","make_name":"\u5965\u8fea(\u8fdb\u53e3)","mod2_name":"Q7 (4LB)","mod3_name":"3.0 TDI","mod3_start_year":"2012","mod3_end_year":"2015"}...]}
		,"code":0}
		{"status":false,"data":'失败原因',"code":''}
	 */
	public function artDetail()
	{
		$proId = empty($_GET['pro_id'])? '': $_GET['pro_id'];
		$TcdmArt = new TcdmArt();
		$res = $TcdmArt->getProductInfo([$proId], ['para', 'picAll', 'model', 'nums']);
		if(!empty($res['data']))
			$res['data'] = array_shift($res['data']);
		echo json_encode($res);
	}

	/**
	  * @method 是否为新版数据
	  * @http  get
	  * @param  pro_id   string [必填] 
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
		{"status":true,"data":bool,"code":0}
		{"status":false,"data":'失败原因',"code":''}
	  */
	function isNew()
	{
		$isNew = false;
		$artId = (int) $_GET['pro_id'];
		$TcdmArt = new TcdmArt();
		$res = $TcdmArt->get('tof_articles', ['art_version'], sprintf(" and art_id = %d", $artId), true);
		$ver = $res['data']['art_version'];
		$isNew = str_pad(1, strlen($ver), "0",STR_PAD_RIGHT) == $ver? true: false;
		LibFc::ReturnJson(true, $isNew);
	}


	/**
	  * @method 通过号码获取TCD的号码对比数据
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  oes                array  [必填] ['11111', '22222'.....]
	  * @author soul
	  * @copyright 2017/11/20
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function getNumContrast()
	{
		$TcdmArt = new TcdmArt();
		$res = $TcdmArt->getNumContrast($_POST['oes']);
		echo json_encode($res);
	}
	

	/**
	  * @method 通过厂商号码获取TCD的配件信息
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  fac                string  [选填]
	  * @param  number             string  [必填]
	  * @author soul
	  * @copyright 2017/11/20
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function getArtByNum()
	{
		$arr = [];
		$fac = $_GET['fac'];
		$formatNum = LibFc::FormatNum($_GET['number']);
		if(strlen($formatNum) > 2)
		{
			$TcdmArt = new TcdmArt();
			$where = sprintf(" select distinct arl_art_id from tof_article_number where arl_format ='%s'", LibFc::Escape($formatNum));
			if(!empty($fac))
			{
				$res = $TcdmArt->getBrandId($fac);
				if(!empty($res['data']))
				{
					$where .= sprintf(" and (arl_bra_id in (%s)	or arl_sup_id in (%s))", implode(',', $res['data']), implode(',', $res['data']));
				}
			}
			$where = ' and art_id in('.$where.') order by sup_brand, art_format';
			$res = $TcdmArt->get('tof_articles join tof_suppliers on sup_id = art_sup_id', ['art_id', 'art_number', 'sup_brand', 'art_pic', 'art_url'], $where);
			foreach($res['data'] as $v)
			{
				$arr[] = [
					'ART_ID'=>$v['art_id'],
					'ART_ARTICLE_NR'=>$v['art_number'],
					'SUP_BRAND'=>$v['sup_brand'],
					'ART_PIC'=>$v['art_pic'],
					'ART_URL'=>	mvc::$cfg['urls']['tcdArt'].$v['art_url'],
				];
			}
		}
		LibFc::ReturnJson(true, $arr);
	}

	

	/**
	  * @method 通过TCD的配件对比数据
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  oes                array  [必填] ['11111', '22222'.....]
	  * @author soul
	  * @copyright 2017/11/20
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function getArtContrast()
	{
		$TcdmArt = new TcdmArt();
		$res = $TcdmArt->getArtContrast(explode(',', $_GET['artIds']));
		echo json_encode($res);
	}



	/**
	  * @method 重建TCD知名品牌的车型/发动机关联表 MS_SUP_TYPES、MS_SUP_ENGINES
	  * @http	get
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  supids              string [选填] 默认所有 62,63...
	  * @author soul
	  * @copyright 2019/5/27
	  * @return {"status":true,"data":true,"code":0,"encry":0,"runtime":74}
	  */
	public function resetFacTypLink()
	{
		$Params = mvc::$URL_PARAMS;
		$supIdArr = [];
		if(!empty($Params['supids']))
		{
			foreach(explode(',', $Params['supids']) as $v)
			{
				$supIdArr[] = (int) $v;
			}
		}
		$TcdmArt = new TcdmArt();
		$res = $TcdmArt->resetFacTypLink($supIdArr);
		LibFc::EchoJson($res);
	}


	/**
	  * @method TCD知名品牌的车型品牌
	  * @http	get
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  supid              string [必填] 厂商ID
	  * @author soul
	  * @copyright 2019/5/27
	  * @return {"status":true,"data":[{"make_id":"11351","make_name":"ABARTH","make_name_cn":"\u963f\u5df4\u65af","initial":"A"}...],"code":0,"encry":0,"runtime":74}
	  */
	public function getFacBrand()
	{
		$Params = mvc::$URL_PARAMS;
		$supid = (int) $Params['supid'];
		$arr = [];
		$TcdmBase = new TcdmBase();
		$res = $TcdmBase->get('tof_manufacturers', ['mfa_id, mfa_brand, mfa_brand_cn'],  sprintf(" and mfa_id in (select distinct sut_brand_id from ms_sup_types where sut_sup_id = %d) order by mfa_brand", $supid));
		foreach($res['data'] as $v)
		{
			$arr[] = [
				'make_id'=>$v['mfa_id'], 
				'make_name'=>$v['mfa_brand'],
				'make_name_cn'=>$v['mfa_brand_cn'],
				'initial'=>substr($v['mfa_brand'], 0, 1)
			];
		}
		LibFc::ReturnJson(true, $arr);
	}


	/**
	  * @method TCD知名品牌的车型品牌的车型级
	  * @http	get
	  * @param  supid              string [必填] 厂商ID
	  * @param  make_id              string [必填] 车型品牌ID
	  * @author soul
	  * @copyright 2019/5/27
	  * @return {"status":true,"data":[{"mod2_id":"4211","mod2_name":"ACE","mod2_start_year":"199810","mod2_end_year":"0"},{"mod2_id":"4210","mod2_name":"ACE","mod2_start_year":"199501","mod2_end_year":"199810"},{"mod2_id":"4212","mod2_name":"ACECA","mod2_start_year":"199201","mod2_end_year":"200112"},{"mod2_id":"4213","mod2_name":"COBRA Mk IV (291N)","mod2_start_year":"199001","mod2_end_year":"0"}],"code":0,"encry":0,"runtime":494}
	  */
	public function getFacBrandModel()
	{
		$Params = mvc::$URL_PARAMS;
		$supid = (int) $Params['supid'];
		$make_id = (int) $Params['make_id'];
		$arr = [];
		if($supid > 0 && $make_id > 0)
		{
			$where = sprintf(" and mod_id in ( select distinct sut_mod_id from ms_sup_types where sut_sup_id = %d and sut_brand_id=%d) order by mod_name, mod_start desc", $supid, $make_id);
			$TcdmBase = new TcdmBase();
			$res = $TcdmBase->get('tof_models', ['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']
				];
			}
		}
		LibFc::ReturnJson(true, $arr);
	}


	/**
	  * @method TCD知名品牌的车型品牌的车型级
	  * @http	get
	  * @param  supid              string [必填] 厂商ID
	  * @param  make_id              string [必填] 车型品牌ID
	  * @param  mod2_id              string [选填] 车型级ID
	  * @param  word              string [选填] 关键字
	  * @author soul
	  * @copyright 2019/5/27
	  * @return {"status":true,"data":[{"mod3_id":"32109","mod2_name":"B7 (F01, F02)","mod3_name":"BITURBO","mod3_engine":"N63 M10 A","mod3_kw":"373","mod3_cc":"4395","mod3_cylinder":"8","mod3_start_year":"200903","mod3_end_year":"201206"},...],"code":0}
	  */
	public function getFacBrandTyp()
	{
		$Params = mvc::$URL_PARAMS;
		$supid = (int) $Params['supid'];
		$make_id = (int) $Params['make_id'];
		$arr = [];
		if($supid > 0 && $make_id > 0)
		{
			$mod2Where = !empty($Params['mod2_id'])? sprintf(" and sut_mod_id=%d", $Params['mod2_id']):'';
			$wordWhere = !empty($Params['word']) ?  sprintf(" and typ_full_name like '%%%s%%'", LibFc::Escape($Params['word'])): '';
			$where = sprintf(" and sut_sup_id = %d and sut_brand_id=%d %s %s order by typ_full_name, typ_start desc", $supid, $make_id, $mod2Where, $wordWhere);
			$TcdmBase = new TcdmBase();
			$modres = $TcdmBase->get('ms_sup_types join tof_types on typ_id = sut_typ_id join tof_models on mod_id = typ_mod_id', ['typ_id, typ_name, mod_name, typ_cc, typ_kw, typ_body, typ_cylinder, typ_start, typ_end, typ_engine'], $where);
			foreach($modres['data'] as $v)
			{
				$arr[] = [
					'mod3_id'=>$v['typ_id'], 
					'mod2_name'=>$v['mod_name'],
					'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']
				];
			}
		}
		LibFc::ReturnJson(true, $arr);
	}


	/**
	  * @method TCD知名品牌的配件
	  * @http	get
	  * @param  supid             int [必填] 厂商ID
	  * @param  mod3_id           int [选填] 车型ID  [eng_id、mod3_id、number 三选一]
	  * @param  number            string [选填] 号码
	  * @param  eng_id            int [选填] 发动机ID
	  * @param  ga_id             int [选填] 分类ID
	  * @author soul
	  * @copyright 2019/5/27
	  * @return {"status":true,"data":[{"pro_id":"1970482","pro_code":"brake_disc,brembo,09502210","pro_url":"http:\/\/www.yiparts.com\/dt\/brake_disc,brembo,09502210.html","pro_name":"Brake Disc","pro_yppartid":["157","13"],"num":[{"factory":"BREMBO","display":"09.5022.10"}],"pic":["http:\/\/3thpics.yiparts.com\/tpic\/S65\/82\/small\/1970482_1.jpg"],"para":[{"name":"Brake Disc Type","val":"Internally Vented","unit":""},{"name":"Ø","val":"250","unit":"mm"},{"name":"for Art.No.","val":"09.5022.10","unit":""},{"name":"Brake Disc Thickness","val":"22","unit":"mm"}]}...],"code":0,"encry":0,"runtime":180}
	  */
	public function getFacArticle()
	{
		$Params = mvc::$URL_PARAMS;
		$supid = (int) $Params['supid'];
		$mod3_id = (int) $Params['mod3_id'];
		$eng_id = (int) $Params['eng_id'];
		$formatNum = LibFc::FormatNum($Params['number']);
		$maxCount = 50;
		$arr = [];
		if($supid > 0)
		{
			$TcdmArt = new TcdmArt();
			if(strlen($formatNum) > 2)
			{
				$tWhere = empty($Params['ga_id']) ? '': sprintf(" and ga_id=%d", $Params['ga_id']);
				$formatNumArr = LibFc::SearchNum($formatNum);
				$table = 'tof_articles join tof_suppliers on sup_id= art_sup_id join tof_generic_articles on ga_id = art_ga_id'.$tWhere;
				$where = sprintf(" and art_format in ('%s') and art_sup_id =%d order by art_format, sup_brand", implode("','", $formatNumArr), $supid);
				$tcdRes = $TcdmArt->get($table, ['art_id'], $where);
			}
			else if($mod3_id > 0)
			{
				$tWhere = empty($Params['ga_id']) ? '': sprintf(" and lat_ga_id=%d", $Params['ga_id']);
				$table =  'tof_articles
					join tof_suppliers on sup_id = art_sup_id 
					left join ms_ga_sup_seq on gss_sup_id=art_sup_id and art_ga_id = gss_ga_id';
				$where =  sprintf(" and art_id in (
					  select distinct lat_art_id from tof_article_types where lat_typ_id=%d and lat_sup_id=%d %s
					) order by art_format, gss_seq, sup_brand limit %d", $mod3_id, $supid, $tWhere, $maxCount);
				$tcdRes = $TcdmArt->get($table, ['art_id'], $where);
			}
			else if($eng_id > 0)
			{
				$tWhere = empty($Params['ga_id']) ? '': sprintf(" AND lat_ga_id=%d", $Params['ga_id']);
				$table =  'tof_articles
					join tof_suppliers on sup_id = art_sup_id 
					left join ms_ga_sup_seq on gss_sup_id=art_sup_id and art_ga_id = gss_ga_id';
				$where =  sprintf(" and art_id in (
					  select distinct lat_art_id from tof_article_types join tof_types_engines on lte_typ_id = lat_typ_id where lte_eng_id=%d and lat_sup_id=%d %s
					) order by art_format, gss_seq, sup_brand limit %d", $eng_id, $supid, $tWhere, $maxCount);
				$tcdRes = $TcdmArt->get($table, ['art_id'], $where);
			}

			if(!empty($tcdRes['data']))
			{
				$res = $TcdmArt->getProductInfo(array_column($tcdRes['data'], 'art_id'), ['para'], 0, [$mod3_id]);
				$arr = array_values($res['data']);
			}
		}
		LibFc::ReturnJson(true, $arr);
	}


	/**
	  * @method TCD知名品牌的车型信息
	  * @http	get
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  mod2_id            string [选填] 车型级ID
	  * @param  mod3_id            string [选填] 车型ID
	  * @author soul
	  * @copyright 2019/5/27
	  * @return 
	  	mod2_id {"status":true,"data":{"make_id":"558","make_name":"NISSAN","mod2_name":"BLUEBIRD (T72 , T12, U12)","mod3_name":"2.0 (T12)","mod3_start_year":"198512","mod3_end_year":"198712"},"code":0,"encry":0,"runtime":149}
		mod3_id {"status":true,"data":{"make_id":"666","make_name":"AC","mod2_name":"ACE","mod2_start_year":"199810","mod2_end_year":"0"},"code":0,"encry":0,"runtime":188}
	  */
	public function getFacModelInfo()
	{
		$Params = mvc::$URL_PARAMS;
		$arr = [];
		if(!empty($Params['mod3_id']))
		{
			$TcdmBase = new TcdmBase();
			$table = 'tof_types
			join tof_models on mod_id = typ_mod_id
			join tof_manufacturers on mfa_id = mod_mfa_id';
			$res = $TcdmBase->get($table, ['mfa_id, mfa_brand_cn, mod_name,typ_name,typ_start,typ_end'], sprintf(" and typ_id =%d", $Params['mod3_id']), true);
			if(!empty($res['data']))
			{
				$arr = [
					'make_id'=>$res['data']['mfa_id'],
					'make_name'=>$res['data']['mfa_brand_cn'],
					'mod2_name'=>$res['data']['mod_name'],
					'mod3_name'=>$res['data']['typ_name'],
					'mod3_start_year'=>$res['data']['typ_start'],
					'mod3_end_year'=>$res['data']['typ_end']
				];
			}
		}
		else if(!empty($Params['mod2_id']))
		{
			$TcdmBase = new TcdmBase();
			$table = 'tof_models join tof_manufacturers on mfa_id = mod_mfa_id';
			$res = $TcdmBase->get($table, ['mfa_id, mfa_brand_cn, mod_name, mod_start,mod_end'], sprintf(" and mod_id=%d", $Params['mod2_id']), true);
			if(!empty($res['data']))
			{
				$arr = [
					'make_id'=>$res['data']['mfa_id'],
					'make_name'=>$res['data']['mfa_brand_cn'],
					'mod2_name'=>$res['data']['mod_name'],
					'mod2_start_year'=>$res['data']['mod_start'],
					'mod2_end_year'=>$res['data']['mod_end']
				];
			}
		}
		LibFc::ReturnJson(true, $arr);
	}



	/**
	  * @method 通过关键字查找TCD知名品牌的发动机
	  * @http	get
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  supid              string [必填] 厂商ID
	  * @param  eng_id             string [选填] 发动机ID
	  * @param  word               string [选填] 关键字
	  * @author soul
	  * @copyright 2019/5/27
	  * @return {"status":true,"data":[{"eng_id":"31898","make_name":"ABARTH","eng_name":"955 A8.000","eng_cylinders":"4","eng_kw":"120~132","eng_hp":"163~180","eng_cc":"1368"},{"eng_id":"24589","make_name":"ALFA ROMEO","eng_name":"955 A8.000","eng_cylinders":"4","eng_kw":"120","eng_hp":"163","eng_cc":"1368"},{"eng_id":"24767","make_name":"FIAT","eng_name":"955 A8.000","eng_cylinders":"4","eng_kw":"120~132","eng_hp":"163~180","eng_cc":"1368"}],"code":0}
	  */
	public function searchFacEngine()
	{
		$Params = mvc::$URL_PARAMS;
		$supid = (int) $Params['supid'];
		$arr = [];
		$where = sprintf(" and sue_sup_id = %d", $supid);
		if(!empty($Params['eng_id']))
		{
			$where .= sprintf(" and eng_id = %d", $Params['eng_id']);
		}
		if(!empty($Params['word']))
		{
			$word = LibFc::Escape($Params['word']);
			if(strlen($word) < 2) LibFc::ReturnJson(true, $arr);
			$where .= sprintf(" and eng_code_format like '%%%s%%'", LibFc::FormatNum($word));
		}
		else
		{
			LibFc::ReturnJson(true, $arr);
		}
		$TcdmBase = new TcdmBase();
		$table = 'ms_sup_engines join tof_engines on sup_eng_id=eng_id join tof_manufacturers on mfa_id =eng_mfa_id';
		$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 = $TcdmBase->get($table, $filed,  $where.' order by mfa_brand, eng_code_format');
		foreach($res['data'] as $v)
		{
			$arr[] = [
				'eng_id'=>$v['eng_id'], 
				'make_name'=>$v['mfa_brand'],
				'eng_name'=>$v['eng_code'],
				'eng_cylinders'=>$v['eng_cylinders'],
				'eng_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'],
				'eng_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'],
				'eng_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'],
			];
			if(count($arr) >= 20) break;
		}
		LibFc::ReturnJson(true, $arr);
	}


	/**
	  * @method 查找TCD知名品牌发动机的零件分类
	  * @http	get
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  supid              string [必填] 厂商ID
	  * @param  eng_id             string [选填] 发动机ID
	  * @author soul
	  * @copyright 2019/5/27
	  * @return {"status":true,"data":[{"ga_id":"1217","ga_des":"Bolt Kit, cylinder head"},{"ga_id":"560","ga_des":"Full Gasket Set, engine"},{"ga_id":"1451","ga_des":"Gasket \/ Seal"},{"ga_id":"150","ga_des":"Gasket Set, crank case"},{"ga_id":"319","ga_des":"Gasket Set, cylinder head"},{"ga_id":"979","ga_des":"Gasket Set, cylinder head cover"},{"ga_id":"318","ga_des":"Gasket, cylinder head"},{"ga_id":"321","ga_des":"Gasket, cylinder head cover"},{"ga_id":"27","ga_des":"Gasket, exhaust manifold"},{"ga_id":"138","ga_des":"Gasket, exhaust pipe"},{"ga_id":"455","ga_des":"Gasket, wet sump"},{"ga_id":"2420","ga_des":"Mounting Kit, charger"},{"ga_id":"1216","ga_des":"Rocker\/ Tappet"},{"ga_id":"323","ga_des":"Seal Set, valve stem"},{"ga_id":"135","ga_des":"Seal, oil drain plug"},{"ga_id":"322","ga_des":"Seal, valve stem"},{"ga_id":"757","ga_des":"Shaft Seal, camshaft"},{"ga_id":"572","ga_des":"Shaft Seal, crankshaft"}],"code":0}
	  */
	public function getFacEnginePart()
	{
		$Params = mvc::$URL_PARAMS;
		$supid = (int) $Params['supid'];
		$eng_id = (int) $Params['eng_id'];
		$where = sprintf(" and ga_id in (select distinct lat_ga_id from tof_article_types join tof_types_engines on lte_typ_id = lat_typ_id where lte_eng_id=%d and lat_sup_id=%d) order by ga_name", $eng_id, $supid);
		$arr = [];
		$TcdmBase = new TcdmBase();
		$res = $TcdmBase->get('tof_generic_articles', ['ga_id, ga_name_cn'],  $where);
		foreach($res['data'] as $v)
		{
			$arr[] = [
				'ga_id'=>$v['ga_id'], 
				'ga_des'=>$v['ga_name_cn']
			];
		}
		LibFc::ReturnJson(true, $arr);
	}


	/**
	  * @method 获取产品的安装位置
	  * @http  POST
	  * @param  pro_ids array 产品ID 
	  * @param  mod3Id int 宜配车型ID 
	  * @param  typids array tcd车型ID
	  * @author soul
	  * @copyright 2017/11/20
	  * @return 
	  {"status":true,"data": [],"code":""}
	  */
	public function getItemLocation()
	{
		$TcdmArt = new TcdmArt();
		$res = $TcdmArt->getItemLocation($_POST['pro_ids'], $_POST['mod3Id'], $_POST['typids']);
		echo json_encode($res);
	}


	/**
	  * @method 获取TCD配件基本信息
	  * @http  POST
	  * @param  pro_ids array 产品ID 
	  * @param  dtype  array para..
	  * @param  mod3Id int 宜配车型ID 
	  * @param  typids array tcd车型ID
	  * @author soul
	  * @copyright 2017/7/7
	  * @return  {"status":true,"data": [],"code":""}
	  */
	public function getTcdProductInfo()
	{
		$TcdmArt = new TcdmArt();
		$res = $TcdmArt->getProductInfo($_POST['pro_ids'], $_POST['dtype'], $_POST['mod3Id'], $_POST['typids']);
		LibFc::EchoJson($res);
	}



	/**
	  * @method 获取TCD中国的配件基本信息
	  * @http  POST
	  * @param  pro_ids array 产品ID 
	  * @param  dtype  array para..
	  * @param  mod3Id int 宜配车型ID 
	  * @param  typids array tcd车型ID
	  * @author soul
	  * @copyright 2017/7/7
	  * @return  {"status":true,"data": [],"code":""}
	  */
	public function getTcdCnProductInfo()
	{
		$TcdCnProduct = new TcdCnProduct();
		$res = $TcdCnProduct->getAnyProductInfo($_POST['pro_ids'], $_POST['dtype'], $_POST['mod3Id'], $_POST['typids']);
		LibFc::EchoJson($res);
	}


	/**
	  * @method 获取tcd产品的国内车型ID
	  * @http  get
	  * @param  proId int [必填] 产品ID
	  * @author soul 
	  * @copyright 2021/7/20
	  * @return json
	  */
	public function getArtYpcModelIds()
	{
		$mod3IdArr = [];
		$proId = $_GET['proId'];
		if($proId > 0)
		{
			$TcdmArt = new TcdmArt();
			$res = $TcdmArt->getYpcModelId($proId);
			$mod3IdArr = $res['data'];
		}
		echo json_encode(['status'=>true, 'data'=>$mod3IdArr]);
	}

	/**
	  * @method 清洗数据
	  * @http  get
	  * @param  number [号码、产品ID二选一] 号码 180510700
	  * @param  art_id [号码、产品ID二选一] 指定产品ID
	  * @param  facseq 竞品品牌优先级 TRW,BREMBO,REMSA
	  * @param  rate_number 号码确认相同率 默认70
	  * @param  rate_model  车型确认相同率 默认50
	  * @author soul
	  * @copyright 
	  */
	public function cleanData()
	{
		$TcdmItem = new TcdmItem();
		$res = $TcdmItem->cleanData($_POST);
		LibFc::EchoJson($res);
	}

	
	/**
	  * @method 检测号码的基本信息[tcd+etk]
	  * @http  post
	  * @param  number string [必填] [{"factory":"OE","number":"000 420 68 00"}]
	  * @author soul
	  * @copyright 
	  */
	public function checkNumberData()
	{
		$TcdmItem = new TcdmItem();
		$nums = json_decode($_POST['number'], true);
		$Params = $_POST;
		unset($_POST['number']);
		$res = $TcdmItem->checkNumberData($nums, $Params);
		LibFc::EchoJson($res);
	}


	/**
	  * @method 获取分类的竞品的统计
	  * @http  get
	  * @param  ganame string [必填] 多个分号分隔
	  * @author soul
	  * @copyright 
	  */
	public function cleanGaTj()
	{
		$Params = mvc::$URL_PARAMS;
		$arr = [];
		$names = [];
		$Params['ganame'] = strtr($Params['ganame'],['；'=>';']);
		foreach(explode(';', $Params['ganame']) as $v){
			if(!empty($v)) $names[] = LibFc::Escape($v);
		}
		if(!empty($names)){
			$TcdmItem = new TcdmItem();
			$where = sprintf(" and (ga_name in ('%s') or ga_name_cn in ('%s')) group by sup_brand order by gss_seq,art_count desc", implode("','", $names), implode("','", $names));
			$table = 'tj_ga_sup 
			LEFT JOIN tof_articles ON gs_art_id = art_id
			JOIN tof_suppliers ON sup_id=gs_sup_id 
			JOIN tof_generic_articles ON ga_id=gs_ga_id 
			JOIN ms_ga_sup_seq ON gss_ga_id = ga_id AND gss_sup_id = sup_Id';
			//获取样例
			$tjres = $TcdmItem->get($table, ['sup_id','ga_id','sup_brand', 'sum(gs_count) art_count', 'art_url2 url','art_number number'], $where);
			foreach($tjres['data'] as $v){
				$v['url'] = 'https://webcache.yiparts.com/item?c=cn_'.$v['url'];
				$arr[] = $v;
			}
		}
		LibFc::ReturnJson(true, $arr);
	}

	/**
	  * @method 号码比较
	  * @http  get
	  * @param  number string [选填] [{"factory":"BOSCH","number":"0986495299"},{"factory":"BOSCH","number":"0986494664"},{"factory":"TEXTAR","number":"2486903"},{"factory":"OE","number":"0084200620"}]
	  * @param  artids string [选填] 1,2,3
	  * @param  ga_id string [选填] 222
	  * @author soul
	  * @copyright 
	  */
	public function compareNum()
	{
		$TcdmItem = new TcdmItem();
		$nums = json_decode($_POST['number'], true);
		$res = $TcdmItem->compareNum($nums, $_POST['artids'], $_POST['type'], $_POST['ga_id']);
		LibFc::EchoJson($res);
	}


	/**
	  * @method 批量号码检测[仅tcd数据]
	  * @http  post
	  * @param  number string [必填] [{"factory":"BOSCH","number":"0986495299"},{"factory":"BOSCH","number":"0986494664"},{"factory":"TEXTAR","number":"2486903"},{"factory":"OE","number":"0084200620"}]
	  * @param  mainfac string [选填] 竞品品牌TRW,BREMBO,REMSA
	  * @author soul
	  * @copyright 
	  */
	public function chkNumBase()
	{
		$TcdmItem = new TcdmItem();
		$nums = json_decode($_POST['number'], true);
		$res = $TcdmItem->chkNumBase($nums, $_POST['mainfac']);
		LibFc::EchoJson($res);
	}


	/**
	  * @method 单个号码检测
	  * @http  post
	  * @param  number string [必填] 号码 0986495299
	  * @param  factory string [选填] 厂商 BOSCH
	  * @param  ganame string [选填] 零件分类
	  * @author soul
	  * @copyright 
	  */
	public function chkOneNumBase()
	{
		$TcdmItem = new TcdmItem();
		$res = $TcdmItem->chkOneNumBase($_POST['number'], $_POST['factory'], $_POST['ganame']);
		LibFc::EchoJson($res);
	}

	
	/**
	  * @method 通过产品ID获取产品号码与厂商
	  * @http  get
	  * @param  artids string [必填] 产品ID 多个逗号分隔 1,2,3
	  * @author soul
	  * @copyright 
	  */
	public function artNumber()
	{
		$Params = mvc::$URL_PARAMS;
		$TcdmItem = new TcdmItem();
		$res = $TcdmItem->artNumber(explode(',', $Params['artids']));
		LibFc::EchoJson($res);
	}


	/**
	  * @method 通过号码获取对应的零件分类
	  * @http  get
	  * @param  number string [必填] 0986495299
	  * @author soul
	  * @copyright {"status":true,"data":[{"ga_id":"316","ga_name":"Thermostat, coolant","ga_name_cn":"冷却水节温器","pro_count":"16"},{"ga_id":"1792","ga_name":"Thermostat Housing","ga_name_cn":"节温器罩","pro_count":"6"}],"code":0,"runtime":63}
	  */
	public function getNumParts()
	{
		$Params = mvc::$URL_PARAMS;
		$arr = [];
		$formatNum = LibFc::FormatNum($Params['number']);
		if($formatNum){
			$TcdmItem = new TcdmItem();
			$table = 'tof_article_number
			join tof_articles on art_id = arl_art_id
			join tof_generic_articles on ga_id = art_ga_id';
			$res = $TcdmItem->get($table, ['ga_id,ga_name,ga_name_cn, COUNT(*) pro_count'], sprintf(" and arl_format='%s' group by ga_name order by COUNT(*) desc", $formatNum));
			LibFc::EchoJson($res);
		}
		LibFc::ReturnJson(true, $arr);
	}


	/**
	  * @method 中台接口 获取汽车品牌列表
	  * @http  get
	  * @param  mfa_type string [必填] 汽车类型 all|pc|cv
	  * @param  lang string [选填] cn|en 默认en
	  * @author soul
	  * @copyright 
	  */
	public function tcdMfaList()
	{
		$Params = mvc::$URL_PARAMS;
		$lang = $Params['lang'] == 'cn'? '_cn': '';
		$arr = [];
		$where = '';
		if(!empty(mvc::$cfg['RQTOKEN']['brand_ids_tcd']) || !empty(mvc::$cfg['RQTOKEN']['ga_ids']) || in_array($Params['mfa_type'], ['pc', 'cv'])){
			$table = 'tof_article_make JOIN tof_articles ON art_id = tam_art_id JOIN tof_manufacturers ON tam_mfa_id = mfa_id';
			if(!empty(mvc::$cfg['RQTOKEN']['brand_ids_tcd'])) $where .= sprintf(" and tam_mfa_id in (%s)", implode(',', mvc::$cfg['RQTOKEN']['brand_ids_tcd']));
			if(!empty(mvc::$cfg['RQTOKEN']['ga_ids'])) $where .= sprintf(" and art_ga_id in (%s)", implode(',', mvc::$cfg['RQTOKEN']['ga_ids']));
			if($Params['mfa_type'] == 'pc') $where .= ' and tam_pc = 1';
			if($Params['mfa_type'] == 'cv') $where .= ' and tam_cv = 1';
			$where .= ' group by mfa_id';
		}else{
			$table = 'tof_manufacturers';
		}
		$where .= ' order by mfa_brand'.$lang;
		$TcdmItem = new TcdmItem();
		$res = $TcdmItem->get($table, ['mfa_id, mfa_brand, mfa_brand_cn'], $where);
		LibFc::EchoJson($res);
	}


	/**
	  * @method 中台接口 竞品和分类
	  * @http  post
	  * @param  mfa_type string [必填] 汽车类型 all|pc|cv
	  * @param  mfa_ids string [必填] 汽车生产商ID 1,2,3... 默认所有
	  * @param  lang string [选填] cn|en 默认en
	  * @author soul
	  * @copyright 
	  */
	public function tcdSupGaByMfa()
	{
		$Params = $_POST;
		$lang = $Params['lang'] == 'cn'? '_cn': '';
		$arr = ['sup'=>[], 'ga'=>[]];
		$supWhere = '';
		$TcdmItem = new TcdmItem();
		if(!empty(mvc::$cfg['RQTOKEN']['brand_ids_tcd']) || !empty(mvc::$cfg['RQTOKEN']['ga_ids']) || in_array($Params['mfa_type'], ['pc', 'cv']) || !empty($Params['mfa_ids'])){
			$tWhere = '';
			if(!empty($Params['mfa_ids'])){
				$Params['mfa_ids'] = explode(',', $Params['mfa_ids']);
				$tids = mvc::$cfg['RQTOKEN']['brand_ids_tcd']? array_intersect(mvc::$cfg['RQTOKEN']['brand_ids_tcd'], $Params['mfa_ids']): array_map('intval', $Params['mfa_ids']);
				$tWhere .= $tids? sprintf(" and tam_mfa_id in (%s)", implode(',', $tids)): ' and 1 = 0';
			}else if(!empty(mvc::$cfg['RQTOKEN']['brand_ids_tcd'])){
				$tWhere .= sprintf(" and tam_mfa_id in (%s)", implode(',', mvc::$cfg['RQTOKEN']['brand_ids_tcd']));
			}
			if(!empty(mvc::$cfg['RQTOKEN']['ga_ids'])) $tWhere .= sprintf(" and art_ga_id in (%s)", implode(',', mvc::$cfg['RQTOKEN']['ga_ids']));
			if($Params['mfa_type'] == 'pc') $tWhere .= ' and tam_pc = 1';
			if($Params['mfa_type'] == 'cv') $tWhere .= ' and tam_cv = 1';
			$res = $TcdmItem->get('tof_article_make JOIN tof_articles ON art_id = tam_art_id', ['distinct art_sup_id'], $tWhere);
			if($res['data']){
				$res = $TcdmItem->get('tof_suppliers', ['sup_id, sup_brand'], sprintf(" and sup_id in (%s) order by sup_brand", implode(',', array_column($res['data'], 'art_sup_id'))));
				$arr['sup'] = $res['data'];
			}

			$res = $TcdmItem->get('tof_article_make JOIN tof_articles ON art_id = tam_art_id', ['distinct art_ga_id'], $tWhere);
			if($res['data']){
				$res = $TcdmItem->get('tof_generic_articles', ['ga_id, ga_name, ga_name_cn'], sprintf(" and ga_id in (%s) order by ga_name%s", implode(',', array_column($res['data'], 'art_ga_id')), $lang));
				$arr['ga'] = $res['data'];
			}
		}else{
			$res = $TcdmItem->get('tof_suppliers', ['sup_id, sup_brand'], ' order by sup_brand');
			$arr['sup'] = $res['data'];

			$res = $TcdmItem->get('tof_generic_articles', ['ga_id, ga_name, ga_name_cn'], ' order by ga_name'.$lang);
			$arr['ga'] = $res['data'];
		}
		LibFc::ReturnJson(true, $arr);
	}


	/**
	  * @method 中台接口 产品统计
	  * @http  post
	  * @param  mfa_type string [必填] 汽车类型 all|pc|cv
	  * @param  mfa_ids string [必填] 汽车生产商ID 1,2,3... 默认所有
	  * @param  tjtype string [必填] ga|sup  按什么做统计
	  * @param  sup_id int [必填] 产品品牌ID
	  * @param  ga_id int [必填] 产品分类
	  * @param  lang string [选填] cn|en 默认en
	  * @author soul
	  * @copyright 
	  */
	public function tcdGaSupTj()
	{
		$Params = $_POST;
		$lang = $_POST['lang'] == 'cn'? '_cn': '';
		$arr = ['sup'=>[], 'ga'=>[]];
		$TcdmItem = new TcdmItem();
		//是否提供的汽车品牌或者非完全授权
		if(!empty(mvc::$cfg['RQTOKEN']['brand_ids_tcd']) || !empty(mvc::$cfg['RQTOKEN']['ga_ids']) || in_array($Params['mfa_type'], ['pc', 'cv']) || !empty($Params['mfa_ids'])){
			$tWhere = '';
			if(!empty($Params['mfa_ids'])){
				$Params['mfa_ids'] = explode(',', $Params['mfa_ids']);
				$tids = mvc::$cfg['RQTOKEN']['brand_ids_tcd']? array_intersect(mvc::$cfg['RQTOKEN']['brand_ids_tcd'], $Params['mfa_ids']): array_map('intval', $Params['mfa_ids']);
				$tWhere .= $tids? sprintf(" and tam_mfa_id in (%s)", implode(',', $tids)): ' and 1 = 0';
			}else if(!empty(mvc::$cfg['RQTOKEN']['brand_ids_tcd'])){
				$tWhere .= sprintf(" and tam_mfa_id in (%s)", implode(',', mvc::$cfg['RQTOKEN']['brand_ids_tcd']));
			}
			if($Params['mfa_type'] == 'pc') $tWhere .= ' and tam_pc = 1';
			if($Params['mfa_type'] == 'cv') $tWhere .= ' and tam_cv = 1';
			if(!empty(mvc::$cfg['RQTOKEN']['ga_ids'])) $tWhere .= sprintf(" and art_ga_id in (%s)", implode(',', mvc::$cfg['RQTOKEN']['ga_ids']));
			//按分类统计
			if($Params['tjtype'] == 'ga' && $Params['sup_id']){
				$tWhere .= sprintf(" and art_sup_id=%d group by ga_id order by pro_count desc, ga_name%s", $Params['sup_id'], $lang);
				$res = $TcdmItem->get('tof_article_make JOIN tof_articles ON art_id = tam_art_id JOIN tof_generic_articles ON ga_id = art_ga_id', ['ga_id, ga_name, ga_name_cn, count(distinct art_id) pro_count'], $tWhere);
				$arr['ga'] = $res['data'];
			}else if($Params['tjtype'] == 'sup' && $Params['ga_id']){
				$tWhere .= sprintf(" and art_ga_id=%d group by sup_id order by pro_count desc, sup_brand", $Params['ga_id']);
				$res = $TcdmItem->get('tof_article_make JOIN tof_articles ON art_id = tam_art_id join tof_suppliers on sup_id=art_sup_id', ['sup_id, sup_brand, count(distinct art_id) pro_count'], $tWhere);
				$arr['sup'] = $res['data'];
			}
		}else{
			//按分类统计
			if($Params['tjtype'] == 'ga' && $Params['sup_id']){
				$tWhere = sprintf(" and art_sup_id=%d group by ga_id order by pro_count desc, ga_name%s", $Params['sup_id'], $lang);
				$res = $TcdmItem->get('tof_articles JOIN tof_generic_articles ON ga_id = art_ga_id', ['ga_id, ga_name, ga_name_cn, count(distinct art_id) pro_count'], $tWhere);
				$arr['ga'] = $res['data'];
			}else if($Params['tjtype'] == 'sup' && $Params['ga_id']){
				$tWhere = sprintf(" and art_ga_id=%d group by sup_id order by pro_count desc, sup_brand", $Params['ga_id']);
				$res = $TcdmItem->get('tof_articles join tof_suppliers on sup_id=art_sup_id', ['sup_id, sup_brand, count(distinct art_id) pro_count'], $tWhere);
				$arr['sup'] = $res['data'];
			}
		}
		LibFc::ReturnJson(true, $arr);
	}

	/**
	  * @method 中台接口 产品列表
	  * @http  post
	  * @param  mfa_type string [必填] 汽车类型 all|pc|cv
	  * @param  mfa_ids string [必填] 汽车生产商ID 1,2,3... 默认所有
	  * @param  tjtype string [必填] 按分类还是品牌做统计 ga|sup  
	  * @param  sup_id int [必填] 产品品牌ID
	  * @param  ga_id int [必填] 产品分类
	  * @param  lang string [选填] cn|en 默认en
	  * @author soul
	  * @copyright 
	  */
	public function tcdGaSupArtList()
	{
		$Params = $_POST;
		$lang = $_POST['lang'] == 'cn'? '_cn': '';
		$arr = [];
		if($Params['ga_id'] && $Params['sup_id']){
			$fields = ['art_id, art_number, art_format, art_min_year, art_max_year, sup_brand, ga_name, ga_name_cn'];
			$TcdmItem = new TcdmItem();
			//是否提供的汽车品牌或者非完全授权
			if(!empty(mvc::$cfg['RQTOKEN']['brand_ids_tcd']) || !empty(mvc::$cfg['RQTOKEN']['ga_ids']) || in_array($Params['mfa_type'], ['pc', 'cv']) || !empty($Params['mfa_ids'])){
				$tWhere = '';
				if(!empty($Params['mfa_ids'])){
					$Params['mfa_ids'] = explode(',', $Params['mfa_ids']);
					$tids = mvc::$cfg['RQTOKEN']['brand_ids_tcd']? array_intersect(mvc::$cfg['RQTOKEN']['brand_ids_tcd'], $Params['mfa_ids']): array_map('intval', $Params['mfa_ids']);
					$tWhere .= $tids? sprintf(" and tam_mfa_id in (%s)", implode(',', $tids)): ' and 1 = 0';
				}else if(!empty(mvc::$cfg['RQTOKEN']['brand_ids_tcd'])){
					$tWhere .= sprintf(" and tam_mfa_id in (%s)", implode(',', mvc::$cfg['RQTOKEN']['brand_ids_tcd']));
				}
				if($Params['mfa_type'] == 'pc') $tWhere .= ' and tam_pc = 1';
				if($Params['mfa_type'] == 'cv') $tWhere .= ' and tam_cv = 1';
				if(!empty(mvc::$cfg['RQTOKEN']['ga_ids'])) $tWhere .= sprintf(" and art_ga_id in (%s)", implode(',', mvc::$cfg['RQTOKEN']['ga_ids']));
				$tWhere .= sprintf(" and art_sup_id=%d and art_ga_id=%d group by art_id order by art_min_year desc", $Params['sup_id'], $Params['ga_id']);
				$res = $TcdmItem->get('tof_article_make JOIN tof_articles ON art_id = tam_art_id JOIN tof_generic_articles ON ga_id = art_ga_id JOIN tof_suppliers on sup_id=art_sup_id', $fields, $tWhere);
				$arr = $res['data'];
			}else{
				$tWhere = sprintf(" and art_sup_id=%d and art_ga_id=%d order by art_min_year desc", $Params['sup_id'], $Params['ga_id']);
				$res = $TcdmItem->get('tof_articles JOIN tof_generic_articles ON ga_id = art_ga_id JOIN tof_suppliers on sup_id=art_sup_id', $fields, $tWhere);
				$arr = $res['data'];
			}
		}
		LibFc::ReturnJson(true, $arr);
	}
}