增加docke部署

This commit is contained in:
2026-04-10 16:44:13 +08:00
parent e2f8054794
commit cd4ddb606d
5076 changed files with 701092 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
<?php
/**
* Created by PhpStorm.
* User: abcdefg
* Date: 2017/8/30
* Time: 11:11
*/
die;
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 = dirname($_SERVER['SCRIPT_FILENAME']) . '/debug/ProcessMatchResult';
$filename = "{$pathname}/{$nowdate}.log";
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;
}
$limit = isset($_REQUEST['c']) ? $_REQUEST['c'] : 0;
if (empty($limit))
$limit = 1000;
$p = ConnectParameter::NewParameter(MASTER_HOSTNAME, MASTER_HOSTPORT, MASTER_DATABASE, MASTER_USERNAME, MASTER_PASSWORD, MASTER_PERSISTENT, MASTER_CHARSET);
/** @var IPDOHelper $reader */
$reader = new PDODelegator(DATABASE_TYPE);
/** @var IPDOHelper $writer */
$writer = new PDODelegator(DATABASE_TYPE);
$log = '';
if (!$reader->Connect($p)) {
$log = JsonObjectToJsonString($reader->GetErrors());
OutputDebugMessage($log);
exit($log);
}
if (!$writer->Connect($p)) {
$log = JsonObjectToJsonString($writer->GetErrors());
OutputDebugMessage($log);
exit($log);
}
$reader->request(
function ($row) {
/** @var IPDOHelper $db */
$db = $GLOBALS['writer'];
$player_id = $row->player_id;
$player_name = $row->player_name;
$used_card = (array)JsonStringToJsonObject($row->room_type);
$used_card = array_shift($used_card);
$game_info = JsonStringToJsonObject($row->game_info);
$player_list = isset($game_info->playerlist) ? $game_info->playerlist : null;
if (!$player_list)
return true;
$winner = null;
$integral = null;
foreach ($player_list as $key => $value) {
if (!$winner || intval($value[1]) > $integral) {
$winner = $value[0];
$integral = intval($value[1]);
}
}
if (strcasecmp($player_name, $winner) == 0) {
$command = /** @lang text */
'update ct_temp set used_card = ?, wins = 1, is_process = 1 where id = ?';
} else {
$command = /** @lang text */
'update ct_temp set used_card = ?, is_process = 1 where id = ?';
}
if ($db->execute($command, $used_card, $row->id)) {
echo 'the record id(', $row->id, ') processing success!<br>', PHP_EOL;
} else {
echo 'the record id(', $row->id, ') processing fail!<br>', PHP_EOL;
}
return true;
}, /** @lang text */
'select * from ct_temp where is_process = 0 limit ' . intval($limit)
);
if (!$reader->isdone())
echo $reader->geterrorinfo(), PHP_EOL;

View File

@@ -0,0 +1,631 @@
<?php
/**
* Created by PhpStorm.
* User: abcdefg
* Date: 2017/7/24
* Time: 14:37
*/
/**
* 同步与游戏的交互日志表
*/
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 = dirname($_SERVER['SCRIPT_FILENAME']) . '/debug/synchronize';
$filename = "{$pathname}/{$nowdate}.log";
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;
}
return false;
}
date_default_timezone_set('Asia/Shanghai');
/// 判断是否需要停服
if (TIMED_OFF_NEEDED) {
$Now = date('H:i:s');
if ($Now >= TIMED_OFF_BEGIN && $Now <= TIMED_OFF_END) {
$output = JsonObjectToJsonString([
'time' => date('Y-m-d H:i:s'),
'code' => ERRORCODE_SERVERTURNOFF,
'info' => sprintf(ERRORINFO_SERVERTURNOFF, TIMED_OFF_BEGIN, TIMED_OFF_END),
]);
OutputDebugMessage($output);
die($output);
}
}
/// 本地数据库
$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(env('EXT_DEV_DB_HOST', 'rm-bp16sbf0l9cavp7h9o.mysql.rds.aliyuncs.com'), (int)env('EXT_DEV_DB_PORT', 3306), env('EXT_DEV_DB_NAME', 'youle_games'), env('EXT_DEV_DB_USER', 'develop'), env('EXT_DEV_DB_PASSWORD', 'develop123!@#'), MASTER_PERSISTENT, MASTER_CHARSET);
else
$external_param = new ConnectParameter(env('EXT_GAME_DB_HOST', 'rm-bp1749tfxu2rpq670lo.mysql.rds.aliyuncs.com'), (int)env('EXT_GAME_DB_PORT', 3306), env('EXT_GAME_DB_NAME', 'game_db'), env('EXT_GAME_DB_USER', 'games'), env('EXT_GAME_DB_PASSWORD', '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()));
}
$processcount = isset($_REQUEST['processcount']) ? intval($_REQUEST['processcount']) : 100;
if (empty($processcount))
$processcount = 100;
$command = <<<EOL
select
idx, uspl_agentid, uspl_channelid, uspl_playerid, uspl_unionid, uspl_opt, uspl_optdata, uspl_opttime, uspl_state, uspl_statetime
from
ct_user_process_log
where
uspl_state = 0
limit
{$processcount}
EOL;
$logs = $external_db->request($command);
$succeeded = '';
$failed = '';
$succeededcount = 0;
$failedcount = 0;
$totalcount = count($logs);
if (0 == $totalcount) {
$output = JsonObjectToJsonString([
'time' => date('Y-m-d H:i:s'),
'total' => $totalcount,
'succeeded' => $succeededcount,
'failed' => $failedcount,
]);
die ($output);
}
$owner_db->begintransaction();
$external_db->begintransaction();
try {
if ($totalcount > 0) {
$error_code = 0;
$error_info = 'success';
foreach ($logs as $log) {
switch ($log->uspl_opt) {
case 1: /// 1:新增玩家
$line = __LINE__;
$data = JsonStringToJsonObject($log->uspl_optdata); /// {"openid":"openid100001", "unionid":"unionid100001", "nickname":"nickname100001","avatar":"","sex":1,"province":"江西","city":"南昌"}
if (!is_object($data)) {
$failed .= $log->idx . ',';
OutputDebugMessage('sync error: invalid json data' . PHP_EOL . 'data: ' . JsonObjectToJsonString($log));
$failedcount++;
break;
}
/// 插入新玩家信息
$command = /** @lang text */<<<EOL
replace into player(play_agentid, play_channelid, play_playerid, play_openid, play_unionid, play_nickname, play_avatar, play_sex, play_province, play_city, play_regtime)
values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
EOL;
$ret = $owner_db->execute($command,
$log->uspl_agentid, $log->uspl_channelid, $log->uspl_playerid, $data->openid, $data->unionid,
$data->nickname, $data->avatar, $data->sex, $data->province, $data->city, $log->uspl_opttime);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
/// 查询是否有匹配的代理信息unionid或openid匹配
if (!empty($data->unionid)) {
$command = /** @lang text */<<<EOL
select idx from sales_user where saus_agentid = ? and saus_channelid = ? and saus_unionid = ?
EOL;
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $data->unionid);
} elseif (!empty($data->openid)) {
$command = /** @lang text */<<<EOL
select idx from sales_user where saus_agentid = ? and saus_channelid = ? and saus_openid = ?
EOL;
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $data->openid);
}
if (!$owner_db->isdone()) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
elseif (!empty($ret)) {
$command = /** @lang text */'update sales_user set player_id = ?, saus_salesman = 1 where idx = ?';
$ret = $owner_db->execute($command, $log->uspl_playerid, $ret[0]->idx);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
} else {
/// 新增一个代理信息
$command = /** @lang text */'update ct_agent_list set max_sales_id = max_sales_id + floor(rand() * (12-6) + 6) where agent_id = ?';
$ret = $owner_db->execute($command, $log->uspl_agentid);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
$command = /** @lang text */'select max_sales_id as agen_maxsalesid, default_sales_power as agen_salespower from ct_agent_list where agent_id = ?';
$agent = $owner_db->request($command, $log->uspl_agentid);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
} elseif (empty($agent)) {
$ret = false;
$line = __LINE__;
$error_code = -1;
$error_info = "找不到指定的渠道编号{$log->uspl_agentid}";
goto end_case_1;
}
$sales_id = $agent[0]->agen_maxsalesid;
$sales_power = $agent[0]->agen_salespower;
$password = md5(rand(1000, 9999));
$command = /** @lang text */<<<EOL
insert into sales_user(
saus_agentid, saus_channelid, saus_salesid, saus_power, saus_openid,
saus_unionid, saus_nickname, saus_avatar, saus_sex, saus_province,
saus_city, saus_firsttime, saus_lasttime, saus_saletime, player_id,
saus_salesman, password
) values (
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
1, ?
)
EOL;
$ret = $owner_db->execute($command,
$log->uspl_agentid, $log->uspl_channelid, $sales_id, $sales_power, $data->openid,
$data->unionid, $data->nickname, $data->avatar, $data->sex, $data->province,
$data->city, $log->uspl_opttime, $log->uspl_opttime, $log->uspl_opttime, $log->uspl_playerid,
$password);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
$sales_user = [
'id' => 0,
'agentid' => $log->uspl_agentid,
'channelid' => $log->uspl_channelid,
'openid' => $data->openid,
'unionid' => $data->unionid,
'nickname' => $data->nickname,
'avatar' => $data->avatar,
'sex' => $data->sex,
'province' => $data->province,
'city' => $data->city,
'firsttime' => $log->uspl_opttime,
'lasttime' => $log->uspl_opttime,
'salesman' => 1,
'salesid' => $sales_id,
'level' => 0,
'parentid' => null,
'salestype' => 1,
'roomcard' => 0,
'bean' => 0,
'saletime' => $log->uspl_opttime,
'tel' => null,
'wechat' => null,
'invitecode' => null,
'power' => null,
'pushrate1' => 33,
'pushrate2' => 8,
'pushmoney1' => 0,
'pushmoney2' => 0,
];
$command = /** @lang text */<<<EOL
insert into ct_user_process_log(to_agent, to_channel, to_user, oper_type, oper_data, remark, oper_time, is_process)
values(?, ?, ?, 21, ?, ?, ?, 0)
EOL;
$ret = $owner_db->execute($command,
$log->uspl_agentid, $log->uspl_channelid, $sales_id,
JsonObjectToJsonString($sales_user), '目标用户为注册的代理信息。oper_data为用户unionid。', time());
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
}
end_case_1:
if ($ret && $owner_db->isdone()) {
$succeeded .= $log->idx . ',';
$succeededcount++;
$output = 'sync done: ' . JsonObjectToJsonString($log);
} else {
$failed .= $log->idx . ',';
$failedcount++;
$output = sprintf('sync error: %s(%d) on line %d.' . PHP_EOL . 'data: %s.', $error_info, $error_code, $line, JsonObjectToJsonString($log));
}
OutputDebugMessage($output);
break;
case 2: /// 2:绑定邀请码
$line = __LINE__;
/// 修改玩家的邀请码
$command = /** @lang text */<<<EOL
update
player
set
play_invitecode = ?
where
play_agentid = ? and play_channelid = ? and play_playerid = ?
EOL;
$ret = $owner_db->execute($command, $log->uspl_optdata, $log->uspl_agentid, $log->uspl_channelid, $log->uspl_playerid);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
/// 获取对应的代理记录
$command = /** @lang text */'select idx from sales_user where saus_agentid = ? and saus_channelid = ? and player_id = ?';
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $log->uspl_playerid);
if (!$owner_db->isdone()) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
} elseif (!empty($ret)) { /// 如果玩家有对应的代理账户则绑定对应的代理关系
$id = $ret[0]->idx;
} else { /// 如果玩家没有对应的代理账户则先生成对应的代理账户
/// 先查询对应的玩家详细信息
$command = /** @lang text */'select play_openid openid, play_unionid unionid, play_nickname nickname, play_avatar avatar, play_sex sex, play_province province, play_city city from player where play_agentid = ? and play_channelid = ? and play_playerid = ?';
$data = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $log->uspl_playerid);
if (!$owner_db->isdone()) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
} elseif (empty($data)) {
$data = $external_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $log->uspl_playerid);
if (!$external_db->isdone()) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
} elseif (empty($data)) {
$data = (object)[
'openid' => null,
'unionid' => $log->uspl_unionid,
'nickname' => null,
'avatar' => null,
'sex' => null,
'province' => null,
'city' => null,
];
} else {
$data = $data[0];
}
} else {
$data = $data[0];
}
/// 查询是否有匹配的代理信息unionid或openid匹配
if (!empty($data->unionid)) {
$command = /** @lang text */'select idx from sales_user where saus_agentid = ? and saus_channelid = ? and saus_unionid = ?';
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $data->unionid);
} elseif (!empty($data->openid)) {
$command = /** @lang text */'select idx from sales_user where saus_agentid = ? and saus_channelid = ? and saus_openid = ?';
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $data->openid);
}
if (!$owner_db->isdone()) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
elseif (!empty($ret)) {
$id = $ret[0]->idx;
} else {
/// 新增一个代理信息
$command = /** @lang text */
'update ct_agent_list set max_sales_id = max_sales_id + floor(rand() * (12-6) + 6) where agent_id = ?';
$ret = $owner_db->execute($command, $log->uspl_agentid);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
$command = /** @lang text */
'select max_sales_id as agen_maxsalesid, default_sales_power as agen_salespower from ct_agent_list where agent_id = ?';
$agent = $owner_db->request($command, $log->uspl_agentid);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
$sales_id = $agent[0]->agen_maxsalesid;
$sales_power = $agent[0]->agen_salespower;
$password = md5(rand(1000, 9999));
$command = /** @lang text */<<<EOL
insert into sales_user(
saus_agentid, saus_channelid, saus_salesid, saus_power, saus_openid,
saus_unionid, saus_nickname, saus_avatar, saus_sex, saus_province,
saus_city, saus_firsttime, saus_lasttime, saus_saletime, player_id,
saus_salesman, password
) values (
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
1, ?
)
EOL;
$ret = $owner_db->execute($command,
$log->uspl_agentid, $log->uspl_channelid, $sales_id, $sales_power, $data->openid,
$data->unionid, $data->nickname, $data->avatar, $data->sex, $data->province,
$data->city, $log->uspl_opttime, $log->uspl_opttime, $log->uspl_opttime, $log->uspl_playerid,
$password);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
$id = $owner_db->lastinsertid();
$sales_user = [
'id' => $id,
'agentid' => $log->uspl_agentid,
'channelid' => $log->uspl_channelid,
'openid' => $data->openid,
'unionid' => $data->unionid,
'nickname' => $data->nickname,
'avatar' => $data->avatar,
'sex' => $data->sex,
'province' => $data->province,
'city' => $data->city,
'firsttime' => $log->uspl_opttime,
'lasttime' => $log->uspl_opttime,
'salesman' => 1,
'salesid' => $sales_id,
'level' => 0,
'parentid' => null,
'salestype' => 1,
'roomcard' => 0,
'bean' => 0,
'saletime' => $log->uspl_opttime,
'tel' => null,
'wechat' => null,
'invitecode' => null,
'power' => null,
'pushrate1' => 33,
'pushrate2' => 8,
'pushmoney1' => 0,
'pushmoney2' => 0,
];
/// 记录新增代理的日志
$command = /** @lang text */<<<EOL
insert into ct_user_process_log(to_agent, to_channel, to_user, oper_type, oper_data, remark, oper_time, is_process)
values(?, ?, ?, 21, ?, ?, ?, 0)
EOL;
$ret = $owner_db->execute($command,
$log->uspl_agentid, $log->uspl_channelid, $sales_id,
JsonObjectToJsonString($sales_user), '目标用户为注册的代理信息。oper_data为用户unionid。', time());
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
}
}
/// 读取渠道是否同步邀请码的配置
$command = /** @lang text */'select sync_invitecode from ct_channel_list where agent_id = ? and channel_id = ?';
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid);
$sync_invitecode = empty($ret) ? 0 : intval($ret[0]->sync_invitecode);
if (0 != $sync_invitecode) {
/// 修改对应代理记录的邀请码
$command = /** @lang text */<<<EOL
update
sales_user
set
saus_parentid = ?,
saus_invitecode = ?,
player_id = ?,
saus_salesman = 1
where
idx = ?
EOL;
$ret = $owner_db->execute($command, $log->uspl_optdata, $log->uspl_optdata, $log->uspl_playerid, $id);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
}
end_case_2:
if ($ret && $owner_db->isdone()) {
$succeeded .= $log->idx . ',';
$succeededcount++;
$output = 'sync done: ' . JsonObjectToJsonString($log);
} else {
$failed .= $log->idx . ',';
$failedcount++;
$output = sprintf('sync error: %s(%d) on line %d.' . PHP_EOL . 'data: %s.', $error_info, $error_code, $line, JsonObjectToJsonString($log));
}
OutputDebugMessage($output);
break;
default:
break;
}
}
/// 成功处理的记录把状态修改为1
if (!empty($succeeded)) {
$succeeded = substr($succeeded, 0, strlen($succeeded) - 1);
$command = /** @lang text */<<<EOL
update
ct_user_process_log
set
uspl_state = 1
where
idx in ({$succeeded})
EOL;
$external_db->execute($command);
}
/// 处理失败的记录把状态修改为-1
if (!empty($failed)) {
$failed = substr($failed, 0, strlen($failed) - 1);
$command = /** @lang text */<<<EOL
update
ct_user_process_log
set
uspl_state = -1
where
idx in ({$failed})
EOL;
$external_db->execute($command);
}
}
if (!$owner_db->commit())
throw new Exception($owner_db->GetErrorInfo(), $owner_db->GetErrorCode());
if (!$external_db->commit())
throw new Exception($external_db->GetErrorInfo(), $external_db->GetErrorCode());
$output = JsonObjectToJsonString([
'time' => date('Y-m-d H:i:s'),
'total' => $totalcount,
'succeeded' => $succeededcount,
'failed' => $failedcount,
]);
if (($succeededcount + $failedcount) != $totalcount)
OutputDebugMessage($output);
} catch (Exception $Exception) {
$owner_db->rollback();
$external_db->rollback();
$output = JsonObjectToJsonString([
'time' => date('Y-m-d H:i:s'),
'code' => $Exception->getCode(),
'info' => $Exception->getMessage(),
]);
OutputDebugMessage($output);
}
die ($output);
/*
insert into ct_user_process_log(to_agent, to_channel, to_user, oper_type, oper_data, remark, oper_time, is_process)
select
saus_agentid,
saus_channelid,
saus_salesid,
21,
concat('{',
'"id":"', ifnull(idx, 0),
'","agentid":"', ifnull(saus_agentid, ''),
'","channelid":"', ifnull(saus_channelid, ''),
'","openid":"', ifnull(saus_openid, ''),
'","unionid":"', ifnull(saus_unionid, ''),
'","nickname":"', ifnull(saus_nickname, ''),
'","avatar":"', ifnull(saus_avatar, ''),
'","sex":"', ifnull(saus_sex, ''),
'","province":"', ifnull(saus_province, ''),
'","city":"', ifnull(saus_city, ''),
'","firsttime":"', ifnull(saus_firsttime, ''),
'","lasttime":"', ifnull(saus_lasttime, ''),
'","salesman":"', ifnull(saus_salesman, 0),
'","salesid":"', ifnull(saus_salesid, ''),
'","level":"', ifnull(saus_level, ''),
'","parentid":"', ifnull(saus_parentid, ''),
'","salestype":"', ifnull(saus_salestype, ''),
'","roomcard":"', ifnull(saus_roomcard, 0),
'","bean":"', ifnull(saus_bean, 0),
'","saletime":"', ifnull(saus_saletime, ''),
'","tel":"', ifnull(saus_tel, ''),
'","wechat":"', ifnull(saus_wechat, ''),
'","invitecode":"', ifnull(saus_invitecode, 0),
'","power":"', ifnull(saus_power, ''),
'","pushrate1":"', ifnull(saus_pushrate1, 0),
'","pushrate2":"', ifnull(saus_pushrate2, 0),
'","pushmoney1":"', ifnull(saus_pushmoney1, 0),
'","pushmoney2":"', ifnull(saus_pushmoney2, 0),
'"}'
),
'目标用户为注册的代理信息。oper_data为用户unionid。',
unix_timestamp(),
0
from
sales_user
where
saus_agentid = 'veRa0qrBf0df2K1G4de2tgfmVxB2jxpv' and saus_channelid = 'FtJf073aa0d6rI1xD8J1Y42fINTm0ziK' and saus_salesid = 240973
*/

View File

@@ -0,0 +1,426 @@
<?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 = dirname($_SERVER['SCRIPT_FILENAME']) . '/debug/SynchronizeReportData';
$filename = "{$pathname}/{$nowdate}.log";
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(env('EXT_DEV_DB_HOST', 'rm-bp16sbf0l9cavp7h9o.mysql.rds.aliyuncs.com'), (int)env('EXT_DEV_DB_PORT', 3306), env('EXT_DEV_DB_NAME', 'youle_games'), env('EXT_DEV_DB_USER', 'develop'), env('EXT_DEV_DB_PASSWORD', 'develop123!@#'), MASTER_PERSISTENT, MASTER_CHARSET);
else
$external_param = new ConnectParameter(env('EXT_GAME_DB_HOST', 'rm-bp1749tfxu2rpq670lo.mysql.rds.aliyuncs.com'), (int)env('EXT_GAME_DB_PORT', 3306), env('EXT_GAME_DB_NAME', 'game_db'), env('EXT_GAME_DB_USER', 'games'), env('EXT_GAME_DB_PASSWORD', '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);

View File

@@ -0,0 +1,158 @@
<?php
/**
* Created by PhpStorm.
* User: abcdefg
* Date: 2017/8/7
* Time: 16:33
*/
die;
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 = dirname($_SERVER['SCRIPT_FILENAME']) . '/debug/TransferAgent';
$filename = "{$pathname}/{$nowdate}.log";
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;
}
$agent = isset($_REQUEST['a']) ? $_REQUEST['a'] : '';
$source = isset($_REQUEST['s']) ? $_REQUEST['s'] : '';
$target = isset($_REQUEST['t']) ? $_REQUEST['t'] : '';
if (empty($agent)) {
$output = 'please enter agent id!';
OutputDebugMessage($output);
die($output);
} elseif (empty($source)) {
$output = 'please enter source sales id!';
OutputDebugMessage($output);
die($output);
} elseif (empty($target)) {
$output = 'please enter target sales id!';
OutputDebugMessage($output);
die($output);
} else {
$connect_parameter = new ConnectParameter(MASTER_HOSTNAME, MASTER_HOSTPORT, MASTER_DATABASE, MASTER_USERNAME, MASTER_PASSWORD, MASTER_PERSISTENT, MASTER_CHARSET);
/** @var IPDOHelper $db */
$db = new PDODelegator(DATABASE_TYPE);
if (!$db->connect($connect_parameter)) {
$output = JsonObjectToJsonString($db->geterrors());
OutputDebugMessage($output);
die($output);
}
if (!$db->begintransaction()) {
$output = JsonObjectToJsonString($db->geterrors());
OutputDebugMessage($output);
die($output);
}
try {
/// 转移房卡
$room_card = 0;
/// 查询源代理信息
$sales_user = $db->request(/** @lang text */
'select saus_roomcard from sales_user where saus_agentid = ? and saus_salesid = ?', $agent, $source);
if (!$db->isdone()) {
throw new Exception($db->geterrorinfo(), $db->geterrorcode());
} elseif (empty($sales_user)) {
throw new Exception("unable to find sales id: {$source}!");
} else {
$room_card = intval($sales_user[0]->saus_roomcard);
}
/// 查询新代理信息
$sales_user = $db->request(/** @lang text */
'select saus_roomcard from sales_user where saus_agentid = ? and saus_salesid = ?', $agent, $target);
if (!$db->isdone()) {
throw new Exception($db->geterrorinfo(), $db->geterrorcode());
} elseif (empty($sales_user)) {
throw new Exception("unable to find sales id: {$target}!");
}
$command = <<<EOL
update
sales_user a,
(
select
saus_agentid, saus_salesid, saus_roomcard - {$room_card} saus_roomcard
from
sales_user
where
saus_agentid = ? and saus_salesid = ?
union all
select
saus_agentid, saus_salesid, saus_roomcard + {$room_card} saus_roomcard
from
sales_user
where
saus_agentid = ? and saus_salesid = ?
) b
set
a.saus_roomcard = b.saus_roomcard
where
a.saus_agentid = b.saus_agentid and a.saus_salesid = b.saus_salesid
EOL;
/// 更新新代理的房卡
$ret = $db->execute($command, $agent, $source, $agent, $target);
if (!$ret)
throw new Exception($db->geterrorinfo(), $db->geterrorcode());
/// 更新代理的邀请码
$ret = $db->execute(/** @lang text */
'update sales_user set saus_invitecode = ? where saus_agentid = ? and saus_invitecode = ?', $target, $agent, $source);
if (!$ret)
throw new Exception($db->geterrorinfo(), $db->geterrorcode());
/// 更新代理的上级代理
$ret = $db->execute(/** @lang text */
'update sales_user set saus_parentid = ? where saus_agentid = ? and saus_parentid = ?', $target, $agent, $source);
if (!$ret)
throw new Exception($db->geterrorinfo(), $db->geterrorcode());
/// 更新玩家的邀请码
$ret = $db->execute(/** @lang text */
'update player set play_invitecode = ? where play_agentid = ? and play_invitecode = ?', $target, $agent, $source);
if (!$ret)
throw new Exception($db->geterrorinfo(), $db->geterrorcode());
$db->commit();
} catch (Exception $Exception) {
$db->rollback();
$output = JsonObjectToJsonString(array('code' => $Exception->getCode(), 'info' => $Exception->getMessage(),));
OutputDebugMessage($output);
die($output);
}
}

View File

@@ -0,0 +1,112 @@
<?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 = dirname($_SERVER['SCRIPT_FILENAME']) . '/debug/autotask';
$filename = "{$pathname}/{$nowdate}.log";
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);

View File

@@ -0,0 +1,88 @@
<?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 = dirname($_SERVER['SCRIPT_FILENAME']) . '/debug/autotask';
$filename = "{$pathname}/{$nowdate}.log";
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');
/**
* @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->BeginTransaction(); /// 启动事务
$sync_date = date('Y-m-d', strtotime(isset($_REQUEST['sync_date']) ? $_REQUEST['sync_date'] : '-1 days')); // 前一天
try {
// 1、从数据库获取自动执行脚本
if ($commands = $db->request('select `sql`, `params`, `table` from ct_report_ext_sql where status = ?;', 1)) {
$commands = json_decode(json_encode($commands), true);
} else {
throw new Exception('从数据库获取自动执行脚本失败', -1);
}
// 2、循环执行脚本
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()));
// 插入前一天的数据
if (!$db->execute($value['sql'], $sync_date))
throw new Exception($db->geterrorinfo() . '(' . __LINE__ . ')', intval($db->geterrorcode()));
}
$db->Commit(); /// 提交事务
$log = 'success!';
} catch (Exception $Exception) /// 异常处理
{
$db->Rollback(); /// 事务回滚
$log = JsonObjectToJsonString(array('code' => $Exception->getCode(), 'info' => $Exception->getMessage(),));
OutputDebugMessage($log);
}
die($log);