SelectionRuleServerFunctions.cs
4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;
}
}
}
}