using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
    /// 
    /// 开工前检查
    /// 
    public static class Check_CheckWorkService
    {
        /// 
        /// 根据开工前检查ID获取开工前检查信息
        /// 
        /// 
        /// 
        public static Model.Check_CheckWork GetCheckWorkByCheckWorkId(string checkWorkId)
        {
            return Funs.DB.Check_CheckWork.FirstOrDefault(e => e.CheckWorkId == checkWorkId);
        }
        /// 
        /// 根据时间段获取开工前检查信息集合
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int GetCountByCheckTime(DateTime startTime, DateTime endTime, string projectId)
        {
            return (from x in Funs.DB.Check_CheckWork 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_CheckWork
                    join y in Funs.DB.Check_CheckWorkDetail on x.CheckWorkId equals y.CheckWorkId
                    where x.CheckTime >= startTime && x.CheckTime <= endTime && x.ProjectId == projectId
                    select y).Count();
        }
        /// 
        /// 添加安全开工前检查
        /// 
        /// 
        public static void AddCheckWork(Model.Check_CheckWork checkWork)
        {
            Model.Check_CheckWork newCheckWork = new Model.Check_CheckWork
            {
                CheckWorkId = checkWork.CheckWorkId,
                CheckWorkCode = checkWork.CheckWorkCode,
                ProjectId = checkWork.ProjectId,
                Area = checkWork.Area,
                CheckTime = checkWork.CheckTime,
                ThisUnitId = checkWork.ThisUnitId,
                MainUnitPerson = checkWork.MainUnitPerson,
                SubUnits = checkWork.SubUnits,
                SubUnitPerson = checkWork.SubUnitPerson,
                PartInPersonNames = checkWork.PartInPersonNames,
                MainUnitDeputy = checkWork.MainUnitDeputy,
                SubUnitDeputy = checkWork.SubUnitDeputy,
                MainUnitDeputyDate = checkWork.MainUnitDeputyDate,
                SubUnitDeputyDate = checkWork.SubUnitDeputyDate,
                AttachUrl = checkWork.AttachUrl,
                IsCompleted = checkWork.IsCompleted,
                States = checkWork.States,
                CompileMan = checkWork.CompileMan,
                IsAgree = checkWork.IsAgree
            };
            Funs.DB.Check_CheckWork.InsertOnSubmit(newCheckWork);
            Funs.DB.SubmitChanges();
            ////增加一条编码记录
            BLL.CodeRecordsService.InsertCodeRecordsByMenuIdProjectIdUnitId(BLL.Const.ProjectCheckWorkMenuId, checkWork.ProjectId, null, checkWork.CheckWorkId, checkWork.CheckTime);
        }
        /// 
        /// 修改安全开工前检查
        /// 
        /// 
        public static void UpdateCheckWork(Model.Check_CheckWork checkWork)
        {
            Model.Check_CheckWork newCheckWork = Funs.DB.Check_CheckWork.FirstOrDefault(e => e.CheckWorkId == checkWork.CheckWorkId);
            if (newCheckWork != null)
            {
                newCheckWork.Area = checkWork.Area;
                newCheckWork.CheckTime = checkWork.CheckTime;
                newCheckWork.ThisUnitId = checkWork.ThisUnitId;
                newCheckWork.MainUnitPerson = checkWork.MainUnitPerson;
                newCheckWork.SubUnits = checkWork.SubUnits;
                newCheckWork.SubUnitPerson = checkWork.SubUnitPerson;
                newCheckWork.PartInPersonNames = checkWork.PartInPersonNames;
                newCheckWork.MainUnitDeputy = checkWork.MainUnitDeputy;
                newCheckWork.SubUnitDeputy = checkWork.SubUnitDeputy;
                newCheckWork.MainUnitDeputyDate = checkWork.MainUnitDeputyDate;
                newCheckWork.SubUnitDeputyDate = checkWork.SubUnitDeputyDate;
                newCheckWork.AttachUrl = checkWork.AttachUrl;
                newCheckWork.IsCompleted = checkWork.IsCompleted;
                newCheckWork.States = checkWork.States;
                newCheckWork.IsAgree = checkWork.IsAgree;
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 根据开工前检查ID删除对应开工前检查记录信息
        /// 
        /// 
        public static void DeleteCheckWork(string checkWorkId)
        {
            var q = (from x in Funs.DB.Check_CheckWork where x.CheckWorkId == checkWorkId select x).FirstOrDefault();
            if (q != null)
            {
                ///删除编码表记录
                BLL.CodeRecordsService.DeleteCodeRecordsByDataId(q.CheckWorkId);
                ////删除附件表
                BLL.CommonService.DeleteAttachFileById(q.CheckWorkId);
                ///删除工程师日志收集记录
                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.CheckWorkId);
                Funs.DB.Check_CheckWork.DeleteOnSubmit(q);
                Funs.DB.SubmitChanges();
            }
        }
    }
}