ModuleServerFunctions.cs
9 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Core;
using Sungero.CoreEntities;
using Sungero.Domain.Shared;
namespace Sungero.ExchangeCore.Server
{
public class ModuleFunctions
{
/// <summary>
/// Получить роль "Пользователи с правами на работу через сервис обмена.
/// </summary>
/// <returns>Роль.</returns>
[Public]
public static IRole GetExchangeServiceUsersRole()
{
return Roles.GetAll(x => x.Sid == Constants.Module.RoleGuid.ExchangeServiceUsersRoleGuid).FirstOrDefault();
}
/// <summary>
/// Получить дату последней синхронизации ящиком в СО.
/// </summary>
/// <param name="box">Ящик.</param>
/// <returns>Дата последней синхронизации.</returns>
public static DateTime GetLastSyncDate(IBusinessUnitBox box)
{
var key = string.Format(Constants.Module.LastBoxSyncDate, box.Id);
var command = string.Format(Queries.Module.GetLastSyncDate, key);
try
{
var executionResult = Docflow.PublicFunctions.Module.ExecuteScalarSQLCommand(command);
var date = string.Empty;
if (!(executionResult is DBNull) && executionResult != null)
date = executionResult.ToString();
Logger.DebugFormat("Last box sync date in DB is {0} (UTC)", date);
DateTime result = DateTime.Parse(date, null, System.Globalization.DateTimeStyles.AdjustToUniversal);
return result;
}
catch (Exception ex)
{
Logger.DebugFormat("Error while getting box sync date", ex);
return Calendar.SqlMinValue;
}
}
/// <summary>
/// Обновить дату последней синхронизации ящика.
/// </summary>
/// <param name="notificationDate">Дата синхронизации ящика.</param>
/// <param name="box">Ящик.</param>
public static void UpdateLastSyncDate(DateTime notificationDate, IBusinessUnitBox box)
{
var key = string.Format(Constants.Module.LastBoxSyncDate, box.Id);
var newDate = notificationDate.ToString("yyyy-MM-ddTHH:mm:ss.ffff+0");
Docflow.PublicFunctions.Module.ExecuteSQLCommandFormat(Queries.Module.UpdateLastSyncDate, new[] { key, newDate });
Logger.DebugFormat("Last box sync date is set to {0} (UTC)", newDate);
}
/// <summary>
/// Запуск фонового процесса "Электронный обмен. Синхронизация контрагентов".
/// </summary>
[Public, Remote]
public static void RequeueCounterpartySync()
{
Jobs.SyncCounterparties.Enqueue();
}
/// <summary>
/// Запуск фонового процесса "Электронный обмен. Синхронизация абонентских ящиков".
/// </summary>
[Public, Remote]
public static void RequeueBoxSync()
{
Sungero.ExchangeCore.Jobs.SyncBoxes.Enqueue();
}
/// <summary>
/// Получить список организаций зарегистрированных в сервисах обмена по ИНН.
/// </summary>
/// <param name="tin">ИНН.</param>
/// <param name="trrc">КПП.</param>
/// <param name="boxes">Абонентские ящики.</param>
/// <returns>Список организаций в формате {Name}|{TIN}|{TRRC}|{BoxId}|{OrganizationId}|{ExchangeStatus}.</returns>
[Public, Remote(IsPure = true)]
public static List<string> FindOrganizationsInExchangeServices(string tin, string trrc, List<IBusinessUnitBox> boxes)
{
var result = new List<string>();
if (string.IsNullOrWhiteSpace(tin))
return result;
foreach (var box in boxes)
{
var client = Functions.BusinessUnitBox.GetClient(box);
var contacts = client.GetContacts();
var organizations = client.FindOrganizationsByInnKpp(tin, trrc);
if (organizations == null || !organizations.Any())
continue;
var organizationInfos = new List<string>();
foreach (var organization in organizations)
{
var contact = contacts.Where(c => c.Organization.OrganizationId == organization.OrganizationId).FirstOrDefault();
var exchangeStatus = string.Empty;
if (contact != null)
exchangeStatus = Functions.BusinessUnitBox.GetCounterpartyExchangeStatus(box, contact.Status).ToString();
var name = organization.Name;
if (organization.IsRoaming)
name += " " + BusinessUnitBoxes.Resources.ExchangeServiceNameFormat(organization.ExchangeServiceName);
var organizationFullInfo = string.Format("{0}|{1}|{2}|{3}|{4}|{5}", name, organization.Inn, organization.Kpp, box.Id, organization.OrganizationId, exchangeStatus);
result.Add(organizationFullInfo);
}
}
return result.Distinct().ToList();
}
/// <summary>
/// Данные для отчета полномочий сотрудника из модуля Электронный обмен.
/// </summary>
/// <param name="employee">Сотрудник для обработки.</param>
/// <returns>Данные для отчета.</returns>
[Public]
public virtual List<Company.Structures.ResponsibilitiesReport.ResponsibilitiesReportTableLine> GetResponsibilitiesReportData(Company.IEmployee employee)
{
var result = new List<Company.Structures.ResponsibilitiesReport.ResponsibilitiesReportTableLine>();
// HACK: Получаем отображаемое имя модуля.
// Dmitriev_IA: Данные из модуля ExchangeCode должны попасть в таблицу модуля Компания.
var companyModuleMetadata = Sungero.Metadata.Services.MetadataSearcher.FindModuleMetadata(Company.PublicConstants.Module.ModuleGuid);
var moduleName = companyModuleMetadata.GetDisplayName();
var modulePriority = Company.PublicConstants.ResponsibilitiesReport.ExchangePriority;
// Цифровые сертификаты.
if (Certificates.AccessRights.CanRead())
{
var certificateResponsibility = Company.Reports.Resources.ResponsibilitiesReport.CertificateResponsibility;
var certificates = Certificates.GetAll()
.Where(x => Equals(x.Owner, employee))
.Where(d => d.Enabled.HasValue && d.Enabled.Value)
.Where(d => !d.NotAfter.HasValue || d.NotAfter.Value > Calendar.Now);
result = Company.PublicFunctions.Module.AppendResponsibilitiesReportResult(result, certificates, moduleName, modulePriority,
certificateResponsibility, null);
}
// Ответственный за абонентские ящики наших организаций.
if (BoxBases.AccessRights.CanRead())
{
var boxResponsibility = Company.Reports.Resources.ResponsibilitiesReport.BoxResponsibility;
var boxes = BoxBases.GetAll()
.Where(x => Equals(x.Responsible, employee))
.Where(d => d.Status == Sungero.CoreEntities.DatabookEntry.Status.Active);
result = Company.PublicFunctions.Module.AppendResponsibilitiesReportResult(result, boxes, moduleName, modulePriority,
boxResponsibility, null);
}
return result;
}
[Public(WebApiRequestType = RequestType.Get)]
public string GetEncryptedData(string data)
{
return Sungero.ExchangeCore.Functions.BusinessUnitBox.GetEncryptedDataRemote(data);
}
/// <summary>
/// Получить список элементов очереди синхронизации сообщений.
/// </summary>
/// <param name="rootBox">Абонентский ящик.</param>
/// <returns>Список элементов очереди сообщений.</returns>
[Remote(IsPure = true)]
public virtual IQueryable<Sungero.ExchangeCore.IMessageQueueItem> GetMessageQueueItems(IBoxBase rootBox)
{
return MessageQueueItems.GetAll().Where(q => Equals(q.RootBox, rootBox));
}
/// <summary>
/// Получить список элементов очереди синхронизации сообщений по ид.
/// </summary>
/// <param name="queueItemIds">Ид элементов очереди синхронизации сообщений.</param>
/// <returns>Список элементов очереди синхронизации сообщений.</returns>
[Public]
public virtual IQueryable<Sungero.ExchangeCore.IMessageQueueItem> GetMessageQueueItems(string queueItemIds)
{
var splittedQueueItemIds = queueItemIds.Split(',')
.Select(int.Parse)
.ToList();
return MessageQueueItems.GetAll().Where(x => splittedQueueItemIds.Contains(x.Id));
}
}
}