修复员工产量Summary topSql CROSS JOIN+WorkerList改LEFT JOIN全员工+sortOrder排序

main
haoliang 1 month ago
parent 13e6f69bec
commit cbe90546db

@ -167,21 +167,21 @@ namespace CncRepository.Impl
var result = conn.QuerySingleOrDefault<WorkerProductionSummaryResponse>(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<string>(topSql, new { Start = startDate, End = endDate }) ?? "-") : "-";
return result;
}
}
public List<WorkerProductionListItem> GetWorkerList(DateTime startDate, DateTime endDate, int? workerId)
public List<WorkerProductionListItem> 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<WorkerProductionListItem>(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;

@ -22,7 +22,7 @@ namespace CncRepository.Interface
MachineProductionSummaryResponse GetMachineSummary(DateTime startDate, DateTime endDate, int? workshopId);
List<MachineProductionListItem> GetMachineList(DateTime startDate, DateTime endDate, int? workshopId, string machineIds);
WorkerProductionSummaryResponse GetWorkerSummary(DateTime startDate, DateTime endDate);
List<WorkerProductionListItem> GetWorkerList(DateTime startDate, DateTime endDate, int? workerId);
List<WorkerProductionListItem> GetWorkerList(DateTime startDate, DateTime endDate, int? workerId, string sortOrder = "desc");
ProgramProductionSummaryResponse GetProgramSummary(DateTime startDate, DateTime endDate, int? workshopId);
List<ProgramProductionListItem> GetProgramList(DateTime startDate, DateTime endDate, string programNames);
}

@ -130,11 +130,11 @@ namespace CncService.Impl
}
/// <inheritdoc/>
public List<WorkerProductionListItem> GetWorkerList(DateTime? startDate, DateTime? endDate, int? workerId)
public List<WorkerProductionListItem> 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);
}
/// <inheritdoc/>

@ -37,7 +37,7 @@ namespace CncService.Interface
WorkerProductionSummaryResponse GetWorkerSummary(DateTime? startDate, DateTime? endDate);
/// <summary>获取员工产量明细列表(按日期范围)</summary>
List<WorkerProductionListItem> GetWorkerList(DateTime? startDate, DateTime? endDate, int? workerId);
List<WorkerProductionListItem> GetWorkerList(DateTime? startDate, DateTime? endDate, int? workerId, string sortOrder = "desc");
/// <summary>获取程序产量汇总(按日期范围)</summary>
ProgramProductionSummaryResponse GetProgramSummary(DateTime? startDate, DateTime? endDate, int? workshopId);

@ -127,9 +127,9 @@ namespace CncWebApi.Controllers
/// </summary>
[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<object>.Success(new { items = result }));
}

Loading…
Cancel
Save