浏览代码

修复生成报表值为null与字典空Key调用

KWD 7 月之前
父节点
当前提交
ff1eb5f4c4

+ 1 - 1
TeamAAS-VM/Data/SystemDatabaseService.cs

@@ -528,7 +528,7 @@ namespace TeamAAS_VP.Data
             var monthStart = new DateTime(now.Year, now.Month, 1);
             var monthEnd = monthStart.AddMonths(1);
             var list = await _db.Queryable<ProductionRecord>()
-                .Where(r => r.ProductName == productName && r.Timestamp >= monthStart && r.Timestamp < monthEnd)
+                .Where(r => r.ProductName == productName && (r.Timestamp >= monthStart && r.Timestamp < monthEnd))
                 .ToListAsync();
             var dict = new Dictionary<DateTime, Dictionary<string, int>>();
             var days = list.GroupBy(r => r.Timestamp.Date).OrderBy(g => g.Key);

+ 58 - 23
TeamAAS-VM/ViewModels/Statistics/ProductionStatementViewModel.cs

@@ -35,7 +35,7 @@ namespace TeamAAS_VP.ViewModels.Statistics
         public Int64 TotalCount
         {
             get { return _TotalCount; }
-            set { SetProperty(ref _TotalCount,value); }
+            set { SetProperty(ref _TotalCount, value); }
         }
 
         private Int64 _MonthCount;
@@ -114,7 +114,7 @@ namespace TeamAAS_VP.ViewModels.Statistics
         public DelegateCommand LoadedCommand =>
             _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
 
-        
+
         #endregion
 
         #region 事件
@@ -123,7 +123,7 @@ namespace TeamAAS_VP.ViewModels.Statistics
 
         public ProductionStatementViewModel(IContainerProvider container, ISystemDatabaseService systemDatabaseService, IProductService productService)
         {
-            _container= container;
+            _container = container;
             _systemDatabaseService = systemDatabaseService;
             _productService = productService;
         }
@@ -133,24 +133,25 @@ namespace TeamAAS_VP.ViewModels.Statistics
         {
             var currentproduct = _productService.GetCurrentProduct();
             if (currentproduct == null) return;
-            await  App.Current.Dispatcher.InvokeAsync(new Action(async () => {
+            await App.Current.Dispatcher.InvokeAsync(new Action(async () =>
+            {
                 YieldChartModel = CreateYieldChartModel(await _systemDatabaseService.GetProductionByCategoriesAsync(currentproduct.Name));
-               TotalCount = await _systemDatabaseService.GetOverallProductionAsync(currentproduct.Name);
+                TotalCount = await _systemDatabaseService.GetOverallProductionAsync(currentproduct.Name);
                 MonthCount = await _systemDatabaseService.GetCurrentMonthProductionAsync(currentproduct.Name);
-               WeekCount = await _systemDatabaseService.GetCurrentWeekProductionAsync(currentproduct.Name);
-               DayCount = await _systemDatabaseService.GetTodayProductionAsync(currentproduct.Name);
-               MonthChartModel = CreateMonth(await _systemDatabaseService.GetMonthlyProductionByDayAndCategoryAsync(currentproduct.Name));
-               WeekChartModel = CreateWeek(await _systemDatabaseService.GetWeeklyProductionByDayAndCategoryAsync(currentproduct.Name));
-               DayChartModel = CreateDay(await _systemDatabaseService.GetDailyProductionByHourAndCategoryAsync(currentproduct.Name));
+                WeekCount = await _systemDatabaseService.GetCurrentWeekProductionAsync(currentproduct.Name);
+                DayCount = await _systemDatabaseService.GetTodayProductionAsync(currentproduct.Name);
+                MonthChartModel = CreateMonth(await _systemDatabaseService.GetMonthlyProductionByDayAndCategoryAsync(currentproduct.Name));
+                WeekChartModel = CreateWeek(await _systemDatabaseService.GetWeeklyProductionByDayAndCategoryAsync(currentproduct.Name));
+                DayChartModel = CreateDay(await _systemDatabaseService.GetDailyProductionByHourAndCategoryAsync(currentproduct.Name));
             }));
-            
+
         }
 
         /// <summary>
         /// 创建产量图表模型
         /// </summary>
         /// <returns></returns>
-        private PlotModel CreateYieldChartModel(Dictionary<string,int> Items)
+        private PlotModel CreateYieldChartModel(Dictionary<string, int> Items)
         {
             List<PieSlice> PieSlices = new List<PieSlice>();
             PieSlices = new List<PieSlice>();
@@ -174,7 +175,7 @@ namespace TeamAAS_VP.ViewModels.Statistics
             }
 
             model.Series.Add(series);
-            
+
             return model;
         }
 
@@ -201,8 +202,8 @@ namespace TeamAAS_VP.ViewModels.Statistics
             var ay1 = new LinearAxis()
             {
                 Key = "y1",
-                Title= Lang.数量,
-                TitlePosition=1,
+                Title = Lang.数量,
+                TitlePosition = 1,
                 Minimum = 0,
                 Position = AxisPosition.Left,
                 IsZoomEnabled = false,
@@ -213,13 +214,18 @@ namespace TeamAAS_VP.ViewModels.Statistics
                 MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
             };
 
+            var now = DateTime.UtcNow;
+            var monthStart = DateTimeAxis.ToDouble(new DateTime(now.Year, now.Month, 1));
+            var temp = new DateTime(now.Year, now.Month, 1);
+            var monthEnd = DateTimeAxis.ToDouble(temp.AddMonths(1).AddDays(-1));
+
             // 定义X轴为日期轴
-            var minValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(0));
-            var maxValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(Items.Keys.Count-1));
+            //var minValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(0));
+            //var maxValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(Items.Keys.Count - 1));
             var ax = new DateTimeAxis()
             {
-                Minimum = minValue,
-                Maximum = maxValue,
+                Minimum = monthStart,
+                Maximum = monthEnd,
                 StringFormat = $"dd{Lang.日}",
                 MajorStep = 1,
                 Position = AxisPosition.Bottom,
@@ -235,6 +241,11 @@ namespace TeamAAS_VP.ViewModels.Statistics
             model.Axes.Add(ay1);
             model.Axes.Add(ax);
 
+            if (Items.Count <= 0)
+            {
+                return model;
+            }
+
             foreach (var category in Items.First().Value.Keys)
             {
                 var barSeries = new LinearBarSeries { Title = category };
@@ -244,7 +255,14 @@ namespace TeamAAS_VP.ViewModels.Statistics
                 barSeries.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
                 foreach (var item in Items)
                 {
-                    barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
+                    if (item.Value.ContainsKey(category))
+                    {
+                        barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
+                    }
+                    else
+                    {
+                        barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0));
+                    }
                 }
 
                 model.Series.Add(barSeries);
@@ -308,7 +326,7 @@ namespace TeamAAS_VP.ViewModels.Statistics
                 Maximum = 7.5,
                 MajorStep = 1,
                 Position = AxisPosition.Bottom,
-                StringFormat= $"{Lang.周} 0",
+                StringFormat = $"{Lang.周} 0",
                 Angle = 0,
                 IsZoomEnabled = false,
                 IsPanEnabled = false,
@@ -320,10 +338,15 @@ namespace TeamAAS_VP.ViewModels.Statistics
 
             model.Axes.Add(ay1);
             model.Axes.Add(ax);
+            if (Items.Count <= 0)
+            {
+                return model;
+            }
+
 
             foreach (var category in Items.First().Value.Keys)
             {
-                
+
                 var barSeries = new LinearBarSeries { Title = category };
                 barSeries.YAxisKey = "y1";
                 barSeries.BarWidth = 10;
@@ -331,7 +354,14 @@ namespace TeamAAS_VP.ViewModels.Statistics
                 barSeries.TrackerFormatString = $"{0}\r\n{Lang.周}{2:0}: {4:0}";
                 foreach (var item in Items)
                 {
-                    barSeries.Points.Add(new OxyPlot.DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category])));
+                    if (item.Value.ContainsKey(category))
+                    {
+                        barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
+                    }
+                    else
+                    {
+                        barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0));
+                    }
                 }
 
                 model.Series.Add(barSeries);
@@ -408,6 +438,11 @@ namespace TeamAAS_VP.ViewModels.Statistics
             model.Axes.Add(ay1);
             model.Axes.Add(ax);
 
+            if (Items.Count<=0)
+            {
+                return model;
+            }
+
             foreach (var category in Items.First().Value.Keys)
             {