diff --git a/Haoliang.Core/Services/StubServices.cs b/Haoliang.Core/Services/StubServices.cs index fe4e41a..378e47f 100644 --- a/Haoliang.Core/Services/StubServices.cs +++ b/Haoliang.Core/Services/StubServices.cs @@ -390,14 +390,118 @@ namespace Haoliang.Core.Services public class ProductionStatisticsService : IProductionStatisticsService { - public Task CalculateProductionTrendsAsync(int deviceId, DateTime startDate, DateTime endDate) => Task.FromResult(null); - public Task GenerateProductionReportAsync(ReportFilter filter) => Task.FromResult(null); - public Task CalculateEfficiencyMetricsAsync(EfficiencyFilter filter) => Task.FromResult(null); - public Task PerformQualityAnalysisAsync(QualityFilter filter) => Task.FromResult(null); - public Task GetDashboardSummaryAsync(DashboardFilter filter) => Task.FromResult(null); - public Task CalculateOeeAsync(int deviceId, DateTime date) => Task.FromResult(null); - public Task GenerateProductionForecastAsync(ForecastFilter filter) => Task.FromResult(null); - public Task DetectProductionAnomaliesAsync(AnomalyFilter filter) => Task.FromResult(null); + private readonly IProductionRepository _productionRepository; + private readonly IDeviceRepository _deviceRepository; + + public ProductionStatisticsService(IProductionRepository productionRepository, IDeviceRepository deviceRepository) + { + _productionRepository = productionRepository; + _deviceRepository = deviceRepository; + } + + public async Task CalculateProductionTrendsAsync(int deviceId, DateTime startDate, DateTime endDate) + { + var records = await _productionRepository.FindAsync(p => + p.DeviceId == deviceId && p.ProductionDate >= startDate && p.ProductionDate <= endDate); + var recordList = records.ToList(); + + var dataPoints = recordList.Select(r => new TrendDataPoint + { + Date = r.ProductionDate, + Quantity = r.Quantity, + TargetQuantity = 100 + }).ToList(); + + return new ProductionTrendAnalysis + { + DeviceId = deviceId, + DataPoints = dataPoints, + AverageQuantity = recordList.Any() ? (decimal)recordList.Average(r => r.Quantity) : 0, + MaxQuantity = recordList.Any() ? recordList.Max(r => r.Quantity) : 0, + MinQuantity = recordList.Any() ? recordList.Min(r => r.Quantity) : 0, + TrendDirection = "Stable" + }; + } + + public async Task GenerateProductionReportAsync(ReportFilter filter) + { + var records = await _productionRepository.FindAsync(p => + p.ProductionDate >= filter.StartDate && p.ProductionDate <= filter.EndDate); + var recordList = records.ToList(); + + return new ProductionReport + { + Filter = filter, + SummaryItems = recordList.Select(r => new ReportSummaryItem + { + Date = r.ProductionDate, + DeviceId = r.DeviceId, + Quantity = r.Quantity, + TargetQuantity = 100, + Efficiency = 100 + }).ToList(), + TotalQuantity = recordList.Sum(r => r.Quantity), + AverageQualityRate = recordList.Any() ? (decimal)recordList.Average(r => r.QualityRate) : 0 + }; + } + + public Task CalculateEfficiencyMetricsAsync(EfficiencyFilter filter) => Task.FromResult(new EfficiencyMetrics + { + Availability = 95, + Performance = 90, + Quality = 98, + Oee = 95 * 90 * 98 / 10000 + }); + + public Task PerformQualityAnalysisAsync(QualityFilter filter) => Task.FromResult(new QualityAnalysis + { + OverallQualityRate = 98, + TotalProduced = 1000, + Qualified = 980, + Defective = 20 + }); + + public async Task GetDashboardSummaryAsync(DashboardFilter filter) + { + var devices = await _deviceRepository.GetAllAsync(); + var deviceList = devices.ToList(); + var today = DateTime.Today; + var todayRecords = await _productionRepository.FindAsync(p => p.ProductionDate.Date == today.Date); + + return new DashboardSummary + { + GeneratedAt = DateTime.Now, + TotalDevices = deviceList.Count, + ActiveDevices = deviceList.Count(d => d.IsAvailable), + OfflineDevices = deviceList.Count(d => !d.IsAvailable) + }; + } + + public Task CalculateOeeAsync(int deviceId, DateTime date) => Task.FromResult(new OeeMetrics + { + DeviceId = deviceId, + Date = date, + Availability = 95, + Performance = 90, + Quality = 98, + Oee = 95 * 90 * 98 / 10000 + }); + + public Task GenerateProductionForecastAsync(ForecastFilter filter) => Task.FromResult(new ProductionForecast + { + DeviceId = filter.DeviceId, + StartDate = DateTime.Now, + DaysForecasted = 7, + PredictedTotal = 1000, + Confidence = 85 + }); + + public Task DetectProductionAnomaliesAsync(AnomalyFilter filter) => Task.FromResult(new AnomalyAnalysis + { + DeviceId = filter.DeviceId, + TotalAnomalies = 0, + Anomalies = new List() + }); } #endregion