prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
// 函数:获取每月销售趋势
function getMonthlySalesTrend($conn, $start_date, $end_date) {
$sql = "SELECT
DATE_FORMAT(order_date, '%Y-%m') as month,
COUNT(id) as orders,
SUM(total_amount) as revenue
FROM orders
WHERE order_date BETWEEN ? AND ?
GROUP BY DATE_FORMAT(order_date, '%Y-%m')
ORDER BY month";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
return $stmt->get_result();
}
// 函数:获取客户国家分布
function getCustomerCountryDistribution($conn) {
$sql = "SELECT
c.countryName,
COUNT(cu.id) as customer_count
FROM customer cu
JOIN country c ON cu.cs_country = c.id
GROUP BY cu.cs_country
ORDER BY customer_count DESC
LIMIT 10";
return $conn->query($sql);
}
// 函数:获取客户类型分布
function getCustomerTypeDistribution($conn) {
$sql = "SELECT
ct.businessType,
COUNT(c.id) as customer_count
FROM customer c
JOIN clienttype ct ON c.cs_type = ct.id
GROUP BY c.cs_type";
return $conn->query($sql);
}
// 函数:获取成交阶段分布
function getDealStageDistribution($conn) {
$sql = "SELECT
cs_deal,
CASE
WHEN cs_deal = 1 THEN '背景调查'
WHEN cs_deal = 2 THEN '明确需求'
WHEN cs_deal = 3 THEN '已成交'
ELSE '其他'
END as stage_name,
COUNT(id) as customer_count
FROM customer
GROUP BY cs_deal";
return $conn->query($sql);
}
// 函数:获取热门产品
function getTopProducts($conn, $start_date, $end_date, $limit = 5) {
$sql = "SELECT
p.ProductName,
SUM(oi.quantity) as total_quantity,
SUM(oi.total_price) as total_revenue
FROM order_items oi
JOIN products p ON oi.product_id = p.id
JOIN orders o ON oi.order_id = o.id
WHERE o.order_date BETWEEN ? AND ?
GROUP BY oi.product_id
ORDER BY total_revenue DESC
LIMIT ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssi", $start_date, $end_date, $limit);
$stmt->execute();
return $stmt->get_result();
}
// 函数:获取业务员销售业绩
function getEmployeeSalesPerformance($conn, $start_date, $end_date) {
$sql = "SELECT
e.em_user as employee_name,
COUNT(o.id) as order_count,
SUM(o.total_amount) as total_sales
FROM orders o
JOIN employee e ON o.employee_id = e.id
WHERE o.order_date BETWEEN ? AND ?
GROUP BY o.employee_id
ORDER BY total_sales DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
return $stmt->get_result();
}
// 函数:获取客户增长趋势
function getCustomerGrowthTrend($conn) {
$sql = "SELECT
DATE_FORMAT(cs_addtime, '%Y-%m') as month,
COUNT(id) as new_customers
FROM customer
WHERE cs_addtime >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
GROUP BY DATE_FORMAT(cs_addtime, '%Y-%m')
ORDER BY month";
return $conn->query($sql);
}
// 函数:获取不同地区的订单数量
function getOrdersByRegion($conn, $start_date, $end_date) {
$sql = "SELECT
c.countryName,
COUNT(o.id) as order_count,
SUM(o.total_amount) as total_amount,
SUM(oi.quantity) as total_quantity
FROM orders o
JOIN customer cu ON o.customer_id = cu.id
JOIN country c ON cu.cs_country = c.id
LEFT JOIN order_items oi ON o.id = oi.order_id
WHERE o.order_date BETWEEN ? AND ?
GROUP BY cu.cs_country
ORDER BY total_quantity DESC
LIMIT 10";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
return $stmt->get_result();
}
// 函数:获取详细时间段订单数量
function getDetailedOrderTrend($conn, $start_date, $end_date, $period = 'day') {
$groupFormat = '%Y-%m-%d';
$intervalUnit = 'DAY';
if ($period == 'week') {
$groupFormat = '%x-W%v'; // ISO year and week number
$intervalUnit = 'WEEK';
} else if ($period == 'month') {
$groupFormat = '%Y-%m';
$intervalUnit = 'MONTH';
}
$sql = "SELECT
DATE_FORMAT(o.order_date, '$groupFormat') as time_period,
COUNT(o.id) as order_count,
SUM(oi.quantity) as total_quantity
FROM orders o
LEFT JOIN order_items oi ON o.id = oi.order_id
WHERE o.order_date BETWEEN ? AND ?
GROUP BY time_period
ORDER BY MIN(o.order_date)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
return $stmt->get_result();
}
// 获取统计数据
$sales_overview = getSalesOverview($conn, $start_date_sql, $end_date_sql);
$monthly_sales = getMonthlySalesTrend($conn, $start_date_sql, $end_date_sql);
$country_distribution = getCustomerCountryDistribution($conn);
$customer_types = getCustomerTypeDistribution($conn);
$deal_stages = getDealStageDistribution($conn);
$top_products = getTopProducts($conn, $start_date_sql, $end_date_sql);
$employee_performance = getEmployeeSalesPerformance($conn, $start_date_sql, $end_date_sql);
$customer_growth = getCustomerGrowthTrend($conn);
$orders_by_region = getOrdersByRegion($conn, $start_date_sql, $end_date_sql);
// 获取详细时间段订单趋势 - 默认按日期
$period = isset($_GET['period']) ? $_GET['period'] : 'day';
$detailed_orders = getDetailedOrderTrend($conn, $start_date_sql, $end_date_sql, $period);
// 将月度销售数据转换为图表所需格式
$monthly_labels = [];
$monthly_orders = [];
$monthly_revenue = [];
while ($row = $monthly_sales->fetch_assoc()) {
$monthly_labels[] = $row['month'];
$monthly_orders[] = $row['orders'];
$monthly_revenue[] = $row['revenue'];
}
// 将国家分布数据转换为图表所需格式
$country_labels = [];
$country_data = [];
while ($row = $country_distribution->fetch_assoc()) {
$country_labels[] = $row['countryName'];
$country_data[] = $row['customer_count'];
}
// 将客户类型数据转换为图表所需格式
$type_labels = [];
$type_data = [];
while ($row = $customer_types->fetch_assoc()) {
$type_labels[] = $row['businessType'];
$type_data[] = $row['customer_count'];
}
// 将成交阶段数据转换为图表所需格式
$stage_labels = [];
$stage_data = [];
while ($row = $deal_stages->fetch_assoc()) {
$stage_labels[] = $row['stage_name'];
$stage_data[] = $row['customer_count'];
}
// 将客户增长数据转换为图表所需格式
$growth_labels = [];
$growth_data = [];
while ($row = $customer_growth->fetch_assoc()) {
$growth_labels[] = $row['month'];
$growth_data[] = $row['new_customers'];
}
// 将地区订单数据转换为图表所需格式
$region_labels = [];
$region_orders = [];
$region_quantities = [];
while ($row = $orders_by_region->fetch_assoc()) {
$region_labels[] = $row['countryName'];
$region_orders[] = $row['order_count'];
$region_quantities[] = $row['total_quantity'];
}
// 将详细时间订单数据转换为图表所需格式
$time_labels = [];
$time_orders = [];
$time_quantities = [];
while ($row = $detailed_orders->fetch_assoc()) {
$time_labels[] = $row['time_period'];
$time_orders[] = $row['order_count'];
$time_quantities[] = $row['total_quantity'];
}
?>
统计分析
产品名称 |
销售数量 |
销售收入 |
fetch_assoc()): ?>
|
|
¥ |
业务员姓名 |
订单数量 |
销售总额 |
fetch_assoc()): ?>
|
|
¥ |