using MWS.Data.InterFace;
using MWS.Data.Manager;
using MWS.Domain.Common.DataExporting;
using MWS.Domain.Dto;
using MWS.Domain.Prints;
using MWS.Domain.View.History;
using MWS.Library.Common;
using MWS.Model;
using MWS.Model.Dto;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Visifire.Charts;
using Visifire.Commons;

namespace MWS.Domain.View.Weight
{
   
    /// <summary>
    /// WeightStatisticsChactPage.xaml 的交互逻辑
    /// </summary>
    public partial class WeightStatisticsChactPage : Page
    {
        private IWeight weightManage;
        private DateTime startTime;
        private DateTime endTime;
        private int mode = 0;
        private int type = 0;
        private int chartType = 0;
        private IDevice deviceManager;
        private ITeam teamManager;
        private IEntrance entranceManager;
        //private List<Device> devices;
        private List<Team> teams;
        private List<T_Entrance> entrances;
        private Chart chart;
        private List<Zigbee> devices;
        private IZigbee zigbeeManager;
        public WeightStatisticsChactPage()
        {
            InitializeComponent();
            weightManage = new WeightManager();
            deviceManager = new DeviceManager();
            teamManager = new TeamManager();
            entranceManager = new EntranceManager();
            zigbeeManager = new ZigbeeManager();
            startTime = DateTime.Now.AddDays(-24);
            endTime = DateTime.Now;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            BindModeComboBox();
            BindTypeComboBox();
            BindChartTypeComboBox();
            devices = zigbeeManager.GetAll();
            //devices = deviceManager.GetAll();
            teams = teamManager.GetAll("");
            entrances = entranceManager.GetAll("");
            btDataSelect.Content = startTime.ToString() + "-" + endTime.ToString();
            BindData();
        }
        private void search_Click(object sender, RoutedEventArgs e)
        {
            BindData();
        }

        public void CreateChartColumn(string name, List<ReportDto> details)
        {
            RenderAs renderAsType = (RenderAs)chartType;
            Chart chart = new Chart();
            chart.Width = 1700;
            chart.Height = 780;
            //chart.Margin = new Thickness(0, 5, 0, 15);
            chart.ToolBarEnabled = true;//工具箱是否可用：具有保存、打印图片功能
            chart.AnimatedUpdate = true;//是否动画更新
            chart.AnimationEnabled = true;//是否动画可用
            chart.ScrollingEnabled = false;//是否启用或禁止滚动
            chart.IndicatorEnabled = true;//指示线是否可用
            chart.View3D = false;//是否3D效果显示
            chart.Theme = "Theme1";//图表主题设置


            //定义图例（可不定义，图表本身自带图例）：图例位置 + 字体样式
            Legend legend = new Legend();
            legend.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;//水平方向位置
            legend.VerticalAlignment = System.Windows.VerticalAlignment.Center;//竖直方向位置
            legend.FontSize = 20;//字体大小
            legend.FontFamily = new FontFamily("宋体,Times New Roman");//字体样式
            chart.Legends.Add(legend);//图表添加图例

            //定义图表标题：标题文本、字体样式设置
            Title title = new Title();
            title.FontFamily = new FontFamily("宋体,Times New Roman");
            title.Text = "统计图表";
            title.FontSize = 20;
            chart.Titles.Add(title);//图表添加标题

            //定义x轴
            Axis xAxis = new Axis();
            xAxis.IntervalType = IntervalTypes.Auto;
            xAxis.Interval = 1;//x轴的间隔
            xAxis.ClipToBounds = false;
            //定义x轴标签：字体样式设置
            AxisLabels xLabels = new AxisLabels();
            xLabels.FontFamily = new FontFamily("宋体,Times New Roman");
            xLabels.FontSize = 12;
            xAxis.AxisLabels = xLabels;


            //定义y轴
            Axis yAxis0 = new Axis();
            AxisLabels yLabels0 = new AxisLabels();
            yLabels0.FontFamily = new FontFamily("宋体,Times New Roman");
            yLabels0.FontSize = 14;
            yAxis0.Suffix = "T";//y轴单位设置
            yAxis0.AxisMinimum = 0;//y轴最小值设置
            //yAxis0.AxisMaximum = 20;//最大值设置
            yAxis0.AxisLabels = yLabels0;

            //双轴定义方法：定义第二条y轴
            //Axis yAxis1 = new Axis();
            //yAxis1.AxisType = AxisTypes.Secondary;
            //AxisLabels yLabels1 = new AxisLabels();
            //yLabels1.FontFamily = new FontFamily("宋体,Times New Roman");
            //yLabels1.FontSize = 14;
            //yAxis1.Suffix = "T";
            //yAxis1.AxisMinimum = 0;
            ////yAxis1.AxisMaximum = 20;
            //yAxis1.AxisLabels = yLabels1;

            chart.AxesX.Add(xAxis);//图表添加x轴
            chart.AxesY.Add(yAxis0);//图表添加y轴
            //chart.AxesY.Add(yAxis1);

            int index = 0;
            switch (type)
            {
                case 1://按班组
                    foreach (var item in teams)
                    {
                        //定义第二条数据线
                        DataSeries series = new DataSeries();
                        series.RenderAs = renderAsType;
                        series.LegendText = item.Name;//图例文本内容
                        //switch (type)
                        //{
                        //    case 1:
                                
                        //        break;
                        //    case 2:
                        //        series1.LegendText = devices[0].Name;//图例文本内容
                        //        break;
                        //    case 3:
                        //        series1.LegendText = entrances[0].Name;//图例文本内容
                        //        break;
                        //}
                        series.MarkerSize = 10;
                        series.LineThickness = 1;
                        //series1.AxisYType = AxisTypes.Secondary;//数据线采用第二条y轴

                        foreach (var s in details)
                        {
                            DataPoint dataPoint = new DataPoint();
                            // 设置X轴点                    
                            dataPoint.AxisXLabel = s.Time.ToString();
                            //设置Y轴点
                            switch (index)
                            {
                                case 0:
                                    dataPoint.YValue = double.Parse(s.Data1.ToString());
                                    break;
                                case 1:
                                    dataPoint.YValue = double.Parse(s.Data2.ToString());
                                    break;
                                case 2:
                                    dataPoint.YValue = double.Parse(s.Data3.ToString());
                                    break;
                                default:
                                    break;
                            }
                            
                            //添加一个点击事件        
                            //dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
                            //添加数据点                   
                            series.DataPoints.Add(dataPoint);
                        }
                        chart.Series.Add(series);
                        index++;
                    }
                    break;
                case 2://按设备
                    foreach (var item in devices)
                    {
                        //定义第二条数据线
                        DataSeries series = new DataSeries();
                        series.RenderAs = renderAsType;
                        series.LegendText = item.Name;//图例文本内容
                        series.MarkerSize = 10;
                        series.LineThickness = 1;
                        //series1.AxisYType = AxisTypes.Secondary;//数据线采用第二条y轴

                        foreach (var s in details)
                        {
                            DataPoint dataPoint = new DataPoint();
                            // 设置X轴点                    
                            dataPoint.AxisXLabel = s.Time.ToString();
                            //设置Y轴点
                            switch (index)
                            {
                                case 0:
                                    dataPoint.YValue = double.Parse(s.Data1.ToString());
                                    break;
                                case 1:
                                    dataPoint.YValue = double.Parse(s.Data2.ToString());
                                    break;
                                case 2:
                                    dataPoint.YValue = double.Parse(s.Data3.ToString());
                                    break;
                                case 3:
                                    dataPoint.YValue = double.Parse(s.Data4.ToString());
                                    break;
                                case 4:
                                    dataPoint.YValue = double.Parse(s.Data5.ToString());
                                    break;
                                case 5:
                                    dataPoint.YValue = double.Parse(s.Data6.ToString());
                                    break;
                                default:
                                    break;
                            }

                            //添加一个点击事件        
                            //dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
                            //添加数据点                   
                            series.DataPoints.Add(dataPoint);
                        }
                        chart.Series.Add(series);
                        index++;
                    }
                    break;
                case 3://按料口
                    foreach (var item in entrances)
                    {
                        //定义第二条数据线
                        DataSeries series = new DataSeries();
                        series.RenderAs = renderAsType;
                        series.LegendText = item.Name;//图例文本内容
         
                        series.MarkerSize = 10;
                        series.LineThickness = 1;
                        //series1.AxisYType = AxisTypes.Secondary;//数据线采用第二条y轴

                        foreach (var s in details)
                        {
                            DataPoint dataPoint = new DataPoint();
                            // 设置X轴点                    
                            dataPoint.AxisXLabel = s.Time.ToString();
                            //设置Y轴点
                            switch (index)
                            {
                                case 0:
                                    dataPoint.YValue = double.Parse(s.Data1.ToString());
                                    break;
                                case 1:
                                    dataPoint.YValue = double.Parse(s.Data2.ToString());
                                    break;
                                case 2:
                                    dataPoint.YValue = double.Parse(s.Data3.ToString());
                                    break;
                              
                                default:
                                    break;
                            }

                            //添加一个点击事件        
                            //dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
                            //添加数据点                   
                            series.DataPoints.Add(dataPoint);
                        }
                        chart.Series.Add(series);
                        index++;
                    }
                    break;
                default:
                    break;
            }
            // 创建一个新的数据线。               
            //定义第一条数据线
            //DataSeries series0 = new DataSeries();
            ////RenderAs图标类型： 柱形图(Column) 曲线图(Line)折线图(Step Line)饼图(Pie)横道图(Bar)区域图(Area)环形图(Doughnut)堆积柱形图(Stacked Column)100 % 堆积柱形图(100 % Stacked Column)
            ////堆积道图(Stacked Bar)100 % 堆积横道图(100 % Stacked Bar)堆积区域图(Stacked Area)100 % 堆积区域图(100 Stacked Area)气泡图(Bubble)散射图(Point)+
            ////流线漏斗图(StreamLineFunnel)漏斗图(Section Funnel)阴阳烛图(CandleStick)股票图(Stock)极区图(Line)雷达图(Radar)光滑曲线图(Spline)高密度曲线图(QuickLine)
            ////series0.RenderAs = RenderAs.Column;
            //series0.RenderAs = renderAsType;
            //series0.LabelEnabled = false;//标签是否可见
            //series0.LegendText = "全部";

            //series0.ShowInLegend = true;//图例是否显示
            //series0.MarkerType = MarkerTypes.Cross;//曲线上数据点的形状
            //series0.MarkerSize = 10;//数据点的形状的大小
            //series0.LineThickness = 1;//数据线的粗细
            //// 设置数据点
            //DataPoint dataPoint;
            //foreach (var item in details)
            //{
            //    dataPoint = new DataPoint();
            //    // 设置X轴点                    
            //    dataPoint.AxisXLabel = item.Time.ToString();
            //    //设置Y轴点                   
            //    dataPoint.YValue = double.Parse(item.NetWeight.ToString());
            //    //添加一个点击事件        
            //    //dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
            //    //添加数据点                   
            //    series0.DataPoints.Add(dataPoint);
            //}
            //chart.Series.Add(series0);



            ////定义第二条数据线
            //DataSeries series1 = new DataSeries();
            //series1.RenderAs = renderAsType;
            //switch (type)
            //{
            //    case 1:
            //        series1.LegendText = teams[0].Name;//图例文本内容
            //        break;
            //    case 2:
            //        series1.LegendText = devices[0].Name;//图例文本内容
            //        break;
            //    case 3:
            //        series1.LegendText = entrances[0].Name;//图例文本内容
            //        break;
            //}
            //series1.MarkerSize = 10;
            //series1.LineThickness = 1;
            ////series1.AxisYType = AxisTypes.Secondary;//数据线采用第二条y轴

            //foreach (var item in details)
            //{
            //    dataPoint = new DataPoint();
            //    // 设置X轴点                    
            //    dataPoint.AxisXLabel = item.Time.ToString();
            //    //设置Y轴点                   
            //    dataPoint.YValue = double.Parse(item.Data1.ToString());
            //    //添加一个点击事件        
            //    //dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
            //    //添加数据点                   
            //    series1.DataPoints.Add(dataPoint);
            //}
            //chart.Series.Add(series1);
            ////定义第三条数据线
            //DataSeries series2 = new DataSeries();
            //series2.RenderAs = renderAsType;
            //switch (type)
            //{
            //    case 1:
            //        series2.LegendText = teams[1].Name;//图例文本内容
            //        break;
            //    case 2:
            //        series2.LegendText = devices[1].Name;//图例文本内容
            //        break;
            //    case 3:
            //        series2.LegendText = entrances[1].Name;//图例文本内容
            //        break;
            //}
            //series2.MarkerSize = 10;
            //series2.LineThickness = 1;
            ////series2.AxisYType = AxisTypes.Secondary;//数据线采用第二条y轴

            //foreach (var item in details)
            //{
            //    dataPoint = new DataPoint();
            //    // 设置X轴点                    
            //    dataPoint.AxisXLabel = item.Time.ToString();
            //    //设置Y轴点                   
            //    dataPoint.YValue = double.Parse(item.Data2.ToString());
            //    //添加一个点击事件        
            //    //dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
            //    //添加数据点                   
            //    series2.DataPoints.Add(dataPoint);
            //}
            //chart.Series.Add(series2);

            //// 添加数据线到数据序列。                
            //if (type==1)
            //{
            //    //定义第四条数据线
            //    DataSeries series3 = new DataSeries();
            //    series3.RenderAs = renderAsType;
            //    series3.LegendText = teams[2].Name;//图例文本内容
            //    series3.MarkerSize = 10;
            //    series3.LineThickness = 1;
            //    //series2.AxisYType = AxisTypes.Secondary;//数据线采用第二条y轴

            //    foreach (var item in details)
            //    {
            //        dataPoint = new DataPoint();
            //        // 设置X轴点                    
            //        dataPoint.AxisXLabel = item.Time.ToString();
            //        //设置Y轴点                   
            //        dataPoint.YValue = double.Parse(item.Data3.ToString());
            //        //添加一个点击事件        
            //        //dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
            //        //添加数据点                   
            //        series3.DataPoints.Add(dataPoint);
            //    }

            //    chart.Series.Add(series3);
            //}

            //////将生产的图表增加到Grid，然后通过Grid添加到上层Grid.           
            ////Grid gr = new Grid();
            ////gr.Children.Add(chart);
            this.StaticsChart.Children.Add(chart);
        }

        private void BindData()
        {
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            startTime = dateSelect[0].ToDateTime();
            endTime = dateSelect[1].ToDateTime();
            mode = Convert.ToInt32(cbMode.SelectedValue);
            type = Convert.ToInt32(cbType.SelectedValue);
            chartType = Convert.ToInt32(cbChartType.SelectedValue);
            //startTime = (DateTime)dpBeginTime.SelectedDate;
            //endTime = (DateTime)dpEndTime.SelectedDate;
            List<ReportDto> reportDtos = new List<ReportDto>();
            //dglist.Columns.Clear();

            //dglist.Columns.Add(new DataGridTextColumn()
            //{
            //    Header = "时间",
            //    Binding = new Binding(string.Format("{0}", "Time")),
            //    Width = new DataGridLength(1, DataGridLengthUnitType.Star),

            //});
            //dglist.Columns.Add(new DataGridTextColumn()
            //{
            //    Header = "斗数小计",
            //    Binding = new Binding(string.Format("{0}", "Count")),
            //    Width = new DataGridLength(1, DataGridLengthUnitType.Star)
            //});
            //dglist.Columns.Add(new DataGridTextColumn()
            //{
            //    Header = "重量小计",
            //    Binding = new Binding(string.Format("{0}", "NetWeight")),
            //    Width = new DataGridLength(1, DataGridLengthUnitType.Star),


            //});
            List<RecordDto> weightRecordDtos = weightManage.GetList(startTime, endTime).ToList();
            switch (mode)//按日、周、月、年
            {
                case 0:
                    switch (type)//
                    {
                        case 1:

                            var tGroup = weightRecordDtos.GroupBy(c => c.Time.Hour);
                           

                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = g.Key.ToString().PadLeft(2, '0') + ":00:00-" + g.Key.ToString().PadLeft(2, '0') + ":59:59";
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.TeamId == teams[0].Id).Sum(c => c.NetWeight), 2);
                                if (teams.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.TeamId == teams[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (teams.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.TeamId == teams[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }
                            break;
                        case 2:
                            tGroup = weightRecordDtos.GroupBy(c => c.Time.Hour);
                         

                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = g.Key.ToString().PadLeft(2, '0') + ":00:00-" + g.Key.ToString().PadLeft(2, '0') + ":59:59";
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.DeviceId == devices[0].Id).Sum(c => c.NetWeight), 2);
                                if (devices.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.DeviceId == devices[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.DeviceId == devices[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 3)
                                {
                                    reportDto.Data4 = Math.Round(g.Where(c => c.DeviceId == devices[3].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 4)
                                {
                                    reportDto.Data5 = Math.Round(g.Where(c => c.DeviceId == devices[4].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 5)
                                {
                                    reportDto.Data6 = Math.Round(g.Where(c => c.DeviceId == devices[5].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);

                            }

                            break;
                        case 3:
                            tGroup = weightRecordDtos.GroupBy(c => c.Time.Hour);
                          
                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = g.Key.ToString().PadLeft(2, '0') + ":00:00-" + g.Key.ToString().PadLeft(2, '0') + ":59:59";
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.EntranceId == entrances[0].Id).Sum(c => c.NetWeight), 2);
                                if (entrances.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.EntranceId == entrances[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (entrances.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.EntranceId == entrances[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }

                            break;
                        default:
                            break;
                    }
                    break;
                case 1:
                    switch (type)//
                    {
                        case 1:
                            //var tGroup = weightRecordDtos.GroupBy(c => c.Time.DayOfWeek);
                            var tGroup = weightRecordDtos.GroupBy(c => c.Time.DayOfWeek);
                            //for (int i = 0; i < teams.Count; i++)
                            //{
                            //    dglist.Columns.Add(new DataGridTextColumn()
                            //    {
                            //        Header = teams[i].Name,
                            //        Binding = new Binding(string.Format("{0}", "Data" + (i + 1))),
                            //        Width = new DataGridLength(1, DataGridLengthUnitType.Star)
                            //    });
                            //}
                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = Tool.GetWeek(g.Key);
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.TeamId == teams[0].Id).Sum(c => c.NetWeight), 2);
                                if (teams.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.TeamId == teams[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (teams.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.TeamId == teams[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }
                            break;
                        case 2:
                            tGroup = weightRecordDtos.GroupBy(c => c.Time.DayOfWeek);
                            //for (int i = 0; i < devices.Count; i++)
                            //{
                            //    dglist.Columns.Add(new DataGridTextColumn()
                            //    {
                            //        Header = devices[i].Name,
                            //        Binding = new Binding(string.Format("{0}", "Data" + (i + 1))),
                            //        Width = new DataGridLength(1, DataGridLengthUnitType.Star)
                            //    });
                            //}
                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = Tool.GetWeek(g.Key);
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.DeviceId == devices[0].Id).Sum(c => c.NetWeight), 2);
                                if (devices.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.DeviceId == devices[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.DeviceId == devices[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 3)
                                {
                                    reportDto.Data4 = Math.Round(g.Where(c => c.DeviceId == devices[3].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 4)
                                {
                                    reportDto.Data5 = Math.Round(g.Where(c => c.DeviceId == devices[4].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 5)
                                {
                                    reportDto.Data6 = Math.Round(g.Where(c => c.DeviceId == devices[5].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }
                            break;
                        case 3:
                            tGroup = weightRecordDtos.GroupBy(c => c.Time.DayOfWeek);
                            //for (int i = 0; i < entrances.Count; i++)
                            //{
                            //    dglist.Columns.Add(new DataGridTextColumn()
                            //    {
                            //        Header = entrances[i].Name,
                            //        Binding = new Binding(string.Format("{0}", "Data" + (i + 1))),
                            //        Width = new DataGridLength(1, DataGridLengthUnitType.Star)
                            //    });
                            //}
                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = Tool.GetWeek(g.Key);
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.EntranceId == entrances[0].Id).Sum(c => c.NetWeight), 2);
                                if (entrances.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.EntranceId == entrances[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (entrances.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.EntranceId == entrances[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }
                            //dt.Columns.AddRange(entrances.Select(c => new DataColumn(c.Name, typeof(double))).ToArray());
                            //foreach (var g in tGroup)
                            //{
                            //    tempList.Clear();
                            //    tempList.Add(Tool.GetWeek(g.Key));
                            //    tempList.Add(g.Count());
                            //    tempList.Add(Math.Round(g.Sum(e => e.NetWeight), 2));
                            //    foreach (var item in entrances)
                            //    {
                            //        tempList.Add(Math.Round(g.Where(c => c.EntranceId == item.Id).Sum(c => c.NetWeight), 2));
                            //    }
                            //    dt.LoadDataRow(tempList.ToArray(), true);
                            //}
                            break;
                        default:
                            break;
                    }
                    break;
                case 2:
                    switch (type)//
                    {
                        case 1:
                            //var tGroup = weightRecordDtos.GroupBy(c => c.Time.DayOfWeek);
                            var tGroup = weightRecordDtos.GroupBy(c => c.Time.Month + "-" + c.Time.Day);
                            //for (int i = 0; i < teams.Count; i++)
                            //{
                            //    dglist.Columns.Add(new DataGridTextColumn()
                            //    {
                            //        Header = teams[i].Name,
                            //        Binding = new Binding(string.Format("{0}", "Data" + (i + 1))),
                            //        Width = new DataGridLength(1, DataGridLengthUnitType.Star)
                            //    });
                            //}
                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = Tool.GetDay(g.Key);
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.TeamId == teams[0].Id).Sum(c => c.NetWeight), 2);
                                if (teams.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.TeamId == teams[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (teams.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.TeamId == teams[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }
                            break;
                        case 2:
                            tGroup = weightRecordDtos.GroupBy(c => c.Time.Month + "-" + c.Time.Day);
                            //for (int i = 0; i < devices.Count; i++)
                            //{
                            //    dglist.Columns.Add(new DataGridTextColumn()
                            //    {
                            //        Header = devices[i].Name,
                            //        Binding = new Binding(string.Format("{0}", "Data" + (i + 1))),
                            //        Width = new DataGridLength(1, DataGridLengthUnitType.Star)
                            //    });
                            //}
                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = Tool.GetDay(g.Key);
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.DeviceId == devices[0].Id).Sum(c => c.NetWeight), 2);
                                if (devices.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.DeviceId == devices[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.DeviceId == devices[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 3)
                                {
                                    reportDto.Data4 = Math.Round(g.Where(c => c.DeviceId == devices[3].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 4)
                                {
                                    reportDto.Data5 = Math.Round(g.Where(c => c.DeviceId == devices[4].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 5)
                                {
                                    reportDto.Data6 = Math.Round(g.Where(c => c.DeviceId == devices[5].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }
                            break;
                        case 3:
                            tGroup = weightRecordDtos.GroupBy(c => c.Time.Month + "-" + c.Time.Day);
                            //for (int i = 0; i < entrances.Count; i++)
                            //{
                            //    dglist.Columns.Add(new DataGridTextColumn()
                            //    {
                            //        Header = entrances[i].Name,
                            //        Binding = new Binding(string.Format("{0}", "Data" + (i + 1))),
                            //        Width = new DataGridLength(1, DataGridLengthUnitType.Star)
                            //    });
                            //}
                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = Tool.GetDay(g.Key);
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.EntranceId == entrances[0].Id).Sum(c => c.NetWeight), 2);
                                if (entrances.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.EntranceId == entrances[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (entrances.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.EntranceId == entrances[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }
                            break;

                        default:
                            break;
                    }
                    break;
                case 3:
                    switch (type)//
                    {
                        case 1:
                            var tGroup = weightRecordDtos.GroupBy(c => c.Time.Month);
                            //for (int i = 0; i < teams.Count; i++)
                            //{
                            //    dglist.Columns.Add(new DataGridTextColumn()
                            //    {
                            //        Header = teams[i].Name,
                            //        Binding = new Binding(string.Format("{0}", "Data" + i + 1)),
                            //        Width = new DataGridLength(1, DataGridLengthUnitType.Star)
                            //    });
                            //}
                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = Tool.GetMonth(g.Key);
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.TeamId == teams[0].Id).Sum(c => c.NetWeight), 2);
                                if (teams.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.TeamId == teams[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (teams.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.TeamId == teams[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }
                            break;
                        case 2:
                            tGroup = weightRecordDtos.GroupBy(c => c.Time.Month);
                            //for (int i = 0; i < devices.Count; i++)
                            //{
                            //    dglist.Columns.Add(new DataGridTextColumn()
                            //    {
                            //        Header = devices[i].Name,
                            //        Binding = new Binding(string.Format("{0}", "Data" + i + 1)),
                            //        Width = new DataGridLength(1, DataGridLengthUnitType.Star)
                            //    });
                            //}
                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = Tool.GetMonth(g.Key);
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.DeviceId == devices[0].Id).Sum(c => c.NetWeight), 2);
                                if (devices.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.DeviceId == devices[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.DeviceId == devices[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 3)
                                {
                                    reportDto.Data4 = Math.Round(g.Where(c => c.DeviceId == devices[3].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 4)
                                {
                                    reportDto.Data5 = Math.Round(g.Where(c => c.DeviceId == devices[4].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (devices.Count > 5)
                                {
                                    reportDto.Data6 = Math.Round(g.Where(c => c.DeviceId == devices[5].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }
                            break;
                        case 3:
                            tGroup = weightRecordDtos.GroupBy(c => c.Time.Month);
                            //for (int i = 0; i < entrances.Count; i++)
                            //{
                            //    dglist.Columns.Add(new DataGridTextColumn()
                            //    {
                            //        Header = entrances[i].Name,
                            //        Binding = new Binding(string.Format("{0}", "Data" + i + 1)),
                            //        Width = new DataGridLength(1, DataGridLengthUnitType.Star)
                            //    });
                            //}
                            foreach (var g in tGroup)
                            {
                                ReportDto reportDto = new ReportDto();
                                reportDto.Time = Tool.GetMonth(g.Key);
                                reportDto.Count = g.Count();
                                reportDto.NetWeight = Math.Round(g.Sum(e => e.NetWeight), 2);
                                reportDto.Data1 = Math.Round(g.Where(c => c.EntranceId == entrances[0].Id).Sum(c => c.NetWeight), 2);
                                if (entrances.Count > 1)
                                {
                                    reportDto.Data2 = Math.Round(g.Where(c => c.EntranceId == entrances[1].Id).Sum(c => c.NetWeight), 2);
                                }
                                if (entrances.Count > 2)
                                {
                                    reportDto.Data3 = Math.Round(g.Where(c => c.EntranceId == entrances[2].Id).Sum(c => c.NetWeight), 2);
                                }
                                reportDtos.Add(reportDto);
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
            CreateChartColumn("重量统计", reportDtos);
            //dglist.ItemsSource = reportDtos;

        }
        /// <summary>
        /// 绑定统计方式
        /// </summary>
        private void BindModeComboBox()
        {
            try
            {
                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("0", "日"),
                    new KeyValuePair<string, string>("1", "周"),
                    new KeyValuePair<string, string>("2", "月"),
                    new KeyValuePair<string, string>("3", "年")
                 };
                cbMode.ItemsSource = list;
                cbMode.SelectedValuePath = "Key";
                cbMode.DisplayMemberPath = "Value";
                cbMode.SelectedIndex = 0;
            }
            catch (Exception ex)
            {

                throw;
            }

        }
        /// <summary>
        /// 绑定统计类型
        /// </summary>
        private void BindTypeComboBox()
        {
            try
            {
                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("1", "班组"),
                    new KeyValuePair<string, string>("2", "设备"),
                    new KeyValuePair<string, string>("3", "料口"),

                 };
                cbType.ItemsSource = list;
                cbType.SelectedValuePath = "Key";
                cbType.DisplayMemberPath = "Value";
                cbType.SelectedIndex = 0;
            }
            catch (Exception ex)
            {

                throw;
            }

        }
        /// <summary>
        /// 绑定统计类型
        /// </summary>
        private void BindChartTypeComboBox()
        {
            try
            {
                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("19", "光滑曲线图"),
                    new KeyValuePair<string, string>("0", "柱状图"),
                    new KeyValuePair<string, string>("1", "曲线图"),
                    new KeyValuePair<string, string>("18", "折线图"),                    
                    new KeyValuePair<string, string>("23", "高密度曲线图"),

                 };
                cbChartType.ItemsSource = list;
                cbChartType.SelectedValuePath = "Key";
                cbChartType.DisplayMemberPath = "Value";
                cbChartType.SelectedIndex = 0;
            }
            catch (Exception ex)
            {

                throw;
            }

        }


        private void btDataSelect_Click(object sender, RoutedEventArgs e)
        {
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            startTime = dateSelect[0].ToDateTime();
            endTime = dateSelect[1].ToDateTime();

            DateTimeSelectWindow dateTimeSelectWindow = new DateTimeSelectWindow(startTime, endTime);
            dateTimeSelectWindow.ShowDialog();
            if ((bool)dateTimeSelectWindow.DialogResult)
            {
                this.startTime = dateTimeSelectWindow.BeginTime;
                this.endTime = dateTimeSelectWindow.EndTime;

                btDataSelect.Content = dateTimeSelectWindow.BeginTime.ToString() + "-" + dateTimeSelectWindow.EndTime.ToString();
            };
        }
    }
}
