Browse Source

fleat: update statistics

igb 3 days ago
parent
commit
7c75cf5c85
1 changed files with 42 additions and 114 deletions
  1. 42 114
      statistics_order_warnings.php

+ 42 - 114
statistics_order_warnings.php

@@ -953,7 +953,7 @@ document.addEventListener('DOMContentLoaded', function() {
  */
 function getDecreasingOrderAmountCustomers($conn, $current_start, $current_end, $previous_start, $previous_end, $threshold, $count_only = false, $selected_employee = 0) {
     // 构建业务员筛选条件
-    $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = ?" : "";
+    $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = " . intval($selected_employee) : "";
     
     // 如果只需要计数
     if ($count_only) {
@@ -962,30 +962,21 @@ function getDecreasingOrderAmountCustomers($conn, $current_start, $current_end,
         LEFT JOIN (
             SELECT customer_id, SUM(total_amount) as amount
             FROM orders
-            WHERE order_date BETWEEN ? AND ?
+            WHERE order_date BETWEEN '{$current_start}' AND '{$current_end}'
             GROUP BY customer_id
         ) current_period ON c.id = current_period.customer_id
         LEFT JOIN (
             SELECT customer_id, SUM(total_amount) as amount
             FROM orders
-            WHERE order_date BETWEEN ? AND ?
+            WHERE order_date BETWEEN '{$previous_start}' AND '{$previous_end}'
             GROUP BY customer_id
         ) previous_period ON c.id = previous_period.customer_id
         JOIN employee e ON c.cs_belong = e.id
         WHERE previous_period.amount > 0
-        AND (current_period.amount IS NULL OR (current_period.amount / previous_period.amount - 1) * 100 <= ?)
-        AND c.cs_deal = 3" . $employee_filter;
+        AND (current_period.amount IS NULL OR (current_period.amount / previous_period.amount - 1) * 100 <= {$threshold})
+        AND c.cs_deal = 3{$employee_filter}";
         
-        $stmt = $conn->prepare($sql);
-        
-        if ($selected_employee > 0) {
-            $stmt->bind_param("ssssdi", $current_start, $current_end, $previous_start, $previous_end, $threshold, $selected_employee);
-        } else {
-            $stmt->bind_param("ssssd", $current_start, $current_end, $previous_start, $previous_end, $threshold);
-        }
-        
-        $stmt->execute();
-        $result = $stmt->get_result();
+        $result = $conn->query($sql);
         $row = $result->fetch_assoc();
         return $row['count'];
     }
@@ -1002,31 +993,22 @@ function getDecreasingOrderAmountCustomers($conn, $current_start, $current_end,
     LEFT JOIN (
         SELECT customer_id, SUM(total_amount) as amount
         FROM orders
-        WHERE order_date BETWEEN ? AND ?
+        WHERE order_date BETWEEN '{$current_start}' AND '{$current_end}'
         GROUP BY customer_id
     ) current_period ON c.id = current_period.customer_id
     LEFT JOIN (
         SELECT customer_id, SUM(total_amount) as amount
         FROM orders
-        WHERE order_date BETWEEN ? AND ?
+        WHERE order_date BETWEEN '{$previous_start}' AND '{$previous_end}'
         GROUP BY customer_id
     ) previous_period ON c.id = previous_period.customer_id
     JOIN employee e ON c.cs_belong = e.id
     WHERE previous_period.amount > 0
-    AND (current_period.amount IS NULL OR (current_period.amount / previous_period.amount - 1) * 100 <= ?)
-    AND c.cs_deal = 3" . $employee_filter . "
+    AND (current_period.amount IS NULL OR (current_period.amount / previous_period.amount - 1) * 100 <= {$threshold})
+    AND c.cs_deal = 3{$employee_filter}
     ORDER BY (current_period.amount / previous_period.amount) ASC";
     
-    $stmt = $conn->prepare($sql);
-    
-    if ($selected_employee > 0) {
-        $stmt->bind_param("ssssdi", $current_start, $current_end, $previous_start, $previous_end, $threshold, $selected_employee);
-    } else {
-        $stmt->bind_param("ssssd", $current_start, $current_end, $previous_start, $previous_end, $threshold);
-    }
-    
-    $stmt->execute();
-    return $stmt->get_result();
+    return $conn->query($sql);
 }
 
 /**
@@ -1034,7 +1016,7 @@ function getDecreasingOrderAmountCustomers($conn, $current_start, $current_end,
  */
 function getAbnormalRepurchaseCycleCustomers($conn, $current_start, $current_end, $threshold, $count_only = false, $selected_employee = 0, $page = 1, $page_size = 10) {
     // 构建业务员筛选条件
-    $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = ?" : "";
+    $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = " . intval($selected_employee) : "";
     
     if ($count_only) {
         $sql = "SELECT COUNT(DISTINCT c.id) as count
@@ -1048,19 +1030,10 @@ function getAbnormalRepurchaseCycleCustomers($conn, $current_start, $current_end
         WHERE c.cs_deal = 3
         AND (
             last_orders.last_order_date IS NULL 
-            OR DATEDIFF(?, last_orders.last_order_date) > ?
-        )" . $employee_filter;
-        
-        $stmt = $conn->prepare($sql);
-        
-        if ($selected_employee > 0) {
-            $stmt->bind_param("sii", $current_end, $threshold, $selected_employee);
-        } else {
-            $stmt->bind_param("si", $current_end, $threshold);
-        }
+            OR DATEDIFF('{$current_end}', last_orders.last_order_date) > {$threshold}
+        ){$employee_filter}";
         
-        $stmt->execute();
-        $result = $stmt->get_result();
+        $result = $conn->query($sql);
         $row = $result->fetch_assoc();
         return $row['count'];
     }
@@ -1072,8 +1045,8 @@ function getAbnormalRepurchaseCycleCustomers($conn, $current_start, $current_end
         c.cs_company,
         last_orders.last_order_date,
         CASE 
-            WHEN last_orders.last_order_date IS NULL THEN DATEDIFF(?, c.cs_addtime)
-            ELSE DATEDIFF(?, last_orders.last_order_date)
+            WHEN last_orders.last_order_date IS NULL THEN DATEDIFF('{$current_end}', c.cs_addtime)
+            ELSE DATEDIFF('{$current_end}', last_orders.last_order_date)
         END as inactive_days,
         IFNULL(order_stats.order_count, 0) as order_count,
         IFNULL(order_stats.total_amount, 0) as total_amount,
@@ -1093,21 +1066,12 @@ function getAbnormalRepurchaseCycleCustomers($conn, $current_start, $current_end
     WHERE c.cs_deal = 3
     AND (
         last_orders.last_order_date IS NULL 
-        OR DATEDIFF(?, last_orders.last_order_date) > ?
-    )" . $employee_filter . "
+        OR DATEDIFF('{$current_end}', last_orders.last_order_date) > {$threshold}
+    ){$employee_filter}
     ORDER BY inactive_days DESC
-    LIMIT ?, ?";
-    
-    $stmt = $conn->prepare($sql);
-    
-    if ($selected_employee > 0) {
-        $stmt->bind_param("sssiii", $current_end, $current_end, $current_end, $threshold, $selected_employee, $offset, $page_size);
-    } else {
-        $stmt->bind_param("sssiii", $current_end, $current_end, $current_end, $threshold, $offset, $page_size);
-    }
+    LIMIT {$offset}, {$page_size}";
     
-    $stmt->execute();
-    return $stmt->get_result();
+    return $conn->query($sql);
 }
 
 /**
@@ -1115,35 +1079,28 @@ function getAbnormalRepurchaseCycleCustomers($conn, $current_start, $current_end
  */
 function getInactiveCustomers($conn, $end_date, $inactive_days, $count_only = false, $page = 1, $page_size = 10, $selected_employee = 0) {
     // 构建业务员筛选条件
-    $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = ?" : "";
+    $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = " . intval($selected_employee) : "";
     
     if ($count_only) {
         $sql = "SELECT COUNT(*) as count
         FROM customer c
         JOIN employee e ON c.cs_belong = e.id
         WHERE c.cs_deal = 3
-        AND DATEDIFF(?, c.cs_updatetime) > ?" . $employee_filter;
-        
-        $stmt = $conn->prepare($sql);
+        AND DATEDIFF('{$end_date}', c.cs_updatetime) > {$inactive_days}{$employee_filter}";
         
-        if ($selected_employee > 0) {
-            $stmt->bind_param("sii", $end_date, $inactive_days, $selected_employee);
-        } else {
-            $stmt->bind_param("si", $end_date, $inactive_days);
-        }
-        
-        $stmt->execute();
-        $result = $stmt->get_result();
+        $result = $conn->query($sql);
         $row = $result->fetch_assoc();
         return $row['count'];
     }
     
+    $offset = ($page - 1) * $page_size;
+    
     $sql = "SELECT 
         c.id,
         c.cs_company,
         c.cs_code,
         last_orders.last_order_date,
-        DATEDIFF(?, c.cs_updatetime) as inactive_days,
+        DATEDIFF('{$end_date}', c.cs_updatetime) as inactive_days,
         c.cs_updatetime as last_update_time,
         IFNULL(order_stats.order_count, 0) as order_count,
         IFNULL(order_stats.total_amount, 0) as total_amount,
@@ -1161,22 +1118,11 @@ function getInactiveCustomers($conn, $end_date, $inactive_days, $count_only = fa
     ) order_stats ON c.id = order_stats.customer_id
     JOIN employee e ON c.cs_belong = e.id
     WHERE c.cs_deal = 3
-    AND DATEDIFF(?, c.cs_updatetime) > ?" . $employee_filter . "
+    AND DATEDIFF('{$end_date}', c.cs_updatetime) > {$inactive_days}{$employee_filter}
     ORDER BY inactive_days DESC
-    LIMIT ?, ?";
-    
-    $offset = ($page - 1) * $page_size;
-    
-    $stmt = $conn->prepare($sql);
+    LIMIT {$offset}, {$page_size}";
     
-    if ($selected_employee > 0) {
-        $stmt->bind_param("ssiiii", $end_date, $end_date, $inactive_days, $selected_employee, $offset, $page_size);
-    } else {
-        $stmt->bind_param("ssiii", $end_date, $end_date, $inactive_days, $offset, $page_size);
-    }
-    
-    $stmt->execute();
-    return $stmt->get_result();
+    return $conn->query($sql);
 }
 
 /**
@@ -1184,7 +1130,7 @@ function getInactiveCustomers($conn, $end_date, $inactive_days, $count_only = fa
  */
 function getChurnCustomers($conn, $end_date, $churn_days, $count_only = false, $page = 1, $page_size = 10, $selected_employee = 0) {
     // 构建业务员筛选条件
-    $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = ?" : "";
+    $employee_filter = $selected_employee > 0 ? " AND c.cs_belong = " . intval($selected_employee) : "";
     
     if ($count_only) {
         $sql = "SELECT COUNT(*) as count
@@ -1198,31 +1144,24 @@ function getChurnCustomers($conn, $end_date, $churn_days, $count_only = false, $
         WHERE c.cs_deal = 3
         AND (
             last_orders.last_order_date IS NULL 
-            OR DATEDIFF(?, last_orders.last_order_date) > ?
-        )" . $employee_filter;
-        
-        $stmt = $conn->prepare($sql);
-        
-        if ($selected_employee > 0) {
-            $stmt->bind_param("sii", $end_date, $churn_days, $selected_employee);
-        } else {
-            $stmt->bind_param("si", $end_date, $churn_days);
-        }
+            OR DATEDIFF('{$end_date}', last_orders.last_order_date) > {$churn_days}
+        ){$employee_filter}";
         
-        $stmt->execute();
-        $result = $stmt->get_result();
+        $result = $conn->query($sql);
         $row = $result->fetch_assoc();
         return $row['count'];
     }
     
+    $offset = ($page - 1) * $page_size;
+    
     $sql = "SELECT 
         c.id,
         c.cs_company,
         c.cs_code,
         last_orders.last_order_date,
         CASE 
-            WHEN last_orders.last_order_date IS NULL THEN DATEDIFF(?, c.cs_addtime)
-            ELSE DATEDIFF(?, last_orders.last_order_date)
+            WHEN last_orders.last_order_date IS NULL THEN DATEDIFF('{$end_date}', c.cs_addtime)
+            ELSE DATEDIFF('{$end_date}', last_orders.last_order_date)
         END as inactive_days,
         IFNULL(order_stats.order_count, 0) as order_count,
         IFNULL(order_stats.total_amount, 0) as total_amount,
@@ -1242,23 +1181,12 @@ function getChurnCustomers($conn, $end_date, $churn_days, $count_only = false, $
     WHERE c.cs_deal = 3
     AND (
         last_orders.last_order_date IS NULL 
-        OR DATEDIFF(?, last_orders.last_order_date) > ?
-    )" . $employee_filter . "
+        OR DATEDIFF('{$end_date}', last_orders.last_order_date) > {$churn_days}
+    ){$employee_filter}
     ORDER BY inactive_days DESC
-    LIMIT ?, ?";
-    
-    $offset = ($page - 1) * $page_size;
-    
-    $stmt = $conn->prepare($sql);
-    
-    if ($selected_employee > 0) {
-        $stmt->bind_param("sssiiiii", $end_date, $end_date, $end_date, $churn_days, $selected_employee, $offset, $page_size);
-    } else {
-        $stmt->bind_param("sssiii", $end_date, $end_date, $end_date, $churn_days, $offset, $page_size);
-    }
+    LIMIT {$offset}, {$page_size}";
     
-    $stmt->execute();
-    return $stmt->get_result();
+    return $conn->query($sql);
 }
 
 // 页面底部