using System;
using System.Linq;
namespace BLL
{
    /// 
    /// 其他危险源辨识文件
    /// 
    public static class Hazard_OtherHazardService
    {
        /// 
        /// 根据其他危险源辨识文件ID获取其他危险源辨识文件信息
        /// 
        /// 
        /// 
        public static Model.Hazard_OtherHazard GetOtherHazardByOtherHazardId(string registrationId)
        {
            return Funs.DB.Hazard_OtherHazard.FirstOrDefault(e => e.OtherHazardId == registrationId);
        }
        /// 
        /// 根据项目主键和开始、结束时间获得其他危险源辨识的数量
        /// 
        /// 项目主键
        /// 开始时间
        /// 结束时间
        /// 
        public static int GetOtherHazardCountByProjectIdAndDate(string projectId, DateTime startTime, DateTime endTime)
        {
            var q = (from x in Funs.DB.Hazard_OtherHazard where x.ProjectId == projectId && x.CompileDate >= startTime && x.CompileDate <= endTime select x).ToList();
            return q.Count();
        }
        /// 
        /// 添加安全其他危险源辨识文件
        /// 
        /// 
        public static void AddOtherHazard(Model.Hazard_OtherHazard registration)
        {
            Model.Hazard_OtherHazard newOtherHazard = new Model.Hazard_OtherHazard
            {
                OtherHazardId = registration.OtherHazardId,
                ProjectId = registration.ProjectId,
                OtherHazardCode = registration.OtherHazardCode,
                OtherHazardName = registration.OtherHazardName,
                AttachUrl = registration.AttachUrl,
                CompileMan = registration.CompileMan,
                CompileDate = registration.CompileDate,
                States = registration.States
            };
            Funs.DB.Hazard_OtherHazard.InsertOnSubmit(newOtherHazard);
            Funs.DB.SubmitChanges();
            ////增加一条编码记录
            BLL.CodeRecordsService.InsertCodeRecordsByMenuIdProjectIdUnitId(BLL.Const.OtherHazardMenuId, registration.ProjectId, null, registration.OtherHazardId, registration.CompileDate);
        }
        /// 
        /// 修改安全其他危险源辨识文件
        /// 
        /// 
        public static void UpdateOtherHazard(Model.Hazard_OtherHazard registration)
        {
            Model.Hazard_OtherHazard newOtherHazard = Funs.DB.Hazard_OtherHazard.FirstOrDefault(e => e.OtherHazardId == registration.OtherHazardId);
            if (newOtherHazard != null)
            {
                newOtherHazard.OtherHazardCode = registration.OtherHazardCode;
                newOtherHazard.OtherHazardName = registration.OtherHazardName;
                newOtherHazard.AttachUrl = registration.AttachUrl;
                newOtherHazard.CompileDate = registration.CompileDate;
                newOtherHazard.States = registration.States;
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 根据其他危险源辨识文件ID删除对应其他危险源辨识文件记录信息
        /// 
        /// 
        public static void DeleteOtherHazard(string registrationId)
        {
            var q = (from x in Funs.DB.Hazard_OtherHazard where x.OtherHazardId == registrationId select x).FirstOrDefault();
            if (q != null)
            {
                ///删除编码表记录
                BLL.CodeRecordsService.DeleteCodeRecordsByDataId(q.OtherHazardId);
                ////删除附件表
                BLL.CommonService.DeleteAttachFileById(q.OtherHazardId);
                Funs.DB.Hazard_OtherHazard.DeleteOnSubmit(q);
                Funs.DB.SubmitChanges();
            }
        }
    }
}