using HandyControl.Controls; using LiveCharts; using Ping9719.WpfEx.Mvvm; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace 智慧物流软件系统.ViewModel { public class DataChartViewModel : BindableBase { #region 加载 private bool isLoad; public bool IsLoad { get => isLoad; set { SetProperty(ref isLoad, value); } } #endregion #region 日期 private List year = new List(); /// /// 年 /// public List Year { get => year; set { SetProperty(ref year, value); } } private List month = new List(); /// /// 月 /// public List Month { get => month; set { SetProperty(ref month, value); } } private int yearIndex; /// /// 年索引 /// public int YearIndex { get => yearIndex; set { SetProperty(ref yearIndex, value); UpdataMonth(); } } private int monthIndex; /// /// 月索引 /// public int MonthIndex { get => monthIndex; set { SetProperty(ref monthIndex, value); StatChart(); } } #endregion #region 图表 public ChartValues ChartValues1 { get; set; } = new ChartValues(); public ChartValues ChartValues2 { get; set; } = new ChartValues(); public ChartValues ChartValues3 { get; set; } = new ChartValues(); public Func LabelFormatterX { get; set; } = value => $"{value}号"; public Func LabelFormatterY { get; set; } = value => $"{value}个"; private IList chartLabelsX; /// /// X轴轴信息 /// public IList ChartLabelsX { get => chartLabelsX; set { SetProperty(ref chartLabelsX, value); } } #endregion #region 方法 /// /// 更新年 /// public async void UpdataYear() { try { IsLoad = true; Year = await DataDb.db.Queryable() .GroupBy(o => o.Time.Year) .OrderByDescending(o => o.Time.Year) .Select(o => o.Time.Year) .ToListAsync(); } catch (Exception ex) { Year = new List(); Growl.Error("刷新数据失败:" + ex.Message); } finally { YearIndex = Year.Any() ? 0 : -1; } } /// /// 更新月 /// public async void UpdataMonth() { try { IsLoad = true; if (!Year.Any() || YearIndex < 0) return; var dt1 = new DateTime(Year[YearIndex], 1, 1); var dt2 = dt1.AddYears(1); Month = await DataDb.db.Queryable() .Where(o => o.Time > dt1 && o.Time < dt2) .GroupBy(o => o.Time.Month) .OrderBy(o => o.Time.Month) .Select(o => o.Time.Month) .ToListAsync(); } catch (Exception ex) { Month = new List(); Growl.Error("刷新数据失败:" + ex.Message); } finally { MonthIndex = Month.Any() ? Month.Count - 1 : -1; } } public ICommand ButStatChartCommand { get => new DelegateCommand(ButStatChart); } /// /// 点击刷新 /// public void ButStatChart() { if (IsLoad) { Growl.Info("有任务正在进行"); return; } if (!Year.Any() || !Month.Any() || YearIndex < 0 || MonthIndex < 0) { Growl.Info("没有选择年份或月份"); return; } StatChart(); } /// /// 刷新统计信息 /// public async void StatChart() { try { if (!Year.Any() || !Month.Any() || YearIndex < 0 || MonthIndex < 0) { ChartValues1.Clear(); ChartValues2.Clear(); ChartValues3.Clear(); return; } IsLoad = true; await Task.Delay(200); var year = Year[YearIndex]; var month = Month[MonthIndex]; var dt1 = new DateTime(year, month, 1); var dt2 = dt1.AddMonths(1); //从数据库中查询、统计数量 var dbdata = await DataDb.db.Queryable() .Where(o => o.Time > dt1 && o.Time < dt2) .GroupBy(o => o.Time.Day) .Select(o => new { day = o.Time.Day, count = SqlFunc.AggregateCount(o.Id), okCount = SqlFunc.AggregateCount(o.Status == "合格" ? "" : null), notCount = SqlFunc.AggregateCount(o.Status != "合格" ? "" : null), }) .OrderBy(o => o.day) .ToListAsync(); ChartLabelsX = dbdata.Select(o => o.day.ToString()).ToList(); ChartValues1.Clear(); ChartValues2.Clear(); ChartValues3.Clear(); ChartValues1.AddRange(dbdata.Select(o => o.count)); ChartValues2.AddRange(dbdata.Select(o => o.okCount)); ChartValues3.AddRange(dbdata.Select(o => o.notCount)); } catch (Exception ex) { Growl.Error("刷新数据失败:" + ex.Message); } finally { IsLoad = false; } } #endregion } }