{$message}");
fclose($file);
return true;
} else
return false;
}
date_default_timezone_set('Asia/Shanghai');
$log = 'start';
/**
* @var IPDOHelper $db
*/
$db = new PDODelegator(DATABASE_TYPE);
if (!$db->Connect(MASTER_HOSTNAME, MASTER_HOSTPORT, MASTER_DATABASE, MASTER_USERNAME, MASTER_PASSWORD, MASTER_CHARSET)) {
$log = JsonObjectToJsonString($db->GetErrors());
OutputDebugMessage($log);
die($log);
}
$db->FetchStyle = PDO::FETCH_ASSOC;
/** @var ISQLCommand $cmd */
///$cmd = new CommandDelegator($db);
/// saved max execute time count.
$MaxExecuteTime = ini_get('max_execution_time');
/// disable timeout limit.
set_time_limit(0);
$sync_date = date('Y-m-d', strtotime(isset($_REQUEST['sync_date']) ? $_REQUEST['sync_date'] : '-1 days')); // 前一天
$tabel_tag = date('Ym', strtotime(isset($_REQUEST['sync_date']) ? $_REQUEST['sync_date'] : '-1 days')); // 前一天
//$db->BeginTransaction(); /// 启动事务
try {
// 1、从数据库获取自动执行脚本
if (empty($commands = $db->request(/** @lang text */'select `sql`, `params`, `table` from ct_report_ext_sql where status = 1')))
throw new Exception('从数据库获取自动执行脚本失败', -1);
// 删除指定天数原有的数据
foreach ($commands as $key => $value) {
if (!$db->execute(/** @lang text */"delete from `{$value['table']}` where date_format(`time`, '%Y-%m-%d') = ?", $sync_date)) {
throw new Exception($db->geterrorinfo() . '(' . __LINE__ . ')', intval($db->geterrorcode()));
}
}
/// 插入指定天数的数据
$index = 0;
echo PHP_EOL;
echo date('Y-m-d H:i:s'), ': start...', '
', PHP_EOL;
foreach ($commands as $key => $value) {
$sql = str_replace('[%ctGrade%]', 'ct_grade_' . $tabel_tag, $value['sql']);
$sql = str_replace('[%ctUserCommission%]', 'ct_user_commission_' . $tabel_tag, $sql);
$sql = str_replace('[%date%]', $sync_date, $sql);
$cmd = str_replace('?', '\'' . $sync_date . '\'', $sql);
OutputDebugMessage($cmd);
if ($db->execute($sql, $sync_date)) {
echo date('Y-m-d H:i:s'), ': the command with the serial number ', ++$index, ' has been successfully executed!', '
', PHP_EOL;
ob_flush();
flush();
} else {
throw new Exception($db->geterrorinfo() . '(' . __LINE__ . ')', intval($db->geterrorcode()));
}
}
//$db->Commit(); /// 提交事务
$log = 'success!';
/// restore max execute time count.
set_time_limit($MaxExecuteTime);
} catch (Exception $Exception) { /// 异常处理
//$db->Rollback(); /// 事务回滚
$log = JsonObjectToJsonString(array('code' => $Exception->getCode(), 'info' => $Exception->getMessage(),));
OutputDebugMessage($log);
/// restore max execute time count.
set_time_limit($MaxExecuteTime);
}
die($log);