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;

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

            startTime = DateTime.Now.AddDays(-24);
            endTime = DateTime.Now;
            //dpBeginTime.SelectedDate = DateTime.Now;
            //dpEndTime.SelectedDate = DateTime.Now.AddHours(24);

        }

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

        }

        private void export_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog fileDialog = new System.Windows.Forms.SaveFileDialog
            {
                Title = "Export Excel",
                Filter = "Excel(*.xlsx)|*.xlsx|Excel 97-2003(*.xls)|*.xls",
                ValidateNames = true,
                AddExtension = true,
                FileName = "报表统计" + DateTime.Now.ToString("yyyyMMddHHmmss")
            };
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                mode = Convert.ToInt32(cbMode.SelectedValue);
                type = Convert.ToInt32(cbType.SelectedValue);
                var dateSelect = btDataSelect.Content.ToString().Split('-');
                startTime = dateSelect[0].ToDateTime();
                endTime = dateSelect[1].ToDateTime();
                //startTime = (DateTime)dpBeginTime.SelectedDate;
                //endTime = (DateTime)dpEndTime.SelectedDate;
                List<ReportDto> reportDtos = new List<ReportDto>();
                string[] strs = new string[6];
                strs[0] = "时间";
                strs[1] = "斗数小计";
                strs[2] = "重量小计";

                List<RecordDto> weightRecordDtos = weightManage.GetList(startTime, endTime).ToList();
                
                switch (mode)//按日、周、月、年
                {
                    case 0:
                        switch (type)//
                        {
                            case 1:
                                var tGroup = weightRecordDtos.GroupBy(c => c.Time.Hour);
                                for (int i = 0; i < teams.Count; i++)
                                {
                                    strs[i + 3] = teams[i].Name;
                                }

                                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(c => c.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);
                                for (int i = 0; i < devices.Count; i++)
                                {
                                    strs[i + 3] = devices[i].Name;
                                }

                                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(c => c.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);
                                for (int i = 0; i < entrances.Count; i++)
                                {
                                    strs[i + 3] = entrances[i].Name;
                                }
                              
                                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(c => c.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++)
                                {
                                    strs[i + 3] = teams[i].Name;
                                }
                                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(c => c.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++)
                                {
                                    strs[i + 3] = devices[i].Name;
                                }
                                
                                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(c => c.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++)
                                {
                                    strs[i + 3] = entrances[i].Name;
                                }
                               
                                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(c => c.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;
                           
                        }
                        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++)
                                {
                                    strs[i + 3] = teams[i].Name;
                                }
                               
                                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(c => c.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++)
                                {
                                    strs[i + 3] = devices[i].Name;
                                }
                                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(c => c.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++)
                                {
                                    strs[i + 3] = entrances[i].Name;
                                }
                                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(c => c.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;
                            
                        }
                        break;
                    case 3:
                        switch (type)//
                        {
                            case 1:
                                var tGroup = weightRecordDtos.GroupBy(c => c.Time.Month);
                                for (int i = 0; i < teams.Count; i++)
                                {
                                    strs[i + 3] = teams[i].Name;
                                }
                                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(c => c.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++)
                                {
                                    strs[i + 3] = devices[i].Name;
                                }
                                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(c => c.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++)
                                {
                                    strs[i + 3] = entrances[i].Name;
                                }
                                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(c => c.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;
                        }
                        break;

                }
                if (reportDtos != null)
                {
                    new WeightStatisticsExcelExporter().ExportToFile(fileDialog.FileName, reportDtos, strs);
                }
            }
        }


        private delegate void DoPrintMethod(PrintDialog pdlg, DocumentPaginator paginator);
        private void print_Click(object sender, RoutedEventArgs e)
        {
            #region 打印预览

            #endregion
            string strType = "";
            DateTime now = DateTime.Now;
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            startTime = dateSelect[0].ToDateTime();
            endTime = dateSelect[1].ToDateTime();
            List<ReportDto> reportDtos = new List<ReportDto>();
            List<RecordDto> weightRecordDtos = weightManage.GetList(startTime, endTime).ToList();
            List<HeaderColumnDto> headerColumnDtos = new List<HeaderColumnDto>();
            headerColumnDtos.Add(new HeaderColumnDto()
            {
                DisplayName = "时间",
                FiledName = "Time"
            });
            headerColumnDtos.Add(new HeaderColumnDto()
            {
                DisplayName = "斗数小计",
                FiledName = "Count"
            });
            headerColumnDtos.Add(new HeaderColumnDto()
            {
                DisplayName = "重量小计",
                FiledName = "NetWeight"
            });

            switch (type)
            {
                case 1:
                    strType += "班组";
                    for (int i = 0; i < teams.Count; i++)
                    {
                        headerColumnDtos.Add(new HeaderColumnDto()
                        {
                            DisplayName = teams[i].Name,
                            FiledName = "Data"+(i+1)
                        });
                    }
                    break;
                case 2:
                    strType += "‘设备";
                    for (int i = 0; i < devices.Count; i++)
                    {
                        headerColumnDtos.Add(new HeaderColumnDto()
                        {
                            DisplayName = devices[i].Name,
                            FiledName = "Data" + (i + 1)
                        });
                    }

                    break;
                case 3:
                    strType += "料口";
                    for (int i = 0; i < entrances.Count; i++)
                    {
                        headerColumnDtos.Add(new HeaderColumnDto()
                        {
                            DisplayName = entrances[i].Name,
                            FiledName = "Data" + (i + 1)
                        });
                    }

                    break;
            
            }
            switch (mode)//按日、周、月、年
            {
                case 0:
                    strType += "、日";
                    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(c => c.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(c => c.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(c => c.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:

                    strType += "、周";
                    switch (type)//
                    {
                        case 1:
                            //var tGroup = weightRecordDtos.GroupBy(c => c.Time.DayOfWeek);
                            var tGroup = weightRecordDtos.GroupBy(c => c.Time.DayOfWeek);
                          
                            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(c => c.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);
                          

                            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(c => c.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);
                           
                            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(c => c.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;

                    }
                    break;
                case 2:
                    strType += "、月";
                    switch (type)//
                    {
                        case 1:
                            //var tGroup = weightRecordDtos.GroupBy(c => c.Time.DayOfWeek);
                            var tGroup = weightRecordDtos.GroupBy(c => c.Time.Month + "-" + c.Time.Day);
                           

                            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(c => c.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);
                           
                            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(c => c.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);
                          
                            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(c => c.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;

                    }
                    break;
                case 3:
                    strType += "、年";
                    switch (type)//
                    {
                        case 1:
                            var tGroup = weightRecordDtos.GroupBy(c => c.Time.Month);
                          
                            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(c => c.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);
                           
                            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(c => c.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);
                           
                            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(c => c.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;
                    }
                    break;

            }
            var source = new PrintTableDto<ReportDto>()
            {
                Title = "报表统计",
                LeftSubtitle = "统计方式："+ strType,
                RightSubtitle = "时间区间：" + startTime.ToString() + " 至 " + endTime.ToString(),
                Footer = "共计：" + weightRecordDtos.Sum(c => c.NetWeight) + " 吨",
                Columns = headerColumnDtos,
                Data = reportDtos
            };
            PrintPreviewWindow previewWnd = new PrintPreviewWindow();
            previewWnd.LoadDocumentAndRender<ReportDto>("/Prints/Documents/PrintTableFlowDocument.xaml", source, new TableDocumentRenderer());
            previewWnd.Owner = Window.GetWindow(this);
            previewWnd.ShowInTaskbar = false;
            previewWnd.ShowDialog();
            #region 打印
            //PrintPreviewWindow printWnd = new PrintPreviewWindow();
            //PrintDialog pdlg = new PrintDialog();
            //FlowDocument doc = printWnd.LoadDocumentAndRender<ReportDto>("/Prints/Documents/PrintTableFlowDocument.xaml", source, new TableDocumentRenderer());
            //Dispatcher.BeginInvoke(new DoPrintMethod(DoPrint), DispatcherPriority.ApplicationIdle, pdlg, ((IDocumentPaginatorSource)doc).DocumentPaginator);

            #endregion
        }
        private void DoPrint(PrintDialog pdlg, DocumentPaginator paginator)
        {
            pdlg.PrintDocument(paginator, "称重记录");
        }

        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();
            };
        }
    }
}
