<?php
/**
  * @method 
  * @author soul
  * @copyright 2016/3/31
  */
class TcdmArt extends TcdmBase
{

	//新版和旧版一起搜索
	public function searchNumberAll($number, $maxCount = 20, $typeArr = [])
	{
		return $this->searchNumber($number, $maxCount, $typeArr);
	}


	/**
	  * @method 通过号码 厂商获取产品ID
	  * @param  
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=>["code"]=> int(0) } 
	  */
	public function getArtIdByFacNumber($number, $fac)
	{
		$arr = ['pro_id'=>0, 'tcd_version'=>''];
		$formatNum = LibFc::FormatNum($number);
		$res = $this->get('tof_articles join tof_suppliers on sup_id = art_sup_id', ['art_id'], sprintf(" and art_format ='%s' and sup_brand = '%s'", LibFc::Escape($formatNum), LibFc::Escape($fac)), true);
		if(!empty($res['data'])) 
			$arr = ['pro_id'=>$res['data']['art_id'], 'tcd_version'=>'old'];
		return LibFc::ReturnData(true, $arr);
	}

	
	/**
	  * @method 通过号码 厂商获取产品车型
	  * @param  
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=>["code"]=> int(0) } 
	  */
	public function getModelByFacNumber($number, $fac)
	{
		$arr = [];
		$formatNum = LibFc::FormatNum($number);
		$res = $this->get('tof_articles join tof_suppliers on sup_id = art_sup_id', ['art_id'], sprintf(" and art_format ='%s' and sup_brand = '%s'", LibFc::Escape($formatNum), LibFc::Escape($fac)), true);
		if(!empty($res['data']))
		{
			$res = $this->getItemModel($res['data']['art_id']);
			$arr = $res['data'];
		}
		return LibFc::ReturnData(true, $arr);
	}

	/**
	 * @method 号码搜索
	 * @param  $number   string [必填] GDB2100
	 * @author hzq
	 * @copyright 2026/8/1
	 * @return 	array {
			"status": true,
			"data": [
				{
					"arl_bra_id": "579",
					"ga_name": "Shock Absorber",
					"ga_name_cn": "减振器",
					"gss_ga_id": "854"
				}
			],
			"code": 0
		}
	 */
	public function getNameByOE($number)
	{
		$arr = [];
		$formatNum = LibFc::FormatNum($number);
		$table = 'tof_article_number
			join tof_articles on arl_art_id = art_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';
		$fileds = ['arl_bra_id', 'ga_name', 'ga_name_cn', 'gss_ga_id', 'COUNT(*) AS total_count'];
		$where =  sprintf(" and arl_sup_id = 0 AND arl_format = '%s' group by gss_ga_id order by total_count desc limit 1",$formatNum);		
		$res = $this->get($table, $fileds, $where);
		if (!empty($res['data'])){
			$arr = $res['data'];
		}	
		return LibFc::ReturnData(true, $arr);
	}

	/**
	  * @method 号码搜索
	  * @param  $number   string [必填] GDB2100
	  * @param  $maxCount string [选填] 最大数量 默认20
	  * @author soul
	  * @copyright 2017/4/13
	  * @return ["status"=>true, "data"=>[
			'pro_id'=>'产品唯一标识',
			'pro_name'=>'',
			'pro_yppartid'=>[对应的宜配ID],
			'num'=>[
				['factory'=>'TRW', 'display'=>'GDB2100'],...
			],
			'pic'=>[],
			'para'=>[
				[ 'name'=>'', 'val'=>'','unit'=>''],
			]
	  ] ] 
	  */
	public function searchNumber($number, $maxCount = 20)
	{	
		$arr = [];
		$formatNum = LibFc::FormatNum($number);
		$maxCount = (int) $maxCount;
		if(strlen($formatNum) > 2)
		{
			//PFI 
			$formatNumArr = LibFc::SearchNum($formatNum);
			$ignoreSupId = [0, 11404];
			$table = 'tof_article_number
			join tof_articles on arl_art_id = art_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';
			$fileds = ['art_id', 'art_pic', 'art_url', 'sup_brand', 'art_number', 'ga_name', 'ga_name_cn', 'art_ga_id', 'art_cn_artid'];
			$where =  sprintf(" and arl_format in ('%s') and art_sup_id not in (%s) group by art_id order by gss_seq, sup_brand limit %d", 
				implode("','", $formatNumArr), implode(',', $ignoreSupId), $maxCount);
			$tcdRes = $this->get($table, $fileds, $where);
			if(!empty($tcdRes['data']))
			{
				$artIdArr = array_column($tcdRes['data'], 'art_id');
				//中国版数据
				$cnArtArr = [];
				if(mvc::$cfg['dLang'] != 'en')
				{
					$artCnIdArr = [];
					foreach($tcdRes['data'] as $v)
					{
						if($v['art_cn_artid'] > 0) $artCnIdArr[] = $v['art_cn_artid'];
					}
					$TcdCnProduct = new TcdCnProduct();
					$res = $TcdCnProduct->getAnyProductInfo($artCnIdArr, ['para']);
					$cnArtArr = $res['data'];
				}

				$lres = $this->getItemLocation($artIdArr);
				$locationArr = $lres['data'];

				//参数
				$paramRes = $this->getArtParam($artIdArr);
				
				//零件对应宜配
				$ypGaArr = [];
				$gaIdArr = array_unique(array_column($tcdRes['data'], 'art_ga_id'));
				$YpsBase = new YpsBase();
				$gaRes = $YpsBase->get('yp_part_tcdga', ['partid, ga_id'], sprintf(" and ga_id in (%s)", implode(',', $gaIdArr)));
				foreach($gaRes['data'] as $v)
				{
					$ypGaArr[$v['ga_id']][] = $v['partid'];
				}

				$snumArr = [];
				$where = sprintf(" and arl_format in ('%s') and arl_art_id in (%s) and arl_kind > 1",  implode("','", $formatNumArr), implode(',', $artIdArr));
				$numRes = $this->get('tof_article_number join tof_brands on bra_id = arl_bra_id', ['arl_art_id, bra_brand, arl_number'], $where);
				foreach($numRes['data'] as $v)
				{
					$snumArr[$v['arl_art_id']] = $v;
				}

				foreach($tcdRes['data'] as $v)
				{
					$tNum = [];
					if(!empty($snumArr[$v['art_id']])){
						$tNum[] = [ 'factory'=>$snumArr[$v['art_id']]['bra_brand'], 'display'=>$snumArr[$v['art_id']]['arl_number']];
					}
					$tNum[] = ['factory'=>$v['sup_brand'], 'display'=>$v['art_number']];
					$pro = [
						'pro_id'=>$v['art_id'],
						'pro_code'=>$v['art_url'],
						'pro_url'=>mvc::$cfg['urls']['yp'].'dt/'.$v['art_url'].'.html',
						'pro_name'=>mvc::$cfg['dLang'] != 'en'? $v['ga_name_cn']: $v['ga_name'],
						'pro_yppartid'=>!empty($ypGaArr[$v['art_ga_id']])? $ypGaArr[$v['art_ga_id']] : [],
						'num'=>$tNum,
						'pro_price'=>['title'=>'销售价', 'price'=>'0.00'],
						'pic'=>!empty($v['art_pic'][0])? [$v['art_pic'][0]['small']] : [],
						'para'=>!empty($paramRes['data'][$v['art_id']])? $paramRes['data'][$v['art_id']] : []
					];
					if(!empty($locationArr[$pro['pro_id']]) && stripos($pro['pro_name'], '(') === false)
						$pro['pro_name'] .= '('.implode(';', $locationArr[$pro['pro_id']]).')';

					if($v['art_cn_artid'] > 0 && !empty($cnArtArr[$v['art_cn_artid']]))
					{
						$cnPro = $cnArtArr[$v['art_cn_artid']];
						$reFiled = ['pic', 'pro_name', 'para'];
						foreach($reFiled as $filed)
						{
							if(!empty($cnPro[$filed])) $pro[$filed] = $cnPro[$filed];
						}
					}
					$arr[] = $pro;
				}
			}
		}
		return LibFc::ReturnData(true, $arr);
	}
	
	
	/**
	  * @method 获取产品信息
	  * @param  $artId
	  * @param  $typeArr ['para', 'picAll', 'model', 'nums'...]
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=>["code"]=> int(0) } 
	  */
	public function getProductInfo($proIdArr, $typeArr = [], $mod3Id = 0, $typIdArr = [])
	{
		$proArr = [];
		foreach($proIdArr as $k=>$v)
		{
			$proIdArr[$k] = (int) $v;
		}
		$proIdArr = array_unique($proIdArr);
		if(!empty($proIdArr))
		{
			$lres = $this->getItemLocation($proIdArr, $mod3Id,  $typIdArr);
			$locationArr = $lres['data'];

			$table = 'tof_articles 
			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';
			$tcdRes = $this->get($table, ['art_id, art_url, art_pic, sup_brand, art_number, ga_name, ga_name_cn, art_cn_artid'], sprintf(" and art_id in(%s) order by gss_seq, sup_brand", implode(',', $proIdArr)));
			if(!empty($tcdRes['data']))
			{
				$proIdArr = array_column($tcdRes['data'], 'art_id');

				//中国版数据
				$cnArtArr = [];
				$artCnIdArr = [];
				foreach($tcdRes['data'] as $v)
				{
					if($v['art_cn_artid'] > 0) $artCnIdArr[] = $v['art_cn_artid'];
				}
				if(!empty($artCnIdArr))
				{
					$TcdCnProduct = new TcdCnProduct();
					$res = $TcdCnProduct->getAnyProductInfo($artCnIdArr, ['para'], $mod3Id, $typIdArr);
					$cnArtArr = $res['data'];
				}
				
				if(in_array('para', $typeArr))
				{
					$paramRes = $this->getArtParam($proIdArr);
				}

				foreach($tcdRes['data'] as $v)
				{
					$pro = [
						'pro_id'=>$v['art_id'],
						'pro_code'=>$v['art_url'],
						'pro_url'=>mvc::$cfg['urls']['yp'].'dt/'.$v['art_url'].'.html',
						'pro_name'=>mvc::$cfg['dLang'] != 'en'? $v['ga_name_cn']: $v['ga_name'],
						'para'=>!empty($paramRes['data'][$v['art_id']])? $paramRes['data'][$v['art_id']] : [],
						'pic'=>[],
						'num'=>[['factory'=>$v['sup_brand'], 'display'=>$v['art_number']]],
						'nums'=>[],
						'model'=>[]
					];
					if(!empty($locationArr[$pro['pro_id']]) && stripos($pro['pro_name'], '(') === false)
						$pro['pro_name'] .= '('.implode(';', $locationArr[$pro['pro_id']]).')';
					
					if(!empty($v['art_pic']))
					{
						foreach($v['art_pic'] as $pic)
						{
							$pro['pic'][] = $pic['small'];
							if(!in_array('picAll', $typeArr)) break;
						}
					}
					
					//中国版参数替换
					if($v['art_cn_artid'] > 0 && !empty($cnArtArr[$v['art_cn_artid']]))
					{
						$cnPro = $cnArtArr[$v['art_cn_artid']];
						$reFiled = mvc::$cfg['dLang'] != 'en'? ['pic', 'pro_name', 'para']: ['pic'];
						foreach($reFiled as $filed)
						{
							if(!empty($cnPro[$filed])) $pro[$filed] = $cnPro[$filed];
						}
					}

					$proArr[$v['art_id']] = $pro;
				}

				if(in_array('nums', $typeArr))
				{
					$table = 'tof_article_number 
					left join tof_suppliers on arl_sup_id = sup_id
					left join tof_brands on arl_bra_id=bra_id';
					$where = sprintf(" and arl_art_id in (%s) and arl_kind < 5 order by bra_brand, arl_format asc", implode(',', $proIdArr));
					$res = $this->get($table, ['arl_art_id', 'bra_brand', 'sup_brand', 'arl_number', 'arl_kind'], $where);
					
					foreach($res['data'] as $v)
					{
						$proArr[$v['arl_art_id']]['nums'][] = ['oe'=>$v['arl_kind'] == 3? 1: 0, 'factory'=>$v['arl_kind'] == 1 ? $v['sup_brand']: $v['bra_brand'], 'display'=>$v['arl_number']];
					}
				}

				if(in_array('model', $typeArr))
				{
					$filed = ["lat_art_id, lat_typ_id, mfa_brand, mfa_id, typ_full_name, typ_cc, typ_kw, typ_body, typ_cylinder, typ_start, typ_end, typ_engine"];
					$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';
					$where = count($proIdArr) == 1 ? sprintf(" and lat_art_id in (%s) limit 1000", implode(',', $proIdArr)) : sprintf(" and lat_art_id in (%s)", implode(',', $proIdArr));
					$res = $this->get($table, $filed, $where);
					if(!empty($res['data']))
					{
						foreach($res['data'] as $v)
						{
							if(count($proArr[$v['lat_art_id']]['model']) > 1000) continue;
							$t = [
								'make_id'=>$v['mfa_id'],
								'make'=>$v['mfa_brand'],
								'vehicle'=>$v['typ_full_name'],
								'cc'=>$v['typ_cc'],
								'kw'=>$v['typ_kw'],
								'body_type'=>$v['typ_body'],
								'cyilinder'=>$v['typ_cylinder'],
								'year'=>$v['typ_start'].'-'.($v['typ_end'] > 0 ? $v['typ_end']: ''),
								'engines'=>$v['typ_engine']
							];
							$proArr[$v['lat_art_id']]['model'][] = $t;
						}
					}
				}
			}
		}
		return LibFc::ReturnData(true, $proArr);
	}


	/**
	  * @method 获取配件的号码
	  * @param  $proId int 配件ID
	  * @param  $type string  oe|ref|all
	  * @param  $page int 第几页
	  * @param  $per int 每页数量
	  * @author soul
	  * @copyright 2017/6/5
	  * @return  ["status"=>true, "data"=>[{"factory":"FERODO","display":"2114023"},...] ]
	  */
	public function getItemNumber($proId, $type, $page, $per)
	{
		$proId = (int) $proId;
		$arr = [];
		$per = empty($per) || $per < 1 ? 20: (int) $per;
		$page = empty($page) || $page < 1 ? 1: (int) $page;
		switch($type)
		{
			case 'main':
				$tableWhere = ' and arl_kind =1';
			break;
			case 'oe':
				$tableWhere = ' and arl_kind =3';
			break;
			case 'ref':
				$tableWhere = ' and arl_kind not in (2,3,5)';
			break;
			default:
				$tableWhere = ' and arl_kind not in (2,5)';
			break;
		}
		$table = 'tof_article_number 
		left join tof_suppliers on arl_sup_id = sup_id
		left join tof_brands on arl_bra_id=bra_id';
		$where = sprintf(" and arl_art_id=%d %s order by factory, arl_format asc limit %d, %d", $proId, $tableWhere, ($page-1)*$per, $per);
		$res = $this->get($table, ['case when arl_kind = 1 then sup_brand else bra_brand end factory', 'arl_number display', 'arl_kind'], $where);
		foreach($res['data'] as $v)
		{
			$arr[] = ['oe'=>$v['arl_kind'] == 3? 1: 0, 'factory'=>$v['factory'], 'display'=>$v['display']];
		}
		return LibFc::ReturnData(true, $arr);
	}


	
	/**
	  * @method 获取产品的宜配适用车型
	  * @param  
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=>["code"]=> int(0) } 
	  */
	public function getYpcModelId($proId)
	{
		$arr = [];
		$proId = (int) $proId;
		if($proId < 1) return LibFc::ReturnData(true, $arr);

		$res = $this->get('tof_article_number', ['arl_format'], sprintf(" and arl_kind =3 and arl_art_id =%d ", $proId));
		if(!empty($res['data']))
		{
			$oeNumArr = array_column($res['data'], 'arl_format');
			$YpcModel = new YpcModel();
			$res = $YpcModel->getM3idByNum($oeNumArr);
			$arr = $res['data'];
		}

		$mres = $this->get('tof_article_types', ['distinct lat_typ_id'], sprintf(" and lat_art_id =%d", $proId));
		if(!empty($mres['data']))
		{
			$YpcBase = new YpcBase();
			$res = $YpcBase->get('yp_model3_tcdcn_link join yp_model3 on mtl_m3_id = m3_id', ['distinct mtl_m3_id'], sprintf(" and mtl_typ_id in (%s)", implode(',', array_column($mres['data'], 'lat_typ_id'))));
			$arr = array_merge($arr, array_column($res['data'], 'mtl_m3_id'));
		}
		return LibFc::ReturnData(true, array_unique($arr));
	}



	/**
	  * @method 查找产品参数
	  * @param  $artIdArr   array [必填] [1,2,3...]
	  * @author soul
	  * @copyright 2017/4/13
	  * @return ["status"=>true, "data"=>[ ['label'=>'', 'value'=>'','unit'=>''],...]] 
	  */
	public function getArtParam($artIdArr)
	{
		$paramArr = [];
		if(!empty($artIdArr))
		{
			$filed = mvc::$cfg['dLang'] != 'en'? ['acr_art_id, cri_id, cri_name_cn cri_name, acr_value, cri_unit, acr_kv']: ['acr_art_id, cri_id, cri_name, acr_value, cri_unit, acr_kv, cri_full_name'];
			$table = 'tof_article_criteria
			join tof_criteria on acr_cri_id = cri_id
			left join ms_replace_criteria on rc_cri_id = cri_id and acr_ga_id = rc_ga_id';
			$Res = $this->get($table, $filed, sprintf(" and acr_art_id in (%s) group by acr_art_id, cri_id order by acr_art_id, min(rc_seq)", implode(',', $artIdArr)));
			foreach($Res['data'] as $Row)
			{
				$Lable = !empty($Row['cri_name'])? $Row['cri_name']: $Row['cri_full_name'];
				$Value = !empty($Row['acr_value'])? str_replace( ',', '.', $Row['acr_value']):  trim($Row['acr_kv']);
				$Unit = !empty($Row['cri_unit'])? $Row['cri_unit']: '';
				$paramArr[$Row['acr_art_id']][] = ['name'=>trim($Lable), 'val'=>trim($Value), 'unit'=>trim($Unit)];
			}
		}
		return LibFc::ReturnData(true, $paramArr);
	}


	/**
	  * @method 获取产品车型
	  * @param  
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=>["code"]=> int(0) } 
	  */
	public function getItemModel($proId)
	{
		$arr = [];
		$proId = (int) $proId;
		$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';
		$res = $this->get($table, ['mfa_id, mfa_brand, mfa_brand_cn, 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'], sprintf(" AND lat_art_id =%d GROUP BY typ_id order by mfa_brand, mod_name, typ_name", $proId));
		foreach($res['data'] as $v)
		{
			$arr[] = [
				'make_id'=>$v['mfa_id'],
				'make_name'=>mvc::$cfg['dLang'] != 'en'? $v['mfa_brand_cn']: $v['mfa_brand'],
				'mod2_name'=>$v['mod_name'],
				'mod3_id'=>$v['typ_id'],
				'mod3_name'=>$v['typ_name'],
				'mod3_start_year'=>$v['typ_start'],
				'mod3_end_year'=>$v['typ_end'],
				'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_typ_type'=>$v['typ_type'],
				'mod3_typ_id_org'=>$v['typ_id_org'],
				'mod3_cou_codes'=>$v['cou_codes'],
			];
		}
		return LibFc::ReturnData(true, $arr);
	}


	/**
	  * @method 获取TCD的号码外比较
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> ["code"]=> int(0) } 
	  */
	public function getNumContrast($oeArr)
	{
		if(empty($oeArr) || !is_array($oeArr))
			return LibFc::ReturnData(false, 'getOeContrast提供的参数不正确。');
		$temp = [];
		foreach($oeArr as $v)
		{
			$tt = explode(':', $v);
			$fac = count($tt) > 1? $tt[0]: 'OE';
			$num = count($tt) > 1? $tt[1]: $tt[0];
			$format = LibFc::FormatNum($num);
			if(strlen($format) > 0)
			{
				$unikey = strtoupper(str_replace(' ', '', $fac).'@'.$format);
				$temp[$unikey]['base'] = ['factory'=>$fac, 'display'=>$num, 'format'=>$format];
			}
		}
		$oeArr = $temp;
		$conArr = ['allKey'=>[], 'nums'=>[]];
		if(count($oeArr) > 0)
		{
			$allNumArr = [];
			$allNumKeyArr = [];
			foreach($oeArr as $unikey=>$v)
			{
				$table = 'tof_articles join tof_article_number on art_id=arl_art_id and arl_kind = 1 join tof_suppliers on sup_id = art_sup_id';
				$where = sprintf(" and art_id in (select arl_art_id from tof_article_number where arl_format ='%s' and arl_kind = 3) group by sup_brand, art_format", LibFc::Escape($v['base']['format']));
				$res = $this->get($table, ['sup_brand, art_number, art_format'], $where);
				foreach($res['data'] as $vv)
				{
					$vv['sup_brand'] = LibFc::ToEnFac($vv['sup_brand']);
					$uniqueKey = strtoupper(str_replace(' ', '', $vv['sup_brand']).'@'.$vv['art_format']);
					$v['nums'][$uniqueKey] = ['factory'=>$vv['sup_brand'], 'display'=>$vv['art_number']];
					
					$allNumKeyArr[$uniqueKey]['key'] = $uniqueKey;
					$allNumKeyArr[$uniqueKey]['count']++;
					$allNumKeyArr[$uniqueKey] = array_merge($allNumKeyArr[$uniqueKey], $v['nums'][$uniqueKey]);
				}
				$oeArr[$unikey] = $v;
			}

			$sortCount = [];
			$sortKey = [];
			foreach($allNumKeyArr as $unique=>$v)
			{
				$sortCount[$unique] = $v['count'];
				$sortKey[$unique] = $v['key'];
			}
			array_multisort($sortCount, SORT_DESC, $sortKey, SORT_ASC, $allNumKeyArr);

			/*foreach($allNumKeyArr as $k=>$v)
			{
				//unset($allNumKeyArr[$k]['key']);
			}*/
			$conArr['allKey'] = $allNumKeyArr;
			$conArr['bynum'] = $oeArr;
		}
		return LibFc::ReturnData(true, $conArr);	
	}


	/**
	  * @method 通过TCD的配件对比数据
	  * @param  $artIdArr   array  配件ID数组
	  * @author soul
	  * @copyright 2018/3/28
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> ["code"]=> int(0) } 
	  */
	public function getArtContrast($artIdArr)
	{
		if(empty($artIdArr) || !is_array($artIdArr))
			return LibFc::ReturnData(false, 'getArtContrast提供的参数不正确。');
		foreach($artIdArr as $k=>$v)
		{
			$artIdArr[$k] = (int) $v;
		}
		
		$res = $this->get('tof_articles join tof_suppliers on sup_id = art_sup_id', ['art_id', 'art_number', 'art_format', 'sup_brand'], sprintf(" and art_id in(%s) order by sup_brand, art_format", implode(',', $artIdArr)));
		$artArr = [];
		$allNumKeyArr = [];
		foreach($res['data'] as $v)
		{
			$v['sup_brand'] = LibFc::ToEnFac($v['sup_brand']);
			$uniqueKey = strtoupper(str_replace(' ', '', $v['sup_brand']).'@'.$v['art_format']);
			$artArr[$v['art_id']] = [
				'base'=>['factory'=>$v['sup_brand'], 'display'=>$v['art_number'], 'format'=>$v['art_format']],
				'nums'=>[$uniqueKey=>['factory'=>$v['sup_brand'], 'display'=>$v['art_number']]]
			];
			$allNumKeyArr[$uniqueKey] = $artArr[$v['art_id']]['nums'][$uniqueKey];
			$allNumKeyArr[$uniqueKey]['key'] = $uniqueKey;
			$allNumKeyArr[$uniqueKey]['count']++;
		}
		if(!empty($artArr))
		{
			$table = 'tof_article_number left join tof_brands on arl_bra_id=bra_id';
			$where = sprintf(" and arl_kind in (3,4) and arl_art_id in (%s) group by arl_art_id, bra_brand, arl_format", implode(',', array_keys($artArr)));
			$res = $this->get($table, ['arl_art_id', 'arl_kind', 'bra_brand', 'arl_number', 'arl_format'], $where);
			foreach($res['data'] as $v)
			{
				$v['bra_brand'] = $v['arl_kind'] == 3? 'OE': LibFc::ToEnFac($v['bra_brand']);
				$uniqueKey = strtoupper(str_replace(' ', '', $v['bra_brand']).'@'.$v['arl_format']);
				if(isset($artArr[$v['arl_art_id']]['nums'][$uniqueKey])) continue;
				$artArr[$v['arl_art_id']]['nums'][$uniqueKey] = ['factory'=>$v['bra_brand'], 'display'=>$v['arl_number']];
				if(!isset($allNumKeyArr[$uniqueKey])) $allNumKeyArr[$uniqueKey] = $artArr[$v['arl_art_id']]['nums'][$uniqueKey];
				$allNumKeyArr[$uniqueKey]['key'] = $uniqueKey;
				$allNumKeyArr[$uniqueKey]['count']++;
			}
		}

		$sortCount = [];
		$sortKey = [];
		foreach($allNumKeyArr as $unique=>$v)
		{
			$sortCount[$unique] = $v['count'];
			$sortKey[$unique] = $v['key'];
		}
		array_multisort($sortCount, SORT_DESC, $sortKey, SORT_ASC, $allNumKeyArr);
		$conArr = [];
		$conArr['allKey'] = $allNumKeyArr;
		$conArr['bynum'] = $artArr;
		return LibFc::ReturnData(true, $conArr);	
	}

	/**
	  * @method 获取厂商BRANDID
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> ["code"]=> int(0) } 
	  */
	public function getBrandId($brand)
	{
		$braIdArr = [];
		if(!empty($brand))
		{
			$res = $this->get('tof_brands', ['bra_id'], sprintf(" AND bra_brand='%s'", LibFc::Escape($brand)));
			$braIdArr = array_column($res['data'], 'bra_id');
		}
		return LibFc::ReturnData(true, $braIdArr);
	}


	/**
	  * @method 重建TCD知名品牌的车型/发动机关联表 MS_SUP_TYPES、MS_SUP_ENGINES
	  * @param  supIdArr              array [选填] 默认所有
	  * @author soul
	  * @copyright 2019/5/27
	  * @return {"status":true,"data":true,"code":0,"encry":0,"runtime":74}
	  */
	public function resetFacTypLink($supIdArr = [])
	{
		set_time_limit(0);
		if(empty($supIdArr))
		{
			$B2cBase = new B2cBase();
			$res = $B2cBase->get('sh_super', ['ssu_tcd_supid'], sprintf(" and ssu_tcd_supid > 0 and ssu_style = 'TCD'"));
			$supIdArr = array_column($res['data'], 'ssu_tcd_supid');
		}
		else
		{
			foreach($supIdArr as $k=>$v)
			{
				$supIdArr[$k] = (int) $v;
			}
		}
		if(!empty($supIdArr))
		{
			$this->TCDMDb->Begin();
			$sqlArr = [
				sprintf("delete from ms_sup_types where sut_sup_id in (%s)",  implode(',', $supIdArr)),
				sprintf("insert into ms_sup_types (sut_sup_id, sut_brand_id, sut_mod_id, sut_typ_id) 
					select lat_sup_id, typ_mfa_id, typ_mod_id, lat_typ_id from 
					tof_article_types 
					join tof_types on typ_id = lat_typ_id 
					where lat_sup_id in (%s) group by lat_sup_id, lat_typ_id", implode(',', $supIdArr)),
			];
			foreach($sqlArr as $sql)
			{
				$ResFlag = $this->TCDMDb->Query($sql);
				if(!$ResFlag) return LibFc::ReturnData(false, $sql.'执行失败。');
			}
		
			$sqlArr = [
				sprintf("delete from ms_sup_engines where sue_sup_id in (%s)",  implode(',', $supIdArr)),
				sprintf("insert into ms_sup_engines (sue_sup_id, sup_eng_id) select sut_sup_id, lte_eng_id from ms_sup_types join tof_types_engines on lte_typ_id = sut_typ_id
					where sut_sup_id in (%s) group by sut_sup_id, lte_eng_id", implode(',', $supIdArr)),
			];
			foreach($sqlArr as $sql)
			{
				$ResFlag = $this->TCDMDb->Query($sql);
				if(!$ResFlag) return LibFc::ReturnData(false, $sql.'执行失败。');
			}
			$this->TCDMDb->End();
		}
		return LibFc::ReturnData(true);	
	}


	/**
	  * @method 获取产品的安装位置
	  * @param  $proIdArr array 产品ID 
	  * @param  $typIdArr array 车型ID
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=>["code"]=> int(0) } 
	  */
	public function getItemLocation($proIdArr, $mod3Id=0, $typIdArr = [])
	{
		foreach($proIdArr as $k=>$v)
		{
			$proIdArr[$k] = (int)$v;
		}
		$arr = [];
		$cri_id = 100;
		if(!empty($proIdArr))
		{
			$locationArr = [];
			if(mvc::$cfg['dLang'] != 'en')
			{
				$YpcBase = new YpcBase();
				$res = $YpcBase->get('yp_location', ['loc_word_format, loc_word_cn']);
				$locationArr = array_column($res['data'], 'loc_word_cn', 'loc_word_format');
			}

			if(!empty($typIdArr))
			{
				foreach($typIdArr as $k=>$v)
				{
					$typIdArr[$k] = (int)$v;
				}

				$res = $this->get('tof_article_types_criteria', ['lac_art_id art_id, lac_kv'], sprintf(" and lac_cri_id=%d AND lac_art_id in (%s) and lac_typ_id in (%s)", $cri_id, implode(',', $proIdArr), implode(',', $typIdArr)));
				foreach($res['data'] as $v)
				{
					if(!empty($v['lac_kv']))
						$arr[$v['art_id']][] = $v['lac_kv'];
				}
			}
			$noIdArr = array_diff($proIdArr, array_keys($arr));
			//产品参数补齐
			if(!empty($noIdArr))
			{
				$res = $this->get('tof_article_criteria', ['acr_art_id art_id, acr_kv'], sprintf(" and acr_cri_id=%d and acr_art_id in (%s)", $cri_id, implode(',', $noIdArr)));
				foreach($res['data'] as $v)
				{
					if(!empty($v['acr_kv']))
						$arr[$v['art_id']][] = $v['acr_kv'];
				}
			}

			$noIdArr = array_diff($proIdArr, array_keys($arr));
			//其它车型位置补齐
			if(!empty($noIdArr))
			{
				$res = $this->get('tof_article_types_criteria', ['lac_art_id art_id, lac_kv'], sprintf("and lac_cri_id=%d and lac_art_id in (%s)", $cri_id, implode(',', $proIdArr)));
				foreach($res['data'] as $v)
				{
					if(!empty($v['lac_kv']))
						$arr[$v['art_id']][] = $v['lac_kv'];
				}
			}
			
			foreach($arr as $proId=>$v)
			{
				$v = array_unique($v);
				if(mvc::$cfg['dLang'] != 'en')
				{
					foreach($v as $kk=>$vv)
					{
						$fvv = LibFc::FormatNum($vv);
						if(!empty($locationArr[$fvv]))
							$v[$kk] = $locationArr[$fvv];
					}
				}
				sort($v);
				$arr[$proId] = $v;
			}
		}
		return LibFc::ReturnData(true, $arr);
	}
}