SelectionRuleServerFunctions.cs 4.37 KB
using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Core;
using Sungero.CoreEntities;
using DirRX.Container.SelectionRule;

namespace DirRX.Container.Server
{
  partial class SelectionRuleFunctions
  {
    /// <summary>
    /// Получить список документов для обработки по указанному правилу.
    /// </summary>
    /// <returns>Список документов для обработки.</returns>
    [Public,Remote(IsPure=true)]
    public virtual IQueryable<Sungero.Docflow.IOfficialDocument> GetDocumentsByRule()
    {
      // Выбрать все не обработанные документы.
      var processedIds = Container.TransferEventLogs.GetAll(x => x.CurrentState != null).Select(x => x.Document.Id).ToList();
      var query = Sungero.Docflow.OfficialDocuments.GetAll(d => !Storage.ArchiveDocumentBases.Is(d) && d.HasVersions && !processedIds.Contains(d.Id));

      // Отфильтровать по видам документов, подразделениям, делам.
      if (_obj.DocumentKinds.Any())
      {
        var documentKindIds = _obj.DocumentKinds.Select(x => x.DocumentKind.Id).ToList();
        query = query.Where(d => d.DocumentKind != null && documentKindIds.Contains(d.DocumentKind.Id));
      }
      
      if (_obj.Departments.Any())
      {
        var departmentIds = _obj.Departments.Select(x => x.Department.Id).ToList();
        query = query.Where(d => d.Department != null && departmentIds.Contains(d.Department.Id));
      }
      
      if (_obj.CaseFiles.Any())
      {
        var caseFileIds = _obj.CaseFiles.Select(x => x.CaseFile.Id).ToList();
        query = query.Where(d => d.CaseFile != null && caseFileIds.Contains(d.CaseFile.Id));
      }
      
      // Дополнительно отфильтровать с помощью функции.
      if (!string.IsNullOrEmpty(_obj.FilteringModule) && !string.IsNullOrEmpty(_obj.FilteringFunction))
        query = (IQueryable<Sungero.Docflow.IOfficialDocument>)Functions.Module.ExecuteServerFunction(_obj.FilteringModule, _obj.FilteringFunction, new object[] { query });
      
      return query;
    }

    /// <summary>
    /// Обработать документ по указанному правилу.
    /// </summary>
    /// <param name="document">Документ.</param>
    /// <returns>Запись справочника Информация об архивном документе.</returns>
    [Public]
    public virtual Container.ITransferEventLog EnqueueDocumentByRule(Sungero.Docflow.IOfficialDocument document)
    {
      // Проверить не был ли документ обработан ранее.
      try
      {
        var eventLog = Container.TransferEventLogs.GetAll(x => x.Document.Equals(document)).FirstOrDefault();       

        // Создать запись протокола обработки или использовать существующую если статус сброшен.
        if (eventLog == null)
        {
          eventLog = Container.TransferEventLogs.Create();
          eventLog.Document = document;
        }
        else if (eventLog.CurrentState != null)
        {
          Functions.TransferEventLog.AddProcessingLog(eventLog, Resources.ActionReprocess, Resources.ActionReprocess);
          eventLog.Save();
          Logger.DebugFormat("EnqueueDocumentByRule. Документ ИД {0} уже обрабатывается. Протокол ИД {1}.", document.Id, eventLog.Id);
          return Container.TransferEventLogs.Null;
        }
        
        eventLog.SelectionRule = _obj;
        eventLog.CurrentState = Container.TransferEventLog.CurrentState.InQueue;
        eventLog.Save();
        
        // Отправить в обработку.
        var asyncHandler = DirRX.Container.AsyncHandlers.ProcessArchiveDocument.Create();
        asyncHandler.eventLogId = eventLog.Id;
        asyncHandler.ruleId = _obj.Id;
        asyncHandler.ExecuteAsync();
        
        return eventLog;
      }
      catch (Exception e)
      {
        var error = e.InnerException != null ? e.InnerException.Message : e.Message;
        Logger.DebugFormat("EnqueueDocumentForInventory. Документ ИД {0}. Ошибка при отправке в обработку {1}", document.Id, error);
        return Container.TransferEventLogs.Null;
      }    
    }    
  }
}