using MWS.Data.InterFace;
using MWS.Data.Manager;
using MWS.Domain.Dto;
using MWS.Library.Common;
using MWS.Library.Common.Module;
using MWS.Model;
using MWS.Model.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;


namespace MWS.Domain.Web.Controller
{
    public class WeightReportController : ApiController
    {

        [HttpGet]      
        public Result<List<ReportDto>> GetReport(int mode = 0, int type = 1, DateTime? beginTime = default, DateTime? endTime = default)
        {
            Result<List<ReportDto>> result = new Result<List<ReportDto>>();

            IWeight weightManage = new WeightManager();
            try
            {
                IZigbee zigbeeManager = new ZigbeeManager();
                IDevice deviceManager = new DeviceManager();
                ITeam teamManager = new TeamManager();
                IEntrance entranceManager = new EntranceManager();
                var teams = teamManager.GetAll("");
                var entrances = entranceManager.GetAll("");
                var devices = zigbeeManager.GetAll();
                List<ReportDto> reportDtos = new List<ReportDto>();
                List<RecordDto> weightRecordDtos = weightManage.GetList(beginTime, 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);

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

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

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

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

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

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

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

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

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

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



                result.Data = reportDtos;
                result.Message = "成功！";
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Message = "失败！";
            }

            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mode">/0日、1周、2月、3年</param>
        /// <param name="type">1班组，2设备，3料口</param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
    }
}
