using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
    /// 
    /// 安全领导小组会议
    /// 
    public static class SafetyLeaderGroupMeetingService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 根据主键获取安全领导小组会议
        /// 
        ///  
        /// 
        public static Model.Meeting_SafetyLeaderGroupMeeting GetSafetyLeaderGroupMeetingById(string SafetyLeaderGroupMeetingId)
        {
            return Funs.DB.Meeting_SafetyLeaderGroupMeeting.FirstOrDefault(e => e.SafetyLeaderGroupMeetingId == SafetyLeaderGroupMeetingId);
        }
        /// 
        /// 根据时间段获取安全领导小组会议集合
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int GetCountByTime(DateTime startTime, DateTime endTime, string projectId)
        {
            return (from x in Funs.DB.Meeting_SafetyLeaderGroupMeeting where x.SafetyLeaderGroupMeetingDate >= startTime && x.SafetyLeaderGroupMeetingDate < endTime && x.ProjectId == projectId && x.States == BLL.Const.State_2 select x).Count();
        }
        /// 
        /// 根据时间段获取安全领导小组会议参会人数
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int? GetSumAttentPersonNumByMeetingDate(DateTime startTime, DateTime endTime, string projectId)
        {
            int? sumAttentPersonNum = (from x in Funs.DB.Meeting_SafetyLeaderGroupMeeting where x.SafetyLeaderGroupMeetingDate >= startTime && x.SafetyLeaderGroupMeetingDate < endTime && x.ProjectId == projectId && x.States == BLL.Const.State_2 select x.AttentPersonNum).Sum();
            if (sumAttentPersonNum == null)
            {
                return 0;
            }
            return sumAttentPersonNum;
        }
        /// 
        /// 根据日期和类型获取会议记录集合
        /// 
        /// 开始时间
        /// 结束时间
        /// 项目号
        /// 会议记录集合
        public static List GetMeetingListsByDate(DateTime startTime, DateTime endTime, string projectId)
        {
            return (from x in Funs.DB.Meeting_SafetyLeaderGroupMeeting where x.SafetyLeaderGroupMeetingDate >= startTime && x.SafetyLeaderGroupMeetingDate <= endTime && x.ProjectId == projectId orderby x.SafetyLeaderGroupMeetingDate select x).ToList();
        }
        /// 
        /// 添加安全领导小组会议
        /// 
        /// 
        public static void AddSafetyLeaderGroupMeeting(Model.Meeting_SafetyLeaderGroupMeeting SafetyLeaderGroupMeeting)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Meeting_SafetyLeaderGroupMeeting newSafetyLeaderGroupMeeting = new Model.Meeting_SafetyLeaderGroupMeeting
            {
                SafetyLeaderGroupMeetingId = SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingId,
                ProjectId = SafetyLeaderGroupMeeting.ProjectId,
                UnitId = SafetyLeaderGroupMeeting.UnitId,
                SafetyLeaderGroupMeetingCode = SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingCode,
                SafetyLeaderGroupMeetingName = SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingName,
                SafetyLeaderGroupMeetingDate = SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingDate,
                CompileMan = SafetyLeaderGroupMeeting.CompileMan,
                SafetyLeaderGroupMeetingContents = SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingContents,
                CompileDate = SafetyLeaderGroupMeeting.CompileDate,
                States = SafetyLeaderGroupMeeting.States,
                AttentPersonNum = SafetyLeaderGroupMeeting.AttentPersonNum,
                MeetingHours = SafetyLeaderGroupMeeting.MeetingHours,
                MeetingHostMan = SafetyLeaderGroupMeeting.MeetingHostMan,
                AttentPerson = SafetyLeaderGroupMeeting.AttentPerson,
                MeetingPlace = SafetyLeaderGroupMeeting.MeetingPlace,
                MeetingHostManId = SafetyLeaderGroupMeeting.MeetingHostManId,
                AttentPersonIds = SafetyLeaderGroupMeeting.AttentPersonIds,
                MeetingHostManOther = SafetyLeaderGroupMeeting.MeetingHostManOther
            };
            db.Meeting_SafetyLeaderGroupMeeting.InsertOnSubmit(newSafetyLeaderGroupMeeting);
            db.SubmitChanges();
            ////增加一条编码记录
            BLL.CodeRecordsService.InsertCodeRecordsByMenuIdProjectIdUnitId(BLL.Const.ProjectSafetyLeaderGroupMeetingMenuId, SafetyLeaderGroupMeeting.ProjectId, null, SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingId, SafetyLeaderGroupMeeting.CompileDate);
        }
        /// 
        /// 修改安全领导小组会议
        /// 
        /// 
        public static void UpdateSafetyLeaderGroupMeeting(Model.Meeting_SafetyLeaderGroupMeeting SafetyLeaderGroupMeeting)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Meeting_SafetyLeaderGroupMeeting newSafetyLeaderGroupMeeting = db.Meeting_SafetyLeaderGroupMeeting.FirstOrDefault(e => e.SafetyLeaderGroupMeetingId == SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingId);
            if (newSafetyLeaderGroupMeeting != null)
            {
                //newSafetyLeaderGroupMeeting.ProjectId = SafetyLeaderGroupMeeting.ProjectId;
                newSafetyLeaderGroupMeeting.UnitId = SafetyLeaderGroupMeeting.UnitId;
                newSafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingCode = SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingCode;
                newSafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingName = SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingName;
                newSafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingDate = SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingDate;
                newSafetyLeaderGroupMeeting.CompileMan = SafetyLeaderGroupMeeting.CompileMan;
                newSafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingContents = SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingContents;
                newSafetyLeaderGroupMeeting.CompileDate = SafetyLeaderGroupMeeting.CompileDate;
                newSafetyLeaderGroupMeeting.States = SafetyLeaderGroupMeeting.States;
                newSafetyLeaderGroupMeeting.AttentPersonNum = SafetyLeaderGroupMeeting.AttentPersonNum;
                newSafetyLeaderGroupMeeting.MeetingHours = SafetyLeaderGroupMeeting.MeetingHours;
                newSafetyLeaderGroupMeeting.MeetingHostMan = SafetyLeaderGroupMeeting.MeetingHostMan;
                newSafetyLeaderGroupMeeting.AttentPerson = SafetyLeaderGroupMeeting.AttentPerson;
                newSafetyLeaderGroupMeeting.MeetingPlace = SafetyLeaderGroupMeeting.MeetingPlace;
                newSafetyLeaderGroupMeeting.MeetingHostManId = SafetyLeaderGroupMeeting.MeetingHostManId;
                newSafetyLeaderGroupMeeting.AttentPersonIds = SafetyLeaderGroupMeeting.AttentPersonIds;
                newSafetyLeaderGroupMeeting.MeetingHostManOther = SafetyLeaderGroupMeeting.MeetingHostManOther;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除安全领导小组会议
        /// 
        /// 
        public static void DeleteSafetyLeaderGroupMeetingById(string SafetyLeaderGroupMeetingId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Meeting_SafetyLeaderGroupMeeting SafetyLeaderGroupMeeting = db.Meeting_SafetyLeaderGroupMeeting.FirstOrDefault(e => e.SafetyLeaderGroupMeetingId == SafetyLeaderGroupMeetingId);
            if (SafetyLeaderGroupMeeting != null)
            {
                ///删除编码表记录
                BLL.CodeRecordsService.DeleteCodeRecordsByDataId(SafetyLeaderGroupMeetingId);
                BLL.CommonService.DeleteAttachFileById(SafetyLeaderGroupMeetingId);
                ////删除流程表
                BLL.CommonService.DeleteFlowOperateByID(SafetyLeaderGroupMeeting.SafetyLeaderGroupMeetingId);
                db.Meeting_SafetyLeaderGroupMeeting.DeleteOnSubmit(SafetyLeaderGroupMeeting);
                db.SubmitChanges();
            }
        }
    }
}