<?php
class zdAction extends baseAction
{
	//商用车用户查询历史采集
	function addBigCarSearchTask()
	{
		//exit('请人工开启');
		$toRate = 1;
		$site = 'ZDAPI';
		$YpwVin = new YpwVin();
		$YPCAR67Db = new BaseDb(mvc::$cfg['DB']['YPCAR67']);
		$cacheFile = mvc::$cfg['PATH_CACHE'].'addBigCarSearchTask.php';

		$where = " and gb_type='gxb' and gp_gl_code='vin18' AND gb_gxb_code IN (SELECT DISTINCT regexp_replace(pv_value,'[^0-9a-zA-Z]', '') gxb_code FROM model_360che.para_value WHERE pv_pl_id IN (8,158))";
		$res = $YpwVin->get('gxb_params JOIN gxb_base ON gb_id = gp_gb_id', ['distinct gp_value'], $where);
		$vin8Arr = array_column($res['data'], 'gp_value');
		foreach($vin8Arr as $vin8)
		{
			if(file_exists($cacheFile)) $cacheArr = include($cacheFile);
			if(!empty($cacheArr[$vin8])) continue;

			//$vsres = $YpwVin->get('yp_vin_search JOIN yp_vin_year ON vin_10=vs_vin10', ['left(vs_vin, 8) vin8, right(left(vs_vin, 11), 2) vin10_11, count(*) vs_count'], sprintf(" AND vin_year <= 2021  and vs_vin like '%s%%' group by vin8, vin10_11", $vin8));
			$vsres = $YpwVin->get('yp_vin_search JOIN yp_vin_year ON vin_10=vs_vin10', ['left(vs_vin, 8) vin8, right(left(vs_vin, 11), 2) vin10_11, count(*) vs_count'], sprintf(" AND vin_year >= 2006  and vs_vin like '%s%%' group by vin8, vin10_11", $vin8));
			foreach($vsres['data'] as $vs)
			{
				$smares = $YpwVin->get('stand_model_any', ['count(*) sma_count'], sprintf(" and sma_vin1_8='%s' and sma_vin10_11='%s'", $vs['vin8'], $vs['vin10_11']), true);
				$smaCount = $smares['data']['sma_count'];

				$rate = $smaCount/$vs['vs_count'];
				if($rate >= $toRate) continue;
				$needCount = ceil(($toRate-$rate)*$vs['vs_count']);
				
				$where = sprintf(" and vs_vin like '%s_%s%%' and vs_vin not in (select sma_vin from stand_model_any where sma_vin1_8='%s' and sma_vin10_11='%s') order by left(vs_vin, 8), right(vs_vin, 8)", $vs['vin8'], $vs['vin10_11'], $vs['vin8'], $vs['vin10_11']);
				$res = $YpwVin->get('yp_vin_search', ['vs_vin sce_vin', '555 sce_level'], $where);
				$vsVinArr = $res['data'];
				//间隔取
				$maxjg = ceil(count($vsVinArr)/$needCount);
				$addArr = [];
				for($jg =$maxjg; $jg >= 1; $jg--)
				{
					$vsVinArr = array_values($vsVinArr);
					foreach($vsVinArr as $tkk=>$tvv)
					{
						if($tkk%$jg === 0){
							$addArr[$tvv['sce_vin']] = $tvv;
							unset($vsVinArr[$tkk]);
						} 
						if(count($addArr) >= $needCount) break;
					}
				}
				$YpwVin->addCatchList($site, array_values($addArr));
			}
			$cacheArr[$vin8] = date('Y-m-d H:i:s');
			LibFc::ArraySave($cacheArr, $cacheFile);
		}
		echo 'ok';
	}


	/**
	  * @method 通过600w表填补多 sma_engine,sma_weight,sma_wheel 泽达的车架号 
	  	filed:sma_engine,sma_weight,sma_wheel
	  	type:ypcar bigcar
		addFiledAnyNullTask?filed=sma_engine&type=bigcar&count=100
		随机允许并发
	  * @author soul
	  * @copyright 2023/2/25
	  * @return 
	  */
	function addFiledAnyNullTask()
	{
		//exit('请人工开启');
		set_time_limit(0);
		$Params = mvc::$URL_PARAMS;
		$filed = $Params['filed'];
		$type = $Params['type'];
		if(!in_array($type, ['ypcar', 'bigcar'])) exit('type参数错误');
		$filedNullVal = ['sma_engine'=>'','sma_weight'=>0, 'sma_wheel'=>0];
		if(!isset($filedNullVal[$filed])) exit('filed参数错误');
		$nullVal = $filedNullVal[$filed];
		$cacheBTime = time()-3600*24;
		$YpwVin = new YpwVin();
		$cacheFile = mvc::$cfg['LOGS'].'cache_'.mvc::$URL_METHOD.'_'.$type.'_'.$filed.'.php';
		$gxbVin8Arr = [];
		$fileds = ['smt_vin1_8 vin1_8, smt_gxb_code gxb_code'];
		if($type == 'ypcar'){
			$where = " and (smt_vin1_8 LIKE 'L%' OR smt_vin1_8 LIKE 'H%') group by smt_vin1_8, smt_gxb_code order by rand()";
			if(!empty($Params['count'])) $where .= sprintf(" limit %d", $Params['count']);
			$res = $YpwVin->get('stand_model_ypgxb JOIN stand_model_tj ON smt_vin1_8 = gxb_vin1_8 AND smt_gxb_code = gxb_code', $fileds, $where);
			$gxbVin8Arr = $res['data'];
		}else if($type == 'bigcar'){
			$where = " and smt_gxb_code in (SELECT DISTINCT regexp_replace(pv_value,'[^0-9a-zA-Z]', '') gxb_code FROM model_360che.para_value WHERE pv_pl_id IN (8,158)) group by smt_vin1_8, smt_gxb_code order by rand()";
			//$where = " and smt_gxb_code in ('JX1042TGE26') group by smt_vin1_8, smt_gxb_code order by rand()";
			if(!empty($Params['count'])) $where .= sprintf(" limit %d", $Params['count']);
			$res = $YpwVin->get('stand_model_tj', $fileds, $where);
			$gxbVin8Arr = $res['data'];
		}
		foreach($gxbVin8Arr as $tv)
		{
			$unikey = $tv['vin1_8'].'_'.$tv['gxb_code'];
			$cacheArr = file_exists($cacheFile)? include($cacheFile): [];
			if(!is_array($cacheArr)) continue;
			if(!empty($cacheArr[$unikey]) && strtotime($cacheArr[$unikey]) > $cacheBTime) continue;
			
			//不能直接判断为空的数量
			$where = " and sma_vin1_8='{$tv['vin1_8']}' and sma_gxb_code='{$tv['gxb_code']}' group by sma_vin1_8, sma_vin10_12";
			$sres = $YpwVin->get('stand_model_any', ['sma_vin1_8, sma_vin10_12, sma_gxb_code, MIN(sma_vin13_17) as vin13_17_min, MAX(sma_vin13_17) as vin13_17_max, count(*) sma_count'], $where);
			foreach($sres['data'] as $rv)
			{
				$addVinArr = [];
				$comWhere = sprintf(" and sma_vin1_8='%s' and sma_vin10_12='%s' and sma_gxb_code='%s'", $rv['sma_vin1_8'], $rv['sma_vin10_12'], $rv['sma_gxb_code']);
				
				//最小值附近直接补3条
				$res = $YpwVin->get('stand_model_any', ['sma_vin', $filed], $comWhere." and sma_vin13_17 >= '{$rv['vin13_17_min']}' order by sma_vin13_17 asc limit 3");
				foreach($res['data'] as $ik=>$iv)
				{
					if(empty($iv[$filed])) $addVinArr[] = $iv['sma_vin']; 
				}

				//最大值附近直接补3条
				$res = $YpwVin->get('stand_model_any', ['sma_vin', $filed], $comWhere." and sma_vin13_17 <= '{$rv['vin13_17_max']}' order by sma_vin13_17 desc limit 3");
				foreach($res['data'] as $ik=>$iv)
				{
					if(empty($iv[$filed])) $addVinArr[] = $iv['sma_vin']; 
				}

				$res = $YpwVin->get('stand_model_any', ['sma_vin', 'sma_vin13_17', $filed], $comWhere." and {$filed}!='{$nullVal}' and sma_vin13_17 >= '{$rv['vin13_17_min']}' order by sma_vin13_17 asc limit 1", true);
				$startArr = $res['data'];
				while(!empty($startArr['sma_vin13_17']) && $startArr['sma_vin13_17'] < $rv['vin13_17_max'])
				{
					//和start的型号不同最小vin
					$res =  $YpwVin->get('stand_model_any', ['sma_vin, sma_vin13_17', $filed], $comWhere." and {$filed}!='{$nullVal}' and {$filed}!='{$startArr[$filed]}' and sma_vin13_17>'{$startArr['sma_vin13_17']}' order by sma_vin13_17 asc limit 1", true);
					if(empty($res['data'])) break;
					$nextArr = $res['data'];

					//和nextArr的型号不同最大vin
					$res =  $YpwVin->get('stand_model_any', ['sma_vin, sma_vin13_17', $filed], $comWhere." and {$filed}!='{$nullVal}' and {$filed}!='{$nextArr[$filed]}' and sma_vin13_17<'{$nextArr['sma_vin13_17']}' order by sma_vin13_17 desc limit 1", true);
					if(empty($res['data'])) break;
					$startArr = $res['data'];

					//start 上下取3条 为空的直接采集
					$bvin13_17 = $startArr['sma_vin13_17'];
					$res = $YpwVin->get('stand_model_any', ['sma_vin, sma_vin13_17', $filed], $comWhere." and sma_vin13_17 < '{$startArr['sma_vin13_17']}' order by sma_vin13_17 desc limit 3");
					foreach($res['data'] as $ik=>$iv)
					{
						if(empty($iv[$filed])) $addVinArr[] = $iv['sma_vin']; 
					}

					$res = $YpwVin->get('stand_model_any', ['sma_vin, sma_vin13_17', $filed], $comWhere." and sma_vin13_17 > '{$startArr['sma_vin13_17']}' order by sma_vin13_17 asc limit 3");
					foreach($res['data'] as $ik=>$iv)
					{
						if(empty($iv[$filed])) $addVinArr[] = $iv['sma_vin']; 
						$bvin13_17 = $iv['sma_vin13_17'];
					}

					//next 上下取3条 为空的直接采集
					$evin13_17 = $nextArr['sma_vin13_17'];
					$res = $YpwVin->get('stand_model_any', ['sma_vin, sma_vin13_17', $filed], $comWhere." and sma_vin13_17 < '{$nextArr['sma_vin13_17']}' order by sma_vin13_17 desc limit 3");
					foreach($res['data'] as $ik=>$iv)
					{
						if(empty($iv[$filed])) $addVinArr[] = $iv['sma_vin']; 
						$evin13_17 = $iv['sma_vin13_17'];
					}

					$res = $YpwVin->get('stand_model_any', ['sma_vin, sma_vin13_17', $filed], $comWhere." and sma_vin13_17 > '{$nextArr['sma_vin13_17']}' order by sma_vin13_17 asc limit 3");
					foreach($res['data'] as $ik=>$iv)
					{
						if(empty($iv[$filed])) $addVinArr[] = $iv['sma_vin']; 
					}
					
					//这个范围内为空的车架号 间隔取3条
					$res = $YpwVin->get('stand_model_any', ['sma_vin'], $comWhere." and {$filed}='{$nullVal}' and sma_vin13_17>'{$bvin13_17}' and sma_vin13_17<'{$evin13_17}' and sma_vin not in (select sce_vin from stand_catch_list where sce_reg_code='ZDAPI') order by sma_vin13_17 asc");
					$vsVinArr = $res['data'];
					if(!empty($vsVinArr))
					{
						$tArr = [];
						$needCount = count($vsVinArr) >= 3? 3: count($vsVinArr);
						$maxjg = ceil(count($vsVinArr)/$needCount);
						for($jg =$maxjg; $jg >= 1; $jg--)
						{
							$vsVinArr = array_values($vsVinArr);
							foreach($vsVinArr as $tkk=>$tvv)
							{
								if($tkk%$jg === 0){
									$tArr[] = $tvv['sma_vin']; 
									unset($vsVinArr[$tkk]);
								} 
								if(count($tArr) >= $needCount) break;
							}
						}
						$addVinArr = array_unique(array_merge($addVinArr, $tArr));
					}

					//交换开始结束
					$startArr = $nextArr;
					$nextArr = [];
				}

				if(!empty($addVinArr))
				{
					$addVinArr = array_unique($addVinArr);
					$addArr = [];
					foreach($addVinArr as $tvin)
					{
						$addArr[] = [
							'sce_reg_code'=>'ZDAPI',
							'sce_vin'=>$tvin,
							'sce_level'=>'333'
						];
					}
					$YpwVin->addCatchList('ZDAPI', $addArr);
				}
			}
			
			$cacheArr = file_exists($cacheFile)? include($cacheFile): [];
			if(!is_array($cacheArr)) continue;
			$cacheArr[$unikey] = date('Y-m-d H:i:s');
			LibFc::ArraySave($cacheArr, $cacheFile);
		}
		echo 'ok';
	}
}