using APT.Infrastructure.Core;
using System;
using APT.Infrastructure.Api;
using APT.MS.Domain.Enums;
using APT.BaseData.Domain.IServices.BI;
using APT.MS.Domain.Entities.BI;
using System.Collections.Generic;
using APT.BaseData.Domain.Entities.FM;
using APT.MS.Domain.Entities.BS;
using System.Linq.Expressions;
using System.Linq;
using APT.BaseData.Domain.Enums;

namespace APT.BaseData.Services.Services.BI
{
    public partial class BIHomeService : CommonService, IBIHomeService
    {
        public BIHomeService(IRepository repository) : base(repository)
        {

        }



        /// <summary>
        /// 查询首页的隐患原因
        /// </summary>
        /// <param name="OrgID"></param>
        /// <param name="listDep">查询部门</param>
        /// <returns></returns>
        public IEnumerable<T_BI_HOME_BSRISKREASON> GetHomeReasons(Guid? OrgID, List<T_FM_DEPARTMENT> listDep)
        {
            if (listDep == null || listDep.Count < 1)
            {
                return new List<T_BI_HOME_BSRISKREASON>();
            }

            int year = DateTime.Now.Year;
            BaseFilter filter = new BaseFilter();
            filter.OrgId = OrgID;
            filter.Order = DbOrder.ASC;
            filter.Sort = "NUM";
            List<Guid> listDepID = listDep.Select(e => e.ID).ToList();
            var listModelReason = GetEntities<T_BS_RISK_REASON>(e => !e.IS_DELETED, filter);
            Expression<Func<T_BI_HOME_BSRISKREASON, bool>> expression = e => e.YEAR == year && e.TYPE == BSCaculateType.RiskReason;
            if (listDepID.Count > 1)
                expression = expression.And(e => listDepID.Contains(e.DEPARTMENT_ID));
            else
                expression = expression.And(e => e.DEPARTMENT_ID == listDepID[0]);

            BaseFilter filterR = new BaseFilter();
            filterR.OrgId = OrgID;
            //filterR.Order = DbOrder.ASC;
            //filterR.Sort = "NUMD";
            filterR.Orders.Add(new DataOrder() { Field = "NUMD", Order = DbOrder.ASC });
            filterR.Orders.Add(new DataOrder() { Field = "NUMS", Order = DbOrder.ASC });
            IEnumerable<T_BI_HOME_BSRISKREASON> listReason = GetOrderEntities(expression, filterR);

            foreach (var item in listReason)
            {
                item.SOURCENAME = listModelReason.First(e => e.ID == item.SOURCEID).NAME;
                item.DEPARTMENTNAME = listDep.First(e => e.ID == item.DEPARTMENT_ID).NAME;
            }

            return listReason;
        }

        /// <summary>
        /// 查询首页的隐患整改情况 月度
        /// </summary>
        /// <param name="OrgID"></param>
        /// <param name="listDep">查询部门</param>
        /// <param name="MonthBefo">提前月份</param>
        /// <returns></returns>
        public IEnumerable<T_BI_HOME_BSRISKREASON> GetHomeCorrect(Guid? OrgID, List<T_FM_DEPARTMENT> listDep, int? MonthBefo = 5, FMDepartmentType? DEPARTMENT_TYPE = null)
        {
            //if (listDep == null || listDep.Count < 1)
            //{
            //    return new List<T_BI_HOME_BSRISKREASON>();
            //}

            DateTime dtMin = DateTime.Now.AddMonths(-1 * MonthBefo.Value);
            List<Guid> listDepID = null;
            if (listDep != null && listDep.Count > 0)
            {
                listDepID = listDep.Select(e => e.ID).ToList();
            }
            Expression<Func<T_BI_HOME_BSRISKREASON, bool>> expression = e => e.TYPE == BSCaculateType.RiskCorrect;
            if (dtMin.Year == DateTime.Now.Year)
            {
                expression = expression.And(e => e.YEAR == dtMin.Year && e.MONTH >= dtMin.Month);
            }
            else
            {
                expression = expression.And(e => ((e.YEAR == dtMin.Year && e.MONTH >= dtMin.Month) || e.YEAR > dtMin.Year));
            }
            if (DEPARTMENT_TYPE.HasValue)
            {
                expression = expression.And(e => e.DEPARTMENT_TYPE == DEPARTMENT_TYPE);
            }
            if (listDepID != null)
            {
                if (listDepID.Count > 1)
                    expression = expression.And(e => listDepID.Contains(e.DEPARTMENT_ID));
                else
                    expression = expression.And(e => e.DEPARTMENT_ID == listDepID[0]);
            }
            BaseFilter filterR = new BaseFilter();
            filterR.OrgId = OrgID;
            //filterR.Order = DbOrder.ASC;
            //filterR.Sort = "NUMD";
            filterR.Orders.Add(new DataOrder() { Field = "DEPARTMENT_ID", Order = DbOrder.ASC });
            filterR.Orders.Add(new DataOrder() { Field = "SOURCEVAL", Order = DbOrder.ASC });// 直接按枚举值 排序 NUMS 
            filterR.Orders.Add(new DataOrder() { Field = "YEAR", Order = DbOrder.ASC });// 直接按枚举值 排序 NUMS 
            filterR.Orders.Add(new DataOrder() { Field = "MONTH", Order = DbOrder.ASC });// 直接按枚举值 排序 NUMS 
            IEnumerable<T_BI_HOME_BSRISKREASON> listReason = GetOrderEntities(expression, filterR);

            foreach (var item in listReason)
            {
                item.SOURCENAME = ((BSCorrectType)item.SOURCEVAL).GetDescription();
                if (listDep != null && listDep.Count > 0)
                {
                    item.DEPARTMENTNAME = listDep.First(e => e.ID == item.DEPARTMENT_ID).NAME;
                }
                else
                {
                    item.DEPARTMENTNAME = "公司级";
                }
            }
            return listReason;
        }


        /// <summary>
        /// 查询首页的隐患整改情况 年度 方法使用前提 先执行一下12个月的 整改情况
        /// </summary>
        /// <param name="OrgID"></param>
        /// <param name="listDep">查询部门</param>
        /// <param name="MonthBefo">提前月份</param>
        /// <returns></returns>
        public IEnumerable<T_BI_HOME_BSRISKREASON> GetHomeCorrectYear(Guid? OrgID, List<T_FM_DEPARTMENT> listDep, int MonthBefo = 0)
        {
            if (listDep == null || listDep.Count < 1)
            {
                return new List<T_BI_HOME_BSRISKREASON>();
            }

            int year = DateTime.Now.Year;
            List<Guid> listDepID = listDep.Select(e => e.ID).ToList();
            Expression<Func<T_BI_HOME_BSRISKREASON, bool>> expression = e => e.TYPE == BSCaculateType.RiskCorrect && e.SOURCEVAL != 20;
            if (MonthBefo == 0)
            {
                expression = expression.And(e => e.YEAR == year);
            }
            else
            {
                DateTime dtMin = DateTime.Now.AddMonths(-1 * MonthBefo);
                expression = expression.And(e => ((e.YEAR == dtMin.Year && e.MONTH >= dtMin.Month) || e.YEAR > dtMin.Year));
            }

            if (listDepID.Count > 1)
                expression = expression.And(e => listDepID.Contains(e.DEPARTMENT_ID));
            else
                expression = expression.And(e => e.DEPARTMENT_ID == listDepID[0]);

            BaseFilter filterR = new BaseFilter();
            filterR.OrgId = OrgID;
            //filterR.Order = DbOrder.ASC;
            //filterR.Sort = "NUMD";
            filterR.Orders.Add(new DataOrder() { Field = "DEPARTMENT_ID", Order = DbOrder.ASC });
            filterR.Orders.Add(new DataOrder() { Field = "SOURCEVAL", Order = DbOrder.ASC });// 直接按枚举值 排序 NUMS 
            IEnumerable<T_BI_HOME_BSRISKREASON> listReason = GetOrderEntities(expression, filterR);

            foreach (var item in listReason)
            {
                item.SOURCENAME = ((BSCorrectType)item.SOURCEVAL).GetDescription();
                item.DEPARTMENTNAME = listDep.First(e => e.ID == item.DEPARTMENT_ID).NAME;
            }
            return listReason;
        }




        /// <summary>
        /// 安全检查 月度 检查类型 数量
        /// </summary>
        /// <param name="OrgID"></param>
        /// <param name="listDep">查询部门</param>
        /// <param name="MonthBefo">提前月份</param>
        /// <returns></returns>
        public IEnumerable<T_BI_HOME_BSRISKREASON> GetHomeCheckCountMonth(Guid? OrgID, List<T_FM_DEPARTMENT> listDep, int? MonthBefo = 5)
        {
            if (listDep == null || listDep.Count < 1)
            {
                return new List<T_BI_HOME_BSRISKREASON>();
            }

            DateTime dtMin = DateTime.Now.AddMonths(-1 * MonthBefo.Value);

            List<Guid> listDepID = listDep.Select(e => e.ID).ToList();

            Expression<Func<T_BI_HOME_BSRISKREASON, bool>> expression = e => e.TYPE == BSCaculateType.SafeCheck;
            if (dtMin.Year == DateTime.Now.Year)
            {
                expression = expression.And(e => e.YEAR == dtMin.Year && e.MONTH >= dtMin.Month);
            }
            else
            {
                expression = expression.And(e => ((e.YEAR == dtMin.Year && e.MONTH >= dtMin.Month) || e.YEAR > dtMin.Year));
            }
            if (listDepID.Count > 1)
                expression = expression.And(e => listDepID.Contains(e.DEPARTMENT_ID));
            else
                expression = expression.And(e => e.DEPARTMENT_ID == listDepID[0]);

            BaseFilter filterR = new BaseFilter();
            filterR.OrgId = OrgID;
            //filterR.Order = DbOrder.ASC;
            //filterR.Sort = "NUMD";
            filterR.Orders.Add(new DataOrder() { Field = "DEPARTMENT_ID", Order = DbOrder.ASC });
            filterR.Orders.Add(new DataOrder() { Field = "SOURCEID", Order = DbOrder.ASC });// 直接按枚举值 排序 NUMS 
            filterR.Orders.Add(new DataOrder() { Field = "MONTH", Order = DbOrder.ASC });// 直接按枚举值 排序 NUMS 
            IEnumerable<T_BI_HOME_BSRISKREASON> listReason = GetOrderEntities(expression, filterR);

            //foreach (var item in listReason)
            //{
            //    //item.SOURCENAME = ((BSCorrectType)item.SOURCEVAL).GetDescription();
            //    item.DEPARTMENTNAME = listDep.First(e => e.ID == item.DEPARTMENT_ID).NAME;
            //}

            return listReason;
        }


    }
}
