using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
    /// 
    /// 事故处理表
    /// 
    public static class AccidentStatisticsService
    {
        /// 
        /// 根据主键获取事故处理
        /// 
        /// 
        /// 
        public static Model.ProjectAccident_AccidentStatistics GetAccidentStatisticsById(string accidentStatisticsId)
        {
            return Funs.DB.ProjectAccident_AccidentStatistics.FirstOrDefault(e => e.AccidentStatisticsId == accidentStatisticsId);
        }
        /// 
        /// 添加事故处理
        /// 
        /// 
        public static void AddAccidentStatistics(Model.ProjectAccident_AccidentStatistics accidentStatistics)
        {
            Model.ProjectAccident_AccidentStatistics newAccidentStatistics = new Model.ProjectAccident_AccidentStatistics
            {
                AccidentStatisticsId = accidentStatistics.AccidentStatisticsId,
                ProjectId = accidentStatistics.ProjectId,
                UnitId = accidentStatistics.UnitId,
                Person = accidentStatistics.Person,
                Treatment = accidentStatistics.Treatment,
                CompileDate = accidentStatistics.CompileDate,
                AttachUrl = accidentStatistics.AttachUrl,
                States = accidentStatistics.States
            };
            Funs.DB.ProjectAccident_AccidentStatistics.InsertOnSubmit(newAccidentStatistics);
            Funs.DB.SubmitChanges();
        }
        /// 
        /// 修改事故处理
        /// 
        /// 
        public static void UpdateAccidentStatistics(Model.ProjectAccident_AccidentStatistics accidentStatistics)
        {
            Model.ProjectAccident_AccidentStatistics newAccidentStatistics = Funs.DB.ProjectAccident_AccidentStatistics.FirstOrDefault(e => e.AccidentStatisticsId == accidentStatistics.AccidentStatisticsId);
            if (newAccidentStatistics != null)
            {
                newAccidentStatistics.ProjectId = accidentStatistics.ProjectId;
                newAccidentStatistics.UnitId = accidentStatistics.UnitId;
                newAccidentStatistics.Person = accidentStatistics.Person;
                newAccidentStatistics.Treatment = accidentStatistics.Treatment;
                newAccidentStatistics.CompileDate = accidentStatistics.CompileDate;
                newAccidentStatistics.AttachUrl = accidentStatistics.AttachUrl;
                newAccidentStatistics.States = accidentStatistics.States;
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除事故处理
        /// 
        /// 
        public static void DeleteAccidentStatisticsById(string accidentStatisticsId)
        {
            Model.ProjectAccident_AccidentStatistics accidentStatistics = Funs.DB.ProjectAccident_AccidentStatistics.FirstOrDefault(e => e.AccidentStatisticsId == accidentStatisticsId);
            if (accidentStatistics != null)
            {
                if (!string.IsNullOrEmpty(accidentStatistics.AttachUrl))
                {
                    BLL.UploadFileService.DeleteFile(Funs.RootPath, accidentStatistics.AttachUrl);//删除附件
                }
                ////删除附件表
                BLL.CommonService.DeleteAttachFileById(accidentStatistics.AccidentStatisticsId);
                ///删除编码表记录
                BLL.CodeRecordsService.DeleteCodeRecordsByDataId(accidentStatistics.AccidentStatisticsId);
                ////删除流程表
                BLL.CommonService.DeleteFlowOperateByID(accidentStatistics.AccidentStatisticsId);
                Funs.DB.ProjectAccident_AccidentStatistics.DeleteOnSubmit(accidentStatistics);
                Funs.DB.SubmitChanges();
            }
        }
    }
}