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

1706 lines
58 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
/**
* Created by PhpStorm.
* User: bahamut
* Date: 2018/1/15
* Time: 14:03
*/
require_once dirname(dirname(__DIR__)) . '/common/ErrorType.php';
require_once dirname(dirname(__DIR__)) . '/common/common.inc.php';
require_once dirname(dirname(__DIR__)) . '/common/BaseMethodHelper.php';
/*******************************************************************************
* 当使用json字符串标记参数时支援的属性为 *
* name: 参数名 *
* type: 参数类型 *
* mask: 参数格式 *
* value: 参数值(最终确定的值) *
* default: 参数默认值 *
* options: 参数可选值enum类型有效 *
* script: 参数要执行的脚本sql脚本类型有效 *
* returnfield: 参数返回字段名sql脚本类型有效 *
*******************************************************************************
* 参数类型可选: *
* unknown: 未知类型 *
* string: 字符串类型 *
* number: 数值类型 *
* enum: 枚举类型options有效并忽略mask属性 *
* date: 日期类型mask固定为yyyy-mm-dd *
* time: 时间类型mask固定为hh:nn:ss *
* datetime: 日期时间类型mask固定为yyyy-mm-dd hh:nn:ss *
* sql: sql脚本类型script和returnfield有效并忽略mask属性 *
*******************************************************************************/
class ReportParameter
{
const PARAMETERTYPE_UNKNOWN = 'unknown'; /// 未知类型
const PARAMETERTYPE_STRING = 'string'; /// 字符串类型
const PARAMETERTYPE_NUMBER = 'number'; /// 数值类型
const PARAMETERTYPE_ENUM = 'enum'; /// 枚举类型options有效并忽略mask属性
const PARAMETERTYPE_DATE = 'date'; /// 日期类型mask固定为yyyy-mm-dd
const PARAMETERTYPE_TIME = 'time'; /// 时间类型mask固定为hh:nn:ss
const PARAMETERTYPE_DATETIME = 'datetime'; /// 日期时间类型mask固定为yyyy-mm-dd hh:nn:ss
const PARAMETERTYPE_SQL = 'sql'; /// sql脚本类型script和returnfield有效并忽略mask属性
/**
* @note 参数名
* @var string
*/
public $name;
/**
* @note 参数类型
* @var string
*/
public $type;
/**
* @note 参数格式
* @var string
*/
public $mask;
/**
* @note 参数值
* @var mixed
*/
public $value;
/**
* @note 参数默认值
* @var mixed
*/
public $default;
/**
* @note 参数可选值enum类型有效
* @var array|mixed
*/
public $options;
/**
* @note 参数要执行的脚本sql脚本类型有效
* @var string
*/
public $script;
/**
* @note 参数返回字段名sql脚本类型有效
* @var string
*/
public $returnfield;
public function __construct($String)
{
$this->name = null;
$this->type = null;
$this->mask = null;
$this->value = null;
$this->default = null;
$this->options = null;
$this->script = null;
$this->returnfield = null;
$this->FromString($String);
}
public function FromArray($Array)
{
if (is_null($Array))
return false;
if (is_object($Array))
$Array = (array)$Array;
if (!is_array($Array))
{
$this->Name = strval($Array);
return true;
}
$Reflect = new ReflectionClass($this);
foreach($Array as $Key => $Value)
{
//if (!$Reflect->HasProperty($Key))
// continue;
if ($Property = $Reflect->GetProperty($Key))
{
//if ($Property->IsPublic())
$Property->SetValue($this, $Value);
}
}
return true;
}
public function FromString($String)
{
if (!$this->FromArray(JsonStringToJsonObject( strtr($String, array("\r" => ' ', "\n" => ' ', )))))
$this->name = empty($String) ? '' : $String;
return true;
}
}
class report extends BaseMethod
{
private $sql_table = 'ct_report_list';
/**
* @param $String
* @param int $SplitLength
* @return array|bool
*/
function utf8_str_split($String, $SplitLength = 1)
{
if (!preg_match('/^[0-9]+$/', $SplitLength) || $SplitLength < 1)
return false;
$Length = mb_strlen($String, 'UTF-8');
if ($Length <= $SplitLength)
return array($String);
preg_match_all('/.{' . $SplitLength . '}|[^x00]{1,' . $SplitLength . '}$/us', $String, $Result);
return $Result[0];
}
/**
* Convert a string to an array
* @param string $String
* @param integer $SplitLength
* @param string $Charset
* @return array of char
*/
function mb_str_split($String, $SplitLength = 1 ,$Charset = USEDCHARSET)
{
if (strcasecmp($Charset, 'utf-8') == 0)
return $this->utf8_str_split($String, $SplitLength);
elseif (func_num_args() == 1)
return preg_split('/(?<!^)(?!$)/u', $String);
if ($SplitLength < 1)
return false;
$Length = mb_strlen($String, $Charset);
$Result = array();
for ($Index = 0; $Index < $Length; $Index += $SplitLength)
{
$Result[] = mb_substr($String, $Index, $SplitLength, $Charset);
}
return $Result;
}
/*******************************************************************************
* 函数名: ParseSqlCommand *
* 功 能: 解析sql脚本并分离出参数列表 *
* 参 数: *
* pszSqlCommand: 要处理的sql语句 *
* pSqlParameters: 处理后的参数列表 *
* pszForwadChars: 参数前缀字符集合 *
* pszReplaceIdentifier: 用来替换的参数占位符(默认为问号,一般不用修改) *
* 返回值: *
* 成功返回TRUE, 否则返回FALSE *
*******************************************************************************
*******************************************************************************
* 当使用json字符串标记参数时支援的属性为 *
* name: 参数名 *
* type: 参数类型 *
* mask: 参数格式 *
* value: 参数值(最终确定的值) *
* default: 参数默认值 *
* options: 参数可选值enum类型有效 *
* script: 参数要执行的脚本sql脚本类型有效 *
* returnfield: 参数返回字段名sql脚本类型有效 *
*******************************************************************************
* 参数类型可选: *
* unknown: 未知类型 *
* string: 字符串类型 *
* number: 数值类型 *
* enum: 枚举类型options有效并忽略mask属性 *
* date: 日期类型mask固定为yyyy-mm-dd *
* time: 时间类型mask固定为hh:nn:ss *
* datetime: 日期时间类型mask固定为yyyy-mm-dd hh:nn:ss *
* sql: sql脚本类型script和returnfield有效并忽略mask属性 *
*******************************************************************************
* @param string $SqlCommand sql语句
* @param array $SqlParameters 返回的参数列表
* @param string $ForwadChars 前导字符,作为判断参数的起始字符
* @param string $ReplaceIdentifier 替换后的参数占位符
* @return bool
* @throws
*******************************************************************************/
private function ParseSqlCommand(
&$SqlCommand, /// sql语句
&$SqlParameters, /// 返回的参数列表
$ForwadChars = '&:', /// 前导字符,作为判断参数的起始字符
$ReplaceIdentifier = '?' /// 替换后的参数占位符
)
{
$SqlParameters = array();
if (empty($SqlCommand))
return false;
$CommandLength = mb_strlen($SqlCommand, USEDCHARSET);
$Result = '';
/// 前导字符,作为判断参数的起始字符
$StringDelimiter = "{$ForwadChars}`'\"-/\0";
/// 分割字符
$NameDelimiter = "{$ForwadChars} ,;()[]{}`=<>\r\n";
///
$CurrentIndex = 0;
$StartIndex = 0;
//$SqlCommandArray = str_split($SqlCommand);
$SqlCommandArray = $this->mb_str_split($SqlCommand, 1, USEDCHARSET);
//$SqlCommandArray = $this->utf8_str_split($SqlCommand);
while ($CurrentIndex < $CommandLength)
{
$c = $SqlCommandArray[$CurrentIndex];
//$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET);
/// 先过滤掉不需要判断的字符,一直碰到参数的前导字符或者是引号
while ($CurrentIndex < $CommandLength)
{
$c = $SqlCommandArray[$CurrentIndex];
//$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET);
if (mb_strstr($StringDelimiter, $c, false, USEDCHARSET))
break;
else
$CurrentIndex++;
}
switch ($c)
{
/// 字符串结束
case "\0":
case '':
break;
/// 引号表示字符串
case '`':
case '\'':
case '"':
/// 记录下这个标记,表示现在是字符串开始;再碰到一个同样的标记表示字符串结束。
$cLiteral = $c;
do
{
/// 快速跳过字符串内容
$CurrentIndex++;
$c = $SqlCommandArray[$CurrentIndex];
//$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET);
if ($cLiteral == $c)
break;
} while ($CurrentIndex < $CommandLength);
$CurrentIndex++;
break;
default:
$CurrentString = mb_substr($SqlCommand, $CurrentIndex, 2, USEDCHARSET);
if (0 == strcasecmp($CurrentString, '--')) /// 单行注释
{
$CurrentIndex += 2;
$c = $SqlCommandArray[$CurrentIndex];
//$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET);
/// 迅速跳过注释行
while ($CurrentIndex < $CommandLength)
{
if ("\r" == $c || "\n" == $c)
{
while ("\r" == $c || "\n" == $c)
{
$CurrentIndex++;
$c = $SqlCommandArray[$CurrentIndex];
//$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET);
}
break;
}
else
{
$CurrentIndex++;
$c = $SqlCommandArray[$CurrentIndex];
//$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET);
}
}
break;
}
else if (0 == strcasecmp($CurrentString, '/*')) /// 多行注释
{
$CurrentIndex += 2;
/// 迅速跳过注释内容
while ($CurrentIndex < $CommandLength)
{
$CurrentString = mb_substr($SqlCommand, $CurrentIndex, 2, USEDCHARSET);
if (0 == strcasecmp($CurrentString, '*/'))
{
$CurrentIndex += 2;
break;
}
else
{
$CurrentIndex++;
}
}
break;
}
else if (mb_strstr($ForwadChars, $c, false, USEDCHARSET))
{
$CurrentIndex++;
$c = $SqlCommandArray[$CurrentIndex];
//$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET);
if (mb_strstr($ForwadChars, $c, false, USEDCHARSET))
{
$CurrentIndex++;
$c = $SqlCommandArray[$CurrentIndex];
//$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET);
}
$Result .= mb_substr($SqlCommand, $StartIndex, $CurrentIndex - $StartIndex - 1) . $ReplaceIdentifier;
if ('{' == $c) /// 处理json字符串表示的参数
{
$ParameterIndex = $CurrentIndex;
$JsonFlagCount = 0;
while ($CurrentIndex < $CommandLength)
{
$c = $SqlCommandArray[$CurrentIndex];
//$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET);
$CurrentIndex++;
switch ($c)
{
case '{':
$JsonFlagCount++;
break;
case '}':
$JsonFlagCount--;
break;
}
if (0 == $JsonFlagCount)
break;
}
}
else /// 非json表示的参数
{
$cLiteral = '';
if (mb_strstr('`\'"', $c, false, USEDCHARSET))
{
$cLiteral = $c;
$CurrentIndex++;
}
$ParameterIndex = $CurrentIndex;
while ($CurrentIndex < $CommandLength)
{
$c = $SqlCommandArray[$CurrentIndex];
//$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET);
if (($cLiteral == $c) || ('' == $cLiteral && mb_strstr($NameDelimiter, $c, false, USEDCHARSET)))
break;
$CurrentIndex++;
}
}
$Parameter = mb_substr($SqlCommand, $ParameterIndex, $CurrentIndex - $ParameterIndex, USEDCHARSET);
array_push($SqlParameters, new ReportParameter($Parameter));
$StartIndex = $CurrentIndex;
}
break;
}
}
$Result .= mb_substr($SqlCommand, $StartIndex, $CurrentIndex - $StartIndex);
$SqlCommand = $Result;
return true;
}
/**
* 获取菜单
* @param RequestParameter $request
* @param ReturnParameter $return
* @return bool
*/
public function getMenus($request, &$return)
{
try {
$biz_content = $request->biz_content;
//查询用户
$admin_id = isset($biz_content['admin_id']) ? $biz_content['admin_id'] : false; // 管理员id
if(!$admin_id){
$return->SetErrors(ERRORCODE_INPARAMERROR, ERRORINFO_INPARAMERROR);
return false;
}
$admin = $this->PDO_Request(/** @lang text */'
SELECT
`level`
FROM ct_manager_info m
WHERE `login_id` = ? AND `is_enabled` = ?;', $admin_id, 1);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if (!$admin) {
$return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR);
return false;
}
$cmd = $this->NewMasterCommand();
// 获取所有报表脚本信息
$items = $this->PDO_Request(/** @lang text */"
select
a.id as cate_id, a.name, a.icon,
b.report_id, b.report_name, b.command_line, b.url, b.desc
from ct_report_cate as a
left join {$this->sql_table} as b
on
a.`id` = b.`cate_id`
where
a.`status` = 1 and
b.`is_enabled` = 1
group by
cate_id, report_id");
$menu = array();
foreach($items as $key=>$val) {
// 主菜单部分
$menu[$val['cate_id']]['id'] = $val['cate_id'];
$menu[$val['cate_id']]['name'] = $val['name'];
$menu[$val['cate_id']]['icon'] = $val['icon'];
$this->ParseSqlCommand($val['command_line'], $SqlParameters);
//$SqlParameters = json_decode( json_encode( $SqlParameters),true);
foreach ($SqlParameters as $sk => $sv) {
// 脚本处理
if (strcasecmp($sv->type, 'sql') == 0) {
$replace_admin_id = $cmd->getidentifiers($admin_id, true); // 处理sql引号
$script = str_replace('[%adminId%]', $replace_admin_id, $sv->script);
$result = $this->PDO_Request($script);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
$SqlParameters[$sk]->options = $result;
unset($SqlParameters[$sk]->script);
}
}
// 子菜单部分
$menu[$val['cate_id']]['child'][] = [
'report_id' => $val['report_id'],
'report_name' => $val['report_name'],
'url' => $val['url'],
'desc' => json_decode($val['desc']),
'values' => $SqlParameters,
];
}
// 5、数据返回部分
$return->biz_content = $menu;
return true;
}
catch (Exception $e) {
$return->SetErrors($e->getCode(), $e->getMessage());
return false;
}
}
/**
* 处理查询
* @param RequestParameter $request
* @param ReturnParameter $return
* @return bool
*/
public function getReportTest($request, &$return)
{
try {
// 获取具体id
$biz_content = $request->biz_content;
$report_id = isset($biz_content['report_id']) ? $biz_content['report_id'] : false; // 报表id
$admin_id = isset($biz_content['admin_id']) ? $biz_content['admin_id'] : false; // 管理员id
$channel = isset($biz_content['channel']) ? $biz_content['channel'] : false; // 指定渠道key
if(!$report_id || !$admin_id){
$return->SetErrors(ERRORCODE_INPARAMERROR, ERRORINFO_INPARAMERROR);
return false;
}
//查询用户
$admin = $this->PDO_Request(/** @lang text */'
SELECT
`level`
FROM ct_manager_info m
WHERE `login_id` = ? AND `is_enabled` = ?;', $admin_id, 1);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if (!$admin) {
$return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR);
return false;
}
$item = $this->PDO_Request(/** @lang text */"select report_id, report_name, command_line, url from {$this->sql_table} where report_id = ?;", $report_id);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
$SqlCommand = $item[0]['command_line'];
# 分页信息
$page_index = empty($biz_content['page_index']) ? 1 : intval($biz_content['page_index']); // 当前页
$page_size = empty($biz_content['page_size']) ? 20: intval($biz_content['page_size']); // 单页容量
$start = ($page_index - 1) * $page_size;
if (!empty($page_index))
$strPage = " LIMIT {$start},{$page_size} ";
$SqlCommand = str_replace("[%strPage%]", $strPage, $SqlCommand);
$cmd=$this->NewMasterCommand();
$replace_admin_id = $cmd->getidentifiers($admin_id, true); // 处理sql引号
$SqlCommand = str_replace('[%adminId%]', $replace_admin_id, $SqlCommand);
$this->ParseSqlCommand($SqlCommand, $SqlParameters);
$SqlParameters = json_decode( json_encode( $SqlParameters),true);
$params = []; // 查询参数
foreach ($SqlParameters as $key => $value) {
if($value['type'] === 'sql') {
// 脚本处理
$result = $this->PDO_Request(/** @lang text */
$value['script']);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if(DEBUG_MODE){
//按指定渠道查询
if($channel){
if(!strstr($result[0]['value'], $channel)){
$return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR);
return false;
}
}else{
$value['default'] = $result[0]['value'];
}
}
}
$params[] = isset($biz_content[$value['returnfield']]) ? $biz_content[$value['returnfield']] : $value['default'];
}
var_dump($SqlCommand);
var_dump($params);die;
// 最终查询
$items = $this->PDO_Request(/** @lang text */
$SqlCommand, $params);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if(is_array($items) && isset($items[0])) {
$data['title'] = array_keys($items[0]);
foreach ($items as $k => $v) {
foreach ($v as $q) {
$data['data'][$k][] = $q;
}
}
}else{
$data = [];
}
// 5、数据返回部分
$return->biz_content = $data;
return true;
}
catch (Exception $e) {
$return->SetErrors($e->getCode(), $e->getMessage());
return false;
}
}
/**
* 处理查询
* @param RequestParameter $request
* @param ReturnParameter $return
* @return bool
*/
public function getReport($request, &$return)
{
/// saved max execute time count.
$MaxExecuteTime = ini_get('max_execution_time');
try {
// 获取具体id
$biz_content = $request->biz_content;
$report_id = isset($biz_content['report_id']) ? $biz_content['report_id'] : false; // 报表id
$admin_id = isset($biz_content['admin_id']) ? $biz_content['admin_id'] : false; // 管理员id
$channel = isset($biz_content['channel']) ? $biz_content['channel'] : false; // 指定渠道key
if(!$report_id || !$admin_id){
$return->SetErrors(ERRORCODE_INPARAMERROR, ERRORINFO_INPARAMERROR);
return false;
}
//查询用户
$admin = $this->PDO_Request(/** @lang text */'
SELECT
`level`
FROM ct_manager_info m
WHERE `login_id` = ? AND `is_enabled` = ?;', $admin_id, 1);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if (!$admin) {
$return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR);
return false;
}
$item = $this->PDO_Request(/** @lang text */'
SELECT
report_id, report_name, command_line, url
FROM '.$this->sql_table.'
WHERE report_id = ?;
', $report_id);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
$SqlCommand = $item[0]['command_line'];
# 分页信息
$page_index = empty($biz_content['page_index']) ? 1 : intval($biz_content['page_index']); // 当前页
$page_size = empty($biz_content['page_size']) ? 20: intval($biz_content['page_size']); // 单页容量
$start = ($page_index - 1) * $page_size;
if (!empty($page_index))
$strPage = " LIMIT {$start},{$page_size} ";
$SqlCommand = str_replace("[%strPage%]", $strPage, $SqlCommand);
$cmd=$this->NewMasterCommand();
$replace_admin_id = $cmd->getidentifiers($admin_id, true); // 处理sql引号
$SqlCommand = str_replace('[%adminId%]', $replace_admin_id, $SqlCommand);
$this->ParseSqlCommand($SqlCommand, $SqlParameters);
$SqlParameters = json_decode( json_encode( $SqlParameters),true);
$params = []; // 查询参数
foreach ($SqlParameters as $key => $value) {
if($value['type'] === 'sql') {
// 脚本处理
$result = $this->PDO_Request(/** @lang text */
$value['script']);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if(DEBUG_MODE){
//按指定渠道查询
if($channel){
if(!strstr($result[0]['value'], $channel)){
$return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR);
return false;
}
}else{
$value['default'] = $result[0]['value'];
}
}
}
$params[] = isset($biz_content[$value['returnfield']]) ? $biz_content[$value['returnfield']] : $value['default'];
}
/// disable timeout limit.
set_time_limit(0);
// 最终查询
$items = $this->PDO_Request(/** @lang text */$SqlCommand, $params);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
/// restore max execute time count.
set_time_limit($MaxExecuteTime);
if(is_array($items) && isset($items[0])) {
$data['title'] = array_keys($items[0]);
foreach ($items as $k => $v) {
foreach ($v as $q) {
$data['data'][$k][] = $q;
}
}
}else{
$data = [];
}
// 5、数据返回部分
$return->biz_content = $data;
return true;
}
catch (Exception $e) {
$return->SetErrors($e->getCode(), $e->getMessage());
return false;
}
}
/**
* 导出报表
* @param RequestParameter $request
* @param ReturnParameter $return
* @return bool
*/
public function export_report_test($request, &$return)
{
// 获取具体id
$biz_content = $request->biz_content;
$report_id = isset($biz_content['report_id']) ? $biz_content['report_id'] : false; // 报表id
$admin_id = isset($biz_content['admin_id']) ? $biz_content['admin_id'] : false; // 管理员id
$channel = isset($biz_content['channel']) ? $biz_content['channel'] : false; // 指定渠道key
if(!$report_id || !$admin_id){
$return->SetErrors(ERRORCODE_INPARAMERROR, ERRORINFO_INPARAMERROR);
return false;
}
//查询用户
$admin = $this->PDO_Request(/** @lang text */'
SELECT
`level`
FROM ct_manager_info m
WHERE `login_id` = ? AND `is_enabled` = ?;', $admin_id, 1);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if (!$admin) {
$return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR);
return false;
}
$item = $this->PDO_Request(/** @lang text */'
SELECT
report_id, report_name, command_line, url
FROM '.$this->sql_table.'
WHERE report_id = ?;
', $report_id);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
$SqlCommand = $item[0]['command_line'];
$SqlCommand = str_replace("[%strPage%]", '', $SqlCommand); // 去掉分页
$cmd=$this->NewMasterCommand();
$replace_admin_id = $cmd->getidentifiers($admin_id, true); // 处理sql引号
$SqlCommand = str_replace('[%adminId%]', $replace_admin_id, $SqlCommand);
$this->ParseSqlCommand($SqlCommand, $SqlParameters);
$SqlParameters = json_decode( json_encode( $SqlParameters),true);
$params = []; // 查询参数
foreach ($SqlParameters as $key => $value) {
if($value['type'] === 'sql') {
// 脚本处理
$result = $this->PDO_Request(/** @lang text */
$value['script']);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if(DEBUG_MODE){
//按指定渠道查询
if($channel){
if(!strstr($result[0]['value'], $channel)){
$return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR);
return false;
}
}else{
$value['default'] = $result[0]['value'];
}
}
}
$params[] = isset($biz_content[$value['returnfield']]) ? $biz_content[$value['returnfield']] : $value['default'];
}
// 最终查询
$items = $this->PDO_Request(/** @lang text */
$SqlCommand, $params);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if(is_array($items) && isset($items[0])) {
$title = array_keys($items[0]);
}else{
$title = false;
}
$config = [
// 'body'=>[
// 'use_time' => '核销时间',
// 'checker_name'=> '核销员',
// 'discount_no' => '券号',
// 'id' => '活动编号',
// 'title' => '活动名称',
// 'price' => '卡券面额',
// 'user_name' => '兑换人',
// ],
'body'=>$title,
'data'=>$items,
];
if (!$items) {
return json(['code'=>'-1', 'data'=>null, 'msg'=>'数据准备失败']);
}
$this->exportData($config);
}
/**
* 导出报表
* @param RequestParameter $request
* @param ReturnParameter $return
* @return bool
*/
public function exportReport($request, &$return)
{
// 获取具体id
$biz_content = $request->biz_content;
$report_id = isset($biz_content['report_id']) ? $biz_content['report_id'] : false; // 报表id
$admin_id = isset($biz_content['admin_id']) ? $biz_content['admin_id'] : false; // 管理员id
$channel = isset($biz_content['channel']) ? $biz_content['channel'] : false; // 指定渠道key
if(!$report_id || !$admin_id){
$return->SetErrors(ERRORCODE_INPARAMERROR, ERRORINFO_INPARAMERROR);
return false;
}
//查询用户
$admin = $this->PDO_Request(/** @lang text */'
SELECT
`level`
FROM ct_manager_info m
WHERE `login_id` = ? AND `is_enabled` = ?;', $admin_id, 1);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if (!$admin) {
$return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR);
return false;
}
$item = $this->PDO_Request(/** @lang text */'
SELECT
report_id, report_name, command_line, url
FROM '.$this->sql_table.'
WHERE report_id = ?;
', $report_id);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
$SqlCommand = $item[0]['command_line'];
$SqlCommand = str_replace("[%strPage%]", '', $SqlCommand); // 去掉分页
$cmd=$this->NewMasterCommand();
$replace_admin_id = $cmd->getidentifiers($admin_id, true); // 处理sql引号
$SqlCommand = str_replace('[%adminId%]', $replace_admin_id, $SqlCommand);
$this->ParseSqlCommand($SqlCommand, $SqlParameters);
$SqlParameters = json_decode( json_encode( $SqlParameters),true);
$params = []; // 查询参数
foreach ($SqlParameters as $key => $value) {
if($value['type'] === 'sql') {
// 脚本处理
$result = $this->PDO_Request(/** @lang text */
$value['script']);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
if(DEBUG_MODE){
//按指定渠道查询
if($channel){
if(!strstr($result[0]['value'], $channel)){
$return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR);
return false;
}
}else{
$value['default'] = $result[0]['value'];
}
}
}
$params[] = isset($biz_content[$value['returnfield']]) ? $biz_content[$value['returnfield']] : $value['default'];
}
$DatabaseHelper = $this->GetMasterDatabase();
// $DatabaseHelper->ExportToFile('test', $SqlCommand, $params);
$DatabaseHelper->ExportToBrowser($SqlCommand, $params);
die;
}
/**
* 导出报表工具方法
* @param RequestParameter $request
* @param ReturnParameter $return
* @return bool
*/
public function exportData($confg)
{
$name = isset($confg['name']) ? $confg['name'] : time(); // 导出文件名
$body = isset($confg['body'])&&is_array($confg['body']) ? $confg['body'] : false; // 表格结构
$data = isset($confg['data'])&&is_array($confg['data']) ? $confg['data'] : false; // 表数据
$type = isset($confg['type']) ? $confg['type'] : 'xls'; // 导出拓展名
if (!$body || !$data) {
return false;
}
$table = '';
$table .= '<table>
<thead>
<tr>';
foreach ($body as $v) {
$table .= "<td class='name'>{$v}</td>";
}
$table .= '</tr>
</thead>
<tbody>';
foreach ($data as $k=>$v) {
$v = array_values($v);
$table .= "<tr>";
foreach ($body as $bk=>$bv) {
$table .= "<td class='name'>{$v[$bk]}</td>";
}
$table .= " </tr>";
}
$table .= '</tbody>
</table>';
// 通过header头控制输出excel表格
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header('Content-Disposition:attachment;filename='.$name.'.'.$type);
header("Content-Transfer-Encoding:binary");
echo $table;
die;
}
public function _businessIncomeSql($date)
{
$date = str_replace('-', '', $date);
// 第一个版本
$sql = <<<EOL
select
#a.agent_id,
#a.channel_id,
a.nickname `渠道`,
(ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) `总售卡数`,
ifnull(d.free_card, 0) `赠送卡数`,
(ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0) `结算卡数`,
ifnull(d.price_card, 0) `结算单价[房卡]`,
(((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) / ifnull(b.sum_card, 1) `实际单价[房卡]`,
(((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) `结算金额[房卡]`,
(((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) * ifnull(a.proportions, 0) `渠道分成[房卡]`,
(((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) - (((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) * ifnull(a.proportions, 0) `业务收入[房卡]`,
(ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) `总金币数`,
ifnull(d.free_gold, 0) `赠送金币数`,
(ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0) `结算金币数`,
ifnull(d.price_gold, 0) `结算单价[金币]`,
(((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) / case when (ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) = 0 then 1 else (ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) end `实际单价[金币]`,
(((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) `结算金额[金币]`,
(((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) * ifnull(a.proportions, 0) `渠道分成[金币]`,
(((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) - (((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) * ifnull(a.proportions, 0) `业务收入[金币]`,
ifnull(e.pay_fee, 0) `实收款[渠道转款]`,
ifnull(f.sum_card_sell, 0) + ifnull(o.sum_card_sell_third, 0) `售卡数量`,
ifnull(f.sum_card_money, 0) + ifnull(o.sum_card_money_third, 0) `售卡金额`,
(ifnull(f.sum_card_money, 0) + ifnull(o.sum_card_money_third, 0)) * 0.99 * (1 - a.proportions) `售卡公司分成`,
ifnull(g.sum_gold_sell, 0) + ifnull(o.sum_gold_sell_third, 0) `售金币数`,
ifnull(g.sum_gold_money, 0) + ifnull(o.sum_gold_money_third, 0) `售金币金额`,
ifnull(e.pay_fee_online, 0) `实收款[线上支付]`,
-- (((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) +
-- (((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) +
-- ifnull(f.sum_card_money, 0) * 0.99 * (1 - a.proportions) +
-- ifnull(g.sum_gold_money, 0) `结算金额`,
((((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) - (((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) * ifnull(a.proportions, 0)) +
((ifnull(f.sum_card_money, 0) + ifnull(o.sum_card_money_third, 0)) * 0.99 * (1 - a.proportions)) +
(ifnull(g.sum_gold_money, 0) + ifnull(o.sum_gold_money_third, 0)) `结算金额`,
ifnull(e.pay_fee, 0) +
-- ifnull(f.sum_card_money, 0) * 0.99 * (1 - a.proportions) +
-- ifnull(g.sum_gold_money, 0) `应收实收`,
ifnull(e.pay_fee_online, 0) `应收实收`,
ifnull(h.receivable, 0) `累计应收未收`
from
ct_channel_list a
left join
(
select
a.satr_agentid agent_id,
a.channel_id,
sum(a.satr_amount) sum_card # 给代理转卡数
from
sales_transferbill a inner join sales_user b on a.satr_agentid = b.saus_agentid and a.channel_id = b.saus_channelid and a.from_sales = b.saus_salesid
where
ifnull(b.statistic_type, 0) = 1 and date_format(a.satr_transfertime, '%Y%m') = '{$date}'
group by
a.satr_agentid, a.channel_id
) b on a.agent_id = b.agent_id and a.channel_id = b.channel_id
left join
(
select
a.saab_agentid agent_id,
a.channel_id channel_id,
ifnull(sum(saab_amount), 0) sum_ask_card # 给玩家和代理的索卡数
from
sales_ask_bill a
inner join sales_user b on a.saab_agentid = b.saus_agentid and a.channel_id = b.saus_channelid and a.saab_salesid = b.saus_salesid
where
b.statistic_type = 1
and a.saab_state = 1
and a.saab_type in (0,1)
and date_format(a.saab_dealtime, '%Y%m') = '{$date}'
group by
a.saab_agentid, a.channel_id
) i on a.agent_id = i.agent_id and a.channel_id = i.channel_id
left join
(
select
a.sase_agentid agent_id,
a.channel_id channel_id,
ifnull(sum(sase_amount), 0) sum_player_card # 给玩家的转卡数
from
sales_sellbill a inner join sales_user b on a.sase_agentid = b.saus_agentid and a.channel_id = b.saus_channelid and a.from_sales = b.saus_salesid
where
b.statistic_type = 1 and date_format(a.sase_selltime, '%Y%m') = '{$date}'
group by
a.sase_agentid, a.channel_id
) j on a.agent_id = j.agent_id and a.channel_id = j.channel_id
left join
(
select
a.agent_id,
a.channel_id,
sum(a.amount) sum_gold # 给代理的转星数
from
trans_star_record a inner join sales_user b on a.agent_id = b.saus_agentid and a.channel_id = b.saus_channelid and a.send_id = b.saus_salesid
where
ifnull(b.statistic_type, 0) = 1 and date_format(a.op_time, '%Y%m') = '{$date}'
group by
a.agent_id, a.channel_id
) c on a.agent_id = c.agent_id and a.channel_id = c.channel_id
left join
(
select
a.ssbe_agentid agent_id,
a.channel_id channel_id,
sum(a.ssbe_amount) sum_player_gold # 给玩家的转星数
from
sales_sellbill_bean a inner join sales_user b on a.ssbe_agentid = b.saus_agentid and a.channel_id = b.saus_channelid and a.from_sales = b.saus_salesid
where
ifnull(b.statistic_type, 0) = 1 and date_format(a.ssbe_selltime, '%Y%m') = '{$date}'
group by
a.ssbe_agentid, a.channel_id
) k on a.agent_id = k.agent_id and a.channel_id = k.channel_id
left join
(
select
a.saab_agentid agent_id,
a.channel_id channel_id,
ifnull(sum(saab_amount), 0) sum_ask_gold # 给玩家和代理的索星星数
from
sales_ask_bill a
inner join sales_user b on a.saab_agentid = b.saus_agentid and a.channel_id = b.saus_channelid and a.saab_salesid = b.saus_salesid
where
b.statistic_type = 1
and a.saab_state = 1
and a.saab_type in (2,3)
and date_format(a.saab_dealtime, '%Y%m') = '{$date}'
group by
a.saab_agentid, a.channel_id
) l on a.agent_id = i.agent_id and a.channel_id = i.channel_id
left join
(
select
agent_id,
channel_id,
free_card,
free_gold,
price_card,
price_gold
from
ct_channel_account_period
where
account_period = '{$date}'
group by
agent_id, channel_id
) d on a.agent_id = d.agent_id and a.channel_id = d.channel_id
left join
(
select
agent_id,
channel_id,
sum(pay_fee) pay_fee,
sum(pay_fee_online) pay_fee_online
from
ct_channel_pay_log
where
is_enable != 0 and date_format(pay_time, '%Y%m') = '{$date}'
group by
agent_id, channel_id
) e on a.agent_id = e.agent_id and a.channel_id = e.channel_id
left join
(
select
sabu_agentid agent_id,
sabu_channelid channel_id,
sum(sabu_amount) sum_card_sell,
sum(sabu_paymoney) sum_card_money
from
sales_buybill
where
sabu_paystate = 1 and date_format(sabu_paytime, '%Y%m') = '{$date}'
group by
sabu_agentid, sabu_channelid
) f on a.agent_id = f.agent_id and a.channel_id = f.channel_id
left join
(
select
agent_id,
channel_id,
sum(star_amount) sum_gold_sell,
sum(pay_money) sum_gold_money
from
order_star
where
`status` = 1 and date_format(pay_time, '%Y%m') = '{$date}'
group by
agent_id, channel_id
) g on a.agent_id = g.agent_id and a.channel_id = g.channel_id
left join
(
select
agent_id,
channel_id,
sum(receivable) receivable
from
ct_channel_account_period
where
is_enable != 0 and account_period <= '{$date}'
group by
agent_id, channel_id
) h on a.agent_id = h.agent_id and a.channel_id = h.channel_id
left join
(
select
agent_id,
channel_id,
sum(case type when 1 then amount else 0 end) sum_card_sell_third,
sum(case type when 2 then amount else 0 end) sum_gold_sell_third,
sum(case type when 1 then fee else 0 end) sum_card_money_third,
sum(case type when 2 then fee else 0 end) sum_gold_money_third
from
ct_pay_for_third
where
date_format(create_time, '%Y%m') = '{$date}'
group by
agent_id, channel_id
) o on a.agent_id = o.agent_id and a.channel_id = o.channel_id
left join
(
select
agent_id,
channel_id,
sum(case product_type when 0 then product_amount else 0 end) sum_card_sell_v2,
sum(case product_type when 1 then product_amount else 0 end) sum_gold_sell_v2,
sum(case product_type when 2 then product_amount else 0 end) sum_diamond_sell_v2,
sum(case product_type when 0 then pay_money else 0 end) sum_card_money_v2,
sum(case product_type when 1 then pay_money else 0 end) sum_gold_money_v2,
sum(case product_type when 2 then pay_money else 0 end) sum_diamond_money_v2
from
ct_order_info
where
pay_status = 1 and date_format(FROM_UNIXTIME(pay_time), '%Y%m') = '{$date}'
) m on a.agent_id = m.agent_id and a.channel_id = m.channel_id
where a.is_open = 1
EOL;
// 第二个版本添加了ct_order_info表中的数据
$sql_v2 = <<<EOL
select
a.nickname `渠道`,
(ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) `总售卡数`,
ifnull(d.free_card, 0) `赠送卡数`,
(ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0) `结算卡数`,
ifnull(d.price_card, 0) `结算单价[房卡]`,
(((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) / ifnull(b.sum_card, 1) `实际单价[房卡]`,
(((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) `结算金额[房卡]`,
(((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) * ifnull(a.proportions, 0) `渠道分成[房卡]`,
(((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) - (((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) * ifnull(a.proportions, 0) `业务收入[房卡]`,
(ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) `总金币数`,
ifnull(d.free_gold, 0) `赠送金币数`,
(ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0) `结算金币数`,
ifnull(d.price_gold, 0) `结算单价[金币]`,
(((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) / case when (ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) = 0 then 1 else (ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) end `实际单价[金币]`,
(((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) `结算金额[金币]`,
(((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) * ifnull(a.proportions, 0) `渠道分成[金币]`,
(((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) - (((ifnull(c.sum_gold, 0) + ifnull(k.sum_player_gold, 0) + ifnull(l.sum_ask_gold, 0)) - ifnull(d.free_gold, 0)) * ifnull(d.price_gold, 0)) * ifnull(a.proportions, 0) `业务收入[金币]`,
ifnull(e.pay_fee, 0) `实收款[渠道转款]`,
(ifnull(f.sum_card_sell, 0) + ifnull(o.sum_card_sell_third, 0) + ifnull(m.sum_card_sell_v2, 0)) `售卡数量`,
(ifnull(f.sum_card_money, 0) + ifnull(o.sum_card_money_third, 0) + ifnull(m.sum_card_money_v2, 0)) `售卡金额`,
((ifnull(f.sum_card_money, 0) + ifnull(o.sum_card_money_third, 0) + ifnull(m.sum_card_money_v2, 0))) * 0.99 * (1 - a.proportions) `售卡公司分成`,
(ifnull(g.sum_gold_sell, 0) + ifnull(o.sum_gold_sell_third, 0) + ifnull(m.sum_gold_sell_v2, 0)) `售金币数`,
(ifnull(g.sum_gold_money, 0) + ifnull(o.sum_gold_money_third, 0) + ifnull(m.sum_gold_money_v2, 0)) `售金币金额`,
ifnull(e.pay_fee_online, 0) `实收款[线上支付]`,
((((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) - (((ifnull(b.sum_card, 0) + ifnull(i.sum_ask_card, 0) + ifnull(j.sum_player_card, 0)) - ifnull(d.free_card, 0)) * ifnull(d.price_card, 0)) * ifnull(a.proportions, 0)) +
(((ifnull(f.sum_card_money, 0) + ifnull(o.sum_card_money_third, 0) + ifnull(m.sum_card_money_v2, 0))) * 0.99 * (1 - a.proportions)) +
(ifnull(g.sum_gold_money, 0) + ifnull(o.sum_gold_money_third, 0) + ifnull(m.sum_gold_money_v2, 0)) `结算金额`,
ifnull(e.pay_fee, 0) + ifnull(e.pay_fee_online, 0) `应收实收`,
ifnull(h.receivable, 0) `累计应收未收`
from
ct_channel_list a
left join
(
select
a.satr_agentid agent_id,
a.channel_id,
sum(a.satr_amount) sum_card # 给代理转卡数
from
sales_transferbill a inner join sales_user b on a.satr_agentid = b.saus_agentid and a.channel_id = b.saus_channelid and a.from_sales = b.saus_salesid
where
ifnull(b.statistic_type, 0) = 1 and date_format(a.satr_transfertime, '%Y%m') = '{$date}'
group by
a.satr_agentid, a.channel_id
) b on a.agent_id = b.agent_id and a.channel_id = b.channel_id
left join
(
select
a.saab_agentid agent_id,
a.channel_id channel_id,
ifnull(sum(saab_amount), 0) sum_ask_card # 给玩家和代理的索卡数
from
sales_ask_bill a
inner join sales_user b on a.saab_agentid = b.saus_agentid and a.channel_id = b.saus_channelid and a.saab_salesid = b.saus_salesid
where
b.statistic_type = 1
and a.saab_state = 1
and a.saab_type in (0,1)
and date_format(a.saab_dealtime, '%Y%m') = '{$date}'
group by
a.saab_agentid, a.channel_id
) i on a.agent_id = i.agent_id and a.channel_id = i.channel_id
left join
(
select
a.sase_agentid agent_id,
a.channel_id channel_id,
ifnull(sum(sase_amount), 0) sum_player_card # 给玩家的转卡数
from
sales_sellbill a inner join sales_user b on a.sase_agentid = b.saus_agentid and a.channel_id = b.saus_channelid and a.from_sales = b.saus_salesid
where
b.statistic_type = 1 and date_format(a.sase_selltime, '%Y%m') = '{$date}'
group by
a.sase_agentid, a.channel_id
) j on a.agent_id = j.agent_id and a.channel_id = j.channel_id
left join
(
select
a.agent_id,
a.channel_id,
sum(a.amount) sum_gold # 给代理的转星数
from
trans_star_record a inner join sales_user b on a.agent_id = b.saus_agentid and a.channel_id = b.saus_channelid and a.send_id = b.saus_salesid
where
ifnull(b.statistic_type, 0) = 1 and date_format(a.op_time, '%Y%m') = '{$date}'
group by
a.agent_id, a.channel_id
) c on a.agent_id = c.agent_id and a.channel_id = c.channel_id
left join
(
select
a.ssbe_agentid agent_id,
a.channel_id channel_id,
sum(a.ssbe_amount) sum_player_gold # 给玩家的转星数
from
sales_sellbill_bean a inner join sales_user b on a.ssbe_agentid = b.saus_agentid and a.channel_id = b.saus_channelid and a.from_sales = b.saus_salesid
where
ifnull(b.statistic_type, 0) = 1 and date_format(a.ssbe_selltime, '%Y%m') = '{$date}'
group by
a.ssbe_agentid, a.channel_id
) k on a.agent_id = k.agent_id and a.channel_id = k.channel_id
left join
(
select
a.saab_agentid agent_id,
a.channel_id channel_id,
ifnull(sum(saab_amount), 0) sum_ask_gold # 给玩家和代理的索星星数
from
sales_ask_bill a
inner join sales_user b on a.saab_agentid = b.saus_agentid and a.channel_id = b.saus_channelid and a.saab_salesid = b.saus_salesid
where
b.statistic_type = 1
and a.saab_state = 1
and a.saab_type in (2,3)
and date_format(a.saab_dealtime, '%Y%m') = '{$date}'
group by
a.saab_agentid, a.channel_id
) l on a.agent_id = i.agent_id and a.channel_id = i.channel_id
left join
(
select
agent_id,
channel_id,
free_card,
free_gold,
price_card,
price_gold
from
ct_channel_account_period
where
account_period = '{$date}'
group by
agent_id, channel_id
) d on a.agent_id = d.agent_id and a.channel_id = d.channel_id
left join
(
select
agent_id,
channel_id,
sum(pay_fee) pay_fee,
sum(pay_fee_online) pay_fee_online
from
ct_channel_pay_log
where
is_enable != 0 and date_format(pay_time, '%Y%m') = '{$date}'
group by
agent_id, channel_id
) e on a.agent_id = e.agent_id and a.channel_id = e.channel_id
left join
(
select
sabu_agentid agent_id,
sabu_channelid channel_id,
sum(sabu_amount) sum_card_sell,
sum(sabu_paymoney) sum_card_money
from
sales_buybill
where
sabu_paystate = 1 and date_format(sabu_paytime, '%Y%m') = '{$date}'
group by
sabu_agentid, sabu_channelid
) f on a.agent_id = f.agent_id and a.channel_id = f.channel_id
left join
(
select
agent_id,
channel_id,
sum(star_amount) sum_gold_sell,
sum(pay_money) sum_gold_money
from
order_star
where
`status` = 1 and date_format(pay_time, '%Y%m') = '{$date}'
group by
agent_id, channel_id
) g on a.agent_id = g.agent_id and a.channel_id = g.channel_id
left join
(
select
agent_id,
channel_id,
sum(receivable) receivable
from
ct_channel_account_period
where
is_enable != 0 and account_period <= '{$date}'
group by
agent_id, channel_id
) h on a.agent_id = h.agent_id and a.channel_id = h.channel_id
left join
(
select
agent_id,
channel_id,
sum(case type when 1 then amount else 0 end) sum_card_sell_third,
sum(case type when 2 then amount else 0 end) sum_gold_sell_third,
sum(case type when 1 then fee else 0 end) sum_card_money_third,
sum(case type when 2 then fee else 0 end) sum_gold_money_third
from
ct_pay_for_third
where
date_format(create_time, '%Y%m') = '{$date}'
group by
agent_id, channel_id
) o on a.agent_id = o.agent_id and a.channel_id = o.channel_id
left join
(
select
agent_id,
channel_id,
sum(case product_type when 0 then product_amount else 0 end) sum_card_sell_v2,
sum(case product_type when 1 then product_amount else 0 end) sum_gold_sell_v2,
sum(case product_type when 2 then product_amount else 0 end) sum_diamond_sell_v2,
sum(case product_type when 0 then pay_money else 0 end) sum_card_money_v2,
sum(case product_type when 1 then pay_money else 0 end) sum_gold_money_v2,
sum(case product_type when 2 then pay_money else 0 end) sum_diamond_money_v2
from
ct_order_info
where
pay_status = 1 and date_format(FROM_UNIXTIME(pay_time), '%Y%m') = '{$date}'
) m on a.agent_id = m.agent_id and a.channel_id = m.channel_id
where a.is_open = 1
EOL;
return $sql_v2;
}
/**
* 查询营业收入
*/
public function businessIncome($request, &$return)
{
$params = $request->biz_content;
$start_time = isset($params['date']) ? $params['date'] : '201801';
//$end_time = isset($params['end_time']) ? $params['end_time'] : '2018-12-31';
$sql = $this->_businessIncomeSql($start_time);
$items = $this->PDO_Request($sql);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
$data = [];
if(is_array($items) && isset($items[0])) {
$data['title'] = array_keys($items[0]);
foreach ($items as $k => $v) {
foreach ($v as $q) {
if(is_numeric($q))
$q = round($q, 2);
$data['data'][$k][] = $q;
}
}
}
// 5、数据返回部分
$return->biz_content = $data;
return true;
}
/**
* 导出营业收入
*/
public function exportBusinessIncome($request, &$return)
{
$params = $request->biz_content;
$start_time = isset($params['start_time']) ? $params['start_time'] : '2018-01-01';
$end_time = isset($params['end_time']) ? $params['end_time'] : '2018-12-31';
$sql = $this->_businessIncomeSql($start_time, $end_time);
$Helper = $this->GetMasterDatabase();
// $Helper->ExportToFile('test', $SqlCommand, $params);
$Helper->ExportToBrowser($sql);
die;
}
/**
* 添加线下数据
* @param $request
* @param $return
* @return bool
*/
public function addOfflineData($request, &$return)
{
$param = $request->biz_content;
$agent_id = @$param['agentid']; /// 代理id
$channel_id = @$param['channelid']; /// 渠道id
$checked_time = @$param['checked_time']; /// 选中的时间
/// 类型 0-待定 1-房卡赠送数 2-房卡实收款 3-平台实收款 4-金币实收款
$type = intval(@$param['type']) ? intval($param['type']) : 0;
$remark = isset($param['remark']) ? $param['remark'] : '';
$data = isset($param['data']) ? intval($param['data']) : 0;
if (empty($agent_id))
{
$return->seterrors(ERRORCODE_AGENTIDERROR, ERRORINFO_AGENTIDERROR);
return false;
}
if (empty($channel_id))
{
$return->seterrors(ERRORCODE_CHANNELIDERROR, ERRORINFO_CHANNELIDERROR);
return false;
}
if (empty($checked_time))
{
$return->seterrors(ERRORCODE_FINANCEYEARERROR, '请输入年份年份');
return false;
}
$sql = 'update ct_report_offline_data set `status` = 0 WHERE `agent_id` = ? AND `channel_id` = ? AND `type` = ? AND `status` = 1 AND date_format(`checked_time`, "%Y-%m") = date_format(?, "%Y-%m");';
$this->PDO_Request($sql, $agent_id, $channel_id, $type, $checked_time);
$sql = /** @lang text */'insert into ct_report_offline_data (
agent_id, channel_id, data, type, remark, checked_time, status
) VALUES (?,?,?,?,?,?,1);';
$items = $this->PDO_Request($sql, $agent_id, $channel_id, $data, $type, $remark, $checked_time);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
return true;
}
/**
* 区域玩家人数统计
* 高德地图 key0a28f9d6ea7e2c5a015adfc3eaca8b1e
* @param $request
* @param $return
* @return bool
*/
public function areaNumberOfPeople($request, &$return)
{
$param = $request->biz_content;
// agentid eg: '00bA05haB0d9ZC0fwGD09Q2OA30insbQ','1B2h0ccl205c390Y28m1Ajdplkuu4wgy'
$channel_array = isset($param['channel_array']) ? $param['channel_array'] : '';
$group_by = isset($param['group_by']) ? $param['group_by'] : 'play_a_province';
// 筛选渠道
$sql_channel_where = '';
if(!empty($channel_array))
$sql_channel_where = "and play_agentid in ({$channel_array})";
// 查询字段集
$query_field = ['play_a_country', 'play_a_province', 'play_a_city', 'play_a_citycode', 'play_a_district'];
// 筛除不需要的查询字段
$tmp = [];
foreach ($query_field as $k => $v) {
array_push($tmp, $v);
if($group_by == $v)
break;
}
$sql_query_field = implode($tmp, ',');
$sql = <<<EOF
SELECT
*
FROM
(
SELECT
count(*) AS number_of_people,
{$sql_query_field}
#play_a_street
#play_a_address
FROM
player
WHERE
1=1
{$sql_channel_where}
GROUP BY
{$group_by}
) a
ORDER BY
number_of_people desc
EOF;
$items = $this->PDO_Request($sql);
if (!$this->pdo_isdone())
{
$return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo());
return false;
}
$data = [];
if(is_array($items) && isset($items[0])) {
$data['header'] = array_keys($items[0]);
foreach ($items as $k => $v) {
foreach ($v as $q) {
$data['body'][$k][] = $q;
}
}
}
$return->biz_content = $data;
return true;
}
/**
* 区域玩家人数统计--数据可视化(在地图上显示)
* 高德地图 key0a28f9d6ea7e2c5a015adfc3eaca8b1e
* @param $request
* @param $return
* @return bool
*/
public function areaNumberOfPeopleMap($request, &$return) {
$param = $request->biz_content;
$channel_array = isset($param['channel_array']) ? $param['channel_array'] : '';
$group_by = isset($param['group_by']) ? $param['group_by'] : 'play_a_province';
$type = isset($param['type']) ? $param['type'] : 'point';
// 筛选渠道
$sql_channel_where = '';
if(!empty($channel_array))
$sql_channel_where = "and play_agentid in ({$channel_array})";
// 统计类型
switch ($type) {
// 单个玩家打点
case 'point':
$sql = "select concat(play_longitude,',', play_latitude) as center from player where 1=1 {$sql_channel_where} and play_longitude is not null;";
break;
// 按街道统计人数的热力图
case 'hotmap':
$sql = "select play_longitude as `lng`, play_latitude as `lat`, count(*) as `count` from player where 1=1 {$sql_channel_where} and play_longitude is not null GROUP BY play_a_street;";
break;
default:
$return->seterrors(200, '未知的类型');
return false;
}
$items = $this->PDO_Request($sql);
$return->biz_content = $items;
return true;
}
}