<?php
class tjAction extends baseAction
{
	//车型库同步到统计库
	public function updateYpcModelToTjdb()
	{
		$PjltjReport = new PjltjReport();
		$PjltjReport->updateYpcModelToTjdb();
		echo 'ok';
	}

	//初始化车型相关原始统计
	public function initModOrgTj()
	{
		//if(!LibFc::hasFunLock(mvc::$URL_METHOD, 600)) return true;
		$PjltjReport = new PjltjReport();
		$PjltjReport->initModOrgTj();
		//LibFc::delFunLock(mvc::$URL_METHOD);
		echo 'ok';
	}

	//行业报表
	public function initReport()
	{
		if(!LibFc::hasFunLock(mvc::$URL_METHOD, 600)) return true;
		$PjltjReport = new PjltjReport();
		$PjltjReport->initReportIndustry();
		$PjltjReport->initReportShop();
		LibFc::delFunLock(mvc::$URL_METHOD);
		echo 'ok';
	}
	
	//绑定车型ID
	public function autoBindModIds()
	{
		$cacheFile = mvc::$cfg['LOGS'].'cache_'.mvc::$URL_METHOD.'.php';
		$tjtable = 'at_tongji_all';
		$PjltjReport = new PjltjReport();

		$res = $PjltjReport->get('at_tongji_mod', ['max(atm_atime) as atm_atime'], '', true);
		$start = $res['data']['atm_atime'];
		if(empty($start)) $start = strtotime('20250101');

		$res = $PjltjReport->get($tjtable, ["max(tj_time) as tj_time"], '', true);
		$end = $res['data']['tj_time'];
		$hours = [];
		$current = strtotime(date("Y-m-d H:00:00", $start));
		while ($current <= $end) {
			$hours[] = date("Y-m-d H:00", $current);
			$current = strtotime("+1 hour", $current);
		}
		shuffle($hours);
		foreach($hours as $hour){
			$lockkey = mvc::$URL_METHOD.'_'.date('YmdH', strtotime($hour));
			if(!LibFc::hasFunLock($lockkey, 1800)) continue;
			$cacheArr = file_exists($cacheFile)? include($cacheFile): [];
			if(!empty($cacheArr[$hour])) continue;
			$runbtime = time();
			$PjltjReport->autoBindModIds($tjtable, strtotime($hour), strtotime($hour)+3600);
			$cacheArr = file_exists($cacheFile)? include($cacheFile): [];
			$msg = date('Y-m-d H:i').'=>'.$hour.' run：'.(time() - $runbtime)."s";
			$cacheArr[$hour] = $msg;
			LibFc::ArraySave($cacheArr, $cacheFile);
			LibFc::delFunLock($lockkey);
			echo $msg."\r\n";
		}
		echo 'ok';
	}
}