Files
youlegames/codes/agent/game/dlweb/api/ext/autotask.php
2026-03-15 01:27:05 +08:00

113 lines
3.6 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: bahamut
* Date: 2018/1/26
* Time: 14:15
*/
/**
* 每日自动任务
*/
require_once dirname(__DIR__) . '/common/config.inc.php';
require_once dirname(__DIR__) . '/common/DatabaseHelper.php';
/**
* @note 打印日志
* @param mixed $message
* @return bool
*/
function OutputDebugMessage($message)
{
if (is_object($message) || is_array($message))
$message = JsonObjectToJsonString($message);
$nowdate = date('Y-m-d');
$nowtime = date('H:i:s');
$pathname = Characet(dirname($_SERVER['SCRIPT_FILENAME']) . '/debug/autotask', 'gbk');
$filename = "{$pathname}/{$nowdate}.log";
$message = Characet($message, 'gbk');
if (!is_dir($pathname))
mkdir($pathname, 0777, true); // 修复不能同时创建俩个不存在的文件夹的bug
if ($file = fopen($filename, 'a+')) {
if (mb_strstr($message, PHP_EOL, false, USEDCHARSET) != PHP_EOL)
$message .= PHP_EOL;
fwrite($file, "{$nowtime} ====> {$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...', '<br>', 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!', '<br>', 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);