-- Удаление записей из связанных таблиц по условию
DELETE FROM VCLib.T_OBJECT_MODIFICATION_ACTION_PARAM p
USING VCLib.T_OBJECT_MODIFICATION_ACTION a
WHERE p.ObjectModificationAction = a.ObjectModificationActionID
  AND a.Name = 'sp_GetSetNosologyList';

DELETE FROM VCLib.T_DEPENDENCY_MODIFICATION_ACTION d
USING VCLib.T_OBJECT_MODIFICATION_ACTION a
WHERE d.ObjectModificationActionID = a.ObjectModificationActionID
  AND a.Name = 'sp_GetSetNosologyList';

DELETE FROM vclib.T_SINGLE_MODIFICATION_ACTION s
USING VCLib.T_OBJECT_MODIFICATION_ACTION a
WHERE s.ObjectModificationActionID = a.ObjectModificationActionID
  AND a.Name = 'sp_GetSetNosologyList';

DELETE FROM VCLib.T_OBJECT_MODIFICATION_ACTION a
WHERE a.Name = 'sp_GetSetNosologyList';


-- Вставка новых записей в T_OBJECT_MODIFICATION_ACTION
INSERT INTO VCLib.T_OBJECT_MODIFICATION_ACTION (
    ObjectModificationActionID,
    Entities,
    Position,
    type_,
    Active,
    Name,
    Instruction,
    InstructionType
)
VALUES
    (
        '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C',
        (SELECT EntityID FROM VCLib.T_ENTITIES WHERE EntName = 'NOSOLOGY' LIMIT 1),
        1,
        (SELECT EntityID FROM VCLib.T_ENTITIES WHERE EntName = 'DEPENDENCY_MODIFICATION_ACTION' LIMIT 1),
        TRUE,
        'sp_GetSetNosologyList',
        'sp_GetSetNosologyList',
        0
    ),
    (
        'CE34CC0F-1226-4122-B55B-9335237559C2',
        (SELECT EntityID FROM VCLib.T_ENTITIES WHERE EntName = 'NOSOLOGY' LIMIT 1),
        1,
        (SELECT EntityID FROM VCLib.T_ENTITIES WHERE EntName = 'SINGLE_MODIFICATION_ACTION' LIMIT 1),
        TRUE,
        'sp_GetSetNosologyList',
        'sp_GetSetNosologyList',
        0
    );


-- Вставка в T_DEPENDENCY_MODIFICATION_ACTION
INSERT INTO VCLib.T_DEPENDENCY_MODIFICATION_ACTION (ObjectModificationActionID, Dependencies)
VALUES
    ('1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', '{NosologyBenefitss}');


-- Вставка в T_SINGLE_MODIFICATION_ACTION
INSERT INTO VCLib.T_SINGLE_MODIFICATION_ACTION (
    ObjectModificationActionID,
    InsertFilter,
    UpdateFilter,
    DeleteFilter,
    EditType
)
VALUES
    (
        'CE34CC0F-1226-4122-B55B-9335237559C2',
        NULL,
        $xml$
<lx:Criteria type="And" xmlns:lx="lexem">
    <lx:CriterionChildExists>
        <condition />
        <lx:Path OwnerQueryLevel="0">
            <lx:StepString name="NosologyBenefitss" />
        </lx:Path>
    </lx:CriterionChildExists>
    <lx:CriterionChildExists not="1">
        <condition>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="ObjectProperties" />
                    <lx:StepString name="Modified" />
                </lx:Path>
                <lx:ConstOperand type="Boolean">True</lx:ConstOperand>
            </lx:Criterion>
        </condition>
        <lx:Path OwnerQueryLevel="0">
            <lx:StepString name="NosologyBenefitss" />
        </lx:Path>
    </lx:CriterionChildExists>
</lx:Criteria>
$xml$,
        NULL,
        4
    );


-- Вставка в T_OBJECT_MODIFICATION_ACTION_PARAM
INSERT INTO VCLib.T_OBJECT_MODIFICATION_ACTION_PARAM (
    ObjectModificationActionParamID,
    ObjectModificationAction,
    ParamValue,
    Name
)
VALUES
    ('DBFD3513-331C-496C-AB53-025D3AAB53CA', '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="FatherName" />
    </lx:Path>$xml$, 'FatherName'),
    ('A5E79579-6B58-450F-9BDC-3D47D72B5846', '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="ObjectProperties" />
        <lx:StepString name="IsNew" />
    </lx:Path>$xml$, 'IsNew'),
    ('DA02D37C-C3E4-46AE-91C2-7F32997653BE', '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="SNILS" />
    </lx:Path>$xml$, 'SNILS'),
    ('F9748750-4A8B-42A6-82C6-B2CF5F7CF772', '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="LastName" />
    </lx:Path>$xml$, 'LastName'),
    ('2555C5AF-94BC-43F6-B382-C57CB033B4EE', '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="PAC_ID" />
    </lx:Path>$xml$, 'PacId'),
    ('514F3E9C-6764-4064-B17D-CA7CC2904A31', '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="NosologyID" />
    </lx:Path>$xml$, 'ID'),
    ('B27C7208-4DC2-460C-A79A-EC7D14378909', '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="DocSeries" />
    </lx:Path>$xml$, 'DocSeries'),
    ('56E11174-FA77-493C-9E4F-F8B6FF8DCAFD', '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="FirstName" />
    </lx:Path>$xml$, 'FirstName'),
    ('47EC6104-4C77-481C-9EFF-FA7658ECC4F4', '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="DocNumber" />
    </lx:Path>$xml$, 'DocNumber'),
    ('BE6D8900-3311-43B1-A6C5-FF01D4BD06DA', '1E94CDEB-A8C8-4371-AB4F-12BC401ED74C', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="Birthday" />
    </lx:Path>$xml$, 'Birthday'),
    ('760192DC-9066-47D2-9BAB-3042DB510067', 'CE34CC0F-1226-4122-B55B-9335237559C2', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="LastName" />
    </lx:Path>$xml$, 'LastName'),
    ('029671C8-709B-41B4-931B-32253F1A6F1A', 'CE34CC0F-1226-4122-B55B-9335237559C2', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="FatherName" />
    </lx:Path>$xml$, 'FatherName'),
    ('DD286FE1-50E0-4916-8DAF-7DD34DD2956F', 'CE34CC0F-1226-4122-B55B-9335237559C2', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="DocNumber" />
    </lx:Path>$xml$, 'DocNumber'),
    ('6F7D396D-F74A-4252-9E3B-8B1141F77E0D', 'CE34CC0F-1226-4122-B55B-9335237559C2', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="FirstName" />
    </lx:Path>$xml$, 'FirstName'),
    ('5C515720-EA45-4254-8059-8DA1BE55CEB2', 'CE34CC0F-1226-4122-B55B-9335237559C2', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="DocSeries" />
    </lx:Path>$xml$, 'DocSeries'),
    ('6951C779-8659-434F-A5D1-B5E883E96494', 'CE34CC0F-1226-4122-B55B-9335237559C2', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="NosologyID" />
    </lx:Path>$xml$, 'ID'),
    ('EEB076F7-0F83-4E42-AE4C-BDEBDBFFF4E0', 'CE34CC0F-1226-4122-B55B-9335237559C2', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="PAC_ID" />
    </lx:Path>$xml$, 'PacId'),
    ('9A61669A-BDDB-4F5B-97E7-C10DD92F2464', 'CE34CC0F-1226-4122-B55B-9335237559C2', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="ObjectProperties" />
        <lx:StepString name="IsNew" />
    </lx:Path>$xml$, 'IsNew'),
    ('A779EBBF-665A-462A-A2D7-EC4F909B9AA1', 'CE34CC0F-1226-4122-B55B-9335237559C2', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="SNILS" />
    </lx:Path>$xml$, 'SNILS'),
    ('2446388A-7582-4900-A953-FD387DABA1D0', 'CE34CC0F-1226-4122-B55B-9335237559C2', 
    $xml$<lx:Path OwnerQueryLevel="0" xmlns:lx="lexem">
        <lx:StepString name="Birthday" />
    </lx:Path>$xml$, 'Birthday');
