<?php
/**
*客户接口
*/
class clientAction extends baseAction
{
	/**
	  * @method 获取宜配车型库
	  * @http  get
	  * @param  token              string [必填] 调用接口凭证 (post|get)			
	  * @author soul
	  * @copyright 2019/11/26
	  * @return 
	  */
	public function getYpcModel()
	{
		ini_set('memory_limit', '512M');
		set_time_limit(0);
		if(!empty(mvc::$cfg['RQTOKEN']['brand_ids_cn'])){
			$file = mvc::$cfg['PATH_CACHE'].'ypcModel_'.mvc::$cfg['RQTOKEN']['user'].'.json';
		}else{
			$file = mvc::$cfg['PATH_CACHE'].'ypcModel.json';
		}
		$arr = [];
		//缓存1小时
		if(!file_exists($file) || (filemtime($file) < (time()-3600)))
		{
			$Ypc2Model = new Ypc2Model();
			$where = !empty(mvc::$cfg['RQTOKEN']['brand_ids_cn'])? sprintf(" and brand_id in (%s)", implode(',', mvc::$cfg['RQTOKEN']['brand_ids_cn'])): '';
			$filed = ['brand_id,brand_code, brand_name, brand_initial, make_id, make_name, mod1_id, mod1_name, mod2_id, mod2_name,mod2_start_year,mod2_end_year,mod2_pc,mod2_cv, mod3_id, mod3_name, mod3_start_year, mod3_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, mod3_chassis, mod3_atime, mod3_etime'];
			$res = $Ypc2Model->getModel(3, $filed, $where.' order by brand_name, make_name, mod2_name, mod3_name, mod3_start_year');
			$arr = $res['data'];
			$arrJson = json_encode($arr);
			file_put_contents($file, $arrJson);
		}
		else
		{//括起来为了防止读取文件失败
			$arrJson = file_get_contents($file);
		}
		header("Content-type:application/octet-stream");
		header('Content-Disposition: attachment;filename="yp_model.json"');
		header('Cache-Control: max-age=0');
		echo $arrJson;
	}


	/**
	  * @method 获取宜配车型库品牌
	  * @http  get
	  * @param  token              string [必填] 调用接口凭证 (post|get)			
	  * @author soul
	  * @copyright 2025/9/10
	  * @return {"status":true,"data":[{"brand_id":"210","brand_name":"AITO","brand_initial":"A"},{"brand_id":"135","brand_name":"埃安","brand_initial":"AA"}]}
	  */
	public function getYpcModelBrand()
	{
		$Ypc2Model = new Ypc2Model();
		$res = $Ypc2Model->Get('bc_brand', ['brand_id, brand_name, brand_initial'], ' order by brand_initial');
		LibFc::EchoJson($res);
	}


	/**
	  * @method 获取TCD车型库
	  * @http  get
	  * @param  token              string [必填] 调用接口凭证 (post|get)			
	  * @author soul
	  * @copyright 2019/11/26
	  * @return 文件下载  yp_model.json

	  	typ_id : 车型ID
		typ_id_org : 原始ID
		typ_type : 2:ktype 16:ntype
		typ_name : 车型名称
		typ_start : 开始年月
		typ_end : 结束年月
		typ_engine : 发动机 逗号分隔
		typ_kw : 功率
		typ_hp : 马力
		typ_cc : 排量
		typ_cylinder : 缸数
		typ_doors : 门数
		typ_body : 类型
		typ_drive : 驱动
		typ_fuel : 燃料
		mod_id : 车代
		mod_name : 车代名称
		mod_chassis : 底盘
		mod_minname : 车系短名
		mfa_id : 汽车品牌
		mfa_brand : 品牌名称
		mfa_brand_cn : 品牌名称
	  */
	public function getTcdModel()
	{
		ini_set('memory_limit', '4096M');
		set_time_limit(0);
		if(!empty(mvc::$cfg['RQTOKEN']['brand_ids_tcd'])){
			$file = mvc::$cfg['PATH_CACHE'].'tcdModel_'.mvc::$cfg['RQTOKEN']['user'].'.json';
		}else{
			$file = mvc::$cfg['PATH_CACHE'].'tcdModel.json';
		}
		$arr = [];
		//缓存1小时
		if(!file_exists($file) || (filemtime($file) < (time()-3600)))
		{
			$TcdmBase = new TcdmBase();
			$where = !empty(mvc::$cfg['RQTOKEN']['brand_ids_tcd'])? sprintf(" and mfa_id in (%s)", implode(',', mvc::$cfg['RQTOKEN']['brand_ids_tcd'])): '';
			$filed = ["mfa_id, mfa_brand_cn, mfa_brand, mfa_initial,
			mod_id, mod_name, mod_chassis,mod_minname,
			typ_id, CASE WHEN typ_type = 2 THEN 'ktype' ELSE 'ntype' END typid_type, typ_id_org, typ_name,
			typ_start, typ_end, typ_engine, 
			CASE WHEN typ_kw = 0 THEN '' ELSE typ_kw END typ_kw,
			CASE WHEN typ_hp = 0 THEN '' ELSE typ_hp END typ_hp,
			CASE WHEN typ_cc = 0 THEN '' ELSE typ_cc END typ_cc,
			CASE WHEN typ_cylinder = 0 THEN '' ELSE typ_cylinder END typ_cylinder,
			CASE WHEN typ_doors = 0 THEN '' ELSE typ_doors END typ_doors,
			typ_body,typ_drive,typ_fuel"];
			$table = 'tof_types JOIN tof_manufacturers ON mfa_id = typ_mfa_id JOIN tof_models ON mod_id = typ_mod_id';
			$res = $TcdmBase->get($table, $filed, $where.' order by mfa_brand, mod_name, typ_name');
			$arr = $res['data'];
			$arrJson = json_encode($arr);
			file_put_contents($file, $arrJson);
		}
		else
		{//括起来为了防止读取文件失败
			$arrJson = file_get_contents($file);
		}
		header("Content-type:application/octet-stream");
		header('Content-Disposition: attachment;filename="tcd_model.json"');
		header('Cache-Control: max-age=0');
		echo $arrJson;
	}

	/**
	  * @method 获取TCD车型库品牌
	  * @http  get
	  * @param  token              string [必填] 调用接口凭证 (post|get)			
	  * @author soul
	  * @copyright 2025/9/10
	  * @return {"status":true,"data":[{"mfa_id":"425","mfa_brand_cn":"WÄRTSILÄ","mfa_brand":"WARTSILA"},{"mfa_id":"506","mfa_brand_cn":"AUWÄRTER","mfa_brand":"AUWARTER"}]}
	  */
	public function getTcdModelBrand()
	{
		$TcdmBase = new TcdmBase();
		$res = $TcdmBase->get('tof_manufacturers', ['mfa_id, mfa_brand_cn, mfa_brand'], ' order by mfa_brand');
		LibFc::EchoJson($res);
	}



	/**
	  * @method VIN解码 返回宜配车型ID
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  vin                string [必填] 17位车架号
	  * @author soul
	  * @copyright 2019/11/26
	  * @return  {"status":true,"data":[14817,14818,14819,14820],"code":0}
	  */
	public function vinDecodeYpcModelId()
	{
		$Params = mvc::$URL_PARAMS;
		$vin = LibFc::FormatVin($Params['vin']);
		$YpcVin = new YpcVin();
		$res = $YpcVin->decodeVin($vin, 'm3ids');
		$arr = $res['data']['model'];
		$YpcVin->tongjiVin($vin, $res['data']['from']);
		LibFc::ReturnJson(true, $arr);
	}


	/**
	  * @method vin图片识别
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  image[旧]          array  ['name'=>'ocr.jpg','data'=>base64_encode(file_get_contents('D:\www\ocr/ocr.jpg'))]
	  * @param  image[新]          string  base64_encode(file_get_contents('D:\www\ocr/ocr.jpg'))
	  * @param  issmall           int [选填] 是否为裁剪后的图 1|0 默认0
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function vinOcr()
	{
		$MainOcr = new MainOcr();
		$imgdata = is_array($_POST['image']) && !empty($_POST['image']['data'])? $_POST['image']['data']: $_POST['image'];
		$res = $MainOcr->vin($imgdata, $_POST);
		LibFc::EchoJson($res);
	}

	

	/**
	  * @method 获取某号码的EPC子组信息
	  * @http  get
	  * @param  etk_id			   int [必填] 
	  * @param  pro_id			   int [必填] 
	  * @param  grp2_id			   int [必填]  零件组ID
	  * @author soul
	  * @copyright 
	  */
	function epcItemGroupInfo()
	{
		$arr = [];
		$Params = mvc::$URL_PARAMS;
		$numArr = [];
		$OthNumber = new OthNumber();
		$LibEncry = new LibEncry();
		$type = 'etk';
		if($Params['etk_id'] && $Params['pro_id'] && $Params['grp2_id']){
			$EpcItem = new EpcItem($Params['etk_id']);
			$res = $EpcItem->getItem($Params['pro_id'], $Params['grp2_id'], $Params['lnkId']);
			$arr['base'] = $res['data'];
		}

		if(empty($arr['base'])){
			foreach(explode(',', $Params['number']) as $num){
				$format = LibFc::FormatNum($num);
				if(strlen($format) < 4) continue;
				$pres = $OthNumber->search(['type'=>$type, 'word'=>$format, 'islike'=>false]);
				if(empty($pres['data'][$type])) continue;
				foreach($pres['data'][$type] as $pv){
					$EpcItem = new EpcItem($pv['etk_id']);
					$res = $EpcItem->getItem($pv['pro_id'], $pv['grp2_id'], $pv['lnkId']);
					$arr['base'] = $res['data'];
					break;
				}
				if($arr['base']) break;
			}
		}
		if($arr['base']){
			$pro_uri =  sprintf("type=etk&proId=%d&etkId=%d&grp2Id=%d&lnkId=%s", $arr['base']['pro_id'], $arr['base']['etk_id'], $arr['base']['grp2_id'], (int) $arr['base']['lnkId']);
			$pro_uri = base64_encode($LibEncry->encode($pro_uri));
			$arr['base']['pro_url'] = mvc::$cfg['HOST']['pjlpc'].'?goto='.urlencode(mvc::$cfg['HOST']['pjlpc'].'item/des?uri='.$pro_uri);

			$EpcItem = new EpcItem($arr['base']['etk_id']);
			$res = $EpcItem->getPartItem($arr['base']['grp2_id']);
			$arr['partitem']= $res['data'];
		}
		LibFc::ReturnJson(true, $arr);
	}
}