using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.share;

namespace wispro.sp.api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]
    public class ReportController : ControllerBase
    {
        spDbContext Context;

        public ReportController(spDbContext context)
        {
            Context = context;


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type">
        /// 0:申诉人；1：申诉类型；2：部门申诉统计；
        /// 3：申诉人每月申诉统计；4：申诉类型每月申诉统计
        /// </param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ChartDatas AppealReport(int type,DateTime? start,DateTime? end,int? userId)
        {
            ChartDatas retDatas = new ChartDatas();

            var results = Context.AppealRecords.Where(b=>(1==1));
            if (start.HasValue)
            {
                results = results.Where(b => b.CreateTime >= start);
            }

            if (end.HasValue)
            {
                results = results.Where(b => b.CreateTime <= end);
            }

            if (userId.HasValue)
            {
                results = results.Where(b => b.CreaterId == userId);
            }

            switch (type)
            {
                case 0:
                    var retData = results.GroupBy(c => c.Creater.Name)
                        .Select(g => new { key = g.Key, Count = g.Count() }).OrderByDescending(g=>g.Count)
                        .ToDictionary(g => g.key, g => g.Count);

                    retDatas.ATitle = "申诉人";
                    retDatas.Title = "申诉人统计";
                    retDatas.Datas = new List<ChartData>();
                    
                    foreach (var key in retData.Keys)
                    {
                        retDatas.Datas.Add(new ChartData() { type = key,value=retData[key]} );
                    }
                    return retDatas;
                    break;
                case 1:
                    var retData1 = results.GroupBy(c => c.Type.Name)
                        .Select(g => new { key = g.Key, Count = g.Count() }).OrderByDescending(g => g.Count)
                        .ToDictionary(g => g.key, g => g.Count);

                    retDatas.ATitle = "申诉类型";
                    retDatas.Title = "申诉类型统计";
                    retDatas.Datas = new List<ChartData>();
                    
                    foreach (var key in retData1.Keys)
                    {
                        retDatas.Datas.Add(new ChartData() { type = key, value = retData1[key] });
                    }
                    return retDatas;
                    break;
                case 2:
                    var retData2 = results.GroupBy(c => c.Creater.Department)
                        .Select(g => new { key = g.Key, Count = g.Count() }).OrderByDescending(g => g.Count)
                        .ToDictionary(g => g.key, g => g.Count);

                    retDatas.ATitle = "部门";
                    retDatas.Title = "部门申诉统计";
                    retDatas.Datas = new List<ChartData>();

                    foreach (var key in retData2.Keys)
                    {
                        retDatas.Datas.Add(new ChartData() { type = key, value = retData2[key] });
                    }
                    return retDatas;
                    break;
                case 3:
                    var retData3 = results.GroupBy(c => new { c.Creater.Name,c.CreateTime.Year, c.CreateTime.Month })
                        .Select(g => new { key = g.Key, Count = g.Count() }).OrderByDescending(g => g.Count)
                        .ToDictionary(g => g.key, g => g.Count);

                    retDatas.ATitle = "申诉人";
                    retDatas.BTitle = "绩效月份";
                    retDatas.Title = "申诉人每月申诉统计";
                    retDatas.Datas = new List<ChartData>();

                    foreach (var key in retData3.Keys)
                    {
                        retDatas.Datas.Add(new ChartData() { type = key.Name, year = $" {DateTime.Parse($"{key.Year}-{key.Month}-1").ToString("yyyy-MM")}", value = retData3[key] });
                    }
                    return retDatas;
                    
                    break;
                case 4:
                    var retData4 = results.GroupBy(c => new { c.Type.Name, c.CreateTime.Year, c.CreateTime.Month })
                        .Select(g => new { key = g.Key, Count = g.Count() }).OrderByDescending(g => g.Count)
                        .ToDictionary(g => g.key, g => g.Count);

                    retDatas.ATitle = "申诉类型";
                    retDatas.BTitle = "绩效月份";
                    retDatas.Title = "申诉类型每月申诉统计";
                    retDatas.Datas = new List<ChartData>();

                    foreach (var key in retData4.Keys)
                    {
                        retDatas.Datas.Add(new ChartData() { type = key.Name, year = $" {DateTime.Parse($"{key.Year}-{key.Month}-1").ToString("yyyy-MM")}", value = retData4[key] });
                    }
                    return retDatas;

                    break;
                case 5:
                    var retData5 = results.GroupBy(c => new { c.Creater.Department, c.CreateTime.Year, c.CreateTime.Month })
                        .Select(g => new { key = g.Key, Count = g.Count() }).OrderByDescending(g => g.Count)
                        .ToDictionary(g => g.key, g => g.Count);

                    retDatas.ATitle = "部门";
                    retDatas.BTitle = "绩效月份";
                    retDatas.Title = "部门每月申诉统计";
                    retDatas.Datas = new List<ChartData>();

                    foreach (var key in retData5.Keys)
                    {
                        retDatas.Datas.Add(new ChartData() { type = key.Department, year = $" {DateTime.Parse($"{key.Year}-{key.Month}-1").ToString("yyyy-MM")}", value = retData5[key] });
                    }
                    return retDatas;

                    break;
                default:
                    return null;
                    break;
            }
            
        }

        /// <summary>
        /// 绩效统计报表
        /// </summary>
        /// <param name="type">
        /// 0：按人统计
        /// 1：按部门统计
        /// 2：按人员专案统计
        /// </param>
        /// /// <param name="isYear">
        /// 是否按年统计，默认为false按月统计
        /// </param>
        /// <returns></returns>
        public ChartDatas StaticsReport(int type,bool isYear=false)
        {
            ChartDatas retDatas = new ChartDatas();
            switch (type)
            {
                case 0:
                    
                    if (isYear)
                    {
                        var retYearData = Context.StaffStatistics.GroupBy(s => new { s.Staff.Name, s.CalMonth.Year })
                        .Select(g => new { Name = g.Key.Name, Year = g.Key.Year ,  sum = g.Sum(s => s.FinianlPoint) })
                        .OrderByDescending(g => g.sum);

                        retDatas.ATitle = "代理人";
                        retDatas.BTitle = "绩效年份";
                        retDatas.Title = "代理人每年绩效统计";
                        retDatas.Datas = new List<ChartData>();

                        foreach (var key in retYearData)
                        {
                            retDatas.Datas.Add(new ChartData() { type = key.Name, year = $"{key.Year}", value = key.sum.HasValue ? key.sum.Value : 0 });
                        }
                        return retDatas;
                    }
                    else
                    {
                        var retData = Context.StaffStatistics.GroupBy(s => new { s.Staff.Name, s.CalMonth.Year,s.CalMonth.Month  })
                        .Select(g => new { Name = g.Key.Name,  Year=g.Key.Year,Month=g.Key.Month , sum = g.Sum(s => s.FinianlPoint) })
                        .OrderByDescending(g => g.sum);

                        retDatas.ATitle = "代理人";
                        retDatas.BTitle = "绩效月份";
                        retDatas.Title = "代理人每月绩效统计";
                        retDatas.Datas = new List<ChartData>();

                        foreach (var key in retData)
                        {
                            retDatas.Datas.Add(new ChartData() { type = key.Name, year = $" {DateTime.Parse($"{key.Year}-{key.Month}-1").ToString("yyyy-MM")}", value = key.sum.HasValue ? key.sum.Value : 0 });
                        }
                        return retDatas;

                    }

                    

                    break;
                case 1:
                    if (isYear)
                    {
                        var retData2 = Context.StaffStatistics.GroupBy(s => new { s.Staff.Department, s.CalMonth.Year })
                            .Select(g => new { Name = g.Key.Department, Year = g.Key.Year, sum = g.Sum(s => s.FinianlPoint) })
                            .OrderByDescending(g => g.sum);

                        retDatas.ATitle = "部门";
                        retDatas.BTitle = "绩效月份";
                        retDatas.Title = "部门每月绩效统计";
                        retDatas.Datas = new List<ChartData>();

                        foreach (var key in retData2)
                        {
                            retDatas.Datas.Add(new ChartData() { type = key.Name, year = $"{key.Year }", value = key.sum.HasValue ? key.sum.Value : 0 });
                        }
                        return retDatas;
                    }
                    else
                    {
                        var retData1 = Context.StaffStatistics.GroupBy(s => new { s.Staff.Department, s.CalMonth.Year,s.CalMonth.Month  })
                            .Select(g => new { Name = g.Key.Department, Year = g.Key.Year, Month = g.Key.Month, sum = g.Sum(s => s.FinianlPoint) })
                            .OrderByDescending(g => g.sum);

                        retDatas.ATitle = "部门";
                        retDatas.BTitle = "绩效月份";
                        retDatas.Title = "部门每月绩效统计";
                        retDatas.Datas = new List<ChartData>();

                        foreach (var key in retData1)
                        {
                            retDatas.Datas.Add(new ChartData() { type = key.Name, year = $"{DateTime.Parse($"{key.Year}-{key.Month}-1").ToString("yyyy-MM")}", value = key.sum.HasValue ? key.sum.Value : 0 });
                        }
                        return retDatas;
                    }
                    break;
                case 2:
                    var retData3 = Context.StaffStatistics.Where(s=>s.jxType.Contains("专案")).GroupBy(s => new { s.Staff.Name, s.CalMonth.Year, s.CalMonth.Month })
                        .Select(g => new { Name = g.Key.Name, Year = g.Key.Year, Month = g.Key.Month, sum = g.Sum(s => s.FinianlPoint) })
                        .OrderByDescending(g => g.sum);

                    retDatas.ATitle = "人员";
                    retDatas.BTitle = "绩效月份";
                    retDatas.Title = "人员每月专案绩效统计";
                    retDatas.Datas = new List<ChartData>();

                    foreach (var key in retData3)
                    {
                        retDatas.Datas.Add(new ChartData() { type = key.Name, year = $"{DateTime.Parse($"{key.Year}-{key.Month}-1").ToString("yyyy-MM")}", value = key.sum.HasValue ? key.sum.Value : 0 });
                    }
                    return retDatas;
                    break;
                case 3:
                    break;

            }

            return retDatas;
        }
    }
}
