using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
    /// 
    /// 季节性/节假日检查
    /// 
    public static class Check_CheckHolidayService
    {
        /// 
        /// 根据季节性/节假日检查ID获取季节性/节假日检查信息
        /// 
        /// 
        /// 
        public static Model.Check_CheckHoliday GetCheckHolidayByCheckHolidayId(string checkHolidayId)
        {
            return Funs.DB.Check_CheckHoliday.FirstOrDefault(e => e.CheckHolidayId == checkHolidayId);
        }
        /// 
        /// 根据时间段获取季节性/节假日检查信息集合
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int GetCountByCheckTime(DateTime startTime, DateTime endTime, string projectId)
        {
            return (from x in Funs.DB.Check_CheckHoliday where x.CheckTime >= startTime && x.CheckTime < endTime && x.ProjectId == projectId && x.States == BLL.Const.State_2 select x).Count();
        }
        /// 
        /// 根据时间段获取已完成的季节性/节假日检查整改数量
        /// 
        /// 开始时间
        /// 结束时间
        /// 项目号
        /// 已完成的季节性/节假日检查整改数量
        public static int GetIsOKViolationCountByCheckTime(DateTime startTime, DateTime endTime, string projectId)
        {
            return (from x in Funs.DB.Check_CheckHoliday
                    join y in Funs.DB.Check_CheckHolidayDetail on x.CheckHolidayId equals y.CheckHolidayId
                    where x.CheckTime >= startTime && x.CheckTime <= endTime && x.ProjectId == projectId
                    select y).Count();
        }
        /// 
        /// 添加安全季节性/节假日检查
        /// 
        /// 
        public static void AddCheckHoliday(Model.Check_CheckHoliday checkHoliday)
        {
            Model.Check_CheckHoliday newCheckHoliday = new Model.Check_CheckHoliday
            {
                CheckHolidayId = checkHoliday.CheckHolidayId,
                CheckHolidayCode = checkHoliday.CheckHolidayCode,
                ProjectId = checkHoliday.ProjectId,
                Area = checkHoliday.Area,
                CheckTime = checkHoliday.CheckTime,
                ThisUnitId = checkHoliday.ThisUnitId,
                MainUnitPerson = checkHoliday.MainUnitPerson,
                SubUnits = checkHoliday.SubUnits,
                SubUnitPerson = checkHoliday.SubUnitPerson,
                PartInPersonNames = checkHoliday.PartInPersonNames,
                MainUnitDeputy = checkHoliday.MainUnitDeputy,
                SubUnitDeputy = checkHoliday.SubUnitDeputy,
                MainUnitDeputyDate = checkHoliday.MainUnitDeputyDate,
                SubUnitDeputyDate = checkHoliday.SubUnitDeputyDate,
                AttachUrl = checkHoliday.AttachUrl,
                IsCompleted = checkHoliday.IsCompleted,
                States = checkHoliday.States,
                CompileMan = checkHoliday.CompileMan
            };
            Funs.DB.Check_CheckHoliday.InsertOnSubmit(newCheckHoliday);
            Funs.DB.SubmitChanges();
            ////增加一条编码记录
            BLL.CodeRecordsService.InsertCodeRecordsByMenuIdProjectIdUnitId(BLL.Const.ProjectCheckHolidayMenuId, checkHoliday.ProjectId, null, checkHoliday.CheckHolidayId, checkHoliday.CheckTime);
        }
        /// 
        /// 修改安全季节性/节假日检查
        /// 
        /// 
        public static void UpdateCheckHoliday(Model.Check_CheckHoliday checkHoliday)
        {
            Model.Check_CheckHoliday newCheckHoliday = Funs.DB.Check_CheckHoliday.FirstOrDefault(e => e.CheckHolidayId == checkHoliday.CheckHolidayId);
            if (newCheckHoliday != null)
            {
                newCheckHoliday.Area = checkHoliday.Area;
                newCheckHoliday.CheckTime = checkHoliday.CheckTime;
                newCheckHoliday.ThisUnitId = checkHoliday.ThisUnitId;
                newCheckHoliday.MainUnitPerson = checkHoliday.MainUnitPerson;
                newCheckHoliday.SubUnits = checkHoliday.SubUnits;
                newCheckHoliday.SubUnitPerson = checkHoliday.SubUnitPerson;
                newCheckHoliday.PartInPersonNames = checkHoliday.PartInPersonNames;
                newCheckHoliday.MainUnitDeputy = checkHoliday.MainUnitDeputy;
                newCheckHoliday.SubUnitDeputy = checkHoliday.SubUnitDeputy;
                newCheckHoliday.MainUnitDeputyDate = checkHoliday.MainUnitDeputyDate;
                newCheckHoliday.SubUnitDeputyDate = checkHoliday.SubUnitDeputyDate;
                newCheckHoliday.AttachUrl = checkHoliday.AttachUrl;
                newCheckHoliday.IsCompleted = checkHoliday.IsCompleted;
                newCheckHoliday.States = checkHoliday.States;
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 根据季节性/节假日检查ID删除对应季节性/节假日检查记录信息
        /// 
        /// 
        public static void DeleteCheckHoliday(string checkHolidayId)
        {
            var q = (from x in Funs.DB.Check_CheckHoliday where x.CheckHolidayId == checkHolidayId select x).FirstOrDefault();
            if (q != null)
            {
                ///删除编码表记录
                BLL.CodeRecordsService.DeleteCodeRecordsByDataId(q.CheckHolidayId);
                ////删除附件表
                BLL.CommonService.DeleteAttachFileById(q.CheckHolidayId);
                ///删除工程师日志收集记录
                if (!string.IsNullOrEmpty(q.MainUnitPerson))
                {
                    List mainUnitPersonIds = Funs.GetStrListByStr(q.MainUnitPerson, ',');
                    foreach (var item in mainUnitPersonIds)
                    {
                        BLL.HSSELogService.CollectHSSELog(q.ProjectId, item, q.CheckTime, "21", "季节性和节假日前HSE检查", Const.BtnDelete, 1);
                    }
                }
                if (!string.IsNullOrEmpty(q.SubUnitPerson))
                {
                    List subUnitPersonIds = Funs.GetStrListByStr(q.SubUnitPerson, ',');
                    foreach (var item in subUnitPersonIds)
                    {
                        BLL.HSSELogService.CollectHSSELog(q.ProjectId, item, q.CheckTime, "21", "季节性和节假日前HSE检查", Const.BtnDelete, 1);
                    }
                }
                BLL.CommonService.DeleteFlowOperateByID(q.CheckHolidayId);
                Funs.DB.Check_CheckHoliday.DeleteOnSubmit(q);
                Funs.DB.SubmitChanges();
            }
        }
    }
}