public bool HaveSendedEmailForBill(int orderId)
        {
            IList <OrderDocumentEmail> result;

            using (var uow = UnitOfWorkFactory.CreateWithoutRoot($"[ES]Получение списка отправленных писем"))
            {
                OrderDocumentEmail orderDocumentEmailAlias = null;
                OrderDocument      orderDocumentAlias      = null;

                result = uow.Session.QueryOver <OrderDocumentEmail>(() => orderDocumentEmailAlias)
                         .JoinAlias(() => orderDocumentEmailAlias.OrderDocument, () => orderDocumentAlias)
                         .Where(() => orderDocumentAlias.Order.Id == orderId)
                         .JoinQueryOver(ode => ode.StoredEmail)
                         .Where(se => se.State != StoredEmailStates.SendingError &&
                                se.State != StoredEmailStates.Undelivered)
                         .WithSubquery.WhereExists(
                    QueryOver.Of <BillDocument>()
                    .Where(bd => bd.Id == orderDocumentEmailAlias.OrderDocument.Id)
                    .Select(bd => bd.Id))
                         .List();
            }

            return(result.Any());
        }
        public void ResendEmailWithErrorSendingStatus(DateTime date)
        {
            using (var uowLocal = UnitOfWorkFactory.CreateWithoutRoot())
            {
                var configuration = uowLocal.GetAll <InstanceMailingConfiguration>().FirstOrDefault();

                var dateCriterion = Projections.SqlFunction(
                    new SQLFunctionTemplate(
                        NHibernateUtil.Date,
                        "Date(?1)"
                        ),
                    NHibernateUtil.Date,
                    Projections.Property <StoredEmail>(x => x.SendDate)
                    );
                ICriterion dateResctict   = Restrictions.Eq(dateCriterion, date.Date);
                ICriterion dateResctictGe = Restrictions.Ge(dateCriterion, date.Date);

                #region OrderDocument

                OrderDocumentEmail orderDocumentEmailAlias      = null;
                OrderDocumentEmail orderDocumentEmailInnerAlias = null;
                OrderDocument      orderDocumentAlias           = null;
                OrderDocument      orderDocumentAliasInner      = null;

                var resendedOrderDocumentQuery = QueryOver.Of <OrderDocumentEmail>(() => orderDocumentEmailInnerAlias)
                                                 .JoinQueryOver(ode => ode.StoredEmail)
                                                 .Where(x => x.State != StoredEmailStates.SendingError)
                                                 .And(dateResctictGe)
                                                 .JoinAlias(() => orderDocumentEmailInnerAlias.OrderDocument, () => orderDocumentAliasInner)
                                                 .Where(() => orderDocumentAliasInner.Order.Id == orderDocumentAlias.Order.Id)
                                                 .Select(o => o.Id);

                var errorSendedOrderDocumentQuery = uowLocal.Session.QueryOver <OrderDocumentEmail>(() => orderDocumentEmailAlias)
                                                    .JoinQueryOver(ode => ode.StoredEmail)
                                                    .Where(se => se.State == StoredEmailStates.SendingError)
                                                    .And(dateResctict)
                                                    .JoinAlias(() => orderDocumentEmailAlias.OrderDocument, () => orderDocumentAlias)
                                                    .WithSubquery.WhereNotExists(resendedOrderDocumentQuery)
                                                    .Future();

                #endregion

                #region Order OrderWithoutShipmentForDebt

                OrderWithoutShipmentForDebtEmail orderWithoutShipmentForDebtEmailAlias      = null;
                OrderWithoutShipmentForDebtEmail orderWithoutShipmentForDebtEmailInnerAlias = null;
                OrderWithoutShipmentForDebt      orderWithoutShipmentForDebtAlias           = null;
                OrderWithoutShipmentForDebt      orderWithoutShipmentForDebtAliasInner      = null;

                var resendedOrderWithoutShipmentForDebtQuery = QueryOver.Of <OrderWithoutShipmentForDebtEmail>(() => orderWithoutShipmentForDebtEmailInnerAlias)
                                                               .JoinQueryOver(ode => ode.StoredEmail)
                                                               .Where(x => x.State != StoredEmailStates.SendingError)
                                                               .And(dateResctictGe)
                                                               .JoinAlias(() => orderWithoutShipmentForDebtEmailInnerAlias.OrderWithoutShipmentForDebt, () => orderWithoutShipmentForDebtAliasInner)
                                                               .Where(() => orderWithoutShipmentForDebtAliasInner.Id == orderWithoutShipmentForDebtAlias.Id)
                                                               .Select(o => o.Id);

                var errorSendedOrderWithoutShipmentForDebtEmailQuery = uowLocal.Session
                                                                       .QueryOver <OrderWithoutShipmentForDebtEmail>(() => orderWithoutShipmentForDebtEmailAlias)
                                                                       .JoinQueryOver(ode => ode.StoredEmail)
                                                                       .Where(se => se.State == StoredEmailStates.SendingError)
                                                                       .And(dateResctict)
                                                                       .JoinAlias(() => orderWithoutShipmentForDebtEmailAlias.OrderWithoutShipmentForDebt, () => orderWithoutShipmentForDebtAlias)
                                                                       .WithSubquery.WhereNotExists(resendedOrderWithoutShipmentForDebtQuery)
                                                                       .Future();

                #endregion

                #region Order OrderWithoutShipmentForAdvancePayment

                OrderWithoutShipmentForAdvancePaymentEmail orderWithoutShipmentForAdvancePaymentEmailAlias      = null;
                OrderWithoutShipmentForAdvancePaymentEmail orderWithoutShipmentForAdvancePaymentEmailInnerAlias = null;
                OrderWithoutShipmentForAdvancePayment      orderWithoutShipmentForAdvancePaymentAlias           = null;
                OrderWithoutShipmentForAdvancePayment      orderWithoutShipmentForAdvancePaymentAliasInner      = null;

                var resendedOrderWithoutShipmentForAdvancePaymentQuery = QueryOver.Of <OrderWithoutShipmentForAdvancePaymentEmail>(() => orderWithoutShipmentForAdvancePaymentEmailInnerAlias)
                                                                         .JoinQueryOver(ode => ode.StoredEmail)
                                                                         .Where(x => x.State != StoredEmailStates.SendingError)
                                                                         .And(dateResctictGe)
                                                                         .JoinAlias(() => orderWithoutShipmentForAdvancePaymentEmailInnerAlias.OrderWithoutShipmentForAdvancePayment, () => orderWithoutShipmentForAdvancePaymentAliasInner)
                                                                         .Where(() => orderWithoutShipmentForAdvancePaymentAliasInner.Id == orderWithoutShipmentForAdvancePaymentAlias.Id)
                                                                         .Select(o => o.Id);

                var errorSendedOrderWithoutShipmentForAdvancePaymentEmailQuery = uowLocal.Session
                                                                                 .QueryOver <OrderWithoutShipmentForAdvancePaymentEmail>(() => orderWithoutShipmentForAdvancePaymentEmailAlias)
                                                                                 .JoinQueryOver(ode => ode.StoredEmail)
                                                                                 .Where(se => se.State == StoredEmailStates.SendingError)
                                                                                 .And(dateResctict)
                                                                                 .JoinAlias(() => orderWithoutShipmentForAdvancePaymentEmailAlias.OrderWithoutShipmentForAdvancePayment, () => orderWithoutShipmentForAdvancePaymentAlias)
                                                                                 .WithSubquery.WhereNotExists(resendedOrderWithoutShipmentForAdvancePaymentQuery)
                                                                                 .Future();

                #endregion

                #region Order OrderWithoutShipmentForPayment

                OrderWithoutShipmentForPaymentEmail orderWithoutShipmentForPaymentEmailAlias      = null;
                OrderWithoutShipmentForPaymentEmail orderWithoutShipmentForPaymentEmailInnerAlias = null;
                OrderWithoutShipmentForPayment      orderWithoutShipmentForPaymentAlias           = null;
                OrderWithoutShipmentForPayment      orderWithoutShipmentForPaymentAliasInner      = null;

                var resendedOrderWithoutShipmentForPaymentQuery = QueryOver.Of <OrderWithoutShipmentForPaymentEmail>(() => orderWithoutShipmentForPaymentEmailInnerAlias)
                                                                  .JoinQueryOver(ode => ode.StoredEmail)
                                                                  .Where(x => x.State != StoredEmailStates.SendingError)
                                                                  .And(dateResctictGe)
                                                                  .JoinAlias(() => orderWithoutShipmentForPaymentEmailInnerAlias.OrderWithoutShipmentForPayment, () => orderWithoutShipmentForPaymentAliasInner)
                                                                  .Where(() => orderWithoutShipmentForPaymentAliasInner.Id == orderWithoutShipmentForPaymentAlias.Id)
                                                                  .Select(o => o.Id);

                var errorSendedOrderWithoutShipmentForPaymentEmailQuery = uowLocal.Session
                                                                          .QueryOver <OrderWithoutShipmentForPaymentEmail>(() => orderWithoutShipmentForPaymentEmailAlias)
                                                                          .JoinQueryOver(ode => ode.StoredEmail)
                                                                          .Where(se => se.State == StoredEmailStates.SendingError)
                                                                          .And(dateResctict)
                                                                          .JoinAlias(() => orderWithoutShipmentForPaymentEmailAlias.OrderWithoutShipmentForPayment, () => orderWithoutShipmentForPaymentAlias)
                                                                          .WithSubquery.WhereNotExists(resendedOrderWithoutShipmentForPaymentQuery)
                                                                          .Future();

                #endregion

                var errorSendedCounterpartyEmails = errorSendedOrderDocumentQuery
                                                    .Union <CounterpartyEmail>(errorSendedOrderWithoutShipmentForDebtEmailQuery)
                                                    .Union <CounterpartyEmail>(errorSendedOrderWithoutShipmentForAdvancePaymentEmailQuery)
                                                    .Union <CounterpartyEmail>(errorSendedOrderWithoutShipmentForPaymentEmailQuery);

                var errorSendedCounterpartyEmailsList = errorSendedCounterpartyEmails.ToList();

                foreach (var sendedEmail in errorSendedCounterpartyEmailsList)
                {
                    try
                    {
                        using (var unitOfWork = UnitOfWorkFactory.CreateWithoutRoot("StoredEmail"))
                        {
                            var storedEmail = new StoredEmail
                            {
                                State            = StoredEmailStates.PreparingToSend,
                                Author           = sendedEmail.StoredEmail.Author,
                                ManualSending    = true,
                                SendDate         = DateTime.Now,
                                StateChangeDate  = DateTime.Now,
                                Subject          = sendedEmail.StoredEmail.Subject,
                                RecipientAddress = sendedEmail.StoredEmail.RecipientAddress
                            };

                            unitOfWork.Save(storedEmail);

                            switch (sendedEmail.Type)
                            {
                            case CounterpartyEmailType.OrderDocument:
                                var orderDocumentEmail = new OrderDocumentEmail
                                {
                                    StoredEmail   = storedEmail,
                                    Counterparty  = sendedEmail.Counterparty,
                                    OrderDocument = ((OrderDocumentEmail)sendedEmail).OrderDocument
                                };

                                unitOfWork.Save(orderDocumentEmail);

                                break;

                            case CounterpartyEmailType.OrderWithoutShipmentForDebt:
                                var orderWithoutShipmentForDebtEmail = new OrderWithoutShipmentForDebtEmail()
                                {
                                    StoredEmail  = storedEmail,
                                    Counterparty = sendedEmail.Counterparty,
                                    OrderWithoutShipmentForDebt = (OrderWithoutShipmentForDebt)sendedEmail.EmailableDocument
                                };

                                unitOfWork.Save(orderWithoutShipmentForDebtEmail);

                                break;

                            case CounterpartyEmailType.OrderWithoutShipmentForAdvancePayment:
                                var orderWithoutShipmentForAdvancePaymentEmail = new OrderWithoutShipmentForAdvancePaymentEmail()
                                {
                                    StoredEmail  = storedEmail,
                                    Counterparty = sendedEmail.Counterparty,
                                    OrderWithoutShipmentForAdvancePayment = (OrderWithoutShipmentForAdvancePayment)sendedEmail.EmailableDocument
                                };

                                unitOfWork.Save(orderWithoutShipmentForAdvancePaymentEmail);

                                break;

                            case CounterpartyEmailType.OrderWithoutShipmentForPayment:
                                var orderWithoutShipmentForPaymentEmail = new OrderWithoutShipmentForPaymentEmail()
                                {
                                    StoredEmail  = storedEmail,
                                    Counterparty = sendedEmail.Counterparty,
                                    OrderWithoutShipmentForPayment = (OrderWithoutShipmentForPayment)sendedEmail.EmailableDocument
                                };

                                unitOfWork.Save(orderWithoutShipmentForPaymentEmail);

                                break;
                            }

                            unitOfWork.Commit();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Ошибка отправки { sendedEmail.Id } : { e.Message }");
                    }
                }
            }
        }