using FineUIPro;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
namespace BLL
{
    /// 
    /// 领导带班检查
    /// 
    public static class Check_ProjectLeaderCheckService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 根据主键获取领导带班检查
        /// 
        /// 
        /// 
        public static Model.Check_ProjectLeaderCheck GetProjectLeaderCheckById(string ProjectLeaderCheckId)
        {
            return Funs.DB.Check_ProjectLeaderCheck.FirstOrDefault(e => e.ProjectLeaderCheckId == ProjectLeaderCheckId);
        }
        #region 公司两级领导带班检查列表
        /// 
        /// 记录数
        /// 
        public static int count
        {
            get;
            set;
        }
        /// 
        /// 定义变量
        /// 
        private static IQueryable getDataLists = from x in db.Check_ProjectLeaderCheck
                                                                                 select x;
        /// 
        ///  数据列表
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable getListData(string projectId, string startTime, string endTime, Grid Grid1)
        {
            IQueryable getDataList = getDataLists;
            if (!string.IsNullOrEmpty(projectId))
            {
                getDataList = getDataList.Where(x => x.ProjectId == projectId);
            }
            if (!string.IsNullOrEmpty(startTime))
            {
                DateTime? startTimeD = Funs.GetNewDateTime(startTime);
                if (startTimeD.HasValue)
                {
                    getDataList = getDataList.Where(x => x.CheckDate >= startTimeD);
                }
            }
            if (!string.IsNullOrEmpty(endTime))
            {
                DateTime? endTimeD = Funs.GetNewDateTime(endTime);
                if (endTimeD.HasValue)
                {
                    getDataList = getDataList.Where(x => x.CheckDate <= endTimeD);
                }
            }
            count = getDataList.Count();
            if (count == 0)
            {
                return null;
            }
            getDataList = SortConditionHelper.SortingAndPaging(getDataList, Grid1.SortField, Grid1.SortDirection, Grid1.PageIndex, Grid1.PageSize);
            return from x in getDataList
                   join y in db.Base_Project on x.ProjectId equals y.ProjectId               
                   into ps                  
                   from y in ps.DefaultIfEmpty()
                   select new
                   {
                       x.ProjectLeaderCheckId,
                       x.ProjectId,
                       db.Base_Project.First(u => u.ProjectId == x.ProjectId).ProjectName,
                       x.CheckCode,
                       x.UnitIds,
                       x.LeaderIds,
                       x.LeaderNames,
                       x.QuestionDef,
                       x.CheckDate,
                       x.IsHoldMeet,
                       IsHoldMeetName = x.IsHoldMeet == true ? "是" : "否",
                       x.Requirement,
                   };
        }
        #endregion
        /// 
        /// 根据日期获取领导带班检查集合
        /// 
        /// 开始时间
        /// 结束时间
        /// 项目号
        /// 领导带班检查集合
        public static List GetProjectLeaderCheckListsByDate(DateTime startTime, DateTime endTime, string projectId)
        {
            return (from x in Funs.DB.Check_ProjectLeaderCheck where x.CheckDate >= startTime && x.CheckDate <= endTime && x.ProjectId == projectId orderby x.CheckDate select x).ToList();
        }
        /// 
        /// 根据时间段获取HSE领导带班检查集合
        /// 
        /// 开始时间
        /// 结束时间
        /// 项目Id
        /// 时间段内的HSE领导带班检查集合
        public static int GetCountByDate(DateTime startTime, DateTime endTime, string projectId)
        {
            return (from x in Funs.DB.Check_ProjectLeaderCheck where x.CheckDate >= startTime && x.CheckDate <= endTime && x.ProjectId == projectId select x).Count();
        }
        /// 
        /// 添加领导带班检查
        /// 
        /// 
        public static void AddProjectLeaderCheck(Model.Check_ProjectLeaderCheck ProjectLeaderCheck)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Check_ProjectLeaderCheck newProjectLeaderCheck = new Model.Check_ProjectLeaderCheck
            {
                ProjectLeaderCheckId = ProjectLeaderCheck.ProjectLeaderCheckId,
                ProjectId = ProjectLeaderCheck.ProjectId,
                UnitIds = ProjectLeaderCheck.UnitIds,
                CheckCode = ProjectLeaderCheck.CheckCode,
                LeaderIds = ProjectLeaderCheck.LeaderIds,
                CheckDate = ProjectLeaderCheck.CheckDate,
                QuestionDef = ProjectLeaderCheck.QuestionDef,
                IsHoldMeet = ProjectLeaderCheck.IsHoldMeet,
                Requirement = ProjectLeaderCheck.Requirement,
                LeaderNames = ProjectLeaderCheck.LeaderNames,
                CompileMan = ProjectLeaderCheck.CompileMan,
                CompileDate = ProjectLeaderCheck.CompileDate,
            };
            db.Check_ProjectLeaderCheck.InsertOnSubmit(newProjectLeaderCheck);
            db.SubmitChanges();
            CodeRecordsService.InsertCodeRecordsByMenuIdProjectIdUnitId(Const.ProjectLeaderCheckMenuId, ProjectLeaderCheck.ProjectId, null, ProjectLeaderCheck.ProjectLeaderCheckId, ProjectLeaderCheck.CheckDate);
        }
        /// 
        /// 修改领导带班检查
        /// 
        /// 
        public static void UpdateProjectLeaderCheck(Model.Check_ProjectLeaderCheck ProjectLeaderCheck)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Check_ProjectLeaderCheck newProjectLeaderCheck = db.Check_ProjectLeaderCheck.FirstOrDefault(e => e.ProjectLeaderCheckId == ProjectLeaderCheck.ProjectLeaderCheckId);
            if (newProjectLeaderCheck != null)
            {
                newProjectLeaderCheck.UnitIds = ProjectLeaderCheck.UnitIds;
                newProjectLeaderCheck.CheckCode = ProjectLeaderCheck.CheckCode;
                newProjectLeaderCheck.LeaderIds = ProjectLeaderCheck.LeaderIds;
                newProjectLeaderCheck.CheckDate = ProjectLeaderCheck.CheckDate;
                newProjectLeaderCheck.QuestionDef = ProjectLeaderCheck.QuestionDef;
                newProjectLeaderCheck.IsHoldMeet = ProjectLeaderCheck.IsHoldMeet;
                newProjectLeaderCheck.Requirement = ProjectLeaderCheck.Requirement;
                newProjectLeaderCheck.LeaderNames = ProjectLeaderCheck.LeaderNames;
                newProjectLeaderCheck.CompileMan = ProjectLeaderCheck.CompileMan;
                newProjectLeaderCheck.CompileDate = ProjectLeaderCheck.CompileDate;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除领导带班检查
        /// 
        /// 
        public static void DeleteProjectLeaderCheckById(string ProjectLeaderCheckId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Check_ProjectLeaderCheck ProjectLeaderCheck = db.Check_ProjectLeaderCheck.FirstOrDefault(e => e.ProjectLeaderCheckId == ProjectLeaderCheckId);
            if (ProjectLeaderCheck != null)
            {
                CodeRecordsService.DeleteCodeRecordsByDataId(ProjectLeaderCheckId);
                ////删除附件表
                BLL.CommonService.DeleteAttachFileById(ProjectLeaderCheckId);
                db.Check_ProjectLeaderCheck.DeleteOnSubmit(ProjectLeaderCheck);
                db.SubmitChanges();
            }
        }
        public static string getLeaderName(object ProjectLeaderCheckId)
        {
            string name = string.Empty;
            if (ProjectLeaderCheckId != null)
            {
                var getData = Check_ProjectLeaderCheckService.GetProjectLeaderCheckById(ProjectLeaderCheckId.ToString());
                if (getData != null)
                {
                    if (!string.IsNullOrEmpty(getData.LeaderIds))
                    {
                        name = UserService.getUserNamesUserIds(getData.LeaderIds);
                    }
                    if (!string.IsNullOrEmpty(getData.LeaderNames))
                    {
                        name += "," + getData.LeaderNames;
                    }
                }
            }
            return name;
        }
    }
}