products_stats.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * 产品统计分析展示页面
  4. */
  5. require_once 'conn.php';
  6. require_once 'statistics_utils.php';
  7. require_once 'statistics_products.php';
  8. // 检查登录状态
  9. if (!isset($_SESSION['employee_id'])) {
  10. checkLogin();
  11. }
  12. // 获取日期范围参数
  13. $date_params = getDateRangeParams();
  14. $start_date = $date_params['start_date_sql'];
  15. $end_date = $date_params['end_date_sql'];
  16. $date_range = $date_params['date_range'];
  17. $period = $date_params['period'];
  18. // 特定产品过滤
  19. $product_id = isset($_GET['product_id']) ? intval($_GET['product_id']) : 0;
  20. // 页面头部
  21. include('header.php');
  22. ?>
  23. <div class="container-fluid px-4">
  24. <h1 class="mt-4">产品统计分析</h1>
  25. <ol class="breadcrumb mb-4">
  26. <li class="breadcrumb-item"><a href="index.php">控制台</a></li>
  27. <li class="breadcrumb-item active">产品统计</li>
  28. </ol>
  29. <!-- 日期筛选 -->
  30. <div class="card mb-4">
  31. <div class="card-header">
  32. <i class="fas fa-filter me-1"></i>
  33. 日期范围筛选
  34. </div>
  35. <div class="card-body">
  36. <form method="get" class="row g-3">
  37. <div class="col-md-3">
  38. <label for="date_range" class="form-label">选择日期范围</label>
  39. <select class="form-select" id="date_range" name="date_range" onchange="toggleCustomDates()">
  40. <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
  41. <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
  42. <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
  43. <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>最近30天</option>
  44. <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>最近90天</option>
  45. <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
  46. </select>
  47. </div>
  48. <div class="col-md-2 custom-date" style="display: <?php echo $date_range == 'custom' ? 'block' : 'none'; ?>">
  49. <label for="start_date" class="form-label">开始日期</label>
  50. <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
  51. </div>
  52. <div class="col-md-2 custom-date" style="display: <?php echo $date_range == 'custom' ? 'block' : 'none'; ?>">
  53. <label for="end_date" class="form-label">结束日期</label>
  54. <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
  55. </div>
  56. <div class="col-md-3">
  57. <label for="period" class="form-label">时间粒度</label>
  58. <select class="form-select" id="period" name="period">
  59. <option value="day" <?php echo $period == 'day' ? 'selected' : ''; ?>>日</option>
  60. <option value="week" <?php echo $period == 'week' ? 'selected' : ''; ?>>周</option>
  61. <option value="month" <?php echo $period == 'month' ? 'selected' : ''; ?>>月</option>
  62. </select>
  63. </div>
  64. <div class="col-md-2 d-flex align-items-end">
  65. <button type="submit" class="btn btn-primary">应用筛选</button>
  66. </div>
  67. </form>
  68. </div>
  69. </div>
  70. <div class="row">
  71. <!-- 热门产品 -->
  72. <div class="col-lg-12 mb-4">
  73. <div class="card">
  74. <?php
  75. $top_products = getTopProducts($conn, $start_date, $end_date, 10);
  76. renderTopProductsTable($top_products);
  77. ?>
  78. </div>
  79. </div>
  80. </div>
  81. <div class="row">
  82. <!-- 产品销售趋势 -->
  83. <div class="col-lg-12 mb-4">
  84. <div class="card">
  85. <?php
  86. $product_trends = getProductSalesTrend($conn, $start_date, $end_date, $product_id, $period);
  87. $time_labels = [];
  88. $quantities = [];
  89. $revenues = [];
  90. while ($row = $product_trends->fetch_assoc()) {
  91. $time_labels[] = $row['time_period'];
  92. $quantities[] = $row['total_quantity'];
  93. $revenues[] = $row['total_revenue'];
  94. }
  95. renderProductSalesTrendChart($time_labels, $quantities, $revenues);
  96. ?>
  97. </div>
  98. </div>
  99. </div>
  100. <div class="row">
  101. <!-- 产品类别销售分布 -->
  102. <div class="col-lg-12 mb-4">
  103. <div class="card">
  104. <?php
  105. $category_sales = getProductCategorySales($conn, $start_date, $end_date);
  106. $categories = [];
  107. $category_quantities = [];
  108. $category_revenues = [];
  109. while ($row = $category_sales->fetch_assoc()) {
  110. $categories[] = $row['category_name'];
  111. $category_quantities[] = $row['total_quantity'];
  112. $category_revenues[] = $row['total_revenue'];
  113. }
  114. renderProductCategorySalesChart($categories, $category_quantities, $category_revenues);
  115. ?>
  116. </div>
  117. </div>
  118. </div>
  119. <div class="row">
  120. <!-- 产品地区关联分析 -->
  121. <div class="col-lg-12 mb-4">
  122. <div class="card">
  123. <?php
  124. $product_region_data = getProductRegionAnalysis($conn, $start_date, $end_date);
  125. renderProductRegionAnalysisTable($product_region_data);
  126. ?>
  127. </div>
  128. </div>
  129. </div>
  130. </div>
  131. <script>
  132. function toggleCustomDates() {
  133. const dateRange = document.getElementById('date_range').value;
  134. const customDates = document.querySelectorAll('.custom-date');
  135. if (dateRange === 'custom') {
  136. customDates.forEach(el => el.style.display = 'block');
  137. } else {
  138. customDates.forEach(el => el.style.display = 'none');
  139. }
  140. }
  141. </script>
  142. <?php
  143. // 页面底部
  144. include('footer.php');
  145. ?>