using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace wispro.sp.share
{
    public class LevelStatisticsResult
    {
        public double doAverage()
        {
            var temList = Statistics.Where(s => s.dS > 0 || s.dA > 0 || s.dB > 0 || s.dC > 0 || s.dD > 0);
            int count = temList.Count();

            double ret = 0.0;

            if (count > 0)
            {
                switch (Grade)
                {
                    case "A":
                        ret = (double)((((decimal)temList.Sum(s => s.dS) * 2.5M) + ((decimal)temList.Sum(s => s.dA) * 1.5M)) / (decimal)count);
                        break;
                    case "B":
                        ret = (double)((((decimal)temList.Sum(s => s.dS) * 2.5M) + ((decimal)temList.Sum(s => s.dA) * 1.5M) + ((decimal)temList.Sum(s => s.dB))) / (decimal)count);
                        break;
                    case "C":
                        ret = (double)((((decimal)temList.Sum(s => s.dS) * 2.5M) + ((decimal)temList.Sum(s => s.dA) * 1.5M) + ((decimal)temList.Sum(s => s.dB)) + ((decimal)temList.Sum(s => s.dC) * 0.7M)) / (decimal)count);
                        break;
                    case "D":
                        ret = (double)((((decimal)temList.Sum(s => s.dS) * 2.5M) + ((decimal)temList.Sum(s => s.dA) * 1.5M) + ((decimal)temList.Sum(s => s.dB)) + ((decimal)temList.Sum(s => s.dC) * 0.7M)) / (decimal)count);
                        break;
                }
            }

            return ret;
        }

        public double vAverage()
        {
            var temList = Statistics.Where(s => s.vS > 0 || s.vA > 0 || s.vB > 0 || s.vC > 0 || s.vD > 0);
            int count = temList.Count();

            double ret = (double)(((decimal)temList.Sum(s => s.dS) * 2.5M + (decimal)temList.Sum(s => s.dA) * 1.5M + (decimal)temList.Sum(s => s.dB) + (decimal)temList.Sum(s => s.dC) * 0.7M) / (decimal)count);

            return ret;
        }

        public double pAverage(int staffId)
        {
            var temObj = Statistics.FirstOrDefault(s => s.StaffId == staffId);

            if(temObj != null)
            {
                switch (Grade)
                {
                    case "A":
                        return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M) / (decimal)doAverage());
                        break;
                    case "B":
                        return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M+(decimal)temObj.dB) / (decimal)doAverage());
                        break;
                    case "C":
                    case "D":
                        return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M + (decimal)temObj.dB + (decimal)temObj.dC * 0.7M) / (decimal)doAverage());
                        break;
                    default:
                        return 0.0;
                }
            }
            else
            {
                return 0.0;
            }
        }

        public double pvAverage(int staffId)
        {
            var temObj = Statistics.FirstOrDefault(s => s.StaffId == staffId);

            if (temObj != null)
            {
                return (double)(((decimal)temObj.vS * 2.5M + (decimal)temObj.vA * 1.5M + (decimal)temObj.vB + (decimal)temObj.vC * 0.7M)/(decimal)vAverage());
            }
            else
            {
                return 0.0;
            }
        }

        public double p2(int staffId)
        {
            var temObj = Statistics.FirstOrDefault(s => s.StaffId == staffId);
            switch (Grade)
            {
                case "B":
                    return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M + (decimal)temObj.dB) / ((decimal)doAverage() * 0.95M));
                    break;
                case "C":
                    return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M + (decimal)temObj.dB + (decimal)temObj.dC * 0.7M) / ((decimal)UpgradeBase*0.7M));
                    break;
                case"D":
                    return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M + (decimal)temObj.dB + (decimal)temObj.dC * 0.7M) / 14M);
                default:
                    return 0.0;
            }
        }

        public double p3(int staffId)
        {
            var temObj = Statistics.FirstOrDefault(s => s.StaffId == staffId);
            switch (Grade)
            {
                case "B":
                    return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M + (decimal)temObj.dB) / ((decimal)doAverage()));
                    break;
                case "C":
                    return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M + (decimal)temObj.dB + (decimal)temObj.dC * 0.7M) / ((decimal)UpgradeBase * 0.8M));
                    break;
                case "D":
                    return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M + (decimal)temObj.dB + (decimal)temObj.dC * 0.7M) / 16M);
                default:
                    return 0.0;
            }
        }

        public double toUpgradeLevel(int staffId)
        {
            var temObj = Statistics.FirstOrDefault(s => s.StaffId == staffId);
            switch (Grade)
            {
                case "B":
                    return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M ) / ((decimal)UpgradeBase));
                    break;
                case "C":
                    return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M + (decimal)temObj.dB ) / ((decimal)UpgradeBase * 0.9M));
                    break;
                case "D":
                    return (double)(((decimal)temObj.dS * 2.5M + (decimal)temObj.dA * 1.5M + (decimal)temObj.dB + (decimal)temObj.dC * 0.7M) / 18M);
                default:
                    return 0.0;
            }
        }

        /// <summary>
        /// 升级基数
        /// </summary>
        public double UpgradeBase { get; set; }

        /// <summary>
        /// 人员等级
        /// </summary>
        public string Grade { get; set; }

        /// <summary>
        /// 个人案件数量统计
        /// </summary>
        public List<LevelStatistics> Statistics { get; set; }
    }

    public class LevelStatistics
    {
        public int StaffId { get; set; }

        public string StaffName { get; set; }

        public string Grade { get; set; }

        public string Department { get; set; }

        public double dS { get; set; }
        public double dA { get; set; }

        public double dB { get; set; }

        public double dC { get; set; }

        public double dD { get; set; }

        public double vS { get; set; }

        public double vA { get; set; }

        public double vB { get; set; }

        public double vC { get; set; }

        public double vD { get; set; }
    }
}
