From cbe90546db83efb3745471f4951c7fe5252ff1fc Mon Sep 17 00:00:00 2001 From: haoliang <821644@qq.com> Date: Tue, 19 May 2026 14:32:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=91=98=E5=B7=A5=E4=BA=A7?= =?UTF-8?q?=E9=87=8FSummary=20topSql=20CROSS=20JOIN+WorkerList=E6=94=B9LEF?= =?UTF-8?q?T=20JOIN=E5=85=A8=E5=91=98=E5=B7=A5+sortOrder=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CncRepository/Impl/DailyProductionRepository.cs | 8 ++++---- src/CncRepository/Interface/IDailyProductionRepository.cs | 2 +- src/CncService/Impl/ProductionService.cs | 4 ++-- src/CncService/Interface/IProductionService.cs | 2 +- src/CncWebApi/Controllers/ProductionController.cs | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/CncRepository/Impl/DailyProductionRepository.cs b/src/CncRepository/Impl/DailyProductionRepository.cs index b9a7ede..25d7db1 100644 --- a/src/CncRepository/Impl/DailyProductionRepository.cs +++ b/src/CncRepository/Impl/DailyProductionRepository.cs @@ -167,21 +167,21 @@ namespace CncRepository.Impl var result = conn.QuerySingleOrDefault(sql, new { Start = startDate, End = endDate }); if (result == null) return new WorkerProductionSummaryResponse(); if (result.ActiveWorkerCount > 0) result.AvgPerWorker = Math.Round((decimal)result.TotalQuantity / result.ActiveWorkerCount, 1); - string topSql = @"SELECT w.name FROM cnc_daily_production dp JOIN cnc_worker_machine wm ON dp.machine_id = wm.machine_id JOIN cnc_worker w ON wm.worker_id = w.id WHERE dp.production_date BETWEEN @Start AND @End GROUP BY w.id, w.name ORDER BY SUM(dp.end_total_count - dp.base_total_count) DESC LIMIT 1"; + string topSql = @"SELECT w.name FROM cnc_daily_production dp JOIN cnc_worker w ON dp.worker_id = w.id WHERE dp.production_date BETWEEN @Start AND @End GROUP BY w.id, w.name ORDER BY SUM(dp.end_total_count - dp.base_total_count) DESC LIMIT 1"; result.TopWorkerName = result.TotalQuantity > 0 ? (conn.ExecuteScalar(topSql, new { Start = startDate, End = endDate }) ?? "-") : "-"; return result; } } - public List GetWorkerList(DateTime startDate, DateTime endDate, int? workerId) + public List GetWorkerList(DateTime startDate, DateTime endDate, int? workerId, string sortOrder = "desc") { + var orderBy = string.Equals(sortOrder, "asc", StringComparison.OrdinalIgnoreCase) ? "ASC" : "DESC"; using (var conn = CreateConnection()) { - string sql = @"SELECT w.name AS WorkerName, COUNT(DISTINCT dp.machine_id) AS MachineCount, COUNT(DISTINCT dp.program_name) AS ProgramCount, COALESCE(SUM(dp.end_total_count - dp.base_total_count), 0) AS TotalQuantity FROM cnc_daily_production dp JOIN cnc_worker w ON dp.worker_id = w.id WHERE dp.production_date BETWEEN @Start AND @End AND dp.end_total_count > dp.base_total_count"; + string sql = $@"SELECT w.name AS WorkerName, COUNT(DISTINCT dp.machine_id) AS MachineCount, COUNT(DISTINCT dp.program_name) AS ProgramCount, COALESCE(SUM(dp.end_total_count - dp.base_total_count), 0) AS TotalQuantity FROM cnc_worker w LEFT JOIN cnc_daily_production dp ON dp.worker_id = w.id AND dp.production_date BETWEEN @Start AND @End AND dp.end_total_count > dp.base_total_count GROUP BY w.id, w.name ORDER BY TotalQuantity {orderBy}"; var parameters = new DynamicParameters(); parameters.Add("Start", startDate); parameters.Add("End", endDate); if (workerId.HasValue) { sql += " AND w.id = @WorkerId"; parameters.Add("WorkerId", workerId.Value); } - sql += " GROUP BY w.id, w.name HAVING COALESCE(SUM(dp.end_total_count - dp.base_total_count), 0) > 0 ORDER BY TotalQuantity DESC"; var items = conn.Query(sql, parameters).ToList(); if (items.Count > 0) { int grandTotal = items.Sum(x => x.TotalQuantity); for (int i = 0; i < items.Count; i++) { items[i].Rank = i + 1; if (grandTotal > 0) items[i].Percentage = Math.Round((decimal)items[i].TotalQuantity / grandTotal * 100, 1); } } return items; diff --git a/src/CncRepository/Interface/IDailyProductionRepository.cs b/src/CncRepository/Interface/IDailyProductionRepository.cs index 97fbea2..2f982af 100644 --- a/src/CncRepository/Interface/IDailyProductionRepository.cs +++ b/src/CncRepository/Interface/IDailyProductionRepository.cs @@ -22,7 +22,7 @@ namespace CncRepository.Interface MachineProductionSummaryResponse GetMachineSummary(DateTime startDate, DateTime endDate, int? workshopId); List GetMachineList(DateTime startDate, DateTime endDate, int? workshopId, string machineIds); WorkerProductionSummaryResponse GetWorkerSummary(DateTime startDate, DateTime endDate); - List GetWorkerList(DateTime startDate, DateTime endDate, int? workerId); + List GetWorkerList(DateTime startDate, DateTime endDate, int? workerId, string sortOrder = "desc"); ProgramProductionSummaryResponse GetProgramSummary(DateTime startDate, DateTime endDate, int? workshopId); List GetProgramList(DateTime startDate, DateTime endDate, string programNames); } diff --git a/src/CncService/Impl/ProductionService.cs b/src/CncService/Impl/ProductionService.cs index fa32f47..bf19771 100644 --- a/src/CncService/Impl/ProductionService.cs +++ b/src/CncService/Impl/ProductionService.cs @@ -130,11 +130,11 @@ namespace CncService.Impl } /// - public List GetWorkerList(DateTime? startDate, DateTime? endDate, int? workerId) + public List GetWorkerList(DateTime? startDate, DateTime? endDate, int? workerId, string sortOrder = "desc") { var s = startDate ?? DateTime.Today; var e = endDate ?? DateTime.Today; - return _dailyProductionRepository.GetWorkerList(s, e, workerId); + return _dailyProductionRepository.GetWorkerList(s, e, workerId, sortOrder); } /// diff --git a/src/CncService/Interface/IProductionService.cs b/src/CncService/Interface/IProductionService.cs index 37c9249..6bde54a 100644 --- a/src/CncService/Interface/IProductionService.cs +++ b/src/CncService/Interface/IProductionService.cs @@ -37,7 +37,7 @@ namespace CncService.Interface WorkerProductionSummaryResponse GetWorkerSummary(DateTime? startDate, DateTime? endDate); /// 获取员工产量明细列表(按日期范围) - List GetWorkerList(DateTime? startDate, DateTime? endDate, int? workerId); + List GetWorkerList(DateTime? startDate, DateTime? endDate, int? workerId, string sortOrder = "desc"); /// 获取程序产量汇总(按日期范围) ProgramProductionSummaryResponse GetProgramSummary(DateTime? startDate, DateTime? endDate, int? workshopId); diff --git a/src/CncWebApi/Controllers/ProductionController.cs b/src/CncWebApi/Controllers/ProductionController.cs index f67c49c..0a0160d 100644 --- a/src/CncWebApi/Controllers/ProductionController.cs +++ b/src/CncWebApi/Controllers/ProductionController.cs @@ -127,9 +127,9 @@ namespace CncWebApi.Controllers /// [HttpGet] [Route("worker/list")] - public IHttpActionResult GetWorkerList(DateTime? startDate = null, DateTime? endDate = null, int? workerId = null) + public IHttpActionResult GetWorkerList(DateTime? startDate = null, DateTime? endDate = null, int? workerId = null, string sortOrder = "desc") { - var result = _productionService.GetWorkerList(startDate, endDate, workerId); + var result = _productionService.GetWorkerList(startDate, endDate, workerId, sortOrder); return Ok(ApiResponse.Success(new { items = result })); }