using APT.BaseData.Domain.Entities.FM;
using APT.Infrastructure.Core;
using Microsoft.AspNetCore.Mvc;
using APT.Infrastructure.Api;
using System.Collections.Generic;
using System.Linq.Expressions;
using System;
using System.Linq;
using APT.BaseData.Domain.ApiModel;
using Ubiety.Dns.Core;
using System.Security.AccessControl;
using APT.MS.Domain.Entities.FO;
using APT.BaseData.Domain.Entities;
using APT.MS.Domain.Enums;
using APT.MS.Domain.Entities.BI;
using static APT.MS.Domain.Enums.BI.BIEnums;
using APT.Utility;
namespace APT.SC.WebApi.Controllers.Api.BI
{
    /// 
    ///  年度安全检查情况综合统计分析
    /// 
    [Route("api/BI/BISafeYear")]
    public class BISafeYearController : AuthorizeApiController
    {
        /// 
        /// 显示
        /// 
        /// 
        /// 
        [HttpPost, Route("BISafeYearShow")]
        public JsonActionResult BISafeYearShow([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                BISafeYear result = new BISafeYear();
                var listCheckLevel = GetEntities(e => e.YEAR == DateTime.Now.Year, null, null);
                if (listCheckLevel != null && listCheckLevel.Any())
                {
                    listCheckLevel = listCheckLevel.OrderBy(e => e.ROW_NO);
                    result.listSafeCheckYear = listCheckLevel.ToList();
                    if (result.listSafeCheckYear != null && result.listSafeCheckYear.Count > 0)
                    {
                        T_BI_BSSAFECHECK_YEAR modelTotal = new T_BI_BSSAFECHECK_YEAR();
                        modelTotal.ID = Guid.NewGuid();
                        modelTotal.ROW_NO = result.listSafeCheckYear.Count;
                        modelTotal.NAME = "合计";
                        modelTotal.COUNTJANUARY = 0;
                        modelTotal.COUNTFEBRUARY = 0;
                        modelTotal.COUNTMARCH = 0;
                        modelTotal.COUNTAPRIL = 0;
                        modelTotal.COUNTMAY = 0;
                        modelTotal.COUNTJUNE = 0;
                        modelTotal.COUNTJULY = 0;
                        modelTotal.COUNTAUGUST = 0;
                        modelTotal.COUNTSEPTEMBER = 0;
                        modelTotal.COUNTOCTOBER = 0;
                        modelTotal.COUNTNOVEMBER = 0;
                        modelTotal.COUNTDECEMBER = 0;
                        modelTotal.COUNTTOTAL = 0;
                        foreach (var item in result.listSafeCheckYear)
                        {
                            modelTotal.COUNTJANUARY += (item.COUNTJANUARY ?? 0);
                            modelTotal.COUNTFEBRUARY += (item.COUNTFEBRUARY ?? 0);
                            modelTotal.COUNTMARCH += (item.COUNTMARCH ?? 0);
                            modelTotal.COUNTAPRIL += (item.COUNTAPRIL ?? 0);
                            modelTotal.COUNTMAY += (item.COUNTMAY ?? 0);
                            modelTotal.COUNTJUNE += (item.COUNTJUNE ?? 0);
                            modelTotal.COUNTJULY += (item.COUNTJULY ?? 0);
                            modelTotal.COUNTAUGUST += (item.COUNTAUGUST ?? 0);
                            modelTotal.COUNTSEPTEMBER += (item.COUNTSEPTEMBER ?? 0);
                            modelTotal.COUNTOCTOBER += (item.COUNTOCTOBER ?? 0);
                            modelTotal.COUNTNOVEMBER += (item.COUNTNOVEMBER ?? 0);
                            modelTotal.COUNTDECEMBER += (item.COUNTDECEMBER ?? 0);
                            modelTotal.COUNTTOTAL += (item.COUNTTOTAL ?? 0);
                        }
                        result.listSafeCheckYear.Add(modelTotal);
                    }
                }
                //总结数据
                IEnumerable listSummery = GetEntities(e => e.CODE == "BI051_BSCOMPANYYEAR", null, null);
                //前12月的数据
                IEnumerable listCheckType = null;
                if (DateTime.Now.Month == 12)
                    listCheckType = GetEntities(e => e.MineType == BSMineTypeEnum.All && e.YEAR == DateTime.Now.Year, null, null);
                else
                    listCheckType = GetEntities(e => e.MineType == BSMineTypeEnum.All && (e.YEAR == DateTime.Now.Year || (e.YEAR == (DateTime.Now.Year - 1) && (e.MONTH > DateTime.Now.Month))), null, null);
                listCheckType = listCheckType.OrderBy(e => e.YEAR).ThenBy(e => e.MONTH);
                if (listSummery != null && listSummery.Any())
                    result.listSummary = listSummery.ToList();
                if (listCheckType != null && listCheckType.Any())
                    result.listCheckTypeYear = listCheckType.ToList();
                var dep = GetEntity(e => !e.PARENT_ID.HasValue && !e.IS_DELETED);
                if (dep != null)
                {
                    result.NAME = dep.NAME;
                }
                return result;
            });
        }
        /// 
        /// 部门级年度安全检查情况综合统计分析
        /// 
        /// 
        /// 
        [HttpPost, Route("BIDepartmentSafeYearShow")]
        public JsonActionResult BIDepartmentSafeYearShow([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                BIDepartmentSafeYear result = new BIDepartmentSafeYear();
                //BSMineTypeEnum MineType = BSMineTypeEnum.Mine;
                List listMineType = new List();
                if (!string.IsNullOrEmpty(filter.Keyword))
                {
                    try
                    {
                        string types = filter.Keyword.Split("=")[1];
                        if (types.Contains("_"))
                        {
                            var listTypes = types.Split("_");
                            //listTypes.foreach ((e) => { listMineType.Add((BSMineTypeEnum)int.Parse(e))})
                            foreach (var item in listTypes)
                            {
                                listMineType.Add((BSMineTypeEnum)int.Parse(item));
                            }
                        }
                        else
                        {
                            listMineType.Add((BSMineTypeEnum)int.Parse(types));
                        }
                    }
                    catch { }
                }
                else
                {
                    listMineType.Add(BSMineTypeEnum.Mine);
                }
                Expression> expression = e => e.YEAR == DateTime.Now.Year;
                //行洛坑 选矿部 = 选矿部 + 尾矿部 看传参 
                if (listMineType.Count == 1)
                {
                    expression = expression.And(e => e.MineType == listMineType[0]);
                }
                else
                {
                    expression = expression.And(e => listMineType.Contains(e.MineType));
                }
                var listCheckLevel = GetEntities(expression, null, null);
                if (listCheckLevel != null && listCheckLevel.Any())
                {
                    listCheckLevel = listCheckLevel.OrderBy(e => e.MineType).ThenBy(e => e.ROW_NO);
                    result.listSafeCheckTypeYear = listCheckLevel.ToList();
                    if (result.listSafeCheckTypeYear != null && result.listSafeCheckTypeYear.Count > 0)
                    {
                        T_BI_BSSAFECHECKTYPE_YEAR modelTotal = new T_BI_BSSAFECHECKTYPE_YEAR();
                        modelTotal.ID = Guid.NewGuid();
                        modelTotal.ROW_NO = result.listSafeCheckTypeYear.Count;
                        modelTotal.NAME = "合计";
                        modelTotal.COUNTJANUARY = 0;
                        modelTotal.COUNTFEBRUARY = 0;
                        modelTotal.COUNTMARCH = 0;
                        modelTotal.COUNTAPRIL = 0;
                        modelTotal.COUNTMAY = 0;
                        modelTotal.COUNTJUNE = 0;
                        modelTotal.COUNTJULY = 0;
                        modelTotal.COUNTAUGUST = 0;
                        modelTotal.COUNTSEPTEMBER = 0;
                        modelTotal.COUNTOCTOBER = 0;
                        modelTotal.COUNTNOVEMBER = 0;
                        modelTotal.COUNTDECEMBER = 0;
                        modelTotal.COUNTTOTAL = 0;
                        foreach (var item in result.listSafeCheckTypeYear)
                        {
                            modelTotal.COUNTJANUARY += (item.COUNTJANUARY ?? 0);
                            modelTotal.COUNTFEBRUARY += (item.COUNTFEBRUARY ?? 0);
                            modelTotal.COUNTMARCH += (item.COUNTMARCH ?? 0);
                            modelTotal.COUNTAPRIL += (item.COUNTAPRIL ?? 0);
                            modelTotal.COUNTMAY += (item.COUNTMAY ?? 0);
                            modelTotal.COUNTJUNE += (item.COUNTJUNE ?? 0);
                            modelTotal.COUNTJULY += (item.COUNTJULY ?? 0);
                            modelTotal.COUNTAUGUST += (item.COUNTAUGUST ?? 0);
                            modelTotal.COUNTSEPTEMBER += (item.COUNTSEPTEMBER ?? 0);
                            modelTotal.COUNTOCTOBER += (item.COUNTOCTOBER ?? 0);
                            modelTotal.COUNTNOVEMBER += (item.COUNTNOVEMBER ?? 0);
                            modelTotal.COUNTDECEMBER += (item.COUNTDECEMBER ?? 0);
                            modelTotal.COUNTTOTAL += (item.COUNTTOTAL ?? 0);
                        }
                        result.listSafeCheckTypeYear.Add(modelTotal);
                    }
                }
                //总结数据
                IEnumerable listSummery = GetEntities(e => listMineType.Contains((BSMineTypeEnum)e.MineType) && e.CODE == "BI052_BSSafeCheckYear", null, null).OrderBy(e => e.NUM).ThenBy(e => e.PART);//e.MineType == (BSMineTypeEditEnum)MineType
                //前12月的数据
                IEnumerable listCheckType = null;
                if (listMineType.Count == 1)
                {
                    if (DateTime.Now.Month == 12)
                        listCheckType = GetEntities(e => e.MineType == listMineType[0] && e.YEAR == DateTime.Now.Year, null, null);
                    else
                        listCheckType = GetEntities(e => e.MineType == listMineType[0] && (e.YEAR == DateTime.Now.Year || (e.YEAR == (DateTime.Now.Year - 1) && (e.MONTH > DateTime.Now.Month))), null, null);
                }
                else
                {
                    if (DateTime.Now.Month == 12)
                        listCheckType = GetEntities(e => listMineType.Contains(e.MineType) && e.YEAR == DateTime.Now.Year, null, null);
                    else
                        listCheckType = GetEntities(e => listMineType.Contains(e.MineType) && (e.YEAR == DateTime.Now.Year || (e.YEAR == (DateTime.Now.Year - 1) && (e.MONTH > DateTime.Now.Month))), null, null);
                }
                listCheckType = listCheckType.OrderBy(e => e.YEAR).ThenBy(e => e.MONTH);
                if (listSummery != null && listSummery.Any())
                {
                    if (listMineType.Count > 1)
                    {
                        //PART 1 3  合并
                        var temp = listSummery.ToList();
                        for (int i = 1; i < temp.Count; i++)
                        {
                            if (temp[i].PART == 1 && temp[i].PART == temp[i - 1].PART)
                            {
                                temp[i - 1].Count1 += temp[i].Count1;
                                temp[i - 1].Count2 += temp[i].Count2;
                                temp[i - 1].Count3 += temp[i].Count3;
                                temp[i - 1].Count4 += temp[i].Count4;
                                if (temp[i - 1].Count2 + temp[i - 1].Count3 == 0 || temp[i - 1].Count1 == 0)
                                {
                                    temp[i - 1].Parameter7 = "0.00%";
                                }
                                else
                                {
                                    temp[i - 1].Parameter7 = Math.Round((100.00 * (temp[i - 1].Count2 + temp[i - 1].Count3) / temp[i - 1].Count1) / 100, 2) + "%";//完成率
                                }
                                if (temp[i - 1].Count2 == 0 || temp[i - 1].Count1 == 0)
                                {
                                    temp[i - 1].Parameter8 = "0.00%";
                                }
                                else
                                {
                                    temp[i - 1].Parameter8 = Math.Round((100.00 * temp[i - 1].Count2 / temp[i - 1].Count1) / 100, 2) + "%";//及时完成率
                                }
                                temp.RemoveAt(i);
                                i--;
                            }
                            else if (temp[i].PART == 3 && temp[i].PART == temp[i - 1].PART)
                            {
                                temp[i - 1].Count1 += temp[i].Count1;
                                temp[i - 1].Count2 += temp[i].Count2;
                                temp[i - 1].Count3 += temp[i].Count3;
                                temp[i - 1].Parameter7 = Math.Round((100.00 * (temp[i - 1].Count2 + temp[i - 1].Count3) / temp[i - 1].Count1) / 100, 2) + "%";//整改率
                                temp.RemoveAt(i);
                                i--;
                            }
                        }
                        result.listSummary = temp;
                    }
                    else
                    {
                        result.listSummary = listSummery.ToList();
                    }
                }
                if (listCheckType != null && listCheckType.Any())
                {
                    if (listMineType.Count > 0)
                    {
                        var Temp = listCheckType.ToList();
                        for (int i = 1; i < Temp.Count; i++)
                        {
                            if (Temp[i].YEAR == Temp[i - 1].YEAR && Temp[i].MONTH == Temp[i - 1].MONTH)
                            {
                                Temp[i - 1].ALLCOUNT += Temp[i].ALLCOUNT;
                                Temp[i - 1].OUTTIMECOUNT += Temp[i].OUTTIMECOUNT;
                                Temp[i - 1].INTTIMECOUNT += Temp[i].INTTIMECOUNT;
                                Temp[i - 1].NOTFINISHCOUN += Temp[i].NOTFINISHCOUN;
                                if (Temp[i - 1].ALLCOUNT == 0)
                                {
                                    Temp[i - 1].FINISHPERCENT = 100;
                                }
                                else if (Temp[i - 1].OUTTIMECOUNT + Temp[i - 1].INTTIMECOUNT == 0)
                                {
                                    Temp[i - 1].FINISHPERCENT = 0;
                                }
                                else
                                {
                                    Temp[i - 1].FINISHPERCENT = (decimal)Math.Round((100.00 * (Temp[i - 1].OUTTIMECOUNT + Temp[i - 1].INTTIMECOUNT) / Temp[i - 1].ALLCOUNT), 2);
                                }
                                Temp.RemoveAt(i);
                                i--;
                            }
                        }
                        result.listCheckTypeYear = Temp;
                    }
                    else
                    {
                        result.listCheckTypeYear = listCheckType.ToList();
                    }
                }
                if (result.listCheckTypeYear == null)
                {
                    result.listCheckTypeYear = new List();
                }
                if (result.listSafeCheckTypeYear == null)
                {
                    result.listSafeCheckTypeYear = new List();
                }
                return result;
                //var dep = GetEntity(e => !e.PARENT_ID.HasValue && !e.IS_DELETED);
                //if (dep != null)
                //{
                //    result.NAME = dep.NAME;
                //}
            });
        }
    }
    public class BISafeYear
    {
        public List listSafeCheckYear { get; set; }
        public List listCheckTypeYear { get; set; }
        public List listSummary { get; set; }
        /// 
        /// 公司名称
        /// 
        public string NAME { get; set; }
    }
    public class BIDepartmentSafeYear
    {
        public List listSafeCheckTypeYear { get; set; }
        public List listCheckTypeYear { get; set; }
        public List listSummary { get; set; }
        /// 
        /// 公司名称
        /// 
        public string NAME { get; set; }
    }
}