using NPOI.POIFS.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class PunishmentService
    {
        /// <summary>
        /// 根据主键获取扣款项
        /// </summary>
        /// <param name="punishmentId"></param>
        /// <returns></returns>
        public static Model.EMC_Punishment GetPunishmentById(string punishmentId)
        {
            return Funs.DB.EMC_Punishment.FirstOrDefault(e => e.PunishmentId == punishmentId);
        }

        public static Model.View_EMC_Punishment GetPunishmentViewById(string punishmentId)
        {
            return Funs.DB.View_EMC_Punishment.FirstOrDefault(e => e.PunishmentId == punishmentId);
        }

        public static Model.View_EMC_Que_Punishment GetQuePunishmentViewById(string punishmentId)
        {
            return Funs.DB.View_EMC_Que_Punishment.FirstOrDefault(e => e.PunishmentId == punishmentId);
        }



        /// <summary>
        /// 添加扣款项
        /// </summary>
        /// <param name="punishment"></param>
        public static void AddPunishment(Model.EMC_Punishment punishment)
        {
            Model.EMC_Punishment newPunishment = new Model.EMC_Punishment();
            newPunishment.PunishmentId = punishment.PunishmentId;
            newPunishment.FO_NO = punishment.FO_NO;
            newPunishment.ItemNo = punishment.ItemNo;
            newPunishment.SES_No = punishment.SES_No;
            newPunishment.PunishDate = punishment.PunishDate;
            newPunishment.Location = punishment.Location;
            newPunishment.Description = punishment.Description;
            newPunishment.Company = punishment.Company;
            newPunishment.Individual = punishment.Individual;
            newPunishment.ViolationDegree = punishment.ViolationDegree;
            newPunishment.BYC_RU = punishment.BYC_RU;
            newPunishment.Violation_Inspector = punishment.Violation_Inspector;
            newPunishment.CompletionDate = punishment.CompletionDate;
            newPunishment.Flag = punishment.Flag;
            newPunishment.ViolationRelatedSes = punishment.ViolationRelatedSes;
            newPunishment.SelectYesNo = punishment.SelectYesNo;
            newPunishment.Def=punishment.Def;
            newPunishment.CreateDate = punishment.CreateDate;

            newPunishment.Contractor= punishment.Contractor;
            newPunishment.Discipline=punishment.Discipline;
            newPunishment.ViolationClauseId= punishment.ViolationClauseId;
            newPunishment.IsFrame= punishment.IsFrame;
            newPunishment.ViolationPerson =punishment.ViolationPerson;
            newPunishment.BYC_Person= punishment.BYC_Person;
            newPunishment.Requisitioner=punishment.Requisitioner;
            newPunishment.AttachUrl = punishment.AttachUrl;
            newPunishment.States = punishment.States;
            newPunishment.MIContractor = punishment.MIContractor;
            newPunishment.SeType = punishment.SeType;
            newPunishment.PVTRequester = punishment.PVTRequester;
            newPunishment.IndividualSESReason = punishment.IndividualSESReason;
            newPunishment.SSRAdminId = punishment.SSRAdminId;
            newPunishment.SSRStaratDate = punishment.SSRStaratDate;
            newPunishment.SSREndDate=punishment.SSREndDate;
            newPunishment.MainCoordinatorStartDate = punishment.MainCoordinatorStartDate;
            newPunishment.MainCoordinatorEndDate = punishment.MainCoordinatorEndDate;
            newPunishment.UserDepStartDate = punishment.UserDepStartDate;
            newPunishment.UserDepEndDate = punishment.UserDepEndDate;

            Funs.DB.EMC_Punishment.InsertOnSubmit(newPunishment);
            Funs.DB.SubmitChanges();
        }

        /// <summary>
        /// 修改扣款项
        /// </summary>
        /// <param name="punishment"></param>
        public static void UpdatePunishment(Model.EMC_Punishment punishment)
        {
            Model.EMC_Punishment newPunishment = Funs.DB.EMC_Punishment.FirstOrDefault(e => e.PunishmentId == punishment.PunishmentId);
            if (newPunishment != null)
            {
                newPunishment.FO_NO = punishment.FO_NO;
                newPunishment.ItemNo = punishment.ItemNo;
                newPunishment.SES_No = punishment.SES_No;
                newPunishment.PunishDate = punishment.PunishDate;
                newPunishment.Location = punishment.Location;
                newPunishment.Description = punishment.Description;
                newPunishment.Company = punishment.Company;
                newPunishment.Individual = punishment.Individual;
                newPunishment.ViolationDegree = punishment.ViolationDegree;
                newPunishment.BYC_RU = punishment.BYC_RU;
                //newPunishment.Violation_Inspector = punishment.Violation_Inspector;
                newPunishment.CompletionDate = punishment.CompletionDate;
                newPunishment.ViolationRelatedSes = punishment.ViolationRelatedSes;
                newPunishment.SelectYesNo = punishment.SelectYesNo;
                newPunishment.Def = punishment.Def;

                newPunishment.Contractor = punishment.Contractor;
                newPunishment.Discipline = punishment.Discipline;
                newPunishment.ViolationClauseId = punishment.ViolationClauseId;
                newPunishment.IsFrame = punishment.IsFrame;
                newPunishment.ViolationPerson = punishment.ViolationPerson;
                newPunishment.BYC_Person = punishment.BYC_Person;
                newPunishment.Requisitioner = punishment.Requisitioner;
                newPunishment.AttachUrl = punishment.AttachUrl;
                newPunishment.States = punishment.States;
                newPunishment.ContractAdmin = punishment.ContractAdmin;
                newPunishment.IndividualPerson = punishment.IndividualPerson;
                newPunishment.UserDep = punishment.UserDep;
                newPunishment.MIContractor = punishment.MIContractor;
                newPunishment.SeType = punishment.SeType;
                newPunishment.PVTRequester = punishment.PVTRequester;
                newPunishment.IndividualSESReason = punishment.IndividualSESReason;
                newPunishment.SSRAdminId = punishment.SSRAdminId;
                newPunishment.SSRStaratDate = punishment.SSRStaratDate;
                newPunishment.SSREndDate = punishment.SSREndDate;
                newPunishment.MainCoordinatorStartDate = punishment.MainCoordinatorStartDate;
                newPunishment.MainCoordinatorEndDate = punishment.MainCoordinatorEndDate;
                newPunishment.UserDepStartDate = punishment.UserDepStartDate;
                newPunishment.UserDepEndDate = punishment.UserDepEndDate;

                Funs.DB.SubmitChanges();
            }
        }

        /// <summary>
        /// 根据主键删除扣款项
        /// </summary>
        /// <param name="punishmentId"></param>
        public static void DeletePunishmentById(string punishmentId)
        {
            Model.EMC_Punishment punishment = Funs.DB.EMC_Punishment.FirstOrDefault(e => e.PunishmentId == punishmentId);
            if (punishment != null)
            {
                Funs.DB.EMC_Punishment.DeleteOnSubmit(punishment);
                Funs.DB.SubmitChanges();
            }
        }

        public static bool IsExistSES_NO(string punishmentId, string sesNo,string flag)
        {
            bool isExist = false;
            var ses = Funs.DB.EMC_Punishment.FirstOrDefault(x => x.SES_No == sesNo && x.PunishmentId != punishmentId && x.Flag == flag);
            if (ses != null)
            {
                isExist = true;
            }
            return isExist;
        }
    }
}