<?php
/**
  * @method 宜配车型库管理
  * @author soul
  * @copyright 2016/3/31
  */
class Ypc2Model extends Ypc2Base
{
	/**
	  * @method 获取车型
	  * @param  $level 1|2|3
	  * @param  $FieldArr 需要查询的字段名称数组
	  * @param  $Where 查询条件
	  * @param  $OneRow true|false
	  * @author soul
	  * @copyright 2017/6/5
	  * @return 
			成功 ["status"=>true, "data"=> $OneRow=false 二维数组 ; $OneRow=true 一维数组] 
			失败 ["status"=>false, "data"=> 错误信息] 
	  */
	public function getModel($Level, $FieldArr, $Where = '', $OneRow = false)
	{
		$Level = (int) $Level;
		switch($Level)
		{
			case 1: 
				$table = 'bc_model1
				join bc_make on mod1_make_id = make_id
				join bc_brand on mod1_brand_id = brand_id';
			break;
			case 2: 
				$table = 'bc_model2
				join bc_model1 on mod2_mod1_id = mod1_id
				join bc_make on mod1_make_id = make_id
				join bc_brand on mod1_brand_id = brand_id';
			break;
			case 3: 
				$table = 'bc_model3
				join bc_model2 on mod3_mod2_id = mod2_id
				join bc_model1 on mod2_mod1_id = mod1_id
				join bc_make on mod1_make_id = make_id
				join bc_brand on mod1_brand_id = brand_id';
			break;
		}
		return $this->get($table, $FieldArr, $Where, $OneRow);
	}


	/**
	  * @method 通过车型ID获取车型信息
	  * @param  $m3IdArr array  [11,2,...]
	  * @param  $cofArr array ['type'=>'page', 'page'=>1, 'per'=>1]
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> [1,2,3]["code"]=> int(0) } 
	  */
	public function getModelListByMod3Ids($m3IdArr, $cofArr)
	{
		$arr = [];
		if(!empty($m3IdArr))
		{
			switch($cofArr['type'])
			{
				case 'page':
					$per = empty($cofArr['per']) || $cofArr['per'] < 1? 20: (int) $cofArr['per'];
					$page = empty($cofArr['page']) || $cofArr['page'] < 1? 1: (int) $cofArr['page'];
					$table = 'bc_model3
						join bc_model2 on mod3_mod2_id = mod2_id
						join bc_model1 on mod2_mod1_id = mod1_id
						join bc_make on mod1_make_id = make_id
						join bc_brand on mod1_brand_id = brand_id';
					$where = sprintf(" and mod3_id in (%s) order by brand_seq, brand_initial, make_seq, make_initial, mod1_name, mod2_name, mod3_name
					limit %d, %d", implode(',', $m3IdArr), ($page-1)*$per, $per);
					$filed = ['mod3_id, brand_id, brand_name, make_id, case when make_show_brand =1 then brand_name else make_name end make_name, mod2_name, mod3_name, mod3_start_year, mod3_end_year, mod2_start_year, mod2_end_year, mod3_engine, mod3_kw, mod3_cc, mod3_transmission, mod3_fuel, mod3_body, mod3_front_brake, mod3_rear_brake, mod3_parking_brake, mod3_drive'];
					$res = $this->get($table, $filed, $where);
					$arr = $res['data'];
				break;
			}
		}
		return LibFc::ReturnData(true, $arr);
	}
}