仪表盘查询优化:已汇总日期用daily_production,未汇总日期实时计算segment,车间产量/机床排行/工人排行/产量趋势均采用混合查询

main
haoliang 4 days ago
parent c836393d3f
commit 1c1698f9dd

@ -17,6 +17,32 @@ namespace CncRepository.Impl.Dashboard
{ {
public DashboardRepository(string connectionString) : base(connectionString) { } public DashboardRepository(string connectionString) : base(connectionString) { }
/// <summary>
/// 通用产量计算SQL片段已汇总日期用cnc_daily_production未汇总日期用cnc_production_segment实时计算。
/// 与machine表JOIN时需在外层提供 machine_id 列。
/// 参数:@StartDate, @EndDate
/// 返回列machine_id, production_date, day_quantity
/// </summary>
private const string DailyQuantityUnionSql = @"
SELECT machine_id, production_date, day_quantity FROM (
SELECT dp.machine_id, dp.production_date, SUM(dp.total_quantity) AS day_quantity
FROM cnc_daily_production dp
WHERE dp.production_date BETWEEN @StartDate AND @EndDate
GROUP BY dp.machine_id, dp.production_date
) summarized
UNION ALL
SELECT seg.machine_id, seg.production_date,
SUM(CASE WHEN seg.is_settled=1 THEN seg.quantity
ELSE COALESCE(seg.end_part_count, seg.start_part_count) - seg.start_part_count END) AS day_quantity
FROM cnc_production_segment seg
WHERE seg.production_date BETWEEN @StartDate AND @EndDate
AND NOT EXISTS (
SELECT 1 FROM cnc_daily_production dp
WHERE dp.machine_id = seg.machine_id AND dp.production_date = seg.production_date
)
GROUP BY seg.machine_id, seg.production_date
) all_days";
/// <summary>汇总卡片数据</summary> /// <summary>汇总卡片数据</summary>
public DashboardSummaryResponse GetSummary() public DashboardSummaryResponse GetSummary()
{ {
@ -24,7 +50,7 @@ namespace CncRepository.Impl.Dashboard
{ {
var onlineCount = conn.ExecuteScalar<int>(@"SELECT COUNT(1) FROM cnc_machine WHERE is_online = 1"); var onlineCount = conn.ExecuteScalar<int>(@"SELECT COUNT(1) FROM cnc_machine WHERE is_online = 1");
var totalMachines = conn.ExecuteScalar<int>(@"SELECT COUNT(1) FROM cnc_machine"); var totalMachines = conn.ExecuteScalar<int>(@"SELECT COUNT(1) FROM cnc_machine");
// 今日总产量:直接从产量分段实时计算 // 今日总产量:直接从产量分段实时计算(今日一定没有日终汇总)
var todayProduction = conn.ExecuteScalar<int>(@" var todayProduction = conn.ExecuteScalar<int>(@"
SELECT COALESCE(SUM(CASE WHEN is_settled=1 THEN quantity SELECT COALESCE(SUM(CASE WHEN is_settled=1 THEN quantity
ELSE COALESCE(end_part_count,start_part_count) - start_part_count END), 0) ELSE COALESCE(end_part_count,start_part_count) - start_part_count END), 0)
@ -68,26 +94,36 @@ namespace CncRepository.Impl.Dashboard
{ {
using (var conn = CreateConnection()) using (var conn = CreateConnection())
{ {
// 当查询范围包含今天时,使用产量分段实时数据;历史日期使用日终汇总
var today = DateTime.Today;
bool includeToday = endDate >= today;
DateTime histEnd = includeToday ? today.AddDays(-1) : endDate;
var sql = @" var sql = @"
SELECT w.Name AS WorkshopName, SELECT w.Name AS WorkshopName,
COALESCE(SUM(CASE WHEN is_settled=1 THEN dp.quantity COALESCE(SUM(ad.day_quantity), 0) AS Quantity,
ELSE COALESCE(dp.end_part_count,dp.start_part_count) - dp.start_part_count END), 0) AS Quantity,
COUNT(DISTINCT m.id) AS MachineCount, COUNT(DISTINCT m.id) AS MachineCount,
CASE CASE
WHEN DATEDIFF(@EndDate, @StartDate) = 0 THEN COALESCE(SUM(CASE WHEN is_settled=1 THEN dp.quantity WHEN DATEDIFF(@EndDate, @StartDate) = 0
ELSE COALESCE(dp.end_part_count,dp.start_part_count) - dp.start_part_count END), 0) / NULLIF(COUNT(DISTINCT m.id),0) THEN COALESCE(SUM(ad.day_quantity), 0) / NULLIF(COUNT(DISTINCT m.id), 0)
ELSE COALESCE(SUM(CASE WHEN is_settled=1 THEN dp.quantity ELSE COALESCE(SUM(ad.day_quantity), 0) / (DATEDIFF(@EndDate, @StartDate) + 1) / NULLIF(COUNT(DISTINCT m.id), 0)
ELSE COALESCE(dp.end_part_count,dp.start_part_count) - dp.start_part_count END), 0) / (DATEDIFF(@EndDate, @StartDate) + 1) / NULLIF(COUNT(DISTINCT m.id),0)
END AS AvgQuantity END AS AvgQuantity
FROM cnc_workshop w FROM cnc_workshop w
LEFT JOIN cnc_machine m ON m.workshop_id = w.id LEFT JOIN cnc_machine m ON m.workshop_id = w.id
LEFT JOIN cnc_production_segment dp ON dp.machine_id = m.id LEFT JOIN (
AND dp.production_date BETWEEN @StartDate AND @EndDate SELECT machine_id, production_date, day_quantity FROM (
SELECT dp.machine_id, dp.production_date, SUM(dp.total_quantity) AS day_quantity
FROM cnc_daily_production dp
WHERE dp.production_date BETWEEN @StartDate AND @EndDate
GROUP BY dp.machine_id, dp.production_date
) summarized
UNION ALL
SELECT seg.machine_id, seg.production_date,
SUM(CASE WHEN seg.is_settled=1 THEN seg.quantity
ELSE COALESCE(seg.end_part_count, seg.start_part_count) - seg.start_part_count END) AS day_quantity
FROM cnc_production_segment seg
WHERE seg.production_date BETWEEN @StartDate AND @EndDate
AND NOT EXISTS (
SELECT 1 FROM cnc_daily_production dp
WHERE dp.machine_id = seg.machine_id AND dp.production_date = seg.production_date
)
GROUP BY seg.machine_id, seg.production_date
) ad ON ad.machine_id = m.id
GROUP BY w.id, w.name"; GROUP BY w.id, w.name";
return conn.Query<WorkshopProductionResponse>(sql, new { StartDate = startDate, EndDate = endDate }).ToList(); return conn.Query<WorkshopProductionResponse>(sql, new { StartDate = startDate, EndDate = endDate }).ToList();
} }
@ -101,11 +137,27 @@ namespace CncRepository.Impl.Dashboard
var sql = @" var sql = @"
SELECT m.id AS MachineId, SELECT m.id AS MachineId,
m.name AS MachineName, m.name AS MachineName,
COALESCE(SUM(CASE WHEN dp.is_settled=1 THEN dp.quantity COALESCE(SUM(ad.day_quantity), 0) AS Quantity
ELSE COALESCE(dp.end_part_count,dp.start_part_count) - dp.start_part_count END), 0) AS Quantity
FROM cnc_machine m FROM cnc_machine m
LEFT JOIN cnc_production_segment dp ON dp.machine_id = m.id LEFT JOIN (
AND dp.production_date BETWEEN @StartDate AND @EndDate SELECT machine_id, production_date, day_quantity FROM (
SELECT dp.machine_id, dp.production_date, SUM(dp.total_quantity) AS day_quantity
FROM cnc_daily_production dp
WHERE dp.production_date BETWEEN @StartDate AND @EndDate
GROUP BY dp.machine_id, dp.production_date
) summarized
UNION ALL
SELECT seg.machine_id, seg.production_date,
SUM(CASE WHEN seg.is_settled=1 THEN seg.quantity
ELSE COALESCE(seg.end_part_count, seg.start_part_count) - seg.start_part_count END) AS day_quantity
FROM cnc_production_segment seg
WHERE seg.production_date BETWEEN @StartDate AND @EndDate
AND NOT EXISTS (
SELECT 1 FROM cnc_daily_production dp
WHERE dp.machine_id = seg.machine_id AND dp.production_date = seg.production_date
)
GROUP BY seg.machine_id, seg.production_date
) ad ON ad.machine_id = m.id
GROUP BY m.id, m.name GROUP BY m.id, m.name
ORDER BY Quantity DESC ORDER BY Quantity DESC
LIMIT @Top"; LIMIT @Top";
@ -120,12 +172,28 @@ namespace CncRepository.Impl.Dashboard
{ {
var sql = @" var sql = @"
SELECT w.name AS WorkerName, SELECT w.name AS WorkerName,
COALESCE(SUM(CASE WHEN dp.is_settled=1 THEN dp.quantity COALESCE(SUM(ad.day_quantity), 0) AS Quantity
ELSE COALESCE(dp.end_part_count,dp.start_part_count) - dp.start_part_count END), 0) AS Quantity
FROM cnc_worker w FROM cnc_worker w
LEFT JOIN cnc_worker_machine wm ON wm.worker_id = w.id LEFT JOIN cnc_worker_machine wm ON wm.worker_id = w.id
LEFT JOIN cnc_production_segment dp ON dp.machine_id = wm.machine_id LEFT JOIN (
AND dp.production_date BETWEEN @StartDate AND @EndDate SELECT machine_id, production_date, day_quantity FROM (
SELECT dp.machine_id, dp.production_date, SUM(dp.total_quantity) AS day_quantity
FROM cnc_daily_production dp
WHERE dp.production_date BETWEEN @StartDate AND @EndDate
GROUP BY dp.machine_id, dp.production_date
) summarized
UNION ALL
SELECT seg.machine_id, seg.production_date,
SUM(CASE WHEN seg.is_settled=1 THEN seg.quantity
ELSE COALESCE(seg.end_part_count, seg.start_part_count) - seg.start_part_count END) AS day_quantity
FROM cnc_production_segment seg
WHERE seg.production_date BETWEEN @StartDate AND @EndDate
AND NOT EXISTS (
SELECT 1 FROM cnc_daily_production dp
WHERE dp.machine_id = seg.machine_id AND dp.production_date = seg.production_date
)
GROUP BY seg.machine_id, seg.production_date
) ad ON ad.machine_id = wm.machine_id
GROUP BY w.id, w.name GROUP BY w.id, w.name
ORDER BY Quantity DESC ORDER BY Quantity DESC
LIMIT @Top"; LIMIT @Top";
@ -138,13 +206,28 @@ namespace CncRepository.Impl.Dashboard
{ {
using (var conn = CreateConnection()) using (var conn = CreateConnection())
{ {
var sql = @"SELECT production_date AS Date, // 已汇总日期用daily_production未汇总日期用segment实时计算
COALESCE(SUM(CASE WHEN is_settled=1 THEN quantity var sql = @"
ELSE COALESCE(end_part_count,start_part_count) - start_part_count END), 0) AS Quantity SELECT production_date AS Date, COALESCE(SUM(day_quantity), 0) AS Quantity
FROM cnc_production_segment FROM (
WHERE production_date >= DATE_SUB(CURDATE(), INTERVAL @Days DAY) SELECT dp.production_date, SUM(dp.total_quantity) AS day_quantity
GROUP BY production_date FROM cnc_daily_production dp
ORDER BY production_date ASC"; WHERE dp.production_date >= DATE_SUB(CURDATE(), INTERVAL @Days DAY)
GROUP BY dp.production_date
UNION ALL
SELECT seg.production_date,
SUM(CASE WHEN seg.is_settled=1 THEN seg.quantity
ELSE COALESCE(seg.end_part_count, seg.start_part_count) - seg.start_part_count END) AS day_quantity
FROM cnc_production_segment seg
WHERE seg.production_date >= DATE_SUB(CURDATE(), INTERVAL @Days DAY)
AND NOT EXISTS (
SELECT 1 FROM cnc_daily_production dp
WHERE dp.production_date = seg.production_date
)
GROUP BY seg.production_date
) all_days
GROUP BY production_date
ORDER BY production_date ASC";
return conn.Query(sql, new { Days = days }).ToList(); return conn.Query(sql, new { Days = days }).ToList();
} }
} }

Loading…
Cancel
Save