<?php
/**
  * @class 车型库操作类
  * @author soul
  * @copyright 2017/4/13
  */
class modelAction extends baseAction
{
	/**
	  * @method 获取宜配车型库品牌
	  * @http  get
	  * @param  token              string [选填] 调用接口凭证 (post|get)
	  * @param  type               string [选填] 默认所有  etk：EPC查询
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data":[{"brand_id":"6","brand_name":"奥迪","brand_keyword":"AD,AUDI,奥迪","brand_initial":"AD","brand_code":"AD"},{"brand_id":"125","brand_name":"阿尔法·罗密欧","brand_keyword":"阿尔法;罗密欧","brand_initial":"AEFLMO","brand_code":"AEFLMO"}...],"code":0}
			{"status":false,"data":"失败原因","code":"错误代码"}
	  */
	public function brand()
	{
		$Params = mvc::$URL_PARAMS;
		$where = !empty($Params['type']) && $Params['type'] == 'epc'? ' and brand_etk_id > 0 order by brand_initial,brand_seq': ' order by brand_initial, brand_seq';
		$Ypc2Base = new Ypc2Base();
		$res = $Ypc2Base->get('bc_brand', ['brand_id', 'brand_name', 'brand_keyword', 'brand_code', 'brand_initial'], $where);
		LibFc::apiReturn($res);
	}
	
	/**
	  * @method 获取某品牌的生产商
	  * @http  get
	  * @param  token              string [选填] 调用接口凭证 (post|get)
	  * @param  brand_id           int    [必填] 品牌ID
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data":[{"make_id":"1","make_name":"北京奔驰"},{"make_id":"22","make_name":"福建奔驰"},{"make_id":"149","make_name":"奔驰(进口)"}],"code":0}
			{"status":false,"data":"失败原因","code":"错误代码"}
	  */
	public function brandMake()
	{
		$Params = mvc::$URL_PARAMS;
		$brandId = (int) $Params['brand_id'];
		if($brandId < 1)
			LibFc::ReturnJson(false, '品牌ID参数不正确！');
		$Ypc2Model = new Ypc2Model();
		$res = $Ypc2Model->getModel(1, ['make_id, make_name'], sprintf(" and mod1_brand_id=%d group by make_id order by make_seq", $brandId));
		LibFc::apiReturn($res);
	}


	/**
	  * @method 获取某品牌的一级车型
	  * @http  get
	  * @param  token              string [选填] 调用接口凭证 (post|get)
	  * @param  brand_id           int [必填] 品牌ID
	  * @param  make_id            int [选填] 生产商ID
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data":[{"brand_code":"BC","brand_id":"3","brand_name":"奔驰","make_id":"1","make_name":"北京奔驰","mod1_id":"1","mod1_name":"C级"}....],"code":0}
			{"status":false,"data":"失败原因","code":"错误代码"}
	  */
	public function model1()
	{
		$Params = mvc::$URL_PARAMS;
		$brandId = (int) $Params['brand_id'];
		if($brandId < 1)
			LibFc::ReturnJson(false, '品牌ID参数不正确！');
		$where = sprintf(" and mod1_brand_id=%d ", $brandId);
		if(!empty($Params['make_id']))
			$where .= sprintf(" and make_id=%d order by mod1_name", (int) $Params['make_id']);
		$Ypc2Model = new Ypc2Model();
		$res = $Ypc2Model->getModel(1, ['brand_code', 'brand_id','brand_name', 'make_id','make_name', 'mod1_id', 'mod1_name'], $where);
		LibFc::apiReturn($res);
	}


	/**
	  * @method 获取某品牌的二级车型
	  * @http  get
	  * @param  token              string [选填] 调用接口凭证 (post|get)
	  * @param  mod1_id            int [必填] 一级车型ID
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data":[{"brand_code":"BC","brand_id":"3","brand_name":"奔驰","make_id":"1","make_name":"北京奔驰","mod1_id":"1","mod1_name":"C级","mod2_id":"1","mod2_name":"C级 (W204)","mod2_start_year":"2007","mod2_end_year":"2014"}...],"code":0}
			{"status":false,"data":"失败原因","code":"错误代码"}
	  */
	public function model2()
	{
		$Params = mvc::$URL_PARAMS;
		$mod1Id = (int) $Params['mod1_id'];
		if($mod1Id < 1)
			LibFc::ReturnJson(false, '车型ID参数不正确！');
		$where = sprintf(" and mod1_id=%d order by mod2_name asc, mod2_start_year desc", $mod1Id);
		$Ypc2Model = new Ypc2Model();
		$res = $Ypc2Model->getModel(2, ['brand_code', 'brand_id','brand_name', 'make_id','make_name', 'mod1_id', 'mod1_name', 'mod2_id', 'mod2_name', 'mod2_start_year', 'mod2_end_year'], $where);
		LibFc::apiReturn($res);
	}


	/**
	  * @method 获取某品牌的二级车型
	  * @http  get
	  * @param  token              string [选填] 调用接口凭证 (post|get)
	  * @param  mod1_id            int    [选填] 一级车型ID
	  * @param  mod2_id            int    [选填] 二级车型ID
	  * @param  cc                 string [选填] 排量
	  * @param  keyword            string [选填] 关键字
	      brand_id、keyword、mod1_id、mod2_id 必填一个
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data":[{"brand_code":"BC","brand_id":"3","brand_name":"奔驰","make_id":"1","make_name":"北京奔驰","mod1_id":"1","mod1_name":"C级","mod2_id":"3","mod2_name":"C级 (W205) 加长版","mod2_start_year":"2015","mod2_end_year":"0","mod3_id":"4287","mod3_mod2_id":"3","mod3_mod1_id":"1","mod3_brand_id":"3","mod3_make_id":"1","mod3_name":"C350 El","mod3_start_year":"2016","mod3_end_year":"0","mod3_kw":"155","mod3_cc":"1991","mod3_engine":"274.920","mod3_transmission":"7A\/MT","mod3_fuel":"油电混合","mod3_body":"4门5座三厢车","mod3_front_brake":"通风盘式","mod3_rear_brake":"盘式","mod3_parking_brake":"电子驻车","mod3_drive":"前置后驱","mod3_keyword":"W205C350274920M274C350El","mod3_cc_name":"2.0"}...],"code":0}
			{"status":false,"data":"失败原因","code":"错误代码"}
	  */
	public function model3()
	{
		$Params = mvc::$URL_PARAMS;
		$where = '';
		if(!empty($Params['brand_id'])) $where .= sprintf(" and brand_id=%d", (int) $Params['brand_id']);
		if(!empty($Params['mod1_id'])) $where .= sprintf(" and mod1_id=%d", (int) $Params['mod1_id']);
		if(!empty($Params['mod2_id'])) $where .= sprintf(" and mod2_id=%d", (int) $Params['mod2_id']);
		if(!empty($Params['keyword']) && strlen(trim($Params['keyword'])) > 1)
		{
			$word = strtoupper(LibFc::Escape($Params['keyword']));
			$where .= sprintf(" and (upper(mod2_name) like '%%%s%%' or upper(mod3_keyword) like '%%%s%%')", $word, $word);
		}
		if(!empty($Params['cc']))
			$where .= sprintf(" and mod3_cc_name = '%s' ", LibFc::Escape($Params['cc']));

		if($where != '')
		{
			$where .= 'order by make_seq, make_initial, make_name, mod2_start_year desc, mod3_start_year desc, mod3_transmission asc';
			$Ypc2Model = new Ypc2Model();
			$res = $Ypc2Model->getModel(3, ['brand_code', 'brand_id','brand_name', 'make_id','make_name', 'mod1_id', 'mod1_name', 'mod2_id', 'mod2_name', 'mod2_start_year', 'mod2_end_year', 'bc_model3.*'], $where);
			LibFc::apiReturn($res);
		}
		LibFc::ReturnJson(false, 'model3提供的参数不正确！');
	}
}