<?php
class itemAction extends baseAction
{
	/**
	*1、号码 分类
	*2、关键字 分类 车型
	*/
	function search()
	{
		$_SESSION['MAIN_MONITOR']['tj'] = 0;
		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				LibTpl::Error('非常抱歉，您无权限访问此页面！');
			}
		}
		
		$Params = mvc::$URL_PARAMS;

		//车架号查询跳转
		if($Params['type'] == 'vin' || ($Params['type'] == 'all' && isset($Params['keyword']) && preg_match('/[0-9a-zA-Z]{17}/', trim($Params['keyword']))))
		{
			$_SESSION['MAIN_MONITOR']['tj'] = 0;
			LibFc::Go(str_ireplace('item/search?', 'item/searchVin?', LibFc::nowUrl()));
		}
		else if($Params['type'] == 'engine')
		{
			LibFc::Go(str_ireplace('item/search?', 'item/searchEngine?', LibFc::nowUrl()));
		}
		else if($Params['type'] == 'models')
		{
			LibFc::Go(str_ireplace('item/search?', 'model/models?', LibFc::nowUrl()));
		}
		$_SESSION['MAIN_MONITOR']['tj'] = 1;
		$B2cBase = new B2cBase();
		if(!empty($Params['sortId']))
		{
			$res = $B2cBase->get('sh_sort', ['sort_id', 'sort_name'], sprintf(" and sort_id=%d", (int) $Params['sortId']), true);
			LibTpl::Set('sortArr', $res['data']);
		}
		if(!empty($Params['mod3_id']) && empty($Params['mod3Id']))
			$Params['mod3Id'] = $Params['mod3_id'];
		if(!empty($Params['mod3Id']) || !empty($Params['mod3Id']))
		{
			$ComModel = new ComModel();
			$res = $ComModel->getModel(3, ['make_name', 'mod2_name', 'bc_model3.*'], sprintf(" and mod3_id = %d", (int) $Params['mod3Id']), true);
            $model = '';
            if(!empty($res['data'])){
                $brandMake = $res['data']['make_show_brand'] && $res['data']['make_show_brand'] == 1 ? $res['data']['brand_name'] .' ' : $res['data']['make_name'].' ';
                $model = $brandMake.$res['data']['mod2_name'].' '.$res['data']['mod3_name'].' '.LibFc::formatViewYear($res['data']['mod3_start_year'], $res['data']['mod3_end_year'], 2, 0);
            }
            LibTpl::Set('model',$model);
		}
		LibTpl::Set('bodyClass', 'search-res');
		LibTpl::Set('Params', $Params);
		LibTpl::Set('title', '配件列表');
		LibTpl::Put();
	}

	function searchVin()
	{
		$Params = mvc::$URL_PARAMS;
		$_SESSION['MAIN_MONITOR']['tj'] = 0;
		if(!in_array('search_vin',mvc::$cfg['permissionArr'])){
			if(empty($_SESSION['_userid'])){
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			LibTpl::Error('非常抱歉，您无权限访问此页面！');
		}

		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				LibTpl::Error('非常抱歉，您无权限访问此页面！');
			}
		}
		$hasImg = false;
		$vin = !isset($Params['keyword']) ? '': LibFc::FormatVin($Params['keyword']);
		LibTpl::Set('vin', $vin);
		$modelArr = [];
		if(strlen($vin) >= 17)
		{
			$vin = substr($vin, 0, 17);
			$needCaptcha = false;
			if(empty($_SESSION['vin'])) $_SESSION['vin'] = [];
			if(empty($_SESSION['vin'][$vin]))
			{
				$LibYpApi = new LibYpApi();
				$capres = $LibYpApi->get('super/checkVinCaptcha', ['uid'=>$_SESSION['_userid'], 'shopid'=>mvc::$cfg['SHOPID'], 'ip'=>LibFc::GetClientIp()]);
				$needCaptcha = $capres['data']['need'];
			}
			if($needCaptcha)
			{//未登录，出验证码
				$captokenOk = false;
				if(!empty($Params['captoken']))
				{
					$OthCheckcode = new OthCheckcode();
					$res = $OthCheckcode->checkDingxiangCaptcha($Params['captoken']);
					$captokenOk = $res['data'];
				}
				if(!$captokenOk){
					LibTpl::Set('Params', $Params);
					LibTpl::Put('index/captcha.php');
					exit;
				}else{
					$LibYpApi = new LibYpApi();
					$LibYpApi->get('super/addCaptchaLog', ['from'=>'gzh', 'uid'=>$_SESSION['_userid'], 'shopid'=>mvc::$cfg['SHOPID'], 'ip'=>LibFc::GetClientIp(), 'remark'=>$Params['captoken']]);
				}
			}
			$_SESSION['vin'][$vin] = 1;
			$LibYpApi = new LibYpApi();
			$deres = $LibYpApi->get('vin/decodeVin', ['vin'=>$vin, 'shopid'=>mvc::$cfg['SHOPID']], [], true);
			if($deres['status'])
			{
				$modelArr = $deres['data'];
				if(!empty($modelArr))
				{
					$B2cBase = new B2cBase();
					//全车件品牌限制
					$res = $B2cBase->get('sh_shop_brand', ['ssb_brand_id'], sprintf(" and ssb_shop_id=%d", mvc::$cfg['SHOPID']));
					$limitBrands = array_column($res['data'], 'ssb_brand_id');
					if(!empty($limitBrands) && !in_array($modelArr['brand_id'], $limitBrands)) LibTpl::Error('此目录尚未开放'.$vin.'的查询。');
					
					//资源包历史扣除
					$hisArr = [
						'ash_uid'=>mvc::$cfg['SHOPID'],
						'ash_type'=>'vin',
						'ash_act'=>'vin/decodeVin',
						'ash_val'=>$vin,
						'ash_uid_by'=>$_SESSION['_userid'],
						'ash_ip'=>LibFc::GetClientIp(),
						'ash_from'=>'gzh'
					];
					$LibYpApi->get('zyb/addHistory', [], $hisArr);
					foreach ($modelArr['model'] as $k => $v)
					{
						if(!empty($v['pic'])) $hasImg = true;
					}
				}
				//查询历史
				if(!empty($_SESSION['_userid']))
				{
					$historyArr = [
						'ush_uid'=>$_SESSION['_userid'],
						'ush_shop_id'=>mvc::$cfg['SHOPID'],
						'ush_type'=>$Params['type']=='all'?'all':'vin',
						'ush_word'=>$vin,
					];
					$LibYpApi = new LibYpApi();
					$LibYpApi->get('super/addSearchHistory', [], $historyArr);
				}
				$_SESSION['MAIN_MONITOR']['tj'] = 1;
			}
			else
			{
				LibTpl::Set('errMsg', $deres['data']);
			}
		}
		LibTpl::Set('hasImg', $hasImg);
		LibTpl::Set('modelArr', $modelArr);
		LibTpl::Set('Params', $Params);
		LibTpl::Set('title', '车架号解码');
		LibTpl::Put('item/searchVin.php');
	}

	//发动机车型
	function searchEngine()
	{
		if(!in_array('search_engine',mvc::$cfg['permissionArr']))
		{
			if(empty($_SESSION['_userid'])){
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			LibTpl::Error('非常抱歉，您无权限访问此页面！');
		}
		
		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				LibTpl::Error('非常抱歉，您无权限访问此页面！');
			}
		}
		$Params = mvc::$URL_PARAMS;
		LibTpl::Set('Params', $Params);
		LibTpl::Set('title', '发动机查询');
		LibTpl::Put('item/searchEngine.php');
	}

	/**VIN识别**/
	function vinOcr()
	{
		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				LibFc::ajaxJsonEncode(['status'=>false,'data'=>'非常抱歉，您无权限访问此页面！']);
			}
		}
		$arr = ['vin'=>'', 'chars'=>[]];
		if(!empty($_POST['data']))
		{
			$post =  ['data'=>substr(strstr($_POST['data'], ','), 1)];
			$post['name'] = empty($_POST['name'])? 'vinFile.jpg': $_POST['name'];
			$LibYpApi = new LibYpApi();
			$res = $LibYpApi->get('ocr/vin', [],['image'=>$post]);
			$arr['vin'] = $res['data'];
			if(strlen($arr['vin']) == 17) $arr['chars'] = str_split($arr['vin'], 1);
		}
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$arr]);
	}

	//获取微信服务器的图片
	function getWxImgdata()
	{
		$Params = mvc::$URL_PARAMS;
		if(!empty($Params['serverId']))
		{
			$LibYpApi = new LibYpApi();
			$res = $LibYpApi->get('wx/getToken');
			$url = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token='.$res['data'].'&media_id='.$Params['serverId'];
			$LibHttp = new LibHttp();
			$imgdata = $LibHttp->fetch($url);
			if(strlen($imgdata) > 100)
			{
				$imgdata = 'data:image/jpeg;base64,'.base64_encode($imgdata);
				$arr = ['name'=>md5($Params['serverId']).'.jpg','data'=>$imgdata];
				LibFc::ajaxJsonEncode(['status'=>true,'data'=>$arr]);
			}
		}
		LibFc::ajaxJsonEncode(['status'=>false,'data'=>'获取VIN识别的图片信息失败！']);
	}

	function ajaxSearch()
	{
		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				LibFc::ajaxJsonEncode(['status'=>false,'data'=>'非常抱歉，您无权限访问此页面！']);
			}
		}
		$Params = mvc::$URL_PARAMS;
		if($Params['level'] == 'item') $Params['type'] = 'item';

		$B2cSearch = new B2cSearch();
		$perPage = 10;
		if(!empty($_SESSION['_userid']))
			$Params['uid'] = $_SESSION['_userid'];

		$res = $B2cSearch->searchShopItem(mvc::$cfg['SHOPID'], $Params, $perPage, ['pro_show_app'=>1]);
		$result = $res['data'];
		unset($result['param']['callback']);
		unset($result['param']['_']);
		unset($result['param']['uid']);
		$itemArr = [];
		if(!empty($result['proIds']))
		{
			$B2cProduct = new B2cProduct();
			$res = $B2cProduct->getAnyProductInfo($result['proIds'], $_SESSION['_userid'],null, [], $Params['mod3_id']);
			$itemArr = $res['data'];
			//不可以购买的人不可以查看第二个号码
			if(empty(mvc::$cfg['_CANBUY']) || mvc::$cfg['HIDEMAINNUM'])
			{
				foreach($itemArr as $k=>$v)
				{
					foreach($v['num'] as $kk=>$vv)
					{
						$itemArr[$k]['num'] = [$vv];
						break;
					}
				}
			}
		}
		LibTpl::Set('Params', $Params);
		LibTpl::Set('itemArr', $itemArr);
		$html = LibTpl::Fetch();
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>['html'=>$html, 'Params'=>$result['param']]]);
	}


	function index()
	{
		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				 LibTpl::Error('非常抱歉，您无权限访问此页面！');
			}
		}
		$B2cBase = new B2cBase();
		$res = $B2cBase->get('sh_sort_tree join sh_sort on sot_sort_id = sort_id', ['sort_id', 'sort_name'], sprintf(" and sot_psort_id = 0 and sort_shop_id=%d order by sort_seq, sort_name", mvc::$cfg['SHOPID']));
		$sortArr = $res['data'];
		LibTpl::Set('sortArr', $sortArr);
		LibTpl::Set('offCanvas', 1);
		LibTpl::Set('footMenu', 'item');
		LibTpl::Set('title', '配件查询');
		LibTpl::Put();
	}


	/**
	 * @method    配件详情
	 * @author 卢
	 * @copyright 2018-07-19
	 */
	function info()
	{
		if(!in_array('item_page',mvc::$cfg['permissionArr'])){
			if(empty($_SESSION['_userid'])){
				$backUrl =  urlencode(LibFc::nowUrl());
				LibFc::Go(mvc::$cfg['SELF_URI'].'user/login?backUrl='.$backUrl);
			}
			LibTpl::Error('非常抱歉，您无权限访问此页面！');
		} 
		
		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				 LibTpl::Error('非常抱歉，您无权限访问此页面！');
			}
		}
		$Params = mvc::$URL_PARAMS;
		$proId = (int) $Params['proId'];
		$B2cProduct = new B2cProduct();
		$res = $B2cProduct->get('bc_product', ['pro_id'], sprintf(" and pro_status= 1 and pro_id = %d and pro_shop_id=%d", $proId, mvc::$cfg['SHOPID']), true);
		if(empty($res['data'])) LibTpl::Error('配件'.$proId.'不存在或者已下架！');

		$uid=!empty($_SESSION['_userid'])?$_SESSION['_userid']:0;
		$res = $B2cProduct->getAnyProductInfo([$proId], $uid, ['numMain', 'picAll','mdPrice'], ['pro_show_app'=>1], $Params['mod3_id']);
		$proArr['base'] = $res['data'][$proId];
		if(empty($proArr['base']['pro_id'])) LibTpl::Error('配件'.$proId.'不存在或者已下架！');

		if(empty(mvc::$cfg['_CANBUY']))
		{
			foreach($proArr['base']['num'] as $k=>$v)
			{
				$proArr['base']['num'] = [$v];
				break;
			}
		}

		$proArr['imgs'] = $res['data'][$proId]['pic'];
		//加入浏览历史
		$B2cHistory = new B2cHistory();
		$r=$B2cHistory->addUserProHistory($_SESSION['_userid'],$proArr['base']['pro_id'],!empty($_SESSION['_shopid'])?$_SESSION['_shopid']:0);
		// print_r($r);
		if(!empty($proArr['base']['pro_prb_id']))
		{
			$brandArr=$B2cProduct->get('bc_probrand',['prb_name'],sprintf(' and prb_id=%d',$proArr['base']['pro_prb_id']),true);
			$data['brand']=$brandArr['data']['prb_name'];
		}
		//是否收藏
		$res=$B2cProduct->get('us_product_collection',['count(1) count'],sprintf(' and prc_pro_id=%d',$proArr['base']['pro_id']),true);
		LibTpl::Set('isColl',$res['data']['count']);
		$B2cBase = new B2cBase();
		$YpwAddr = new YpwAddr();
		//店铺信息
		$res=$B2cBase->get('sh_shop',['shop_pct_ids'],sprintf(' and shop_id=%d',mvc::$cfg['SHOPID']),true);
		$res=$YpwAddr->getAddrByIds($res['data']['shop_pct_ids']);
		LibTpl::Set('area',array_slice($res['data'],0,2));

		$B2cPart = new B2cPart();
		$table = $where = sprintf("bc_part_param_link 
			join bc_param_label on ppl_pal_id=pal_id and ppl_part_id = %d
			left join bc_product_param_float on ppf_pal_id=pal_id and ppf_pro_id = %d
			left join bc_product_param_string on pps_pal_id=pal_id and pps_pro_id = %d
			left join bc_param_unit on ppl_pau_id = pau_id", $proArr['base']['pro_part_id'], $proId, $proId);
		$where = ' and (ppf_pro_id  is not null or pps_pro_id is not null) order by ppl_seq';
		$res = $B2cBase->get($table, ['pal_id', 'pal_name, ppl_style, pps_value, ppf_value, pau_name'], $where);
		foreach($res['data'] as $v)
		{
			$v['pau_name'] = empty($v['pau_name']) ? '': $v['pau_name'];
			$v['val'] = empty($v['pps_value']) ? $v['ppf_value']: $v['pps_value'];
			$proArr['partParam'][$v['pal_id']] = ['name'=>$v['pal_name'], 'val'=>$v['val'], 'unit'=>$v['pau_name']];
		}

		//获取自定义配件参数
		if($proArr['base']['pro_ppt_id'] > 0)
		{
			$tplId = $proArr['base']['pro_ppt_id'];
			//有值的
			$feild = ['prpv_prl_id', 'prpv_pru_id', 'prpv_value','prl_name','pru_name'];
			$res=$B2cBase->get('bc_product_para_val join bc_product on pro_id= prpv_pro_id left join bc_product_label on prl_id=prpv_prl_id left join bc_product_unit on pru_id=prpv_pru_id', $feild, sprintf(" and pro_id=%d and pro_ppt_id =%d and (prpv_value is not null or prpv_value!='')", $proId, $tplId));
			$oldValArr = [];
			$oldValArr=array_column($res['data'], null,'prpv_prl_id');
			//新的
			$fieldArr=['pptl_ppt_id','prl_id','pru_id','pptl_seq','prl_name','pru_name'];
			$newlink=$B2cBase->get('bc_product_para_tpl_link left join bc_product_label on prl_id=pptl_prl_id left join bc_product_unit on pru_id=pptl_pru_id',$fieldArr,sprintf(' and pptl_ppt_id=%d order by pptl_seq asc,pptl_prl_id asc',$tplId));
			$newArr= array_column($newlink['data'], null,'prl_id');
			if(count($oldValArr))
			{
				foreach ($oldValArr as $k => $v)
				{
					$newArr[$k]=isset($newArr[$k])?array_merge($v,$newArr[$k]):$v;
				}
			}
			$data['proParams'] = $newArr;
		}

		//运费模板
		$frtId=$proArr['base']['pri_frt_id'];
		if($frtId)
		{
			$templateData=$B2cBase->get('sh_freight_tpl',['*'],' and frt_id='.$frtId,true);
			$sendAddrData=$YpwAddr->getAddrByIds($templateData['data']['frt_send_pct']);
			$addrArr=array_slice($sendAddrData['data'], 0,2);
			$templateData['data']['sendAddr']=mb_substr($addrArr[0], 0,2)==mb_substr($addrArr[1], 0,2)? $addrArr[1] : implode('',$addrArr);
			$data['template']=$templateData['data'];
		}
		$dataArr = array();
		$comLabel = $B2cBase->Get('or_evaluate_label_link left join or_evaluate_label on ell_evl_id=evl_id',['evl_id','evl_name'],sprintf(' and ell_pro_id=%d limit 6',$proArr['base']['pro_id']));
		foreach ($comLabel['data'] as $labK => $labV)
		{
			$dataArr[$labV['evl_id']] = $labV;
			if(array_key_exists($labV['evl_id'], $dataArr))
			{
				unset($dataArr[$labV['evl_id']]);
				$dataArr[$labV['evl_id']] = $labV;
			}else
			{
				$dataArr[$labV['evl_id']] = $labV;
			}
		}
		//累计评价
		$evaluateData=$B2cBase->get('or_evaluate_product',['count(*) count'],' and evp_pro_id='.$proId,true);
		$data['evaCount']=$evaluateData['data']['count'];
		if($data['evaCount'])
		{
			$YpwUser = new YpwUser();
			//第一条评价
			$res=$B2cBase->get('or_evaluate_product',['evp_comment','evp_add_time','evp_pro_id','evp_uid'],sprintf(' and evp_pro_id=%d order by evp_add_time desc',$proId),true);
			$logoInfo = $YpwUser->Get('yp_user', array('u_logo'), ' and u_id='.$res['data']['evp_uid'], true);
			$data['firstEval']=$res['data'];
			$data['firstEval']['u_logo']=$logoInfo['data']['u_logo'];
		}
		$data['comLabelArr']=$dataArr;
		$res = $B2cPart->getAllPart();
		$data['partArr']=$res['data'];
		//车型数量
		$res=$B2cBase->get('bc_product_brand_link',['count(1) count'],'and pbl_pro_id='.$proId,true);
		LibTpl::Set('modelNum',$res['data']['count']);
		//店铺分类
		$B2cShopSort = new B2cShopSort();
		$thisShopSort = $B2cShopSort->getShopSort($proArr['base']['pro_shop_id']);
		$data['thisShopSort']=$thisShopSort['data'];
		$data['proArr']=$proArr;
		$data['view']=$view;
		// 售后保障
		$B2cProduct = new B2cProduct();
		$res = $B2cProduct->getProAfterSellService($proId);
		$data['afterSel'] = array_filter($res['data']);
		LibTpl::Set('itemInfoData',$data);
		LibTpl::Set('bodyClass', 'item-page');
		LibTpl::Set('title', '配件详情');
		LibTpl::Put();
	}
	/**
	 * @method    获取运费城市列表
	 * @author 卢
	 * @copyright 2017-07-31
	 */
	function ajaxGetTemplateHtml()
	{
		$Params = mvc::$URL_PARAMS;
		// print_r($Params);
		$YpwAddr=new YpwAddr();
		$res=$YpwAddr->getAddrTree();
		$data=[];
		if(!empty($res['data']))
		{
			foreach ($res['data'] as $k => $v)
			{
				$data[$k]=['value'=>$v['pc_id'],'text'=>$v['pc_name']];
				if(!empty($v['child']))
				{
					foreach ($v['child'] as $i => $j)
					{
						$data[$k]['children'][]=['value'=>$j['pc_id'],'text'=>$j['pc_name']];
					}
				}
			}
		}
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$data]);
	}
	/**
	 * @method    获取某个城市的运费
	 * @author 卢
	 * @copyright 2017-07-31
	 */
	function getFrtContent()
	{
		$B2cBase = new B2cBase();
		$Params = mvc::$URL_PARAMS;
		$tplId=$Params['tplId'];
		$ftl_city=$Params['ftl_city'];
		if(LibFc::Int($tplId)&&LibFc::Int($ftl_city))
		{
			$tplData=$B2cBase->get('sh_freight_tpl_link',['*'],' and ftl_frt_id='.$tplId);
			$result=array();
			// print_r($tplData['data']);
			$method=array();
			foreach ($tplData['data'] as $k => $v)
			{
				if(in_array($v['ftl_shipping_method'], $method)) continue;
				$cityArr=explode(',', $v['ftl_cities']);
				$name=$v['ftl_shipping_method']>0 ? 'EMS' : '快递';
				$result[$v['ftl_shipping_method']]['name']=$name;
				if($v['ftl_default']=='Y'&&!in_array($ftl_city, $cityArr))
				{
					$result[$v['ftl_shipping_method']]['price']=sprintf('%.2f',$v['ftl_begin_money']);
					continue;
				}
				if(in_array($ftl_city, $cityArr))
				{
					$result[$v['ftl_shipping_method']]['price']=sprintf('%.2f',$v['ftl_begin_money']);
					$method[]=$v['ftl_shipping_method'];
					continue;
				}


			}
			LibFc::ajaxJsonEncode(['status'=>true,'data'=>$result]);
		}
	}
	/**
	 * @method    配件评价页面
	 * @author 卢
	 * @copyright 2018-07-19
	 */
	function evaluate()
	{
		$Params = mvc::$URL_PARAMS;
		if(!LibFc::Int($Params['proId']))
		{
			LibTpl::Error('配件不存在');
		}
		$B2cBase = new B2cBase();
		$dataArr = array();
		$comLabel = $B2cBase->get('or_evaluate_label_link left join or_evaluate_label on ell_evl_id=evl_id',['evl_id','evl_name'],sprintf(' and ell_pro_id=%d',$Params['proId']));
		foreach ($comLabel['data'] as $labK => $labV)
		{
			$dataArr[$labV['evl_id']] = $labV;
			if(array_key_exists($labV['evl_id'], $dataArr))
			{
				unset($dataArr[$labV['evl_id']]);
				$dataArr[$labV['evl_id']] = $labV;
			}else
			{
				$dataArr[$labV['evl_id']] = $labV;
			}
		}
		// print_r($dataArr);
		LibTpl::Set('labelArr',$dataArr);
		LibTpl::Set('footMenu', 'item');
		LibTpl::Set('title', '配件详情');
		LibTpl::Put();
	}
	/**
	 * @method    ajax获取配件评价
	 * @author 卢
	 * @copyright 2018-07-19
	 */
	function getEvaluate()
	{
		$Params = mvc::$URL_PARAMS;
		if(LibFc::Int($Params['proId']))
		{
			$B2cBase = new B2cBase();
			$YpwUser = new YpwUser();
			$base = sprintf(' and evp_pro_id=%d',$Params['proId']);
			//时间排序
			$seq = $Params['seq'] == 'time'?' order by evp_add_time desc':' order by evp_add_time asc';
			//有内容
			$hasCom = $Params['hasCom'] == 'hasCom'?" and evp_comment is not null":'';
			if(empty($Params['order'])) $Params['order'] = 'all';
			switch ($Params['order'])
			{
				case 'all':
					$order = '';
					break;
				case 'hasImg':
					$order = ' and evp_id in(select evp_id from or_evaluate_product left join or_evaluate_img on evp_id=evi_evi_id where evi_id is not null group by evp_id)';
					break;
				default:
					$Params['order'] = (int) $Params['order'];
					$order = sprintf(' and evp_id in(select ell_evp_id from or_evaluate_label_link left join or_evaluate_label on ell_evl_id=evl_id where ell_evl_id=%d)', $Params['order']);
					break;
			}
			LibTpl::Set('order', $Params['order']);
			//获取改配件的所有评论的id
			$where = $base.$order.$hasImg.$hasCom.$seq;
			$count = $B2cBase->get('or_evaluate_product',['count(*) as count'],$base.$order.$hasImg.$hasCom,true);
			$total = $count['data']['count'];
			$perPage = 20;
			$nowPage = LibFc::Int($Params['pageNum']) && $_POST['pageNum'] > 0?$Params['pageNum']:1;
			$totalPage = ceil($total/$perPage);
			LibTpl::Set('nowPage', $nowPage);
			$dataArr = $B2cBase->Get('or_evaluate_product',['*'], $where.sprintf(' limit %d,%d', ($nowPage-1)*$perPage, $perPage));
			//获取每个评论下的图片 和 评论用户的信息
			if(!empty($dataArr['data']))
			{
				foreach($dataArr['data'] as $key=>$value)
				{
					$imgArr = $B2cBase->Get('or_evaluate_img',['evi_path','evi_name','evi_ext'],' and evi_evi_id='.$value['evp_id'].' order by evi_seq asc');
					$dataArr['data'][$key]['imgInfo'] = $imgArr['data'];
					if(!empty($value['evp_uid']))
					{
						$user = $YpwUser->getUser(['u_name'], sprintf(" and u_id=%d",$value['evp_uid']), true);
						$dataArr['data'][$key]['comUserName'] = $user['data']['u_name'];
					}
				}
			}
			$code=empty($dataArr['data'])||$totalPage<=$nowPage?0:1;
			// print_r($dataArr['data']);
			LibTpl::Set('comArr', $dataArr['data']);
			$result=LibTpl::Fetch('item/ajaxGetCommentHtml.php');
			$status = ['status'=>true,'data'=>['tpl'=>$result,'code'=>$code]];
			LibFc::ajaxJsonEncode($status);
		}
	}

	//品牌选择
	function brand()
	{
		$Params = mvc::$URL_PARAMS;
		LibTpl::Set('Params', $Params);
		LibTpl::Set('title', '品牌选择');
		LibTpl::Put();
	}

	//车型选择
	function models()
	{
		$Params = mvc::$URL_PARAMS;
		LibTpl::Set('Params', $Params);
		LibTpl::Set('title', '车型选择');
		LibTpl::Put();
	}


	/**
	 * @method    获取适用车型信息
	 * @author     廖
	 * @copyright 2018-1-3
	 * @return    LibFc::ReturnData(['status'=>false,'data'=>html]);
	 */
	function ajaxGetFitCarHtml()
	{
		if(empty(mvc::$cfg['_SHOWMODEL']))
			LibFc::ajaxJsonEncode(['status'=>true,'data'=>[]]);
		$Params = mvc::$URL_PARAMS;
		$proId = (int) $Params['proId'];
		if($proId > 0)
		{
			$B2cBase = new B2cBase();
			$ComModel = new ComModel();
			$data=[];
			switch ($Params['level'])
			{
				case '1':
					$res = $B2cBase->get('bc_product_brand_link',['pbl_brand_id'],'and pbl_pro_id='.$proId);
					if(!empty($res['data']))
					{
						$brandId = array_column($res['data'], 'pbl_brand_id');

						$res = $ComModel->getModel(1, ['brand_name,brand_id'], sprintf(' and mod1_brand_id in(%s) group by brand_id order by brand_initial',implode(',',$brandId)));
						$data=$res['data'];
					}
				break;
				case '2':
					$res3 = $B2cBase->get('bc_product_model3_link',['pml3_mod3_id'],'and pml3_pro_id='.$proId);
					$modId = array_filter(array_column($res3['data'], 'pml3_mod3_id'),'LibFc::Int');
					if(!empty($modId))
					{
						$where = sprintf(" and mod3_id in (%s) and mod1_brand_id=%d order by brand_initial, make_seq, make_initial, mod1_name, mod2_name, mod3_name", implode(',', $modId),(int)$Params['brandId']);
						$filed = ['brand_name, make_name, make_show_brand, mod1_name, mod2_name, mod2_start_year,mod2_end_year,mod3_id,mod3_name,mod3_start_year,mod3_start_month,mod3_end_year,mod3_end_month,mod3_kw,mod3_cc,mod3_engine,mod3_transmission,mod3_fuel,mod3_drive'];
						$res = $ComModel->getModel(3, $filed, $where);
						$data = array_column($res['data'], null, 'mod3_id');
						
						//车型参数
						$modelPara = [];
						$where = sprintf(" and pml3_pro_id = %d and pml3_mod3_id in (%s) and pml3_remark != '' group by pml3_mod3_id, pml3_remark", $proId, implode(',', array_keys($data)));
						$res = $B2cBase->get('bc_product_model3_link', ['pml3_mod3_id', 'pml3_remark'], $where);
						foreach($res['data'] as $pv)
						{
							$modelPara[$pv['pml3_mod3_id']][] = $pv['pml3_remark'];
						}

						$where = sprintf(" and pmp_pro_id = %d and pmp_mod3_id in (%s) and pmp_info != '' group by pmp_mod3_id, pmp_info", $proId, implode(',', array_keys($data)));
						$res = $B2cBase->get('bc_product_model3_para', ['pmp_mod3_id', 'pmp_info'], $where);
						foreach($res['data'] as $pv)
						{
							$modelPara[$pv['pmp_mod3_id']][] = $pv['pmp_info'];
						}
						foreach($modelPara as $pk=>$pv)
						{
							$data[$pk]['mod3_para'] = implode(';', array_unique($pv));
						}
						$data = array_values($data);
					}
				break;
			}
			$status = ['status'=>true,'data'=>$data];
			LibFc::ajaxJsonEncode($status);
		}
		LibFc::ajaxJsonEncode(['status'=>false,'data'=>'参数不正确!']);
	}
	function getSortTopItem()
	{
		$Params = mvc::$URL_PARAMS;
		$sortId = !empty($Params['sortId'])? (int) $Params['sortId']: 0;
		$B2cBase = new B2cBase();
		$whereArr = [
			sprintf(" and sot_psort_id = %d and sort_shop_id=%d order by sort_seq, sort_name", $sortId, mvc::$cfg['SHOPID']),
			sprintf(" and sort_id = %d and sort_shop_id=%d", $sortId, mvc::$cfg['SHOPID']),
		];
		foreach($whereArr as $where)
		{
			$res = $B2cBase->get('sh_sort_tree join sh_sort on sot_sort_id = sort_id', ['sort_id', 'sort_name'], $where);
			if(!empty($res['data'])) break;
		}
		$sortItemArr = [];
		$proIdArr = [];
		foreach($res['data'] as $v)
		{
			if(isset($sortItemArr[$v['sort_id']])) continue;

			//获取此分类的配件 橱窗》销量
			$twhere = !empty(mvc::$cfg['PROALL'])? '' : 'and pro_status =1';
			$twhere .= sprintf(" and prs_sort_id=%d and pro_shop_id=%d order by pro_promoted desc, pro_sold_count desc, pro_host desc, pro_id desc limit 12", $v['sort_id'], mvc::$cfg['SHOPID']);
			$res = $B2cBase->get('bc_product_sort join bc_product on prs_pro_id = pro_id', ['pro_id'], $twhere);
			$tempProIdArr = array_column($res['data'], 'pro_id');
			$proIdArr = array_merge($proIdArr, $tempProIdArr);
			$sortItemArr[$v['sort_id']] = ['info'=>$v, 'item'=>$tempProIdArr];
		}

		//获取产品的基本信息
		$itemArr = [];
		if(!empty($proIdArr))
		{
			$B2cProduct = new B2cProduct();
			$res = $B2cProduct->getAnyProductInfo($proIdArr, $_SESSION['_userid']);
			$itemArr = $res['data'];
		}
		LibTpl::Set('sortItemArr', $sortItemArr);
		LibTpl::Set('itemArr', $itemArr);
		$html = LibTpl::Fetch();
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$html]);
	}



	/**获取店铺的适用车型***/
	function ajaxGetModel()
	{
		$Params = mvc::$URL_PARAMS;
		$level = !empty($Params['level'])? (int) $Params['level']: 0;
		$B2cBase = new B2cBase();
		$ComModel = new ComModel();
		$html = '';
		$twhere = !empty(mvc::$cfg['PROALL'])? '' : 'and pro_status =1';
		$table = 'bc_product join sh_shop on pro_shop_id=shop_id and shop_status = 1 '.$twhere.' and pro_count>0 and shop_id='.mvc::$cfg['SHOPID'];
		$where = '';
		if($Params['type'] != 'number')
		{
			$Params['sortId'] = (int) $Params['sortId'];
			if($Params['sortId'] > 0)
			{
				$table .= ' join bc_product_sort on prs_pro_id = pro_id';
				$where .= sprintf(" and prs_sort_id=%d", $Params['sortId']);
			}

			//配件关键字
			if($Params['keyword'] != '')
			{
				$keywordArr = LibFc::keyword($Params['keyword']);
				$B2cKeyword = new B2cKeyword();
				$res = $B2cKeyword->getKeywordKeyIds($keywordArr);
				if(!empty($res['data']))
				{
					$tempWhere = sprintf(" and pro_id in (select prk_pro_id from bc_product_keyword where prk_key_id in (%s) group by prk_pro_id having count(*) = %d)", implode(',', $res['data']), count($res['data']));
				}
				else
				{
					$tempWhere = sprintf(" and pro_name like '%%%s%%'", LibFc::Escape($Params['keyword']));
				}
				$where .= $tempWhere;
			}
		}

		switch($level)
		{
			case 0:
				$table .= ' join bc_product_brand_link on pbl_pro_id=pro_id';
				$res = $B2cBase->get($table, ['distinct pbl_brand_id'], $where);
				$brandIdArr = array_column($res['data'], 'pbl_brand_id');
				$res = $ComModel->getAllBrand();
				$AllBrand = $res['data'];
				$initialArr = [];
				$brandArr = [];
				foreach($AllBrand as $brandId=>$v)
				{
					if(!in_array($brandId, $brandIdArr)) continue;
					$f = substr($v['brand_initial'], 0, 1);
					$initialArr[] = $f;
					$brandArr[$f][] = $v;
				}
				$initialArr = array_unique($initialArr);
				sort($initialArr);
				LibTpl::Set('initialArr', $initialArr);
				LibTpl::Set('brandArr', $brandArr);
				if(!empty($Params['tpl']) && $Params['tpl'] == 'select')
				{
					$html = LibTpl::Fetch('item/ajaxModelBrandSelect.php');
				}
				else
				{
					$html = LibTpl::Fetch('item/ajaxModelBrand.php');
				}
			break;
			case 1:
				$modArr = [];
				if(!empty($Params['brandId']))
				{
					$res = $ComModel->getModel(1, ['distinct mod1_id'], sprintf(" and brand_id=%d", (int) $Params['brandId']));
					if(!empty($res['data']))
					{
						$where .= sprintf(" and pml1_mod1_id in(%s)", implode(',', array_column($res['data'], 'mod1_id')));
						$table .= ' join bc_product_model1_link on pml1_pro_id=pro_id';
						$res = $B2cBase->get($table, ['distinct pml1_mod1_id'], $where);
						if(!empty($res['data']))
						{
							$where = sprintf(" and mod1_id in(%s) order by make_seq, make_initial, make_name, mod1_name", implode(',', array_column($res['data'], 'pml1_mod1_id')));
							$res = $ComModel->getModel(1, ['make_id','make_name', 'mod1_id', 'mod1_name'], $where);
							foreach($res['data'] as $v)
							{
								$modArr[$v['make_id']]['info'] = $v;
								$modArr[$v['make_id']]['mods'][] = $v;
							}
						}
					}
				}
				LibTpl::Set('modArr', $modArr);
				$html = LibTpl::Fetch('item/ajaxModel1.php');
			break;
			case 2:
				$modArr = [];
				if(!empty($Params['mod1Id']))
				{
					$res = $ComModel->getModel(3, ['distinct mod3_id'], sprintf(" and mod1_id=%d", (int) $Params['mod1Id']));
					if(!empty($res['data']))
					{
						$where .= sprintf(" and pml3_mod3_id in(%s)", implode(',', array_column($res['data'], 'mod3_id')));
						$table .= ' join bc_product_model3_link on pml3_pro_id=pro_id';
						$res = $B2cBase->get($table, ['distinct pml3_mod3_id'], $where);
						if(!empty($res['data']))
						{
							$where = sprintf(" and mod3_id in(%s) order by mod2_name, mod3_name, mod3_start_year", implode(',', array_column($res['data'], 'pml3_mod3_id')));
							$res = $ComModel->getModel(3, ['mod3_id', 'mod2_name', 'mod3_name', 'mod3_start_year', 'mod3_end_year', 'mod3_engine', 'mod3_kw', 'mod3_cc', 'mod3_transmission', 'mod3_fuel'], $where);
							$modArr = $res['data'];
						}
					}
				}
				LibTpl::Set('modArr', $modArr);
				$html = LibTpl::Fetch('item/ajaxModel3.php');
			break;
		}
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$html]);
	}


	/**搜索店铺的适用车型***/
	function ajaxSearchModel()
	{
		$Params = mvc::$URL_PARAMS;
		$ComModel = new ComModel();
		$modelArr = [];
		$word = strtoupper(trim($Params['word']));
		if($word != '')
		{
			$ComModel = new ComModel();
			$where = sprintf(" and (brand_name like '%%%s%%' or mod1_name like '%%%s%%')", LibFc::Escape($word), LibFc::Escape($word));
			$res = $ComModel->getModel(1, ['distinct mod1_id'], $where);
			if(!empty($res['data']))
			{
				$B2cBase = new B2cBase();
				$twhere = !empty(mvc::$cfg['PROALL'])? '' : 'and pro_status =1';
				$table = 'bc_product join sh_shop on pro_shop_id=shop_id and shop_status = 1 '.$twhere.' and pro_count>0 and shop_id='.mvc::$cfg['SHOPID'].'  join bc_product_model1_link on pml1_pro_id=pro_id';
				$where = sprintf(" and pml1_mod1_id in(%s)", implode(',', array_column($res['data'], 'mod1_id')));
				$res = $B2cBase->get($table, ['distinct pml1_mod1_id'], $where);
				if(!empty($res['data']))
				{
					$where = sprintf(" and mod1_id in(%s) order by brand_seq, brand_initial, make_seq, make_initial, make_name, mod1_name", implode(',', array_column($res['data'], 'pml1_mod1_id')));
					$res = $ComModel->getModel(1, ['brand_id','brand_name', 'make_id', 'make_name', 'mod1_id', 'mod1_name'], $where);
					if(!empty($res['data']))
					{
						foreach($res['data'] as $v)
						{
							if(empty($modelArr['make'][$v['make_id']]))
								$modelArr['make'][$v['make_id']] = $v;
							$modelArr['model'][$v['make_id']][] = $v;
						}
					}
				}

			}
		}
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$modelArr]);
	}


	/**获取店铺的适用发动机***/
	function ajaxGetEngine()
	{
		$Params = mvc::$URL_PARAMS;
		$B2cBase = new B2cBase();
		$arr = [];
		$twhere = !empty(mvc::$cfg['PROALL'])? '' : 'and pro_status =1';
		$where = sprintf(" and pml3_pro_id in (
			select distinct pro_id from bc_product 
			join sh_shop on pro_shop_id=shop_id and shop_status = 1 %s and pro_count>0 and shop_id=%d
			join bc_product_brand_link on pbl_pro_id=pro_id where pbl_brand_id=%d
		)",$twhere, mvc::$cfg['SHOPID'], $Params['brandId']);
		$res = $B2cBase->get('bc_product_model3_link', ['distinct pml3_mod3_id'], $where);
		if(!empty($res['data']))
		{
			$ComModel = new ComModel();
			$where = sprintf(" and mod3_id in(%s) and mod3_brand_id = %d", implode(',', array_column($res['data'], 'pml3_mod3_id')), $Params['brandId']);
			$res = $ComModel->getModel(3, ['distinct mod3_engine'], $where);
			$arr = array_column($res['data'], 'mod3_engine');
			sort($arr);
		}
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$arr]);
	}


	function ajaxGetModelInfo()
	{
		$Params = mvc::$URL_PARAMS;
		$html = !empty($Params['style']) && $Params['style'] == 'data' ? []: '';
		if(!empty($Params['mod3Id']))
		{
			$ComModel = new ComModel();
			$res = $ComModel->getModel(3, ['make_name', 'mod2_name', 'bc_model3.*'], sprintf(" and mod3_id = %d", (int) $Params['mod3Id']), true);
			if(!empty($res))
			{
				if(!empty($Params['style']) && $Params['style'] == 'data')
				{
					$html = $res['data'];
					unset($html['mod3_keyword']);
					$html['mod3_year'] = LibFc::formatViewYear($html['mod3_start_year'], $html['mod3_end_year'], 2, 0);
				}
				else
				{
					LibTpl::Set('modArr', $res['data']);
					$html = LibTpl::Fetch();
				}
			}
		}
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$html]);
	}

	/**获取某种参数下的分类**/
	function ajaxGetSearchSort()
	{
		$Params = mvc::$URL_PARAMS;
		$B2cBase = new B2cBase();
		$twhere = !empty(mvc::$cfg['PROALL'])? '' : 'and pro_status =1';
		$table = 'bc_product join sh_shop on pro_shop_id=shop_id and shop_status = 1 '.$twhere.' and pro_count>0 and shop_id='.mvc::$cfg['SHOPID'].' join bc_product_sort on prs_pro_id = pro_id';
		$where = '';
		if($Params['type'] == 'number')
		{
			$Params['formatNum'] = LibFc::FormatNum($Params['keyword']);
			if(strlen($Params['formatNum']) > 2)
			{
				$where .= sprintf(" and (num_format like '%s%%' or num_reformat like '%s%%')", $Params['formatNum'], strrev($Params['formatNum']));
				$table .= ' join bc_number on pro_id = num_pro_id';
			}
			else
			{
				$where .= ' and 1=0';
			}
		}
		else
		{
			$Params['mod3Id'] = (int) $Params['mod3Id'];
			if($Params['mod3Id'] > 0)
			{
				$table .= ' join bc_product_model3_link on pro_id = pml3_pro_id';
				$where .= sprintf(" and pml3_mod3_id=%d", $Params['mod3Id']);
			}

			//配件关键字
			if($Params['keyword'] != '')
			{
				$keywordArr = LibFc::keyword($Params['keyword']);
				$B2cKeyword = new B2cKeyword();
				$res = $B2cKeyword->getKeywordKeyIds($keywordArr);
				if(!empty($res['data']))
				{
					$tempWhere = sprintf(" and pro_id in (select prk_pro_id from bc_product_keyword where prk_key_id in (%s) group by prk_pro_id having count(*) = %d)", implode(',', $res['data']), count($res['data']));
				}
				else
				{
					$tempWhere = sprintf(" and pro_name like '%%%s%%'", LibFc::Escape($Params['keyword']));
				}
				$where .= $tempWhere;
			}
		}
		$res = $B2cBase->get($table, ['distinct prs_sort_id'], $where);
		if(!empty($res['data']))
		{
			$sortIdArr = array_column($res['data'], 'prs_sort_id');
			//追加父级
			$res = $B2cBase->get('sh_sort_tree', ['distinct sot_psort_id'], sprintf(" and sot_sort_id in(%s) and sot_psort_id > 0", implode(',', $sortIdArr)));
			$sortIdArr = array_merge($sortIdArr, array_column($res['data'], 'sot_psort_id'));
			$res = $B2cBase->get('sh_sort_tree join sh_sort on sot_sort_id = sort_id', ['sort_id', 'sort_name', 'sot_psort_id'], sprintf(" and sort_id in(%s) and sort_shop_id=%d order by sort_seq, sort_name", implode(',', $sortIdArr), mvc::$cfg['SHOPID']));
			$sortArr = [];
			$sortTreeArr = [];
			foreach($res['data'] as $v)
			{
				$sortTreeArr[$v['sot_psort_id']][] = $v['sort_id'];
				$sortArr[$v['sort_id']] = $v;
			}
		}
		LibTpl::Set('sortTreeArr', $sortTreeArr);
		LibTpl::Set('sortArr', $sortArr);
		$html = LibTpl::Fetch();
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$html]);
	}




	function addUserShopCart()
	{
		$OthCheck = new OthCheck();
		$OthCheck->mustLogin('gzh');

		$B2cShoppingCart = new B2cShoppingCart();
		//参数检测
		if(!LibFc::Int($_POST['GoodNum'])||!LibFc::Int($_POST['thisGoodsId']))
		{
			$msg = array('status'=>false,'data'=>'参数错误！','code'=>0);
			return LibFc::ajaxJsonEncode($msg);
		}

		// 保存
		$B2cShoppingCart = new B2cShoppingCart();
		$B2cShoppingCart->addCart($_POST['thisGoodsId'],$_SESSION['_userid'],$_POST['GoodNum']);

		// 调用接口获取当前购物车数量
		$res = $B2cShoppingCart->getCarGoodNum($_SESSION['_userid']);

		if($res['data'])
		{
			$array = array('status'=>true,'data'=>$res['data'],'code'=>0);
			LibFc::ajaxJsonEncode($array);
		}

	}
	/**
	 * @method    保存收藏配件
	 * @author     廖
	 * @copyright 2017-07-31
	 * @return    LibFc::ReturnData(['status'=>false,'data'=>'xxx']);
	 */
	function ajaxSaveCollPro()
	{
		$OthCheck = new OthCheck();
		$OthCheck->mustLogin('gzh');
		$Params = mvc::$URL_PARAMS;
		if(LibFc::Int($Params['proId']))
		{
			$B2cCollPro = new B2cCollPro();
			if(isset($_SESSION['_shopid']) && LibFc::Int($Params['proId']))
			{
				$res = $B2cCollPro->get('bc_product',['count(*) as count'],sprintf(' and pro_id=%d and pro_shop_id=%d',$Params['proId'],$_SESSION['_shopid']),true);
				if($res['data']['count']>0)
				{
					LibFc::ajaxJsonEncode(['status'=>false,'data'=>'不能收藏自己店铺的配件']);
					exit();
				}
			}
			$re = $B2cCollPro->get('us_product_collection',['count(*) as count'],sprintf(' and prc_uid=%d and prc_pro_id=%d',$_SESSION['_userid'],$Params['proId']),true);
			if($re['data']['count']>0)
			{
				$res = $B2cCollPro->cancelCollPro($_SESSION['_userid'],$Params['proId']);
				if($res['status'])
				{
					LibFc::ajaxJsonEncode(['status'=>true,'data'=>false]);
				}else
				{
					LibFc::ajaxJsonEncode(['status'=>false,'data'=>'取消收藏失败！']);
				}
			}else
			{
				$re = $B2cCollPro->addUserCollPro($_SESSION['_userid'],$Params['proId']);
				if($re['status'])
				{
					LibFc::ajaxJsonEncode(['status'=>true,'data'=>true]);
				}
				exit();
			}
		}else
		{
			LibFc::ajaxJsonEncode(['status'=>false,'data'=>'收藏配件失败！']);
		}
	}
	/**
	 * @method    猜你喜欢
	 * @author 卢
	 * @copyright 2018-07-11
	 */
	function ajaxGetLove()
	{
		$OthCheck = new OthCheck();
		$OthCheck->mustLogin('gzh');
		$Params = mvc::$URL_PARAMS;
		$B2cHistory = new B2cHistory();
		$twhere = !empty(mvc::$cfg['PROALL'])? '' : 'and pro_status =1';
		$where=sprintf('%s and pro_count>0 and shop_status=1 and pro_shop_id=%d',$twhere, mvc::$cfg['SHOPID']);
		$lovelyArr = $B2cHistory->getGuessLovely($_SESSION['_userid'],$perPage=8,$nowPage=1,$where,[],true);
		// print_r($lovelyArr);
		LibTpl::Set('itemArr', $lovelyArr['data']);
		// LibTpl::Set('itemArr', $itemArr);
		$html = LibTpl::Fetch();
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>['html'=>$html]]);
	}



	/**
	 * @method  配件的描叙供客户端显示使用
	 * @author soul 2020/3/18
	 */
	function baseDes()
	{
		$Params = mvc::$URL_PARAMS;
		$B2cBase = new B2cBase();
		$res=$B2cBase->get('bc_product_info',['pri_des'],sprintf(' and pri_pro_id=%d', $Params['proId']),true);
		LibTpl::Set('itemArr', $res['data']);
		LibTpl::Set('bodyClass', 'item-page');
		LibTpl::Set('title', '配件描叙');
		LibTpl::Put();
	}

	/**
	 * @method  取make
	 */
	function getShopMake()
	{
		$Params = mvc::$URL_PARAMS;
		$ComBase = new ComBase();
		$db = $ComBase->COMDb;
		$sql = sprintf("SELECT brand_id,brand_code,brand_name FROM bc_brand
			JOIN ypb2c.bc_product_brand_link ON pbl_brand_id = brand_id
			JOIN ypb2c.bc_product ON pro_shop_id = %d AND pro_id = pbl_pro_id
			GROUP BY brand_id",mvc::$cfg['SHOPID']);
		$res=$db->table($sql);
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$res]);
	}
	/**
	 * @method  取Model1
	 */
	function getShopModel1()
	{
		$Params = mvc::$URL_PARAMS;
		$ComBase = new ComBase();
		$db = $ComBase->COMDb;
		$sql = sprintf("SELECT mod1_id,mod1_name FROM `bc_model1`
			JOIN ypb2c.`bc_product_model1_link` ON pml1_mod1_id=mod1_id
			JOIN ypb2c.`bc_product` ON pro_shop_id = %d AND pro_id = pml1_pro_id
			WHERE mod1_brand_id=%d
			GROUP BY mod1_id",mvc::$cfg['SHOPID'], $Params['brand_id']);
		$res=$db->table($sql);
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$res]);
	}
	/**
	 * @method  取year
	 */
	function getShopYear()
	{
		$Params = mvc::$URL_PARAMS;
		$ComBase = new ComBase();
		$db = $ComBase->COMDb;
		$sql = sprintf("SELECT mod3_mod1_id,MIN(mod3_start_year) min,MAX(IF(mod3_end_year=0,9999,mod3_end_year)) max FROM `bc_model3`
			JOIN ypb2c.`bc_product_model3_link` ON pml3_mod3_id=mod3_id
			JOIN ypb2c.`bc_product` ON pro_shop_id = %d AND pro_id = pml3_pro_id
			WHERE mod3_mod1_id = %d
			GROUP BY mod3_mod1_id",mvc::$cfg['SHOPID'], $Params['mode1_id']);
		$data = [];
		if($res=$db->row($sql)){
			$min = $res['min']*1;
			$max = $res['max']*1;
			if($min==0) $min = 2000;
			if($max==9999) $max = date('Y');
			for($i=$min;$i<=$max;$i++)
				$data[] = $i;
		}

		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$data]);
	}
	/**
	 * @method  取排量
	 */
	function getShopCc()
	{
		$Params = mvc::$URL_PARAMS;
		$ComBase = new ComBase();
		$db = $ComBase->COMDb;
		$sql = sprintf("SELECT mod3_cc,mod3_cc_name FROM `bc_model3`
			JOIN ypb2c.`bc_product_model3_link` ON pml3_mod3_id=mod3_id
			JOIN ypb2c.`bc_product` ON pro_shop_id = %d AND pro_id = pml3_pro_id
			WHERE mod3_mod1_id = %d
			GROUP BY mod3_cc_name",mvc::$cfg['SHOPID'], $Params['mode1_id']);
		$res=$db->table($sql);
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$res]);
	}

	function getHistory()
	{
		$Params = mvc::$URL_PARAMS;
		$LibYpApi = new LibYpApi();
		$Params['ush_shop_id'] = mvc::$cfg['SHOPID'];
		$Params['ush_uid'] = $_SESSION['_userid'];
		$Params['ush_type'] = $Params['type'];
		$res = $LibYpApi->Get('super/getSearchHistory', $Params);
		LibFc::ajaxJsonEncode($res);
	}

	function clearHistory()
	{
		$Params = mvc::$URL_PARAMS;
		$LibYpApi = new LibYpApi();
		$Params['ush_shop_id'] = mvc::$cfg['SHOPID'];
		$Params['ush_uid'] = $_SESSION['_userid'];
		$Params['ush_type'] = $Params['type'];
		$res = $LibYpApi->Get('super/clearSearchHistory', $Params);
		LibFc::ajaxJsonEncode($res);
	}

	function ajaxWxShare()
	{	
		$Params = mvc::$URL_PARAMS;
		$proId = $Params['proId'];
		$OthWxJs = new OthWxJs();
		$signRes = $OthWxJs->getSign($Params['url']??'');
		$B2cProduct = new B2cProduct();
		$uid=!empty($_SESSION['_userid'])?$_SESSION['_userid']:0;
		$prodres = $B2cProduct->getAnyProductInfo([$proId], $uid, ['numMain', 'picMain'],['pro_show_app'=>1]);
		$proArr = $prodres['data'][$proId];
		$title = $proArr['pro_name'];
		$desc  = '';
		if($proArr['num']){
			$desc  = $title;
			$item = '';
			foreach ($proArr['num'] as $num) {
				if(!in_array('item_mainnum',mvc::$cfg['permissionArr']) && $num['type'] == 'main' ||  $num['type'] == 'zb' &&!in_array('item_selfnum',mvc::$cfg['permissionArr'])){
					continue;
				}
				if($num){
					$item = $num;
					break;
				}	
			}
			if($item){
				$title = $item['num_factory'].':'.$item['num_display'];
			}
		}

		$shareData = [
			'title'=> $title,
			'imgUrl'=> empty($proArr['pic'])?'':mvc::$cfg['FILE_CENTER']['b2c']['url_get'].'&c=product&f='.$proArr['pic'][0]['spi_path'].'800/'.$proArr['pic'][0]['spi_name'].mvc::$cfg['FILE_CENTER']['b2c']['get_uri'],
			'link'=>mvc::$cfg['SELF_URI'].'item/info?proId='.$proId,
			'desc'=>$desc,
		];
		$data = [
			'shareData'=>$shareData,
			'signPackage'=>$signRes['data']
		];
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$data]);
	}

	function ajaxGetSearchSortNew()
	{
		$Params = mvc::$URL_PARAMS;
		$B2cBase = new B2cBase();
		$twhere = !empty(mvc::$cfg['PROALL'])? '' : 'and pro_status =1';
		$table = 'bc_product join sh_shop on pro_shop_id=shop_id and shop_status = 1 '.$twhere.' and shop_id='.mvc::$cfg['SHOPID'].' join bc_product_sort on prs_pro_id = pro_id';
		$where = '';
		if($Params['type'] == 'number')
		{
			$Params['formatNum'] = LibFc::FormatNum($Params['keyword']);
			if(strlen($Params['formatNum']) > 2)
			{
				$where .= sprintf(" and (num_format like '%s%%' or num_reformat like '%s%%')", $Params['formatNum'], strrev($Params['formatNum']));
				$table .= ' join bc_number on pro_id = num_pro_id';
			}
			else
			{
				$where .= ' and 1=0';
			}
		}
		else
		{
			$Params['mod3Id'] = (int) $Params['mod3Id'];
			if($Params['mod3Id'] > 0)
			{
				$table .= ' join bc_product_model3_link on pro_id = pml3_pro_id';
				$where .= sprintf(" and pml3_mod3_id=%d", $Params['mod3Id']);
			}
		}
		$res = $B2cBase->get($table, ['distinct prs_sort_id'], $where);
		if(!empty($res['data']))
		{
			$sortIdArr = array_column($res['data'], 'prs_sort_id');
			//追加父级
			$res = $B2cBase->get('sh_sort_tree', ['distinct sot_psort_id'], sprintf(" and sot_sort_id in (%s) and sot_psort_id > 0", implode(',', $sortIdArr)));
			$sortIdArr = array_merge($sortIdArr, array_column($res['data'], 'sot_psort_id'));
			$res = $B2cBase->get('sh_sort_tree join sh_sort on sot_sort_id = sort_id', ['sort_id', 'sort_name', 'sot_psort_id'], sprintf(" and sort_id in(%s) and sort_shop_id=%d  order by sort_seq, sort_name ", implode(',', $sortIdArr), mvc::$cfg['SHOPID']));
			$sortArr = [];
			foreach($res['data'] as $v)
			{
				$temp = [
					'sort_id'=>$v['sort_id'],
					'sort_name'=>$v['sort_name']
				];
				if($v['sot_psort_id']==0){
					$sortArr[$v['sort_id']]['sort_id'] = $v['sort_id'];
					$sortArr[$v['sort_id']]['sort_name'] = $v['sort_name'];
				}else{
					$sortArr[$v['sot_psort_id']]['sons'][] = $temp;
				}
			}
			$data  = array_values($sortArr);
			LibFc::ajaxJsonEncode(['status'=>true,'data'=>$data]);

		}
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>[]]);
	}
}
