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

427 lines
15 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 同步报表数据
*/
require_once dirname(__DIR__) . '/common/config.inc.php';
require_once dirname(__DIR__) . '/common/DatabaseHelper.php';
//http://localhost/game_proxy/trunk/api/ext/SynchronizeReportData.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/SynchronizeReportData', 'gbk');
$filename = "{$pathname}/{$nowdate}.log";
$message = Characet($message, 'gbk');
if (!is_dir($pathname))
mkdir($pathname, 0777, true);
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');
/// 设置超时时间
set_time_limit(0);
/// 本地数据库
$owner_param = new ConnectParameter(MASTER_HOSTNAME, MASTER_HOSTPORT, MASTER_DATABASE, MASTER_USERNAME, MASTER_PASSWORD, MASTER_PERSISTENT, MASTER_CHARSET);
/// 游戏数据库
if (DEBUG_MODE)
$external_param = new ConnectParameter('rm-bp16sbf0l9cavp7h9o.mysql.rds.aliyuncs.com', 3306, 'youle_games', 'develop', 'develop123!@#', MASTER_PERSISTENT, MASTER_CHARSET);
else
$external_param = new ConnectParameter('rm-bp1749tfxu2rpq670lo.mysql.rds.aliyuncs.com', 3306, 'game_db', 'games', 'Games0791!!', MASTER_PERSISTENT, MASTER_CHARSET);
//$external_param = new ConnectParameter('rm-bp1x3i28se22s9z75o.mysql.rds.aliyuncs.com', 3306, 'game_db', 'games', 'Games2017@)!&', MASTER_PERSISTENT, MASTER_CHARSET);
/** @var IPDOHelper $owner_db */
$owner_db = new PDODelegator(DATABASE_TYPE);
/** @var IPDOHelper $external_db */
$external_db = new PDODelegator(DATABASE_TYPE);
if (!$owner_db->connect($owner_param)) {
OutputDebugMessage($owner_db->geterrors());
die (JsonObjectToJsonString($owner_db->geterrors()));
}
if (!$external_db->connect($external_param)) {
OutputDebugMessage($external_db->geterrors());
die (JsonObjectToJsonString($external_db->geterrors()));
}
/// 同步时间
$sync_date = date('Y-m-d', strtotime(isset($_REQUEST['sync_date']) ? $_REQUEST['sync_date'] : '-1 days'));
$owner_db->begintransaction();
$external_db->begintransaction();
try {
/** @var ISQLCommand $cmd */
$cmd = new CommandDelegator($owner_db);
/// 报表时间
$report_date = $cmd->getidentifiers($sync_date, true);
/// todo: 先同步report_agent_day和report_game_day两个表
/// 同步report_agent_day
$command = /** @lang text */<<<EOL
select
read_agentid, read_day, read_newplayer, read_newsalesman, read_playeraward, read_salesmoney
from
report_agent_day
where
read_day = ?
EOL;
$data = $external_db->request($command, $sync_date);
if (!$external_db->isdone())
throw new Exception($external_db->geterrorinfo() . '(' . __LINE__ . ')', intval($external_db->geterrorcode()));
$command = '';
foreach ($data as $item) {
$command .= sprintf('select %s read_agentid, %s read_day, %s read_newplayer, %s read_newsalesman, %s read_playeraward, %s read_salesmoney' . PHP_EOL . 'union all' . PHP_EOL,
$cmd->getidentifiers($item->read_agentid, true),
$cmd->getidentifiers($item->read_day, true),
$item->read_newplayer,
$item->read_newsalesman,
$item->read_playeraward,
$item->read_salesmoney);
}
$command = substr($command, 0, strlen($command) - strlen(PHP_EOL . 'union all' . PHP_EOL));
$command = /** @lang text */<<<EOL
replace into
report_agent_day(read_agentid, read_day, read_newplayer, read_newsalesman, read_playeraward, read_salesmoney)
select
read_agentid, read_day, read_newplayer, read_newsalesman, read_playeraward, read_salesmoney
from
(
{$command}
) t
EOL;
/// 删除前一天原有的数据
if (!$cmd->execute(/** @lang text */
'delete from report_agent_day where read_day = ?', $sync_date)
)
throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
/// 插入前一天的数据
if (!$cmd->execute($command))
throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
echo PHP_EOL, 'the table report_agent_day was synchronized!', PHP_EOL;
ob_flush();
flush();
/// ================================================================================================================
/// 同步report_game_day
$command = /** @lang text */<<<EOL
select
regd_agentid, regd_gameid, regd_day, regd_newplayer, regd_useroomcard, regd_asetcount, regd_maxplayer, regd_maxplayertime, regd_maxroom, regd_maxroomtime
from
report_game_day
where
regd_day = ?
EOL;
$data = $external_db->request($command, $sync_date);
if (!$external_db->isdone())
throw new Exception($external_db->geterrorinfo() . '(' . __LINE__ . ')', intval($external_db->geterrorcode()));
$command = '';
foreach ($data as $item) {
$command .= sprintf('select %s regd_agentid, %s regd_gameid, %s regd_day, %s regd_newplayer, %s regd_useroomcard, %s regd_asetcount, %s regd_maxplayer, %s regd_maxplayertime, %s regd_maxroom, %s regd_maxroomtime' . PHP_EOL . 'union all' . PHP_EOL,
$cmd->getidentifiers($item->regd_agentid, true),
$cmd->getidentifiers($item->regd_gameid, true),
$cmd->getidentifiers($item->regd_day, true),
$item->regd_newplayer,
$item->regd_useroomcard,
$item->regd_asetcount,
$item->regd_maxplayer,
empty($item->regd_maxplayertime) ? 'null' : $cmd->getidentifiers($item->regd_maxplayertime, true),
$item->regd_maxroom,
empty($item->regd_maxroomtime) ? 'null' : $cmd->getidentifiers($item->regd_maxroomtime, true));
}
$command = substr($command, 0, strlen($command) - strlen(PHP_EOL . 'union all' . PHP_EOL));
$command = /** @lang text */<<<EOL
replace into
report_game_day(regd_agentid, regd_gameid, regd_day, regd_newplayer, regd_useroomcard, regd_asetcount, regd_maxplayer, regd_maxplayertime, regd_maxroom, regd_maxroomtime)
select
regd_agentid, regd_gameid, regd_day, regd_newplayer, regd_useroomcard, regd_asetcount, regd_maxplayer, regd_maxplayertime, regd_maxroom, regd_maxroomtime
from
(
{$command}
) t
EOL;
/// 删除前一天原有的数据
if (!$cmd->execute(/** @lang text */
'delete from report_game_day where regd_day = ?', $sync_date)
)
throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
/// 插入前一天的数据
if (!$cmd->execute($command))
throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
echo 'the table report_game_day was synchronized!', PHP_EOL;
ob_flush();
flush();
/// ================================================================================================================
/// 生成前先删除原有的数据
if (!$cmd->execute(/** @lang text */'delete from ct_report_info where date_format(report_date, \'%Y-%m-%d\') = ?', $sync_date) )
throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
/// ================================================================================================================
//
// /// 更新报表明细数据(总计、不分组的数据)
// $base_view = /** @lang text */<<<EOL
// select
// a.agent_id, a.channel_id, a.report_id, a.report_name, a.report_caption, a.command_line, a.is_group, a.is_autorun
// from
// (
// select
// b.agent_id agent_id, b.channel_id channel_id, a.report_id, a.report_name, a.report_caption, a.command_line, a.is_group, a.is_autorun
// from
// ct_report_list a, ct_channel_list b
// where
// ifnull(a.agent_id, '') = '' and ifnull(a.channel_id, '') = '' and ifnull(a.is_enabled, 0) != 0
// ) a
// left join
// (
// select
// agent_id, channel_id, report_id, report_name, report_caption, command_line, is_group, is_autorun
// from
// ct_report_list
// where
// ifnull(agent_id, '') != '' and ifnull(channel_id, '') != '' and ifnull(is_enabled, 0) != 0
// ) b
// on a.agent_id = b.agent_id and a.channel_id = b.channel_id and a.report_name = b.report_name
// where
// b.report_id is null
//
// union all
//
// select
// agent_id, channel_id, report_id, report_name, report_caption, command_line, is_group, is_autorun
// from
// ct_report_list
// where
// ifnull(agent_id, '') != '' and ifnull(channel_id, '') != '' and ifnull(is_enabled, 0) != 0
//EOL;
//
// /// 读出列表
// $command = /** @lang text */<<<EOL
//select
// a.agent_id, a.channel_id, a.report_id, a.report_name, a.report_caption, a.command_line
//from
//(
//{$base_view}
//) a
//where
// ifnull(a.is_group, 0) = 0 and ifnull(a.is_autorun, 0) != 0
//EOL;
//
// $report_data = '';
// $report_date = $cmd->getidentifiers($sync_date, true);
//
// $data = $cmd->request($command);
// if (!$owner_db->isdone())
// throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
//
// if (!empty($data)) {
// foreach ($data as $item) {
// $agent_id = $cmd->getidentifiers($item->agent_id, true);
// $channel_id = $cmd->getidentifiers($item->channel_id, true);
// $report_id = $item->report_id;
//
// if (empty($command_line = $item->command_line)) {
// $report_data .= " when a.agent_id = {$agent_id} and a.channel_id = {$channel_id} and a.report_id = {$report_id} then 0" . PHP_EOL;
// } else {
// $command_line = str_ireplace('%agent_id%', $agent_id, $command_line);
// $command_line = str_ireplace('%channel_id%', $channel_id, $command_line);
// $command_line = str_ireplace('%report_date%', $report_date, $command_line);
//
// $report_data .= " when a.agent_id = {$agent_id} and a.channel_id = {$channel_id} and a.report_id = {$report_id} then ({$command_line})" . PHP_EOL;
// }
// }
// $report_data = ' case' . PHP_EOL . $report_data . ' else 0' . PHP_EOL . ' end report_data';
//
// /// 生成数据的sql语句
// $command = /** @lang text */<<<EOL
//insert into ct_report_info
// (agent_id, channel_id, report_id, report_date, report_data)
//select
// a.agent_id,
// a.channel_id,
// a.report_id,
// #a.report_name,
// #a.report_caption,
// a.report_date,
//{$report_data}
//from
//(
// select
// a.agent_id,
// a.channel_id,
// a.report_id,
// a.report_name,
// a.report_caption,
// {$report_date} report_date
// #date_format(now(), '%Y-%m-%d') report_date
// from
// (
//{$base_view}
// ) a
// where
// ifnull(a.is_group, 0) = 0 and ifnull(a.is_autorun, 0) != 0
//) a
//#where
//# a.agent_id = 'veRa0qrBf0df2K1G4de2tgfmVxB2jxpv' and a.channel_id = 'FtJf073aa0d6rI1xD8J1Y42fINTm0ziK'
//
//EOL;
//
// /// 生成前一天的报表数据
// if (!$cmd->execute($command))
// throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
//
// echo 'the report with no group was created!', PHP_EOL;
// ob_flush();
// flush();
// }
//
// /// ================================================================================================================
//
// /// 更新报表明细数据按sales_id分组的数据
//
// /// 获取列表
// $command = /** @lang text */<<<EOL
//select
// agent_id, channel_id, report_id, report_name, report_caption, command_line
//from
// (
//{$base_view}
// ) a
//where
// ifnull(a.is_group, 0) != 0 and ifnull(a.is_autorun, 0) != 0
//EOL;
//
// $data = $cmd->request($command);
// if (!$owner_db->isdone())
// throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
//
// if (!empty($data)) {
// $command = '';
// foreach ($data as $item) {
// if (empty($command_line = $item->command_line))
// continue;
//
// $agent_id = $cmd->getidentifiers($item->agent_id, true);
// $channel_id = $cmd->getidentifiers($item->channel_id, true);
// $report_id = $item->report_id;
//
// $command_line = str_ireplace('%report_id%', $report_id, $command_line);
// $command_line = str_ireplace('%agent_id%', $agent_id, $command_line);
// $command_line = str_ireplace('%channel_id%', $channel_id, $command_line);
// //$command_line = str_ireplace('%sales_id%', '__tmp__.sales_id', $command_line);
// $command_line = str_ireplace('%report_date%', $report_date, $command_line);
//
// $command .= PHP_EOL . $command_line . PHP_EOL . 'union all';
// }
//
// $command = /** @lang text */PHP_EOL . 'insert into ct_report_info(agent_id, channel_id, sales_id, report_id, report_date, report_data)' . PHP_EOL .
// mb_substr($command, 0, mb_strlen($command, USEDCHARSET) - mb_strlen('union all', USEDCHARSET), USEDCHARSET);
// if (!$owner_db->execute($command))
// throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
// echo 'the report with group of sales was created!', PHP_EOL;
// ob_flush();
// flush();
// }
/* 2018-08-10由于原语句过慢修改执行方式 */
/// 提取自动执行的报表脚本
$sql = /** @lang text */<<<EOL
select
a.report_id, a.report_name, a.report_caption, a.command_line, a.is_group, a.is_autorun
from
ct_report_list a
where
ifnull(a.is_autorun, 0) != 0 and ifnull(a.is_enabled, 0) != 0
EOL;
$list = $cmd->request($sql);
if (!$owner_db->isdone())
throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
if (!empty($list)) {
$command = '';
foreach ($list as $item) {
if (empty($command_line = $item->command_line))
continue;
$report_id = $item->report_id;
$command_line = str_ireplace('%report_id%', $report_id, $command_line);
$command_line = str_ireplace('%report_date%', $report_date, $command_line);
$command .= $command_line . PHP_EOL . PHP_EOL . 'union all' . PHP_EOL . PHP_EOL;
}
$command = PHP_EOL . PHP_EOL . mb_substr($command, 0, mb_strlen($command, USEDCHARSET) - mb_strlen('union all' . PHP_EOL . PHP_EOL, USEDCHARSET), USEDCHARSET);
$command = /** @lang text */<<<EOL
insert into ct_report_info(
agent_id, channel_id, sales_id, report_id, report_date, report_data
)
select
agent_id, channel_id, sales_id, report_id, report_date, report_data
from
(
{$command}
) t
EOL;
if (!$owner_db->execute($command))
throw new Exception($owner_db->geterrorinfo() . '(' . __LINE__ . ')', intval($owner_db->geterrorcode()));
echo 'the report was created!', PHP_EOL;
ob_flush();
flush();
}
/// ================================================================================================================
$owner_db->commit();
$external_db->commit();
$output = "the data of {$sync_date} was updated!";
} catch (Exception $Exception) {
$owner_db->rollback();
$external_db->rollback();
$output = JsonObjectToJsonString(array('code' => $Exception->GetCode(), 'info' => $Exception->GetMessage(),));
OutputDebugMessage($output);
}
die ($output);