<?php /** * 统计分析工具函数 * * 包含所有统计模块共用的工具函数和日期处理逻辑 */ // 确保直接访问时需要先登录 require_once 'conn.php'; if (!isset($_SESSION['employee_id'])) { checkLogin(); } //检查是否管理员 checkPermissionDie(1,2); /** * 获取和处理日期范围参数 * * @return array 包含开始日期、结束日期和其他日期相关参数 */ function getDateRangeParams() { // 计算日期范围 $current_month_start = date('Y-m-01'); $current_month_end = date('Y-m-t'); $last_month_start = date('Y-m-01', strtotime('-1 month')); $last_month_end = date('Y-m-t', strtotime('-1 month')); $current_year_start = date('Y-01-01'); $current_year_end = date('Y-12-31'); // 可选的日期范围筛选 $date_range = isset($_GET['date_range']) ? $_GET['date_range'] : 'current_month'; $custom_start = isset($_GET['start_date']) ? $_GET['start_date'] : ''; $custom_end = isset($_GET['end_date']) ? $_GET['end_date'] : ''; $period = isset($_GET['period']) ? $_GET['period'] : 'day'; // 设置日期范围 if ($date_range == 'custom' && !empty($custom_start) && !empty($custom_end)) { $start_date = $custom_start; $end_date = $custom_end; } else { switch ($date_range) { case 'last_month': $start_date = $last_month_start; $end_date = $last_month_end; break; case 'current_year': $start_date = $current_year_start; $end_date = $current_year_end; break; case 'last_30_days': $start_date = date('Y-m-d', strtotime('-30 days')); $end_date = date('Y-m-d'); break; case 'last_90_days': $start_date = date('Y-m-d', strtotime('-90 days')); $end_date = date('Y-m-d'); break; case 'current_month': default: $start_date = $current_month_start; $end_date = $current_month_end; break; } } // 格式化日期用于SQL查询 $start_date_sql = date('Y-m-d', strtotime($start_date)); $end_date_sql = date('Y-m-d', strtotime($end_date)) . ' 23:59:59'; return [ 'date_range' => $date_range, 'custom_start' => $custom_start, 'custom_end' => $custom_end, 'period' => $period, 'start_date' => $start_date, 'end_date' => $end_date, 'start_date_sql' => $start_date_sql, 'end_date_sql' => $end_date_sql ]; } /** * 生成图表颜色数组 * * @param int $count 需要的颜色数量 * @param bool $transparent 是否透明 * @return array 背景色和边框色数组 */ function generateChartColors($count = 10, $transparent = true) { $colors = [ ['rgba(255, 99, 132, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(255, 99, 132, 1)'], ['rgba(54, 162, 235, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(54, 162, 235, 1)'], ['rgba(255, 206, 86, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(255, 206, 86, 1)'], ['rgba(75, 192, 192, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(75, 192, 192, 1)'], ['rgba(153, 102, 255, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(153, 102, 255, 1)'], ['rgba(255, 159, 64, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(255, 159, 64, 1)'], ['rgba(199, 199, 199, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(199, 199, 199, 1)'], ['rgba(83, 102, 255, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(83, 102, 255, 1)'], ['rgba(40, 159, 64, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(40, 159, 64, 1)'], ['rgba(210, 199, 199, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(210, 199, 199, 1)'] ]; $result = []; // 确保有足够的颜色 while (count($result) < $count) { foreach ($colors as $color) { $result[] = $color; if (count($result) >= $count) { break; } } } return array_slice($result, 0, $count); } /** * 格式化数值,处理空值和小数位数 * * @param mixed $value 要格式化的值 * @param int $decimals 小数位数 * @return string 格式化后的数值 */ function formatNumber($value, $decimals = 2) { if ($value === null || $value === '') { return '0'; } return number_format((float)$value, $decimals); } /** * 获取时间粒度对应的MySQL DATE_FORMAT格式 * * @param string $period 时间粒度 (day/week/month) * @return string MySQL DATE_FORMAT格式字符串 */ function getPeriodFormat($period) { switch ($period) { case 'week': return '%x-W%v'; // ISO year and week number case 'month': return '%Y-%m'; case 'day': default: return '%Y-%m-%d'; } }