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;
using APT.MS.Domain.Entities.BS;

namespace APT.SC.WebApi.Controllers.Api.BI
{
    /// <summary>
    ///  年度监测汇总
    /// </summary>
    [Route("api/BI/BISafe")]
    public class BISafeController : AuthorizeApiController<T_BI_BSSAFECHECK>
    {
        //IBIBSSafeCheckService BIBSSafeCheckService { get; set; }
        //public BISafeController(IBIBSSafeCheckService bIBSSafeCheckService)
        //{
        //    BIBSSafeCheckService = bIBSSafeCheckService;
        //}

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetShow")]
        public JsonActionResult<BISafe> GetShow([FromBody] KeywordFilter filter)
        {
            return SafeExecute<BISafe>(() =>
            {
                BISafe result = new BISafe();
                string Parameter1 = string.Empty;
                string Parameter2 = string.Empty;
                if (filter.FilterGroup.Rules.Count == 2)
                {
                    foreach (var item in filter.FilterGroup.Rules)
                    {
                        if (item.Field == "Parameter1")
                        {
                            Parameter1 = item.Value.ToString();
                        }
                        else if (item.Field == "Parameter2")
                        {
                            if (item.Value == null)
                            {
                                throw new Exception("参数2为空！");
                            }
                            else
                            {
                                Parameter2 = item.Value.ToString();
                            }
                        }
                    }
                }

                if (!string.IsNullOrEmpty(Parameter1) || !string.IsNullOrEmpty(Parameter2))
                {
                    string Code = "BI002_BSSAFECHECK"; //编码
                    FilterTimeType? timeType = null;
                    BSMineTypeEditEnum? mineType = null;
                    try
                    {
                        timeType = (FilterTimeType)(int.Parse(Parameter1));
                    }
                    catch { }
                    try
                    {
                        mineType = (BSMineTypeEditEnum)(int.Parse(Parameter2));
                    }
                    catch { }

                    if (timeType != null && mineType != null)
                    {
                        var listSafeCheck = GetEntities<T_BI_BSSAFECHECK>(e => e.ORG_ID == filter.OrgId.Value && e.TimeType == timeType.Value && e.MineType == mineType.Value, null).OrderBy(e => e.TEAMNAME).ToList();
                        var listCheckType = GetEntities<T_BI_BSCHECKTYPE>(e => e.ORG_ID == filter.OrgId.Value && e.TimeType == timeType.Value && e.MineType == mineType.Value, null).OrderBy(e => e.name).ToList();
                        var summary = GetEntity<T_BI_SUMMARY>(e => e.ORG_ID == filter.OrgId.Value && e.TimeType == timeType.Value && e.MineType == mineType.Value && e.CODE == Code);
                        result.listSafeCheck = listSafeCheck;
                        result.listCheckType = listCheckType;
                        result.summary = summary;

                    }
                }
                return result;
            });
        }


        #region      安全检查绩效报表

        #region    主界面与各弹窗的方法

        /// <summary>
        /// 显示  
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetBI056Main")]
        public JsonActionResult<ModelBI056Main> GetBI056Main([FromBody] KeywordFilter filter)
        {
            return SafeExecute<ModelBI056Main>(() =>
            {
                ModelBI056Main result = new ModelBI056Main();
                DateTime dtStart = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-01-01 00:00:00");
                DateTime? dtEnd = null;

                #region  enmusLevel=> lisLevelOrder   enmusMineType=>lisMineTypeOrder 

                //var enmus = GetEntities<T_FM_ENUMS>(e => !e.IS_DELETED, null, "ID", "VALUE", "NUM", "CODE", "NAME");
                var enmus = GetEntities<T_FM_ENUMS>(e => !e.IS_DELETED, null, null);
                if (enmus == null || !enmus.Any())
                    throw new Exception("获取生成单元、组织层级等枚举信息失败！");
                List<string> lisLevelOrder = new List<string>();
                List<string> lisMineTypeOrder = new List<string>();
                var enmusLevel = enmus.Where(e => e.CODE == "HMLevleEnum").OrderBy(e => e.NUM).ToList();
                var enmusMineType = enmus.Where(e => e.CODE == "BSMineTypeEnum").OrderBy(e => e.NUM).ToList();

                if (enmusLevel == null || !enmusLevel.Any())
                    throw new Exception("获取组织层级枚举信息失败！");
                else
                {
                    foreach (var item in enmusLevel)
                    {
                        lisLevelOrder.Add(item.NAME);
                    }
                }
                if (enmusMineType == null || !enmusMineType.Any())
                    throw new Exception("获取生成单元枚举信息失败！");
                else
                {
                    foreach (var item in enmusMineType)
                    {
                        lisMineTypeOrder.Add(item.NAME);
                    }
                }
                #endregion

                #region 改良版 listSafeCheck 所有的符合条件的检查信息

                Expression<Func<T_BS_SAFE_CHECK, bool>> expression = e => !e.IS_DELETED && e.CHECKOBJECT.HasValue && e.CHECK_TYPE_LEVEL_ID.HasValue && e.CHECKTIME.HasValue && dtStart <= e.CHECKTIME.Value;

                if (dtEnd.HasValue)
                    expression = expression.And(e => e.CHECKTIME.HasValue && dtEnd.Value >= e.CHECKTIME.Value);

                BaseFilter filterSearch = new BaseFilter(filter.OrgId);
                filterSearch.IgnoreDataRule = true;
                filterSearch.Include.Add("Nav_CheckTypeLevel");
                filterSearch.SelectField = new List<string>() { "ID", "CHECKOBJECT", "CHECK_TYPE_ID", "IS_DELETED", "CHECKTIME", "Nav_CheckTypeLevel.CheckLevel_ENUMS_ID" };
                //var listSafeCheck = GetEntities(expression, null, "Nav_CheckTypeLevel");
                var listSafeCheck = GetEntities(expression, filterSearch, "Nav_CheckTypeLevel");

                #endregion

                #region    1_1 改良版

                List<List<int>> listCheckCount = new List<List<int>>();
                List<int> listCheckLevelCount = null;
                foreach (var item in enmusLevel)
                {
                    //mineTypeName: ["露天矿", "选矿厂", "尾矿库", "职能部门"],
                    //legendName: ["公司级", "部门级", "车间级", "班组级"],
                    //checkCount: [[3, 3, 3, 3], [12, 12, 12, 6], [24, 24, 24, 12], [90, 90, 120, 36]],

                    listCheckLevelCount = new List<int>();
                    //选矿 10,  尾矿  20, 矿山  30,  地下矿  40,
                    foreach (var itemMine in enmusMineType)
                    {
                        int count = listSafeCheck.Where(e => e.CHECKOBJECT == itemMine.VALUE && e.Nav_CheckTypeLevel.CheckLevel_ENUMS_ID == item.ID).Count();
                        listCheckLevelCount.Add(count);
                    }
                    listCheckCount.Add(listCheckLevelCount);
                }

                //数据组装
                result.Count1_1 = listCheckCount;
                result.lisMineTypeOrder = lisMineTypeOrder;
                result.lisLevelOrder = lisLevelOrder;

                #endregion



                #region  3_1 改良版


                List<List<int>> listCheckTypeCount = new List<List<int>>();
                List<int> listCheckTypeCountPer = null;

                var listCheckType = GetEntities<T_BS_CHECK_TYPE>(e => !e.IS_DELETED, null).OrderBy(e => e.PARENT_ID).ThenBy(e => e.NUM);
                List<string> listCheckTypeName = new List<string>();
                List<Guid> listCheckTypeID = new List<Guid>();
                Dictionary<Guid, List<Guid>> dicParent = new Dictionary<Guid, List<Guid>>();
                List<Guid> listSum = new List<Guid>();
                Guid guidParent = Guid.Empty;
                foreach (var item in listCheckType)
                {
                    if (item.PARENT_ID.HasValue)
                    {
                        if (!listCheckTypeName.Contains(item.NAME))
                        {
                            listCheckTypeName.Add(item.NAME);
                            listCheckTypeID.Add(item.ID);
                            guidParent = item.PARENT_ID.Value;
                        }
                        listSum.Add(item.ID);
                    }
                    else
                    {
                        listCheckTypeName.Add(item.NAME);
                        listCheckTypeID.Add(item.ID);
                    }
                }
                listSum.Add(guidParent);//有些人直接定义 综合检查
                dicParent.Add(guidParent, listSum);

                foreach (var item in enmusMineType)
                {
                    listCheckTypeCountPer = new List<int>();

                    foreach (var itemCheckType in listCheckTypeID)
                    {

                        //检查类型
                        if (itemCheckType == guidParent)
                        {
                            int count = listSafeCheck.Where(e => e.CHECKOBJECT == item.VALUE && listSum.Contains(e.CHECK_TYPE_ID.Value)).Count();
                            listCheckTypeCountPer.Add(count);
                        }
                        else
                        {
                            int count = listSafeCheck.Where(e => e.CHECKOBJECT == item.VALUE && e.CHECK_TYPE_ID == itemCheckType).Count();
                            listCheckTypeCountPer.Add(count);
                        }
                    }
                    listCheckTypeCount.Add(listCheckTypeCountPer);
                }

                //数据组装
                result.Count3_1 = listCheckTypeCount;
                result.lisCheckTypeName = listCheckTypeName;
                #endregion

                #region      隐患  1_2    2_1    2_2    3_2    

                Expression<Func<T_BS_RISK_SUBMIT_CONTENT, bool>> expression2_2 = e => !e.IS_DELETED && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value >= dtStart  && e.RiskContentState.HasValue && !e.IS_DELETED;// && (e.RiskContentState.Value == 60 || e.RiskContentState.Value == 80)&& e.DESCREPTION != null
                if (dtEnd.HasValue)
                    expression2_2 = expression2_2.And(e => e.CREATE_TIME.HasValue && dtEnd.Value >= e.CREATE_TIME.Value);

                var listContent = GetEntities(expression2_2, null, "Nav_Submit");

                #region    1_2   各生产单元 隐患数

                List<int> listRiskMineCount1_2 = new List<int>();
                foreach (var item in enmusMineType)
                {
                    listRiskMineCount1_2.Add(listContent.Where(e => e.Nav_Submit != null && e.Nav_Submit.CHECKOBJECT.HasValue && e.Nav_Submit.CHECKOBJECT.Value == (BSMineTypeEnum)item.VALUE).Count());
                }
                result.listRiskMineCount1_2 = listRiskMineCount1_2;
                #endregion

                #region     2_1 月度  已完成 未完成 整改率

                List<int> listMonth = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12 };
                if (dtStart.Year > DateTime.Now.Year)
                {
                    listMonth.Clear();
                }
                else if (dtStart.Year == DateTime.Now.Year)
                {
                    listMonth = listMonth.Where(e => e <= DateTime.Now.Month).OrderBy(e => e).ToList();
                }

                List<int> listFinish2_1 = new List<int>();
                List<int> listNotFinish2_1 = new List<int>();
                List<decimal> listFinish2_1Percent = new List<decimal>();
                foreach (var item in listMonth)
                {
                    listFinish2_1.Add(listContent.Where(e => e.CREATE_TIME.HasValue && e.CREATE_TIME.Value.Month == item && (e.RiskContentState.Value == 60 || e.RiskContentState.Value == 80)).Count());
                    listNotFinish2_1.Add(listContent.Where(e => e.CREATE_TIME.HasValue && e.CREATE_TIME.Value.Month == item && e.RiskContentState.Value != 60 && e.RiskContentState.Value != 80).Count());
                }
                for (int i = 0; i < listMonth.Count; i++)
                {
                    if (listNotFinish2_1[i] == 0)
                    {
                        listFinish2_1Percent.Add(100);
                    }
                    else if (listFinish2_1[i] == 0)
                    {
                        listFinish2_1Percent.Add(0);
                    }
                    else
                    {
                        listFinish2_1Percent.Add((decimal)Math.Round((listFinish2_1[i] * 100.0 / (listNotFinish2_1[i] + listFinish2_1[i])), 2));
                    }
                }


                //result.listMonth = listMonth;
                result.listFinish2_1 = listFinish2_1;
                result.listNotFinish2_1 = listNotFinish2_1;
                result.listFinish2_1Percent = listFinish2_1Percent;


                #endregion

                #region   2_2  隐患Top
                var query = listContent.GroupBy(e => e.DESCREPTION).Select(e => new
                {
                    DESCREPTION = e.Key,
                    Count = e.Count()
                }).OrderByDescending(e => e.Count).Take(6);

                if (query != null && query.Any())
                {
                    List<string> ListDescreption2_2 = new List<string>();
                    List<int> Count2_2 = new List<int>();
                    foreach (var item in query)
                    {
                        Count2_2.Add(item.Count);
                        ListDescreption2_2.Add(item.DESCREPTION);
                    }
                    result.Count2_2 = Count2_2;
                    result.ListDescreption2_2 = ListDescreption2_2;
                }
                else
                {
                    result.Count2_2 = new List<int>();
                    result.ListDescreption2_2 = new List<string>();
                }
                #endregion

                #region    3_2 检查类型隐患统计

                List<List<int>> listRiskMoneCheckType = new List<List<int>>();
                foreach (var item in enmusMineType)
                {
                    listCheckTypeCountPer = new List<int>();
                    foreach (var itemCheckType in listCheckTypeID)
                    {
                        //检查类型
                        if (itemCheckType == guidParent)
                        {
                            int count = listContent.Where(e => e.Nav_Submit != null && e.Nav_Submit.CHECK_TYPE_ID.HasValue && e.Nav_Submit.CHECKOBJECT.HasValue && e.Nav_Submit.CHECKOBJECT == (BSMineTypeEnum)item.VALUE && listSum.Contains(e.Nav_Submit.CHECK_TYPE_ID.Value)).Count();
                            listCheckTypeCountPer.Add(count);
                        }
                        else
                        {
                            int count = listContent.Where(e => e.Nav_Submit != null && e.Nav_Submit.CHECK_TYPE_ID.HasValue && e.Nav_Submit.CHECKOBJECT.HasValue && e.Nav_Submit.CHECKOBJECT == (BSMineTypeEnum)item.VALUE && e.Nav_Submit.CHECK_TYPE_ID == itemCheckType).Count();
                            listCheckTypeCountPer.Add(count);
                        }
                    }
                    listRiskMoneCheckType.Add(listCheckTypeCountPer);
                }
                result.Count3_2 = listRiskMoneCheckType;

                #endregion

                #endregion


                return result;
            });
        }

        //#region     旧版直接查询 count

        ///// <summary>
        ///// 显示  
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <returns></returns>
        //[HttpPost, Route("GetBI056Main")]
        //public JsonActionResult<ModelBI056Main> GetBI056Main([FromBody] KeywordFilter filter)
        //{
        //    return SafeExecute<ModelBI056Main>(() =>
        //    {
        //        ModelBI056Main result = new ModelBI056Main();

        //        DateTime dtStart = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-01-01 00:00:00");
        //        DateTime? dtEnd = null;

        //        #region  enmusLevel=> lisLevelOrder   enmusMineType=>lisMineTypeOrder 

        //        //var enmus = GetEntities<T_FM_ENUMS>(e => !e.IS_DELETED, null, "ID", "VALUE", "NUM", "CODE", "NAME");
        //        var enmus = GetEntities<T_FM_ENUMS>(e => !e.IS_DELETED, null, null);
        //        if (enmus == null || !enmus.Any())
        //            throw new Exception("获取生成单元、组织层级等枚举信息失败！");
        //        List<string> lisLevelOrder = new List<string>();
        //        List<string> lisMineTypeOrder = new List<string>();
        //        var enmusLevel = enmus.Where(e => e.CODE == "HMLevleEnum").OrderBy(e => e.NUM).ToList();
        //        var enmusMineType = enmus.Where(e => e.CODE == "BSMineTypeEnum").OrderBy(e => e.NUM).ToList();

        //        if (enmusLevel == null || !enmusLevel.Any())
        //            throw new Exception("获取组织层级枚举信息失败！");
        //        else
        //        {
        //            foreach (var item in enmusLevel)
        //            {
        //                lisLevelOrder.Add(item.NAME);
        //            }
        //        }
        //        if (enmusMineType == null || !enmusMineType.Any())
        //            throw new Exception("获取生成单元枚举信息失败！");
        //        else
        //        {
        //            foreach (var item in enmusMineType)
        //            {
        //                lisMineTypeOrder.Add(item.NAME);
        //            }
        //        }
        //        #endregion

        //        #region    1_1   listCheckCount(List<List<int>>)

        //        Expression<Func<T_BS_SAFE_CHECK, bool>> expression = e => !e.IS_DELETED && e.CHECKOBJECT.HasValue && e.CHECK_TYPE_LEVEL_ID.HasValue && e.CHECKTIME.HasValue && dtStart <= e.CHECKTIME.Value;

        //        if (dtEnd.HasValue)
        //            expression = expression.And(e => e.CHECKTIME.HasValue && dtEnd.Value >= e.CHECKTIME.Value);

        //        //BSMineTypeEditEnum  选矿 10,  尾矿  20, 矿山  30,  地下矿  40,
        //        //FMDepartmentType    层级 3公司级 0部门级 1车间级 2班组级

        //        //expression 不能组合 
        //        //公司级 部门级 车间级 班组级 用filter过滤 
        //        BaseFilter filterSearch1_1 = new BaseFilter(filter.OrgId);
        //        filterSearch1_1.IgnoreDataRule = true;
        //        filterSearch1_1.Include.Add("Nav_CheckTypeLevel");

        //        FilterGroup filterGroup = new FilterGroup();
        //        filterGroup.IsAnd = true;

        //        FilterRule filterGroupRule = new FilterRule();
        //        filterGroupRule.Field = "Nav_CheckTypeLevel.CheckLevel_ENUMS_ID";

        //        FilterRule filterGroupMineRule = new FilterRule();
        //        filterGroupMineRule.Field = "CHECKOBJECT";

        //        List<List<int>> listCheckCount = new List<List<int>>();
        //        List<int> listCheckLevelCount = null;
        //        foreach (var item in enmusLevel)
        //        {
        //            //mineTypeName: ["露天矿", "选矿厂", "尾矿库", "职能部门"],
        //            //legendName: ["公司级", "部门级", "车间级", "班组级"],
        //            //checkCount: [[3, 3, 3, 3], [12, 12, 12, 6], [24, 24, 24, 12], [90, 90, 120, 36]],

        //            listCheckLevelCount = new List<int>();
        //            //选矿 10,  尾矿  20, 矿山  30,  地下矿  40,
        //            foreach (var itemMine in enmusMineType)
        //            {
        //                if (filterGroup.Rules != null && filterGroup.Rules.Any())
        //                    filterGroup.Rules.Clear();

        //                filterGroupRule.Value = item.ID;
        //                filterGroup.Rules.Add(filterGroupRule);
        //                filterGroupMineRule.Value = itemMine.VALUE;
        //                filterGroup.Rules.Add(filterGroupMineRule);

        //                filterSearch1_1.FilterGroup = filterGroup;

        //                int count = GetCount(expression, filterSearch1_1);
        //                listCheckLevelCount.Add(count);
        //            }
        //            listCheckCount.Add(listCheckLevelCount);
        //        }

        //        //数据组装
        //        result.Count1_1 = listCheckCount;
        //        result.lisMineTypeOrder = lisMineTypeOrder;
        //        result.lisLevelOrder = lisLevelOrder;

        //        #endregion

        //        #region 3_1

        //        List<List<int>> listCheckTypeCount = new List<List<int>>();
        //        List<int> listCheckTypeCountPer = null;

        //        var listCheckType = GetEntities<T_BS_CHECK_TYPE>(e => !e.IS_DELETED, null).OrderBy(e => e.PARENT_ID).ThenBy(e => e.NUM);
        //        List<string> listCheckTypeName = new List<string>();
        //        List<Guid> listCheckTypeID = new List<Guid>();
        //        Dictionary<Guid, List<Guid>> dicParent = new Dictionary<Guid, List<Guid>>();
        //        List<Guid> listSum = new List<Guid>();
        //        Guid guidParent = Guid.Empty;
        //        foreach (var item in listCheckType)
        //        {
        //            if (item.PARENT_ID.HasValue)
        //            {
        //                if (!listCheckTypeName.Contains(item.NAME))
        //                {
        //                    listCheckTypeName.Add(item.NAME);
        //                    listCheckTypeID.Add(item.ID);
        //                    guidParent = item.PARENT_ID.Value;
        //                }
        //                listSum.Add(item.ID);
        //            }
        //            else
        //            {
        //                listCheckTypeName.Add(item.NAME);
        //                listCheckTypeID.Add(item.ID);
        //            }
        //        }
        //        listSum.Add(guidParent);//有些人直接定义 综合检查
        //        dicParent.Add(guidParent, listSum);


        //        Expression<Func<T_BS_SAFE_CHECK, bool>> expression3_1 = e => !e.IS_DELETED && e.CHECKOBJECT.HasValue && e.CHECK_TYPE_ID.HasValue && e.CHECKTIME.HasValue && dtStart <= e.CHECKTIME.Value;

        //        if (dtEnd.HasValue)
        //            expression3_1 = expression3_1.And(e => e.CHECKTIME.HasValue && dtEnd.Value >= e.CHECKTIME.Value);


        //        Expression<Func<T_BS_SAFE_CHECK, bool>> expression3_1_Contains = expression3_1.And(e => listSum.Contains(e.CHECK_TYPE_ID.Value));


        //        BaseFilter filterSearch3_1 = new BaseFilter(filter.OrgId);
        //        filterSearch3_1.IgnoreDataRule = true;

        //        filterGroupRule = new FilterRule();
        //        filterGroupRule.Field = "CHECK_TYPE_ID";

        //        filterGroupMineRule = new FilterRule();
        //        filterGroupMineRule.Field = "CHECKOBJECT";

        //        foreach (var item in enmusMineType)
        //        {
        //            listCheckTypeCountPer = new List<int>();

        //            foreach (var itemCheckType in listCheckTypeID)
        //            {
        //                if (filterGroup.Rules != null && filterGroup.Rules.Any())
        //                    filterGroup.Rules.Clear();

        //                //检查类型
        //                if (itemCheckType == guidParent)
        //                {
        //                    //报错  expression3_1 => expression3_1_Contains
        //                    //filterGroupRule.Operate = FilterOperate.Contains;
        //                    //filterGroupRule.Value = listSum;

        //                    //生产单元
        //                    filterGroupMineRule.Value = item.VALUE;
        //                    filterGroup.Rules.Add(filterGroupMineRule);
        //                    filterSearch3_1.FilterGroup = filterGroup;

        //                    int count = GetCount(expression3_1_Contains, filterSearch3_1);
        //                    listCheckTypeCountPer.Add(count);
        //                }
        //                else
        //                {
        //                    filterGroupRule.Value = itemCheckType;

        //                    filterGroup.Rules.Add(filterGroupRule);

        //                    //生产单元
        //                    filterGroupMineRule.Value = item.VALUE;
        //                    filterGroup.Rules.Add(filterGroupMineRule);

        //                    filterSearch3_1.FilterGroup = filterGroup;

        //                    int count = GetCount(expression3_1, filterSearch3_1);
        //                    listCheckTypeCountPer.Add(count);
        //                }
        //                //filterGroup.Rules.Add(filterGroupRule);

        //                ////生产单元
        //                //filterGroupMineRule.Value = item.VALUE;
        //                //filterGroup.Rules.Add(filterGroupMineRule);

        //                //filterSearch3_1.FilterGroup = filterGroup;

        //                //int count = GetCount(expression3_1, filterSearch3_1);
        //                //listCheckTypeCountPer.Add(count);
        //            }
        //            listCheckTypeCount.Add(listCheckTypeCountPer);
        //        }
        //        result.Count3_1 = listCheckTypeCount;
        //        result.lisCheckTypeName = listCheckTypeName;

        //        #endregion

        //        return result;
        //    });
        //}
        //#endregion

        #region    各弹窗方法

        #region      1_1  GetBI056_1_1
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetBI056_1_1")]
        public JsonActionResult<ModelBI056_1_1> GetBI056_1_1([FromBody] KeywordFilter filter)
        {
            return SafeExecute<ModelBI056_1_1>(() =>
            {
                ModelBI056_1_1 result = new ModelBI056_1_1();
                int year = DateTime.Now.Year;
                int month = DateTime.Now.Month;
                List<int> listMonth = null;
                DateTime dtStart = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-01-01 00:00:00");
                DateTime? dtEnd = null;

                if (!string.IsNullOrEmpty(filter.Keyword))
                {
                    var season = int.Parse(filter.Keyword);
                    switch (season)
                    {
                        case 1:
                            listMonth = new List<int>() { 1, 2, 3 };
                            break;
                        case 2:
                            listMonth = new List<int>() { 4, 5, 6 };
                            break;
                        case 3:
                            listMonth = new List<int>() { 7, 8, 9 };
                            break;
                        case 4:
                            listMonth = new List<int>() { 10, 11, 12 };
                            break;
                        default:
                            break;
                    }

                    dtStart = dtStart.AddMonths(3 * (season - 1));
                    dtEnd = dtStart.AddMonths(3).AddSeconds(-1);
                }


                #region  enmusLevel=> lisLevelOrder   enmusMineType=>lisMineTypeOrder 

                var enmus = GetEntities<T_FM_ENUMS>(e => !e.IS_DELETED, null, null);
                if (enmus == null || !enmus.Any())
                    throw new Exception("获取生成单元、组织层级等枚举信息失败！");
                List<string> lisLevelOrder = new List<string>();
                List<string> lisMineTypeOrder = new List<string>();
                var enmusLevel = enmus.Where(e => e.CODE == "HMLevleEnum").OrderBy(e => e.NUM).ToList();
                var enmusMineType = enmus.Where(e => e.CODE == "BSMineTypeEnum").OrderBy(e => e.NUM).ToList();

                if (enmusLevel == null || !enmusLevel.Any())
                    throw new Exception("获取组织层级枚举信息失败！");
                else
                {
                    foreach (var item in enmusLevel)
                    {
                        lisLevelOrder.Add(item.NAME);
                    }
                }
                if (enmusMineType == null || !enmusMineType.Any())
                    throw new Exception("获取生成单元枚举信息失败！");
                else
                {
                    foreach (var item in enmusMineType)
                    {
                        lisMineTypeOrder.Add(item.NAME);
                    }
                }

                #endregion

                List<List<int>> listResult = new List<List<int>>();
                List<int> listResultPer = null;
                int count = 0;



                #region 改良版 listSafeCheck 所有的符合条件的检查信息

                Expression<Func<T_BS_SAFE_CHECK, bool>> expression = e => !e.IS_DELETED && e.CHECKOBJECT.HasValue && e.CHECK_TYPE_LEVEL_ID.HasValue && e.CHECKTIME.HasValue && dtStart <= e.CHECKTIME.Value;

                if (dtEnd.HasValue)
                    expression = expression.And(e => e.CHECKTIME.HasValue && dtEnd.Value >= e.CHECKTIME.Value);

                BaseFilter filterSearch = new BaseFilter(filter.OrgId);
                filterSearch.IgnoreDataRule = true;
                filterSearch.Include.Add("Nav_CheckTypeLevel");
                filterSearch.SelectField = new List<string>() { "ID", "CHECKOBJECT", "CHECK_TYPE_ID", "IS_DELETED", "CHECKTIME", "Nav_CheckTypeLevel.CheckLevel_ENUMS_ID" };
                //var listSafeCheck = GetEntities(expression, null, "Nav_CheckTypeLevel");
                var listSafeCheck = GetEntities(expression, filterSearch, "Nav_CheckTypeLevel");

                #endregion

                foreach (var item in enmusLevel)
                {
                    listResultPer = new List<int>();
                    foreach (var itemMonth in listMonth)
                    {
                        if (itemMonth > month && dtStart.Year == year)//同年 如果月份 大于当前月   直接跳出计算
                        {
                            break;
                        }
                        //层级 各 生产单元  几月到几月 安全检查数量
                        foreach (var itemMine in enmusMineType)
                        {
                            count = listSafeCheck.Where(e => e.CHECKOBJECT == itemMine.VALUE && e.Nav_CheckTypeLevel.CheckLevel_ENUMS_ID == item.ID && e.CHECKTIME.Value.Month == itemMonth).Count();
                            listResultPer.Add(count);
                        }
                    }
                    listResult.Add(listResultPer);
                }

                //数据组装
                result.Count = listResult;
                result.lisMineTypeOrder = lisMineTypeOrder;
                result.lisLevelOrder = lisLevelOrder;

                return result;
            });
        }

        #endregion

        #region   1_2  GetBI056_1_2

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetBI056_1_2")]
        public JsonActionResult<ModelBI056_1_2> GetBI056_1_2([FromBody] KeywordFilter filter)
        {
            return SafeExecute<ModelBI056_1_2>(() =>
            {
                ModelBI056_1_2 result = new ModelBI056_1_2();
                DateTime dtStart = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-01-01 00:00:00");
                DateTime? dtEnd = null;

                List<string> lisMineTypeOrder = new List<string>();
                var enmusMineType = GetEntities<T_FM_ENUMS>(e => !e.IS_DELETED && e.CODE == "BSMineTypeEnum", null, null).OrderBy(e => e.NUM).ToList();

                if (enmusMineType == null || !enmusMineType.Any())
                    throw new Exception("获取生成单元枚举信息失败！");
                else
                {
                    foreach (var item in enmusMineType)
                    {
                        lisMineTypeOrder.Add(item.NAME);
                    }
                }

                List<int> listMonth = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12 };
                if (dtStart.Year > DateTime.Now.Year)
                {
                    listMonth.Clear();
                }
                else if (dtStart.Year == DateTime.Now.Year)
                {
                    listMonth = listMonth.Where(e => e <= DateTime.Now.Month).OrderBy(e => e).ToList();
                }

                List<List<int>> ListCount = new List<List<int>>();
                Expression<Func<T_BS_RISK_SUBMIT_CONTENT, bool>> expression = e => !e.IS_DELETED  && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value >= dtStart;//&& e.DESCREPTION != null
                if (dtEnd.HasValue)
                    expression = expression.And(e => e.CREATE_TIME.HasValue && dtEnd.Value >= e.CREATE_TIME.Value);

                var listContent = GetEntities(expression, null, "Nav_Submit");
                BSMineTypeEnum? mineType = null;
                List<int> ListCountPer = null;
                foreach (var item in enmusMineType)
                {
                    mineType = (BSMineTypeEnum)item.VALUE;
                    ListCountPer = new List<int>();
                    foreach (var itemMonth in listMonth)
                    {
                        ListCountPer.Add(listContent.Count(e => e.Nav_Submit.CHECKOBJECT.HasValue && e.Nav_Submit.CHECKOBJECT.Value == mineType && e.CREATE_TIME.Value.Month == itemMonth));
                    }
                    ListCount.Add(ListCountPer);
                }

                //数据组装
                result.lisMineTypeOrder = lisMineTypeOrder;
                result.ListCount = ListCount;
                return result;
            });
        }

        #endregion

        #region   2_1  GetBI056_2_1

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetBI056_2_1")]
        public JsonActionResult<ModelBI056_2_1> GetBI056_2_1([FromBody] KeywordFilter filter)
        {
            return SafeExecute<ModelBI056_2_1>(() =>
            {
                ModelBI056_2_1 result = new ModelBI056_2_1();
                DateTime dtStart = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-01-01 00:00:00");
                DateTime? dtEnd = null;

                List<string> lisMineTypeOrder = new List<string>();
                var enmusMineType = GetEntities<T_FM_ENUMS>(e => !e.IS_DELETED && e.CODE == "BSMineTypeEnum", null, null).OrderBy(e => e.NUM).ToList();

                if (enmusMineType == null || !enmusMineType.Any())
                    throw new Exception("获取生成单元枚举信息失败！");
                else
                {
                    foreach (var item in enmusMineType)
                    {
                        lisMineTypeOrder.Add(item.NAME);
                    }
                }

                Expression<Func<T_BS_RISK_SUBMIT_CONTENT, bool>> expression2_2 = e => !e.IS_DELETED && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value >= dtStart  && e.RiskContentState.HasValue && !e.IS_DELETED;//&& e.DESCREPTION != null
                if (dtEnd.HasValue)
                    expression2_2 = expression2_2.And(e => e.CREATE_TIME.HasValue && dtEnd.Value >= e.CREATE_TIME.Value);

                var listContent = GetEntities(expression2_2, null, "Nav_Submit");
                List<int> listMonth = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12 };
                if (dtStart.Year > DateTime.Now.Year)
                {
                    listMonth.Clear();
                }
                else if (dtStart.Year == DateTime.Now.Year)
                {
                    listMonth = listMonth.Where(e => e <= DateTime.Now.Month).OrderBy(e => e).ToList();
                }

                List<int> listFinishPer2_1 = null;
                List<int> listNotFinishPer2_1 = null;
                List<decimal> listFinishPer2_1Percent = null;

                List<List<int>> listFinish2_1 = new List<List<int>>();
                List<List<int>> listNotFinish2_1 = new List<List<int>>();
                List<List<decimal>> listFinish2_1Percent = new List<List<decimal>>();

                BSMineTypeEnum? CHECKOBJECT = null;
                foreach (var itemEnums in enmusMineType)
                {
                    CHECKOBJECT = (BSMineTypeEnum)itemEnums.VALUE;
                    listFinishPer2_1 = new List<int>();
                    listNotFinishPer2_1 = new List<int>();
                    listFinishPer2_1Percent = new List<decimal>();
                    foreach (var item in listMonth)
                    {
                        listFinishPer2_1.Add(listContent.Count(e => e.Nav_Submit.CHECKOBJECT.HasValue && e.Nav_Submit.CHECKOBJECT.Value == CHECKOBJECT && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value.Month == item && (e.RiskContentState.Value == 60 || e.RiskContentState.Value == 80)));
                        listNotFinishPer2_1.Add(listContent.Count(e => e.Nav_Submit.CHECKOBJECT.HasValue && e.Nav_Submit.CHECKOBJECT.Value == CHECKOBJECT && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value.Month == item && e.RiskContentState.Value != 60 && e.RiskContentState.Value != 80));
                    }

                    for (int i = 0; i < listMonth.Count; i++)
                    {
                        if (listNotFinishPer2_1[i] == 0)
                        {
                            listFinishPer2_1Percent.Add(100);
                        }
                        else if (listFinishPer2_1[i] == 0)
                        {
                            listFinishPer2_1Percent.Add(0);
                        }
                        else
                        {
                            listFinishPer2_1Percent.Add((decimal)Math.Round((listFinishPer2_1[i] * 100.0 / (listNotFinishPer2_1[i] + listFinishPer2_1[i])), 2));
                        }
                    }
                    listFinish2_1.Add(listFinishPer2_1);
                    listNotFinish2_1.Add(listNotFinishPer2_1);
                    listFinish2_1Percent.Add(listFinishPer2_1Percent);
                }

                result.lisMineTypeOrder = lisMineTypeOrder;
                result.listFinish2_1 = listFinish2_1;
                result.listNotFinish2_1 = listNotFinish2_1;
                result.listFinish2_1Percent = listFinish2_1Percent;

                return result;
            });
        }

        #endregion

        #region   2_2  GetBI056_2_2



        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetBI056_2_2")]
        public JsonActionResult<ModelBI056_2_2> GetBI056_2_2([FromBody] KeywordFilter filter)
        {
            return SafeExecute<ModelBI056_2_2>(() =>
            {
                ModelBI056_2_2 result = new ModelBI056_2_2();
                DateTime dtStart = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-01-01 00:00:00");
                DateTime? dtEnd = null;

                List<string> lisMineTypeOrder = new List<string>();
                var enmusMineType = GetEntities<T_FM_ENUMS>(e => !e.IS_DELETED && e.CODE == "BSMineTypeEnum", null, null).OrderBy(e => e.NUM).ToList();

                if (enmusMineType == null || !enmusMineType.Any())
                    throw new Exception("获取生成单元枚举信息失败！");
                else
                {
                    foreach (var item in enmusMineType)
                    {
                        lisMineTypeOrder.Add(item.NAME);
                    }
                }

                List<List<int>> ListCount = new List<List<int>>();
                List<List<string>> ListDescreption = new List<List<string>>();

                Expression<Func<T_BS_RISK_SUBMIT_CONTENT, bool>> expression2_2 = e => !e.IS_DELETED  && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value >= dtStart;//&& e.DESCREPTION != null&& e.RiskContentState.HasValue && (e.RiskContentState.Value == 60 || e.RiskContentState.Value == 80)
                if (dtEnd.HasValue)
                    expression2_2 = expression2_2.And(e => e.CREATE_TIME.HasValue && dtEnd.Value >= e.CREATE_TIME.Value);

                BSMineTypeEnum? mineType = null;

                List<int> ListCountPer = null;
                List<string> ListDescreptionPer = null;
                foreach (var item in enmusMineType)
                {
                    mineType = (BSMineTypeEnum)item.VALUE;
                    Expression<Func<T_BS_RISK_SUBMIT_CONTENT, bool>> expression2_2Mine = expression2_2.And(e => e.Nav_Submit.CHECKOBJECT.HasValue && e.Nav_Submit.CHECKOBJECT.Value == mineType);
                    var listContent = GetEntities(expression2_2Mine, null, "Nav_Submit");
                    var query = listContent.GroupBy(e => e.DESCREPTION).Select(e => new
                    {
                        DESCREPTION = e.Key,
                        Count = e.Count()
                    }).OrderByDescending(e => e.Count).Take(6);
                    ListCountPer = new List<int>();
                    ListDescreptionPer = new List<string>();
                    if (query != null && query.Any())
                    {
                        foreach (var itemVal in query)
                        {
                            ListCountPer.Add(itemVal.Count);
                            ListDescreptionPer.Add(itemVal.DESCREPTION);
                        }
                    }
                    ListCount.Add(ListCountPer);
                    ListDescreption.Add(ListDescreptionPer);
                }

                //数据组装
                result.lisMineTypeOrder = lisMineTypeOrder;
                result.ListCount = ListCount;
                result.ListDescreption = ListDescreption;
                return result;
            });
        }

        #endregion

        #region    3_1  GetBI056_3_1

        /// <summary>
        /// 各生产单元 各月 各检查类型数量
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetBI056_3_1")]
        public JsonActionResult<ModelBI056_3_1> GetBI056_3_1([FromBody] KeywordFilter filter)
        {
            return SafeExecute<ModelBI056_3_1>(() =>
            {
                ModelBI056_3_1 result = new ModelBI056_3_1();
                DateTime dtStart = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-01-01 00:00:00");
                DateTime? dtEnd = null;

                var enmusMineType = GetEntities<T_FM_ENUMS>(e => !e.IS_DELETED && e.CODE == "BSMineTypeEnum", null, null).OrderBy(e => e.NUM).ToList();
                List<string> lisMineTypeOrder = new List<string>();
                if (enmusMineType == null || !enmusMineType.Any())
                    throw new Exception("获取生成单元枚举信息失败！");
                else
                {
                    foreach (var item in enmusMineType)
                    {
                        lisMineTypeOrder.Add(item.NAME);
                    }
                }

                var listCheckType = GetEntities<T_BS_CHECK_TYPE>(e => !e.IS_DELETED, null, "Nav_ListMineType").OrderBy(e => e.PARENT_ID).ThenBy(e => e.NUM).ToList();

                Dictionary<Guid, List<Guid>> dicParent = new Dictionary<Guid, List<Guid>>();
                List<Guid> listSum = new List<Guid>();
                Guid guidParent = Guid.Empty;

                foreach (var item in listCheckType)
                {
                    if (item.PARENT_ID.HasValue)
                    {
                        if (guidParent != Guid.Empty)
                        {
                            guidParent = item.PARENT_ID.Value;
                        }
                        listSum.Add(item.ID);
                    }
                }

                listSum.Add(guidParent);//有些人直接定义 综合检查
                dicParent.Add(guidParent, listSum);


                List<List<T_BS_CHECK_TYPE>> listCheckTypes = new List<List<T_BS_CHECK_TYPE>>();
                foreach (var item in enmusMineType)
                {
                    listCheckTypes.Add(listCheckType.Where(e => e.Nav_ListMineType.Where(e => e.MINETYPE_ENUMS_ID == item.ID).Any()).ToList());
                }
                List<List<string>> lisMineCheckTypeOrder = new List<List<string>>();
                List<string> lisMineCheckTypeOrderPer = null;
                foreach (var item in listCheckTypes)
                {
                    lisMineCheckTypeOrderPer = new List<string>();
                    foreach (var itemCheckType in item)
                    {
                        lisMineCheckTypeOrderPer.Add(itemCheckType.NAME);
                    }
                    lisMineCheckTypeOrder.Add(lisMineCheckTypeOrderPer);
                }

                #region 改良版 listSafeCheck 所有的符合条件的检查信息

                Expression<Func<T_BS_SAFE_CHECK, bool>> expression = e => !e.IS_DELETED && e.CHECKOBJECT.HasValue && e.CHECK_TYPE_LEVEL_ID.HasValue && e.CHECKTIME.HasValue && dtStart <= e.CHECKTIME.Value;

                if (dtEnd.HasValue)
                    expression = expression.And(e => e.CHECKTIME.HasValue && dtEnd.Value >= e.CHECKTIME.Value);

                BaseFilter filterSearch = new BaseFilter(filter.OrgId);
                filterSearch.IgnoreDataRule = true;
                filterSearch.Include.Add("Nav_CheckTypeLevel");
                filterSearch.SelectField = new List<string>() { "ID", "CHECKOBJECT", "CHECK_TYPE_ID", "IS_DELETED", "CHECKTIME", "Nav_CheckTypeLevel.CheckLevel_ENUMS_ID" };
                //var listSafeCheck = GetEntities(expression, null, "Nav_CheckTypeLevel");
                var listSafeCheck = GetEntities(expression, filterSearch, "Nav_CheckTypeLevel");
                #endregion


                #region  3_1 改良版
                List<List<List<int>>> listCheckTypeMonthCount = new List<List<List<int>>>();
                List<List<int>> listCheckTypeCount = null;
                List<int> listCheckTypeCountPer = null;

                List<int> listMonth = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12 };
                if (dtStart.Year > DateTime.Now.Year)
                {
                    listMonth.Clear();
                }
                else if (dtStart.Year == DateTime.Now.Year)
                {
                    listMonth = listMonth.Where(e => e <= DateTime.Now.Month).OrderBy(e => e).ToList();
                }

                var enmusMineTypeCount = enmusMineType.Count();
                for (int i = 0; i < enmusMineTypeCount; i++)
                {
                    listCheckTypeCount = new List<List<int>>();
                    int MineCheckTypeCount = listCheckTypes[i].Count;
                    for (int j = 0; j < MineCheckTypeCount; j++)
                    {
                        listCheckTypeCountPer = new List<int>();
                        foreach (int itemMonth in listMonth)
                        {
                            //检查类型
                            if (listCheckTypes[i][j].ID == guidParent)
                            {
                                int count = listSafeCheck.Where(e => e.CHECKOBJECT == enmusMineType[i].VALUE && e.CHECKTIME.Value.Month == itemMonth && listSum.Contains(e.CHECK_TYPE_ID.Value)).Count();
                                listCheckTypeCountPer.Add(count);
                            }
                            else
                            {
                                int count = listSafeCheck.Where(e => e.CHECKOBJECT == enmusMineType[i].VALUE && e.CHECKTIME.Value.Month == itemMonth && e.CHECK_TYPE_ID == listCheckTypes[i][j].ID).Count();
                                listCheckTypeCountPer.Add(count);
                            }
                        }
                        listCheckTypeCount.Add(listCheckTypeCountPer);
                    }
                    listCheckTypeMonthCount.Add(listCheckTypeCount);
                }

                //数据组装
                result.lisMineTypeOrder = lisMineTypeOrder;
                result.lisMineCheckTypeOrder = lisMineCheckTypeOrder;
                result.Count3_1 = listCheckTypeMonthCount;

                #endregion


                return result;
            });
        }

        #endregion

        #region    3_2  GetBI056_3_2

        /// <summary>
        /// 各生产单元 各月 各检查类型数量
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetBI056_3_2")]
        public JsonActionResult<ModelBI056_3_2> GetBI056_3_2([FromBody] KeywordFilter filter)
        {
            return SafeExecute<ModelBI056_3_2>(() =>
            {
                ModelBI056_3_2 result = new ModelBI056_3_2();
                DateTime dtStart = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-01-01 00:00:00");
                DateTime? dtEnd = null;

                List<int> listMonth = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12 };
                if (dtStart.Year > DateTime.Now.Year)
                {
                    listMonth.Clear();
                }
                else if (dtStart.Year == DateTime.Now.Year)
                {
                    listMonth = listMonth.Where(e => e <= DateTime.Now.Month).OrderBy(e => e).ToList();
                }

                var enmusMineType = GetEntities<T_FM_ENUMS>(e => !e.IS_DELETED && e.CODE == "BSMineTypeEnum", null, null).OrderBy(e => e.NUM).ToList();
                List<string> lisMineTypeOrder = new List<string>();
                if (enmusMineType == null || !enmusMineType.Any())
                    throw new Exception("获取生成单元枚举信息失败！");
                else
                {
                    foreach (var item in enmusMineType)
                    {
                        lisMineTypeOrder.Add(item.NAME);
                    }
                }

                var listCheckType = GetEntities<T_BS_CHECK_TYPE>(e => !e.IS_DELETED, null, "Nav_ListMineType").OrderBy(e => e.PARENT_ID).ThenBy(e => e.NUM);
                List<string> listCheckTypeName = new List<string>();
                List<Guid> listCheckTypeID = new List<Guid>();
                Dictionary<Guid, List<Guid>> dicParent = new Dictionary<Guid, List<Guid>>();
                List<Guid> listSum = new List<Guid>();
                Guid guidParent = Guid.Empty;
                foreach (var item in listCheckType)
                {
                    if (item.PARENT_ID.HasValue)
                    {
                        if (!listCheckTypeName.Contains(item.NAME))
                        {
                            listCheckTypeName.Add(item.NAME);
                            listCheckTypeID.Add(item.ID);
                            guidParent = item.PARENT_ID.Value;
                        }
                        listSum.Add(item.ID);
                    }
                    else
                    {
                        listCheckTypeName.Add(item.NAME);
                        listCheckTypeID.Add(item.ID);
                    }
                }
                listSum.Add(guidParent);//有些人直接定义 综合检查
                dicParent.Add(guidParent, listSum);

                List<List<T_BS_CHECK_TYPE>> listCheckTypes = new List<List<T_BS_CHECK_TYPE>>();
                foreach (var item in enmusMineType)
                {
                    listCheckTypes.Add(listCheckType.Where(e => e.Nav_ListMineType.Where(e => e.MINETYPE_ENUMS_ID == item.ID).Any()).ToList());
                }

                Expression<Func<T_BS_RISK_SUBMIT_CONTENT, bool>> expression = e => !e.IS_DELETED && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value >= dtStart  && e.RiskContentState.HasValue && !e.IS_DELETED;
                if (dtEnd.HasValue)
                    expression = expression.And(e => e.CREATE_TIME.HasValue && dtEnd.Value >= e.CREATE_TIME.Value);
                var listContent = GetEntities(expression, null, "Nav_Submit").ToList();

                List<List<string>> lisMineCheckTypeOrder = new List<List<string>>();
                List<string> lisMineCheckTypeOrderPer = null;
                foreach (var item in listCheckTypes)
                {
                    lisMineCheckTypeOrderPer = new List<string>();
                    foreach (var itemCheckType in item)
                    {
                        lisMineCheckTypeOrderPer.Add(itemCheckType.NAME);
                    }
                    lisMineCheckTypeOrder.Add(lisMineCheckTypeOrderPer);
                }


                List<List<List<int>>> listRisk = new List<List<List<int>>>();
                List<List<int>> listRiskMoneCheckType = null;
                List<int> listCheckTypeCountPer = null;
                int MintTypeIndex = -1;
                foreach (var item in enmusMineType)
                {
                    MintTypeIndex++;
                    listRiskMoneCheckType = new List<List<int>>();
                    foreach (var itemCheckType in listCheckTypes[MintTypeIndex])
                    {
                        listCheckTypeCountPer = new List<int>();
                        foreach (var itemMonth in listMonth)
                        {
                            //检查类型
                            if (itemCheckType.ID == guidParent)
                            {
                                int count = listContent.Count(e => e.Nav_Submit != null && e.Nav_Submit.CHECK_TYPE_ID.HasValue && listSum.Contains(e.Nav_Submit.CHECK_TYPE_ID.Value) && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value.Month == itemMonth && e.Nav_Submit.CHECKOBJECT.HasValue && e.Nav_Submit.CHECKOBJECT == (BSMineTypeEnum)item.VALUE && listSum.Contains(e.Nav_Submit.CHECK_TYPE_ID.Value));
                                listCheckTypeCountPer.Add(count);
                            }
                            else
                            {
                                int count = listContent.Count(e => e.Nav_Submit != null && e.Nav_Submit.CHECK_TYPE_ID.HasValue && e.Nav_Submit.CHECK_TYPE_ID == itemCheckType.ID && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value.Month == itemMonth && e.Nav_Submit.CHECKOBJECT.HasValue && e.Nav_Submit.CHECKOBJECT == (BSMineTypeEnum)item.VALUE && e.Nav_Submit.CHECK_TYPE_ID == itemCheckType.ID);
                                listCheckTypeCountPer.Add(count);
                            }
                        }
                        listRiskMoneCheckType.Add(listCheckTypeCountPer);
                    }
                    listRisk.Add(listRiskMoneCheckType);
                }

                result.lisMineTypeOrder = lisMineTypeOrder;
                result.lisMineCheckType = lisMineCheckTypeOrder;
                result.Count3_1 = listRisk;
                return result;
            });
        }

        #endregion

        #endregion

        #endregion

        #region     model

        public class ModelBI056Main
        {
            public List<string> lisMineTypeOrder { get; set; }
            public List<string> lisLevelOrder { get; set; }
            public List<List<int>> Count1_1 { get; set; }
            public List<int> listRiskMineCount1_2 { get; set; }



            public List<int> listFinish2_1 { get; set; }
            public List<int> listMonth { get; set; }
            public List<int> listNotFinish2_1 { get; set; }
            public List<decimal> listFinish2_1Percent { get; set; }

            public List<string> ListDescreption2_2 { get; set; }
            public List<int> Count2_2 { get; set; }
            public List<List<int>> Count3_1 { get; set; }
            public List<string> lisCheckTypeName { get; set; }
            public List<List<int>> Count3_2 { get; set; }
        }

        public class ModelBI056_1_1
        {
            public List<string> lisMineTypeOrder { get; set; }
            public List<string> lisLevelOrder { get; set; }
            public List<List<int>> Count { get; set; }
        }

        public class ModelBI056_1_2
        {
            public List<string> lisMineTypeOrder { get; set; }
            public List<List<int>> ListCount { get; set; }
        }

        public class ModelBI056_2_1
        {
            public List<string> lisMineTypeOrder { get; set; }
            public List<List<int>> listFinish2_1 { get; set; }
            public List<List<int>> listNotFinish2_1 { get; set; }
            public List<List<decimal>> listFinish2_1Percent { get; set; }
        }
        public class ModelBI056_2_2
        {
            public List<string> lisMineTypeOrder { get; set; }
            public List<List<int>> ListCount { get; set; }
            public List<List<string>> ListDescreption { get; set; }
        }


        public class ModelBI056_3_1
        {
            /// <summary>
            /// 生产单元
            /// </summary>
            public List<string> lisMineTypeOrder { get; set; }
            /// <summary>
            /// 生产单元 检查类型
            /// </summary>
            public List<List<string>> lisMineCheckTypeOrder { get; set; }

            /// <summary>
            /// 检查数量
            /// </summary>
            public List<List<List<int>>> Count3_1 { get; set; }

        }

        public class ModelBI056_3_2
        {
            /// <summary>
            /// 生产单元
            /// </summary>
            public List<string> lisMineTypeOrder { get; set; }

            /// <summary>
            /// 生产单元 检查类型
            /// </summary>
            public List<List<string>> lisMineCheckType { get; set; }

            /// <summary>
            /// 检查数量
            /// </summary>
            public List<List<List<int>>> Count3_1 { get; set; }

        }

        #endregion

        #endregion
    }


    public class BISafe
    {
        public List<T_BI_BSSAFECHECK> listSafeCheck { get; set; }
        public List<T_BI_BSCHECKTYPE> listCheckType { get; set; }
        public T_BI_SUMMARY summary { get; set; }

    }
}