<?php
/**
  * @method 宜配车型
  * @author soul
  * @copyright 2017/9/30
  */
class YpcModel extends YpcBase
{
	/**
	  * @method 查询车型
	  * @param  $level 1 2 3
	  * @param  $fieldArr 需要查询的字段名称数组
	  * @param  $where 查询条件
	  * @param  $oneRow true|false
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> $OneRow=false 二维数组 ; $OneRow=true 一维数组["code"]=> int(0) } 
	  */
	public function getModel($level, $fieldArr, $where = '', $oneRow = false)
	{
		$level = (int) $level;
		switch($level)
		{
			case 1: 
				$table = 'yp_model1
					JOIN yp_make ON m1_m_id = m_id
					LEFT JOIN yp_brand ON m1_b_id = b_id';
			break;
			case 2: 
				$table = 'yp_model2
				JOIN yp_model1 ON m2_m1_id = m1_id
				JOIN yp_make ON m1_m_id = m_id
				LEFT JOIN yp_brand ON m1_b_id = b_id';
			break;
			case 3: 
				$table = 'yp_model3
				JOIN yp_model2 ON m3_m2_id = m2_id
				JOIN yp_model1 ON m2_m1_id = m1_id
				JOIN yp_make ON m1_m_id = m_id
				LEFT JOIN yp_brand ON m1_b_id = b_id';
			break;
		}
		if($table != '')
		{
			return $this->get($table, $fieldArr, $where, $oneRow);
		}
		return LibFc::ReturnData(false, 'getModel 查询提供的参数不正确。');
	}

	/**
	  * @method 查询VIN信息
	  * @param  $fieldArr 需要查询的字段名称数组
	  * @param  $where 查询条件
	  * @param  $oneRow true|false
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> $OneRow=false 二维数组 ; $OneRow=true 一维数组["code"]=> int(0) } 
	  */
	public function getModel3Vin($fieldArr, $where = '', $oneRow = false)
	{
		$table = 'yp_vin_model3_link
		JOIN yp_vin ON vm3_vin_id = vin_id
		JOIN yp_model3 ON vm3_m3_id = m3_id
		JOIN yp_model2 ON m3_m2_id = m2_id
		JOIN yp_model1 ON m2_m1_id = m1_id
		JOIN yp_make ON m1_m_id = m_id
		LEFT JOIN yp_brand ON m1_b_id = b_id';
		return $this->get($table, $fieldArr, $where, $oneRow);
	}

	/**
	  * @method 查询工信部型号
	  * @param  $fieldArr 需要查询的字段名称数组
	  * @param  $where 查询条件
	  * @param  $oneRow true|false
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> $OneRow=false 二维数组 ; $OneRow=true 一维数组["code"]=> int(0) } 
	  */
	public function getModelGxbNum($fieldArr, $where = '', $oneRow = false)
	{
		$table = 'yp_model3_gxb_num
		JOIN yp_model3 ON mg_m3_id = m3_id
		JOIN yp_model2 ON m3_m2_id = m2_id
		JOIN yp_model1 ON m2_m1_id = m1_id
		JOIN yp_make ON m1_m_id = m_id';
		return $this->get($table, $fieldArr, $where, $oneRow);
	}

	
	/**
	  * @method 通过OE查找宜配车型库的适用车型
	  * @param  $numArr 号码数组
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> [1,2,3]["code"]=> int(0) } 
	  */
	public function getM3idByNum($numArr)
	{
		if(empty($numArr) || !is_array($numArr))
		{
			return LibFc::ReturnData(false, 'getM3idByNum 查询提供的参数不正确。');
		}
		foreach($numArr as $k=>$v)
		{
			$numArr[$k] = LibFc::FormatNum($v);
		}
		return $this->getM3idByNumEtk($numArr);
	}


	/**
	  * @method 通过OE查找宜配车型库的适用车型【ETK】
	  * @param  $numArr 号码数组
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> [1,2,3]["code"]=> int(0) } 
	  */
	private function getM3idByNumEtk($numArr)
	{
		$m3IdArr = [];
		$EtkBasic = new EtkBasic();
		$res = $EtkBasic->getFieldUniVal('data_product_search', 'src_pro_id', sprintf('and src_format in ("%s")', implode('","', $numArr)));
		if(!empty($res['data']))
		{
			$pidArr = $res['data'];
			$table = 'data_vins join data_product_vins on vin_id=ppv_vin_id';
			$where = sprintf('and ppv_pro_id in (%s)', implode(',', $pidArr));
			$res = $EtkBasic->getFieldUniVal($table, 'vin_name', $where);
			if(!empty($res['data']))
			{
				$vinNameArr = $res['data'];
				$codeArr = [];
				foreach($vinNameArr as $v)
				{
					if(stripos($v, '@') > 0)
						$codeArr['modcode'] = 'modcode';
					else
						$codeArr['etkcode'] = 'etkcode';
					if(count($codeArr['etkcode']) == 2) break;
				}
				foreach($codeArr as $codeType)
				{
					$res = $this->getMod3IdByCode($vinNameArr, $codeType);
					$m3IdArr = array_merge($m3IdArr, $res['data']);
				}
			}
		}
		return LibFc::ReturnData(true, array_unique($m3IdArr));
	}
	/**
	 * @method    根据车型Id获取保养件id
	 * @param     [int]       $m3id [车型id]
	 * @return    [arr]             ['status'=>true,'data'=>[['cat_id'=>1],['cat_id'=>2]]]
	 * @author lu
	 * @copyright 2019-01-08
	 */
	public function getByjByM3Id($m3id,$fiels=[],$where='')
	{
		if(LibFc::Int($m3id))
		{
			$where.=sprintf(' and cat_m3_id=%d',$m3id);
			$fiels=!empty($fiels)?$fiels:['cat_id'];
			$res=$this->get('temp_chapeijian',$fiels,$where);
			return LibFc::ReturnData($res['status'], $res['data']);
		}
		return LibFc::ReturnData(false, '车型ID不能为空');
	}


	/**
	  * @method 检查这个车型是否有EPC数据
	  * @param  mod3Id 车型ID
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":'error'}
	  */
	public function checkModelPart($mod3Id)
	{
		$res=$this->getModel(3, ['b_etk_id as etk_id'], sprintf(' and m3_id=%d and b_etk_id>0 and b_etk_id<999', $mod3Id),true);
		$arr = $res['data'];
		$arr['has'] = 0;
		$etkId = $arr['etk_id'];
		if(!empty(mvc::$cfg['DB']['EPC'][$etkId]))
		{
			switch($etkId)
			{
				case 8:
					$res = $this->get('yp_model3_modcode', ['distinct mem_code as mem_code'], sprintf(" and mem_m3_id=%d", $mod3Id));
				    $etkCodeArr = array_column($res['data'], 'mem_code');
					/*if(empty($res['data'])) LibFc::ReturnData(true, $arr);
					$etkCodeArr = [];
					foreach($res['data'] as $mv)
					{
						//ACV40L-JEAGKC@05/2006-01/2009
						$t = explode('@', $mv['mem_code']);
						$tt = explode('-', $t[1]);
						$etkCodeArr[] = $t[0].'@'.$tt[0];
					}
					$etkCodeArr = array_unique($etkCodeArr);*/
				break;
				default:
					$res = $this->get('yp_model3_etk_code', ['distinct mec_code as mec_code'], sprintf(" and mec_m3_id=%d", $mod3Id));
					$etkCodeArr = array_column($res['data'], 'mec_code');
				break;
			}
			$EpcItem = new EpcItem($etkId);
			$res = $EpcItem->getGroup($etkCodeArr, $mod3Id);
			$arr['has'] = empty($res['data']) ? 0: 1;
		}
		return LibFc::ReturnData(true, $arr);
	}



	/**
	  * @method 通过原厂代码或者etk代码查找的适用车型ID
	  * @param  codeArr ['aa',...]
	  * @param  codeType etkcode || modcode
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":true,"data": [11,22,333...],"code":""}
	  {"status":false,"data":'失败原因',"code":'error'}
	  */
	public function getMod3IdByCode($codeArr, $codeType = 'etkcode')
	{
		$m3IdArr = [];
		if(!empty($codeArr))
		{
			$codeArr = array_unique($codeArr);
			$YpcBase = new YpcBase();
			if($codeType == 'modcode')
			{
				$res = $this->get('yp_model3_modcode', ['DISTINCT mem_m3_id'], sprintf(" and mem_code in ('%s')", implode("','", $codeArr)));
				$m3IdArr = array_unique(array_column($res['data'], 'mem_m3_id'));
			}
			else
			{
				$res = $this->get('yp_model3_etk_code', ['DISTINCT mec_m3_id'], sprintf(" and mec_code in ('%s')", implode("','", $codeArr)));
				$m3IdArr = array_unique(array_column($res['data'], 'mec_m3_id'));
			}
		}
		return LibFc::ReturnData(true, $m3IdArr);
	}

	/**
	  * @method 获取型号的扩展数据
	  * @param  gxbArr ['aa',...]
	  * @param  cof [
			'gxbqpr'=>0|1(轮胎数/驾驶室准乘人数),
			'gxbimg'=>0|1(取图),
		]
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":true,"data": [11,22,333...],"code":""}
	  {"status":false,"data":'失败原因',"code":'error'}
	  */
	function getGxbExtInfo($gxbArr, $cof = [])
	{
		$cof = ['gxbqpr'=>1, 'gxbimg'=>1];
		$arr = [];
		$formatArr = [];
		foreach($gxbArr as $gxb)
		{
			$t = LibFc::FormatNum($gxb);
			if(!empty($t)) $formatArr[] = $t;
		}
		if(!empty($formatArr))
		{
			$formatArr = array_unique($formatArr);
			
			$YPCAR67Db = new BaseDb(mvc::$cfg['DB']['YPCAR67']);
			$res = $YPCAR67Db->get('model_chinacar.all_model', ['clxh', 'group_concat(distinct tymc) as rname', 'group_concat(distinct bsqlx) as rtran', 'group_concat(distinct qdxs) as rdrive'], sprintf(" and clxh != '' and clxh in ('%s') group by clxh", implode("','", $gxbArr)));
			$arr = array_column($res['data'], null, 'clxh');
			$res = $YPCAR67Db->get('model_chinacar.all_new_energy', ['nen_format', 'nen_name as rname', "group_concat(distinct case when nen_endurance_1 != '' then nen_endurance_1 else nen_endurance_2 end) as rxuhang"], sprintf(" and nen_format != '' and nen_format in ('%s') group by nen_format", implode("','", $gxbArr)));
			foreach($res['data'] as $v)
			{
				if(empty($arr[$v['nen_format']]['rname']) && !empty($v['rname']))
					$arr[$v['nen_format']]['rname'] = $v['rname'];

				$arr[$v['nen_format']]['rxuhang'] = $v['rxuhang'];
			}
			if(!empty($cof['gxbqpr']))
			{
				$renameArr = [
					'轮胎数'=>'tyre',
					'驾驶室准乘人数'=>'frontman',
				];
				//轮胎数、前排
				$res = $YPCAR67Db->get('model_jdcsww.data_model join model_jdcsww.data_info ON jml_id = ino_jml_id', ['jml_model_format', 'ino_text', 'group_concat(distinct ino_value) as vals'], sprintf(" and ino_value != '' and jml_model_format in ('%s') AND ino_text IN ('轮胎数','驾驶室准乘人数') group by jml_model_format, ino_text", implode("','", $gxbArr)));
				foreach($res['data'] as $v)
				{
					$filed = strtr($v['ino_text'], $renameArr);
					$arr[$v['jml_model_format']][$filed] = $v['vals'];
				}
			}

			if(!empty($cof['gxbimg']))
			{
				$res = $this->get('yp_image_gxb', ["gxb_code, CONCAT('https://3thpics.paojd.cn/etk/', gxb_path, '/', gxb_name) AS imgurl"], sprintf(" and gxb_code in ('%s') group by gxb_code", implode("','", $gxbArr)));
				foreach($res['data'] as $v)
				{
					$arr[$v['gxb_code']]['imgurl'] = $v['imgurl'];
				}
			}
		}
		return LibFc::ReturnData(true, $arr);
	}
}