{$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 */<<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 */<<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 */<<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 */<<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 */<<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 */<<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 */<<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 */<<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 */<<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);