convert_after_postgres.sql
3.03 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
DO $$
-- Дискриминатор свойства-коллекции адресатов входящего письма (9EE5DA19-7A73-4C27-A743-EB68981C6EC2).
declare addresseesDiscriminator uuid = '9EE5DA19-7A73-4C27-A743-EB68981C6EC2';
declare addresseesNewId int;
declare documentToConvertCount int;
declare InAddresseesTableLastId int;
begin
-- Установить свойство IsManyAddresses в False.
update sungero_content_edoc
set inismanyaddr_docflow_sungero = 'false'
where discriminator = '8DD00491-8FD0-4A7A-9CF3-8B6DC2E6455D'
and inismanyaddr_docflow_sungero is null;
-- Синхронизировать единичного адресата в коллекцию адресатов.
if exists(select 1 from INFORMATION_SCHEMA.TABLES where table_name = 'sungero_docflow_inaddressees')
then
if exists(select * from information_schema.tables where table_name = 'temp_addresseestable')
then
drop table temp_addresseestable;
end if;
create table temp_addresseestable
(
Id serial,
DocId int,
AddresseeId int,
DepartmentId int
);
insert into temp_addresseestable (DocId, AddresseeId, DepartmentId)
select distinct
doc.id,
doc.inaddressee_docflow_sungero,
emp.department_company_sungero
from sungero_content_edoc as doc
left join sungero_docflow_inaddressees addr on doc.id = addr.edoc
left join sungero_core_recipient emp on doc.inaddressee_docflow_sungero = emp.id
where doc.discriminator = '8DD00491-8FD0-4A7A-9CF3-8B6DC2E6455D'
and doc.inaddressee_docflow_sungero is not null
and addr.edoc is null
order by doc.id;
-- Подсчет количества новых элементов.
select COUNT(DocId) into documentToConvertCount from temp_addresseestable;
-- Получение последнего ИД в таблице для свойства-коллекции адресатов входящего письма.
InAddresseesTableLastId := (select lastid from sungero_system_ids where tablename = 'sungero_docflow_inaddressees');
-- Резервирование id в таблице.
addresseesNewId := (select sungero_system_GetNewId('sungero_docflow_inaddressees', documentToConvertCount));
insert into sungero_docflow_inaddressees
(
id,
discriminator,
edoc,
number,
addressee,
department
)
select
InAddresseesTableLastId + t.Id,
addresseesDiscriminator,
t.DocId,
1,
t.AddresseeId,
t.DepartmentId
from temp_addresseestable as t;
drop table temp_addresseestable;
end if;
-- Заполнить ManyAddressesLabel у одноадресных входящих писем.
update sungero_content_edoc as doc
set manyaddrlabel_docflow_sungero = recip.name
from sungero_core_recipient as recip
where doc.inaddressee_docflow_sungero = recip.Id
and doc.inismanyaddr_docflow_sungero = false
and doc.inaddressee_docflow_sungero is not null;
-- Установить свойство IsAutoExecLeadingActionItem в False.
update sungero_docflow_personsetting
set ExecLeadAI = 'false'
where ExecLeadAI is null;
end $$;