prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
/**
* 获取订单转化率统计
*/
function getOrderConversionStats($conn, $start_date, $end_date) {
$sql = "SELECT
order_status,
COUNT(*) as count,
SUM(total_amount) as amount
FROM orders
WHERE order_date BETWEEN ? AND ?
GROUP BY order_status";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
return $stmt->get_result();
}
/**
* 获取产品类别销售统计
*/
function getProductCategorySales($conn, $start_date, $end_date) {
$sql = "SELECT
pc.name as category_name,
COUNT(DISTINCT o.id) as order_count,
SUM(oi.quantity) as total_quantity,
SUM(oi.total_price) as total_revenue
FROM orders o
JOIN order_items oi ON o.id = oi.order_id
JOIN products p ON oi.product_id = p.id
JOIN product_categories pc ON p.category_id = pc.id
WHERE o.order_date BETWEEN ? AND ?
AND o.order_status != 0
GROUP BY pc.id
ORDER BY total_revenue DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
return $stmt->get_result();
}
/**
* 获取客户地区分布
*/
function getCustomerDistribution($conn, $start_date, $end_date) {
$sql = "SELECT
c.countryName as region,
COUNT(DISTINCT o.customer_id) as customer_count,
COUNT(o.id) as order_count,
SUM(o.total_amount) as total_revenue
FROM orders o
JOIN customer cu ON o.customer_id = cu.id
JOIN country c ON cu.cs_country = c.id
WHERE o.order_date BETWEEN ? AND ?
AND o.order_status != 0
GROUP BY c.id
ORDER BY total_revenue DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
return $stmt->get_result();
}
/**
* 获取销售员业绩统计
*/
function getEmployeePerformance($conn, $start_date, $end_date) {
$sql = "SELECT
e.em_user as employee_name,
COUNT(DISTINCT o.id) as order_count,
COUNT(DISTINCT o.customer_id) as customer_count,
SUM(o.total_amount) as total_revenue,
AVG(o.total_amount) as avg_order_value
FROM orders o
JOIN employee e ON o.employee_id = e.id
WHERE o.order_date BETWEEN ? AND ?
AND o.order_status != 0
GROUP BY e.id
ORDER BY total_revenue DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
return $stmt->get_result();
}
/**
* 获取支付状态统计
*/
function getPaymentStatusStats($conn, $start_date, $end_date) {
$sql = "SELECT
payment_status,
COUNT(*) as count,
SUM(total_amount) as amount
FROM orders
WHERE order_date BETWEEN ? AND ?
AND order_status != 0
GROUP BY payment_status";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
return $stmt->get_result();
}
/**
* 获取每月销售趋势
*
* @param mysqli $conn 数据库连接
* @param string $start_date 开始日期
* @param string $end_date 结束日期
* @return mysqli_result 月度销售数据结果集
*/
function getMonthlySalesTrend($conn, $start_date, $end_date) {
$sql = "SELECT
DATE_FORMAT(order_date, '%Y-%m') as month,
COUNT(DISTINCT id) as orders,
SUM(total_amount) as revenue,
COUNT(DISTINCT customer_id) as unique_customers
FROM orders
WHERE order_date BETWEEN ? AND ?
AND order_status != 0
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();
}
/**
* 获取详细时间段订单趋势
*
* @param mysqli $conn 数据库连接
* @param string $start_date 开始日期
* @param string $end_date 结束日期
* @param string $period 时间粒度 (day/week/month)
* @return mysqli_result 订单趋势数据结果集
*/
function getDetailedOrderTrend($conn, $start_date, $end_date, $period = 'day') {
$groupFormat = '%Y-%m-%d';
if ($period == 'week') {
$groupFormat = '%x-W%v';
} else if ($period == 'month') {
$groupFormat = '%Y-%m';
}
$sql = "SELECT
DATE_FORMAT(o.order_date, '$groupFormat') as time_period,
COUNT(DISTINCT o.id) as order_count,
SUM(oi.quantity) as total_quantity,
SUM(o.total_amount) as total_amount,
COUNT(DISTINCT o.customer_id) as unique_customers
FROM orders o
LEFT JOIN order_items oi ON o.id = oi.order_id
WHERE o.order_date BETWEEN ? AND ?
AND o.order_status != 0
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();
}
/**
* 渲染销售概览卡片
*
* @param array $sales_overview 销售概览数据
* @return void
*/
function renderSalesOverviewCards($sales_overview) {
// 添加空值检查函数
?>
'待确认',
2 => '已确认',
3 => '生产中',
4 => '已发货',
5 => '已完成',
0 => '已取消'
];
$total_orders = 0;
$data = [];
while ($row = $conversion_stats->fetch_assoc()) {
$total_orders += $row['count'];
$data[$row['order_status']] = $row;
}
?>
订单状态 |
订单数 |
转化率 |
金额 |
$status_name): ?>
|
|
% |
|
fetch_assoc()) {
$labels[] = $row['category_name'];
$quantities[] = $row['total_quantity'];
$revenues[] = $row['total_revenue'];
}
?>
产品类别 |
订单数 |
销售数量 |
销售金额 |
data_seek(0);
while ($row = $category_sales->fetch_assoc()):
?>
|
|
|
¥ |
fetch_assoc()) {
$regions[] = $row['region'];
$customers[] = $row['customer_count'];
$revenues[] = $row['total_revenue'];
}
?>
地区 |
客户数 |
订单数 |
销售金额 |
data_seek(0);
while ($row = $customer_distribution->fetch_assoc()):
?>
|
|
|
¥ |
销售员 |
订单数 |
客户数 |
总销售额 |
平均订单金额 |
fetch_assoc()): ?>
|
|
|
|
|
'未付款',
1 => '部分付款',
2 => '已付清'
];
$data = [];
while ($row = $payment_stats->fetch_assoc()) {
$data[$row['payment_status']] = $row;
}
?>
支付状态 |
订单数 |
订单金额 |
$status_name): ?>
|
|
¥ |
query($sql);
return $result->fetch_all(MYSQLI_ASSOC);
}
/**
* 获取业务员详细信息
*/
function getEmployeeDetail($conn, $employee_id) {
$sql = "SELECT id, em_user, em_email, em_tel FROM employee WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $employee_id);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
/**
* 获取业务员统计数据
*/
function getEmployeeStats($conn, $employee_id, $start_date, $end_date) {
$sql = "SELECT
COUNT(DISTINCT o.id) as total_orders,
SUM(o.total_amount) as total_revenue,
COUNT(DISTINCT o.customer_id) as customer_count,
AVG(o.total_amount) as avg_order_value,
SUM(CASE WHEN o.order_status = 5 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) as completion_rate
FROM orders o
WHERE o.employee_id = ?
AND o.order_date BETWEEN ? AND ?
AND o.order_status != 0";
$stmt = $conn->prepare($sql);
$stmt->bind_param("iss", $employee_id, $start_date, $end_date);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
/**
* 渲染业务员销售趋势
*/
function renderEmployeeSalesTrend($conn, $employee_id, $start_date, $end_date) {
$sql = "SELECT
DATE_FORMAT(order_date, '%Y-%m-%d') as date,
COUNT(DISTINCT id) as orders,
SUM(total_amount) as revenue
FROM orders
WHERE employee_id = ?
AND order_date BETWEEN ? AND ?
AND order_status != 0
GROUP BY DATE_FORMAT(order_date, '%Y-%m-%d')
ORDER BY date";
$stmt = $conn->prepare($sql);
$stmt->bind_param("iss", $employee_id, $start_date, $end_date);
$stmt->execute();
$result = $stmt->get_result();
$dates = [];
$orders = [];
$revenues = [];
while ($row = $result->fetch_assoc()) {
$dates[] = $row['date'];
$orders[] = $row['orders'];
$revenues[] = $row['revenue'];
}
?>
prepare($sql);
$stmt->bind_param("iss", $employee_id, $start_date, $end_date);
$stmt->execute();
$result = $stmt->get_result();
$regions = [];
$customers = [];
$revenues = [];
while ($row = $result->fetch_assoc()) {
$regions[] = $row['region'];
$customers[] = $row['customer_count'];
$revenues[] = $row['total_revenue'];
}
?>
prepare($sql);
$stmt->bind_param("iss", $employee_id, $start_date, $end_date);
$stmt->execute();
$result = $stmt->get_result();
$categories = [];
$quantities = [];
$revenues = [];
while ($row = $result->fetch_assoc()) {
$categories[] = $row['category_name'];
$quantities[] = $row['total_quantity'];
$revenues[] = $row['total_revenue'];
}
?>