diff --git a/app/admin/controller/Statistics.php b/app/admin/controller/Statistics.php index b38d83d..37e92da 100755 --- a/app/admin/controller/Statistics.php +++ b/app/admin/controller/Statistics.php @@ -2148,4 +2148,120 @@ class Statistics extends Base } } + /** + * 获取每日数据统计 + * @return \think\response\Json + */ + public function getDailyStatistics() + { + try { + // 获取日期范围(默认最近30天) + $endDate = date('Y-m-d'); + $startDate = date('Y-m-d', strtotime('-30 days')); + + // 1. 按天订单数据统计 + $orderStats = Db::query(" + SELECT + DATE(FROM_UNIXTIME(o.addtime)) as day, + COUNT(1) as order_count, + COUNT(DISTINCT o.user_id) as user_count, + IFNULL(SUM(price), 0) as price, + IFNULL(SUM(use_money), 0) as use_money, + IFNULL(SUM(use_integral), 0) as use_integral, + IFNULL(SUM(use_money2), 0) as use_money2 + FROM `order` o + LEFT JOIN `user` u ON o.user_id = u.id + WHERE u.status = 1 + AND u.istest = 0 + AND o.status = 1 + AND o.addtime >= UNIX_TIMESTAMP(?) + AND o.addtime < UNIX_TIMESTAMP(?) + GROUP BY DATE(FROM_UNIXTIME(o.addtime)) + ", [$startDate, $endDate]); + + // 2. 登录人数统计 + $loginStats = Db::query(" + SELECT + login_date as day, + COUNT(1) as user_login + FROM user_login_log + WHERE login_date >= ? + AND login_date < ? + GROUP BY login_date + ", [$startDate, $endDate]); + + // 3. 每日新增用户统计 + $registerStats = Db::query(" + SELECT + DATE(FROM_UNIXTIME(addtime)) as day, + COUNT(1) as user_register + FROM `user` + WHERE status = 1 + AND istest = 0 + AND addtime >= UNIX_TIMESTAMP(?) + AND addtime < UNIX_TIMESTAMP(?) + GROUP BY DATE(FROM_UNIXTIME(addtime)) + ", [$startDate, $endDate]); + + // 4. 钻石每日消费数据 + $diamondStats = Db::query(" + SELECT + DATE(o.created_at) as day, + IFNULL(SUM(amount_paid), 0) as amount_paid + FROM diamond_orders o + LEFT JOIN `user` u ON o.user_id = u.id + WHERE o.status = 'success' + AND u.status = 1 + AND u.istest = 0 + AND o.created_at >= ? + AND o.created_at < ? + GROUP BY DATE(o.created_at) + ", [$startDate, $endDate]); + + // 生成日期范围内的所有日期 + $allDates = []; + $currentDate = strtotime($startDate); + $endTimestamp = strtotime($endDate); + while ($currentDate < $endTimestamp) { + $allDates[] = date('Y-m-d', $currentDate); + $currentDate = strtotime('+1 day', $currentDate); + } + + // 将统计数据转换为以日期为键的关联数组 + $orderMap = array_column($orderStats, null, 'day'); + $loginMap = array_column($loginStats, null, 'day'); + $registerMap = array_column($registerStats, null, 'day'); + $diamondMap = array_column($diamondStats, null, 'day'); + + // 合并所有数据 + $result = []; + foreach ($allDates as $date) { + $result[] = [ + 'day' => $date, + 'order_count' => isset($orderMap[$date]) ? intval($orderMap[$date]['order_count']) : 0, + 'user_count' => isset($orderMap[$date]) ? intval($orderMap[$date]['user_count']) : 0, + 'price' => isset($orderMap[$date]) ? floatval($orderMap[$date]['price']) : 0, + 'use_money' => isset($orderMap[$date]) ? floatval($orderMap[$date]['use_money']) : 0, + 'use_integral' => isset($orderMap[$date]) ? floatval($orderMap[$date]['use_integral']) : 0, + 'use_money2' => isset($orderMap[$date]) ? floatval($orderMap[$date]['use_money2']) : 0, + 'user_login' => isset($loginMap[$date]) ? intval($loginMap[$date]['user_login']) : 0, + 'user_register' => isset($registerMap[$date]) ? intval($registerMap[$date]['user_register']) : 0, + 'amount_paid' => isset($diamondMap[$date]) ? floatval($diamondMap[$date]['amount_paid']) : 0 + ]; + } + + return json([ + 'code' => 0, + 'msg' => '获取成功', + 'data' => $result + ]); + + } catch (\Exception $e) { + return json([ + 'code' => 1, + 'msg' => '获取数据失败:' . $e->getMessage() + ]); + } + } + } diff --git a/app/admin/route/app.php b/app/admin/route/app.php index b7adfa8..da1fa99 100755 --- a/app/admin/route/app.php +++ b/app/admin/route/app.php @@ -336,6 +336,8 @@ Route::rule('statistics_orderList', 'Statistics/goodsList', 'GET'); Route::rule('statistics_order', 'Statistics/orderList', 'GET'); //出货概览 Route::rule('statistics_productsOverview', 'Statistics/productsOverview', 'GET'); +//每日数据统计 +Route::rule('statistics_dailyStatistics', 'Statistics/getDailyStatistics', 'GET'); //利润支出 Route::rule('ProfitExpenses', 'Profit/index', 'GET'); diff --git a/app/admin/view/Statistics/userstatistics.html b/app/admin/view/Statistics/userstatistics.html index 797934d..756d76b 100755 --- a/app/admin/view/Statistics/userstatistics.html +++ b/app/admin/view/Statistics/userstatistics.html @@ -8,30 +8,34 @@
+ +