<?php
class epcAction extends baseAction
{
	/**
	  * @method 根据EPC唯一编码结合600w表计算车架号的型号
	  * @param  wmi string  LBE
	  * @author soul
	  * @copyright 2022/3/5
	  */
	public function addSma()
	{
		set_time_limit(0);
		$Params = mvc::$URL_PARAMS;
		$YPCAR67Db = new BaseDb(mvc::$cfg['DB']['YPCAR67']);
		$YpwVin = new YpwVin();
		$wmi = $Params['wmi'];
		$file = mvc::$cfg['LOGS'].'addSma_'.$wmi.'.txt';
		//if(file_exists($file) && filemtime($file) < time()-3600*24) @unlink($file);
		@unlink($file);
		$runcount = 2000000;
		switch($wmi)
		{
			case 'LBE':
			case 'LJD':
				$where = sprintf(" and lnk_vin_8bit LIKE '%s%%' GROUP BY lnk_extend_code", $wmi);
				$tjres = $YPCAR67Db->get('temp_hyundai', ['lnk_extend_code'], $where);
				shuffle($tjres['data']);
				foreach($tjres['data'] as $tv)
				{
					if($runcount < 1) break;
					if(file_exists($file)) $cacheTxt = @file_get_contents($file);
					if(stripos($cacheTxt, $tv['lnk_extend_code']) !== false) continue;
					if(!LibFc::hasFunLock($tv['lnk_extend_code'], 600)) continue;
					$runcount--;

					$res = $YPCAR67Db->get('temp_hyundai', ['lnk_vin sma_vin', 'lnk_dated sma_year'], sprintf(" and lnk_extend_code='%s'", $tv['lnk_extend_code']));
					$epcVinArr = array_column($res['data'], null, 'sma_vin');
					$res = $YpwVin->getSmaGxb(array_keys($epcVinArr), ['sma_gxb_code', 'sma_vin'], " and sma_gxb_code != '' and sma_from != 'ocr'");
					$hasArr = array_column($res['data'], null, 'sma_vin');
					$newVinArr = array_diff(array_keys($epcVinArr), array_keys($hasArr));
					$gxbArr = array_unique(array_column($hasArr, 'sma_gxb_code'));
					//存在的数量大约10条
					if(count($newVinArr) < 1)
					{
						LibLog::write($tv['lnk_extend_code'].' 无新VIN', $file);
						LibFc::delFunLock($tv['lnk_extend_code']);
						continue;
					}
					//泽达
					if(count($epcVinArr) < 10)
					{
						$YpwVin->catchAnyVinfromZdApi($newVinArr);
						LibLog::write($tv['lnk_extend_code'].' 泽达:'.count($epcVinArr), $file);
						LibFc::delFunLock($tv['lnk_extend_code']);
						continue;
					}
					else if(count($gxbArr) == 1)
					{
						$zdVinArr = [];
						//取首尾
						$res = $YPCAR67Db->get('temp_hyundai', ['lnk_vin sma_vin'], sprintf(" and lnk_extend_code='%s' order by lnk_vin_8bit asc, right(lnk_vin, 8) asc limit 1", $tv['lnk_extend_code']), true);
						if(empty($hasArr[$res['data']['sma_vin']])) $zdVinArr[] = $res['data']['sma_vin'];

						$res = $YPCAR67Db->get('temp_hyundai', ['lnk_vin sma_vin'], sprintf(" and lnk_extend_code='%s' order by lnk_vin_8bit desc, right(lnk_vin, 8) desc limit 1", $tv['lnk_extend_code']), true);
						if(empty($hasArr[$res['data']['sma_vin']])) $zdVinArr[] = $res['data']['sma_vin'];

						$res = $YpwVin->catchAnyVinfromZdApi($zdVinArr);
						foreach($res['data'] as $zv)
						{
							if(empty($zv['viz_gxb_code'])) continue;
							$hasArr[$zv['viz_vin']] = ['sma_gxb_code'=>$zv['viz_gxb_code'], 'sma_vin'=>$zv['viz_vin']];
							if(!empty($zv['viz_gxb_code']) && !in_array($zv['viz_gxb_code'], $gxbArr))
							{
								$gxbArr[] = $zv['viz_gxb_code'];
							}
						}
						
						//还是不足10个，继续泽达追加
						if(count($hasArr) < 10)
						{
							foreach($newVinArr as $avin)
							{
								$res = $YpwVin->catchAnyVinfromZdApi([$avin]);
								if(empty($res['data'][$avin]['viz_gxb_code'])) continue;
								$zv = $res['data'][$avin];
								$hasArr[$zv['viz_vin']] = ['sma_gxb_code'=>$zv['viz_gxb_code'], 'sma_vin'=>$zv['viz_vin']];
								if(!empty($zv['viz_gxb_code']) && !in_array($zv['viz_gxb_code'], $gxbArr))
								{
									$gxbArr[] = $zv['viz_gxb_code'];
								}
								if(count($hasArr) >= 10) break;
							}
						}
					}

					if(count($hasArr) < 10) 
					{
						LibLog::write($tv['lnk_extend_code'].' 存在VIN:'.count($hasArr), $file);
						LibFc::delFunLock($tv['lnk_extend_code']);
						continue;
					}

					if(count($gxbArr) == 1) 
					{
						//取型号的显示型号
						$res = $YpwVin->get('stand_model_any', ['sma_gxb_code_show'], sprintf(" and sma_gxb_code='%s' and sma_gxb_code_show != '' limit 1", $gxbArr[0]), true);
						$gxbshow = $res['data']['sma_gxb_code_show'];
						
						$addArr = [];
						$total = count($newVinArr);
						foreach($newVinArr as $avin)
						{
							$total--;
							$add = $epcVinArr[$avin];
							$add['sma_gxb_code_show'] = $gxbshow;
							$add['sma_from'] = 'epc';
							if(isset($add['sma_year']))
							{
								$vinYear = LibFc::VinYear($avin);
								$add['sma_year'] = date('Y-m-d', strtotime($add['sma_year']));
								if(abs(substr($add['sma_year'], 0, 4) - $vinYear) > 2) unset($add['sma_year']);
							}
							$addArr[] = $add;
							if($total == 0 || count($addArr) == 1000)
							{
								$YpwVin->addAnySma($addArr, false);
								$addArr = [];
							}
						}
						
						LibLog::write($tv['lnk_extend_code'].' ok'.count($newVinArr), $file);
						LibFc::delFunLock($tv['lnk_extend_code']);
					}
					else
					{
						LibLog::write($tv['lnk_extend_code'].' 多型号:'.implode(',', $gxbArr), $file);
						LibFc::delFunLock($tv['lnk_extend_code']);
					}
				}
			break;
			case 'LFV':
				$bytable = 'vw_catch';
				$where = sprintf(" and cat_vin LIKE '%s%%' GROUP BY cat_extend_code", $wmi);
				$tjres = $YPCAR67Db->get($bytable, ['cat_extend_code lnk_extend_code'], $where);
				shuffle($tjres['data']);
				foreach($tjres['data'] as $tv)
				{
					if($runcount < 1) break;
					if(file_exists($file)) $cacheTxt = @file_get_contents($file);
					if(stripos($cacheTxt, $tv['lnk_extend_code']) !== false) continue;
					if(!LibFc::hasFunLock($tv['lnk_extend_code'], 1800)) continue;
					$runcount--;

					$res = $YPCAR67Db->get($bytable, ['cat_vin sma_vin', 'cat_yearm sma_year', 'cat_engine sma_engine'], sprintf(" and cat_extend_code='%s'", $tv['lnk_extend_code']));
					$epcVinArr = array_column($res['data'], null, 'sma_vin');
					$res = $YpwVin->getSmaGxb(array_keys($epcVinArr), ['sma_gxb_code', 'sma_vin'], " and sma_gxb_code != '' and sma_from != 'ocr'");
					$hasArr = array_column($res['data'], null, 'sma_vin');
					$newVinArr = array_diff(array_keys($epcVinArr), array_keys($hasArr));
					$gxbArr = array_unique(array_column($hasArr, 'sma_gxb_code'));
					//存在的数量大约10条
					if(count($newVinArr) < 1)
					{
						LibLog::write($tv['lnk_extend_code'].' 无新VIN', $file);
						LibFc::delFunLock($tv['lnk_extend_code']);
						continue;
					}
					//泽达
					if(count($epcVinArr) < 10)
					{
						$YpwVin->catchAnyVinfromZdApi($newVinArr);
						LibLog::write($tv['lnk_extend_code'].' 泽达:'.count($epcVinArr), $file);
						LibFc::delFunLock($tv['lnk_extend_code']);
						continue;
					}
					else if(count($gxbArr) == 1)
					{
						$zdVinArr = [];
						//取首尾
						$res = $YPCAR67Db->get($bytable, ['cat_vin sma_vin'], sprintf(" and cat_extend_code='%s' order by cat_10 asc, cat_127 asc limit 1", $tv['lnk_extend_code']), true);
						if(empty($hasArr[$res['data']['sma_vin']])) $zdVinArr[] = $res['data']['sma_vin'];

						$res = $YPCAR67Db->get($bytable, ['cat_vin sma_vin'], sprintf(" and cat_extend_code='%s' order by cat_10 desc, cat_127 desc limit 1", $tv['lnk_extend_code']), true);
						if(empty($hasArr[$res['data']['sma_vin']])) $zdVinArr[] = $res['data']['sma_vin'];

						$res = $YpwVin->catchAnyVinfromZdApi($zdVinArr);
						foreach($res['data'] as $zv)
						{
							if(empty($zv['viz_gxb_code'])) continue;
							$hasArr[$zv['viz_vin']] = ['sma_gxb_code'=>$zv['viz_gxb_code'], 'sma_vin'=>$zv['viz_vin']];
							if(!empty($zv['viz_gxb_code']) && !in_array($zv['viz_gxb_code'], $gxbArr))
							{
								$gxbArr[] = $zv['viz_gxb_code'];
							}
						}
						
						//还是不足10个，继续泽达追加
						if(count($hasArr) < 10)
						{
							foreach($newVinArr as $avin)
							{
								$res = $YpwVin->catchAnyVinfromZdApi([$avin]);
								if(empty($res['data'][$avin]['viz_gxb_code'])) continue;
								$zv = $res['data'][$avin];
								$hasArr[$zv['viz_vin']] = ['sma_gxb_code'=>$zv['viz_gxb_code'], 'sma_vin'=>$zv['viz_vin']];
								if(!empty($zv['viz_gxb_code']) && !in_array($zv['viz_gxb_code'], $gxbArr))
								{
									$gxbArr[] = $zv['viz_gxb_code'];
								}
								if(count($hasArr) >= 10) break;
							}
						}
					}

					if(count($hasArr) < 10) 
					{
						LibLog::write($tv['lnk_extend_code'].' 存在VIN:'.count($hasArr), $file);
						LibFc::delFunLock($tv['lnk_extend_code']);
						continue;
					}

					if(count($gxbArr) == 1) 
					{
						//取型号的显示型号
						$res = $YpwVin->get('stand_model_any', ['sma_gxb_code_show'], sprintf(" and sma_gxb_code='%s' and sma_gxb_code_show != '' limit 1", $gxbArr[0]), true);
						$gxbshow = $res['data']['sma_gxb_code_show'];
						
						$addArr = [];
						$total = count($newVinArr);
						foreach($newVinArr as $avin)
						{
							$total--;
							$add = $epcVinArr[$avin];
							$add['sma_gxb_code_show'] = $gxbshow;
							$add['sma_from'] = 'epc';
							if(isset($add['sma_year']))
							{
								$vinYear = LibFc::VinYear($avin);
								$add['sma_year'] = date('Y-m-d', strtotime($add['sma_year']));
								if(abs(substr($add['sma_year'], 0, 4) - $vinYear) > 2) unset($add['sma_year']);
							}

							if(!empty($add['sma_engine']))
							{
								//FV6498BCBEV  EBR/EBP
								if(strlen($add['sma_engine']) > 3 && stripos($add['sma_engine'], '/') === false)
								{
									//只取前3位
									$add['sma_engine'] = substr($add['sma_engine'], 0, 3);
								}
							}
							$addArr[] = $add;
							if($total == 0 || count($addArr) == 1000)
							{
								$YpwVin->addAnySma($addArr, false);
								$addArr = [];
							}
						}
						LibLog::write($tv['lnk_extend_code'].' ok:'.count($newVinArr), $file);
						LibFc::delFunLock($tv['lnk_extend_code']);
					}
					else
					{
						LibLog::write($tv['lnk_extend_code'].' 多型号:'.implode(',', $gxbArr), $file);
						LibFc::delFunLock($tv['lnk_extend_code']);
					}
				}
			break;
			//荣威名爵
			case 'LSJ':
				$where = sprintf(" and vin1_8 LIKE '%s%%' GROUP BY vin_extend_code", $wmi);
				$tjres = $YPCAR67Db->get('temp_roewe_vin', ['vin_extend_code'], $where);
				shuffle($tjres['data']);
				foreach($tjres['data'] as $tv)
				{
					if($runcount < 1) break;
					echo $tv['vin_extend_code']."\n";
					if(file_exists($file)) $cacheTxt = @file_get_contents($file);
					if(stripos($cacheTxt, $tv['vin_extend_code']) !== false) continue;
					if(!LibFc::hasFunLock($tv['vin_extend_code'], 600)) continue;
					$runcount--;

					$res = $YPCAR67Db->get('temp_roewe_vin', ['vin_code sma_vin'], sprintf(" and vin_extend_code='%s'", $tv['vin_extend_code']));
					$epcVinArr = array_column($res['data'], null, 'sma_vin');
					$res = $YpwVin->getSmaGxb(array_keys($epcVinArr), ['sma_gxb_code', 'sma_vin'], " and sma_gxb_code != '' and sma_from != 'ocr'");
					$gxbTj = [];
					foreach($res['data'] as $xv){
						if(!isset($gxbTj[$xv['sma_gxb_code']])){
							$gxbTj[$xv['sma_gxb_code']] = 1;
						}else{
							$gxbTj[$xv['sma_gxb_code']]++;
						}
					}
					$hasArr = array_column($res['data'], null, 'sma_vin');
					$newVinArr = array_diff(array_keys($epcVinArr), array_keys($hasArr));
					//存在的数量大约10条
					if(count($newVinArr) < 1)
					{
						LibLog::write($tv['vin_extend_code'].' 无新VIN', $file);
						LibFc::delFunLock($tv['vin_extend_code']);
						continue;
					}
					//泽达
					if(count($epcVinArr) < 10)
					{
						$YpwVin->catchAnyVinfromZdApi($newVinArr);
						LibLog::write($tv['vin_extend_code'].' 泽达:'.count($epcVinArr), $file);
						LibFc::delFunLock($tv['vin_extend_code']);
						continue;
					}
					else if(count($gxbTj) == 1)
					{
						$zdVinArr = [];
						//取首尾
						$res = $YPCAR67Db->get('temp_roewe_vin', ['vin_code sma_vin'], sprintf(" and vin_extend_code='%s' order by vin1_8 asc, vin10_11 asc, vin_12_17 asc limit 1", $tv['vin_extend_code']), true);
						if(empty($hasArr[$res['data']['sma_vin']])) $zdVinArr[] = $res['data']['sma_vin'];

						$res = $YPCAR67Db->get('temp_roewe_vin', ['vin_code sma_vin'], sprintf(" and vin_extend_code='%s' order by vin1_8 desc,vin10_11 desc,vin_12_17 desc limit 1", $tv['vin_extend_code']), true);
						if(empty($hasArr[$res['data']['sma_vin']])) $zdVinArr[] = $res['data']['sma_vin'];

						$res = $YpwVin->catchAnyVinfromZdApi($zdVinArr);
						foreach($res['data'] as $zv)
						{
							if(empty($zv['viz_gxb_code'])) continue;
							$hasArr[$zv['viz_vin']] = ['sma_gxb_code'=>$zv['viz_gxb_code'], 'sma_vin'=>$zv['viz_vin']];
							$gxbTj[$zv['viz_gxb_code']]++;
						}
						
						//还是不足10个，继续泽达追加
						if(count($hasArr) < 10)
						{
							foreach($newVinArr as $avin)
							{
								$res = $YpwVin->catchAnyVinfromZdApi([$avin]);
								if(empty($res['data'][$avin]['viz_gxb_code'])) continue;
								$zv = $res['data'][$avin];
								$hasArr[$zv['viz_vin']] = ['sma_gxb_code'=>$zv['viz_gxb_code'], 'sma_vin'=>$zv['viz_vin']];
								$gxbTj[$zv['viz_gxb_code']]++;
								if(count($hasArr) >= 10) break;
							}
						}
					}

					if(count($hasArr) < 10) 
					{
						LibLog::write($tv['vin_extend_code'].' 存在VIN:'.count($hasArr), $file);
						LibFc::delFunLock($tv['vin_extend_code']);
						continue;
					}
					arsort($gxbTj);
					$gxbTj = array_map(function ($key, $value) {
						return ["gxb" => $key, "count" => $value];
					}, array_keys($gxbTj), $gxbTj);
					
					if(count($gxbTj) == 1 || ($gxbTj[0]['count'] >= 200 && $gxbTj[1]['count']/$gxbTj[0]['count'] <= 0.01))
					{
						//取型号的显示型号
						$res = $YpwVin->get('stand_model_any', ['sma_gxb_code_show'], sprintf(" and sma_gxb_code='%s' and sma_gxb_code_show != '' limit 1", $gxbTj[0]['gxb']), true);
						if(empty($res['data'])){
							LibLog::write($tv['vin_extend_code'].' 型号不存在:'.$gxbTj[0]['gxb'], $file);
							LibFc::delFunLock($tv['vin_extend_code']);
						}
						$gxbshow = $res['data']['sma_gxb_code_show'];
						
						$addArr = [];
						$total = count($newVinArr);
						foreach($newVinArr as $avin)
						{
							$total--;
							$add = $epcVinArr[$avin];
							$add['sma_gxb_code_show'] = $gxbshow;
							$add['sma_from'] = 'epc';
							$addArr[] = $add;
							if($total == 0 || count($addArr) == 1000)
							{
								$YpwVin->addAnySma($addArr, false);
								$addArr = [];
							}
						}
						
						LibLog::write($tv['vin_extend_code'].' 型号:'.$gxbshow.'=>ok=>'.count($newVinArr), $file);
						LibFc::delFunLock($tv['vin_extend_code']);
					}
					else
					{
						LibLog::write($tv['vin_extend_code'].' 多型号:'.implode(',', array_column($gxbTj, 'gxb')), $file);
						LibFc::delFunLock($tv['vin_extend_code']);
					}
				}
			break;
		}
		LibLog::write(' all end', $file);
		echo 'ok';
	}
		
	//合并时间
	public function mergePeriods($periods, $mindate = 0)
	{
		$merged = array();
		// 将时间段按照开始时间排序
		usort($periods, function($a, $b) {
			return strtotime($a['bymd']) - strtotime($b['bymd']);
		});
		//$merged[] = $periods[0];
		$abstime = 3600*24*$mindate;
		foreach ($periods as $period) {
			$lastPeriod = end($merged);
			if ($lastPeriod['eymd'] && (strtotime($period['bymd']) <= strtotime($lastPeriod['eymd']) || strtotime($period['bymd']) - strtotime($lastPeriod['eymd']) <= $abstime)) {
				// 如果有重叠部分，更新开始时间
				$lastPeriod['bymd'] = min($lastPeriod['bymd'], $period['bymd']);
				// 如果有重叠部分，更新结束时间
				$lastPeriod['eymd'] = max($lastPeriod['eymd'], $period['eymd']);
				$merged[key($merged)] = $lastPeriod;
			} else {
				// 如果没有重叠，添加新的时间段
				$merged[] = $period;
			}
		}
		return $merged;
	}

	//计算现代因为产品断代需要重采的 ?etkid=3
	public function jsHydNeedCatchPro()
	{
		set_time_limit(0);
		$Params = mvc::$URL_PARAMS;
		$etkid = (int) $Params['etkid'];
		$YPCAR67Db = new BaseDb(mvc::$cfg['DB']['YPCAR67']);
		$EpcBase = new EpcBase($etkid);
		
		$table = 'yp_model3
		JOIN yp_model2 ON m3_m2_id = m2_id
		JOIN yp_model1 ON m2_m1_id = m1_id
		JOIN yp_brand ON m1_b_id = b_id
		JOIN yp_model3_etk_code ON mec_m3_id = m3_id';
		//有任务未完成的车型不参与
		$where = sprintf(" and b_etk_id = %d and m3_id not in (select ept_m3_id from etk_product_task where ept_status = 0) GROUP BY m3_id", $etkid);
		$res = $YPCAR67Db->get($table, ['m3_id, b_name,m1_name, m2_name, m3_name, GROUP_CONCAT(DISTINCT mec_code) m3_etkcodes'], $where);
		$chkModArr = array_column($res['data'], null, 'm3_id');
		foreach($chkModArr as $m3id=>$mod)
		{
			//任务关联的etkcode
			$res = $YPCAR67Db->get('etk_product_task', ['distinct ept_etk_code'], sprintf(" and ept_status=1 and ept_m3_id=%d", $mod['m3_id']));
			$mod['etkcodes'] = array_merge(explode(',', $mod['m3_etkcodes']), array_column($res['data'], 'ept_etk_code'));
			$mod['etkcodes_str'] = implode("','", $mod['etkcodes']);
			switch($etkid)
			{
				case 3: 
					//对应VIN最大最小年份
					$res =  $YPCAR67Db->get('temp_hyundai', ['MIN(lnk_dated) min_dated, MAX(lnk_dated) max_dated'], sprintf(" and lnk_extend_code IN (SELECT mem_code FROM yp_model3_modcode WHERE mem_m3_id=%d)", $mod['m3_id']), true);
					if(empty($res['data']['min_dated']) || empty($res['data']['max_dated'])) break;
					$mod = array_merge($mod, $res['data']);
					$mod['min_dated'] = date('Ymd', strtotime($mod['min_dated']));
					$mod['max_dated'] = date('Ymd', strtotime($mod['max_dated']));
					//var_dump($mod);
					$table = 'data_product
					JOIN data_links ON lnk_pro_id = pro_id
					JOIN data_part ON prt_id = lnk_prt_id
					JOIN data_group ON prt_grp_id = grp_id
					JOIN data_value ON grp_vin_id = vin_id';
					$fields = ['lnk_id, lnk_prt_id, lnk_code AS weizhi, grp_item_1 grp1_code, grp_name_1 as grp1_name, prt_item_3 grp2_code, prt_name_2 as grp2_name, lnk_name AS pro_name, lnk_year_s, lnk_year_e, pro_number as display'];
					$prores = $EpcBase->get($table, $fields, sprintf(" and vin_name in ('%s')", $mod['etkcodes_str']));

					//由于同一个位置有多个号码 LJD7AA1D7N0200575 发动机	20-221 气缸盖	22110B
					//用主组+子组+位置进行合并，合并后的时间在最小最大时间范围（不含相等）内的重采

					$mergeArr = [];
					foreach($prores['data'] as $pv){
						$unikey = json_encode([$pv['grp1_code']=>$pv['grp1_name'], $pv['grp2_code']=>$pv['grp2_name'], $pv['weizhi']=>$pv['pro_name']], JSON_UNESCAPED_UNICODE);
						$pv['unikey'] = $unikey;
						$pv['bymd'] = !empty($pv['lnk_year_s']) ? date('Ymd', strtotime($pv['lnk_year_s'])): $mod['min_dated'];
						$pv['eymd'] = !empty($pv['lnk_year_e']) ? date('Ymd', strtotime($pv['lnk_year_e'])): $mod['max_dated'];
						if(empty($mergeArr[$unikey])){
							$merge = ['pro'=>[],'dates'=>[]];
						}else{
							$merge = $mergeArr[$unikey];
						}
						$merge['pro'][] = $pv;
						$merge['dates'][$pv['bymd'].'~'.$pv['eymd']] = ['bymd'=>$pv['bymd'], 'eymd'=>$pv['eymd']];
						$mergeArr[$unikey] = $merge;
					}
					
					$needArr = [];
					foreach($mergeArr as $unikey=>$merge){
						$merge['dates'] = array_values($merge['dates']);
						//合并
						if(count($merge['dates']) > 1) $merge['dates'] = $this->mergePeriods($merge['dates'], 1);
						$merge['needs'] = [];
						if(count($merge['dates']) > 1){
							var_dump($merge, 111111111);
							foreach($merge['dates'] as $dv){
									
							}
							exit;
						}else if(count($merge['dates']) == 1){
							$dv = $merge['dates'][0];
							if($dv['bymd'] <= $mod['min_dated'] && $dv['eymd'] >= $mod['max_dated']){//不需要处理
							}else if($dv['bymd'] > $mod['min_dated']){//左侧
								$merge['needs'][] = ['bymd'=>$mod['min_dated'], 'eymd'=>$dv['bymd']];
							}else if($dv['eymd'] < $mod['max_dated']){//右侧
								$merge['needs'][] = ['bymd'=>$dv['eymd'], 'eymd'=>$mod['max_dated']];
							}
						}
						if(!empty($merge['needs'])){
							foreach($merge['needs'] as $dv){
								$tkey = $dv['bymd'].'~'.$dv['eymd'];
								if(empty($needArr[$tkey])) $needArr[$tkey] = $dv;
								$needArr[$tkey]['pro'][] = $merge['pro'][0];
							}
						}
					}
					//查找新的etk code
					foreach($needArr as $tk=>$tv){
						$where = sprintf(" and lnk_extend_code IN (SELECT mem_code FROM yp_model3_modcode WHERE mem_m3_id=%d) and UNIX_TIMESTAMP(lnk_dated) >=%d and UNIX_TIMESTAMP(lnk_dated) <=%d GROUP BY ym ORDER BY COUNT(*) DESC limit 1", $mod['m3_id'], strtotime($tv['bymd']), strtotime($tv['eymd']));
						$res = $YPCAR67Db->get('temp_hyundai', ["lnk_vin, lnk_dated, FROM_UNIXTIME(UNIX_TIMESTAMP(lnk_dated),'%Y%m') ym, COUNT(*)"], $where, true);
						//echo  $where, '<br>';
						$tv['etk_code'] = $res['data']['lnk_vin'];
						foreach($tv['pro'] as $tpv){
							$add = [
								'ept_m3_id'=>$mod['m3_id'],
								'ept_etk_id'=>$etkid,
								'ept_etk_code'=>$tv['etk_code'],
								'ept_etk_code_from'=>$mod['m3_etkcodes'],
								'ept_etk_bymd'=>$tv['bymd'],
								'ept_etk_eymd'=>$tv['eymd'],
								'ept_prokey'=>$tpv['unikey'],
								'ept_pro'=>json_encode($tpv, JSON_UNESCAPED_UNICODE),
								'ept_atime'=>time()
							];
							//$chk = $YPCAR67Db->get('etk_product_task', ['ept_id'], sprintf(" and ept_etk_code='%s' and ept_prokey='%s'", $add['ept_etk_code'], LibFc::Escape($add['ept_prokey'])));
							$chk = $YPCAR67Db->get('etk_product_task', ['ept_id'], sprintf(" and ept_m3_id=%d and ept_prokey='%s'", $mod['m3_id'], LibFc::Escape($add['ept_prokey'])));
							if(empty($chk['data'])){
								$YPCAR67Db->add('etk_product_task', $add);
							}
						}
					}
				break;

			}
		}
		echo 'ok';
	}


	
	//计算一车一件需要采集的车架号，并添加到任务里 116每天执行一次
	public function jsMjNeedCatchVin()
	{
		exit;
		$YPCAR67Db = new BaseDb(mvc::$cfg['DB']['YPCAR67']);
		$table = 'yp_model3
		JOIN yp_model2 ON m3_m2_id = m2_id
		JOIN yp_model1 ON m2_m1_id = m1_id
		JOIN yp_make ON m1_m_id = m_id
		JOIN yp_brand ON m1_b_id = b_id';
		$tres = $YPCAR67Db->get($table, ['m3_id, m3_vin_demo'], " and b_etk_id=0 and b_epc_mj = 1 AND LENGTH(m3_vin_demo) = 17 and m3_vin_demo not in (select ce_vin from catch_epcmj where ce_type='vin') ORDER BY m3_id DESC");
		$YpcModel = new YpcModel();
		foreach($tres['data'] as $k=>$v){
			$res = $YpcModel->checkModelPart($v['m3_id']);
			if(empty($res['data']['has'])){
				$chkres = $YPCAR67Db->get('catch_epcmj', ['ce_vin'], sprintf(" and ce_vin='%s' and ce_type='vin'", $v['m3_vin_demo']));
				if(empty($chkres['data'])){
					$add = [
						'ce_vin'=>$v['m3_vin_demo'],
						'ce_m3_id'=>$v['m3_id'],
						'ce_type'=>'vin',
						'ce_atime'=>time()
					];
					$YPCAR67Db->add('catch_epcmj', $add);
				}
			}
		}
		echo 'ok';
	}
}