using APT.BaseData.Domain.Entities.FM;
using APT.Infrastructure.Core;
using Microsoft.AspNetCore.Mvc;
using APT.Infrastructure.Api;
using System.Collections.Generic;
using System;
using System.Linq;
using APT.Utility;
using APT.MS.Domain.Entities.BI;
using APT.BaseData.Domain.IServices.BI;

namespace APT.SC.WebApi.Controllers.Api.BI
{
    /// <summary>
    ///  首页图表
    /// </summary>
    [Route("api/BI/BIHome")]
    public class BIHomeController : AuthorizeApiController<T_BI_HOME_BSRISKREASON>
    {
        IBIHomeService BIHomeService { get; set; }

        public BIHomeController(IBIHomeService biHomeService)
        {
            BIHomeService = biHomeService;
        }
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("GetHomeInfo1")]
        public JsonActionResult<HomeResult> GetHomeInfo1([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                //全部 改成 看自己 所在组织
                //安环 取公司
                //班组 取 车间

                HomeResult result = new HomeResult();
                List<T_FM_DEPARTMENT> listDep = new List<T_FM_DEPARTMENT>();
                DateTime dtNow = DateTime.Now;

                Guid? depID = APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID;
                if (!depID.HasValue)
                {
                    return result;
                }
                var depLogin = GetEntity<T_FM_DEPARTMENT>(APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID.Value);
                ////FMDepartmentType 部门=0,  车间=1,   班组=2,  公司=3
                //if (depLogin.DEPARTMENT_TYPE == 2)
                //{
                //    //班组级 因数据少不好看 显示车间信息（功能需求变更）
                //    listDep = GetEntities<T_FM_DEPARTMENT>(e => e.PARENT_ID.HasValue && e.PARENT_ID.Value == depLogin.PARENT_ID, null, null).ToList();
                //}
                //else
                //{
                //    listDep = GetEntities<T_FM_DEPARTMENT>(e => e.PARENT_ID.HasValue && e.PARENT_ID.Value == depID, null, null).ToList();
                //}

                if (depLogin.DEPARTMENT_TYPE == 2)
                {
                    //班组级 因数据少不好看 显示车间信息（功能需求变更）
                    listDep = GetEntities<T_FM_DEPARTMENT>(e => e.ID == depLogin.PARENT_ID.Value, null, null).ToList();
                }
                else
                {
                    if (depLogin.DEPARTMENT_STATUS == 2)//安环 看公司级
                    {
                        listDep.Add(GetEntity<T_FM_DEPARTMENT>(e => e.DEPARTMENT_TYPE == 3 && !e.IS_DELETED));
                    }
                    else
                    {
                        listDep.Add(depLogin);
                    }
                }


                if (!listDep.Any())
                {
                    return result;
                }

                #region //隐患原因

                List<T_BI_HOME_BSRISKREASON> listReasonT = BIHomeService.GetHomeReasons(filter.OrgId, listDep).ToList();
                List<HomeRISKREASON> listReason = new List<HomeRISKREASON>();
                int reasonMax = listReasonT.Max(e => e.NUMS);

                //List<int> listEnumVal = new List<int> { 5, 10, 15, 20 };
                List<string> LISTNAME = new List<string>();// { "按时整改隐患", "超期整改隐患", "未整改隐患", "隐患整改率" };



                HomeRISKREASON model = null;
                List<int> LISVALUE = null;
                for (int i = 0; i < listReasonT.Count; i++)
                {
                    if (i == 0 || listReasonT[i].DEPARTMENT_ID != listReasonT[i - 1].DEPARTMENT_ID)
                    {
                        model = new HomeRISKREASON();
                        model.DEPARTMENTNAME = listReasonT[i].DEPARTMENTNAME;
                        model.YEAR = listReasonT[i].YEAR;
                        model.SOURCENAME = listReasonT[i].SOURCENAME;

                        LISVALUE = new List<int>();
                    }

                    LISVALUE.Add(listReasonT[i].COUNT);
                    if (LISTNAME.Count < reasonMax)
                    {
                        LISTNAME.Add(listReasonT[i].SOURCENAME);
                    }

                    if (listReasonT[i].NUMS == 1)
                    {
                        model.NAME1 = listReasonT[i].SOURCENAME;
                        model.COUNT1 = listReasonT[i].COUNT;
                    }
                    if (listReasonT[i].NUMS == 2)
                    {
                        model.NAME2 = listReasonT[i].SOURCENAME;
                        model.COUNT2 = listReasonT[i].COUNT;
                    }
                    if (listReasonT[i].NUMS == 3)
                    {
                        model.NAME3 = listReasonT[i].SOURCENAME;
                        model.COUNT3 = listReasonT[i].COUNT;
                    }
                    if (listReasonT[i].NUMS == 4)
                    {
                        model.NAME4 = listReasonT[i].SOURCENAME;
                        model.COUNT4 = listReasonT[i].COUNT;
                    }
                    if (listReasonT[i].NUMS == 5)
                    {
                        model.NAME5 = listReasonT[i].SOURCENAME;
                        model.COUNT5 = listReasonT[i].COUNT;
                    }

                    if (reasonMax == listReasonT[i].NUMS)
                    {
                        model.LISVALUE = LISVALUE;
                        model.LISTNAME = LISTNAME;
                        listReason.Add(model);
                    }
                }
                result.ListR = listReason;

                #endregion

                #region //月度 隐患整改情况


                //List<T_FM_DEPARTMENT> listLoginDep = new List<T_FM_DEPARTMENT>();
                //if (APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID.HasValue)
                //{
                //    listLoginDep.Add(depLogin);
                //}
                //else
                //{
                //    //公司级
                //    //listLoginDep = listDep;
                //    listLoginDep = GetEntities<T_FM_DEPARTMENT>(e => e.DEPARTMENT_TYPE == 3 && !e.IS_DELETED && e.ENABLE_STATUS == 0, null, null).ToList();
                //}

                //List<T_BI_HOME_BSRISKREASON> listCorrectT = BIHomeService.GetHomeCorrect(filter.OrgId, listLoginDep).ToList();


                //月度 整改情况
                //公司/部门/车间 显示本组织 的数据
                //班组 显示 车间的数据
                //List<T_FM_DEPARTMENT> listDepC = new List<T_FM_DEPARTMENT>();
                //if (depLogin.DEPARTMENT_TYPE == 2)
                //{
                //    //depLogin
                //    listDepC.Add(GetEntity<T_FM_DEPARTMENT>(e => e.ID == depLogin.PARENT_ID.Value));
                //}
                //else
                //{
                //    if (depLogin.DEPARTMENT_STATUS == 2)//安环 看公司级
                //    {
                //        listDepC.Add(GetEntity<T_FM_DEPARTMENT>(e => e.DEPARTMENT_TYPE == 3 && !e.IS_DELETED));
                //    }
                //    else
                //    {
                //        listDepC.Add(depLogin);
                //    }
                //}

                //List<T_BI_HOME_BSRISKREASON> listCorrectT = BIHomeService.GetHomeCorrect(filter.OrgId, listDepC).ToList();
                ////List<T_BI_HOME_BSRISKREASON> listCorrectT = BIHomeService.GetHomeCorrect(filter.OrgId, listDep).ToList();
                List<T_BI_HOME_BSRISKREASON> listCorrectT = BIHomeService.GetHomeCorrect(filter.OrgId, listDep).ToList();
                string Val = string.Empty;// 0 ,1,2,3
                string NameMonth = dtNow.AddMonths(-5).Month.ToString() + "月," + dtNow.AddMonths(-4).Month.ToString() + "月," + dtNow.AddMonths(-3).Month.ToString() + "月," + dtNow.AddMonths(-2).Month.ToString() + "月," + dtNow.AddMonths(-1).Month.ToString() + "月," + dtNow.Month.ToString() + "月";
                List<HomeCorrect> listC = new List<HomeCorrect>();
                HomeCorrect modelC = null;
                for (int i = 0; i < listCorrectT.Count; i++)
                {
                    if (i == 0 || listCorrectT[i].DEPARTMENT_ID != listCorrectT[i - 1].DEPARTMENT_ID || listCorrectT[i].SOURCEVAL != listCorrectT[i - 1].SOURCEVAL)
                    {
                        modelC = new HomeCorrect();
                        modelC.DEPARTMENTNAME = listCorrectT[i].DEPARTMENTNAME;
                        modelC.DEPARTMENT_ID = listCorrectT[i].DEPARTMENT_ID;
                        modelC.SOURCENAME = listCorrectT[i].SOURCENAME;
                        modelC.YEAR = listCorrectT[i].YEAR;
                        modelC.NAME = NameMonth;
                        Val = "";
                    }
                    if (listCorrectT[i].MONTH == 1)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M1 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M1 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M1 + ",";
                    }


                    if (listCorrectT[i].MONTH == 2)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M2 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M2 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M2 + ",";
                    }

                    if (listCorrectT[i].MONTH == 3)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M3 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M3 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M3 + ",";
                    }

                    if (listCorrectT[i].MONTH == 4)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M4 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M4 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M4 + ",";
                    }

                    if (listCorrectT[i].MONTH == 5)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M5 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M5 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M5 + ",";
                    }

                    if (listCorrectT[i].MONTH == 6)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M6 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M6 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M6 + ",";
                    }

                    if (listCorrectT[i].MONTH == 7)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M7 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M7 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M7 + ",";
                    }

                    if (listCorrectT[i].MONTH == 8)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M8 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M8 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M8 + ",";
                    }

                    if (listCorrectT[i].MONTH == 9)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M9 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M9 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M9 + ",";
                    }

                    if (listCorrectT[i].MONTH == 10)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M10 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M10 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M10 + ",";
                    }

                    if (listCorrectT[i].MONTH == 11)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M11 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M11 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M11 + ",";
                    }

                    if (listCorrectT[i].MONTH == 12)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M12 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M12 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M12 + ",";
                    }

                    if (listCorrectT[i].MONTH == dtNow.Month)
                    {
                        modelC.Val = Val.Substring(0, Val.Length - 1);
                        listC.Add(modelC);
                    }
                }
                result.ListC = listC;

                #endregion

                #region    年度 各下属组织 隐患整改情况

                List<T_BI_HOME_BSRISKREASON> listReasonYearT = BIHomeService.GetHomeCorrectYear(filter.OrgId, listDep).ToList();
                List<HomeCorrectYear> ListCYear = new List<HomeCorrectYear>();
                HomeCorrectYear modelCY = null;
                int CountYearT = listReasonYearT.Count;
                for (int i = 0; i < CountYearT; i++)
                {
                    if (i == 0 || listReasonYearT[i].DEPARTMENT_ID != listReasonYearT[i - 1].DEPARTMENT_ID)
                    {
                        modelCY = new HomeCorrectYear();
                        modelCY.DEPARTMENTNAME = listReasonYearT[i].DEPARTMENTNAME;
                        modelCY.DEPARTMENT_ID = listReasonYearT[i].DEPARTMENT_ID;
                        modelCY.YEAR = listReasonYearT[i].YEAR;
                    }

                    // 按时整改隐患 5     超期整改隐患 10 未整改隐患 15
                    if (listReasonYearT[i].SOURCEVAL == 5)
                    {
                        modelCY.CountIn += listReasonYearT[i].COUNT;
                    }
                    else if (listReasonYearT[i].SOURCEVAL == 10)
                    {
                        modelCY.CountOut += listReasonYearT[i].COUNT;
                    }
                    else if (listReasonYearT[i].SOURCEVAL == 15)
                    {
                        modelCY.CountNot += listReasonYearT[i].COUNT;
                    }

                    if (i == CountYearT - 1 || listReasonYearT[i].DEPARTMENT_ID != listReasonYearT[i + 1].DEPARTMENT_ID)
                    {
                        modelCY.Count = modelCY.CountIn + modelCY.CountOut + modelCY.CountNot;
                        ListCYear.Add(modelCY);
                    }
                }

                result.ListCYear = ListCYear;

                #endregion


                #region      月度 6 个月 安全检查、检查类型 数量 

                //按生产单元 获取

                if (!string.IsNullOrEmpty(filter.Keyword))
                {
                    int MineType = int.Parse(filter.Keyword.ToString());
                    BaseFilter filterD = new BaseFilter();
                    filterD.SelectField = new List<string> { "ID", "NAME", "DEPARTMENT_TYPE", "PARENT_ID" };

                    var listDep1MineType = GetEntities<T_FM_DEPARTMENT>(t => t.MineType.HasValue && t.MineType == MineType && t.DEPARTMENT_TYPE == 0, filterD, null);//部门就可以了 因为都相向 统计了
                    if (listDep1MineType != null && listDep1MineType.Any())
                    {
                        List<HomeSafeCheckCountMonth> ListSafeCheckYearMonth = new List<HomeSafeCheckCountMonth>();
                        int befoMonth = 12;
                        List<T_BI_HOME_BSRISKREASON> ListSafeCheckYearMonthT = BIHomeService.GetHomeCheckCountMonth(filter.OrgId, listDep1MineType.ToList(), befoMonth).ToList();
                        var listCheckTypeIDT = ListSafeCheckYearMonthT.Select(e => e.SOURCEID).Distinct().ToList();

                        var enums = GetEntity<T_FM_ENUMS>(e => e.VALUE == MineType && e.CODE == "BSMineTypeEnum");
                        if (enums != null)
                        {
                            var listTypeMine = GetEntities<MS.Domain.Entities.BS.T_BS_CHECK_TYPE_MINETYPE>(e => e.MINETYPE_ENUMS_ID == enums.ID, null, null);
                            if (listTypeMine != null && listTypeMine.Any())
                            {
                                var listMineCheckTypeID = listTypeMine.Where(e => e.CHECK_TYPE_ID.HasValue).Select(e => e.CHECK_TYPE_ID.Value);
                                for (int i = 0; i < listCheckTypeIDT.Count; i++)
                                {
                                    if (!listMineCheckTypeID.Contains(listCheckTypeIDT[i]))
                                    {
                                        listCheckTypeIDT.RemoveAt(i);
                                        i--;
                                    }
                                }
                            }
                        }

                        var listCheckType = GetEntities<MS.Domain.Entities.BS.T_BS_CHECK_TYPE>(e => listCheckTypeIDT.Contains(e.ID), null, null).OrderBy(e => e.PARENT_ID).ThenBy(e => e.NUM).ToList();

                        List<Guid> listCheckTypeID = new List<Guid>();
                        List<string> listCheckTypeNAME = new List<string>();

                        Guid IDZH = Guid.Empty;//综合检查(只有一个)
                        List<Guid> listGUID = new List<Guid>();//综合检查子集
                        for (int i = 0; i < listCheckType.Count; i++)
                        {
                            if (listCheckType[i].PARENT_ID.HasValue)
                            {
                                if (IDZH == Guid.Empty)
                                {
                                    IDZH = listCheckType[i].PARENT_ID.Value;
                                    listCheckTypeID.Add(IDZH);
                                    var checkTypeP = GetEntity<MS.Domain.Entities.BS.T_BS_CHECK_TYPE>(IDZH);
                                    listCheckTypeNAME.Add(checkTypeP.NAME);
                                }
                                listGUID.Add(listCheckType[i].ID);
                            }
                            else
                            {
                                listCheckTypeID.Add(listCheckType[i].ID);
                                listCheckTypeNAME.Add(listCheckType[i].NAME);
                            }
                        }

                        int year = DateTime.Now.Year;
                        int Month = DateTime.Now.Month;
                        DateTime dtTemp = DateTime.Now;
                        IEnumerable<T_BI_HOME_BSRISKREASON> listPerChearch = null;
                        IEnumerable<T_BI_HOME_BSRISKREASON> listPerChearchPer = null;

                        //List<int> listListCount = null;
                        for (int i = 0; i < befoMonth; i++)
                        {
                            dtTemp = dtNow.AddMonths(-1 * i);
                            year = dtTemp.Year;
                            Month = dtTemp.Month;
                            listPerChearch = ListSafeCheckYearMonthT.Where(e => e.YEAR == year && e.MONTH == Month);
                            HomeSafeCheckCountMonth modelper = new HomeSafeCheckCountMonth();
                            modelper.Total = listPerChearch.Count();
                            modelper.listSOURCENAME = listCheckTypeNAME;
                            modelper.YEAR = year;
                            modelper.MONTH = Month;
                            List<int> listListCount = new List<int>();

                            for (int j = 0; j < listCheckTypeID.Count; j++)
                            {
                                if (listCheckTypeID[j] != IDZH)
                                {
                                    listPerChearchPer = listPerChearch.Where(e => e.YEAR == year && e.MONTH == Month && e.SOURCEID == listCheckTypeID[j]);//各个检查
                                }
                                else
                                {
                                    listPerChearchPer = listPerChearch.Where(e => e.YEAR == year && e.MONTH == Month && listGUID.Contains(e.SOURCEID));//综合检查
                                }

                                if (listPerChearchPer == null || !listPerChearchPer.Any())
                                {
                                    listListCount.Add(0);
                                }
                                else
                                {
                                    listListCount.Add(listPerChearchPer.Sum(e => e.COUNT));
                                }
                            }
                            modelper.listListCount = listListCount;
                            ListSafeCheckYearMonth.Add(modelper);
                        }
                        result.ListSafeCheckYearMonth = ListSafeCheckYearMonth.OrderBy(e => e.YEAR).ThenBy(e => e.MONTH).ToList();
                    }
                }


                #endregion

                return result;
            });
        }


        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("GetHomeInfo")]
        public JsonActionResult<HomeResult> GetHomeInfo([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                //按生产单元 获取数据 
                HomeResult result = new HomeResult();
                if (string.IsNullOrEmpty(filter.Keyword))
                {
                    return result;
                }
                int MineType = int.Parse(filter.Keyword);

                #region     Collection was modified, enumeration operation may not execute（foreach）   不要   SelectField  看看结果  如果继续报错 返回来
                //TODO wyw  SelectField

                //BaseFilter filterD = new BaseFilter();
                //filterD.SelectField = new List<string> { "ID", "NAME", "DEPARTMENT_TYPE", "PARENT_ID", "MineType" };
                //var listDep1MineType = GetEntities<T_FM_DEPARTMENT>(t => t.MineType.HasValue && t.MineType == MineType && t.DEPARTMENT_TYPE == 0, filterD, null);//部门就可以了 因为都相向 统计了 

                var listDep1MineType = GetEntities<T_FM_DEPARTMENT>(t => t.MineType.HasValue && t.MineType == MineType && t.DEPARTMENT_TYPE == 0, null, null);//部门就可以了 因为都相向 统计了

                #endregion

                if (listDep1MineType == null || !listDep1MineType.Any())
                {
                    return result;
                }

                List<T_FM_DEPARTMENT> listDep = new List<T_FM_DEPARTMENT>();
                DateTime dtNow = DateTime.Now;
                listDep = listDep1MineType.ToList();


                #region //////隐患原因( 不需要了 )

                //List<T_BI_HOME_BSRISKREASON> listReasonT = BIHomeService.GetHomeReasons(filter.OrgId, listDep).ToList();
                //List<HomeRISKREASON> listReason = new List<HomeRISKREASON>();
                //int reasonMax = listReasonT.Max(e => e.NUMS);

                ////List<int> listEnumVal = new List<int> { 5, 10, 15, 20 };
                //List<string> LISTNAME = new List<string>();// { "按时整改隐患", "超期整改隐患", "未整改隐患", "隐患整改率" };

                //HomeRISKREASON model = null;
                //List<int> LISVALUE = null;
                //for (int i = 0; i < listReasonT.Count; i++)
                //{
                //    if (i == 0 || listReasonT[i].DEPARTMENT_ID != listReasonT[i - 1].DEPARTMENT_ID)
                //    {
                //        model = new HomeRISKREASON();
                //        model.DEPARTMENTNAME = listReasonT[i].DEPARTMENTNAME;
                //        model.YEAR = listReasonT[i].YEAR;
                //        model.SOURCENAME = listReasonT[i].SOURCENAME;

                //        LISVALUE = new List<int>();
                //    }

                //    LISVALUE.Add(listReasonT[i].COUNT);
                //    if (LISTNAME.Count < reasonMax)
                //    {
                //        LISTNAME.Add(listReasonT[i].SOURCENAME);
                //    }

                //    if (listReasonT[i].NUMS == 1)
                //    {
                //        model.NAME1 = listReasonT[i].SOURCENAME;
                //        model.COUNT1 = listReasonT[i].COUNT;
                //    }
                //    if (listReasonT[i].NUMS == 2)
                //    {
                //        model.NAME2 = listReasonT[i].SOURCENAME;
                //        model.COUNT2 = listReasonT[i].COUNT;
                //    }
                //    if (listReasonT[i].NUMS == 3)
                //    {
                //        model.NAME3 = listReasonT[i].SOURCENAME;
                //        model.COUNT3 = listReasonT[i].COUNT;
                //    }
                //    if (listReasonT[i].NUMS == 4)
                //    {
                //        model.NAME4 = listReasonT[i].SOURCENAME;
                //        model.COUNT4 = listReasonT[i].COUNT;
                //    }
                //    if (listReasonT[i].NUMS == 5)
                //    {
                //        model.NAME5 = listReasonT[i].SOURCENAME;
                //        model.COUNT5 = listReasonT[i].COUNT;
                //    }

                //    if (reasonMax == listReasonT[i].NUMS)
                //    {
                //        model.LISVALUE = LISVALUE;
                //        model.LISTNAME = LISTNAME;
                //        listReason.Add(model);
                //    }
                //}
                //result.ListR = listReason;

                #endregion

                #region //月度 隐患整改情况


                List<T_BI_HOME_BSRISKREASON> listCorrectT = BIHomeService.GetHomeCorrect(filter.OrgId, listDep).ToList();
                string Val = string.Empty;// 0 ,1,2,3
                //string NameMonth = dtNow.AddMonths(-5).Month.ToString() + "月," + dtNow.AddMonths(-4).Month.ToString() + "月," + dtNow.AddMonths(-3).Month.ToString() + "月," + dtNow.AddMonths(-2).Month.ToString() + "月," + dtNow.AddMonths(-1).Month.ToString() + "月," + dtNow.Month.ToString() + "月";
                string NameMonth = dtNow.AddMonths(-5).ToString("yyyy-MM") + "," + dtNow.AddMonths(-4).ToString("yyyy-MM") + "," + dtNow.AddMonths(-3).ToString("yyyy-MM") + "," + dtNow.AddMonths(-2).ToString("yyyy-MM") + "," + dtNow.AddMonths(-1).ToString("yyyy-MM") + "," + dtNow.ToString("yyyy-MM");
                List<HomeCorrect> listC = new List<HomeCorrect>();
                HomeCorrect modelC = null;
                bool isNormal = true;//数据是否正常

                var modelCorr = listCorrectT.Where(e => e.YEAR == dtNow.Year && e.MONTH == dtNow.Month);
                if (modelCorr == null)
                {
                    isNormal = false;
                }

                for (int i = 0; i < listCorrectT.Count; i++)
                {
                    if (i == 0 || listCorrectT[i].DEPARTMENT_ID != listCorrectT[i - 1].DEPARTMENT_ID || listCorrectT[i].SOURCEVAL != listCorrectT[i - 1].SOURCEVAL)
                    {
                        if (!isNormal && !string.IsNullOrEmpty(Val) && modelC != null)
                        {
                            modelC.Val = Val + "0";
                            listC.Add(modelC);
                        }
                        modelC = new HomeCorrect();
                        modelC.DEPARTMENTNAME = listCorrectT[i].DEPARTMENTNAME;
                        modelC.DEPARTMENT_ID = listCorrectT[i].DEPARTMENT_ID;
                        modelC.SOURCENAME = listCorrectT[i].SOURCENAME;
                        modelC.YEAR = listCorrectT[i].YEAR;
                        modelC.NAME = NameMonth;
                        Val = "";
                    }
                    if (listCorrectT[i].MONTH == 1)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M1 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M1 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M1 + ",";
                    }


                    if (listCorrectT[i].MONTH == 2)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M2 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M2 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M2 + ",";
                    }

                    if (listCorrectT[i].MONTH == 3)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M3 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M3 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M3 + ",";
                    }

                    if (listCorrectT[i].MONTH == 4)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M4 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M4 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M4 + ",";
                    }

                    if (listCorrectT[i].MONTH == 5)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M5 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M5 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M5 + ",";
                    }

                    if (listCorrectT[i].MONTH == 6)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M6 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M6 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M6 + ",";
                    }

                    if (listCorrectT[i].MONTH == 7)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M7 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M7 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M7 + ",";
                    }

                    if (listCorrectT[i].MONTH == 8)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M8 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M8 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M8 + ",";
                    }

                    if (listCorrectT[i].MONTH == 9)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M9 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M9 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M9 + ",";
                    }

                    if (listCorrectT[i].MONTH == 10)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M10 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M10 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M10 + ",";
                    }

                    if (listCorrectT[i].MONTH == 11)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M11 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M11 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M11 + ",";
                    }

                    if (listCorrectT[i].MONTH == 12)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M12 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M12 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M12 + ",";
                    }

                    if (isNormal && listCorrectT[i].MONTH == dtNow.Month && listCorrectT[i].YEAR == dtNow.Year)
                    {
                        //有当月数据 是当前月的时候 放入数据
                        modelC.Val = Val.Substring(0, Val.Length - 1);
                        listC.Add(modelC);
                    }
                }
                result.ListC = listC;

                #endregion

                #region    年度 各下属组织 隐患整改情况

                List<T_BI_HOME_BSRISKREASON> listReasonYearT = BIHomeService.GetHomeCorrectYear(filter.OrgId, listDep).ToList();
                List<HomeCorrectYear> ListCYear = new List<HomeCorrectYear>();
                HomeCorrectYear modelCY = null;
                int CountYearT = listReasonYearT.Count;
                for (int i = 0; i < CountYearT; i++)
                {
                    if (i == 0 || listReasonYearT[i].DEPARTMENT_ID != listReasonYearT[i - 1].DEPARTMENT_ID)
                    {
                        modelCY = new HomeCorrectYear();
                        modelCY.DEPARTMENTNAME = listReasonYearT[i].DEPARTMENTNAME;
                        modelCY.DEPARTMENT_ID = listReasonYearT[i].DEPARTMENT_ID;
                        modelCY.YEAR = listReasonYearT[i].YEAR;
                    }

                    // 按时整改隐患 5     超期整改隐患 10 未整改隐患 15
                    if (listReasonYearT[i].SOURCEVAL == 5)
                    {
                        modelCY.CountIn += listReasonYearT[i].COUNT;
                    }
                    else if (listReasonYearT[i].SOURCEVAL == 10)
                    {
                        modelCY.CountOut += listReasonYearT[i].COUNT;
                    }
                    else if (listReasonYearT[i].SOURCEVAL == 15)
                    {
                        modelCY.CountNot += listReasonYearT[i].COUNT;
                    }

                    if (i == CountYearT - 1 || listReasonYearT[i].DEPARTMENT_ID != listReasonYearT[i + 1].DEPARTMENT_ID)
                    {
                        modelCY.Count = modelCY.CountIn + modelCY.CountOut + modelCY.CountNot;
                        ListCYear.Add(modelCY);
                    }
                }

                result.ListCYear = ListCYear;

                #endregion


                #region      月度 6 个月 安全检查、检查类型 数量 

                //按生产单元 获取

                if (!string.IsNullOrEmpty(filter.Keyword))
                {
                    //int MineType = int.Parse(filter.Keyword.ToString());
                    //BaseFilter filterD = new BaseFilter();
                    //filterD.SelectField = new List<string> { "ID", "NAME", "DEPARTMENT_TYPE", "PARENT_ID" };

                    //var listDep1MineType = GetEntities<T_FM_DEPARTMENT>(t => t.MineType.HasValue && t.MineType == MineType && t.DEPARTMENT_TYPE == 0, filterD, null);//部门就可以了 因为都相向 统计了
                    if (listDep != null && listDep.Any())
                    {
                        List<HomeSafeCheckCountMonth> ListSafeCheckYearMonth = new List<HomeSafeCheckCountMonth>();
                        int befoMonth = 12;
                        List<T_BI_HOME_BSRISKREASON> ListSafeCheckYearMonthT = BIHomeService.GetHomeCheckCountMonth(filter.OrgId, listDep.ToList(), befoMonth).ToList();
                        var listCheckTypeIDT = ListSafeCheckYearMonthT.Select(e => e.SOURCEID).Distinct().ToList();

                        var enums = GetEntity<T_FM_ENUMS>(e => e.VALUE == MineType && e.CODE == "BSMineTypeEnum");
                        if (enums != null)
                        {
                            var listTypeMine = GetEntities<MS.Domain.Entities.BS.T_BS_CHECK_TYPE_MINETYPE>(e => e.MINETYPE_ENUMS_ID == enums.ID, null, null);
                            if (listTypeMine != null && listTypeMine.Any())
                            {
                                var listMineCheckTypeID = listTypeMine.Where(e => e.CHECK_TYPE_ID.HasValue).Select(e => e.CHECK_TYPE_ID.Value);
                                for (int i = 0; i < listCheckTypeIDT.Count; i++)
                                {
                                    if (!listMineCheckTypeID.Contains(listCheckTypeIDT[i]))
                                    {
                                        listCheckTypeIDT.RemoveAt(i);
                                        i--;
                                    }
                                }
                            }
                        }

                        var listCheckType = GetEntities<MS.Domain.Entities.BS.T_BS_CHECK_TYPE>(e => listCheckTypeIDT.Contains(e.ID), null, null).OrderBy(e => e.PARENT_ID).ThenBy(e => e.NUM).ToList();

                        List<Guid> listCheckTypeID = new List<Guid>();
                        List<string> listCheckTypeNAME = new List<string>();

                        Guid IDZH = Guid.Empty;//综合检查(只有一个)
                        List<Guid> listGUID = new List<Guid>();//综合检查子集
                        for (int i = 0; i < listCheckType.Count; i++)
                        {
                            if (listCheckType[i].PARENT_ID.HasValue)
                            {
                                if (IDZH == Guid.Empty)
                                {
                                    IDZH = listCheckType[i].PARENT_ID.Value;
                                    listCheckTypeID.Add(IDZH);
                                    var checkTypeP = GetEntity<MS.Domain.Entities.BS.T_BS_CHECK_TYPE>(IDZH);
                                    listCheckTypeNAME.Add(checkTypeP.NAME);
                                }
                                listGUID.Add(listCheckType[i].ID);
                            }
                            else
                            {
                                listCheckTypeID.Add(listCheckType[i].ID);
                                listCheckTypeNAME.Add(listCheckType[i].NAME);
                            }
                        }

                        int year = DateTime.Now.Year;
                        int Month = DateTime.Now.Month;
                        DateTime dtTemp = DateTime.Now;
                        IEnumerable<T_BI_HOME_BSRISKREASON> listPerChearch = null;
                        IEnumerable<T_BI_HOME_BSRISKREASON> listPerChearchPer = null;

                        //List<int> listListCount = null;
                        for (int i = 0; i < befoMonth; i++)
                        {
                            dtTemp = dtNow.AddMonths(-1 * i);
                            year = dtTemp.Year;
                            Month = dtTemp.Month;
                            listPerChearch = ListSafeCheckYearMonthT.Where(e => e.YEAR == year && e.MONTH == Month);
                            HomeSafeCheckCountMonth modelper = new HomeSafeCheckCountMonth();
                            modelper.Total = listPerChearch.Count();
                            modelper.listSOURCENAME = listCheckTypeNAME;
                            modelper.YEAR = year;
                            modelper.MONTH = Month;
                            List<int> listListCount = new List<int>();

                            for (int j = 0; j < listCheckTypeID.Count; j++)
                            {
                                if (listCheckTypeID[j] != IDZH)
                                {
                                    listPerChearchPer = listPerChearch.Where(e => e.YEAR == year && e.MONTH == Month && e.SOURCEID == listCheckTypeID[j]);//各个检查
                                }
                                else
                                {
                                    listPerChearchPer = listPerChearch.Where(e => e.YEAR == year && e.MONTH == Month && listGUID.Contains(e.SOURCEID));//综合检查
                                }

                                if (listPerChearchPer == null || !listPerChearchPer.Any())
                                {
                                    listListCount.Add(0);
                                }
                                else
                                {
                                    listListCount.Add(listPerChearchPer.Sum(e => e.COUNT));
                                }
                            }
                            modelper.listListCount = listListCount;
                            ListSafeCheckYearMonth.Add(modelper);
                        }
                        result.ListSafeCheckYearMonth = ListSafeCheckYearMonth.OrderBy(e => e.YEAR).ThenBy(e => e.MONTH).ToList();
                    }
                }


                #endregion

                return result;
            });
        }

        /// <summary>
        /// 首页 安全检查次数统计明细(BI054HomeDetail)
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("GetHomeDetailMonthCheckType")]
        public JsonActionResult<HomeDetailSafeCheckCountMonth> GetHomeDetailMonthCheckType([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                //按生产单元 获取数据 
                HomeDetailSafeCheckCountMonth result = new HomeDetailSafeCheckCountMonth();
                if (string.IsNullOrEmpty(filter.Keyword))
                {
                    return result;
                }
                int MineType = int.Parse(filter.Keyword);

                BaseFilter filterD = new BaseFilter();
                filterD.SelectField = new List<string> { "ID", "NAME", "DEPARTMENT_TYPE", "PARENT_ID" };


                var listDep1MineType = GetEntities<T_FM_DEPARTMENT>(t => t.MineType.HasValue && t.MineType == MineType && (t.DEPARTMENT_TYPE == 1 || t.DEPARTMENT_TYPE == 2), filterD, null);
                //FMDepartmentType 部门=0,  车间=1,   班组=2,  公司=3
                if (listDep1MineType == null || !listDep1MineType.Any())
                {
                    return result;
                }
                List<Guid> listDepIDCJ = listDep1MineType.Where(e => e.DEPARTMENT_TYPE == 1).Select(e => e.ID).ToList();//车间ID 用于 统计 每个月各类型的检查数量

                DateTime dtNow = DateTime.Now;
                List<T_FM_DEPARTMENT> listDepCJ = new List<T_FM_DEPARTMENT>();
                List<T_FM_DEPARTMENT> listDepBZ = new List<T_FM_DEPARTMENT>();

                listDepCJ = listDep1MineType.Where(e => e.DEPARTMENT_TYPE == 1).OrderBy(e => e.NUM).ToList();
                listDepBZ = listDep1MineType.Where(e => e.DEPARTMENT_TYPE == 2).OrderBy(e => e.NUM).ToList();
                T_FM_ENUMS enums = null;

                #region      月度 12 个月 安全检查、检查类型 数量 

                if (listDepCJ == null || listDepCJ.Count < 1 || listDepBZ == null || listDepBZ.Count < 1)
                {
                    //深度查找 部门 MineType 只能存一个值
                    enums = GetEntity<T_FM_ENUMS>(e => e.VALUE == MineType && e.CODE == "BSMineTypeEnum");
                    if (enums == null)
                    {
                        throw new Exception("获取生产类型失败");
                    }

                    var listDepID = GetEntities<T_FM_DEPARTMENT_PRODUCTION_UNIT>(e => e.ENUMS_ID == enums.ID, null, null).Select(e => e.DEPARTMENT_ID);
                    if (listDepID != null && listDepID.Any())
                    {
                        listDep1MineType = GetEntities<T_FM_DEPARTMENT>(t => listDepID.Contains(t.ID) && (t.DEPARTMENT_TYPE == 1 || t.DEPARTMENT_TYPE == 2), filterD, null);
                        if (listDep1MineType != null && listDep1MineType.Any())
                        {
                            listDepCJ = listDep1MineType.Where(e => e.DEPARTMENT_TYPE == 1).ToList();
                            listDepBZ = listDep1MineType.Where(e => e.DEPARTMENT_TYPE == 2).ToList();
                        }
                    }
                }

                if (listDepCJ == null || listDepCJ.Count < 1 || listDepBZ == null || listDepBZ.Count < 1)
                {
                    throw new Exception("获取生产单元组织信息失败！");
                }

                List<HomeSafeCheckCountMonth> ListSafeCheckYearMonth = new List<HomeSafeCheckCountMonth>();
                int befoMonth = 11;//计算 12 个月 就是提前 11个月
                List<T_BI_HOME_BSRISKREASON> ListSafeCheckYearMonthT = BIHomeService.GetHomeCheckCountMonth(filter.OrgId, listDep1MineType.ToList(), befoMonth).ToList();

                var listCheckTypeIDT = ListSafeCheckYearMonthT.Select(e => e.SOURCEID).Distinct().ToList();
                if (enums == null)
                {
                    enums = GetEntity<T_FM_ENUMS>(e => e.VALUE == MineType && e.CODE == "BSMineTypeEnum");
                }
                if (enums != null)
                {
                    var listTypeMine = GetEntities<MS.Domain.Entities.BS.T_BS_CHECK_TYPE_MINETYPE>(e => e.MINETYPE_ENUMS_ID == enums.ID, null, null);
                    if (listTypeMine != null && listTypeMine.Any())
                    {
                        var listMineCheckTypeID = listTypeMine.Where(e => e.CHECK_TYPE_ID.HasValue).Select(e => e.CHECK_TYPE_ID.Value);
                        for (int i = 0; i < listCheckTypeIDT.Count; i++)
                        {
                            if (!listMineCheckTypeID.Contains(listCheckTypeIDT[i]))
                            {
                                listCheckTypeIDT.RemoveAt(i);
                                i--;
                            }
                        }
                    }
                }

                var listCheckType = GetEntities<MS.Domain.Entities.BS.T_BS_CHECK_TYPE>(e => listCheckTypeIDT.Contains(e.ID), null, null).OrderBy(e => e.PARENT_ID).ThenBy(e => e.NUM).ToList();

                List<Guid> listCheckTypeID = new List<Guid>();
                List<string> listCheckTypeNAME = new List<string>();

                Guid IDZH = Guid.Empty;//综合检查(只有一个)
                List<Guid> listGUID = new List<Guid>();//综合检查子集
                for (int i = 0; i < listCheckType.Count; i++)
                {
                    if (listCheckType[i].PARENT_ID.HasValue)
                    {
                        if (IDZH == Guid.Empty)
                        {
                            IDZH = listCheckType[i].PARENT_ID.Value;
                            listCheckTypeID.Add(IDZH);
                            var checkTypeP = GetEntity<MS.Domain.Entities.BS.T_BS_CHECK_TYPE>(IDZH);
                            listCheckTypeNAME.Add(checkTypeP.NAME);
                        }
                        listGUID.Add(listCheckType[i].ID);
                    }
                    else
                    {
                        listCheckTypeID.Add(listCheckType[i].ID);
                        listCheckTypeNAME.Add(listCheckType[i].NAME);
                    }
                }

                int year = DateTime.Now.Year;
                int Month = DateTime.Now.Month;
                DateTime dtTemp = DateTime.Now;
                IEnumerable<T_BI_HOME_BSRISKREASON> listPerChearch = null;
                IEnumerable<T_BI_HOME_BSRISKREASON> listPerChearchPer = null;

                for (int i = 0; i <= befoMonth; i++)
                {
                    dtTemp = dtNow.AddMonths(-1 * i);
                    year = dtTemp.Year;
                    Month = dtTemp.Month;
                    listPerChearch = ListSafeCheckYearMonthT.Where(e => e.YEAR == year && e.MONTH == Month && listDepIDCJ.Contains(e.DEPARTMENT_ID));//只统计到车间
                    HomeSafeCheckCountMonth modelper = new HomeSafeCheckCountMonth();
                    modelper.Total = listPerChearch.Count();
                    modelper.listSOURCENAME = listCheckTypeNAME;
                    modelper.YEAR = year;
                    modelper.MONTH = Month;
                    List<int> listListCount = new List<int>();

                    for (int j = 0; j < listCheckTypeID.Count; j++)
                    {
                        if (listCheckTypeID[j] != IDZH)
                        {
                            listPerChearchPer = listPerChearch.Where(e => e.YEAR == year && e.MONTH == Month && e.SOURCEID == listCheckTypeID[j]);//各个检查
                        }
                        else
                        {
                            listPerChearchPer = listPerChearch.Where(e => e.YEAR == year && e.MONTH == Month && listGUID.Contains(e.SOURCEID));//综合检查
                        }

                        if (listPerChearchPer == null || !listPerChearchPer.Any())
                        {
                            listListCount.Add(0);
                        }
                        else
                        {
                            listListCount.Add(listPerChearchPer.Sum(e => e.COUNT));
                        }
                    }
                    modelper.listListCount = listListCount;
                    ListSafeCheckYearMonth.Add(modelper);
                }

                #endregion

                result.ListSafeCheckYearMonth = ListSafeCheckYearMonth.OrderBy(e => e.YEAR).ThenBy(e => e.MONTH).ToList();

                #region    表数据

                List<DetailSafeCheckCountDepartment> listCheckDepCountCJ = new List<DetailSafeCheckCountDepartment>();
                List<DetailSafeCheckCountDepartment> listCheckDepCountBZ = new List<DetailSafeCheckCountDepartment>();

                List<int> listCheckTypeCount = null;
                DetailSafeCheckCountDepartment modelCountDep = null;
                int checkTypeCount = listCheckTypeID.Count;
                foreach (var item in listDepCJ)
                {
                    modelCountDep = new DetailSafeCheckCountDepartment();
                    modelCountDep.listName = listCheckTypeNAME;
                    modelCountDep.PARENT_ID = item.PARENT_ID.Value;
                    modelCountDep.Department_ID = item.ID;
                    modelCountDep.NUM = item.NUM;
                    modelCountDep.RowCount = listDepBZ.Where(e => e.PARENT_ID.HasValue && e.PARENT_ID.Value == item.ID).Count();
                    modelCountDep.DepartmentName = item.NAME;
                    modelCountDep.DepartmentType = item.DEPARTMENT_TYPE;
                    listCheckTypeCount = new List<int>();
                    for (int i = 0; i < checkTypeCount; i++)
                    {
                        listCheckTypeCount.Add(ListSafeCheckYearMonthT.Where(e => e.SOURCEID == listCheckTypeID[i] && e.DEPARTMENT_ID == item.ID).Sum(e => e.COUNT));
                    }
                    modelCountDep.listCount = listCheckTypeCount;
                    listCheckDepCountCJ.Add(modelCountDep);
                }

                foreach (var item in listDepBZ)
                {
                    modelCountDep = new DetailSafeCheckCountDepartment();
                    modelCountDep.listName = listCheckTypeNAME;
                    modelCountDep.PARENT_ID = item.PARENT_ID.Value;
                    modelCountDep.Department_ID = item.ID;
                    modelCountDep.NUM = item.NUM;
                    modelCountDep.RowCount = 1;
                    modelCountDep.DepartmentName = item.NAME;
                    modelCountDep.DepartmentType = item.DEPARTMENT_TYPE;
                    listCheckTypeCount = new List<int>();
                    for (int i = 0; i < checkTypeCount; i++)
                    {
                        listCheckTypeCount.Add(ListSafeCheckYearMonthT.Where(e => e.SOURCEID == listCheckTypeID[i] && e.DEPARTMENT_ID == item.ID).Sum(e => e.COUNT));
                    }
                    modelCountDep.listCount = listCheckTypeCount;
                    listCheckDepCountBZ.Add(modelCountDep);
                }

                //result.ListSafeCheckCountDepartmentBZ = listCheckDepCountBZ;
                foreach (var item in listCheckDepCountCJ)
                {
                    item.listChild = listCheckDepCountBZ.Where(e => e.PARENT_ID == item.Department_ID).OrderBy(e => e.NUM).ToList();
                }

                #endregion

                //result.ListSafeCheckCountDepartment = listCheckDepCountCJ;

                //取明细
                List<DetailSafeCheckCountDepartment> ListSafeCheckYearMonthLast = new List<DetailSafeCheckCountDepartment>();
                int allIndex = 0;
                foreach (var item in listCheckDepCountCJ)
                {
                    foreach (var itemC in item.listChild)
                    {
                        allIndex++;
                        itemC.NUM = allIndex;
                        itemC.Parent = item;
                        ListSafeCheckYearMonthLast.Add(itemC);
                    }
                }
                result.ListSafeCheckCountDepartment = ListSafeCheckYearMonthLast;

                return result;
            });
        }

        /// <summary>
        /// 月度隐患整改情况(BI055HomeDetail)
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("GetHomeDetailMonthRisk")]
        public JsonActionResult<HomeDetailRiskCountMonth> GetHomeDetailMonthRisk([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                //按生产单元 获取数据 
                HomeDetailRiskCountMonth result = new HomeDetailRiskCountMonth();
                if (string.IsNullOrEmpty(filter.Keyword))
                {
                    return result;
                }
                int MineType = int.Parse(filter.Keyword);

                BaseFilter filterD = new BaseFilter();
                filterD.SelectField = new List<string> { "ID", "NAME", "DEPARTMENT_TYPE", "PARENT_ID" };

                ///部门
                //var listDep1MineTypeSum = GetEntities<T_FM_DEPARTMENT>(t => t.MineType.HasValue && t.MineType == MineType && t.DEPARTMENT_TYPE == 0, filterD, null);
                ///车间 班组
                var listDep1MineType = GetEntities<T_FM_DEPARTMENT>(t => t.MineType.HasValue && t.MineType == MineType && (t.DEPARTMENT_TYPE == 1 || t.DEPARTMENT_TYPE == 2 || t.DEPARTMENT_TYPE == 0), filterD, null);

                //FMDepartmentType 部门=0,  车间=1,   班组=2,  公司=3
                if (listDep1MineType == null || !listDep1MineType.Any())
                {
                    return result;
                }

                DateTime dtNow = DateTime.Now;
                List<T_FM_DEPARTMENT> listDepCJ = new List<T_FM_DEPARTMENT>();
                List<T_FM_DEPARTMENT> listDepBZ = new List<T_FM_DEPARTMENT>();

                listDepCJ = listDep1MineType.Where(e => e.DEPARTMENT_TYPE == 1).OrderBy(e => e.NUM).ToList();
                listDepBZ = listDep1MineType.Where(e => e.DEPARTMENT_TYPE == 2).OrderBy(e => e.NUM).ToList();


                #region //月度 隐患整改情况
                int MonthBefo = 11;//计算12个月 就是提前 11个月

                List<T_BI_HOME_BSRISKREASON> listCorrectT = BIHomeService.GetHomeCorrect(filter.OrgId, listDep1MineType.ToList(), MonthBefo).ToList();
                string Val = string.Empty;// 0 ,1,2,3

                //string NameMonth = dtNow.AddMonths(-5).Month.ToString() + "月," + dtNow.AddMonths(-4).Month.ToString() + "月," + dtNow.AddMonths(-3).Month.ToString() + "月," + dtNow.AddMonths(-2).Month.ToString() + "月," + dtNow.AddMonths(-1).Month.ToString() + "月," + dtNow.Month.ToString() + "月";
                string NameMonth = String.Empty;
                for (int i = MonthBefo; i > -1; i--)
                {
                    NameMonth += dtNow.AddMonths(-1 * i).ToString("yy-MM") + ",";
                }
                NameMonth = NameMonth.Substring(0, NameMonth.Length - 1);


                List<HomeCorrect> listC = new List<HomeCorrect>();
                HomeCorrect modelC = null;
                bool isNormal = true;//数据是否正常

                var modelCorr = listCorrectT.Where(e => e.YEAR == dtNow.Year && e.MONTH == dtNow.Month);
                if (modelCorr == null)
                {
                    isNormal = false;
                }

                for (int i = 0; i < listCorrectT.Count; i++)
                {
                    if (i == 0 || listCorrectT[i].DEPARTMENT_ID != listCorrectT[i - 1].DEPARTMENT_ID || listCorrectT[i].SOURCEVAL != listCorrectT[i - 1].SOURCEVAL)
                    {
                        if (!isNormal && !string.IsNullOrEmpty(Val) && modelC != null)
                        {
                            modelC.Val = Val + "0";
                            listC.Add(modelC);
                        }

                        modelC = new HomeCorrect();
                        modelC.DEPARTMENTNAME = listCorrectT[i].DEPARTMENTNAME;
                        modelC.DEPARTMENT_ID = listCorrectT[i].DEPARTMENT_ID;
                        modelC.SOURCENAME = listCorrectT[i].SOURCENAME;
                        modelC.YEAR = listCorrectT[i].YEAR;
                        modelC.NAME = NameMonth;
                        Val = "";
                    }
                    if (listCorrectT[i].MONTH == 1)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M1 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M1 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M1 + ",";
                    }


                    if (listCorrectT[i].MONTH == 2)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M2 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M2 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M2 + ",";
                    }

                    if (listCorrectT[i].MONTH == 3)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M3 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M3 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M3 + ",";
                    }

                    if (listCorrectT[i].MONTH == 4)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M4 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M4 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M4 + ",";
                    }

                    if (listCorrectT[i].MONTH == 5)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M5 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M5 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M5 + ",";
                    }

                    if (listCorrectT[i].MONTH == 6)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M6 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M6 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M6 + ",";
                    }

                    if (listCorrectT[i].MONTH == 7)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M7 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M7 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M7 + ",";
                    }

                    if (listCorrectT[i].MONTH == 8)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M8 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M8 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M8 + ",";
                    }

                    if (listCorrectT[i].MONTH == 9)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M9 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M9 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M9 + ",";
                    }

                    if (listCorrectT[i].MONTH == 10)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M10 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M10 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M10 + ",";
                    }

                    if (listCorrectT[i].MONTH == 11)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M11 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M11 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M11 + ",";
                    }

                    if (listCorrectT[i].MONTH == 12)
                    {
                        if (listCorrectT[i].SOURCEVAL == 20)
                        {
                            modelC.M12 = listCorrectT[i].PERCENT.ToString("0.00");
                        }
                        else
                        {
                            modelC.M12 = listCorrectT[i].COUNT.ToString();
                        }
                        Val += modelC.M12 + ",";
                    }

                    if (isNormal && listCorrectT[i].MONTH == dtNow.Month && listCorrectT[i].YEAR == dtNow.Year)
                    {
                        //有当月数据 是当前月的时候 放入数据
                        modelC.Val = Val.Substring(0, Val.Length - 1);
                        listC.Add(modelC);
                    }
                }


                #endregion

                var listDepID = listDep1MineType.Where(e => e.DEPARTMENT_TYPE == 0).Select(e => e.ID);//部门ID
                result.ListC = listC.Where(e => listDepID.Contains(e.DEPARTMENT_ID)).ToList();

                #region    子表信息

                //BSCorrectType
                List<EnumsResult> enums = DataHelper.GetEnum("BSCorrectType");

                List<string> listName = new List<string>();
                List<int> listVal = new List<int>();
                foreach (var item in enums)
                {
                    listVal.Add(item.ID);
                    listName.Add(item.NAME);
                }

                List<DetailSafeCheckCountDepartment> listCheckDepCountCJ = new List<DetailSafeCheckCountDepartment>();
                List<DetailSafeCheckCountDepartment> listCheckDepCountBZ = new List<DetailSafeCheckCountDepartment>();

                List<int> listCheckTypeCount = null;
                DetailSafeCheckCountDepartment modelCountDep = null;
                int checkTypeCount = listVal.Count;

                int Percent = 3;//第4项 是辈分比
                int All = 0;
                foreach (var item in listDepCJ)
                {
                    modelCountDep = new DetailSafeCheckCountDepartment();
                    modelCountDep.listName = listName;
                    modelCountDep.PARENT_ID = item.PARENT_ID.Value;
                    modelCountDep.Department_ID = item.ID;
                    modelCountDep.NUM = item.NUM;
                    modelCountDep.RowCount = listDepBZ.Where(e => e.PARENT_ID.HasValue && e.PARENT_ID.Value == item.ID).Count();
                    modelCountDep.DepartmentName = item.NAME;
                    modelCountDep.DepartmentType = item.DEPARTMENT_TYPE;
                    listCheckTypeCount = new List<int>();
                    for (int i = 0; i < checkTypeCount; i++)
                    {
                        if (Percent == i)
                        {
                            All = listCheckTypeCount[0] + listCheckTypeCount[1] + listCheckTypeCount[2];
                            if (All == 0)
                            {
                                listCheckTypeCount.Add(10000);
                            }
                            else
                            {
                                listCheckTypeCount.Add((int)(10000 * (listCheckTypeCount[0] + listCheckTypeCount[1]) / All));
                            }
                        }
                        else
                        {
                            listCheckTypeCount.Add(listCorrectT.Where(e => e.SOURCEVAL == listVal[i] && e.DEPARTMENT_ID == item.ID).Sum(e => e.COUNT));
                        }
                    }
                    modelCountDep.listCount = listCheckTypeCount;
                    listCheckDepCountCJ.Add(modelCountDep);
                }

                foreach (var item in listDepBZ)
                {
                    modelCountDep = new DetailSafeCheckCountDepartment();
                    modelCountDep.listName = listName;
                    modelCountDep.PARENT_ID = item.PARENT_ID.Value;
                    modelCountDep.Department_ID = item.ID;
                    modelCountDep.NUM = item.NUM;
                    modelCountDep.RowCount = 1;
                    modelCountDep.DepartmentName = item.NAME;
                    modelCountDep.DepartmentType = item.DEPARTMENT_TYPE;
                    listCheckTypeCount = new List<int>();
                    for (int i = 0; i < checkTypeCount; i++)
                    {
                        if (Percent == i)
                        {
                            All = listCheckTypeCount[0] + listCheckTypeCount[1] + listCheckTypeCount[2];
                            if (All == 0)
                            {
                                listCheckTypeCount.Add(10000);
                            }
                            else
                            {
                                listCheckTypeCount.Add((int)(10000 * (listCheckTypeCount[0] + listCheckTypeCount[1]) / All));
                            }
                        }
                        else
                        {
                            listCheckTypeCount.Add(listCorrectT.Where(e => e.SOURCEVAL == listVal[i] && e.DEPARTMENT_ID == item.ID).Sum(e => e.COUNT));
                        }
                    }
                    modelCountDep.listCount = listCheckTypeCount;
                    listCheckDepCountBZ.Add(modelCountDep);
                }

                foreach (var item in listCheckDepCountCJ)
                {
                    item.listChild = listCheckDepCountBZ.Where(e => e.PARENT_ID == item.Department_ID).OrderBy(e => e.NUM).ToList();
                }

                //result.ListSafeCheckYearMonth = listCheckDepCountCJ;

                //取明细
                List<DetailSafeCheckCountDepartment> ListSafeCheckYearMonth = new List<DetailSafeCheckCountDepartment>();
                int allIndex = 0;
                foreach (var item in listCheckDepCountCJ)
                {
                    foreach (var itemC in item.listChild)
                    {
                        allIndex++;
                        itemC.NUM = allIndex;
                        itemC.Parent = item;
                        ListSafeCheckYearMonth.Add(itemC);
                    }
                }

                result.ListSafeCheckYearMonth = ListSafeCheckYearMonth;

                #endregion

                return result;
            });
        }
    }

    /// <summary>
    /// 隐患原因 数据
    /// </summary>
    public class HomeRISKREASON
    {
        /// <summary>
        /// 年
        /// </summary>
        public int YEAR { get; set; }

        /// <summary>
        /// 组织ID
        /// </summary>
        public Guid DEPARTMENT_ID { get; set; }

        /// <summary>
        /// 组织ID
        /// </summary>
        public string DEPARTMENTNAME { get; set; }

        /// <summary>
        /// 类型
        /// </summary>
        public string SOURCENAME { get; set; }

        /// <summary>
        /// 隐患原因
        /// </summary>
        public List<string> LISTNAME { get; set; }

        /// <summary>
        /// 隐患原因数量
        /// </summary>
        public List<int> LISVALUE { get; set; }

        /// <summary>
        /// 数据源排序
        /// </summary>
        public int NUMS { get; set; }

        /// <summary>
        /// 数据1
        /// </summary>
        public int COUNT1 { get; set; }

        /// <summary>
        /// 名称1
        /// </summary>
        public string NAME1 { get; set; }

        /// <summary>
        /// 数据2
        /// </summary>
        public int COUNT2 { get; set; }

        /// <summary>
        /// 名称2
        /// </summary>
        public string NAME2 { get; set; }

        /// <summary>
        /// 数据3
        /// </summary>
        public int COUNT3 { get; set; }

        /// <summary>
        /// 名称3
        /// </summary>
        public string NAME3 { get; set; }

        /// <summary>
        /// 数据4
        /// </summary>
        public int COUNT4 { get; set; }

        /// <summary>
        /// 名称4
        /// </summary>
        public string NAME4 { get; set; }

        /// <summary>
        /// 数据5
        /// </summary>
        public int COUNT5 { get; set; }

        /// <summary>
        /// 名称5
        /// </summary>
        public string NAME5 { get; set; }

    }

    /// <summary>
    /// 整改情况
    /// </summary>
    public class HomeCorrect
    {
        /// <summary>
        /// 年
        /// </summary>
        public int YEAR { get; set; }

        /// <summary>
        /// 月
        /// </summary>
        public int MONTH { get; set; }

        /// <summary>
        /// 组织ID
        /// </summary>
        public Guid DEPARTMENT_ID { get; set; }

        /// <summary>
        /// 组织ID
        /// </summary>
        public string DEPARTMENTNAME { get; set; }


        /// <summary>
        /// 类型
        /// </summary>
        public string SOURCENAME { get; set; }


        /// <summary>
        /// 1月
        /// </summary>
        public string M1 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M2 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M3 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M4 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M5 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M6 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M7 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M8 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M9 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M10 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M11 { get; set; }

        /// <summary>
        /// 1月
        /// </summary>
        public string M12 { get; set; }

        public string Val { get; set; }
        public string NAME { get; set; }
    }

    /// <summary>
    /// 组织 年度 隐患整改情况
    /// </summary>
    public class HomeCorrectYear
    {
        /// <summary>
        /// 年
        /// </summary>
        public int YEAR { get; set; }

        /// <summary>
        /// 组织ID
        /// </summary>
        public Guid DEPARTMENT_ID { get; set; }

        /// <summary>
        /// 组织名称
        /// </summary>
        public string DEPARTMENTNAME { get; set; }

        /// <summary>
        /// 按时整改
        /// </summary>
        public int CountIn { get; set; }

        /// <summary>
        /// 超时整改
        /// </summary>
        public int CountOut { get; set; }

        /// <summary>
        /// 未整改
        /// </summary>
        public int CountNot { get; set; }

        /// <summary>
        /// 未整改
        /// </summary>
        public int Count { get; set; }

    }

    /// <summary>
    /// 安全检查 月度 检查类型 数量
    /// </summary>
    public class HomeSafeCheckCountMonth
    {
        /// <summary>
        /// 年
        /// </summary>
        public int YEAR { get; set; }

        /// <summary>
        /// 月
        /// </summary>
        public int MONTH { get; set; }

        /// <summary>
        /// 总数
        /// </summary>
        public int Total { get; set; }
        public List<int> listListCount { get; set; }
        public List<string> listSOURCENAME { get; set; }
    }


    /// <summary>
    /// 首页数据组合
    /// </summary>
    public class HomeResult
    {
        /// <summary>
        /// 隐患原因
        /// </summary>
        public List<HomeRISKREASON> ListR { get; set; }

        /// <summary>
        /// 月度隐患整改情况
        /// </summary>
        public List<HomeCorrect> ListC { get; set; }


        /// <summary>
        /// 年度隐患整改情况
        /// </summary>
        public List<HomeCorrectYear> ListCYear { get; set; }


        /// <summary>
        /// 安全检查 月度 数量
        /// </summary>
        public List<HomeSafeCheckCountMonth> ListSafeCheckYearMonth { get; set; }
    }


    /// <summary>
    /// 组织 安全检查类型 对应次数  
    /// </summary>
    public class DetailSafeCheckCountDepartment
    {
        /// <summary>
        /// 组织名称
        /// </summary>
        public string DepartmentName { get; set; }

        /// <summary>
        /// 父级ID
        /// </summary>
        public Guid PARENT_ID { get; set; }
        /// <summary>
        /// 部门ID
        /// </summary>
        public Guid Department_ID { get; set; }
        /// <summary>
        /// 排序
        /// </summary>
        public int NUM { get; set; }
        /// <summary>
        /// 组织类型
        /// </summary>
        public int DepartmentType { get; set; }

        /// <summary>
        /// 数据占行数(班组为1 车间为下一级班组数量)
        /// </summary>
        public int RowCount { get; set; } = 1;

        /// <summary>
        /// 数量集合（每次传参 集合顺序一致）
        /// </summary>
        public List<int> listCount { get; set; }

        /// <summary>
        /// 隐患整改率（月度隐患整改情况明细 专属）
        /// </summary>
        public decimal Percent { get; set; }

        /// <summary>
        /// 名称集合 （每次传参 集合顺序一致）
        /// </summary>
        public List<string> listName { get; set; }

        /// <summary>
        /// 子信息
        /// </summary>
        public List<DetailSafeCheckCountDepartment> listChild { get; set; }

        /// <summary>
        /// 父项
        /// </summary>
        public DetailSafeCheckCountDepartment Parent { get; set; }

    }

    /// <summary>
    /// 安全检查次数统计
    /// </summary>
    public class HomeDetailSafeCheckCountMonth
    {

        /// <summary>
        /// 安全检查 月度 数量
        /// </summary>
        public List<HomeSafeCheckCountMonth> ListSafeCheckYearMonth { get; set; }

        /// <summary>
        /// 每个组织 每种检查 对应次数
        /// </summary>
        public List<DetailSafeCheckCountDepartment> ListSafeCheckCountDepartment { get; set; }

    }

    /// <summary>
    /// 安全检查次数统计
    /// </summary>
    public class HomeDetailRiskCountMonth
    {
        /// <summary>
        /// 月度隐患整改情况
        /// </summary>
        public List<HomeCorrect> ListC { get; set; }

        /// <summary>
        /// 安全检查 月度 数量
        /// </summary>
        public List<DetailSafeCheckCountDepartment> ListSafeCheckYearMonth { get; set; }
    }
}