<?php
/**
*商城车型库【宜配车型库中文版】
*/
class ypc2Action extends baseAction
{
	/**
	  * @method 获取品牌信息
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  data               array  [必填] 
												 [
													'filed'=>['brand_id', ....]
													'where'=>'and brand_id=111'
													'oneRow'=>true|false
												 ]						
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":'error'}
	  */
	public function getBrand()
	{
		if(is_array($_POST['filed']) && !empty($_POST['where']))
		{
			$oneRow = isset($_POST['oneRow'])? $_POST['oneRow']: false;
			$Ypc2Base = new Ypc2Base();
			$res = $Ypc2Base->get('bc_brand', $_POST['filed'], $_POST['where'], $oneRow);
			echo json_encode($res);
		}
		else
		{
			LibFc::ReturnJson(false, '参数错误');
		}
	}


	/**
	  * @method 获取所有汽车品牌信息
	  * @http  get
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":'error'}
	  */
	public function getAllBrand()
	{
		$Ypc2Base = new Ypc2Base();
		$res = $Ypc2Base->get('bc_brand', ['brand_id', 'brand_name', 'brand_initial', 'brand_etk_id'], ' order by brand_initial');
		echo json_encode($res);
	}



	/**
	  * @method 根据关键字匹配品牌
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  word               array  [必填] 风行@力帆@全球鹰@吉利@英伦@一汽@比亚迪@哈弗@长城				
	  * @author soul
	  * @copyright 2019/5/23
	  * @return {"status":true,"data":{"brand_id":"41","brand_name":"\u4e1c\u98ce\u98ce\u884c"},"code":0}
	  */
	public function matchOneBrand()
	{
		$Params = mvc::$URL_PARAMS;
		$arr = [];
		if(!empty($Params['word']))
		{
			$Ypc2Base = new Ypc2Base();
			foreach(explode('@', $Params['word']) as $word)
			{
				$res = $Ypc2Base->get('bc_brand', ['brand_id', 'brand_name'], sprintf(" and brand_keyword like '%%%s%%' limit 1", LibFc::Escape($word)), true);
				if(!empty($res['data']))
				{
					$arr = $res['data'];
					break;
				}
			}
		}
		LibFc::ReturnJson(true, $arr);
	}



	/**
	  * @method 获取车型
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  data               array  [必填] 
												 [
													'level'=>1|2|3
													'filed'=>['brand_id', ....]
													'where'=>'and brand_id=111'
													'oneRow'=>true|false
												 ]						
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":'error'}
	  */
	public function getModel()
	{
		if(!empty($_POST['level']) && in_array($_POST['level'], [1,2,3])  && is_array($_POST['filed']) && !empty($_POST['where']))
		{
			$oneRow = isset($_POST['oneRow'])? $_POST['oneRow']: false;
			$Ypc2Model = new Ypc2Model();
			$res = $Ypc2Model->getModel($_POST['level'], $_POST['filed'], $_POST['where'], $oneRow);
			echo json_encode($res);
		}
		else
		{
			LibFc::ReturnJson(false, '参数错误');
		}
	}


	/**
	  * @method 获取车型的详细信息
	  * @http  get
	  * @param  token              string [必填] 调用接口凭证 (post|get)	
	   * @param  mod3Id             int [必填] 车型ID
	  * @param  vin                string [选填] 17位车架号  LBELMBKC1EY570664
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":'error'}
	  */
	public function getModel3Des()
	{
		$Params = mvc::$URL_PARAMS;
		$arr = [];
		if(!empty($Params['mod3Id']))
		{
			$Ypc2Model = new Ypc2Model();
			$res = $Ypc2Model->getModel(3, ['brand_id', 'brand_etk_id', 'brand_code', 'make_show_brand', 'brand_name', 'make_id', 'make_name', 'mod1_name', 'mod2_name', 'mod2_pc', 'mod2_cv', 'bc_model3.*'], sprintf(" and mod3_id=%d", $Params['mod3Id']), true);
			if(!empty($res['data']))
			{
				$arr = $res['data'];
				$arr['mod_pic'] = mvc::$cfg['urls']['b2c'].'static/images/chebiao/70/'.$arr['brand_code'].'.png';
				if(!empty($Params['vin']))
				{
					$YpcVin = new YpcVin();
					$res = $YpcVin->getVinBaseInfo($Params['vin']);
					$arr = array_merge($arr, $res['data']);
					$res = $YpcVin->getYpcModelVinImg($Params['mod3Id'], $Params['vin']);
					if(!empty($res['data'])) $arr['mod3_imgs'] = implode(',', $res['data']);
				}
			}
		}
		LibFc::ReturnJson(true, $arr);
	}

	/**
	  * @method 搜索
	  * @http POST  
	  * @param  word string 关键字
	  * @param  count int 返回数量 默认 10000
	  * @author soul 
	  * @return
	  */
	public function searchByEs()
	{
		$blparams = ['word'=>$_POST['word'], 'count'=>(int)$_POST['count']];
		$Ypc2Elastic = new Ypc2Elastic();
		$res = $Ypc2Elastic->searchId($blparams);
		LibFc::EchoJson($res);
	}
}