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('/(?\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` inner join ct_manager_auth c on b.report_id = c.report_id where a.`status` = 1 and b.`is_enabled` = 1 and c.login_id = ? and c.status = 1 group by cate_id, report_id", $admin_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' => '', '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 getReport($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 INNER JOIN ct_manager_auth b ON m.login_id = b.login_id WHERE b.login_id = ? AND b.report_id = ? AND b.status = 1 AND m.is_enabled = 1;', $admin_id, $report_id); 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']; } // 最终查询 $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 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 INNER JOIN ct_manager_auth b ON m.login_id = b.login_id WHERE b.login_id = ? AND b.report_id = ? AND b.status = 1 AND m.is_enabled = 1;', $admin_id, $report_id); 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']; } $Helper = $this->GetMasterDatabase(); // $Helper->ExportToFile('test', $SqlCommand, $params); $Helper->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 .= ''; foreach ($body as $v) { $table .= ""; } $table .= ''; foreach ($data as $k=>$v) { $v = array_values($v); $table .= ""; foreach ($body as $bk=>$bv) { $table .= ""; } $table .= " "; } $table .= '
{$v}
{$v[$bk]}
'; // 通过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; } /** * 返回营业收入的sql语句 */ /*private function _businessIncomeSql($start_time, $end_time) { $business_income_sql = <<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; } /** * 区域玩家人数统计 * @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 = <<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; } }