declare @featureName varchar(100) = 'CheckForLO'
declare @moduleType varchar(100) = 'MedicalExpertiseClientModule'

if exists (
	select * from VCLib.T_BASE_FEATURE_SETTINGS
	where ClientModuleFeature in (
		select ClientModuleFeatureID from VCLib.T_CLIENT_MODULE_FEATURE cmf
		join VCLib.T_CLIENT_MODULES cm on cmf.ClientModules = cm.ClientModuleID
		where cmf.Name = @featureName and cm.ModuleType = moduleType
	)
)
begin
      INSERT INTO VCLib.T_DATAFORM_VALIDATION (Entities)
      SELECT (select e.EntityID from VCLib.T_ENTITIES e where e.[Schema] + '.' + e.EntName = 'IES.MEDICAL_REQUEST')
      where not exists
      (
      select *
      from VCLib.T_DATAFORM_VALIDATION dv
      join VCLib.T_ENTITIES e on e.EntityID = dv.Entities
      where e.[Schema] + '.' + e.EntName = 'IES.MEDICAL_REQUEST'
      );
      
      INSERT INTO VCLib.T_VALIDATION_CONDITION (ValidationConditionID, ValidationGroup, Condition, Context, ValidationDescription, IsHierarchical, DataformValidation, ValidationType, ServerSide)
      SELECT
      '99e69efe-1f5a-435a-ab4b-2db390f3693c'--ValidationConditionID, uniqueidentifier
      ,'Проверка Эксперта в зависимости от вида экспертизы и нарушений ЭКМП'--ValidationGroup, varchar(255)
      ,'<lx:Case xmlns:lx="lexem">
    <When>
        <lx:Criteria type="And">
            <lx:Criteria type="Or">
                <lx:Criterion type="=">
                    <lx:Path OwnerQueryLevel="0">
                        <lx:StepString name="F006ControlType" />
                        <lx:StepString name="ControlType" />
                    </lx:Path>
                    <lx:ChoiceOperand Entity="IES.F006_CONTROL_TYPE" AttributeName="ControlType" type="Int32" PrimaryKey="0" />
                </lx:Criterion>
                <lx:Criterion type="=">
                    <lx:Path OwnerQueryLevel="0">
                        <lx:StepString name="F006ControlType" />
                        <lx:StepString name="ControlType" />
                    </lx:Path>
                    <lx:ChoiceOperand Entity="IES.F006_CONTROL_TYPE" AttributeName="ControlType" type="Int32" PrimaryKey="1" />
                </lx:Criterion>
            </lx:Criteria>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="F042Expert" />
                </lx:Path>
                <lx:BaseObjectOperand Entity="IES.F042_TREKMP" isNull="1" />
            </lx:Criterion>
            <lx:CriterionChildExists>
                <condition>
                    <lx:Criterion type="=">
                        <lx:Path OwnerQueryLevel="0">
                            <lx:StepString name="Schet" />
                            <lx:StepString name="SchetType" />
                        </lx:Path>
                        <lx:ChoiceOperand Entity="IES.SCHET" AttributeName="SchetType" type="Int32" PrimaryKey="539" />
                    </lx:Criterion>
                </condition>
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="MedicalExpertiseSchets" />
                </lx:Path>
            </lx:CriterionChildExists>
        </lx:Criteria>
    </When>
    <Then>
        <lx:ConstOperand type="String"><![CDATA[Заполните поле Эксперт(F042) на заявке]]></lx:ConstOperand>
    </Then>
    <When>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="F006ControlType" />
                    <lx:StepString name="ControlType" />
                </lx:Path>
                <lx:ChoiceOperand Entity="IES.F006_CONTROL_TYPE" AttributeName="ControlType" type="Int32" PrimaryKey="1" />
            </lx:Criterion>
            <lx:CriterionChildExists>
                <condition>
                    <lx:Criteria type="And">
                        <lx:Criterion type="=">
                            <lx:Path OwnerQueryLevel="0">
                                <lx:StepString name="F042Trekmp" />
                            </lx:Path>
                            <lx:BaseObjectOperand Entity="IES.F042_TREKMP" isNull="1" />
                        </lx:Criterion>
                        <lx:Criterion type="=">
                            <lx:Path OwnerQueryLevel="1">
                                <lx:StepString name="Expert" />
                            </lx:Path>
                            <lx:BaseObjectOperand Entity="IES.F004_EXPERT" isNull="1" />
                        </lx:Criterion>
                    </lx:Criteria>
                </condition>
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="MedicalRequestItems" />
                </lx:Path>
            </lx:CriterionChildExists>
        </lx:Criteria>
    </When>
    <Then>
        <lx:ConstOperand type="String"><![CDATA[Заполните поле Эксперт на всех записях заявки]]></lx:ConstOperand>
    </Then>
    <When>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="F006ControlType" />
                    <lx:StepString name="ControlType" />
                </lx:Path>
                <lx:ChoiceOperand Entity="IES.F006_CONTROL_TYPE" AttributeName="ControlType" type="Int32" PrimaryKey="2" />
            </lx:Criterion>
            <lx:Criterion not="1" type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="MedicalExpertise" />
                </lx:Path>
                <lx:BaseObjectOperand Entity="IES.MEDICAL_EXPERTISE" isNull="1" />
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="MedicalExpertise" />
                    <lx:StepString name="IsReexpertise" />
                </lx:Path>
                <lx:ConstOperand type="Boolean">False</lx:ConstOperand>
            </lx:Criterion>
            <lx:CriterionChildExists>
                <condition>
                    <lx:Criteria type="And">
                        <lx:Criterion type="&gt;">
                            <lx:ChildCount>
                                <condition>
                                    <lx:Criterion type="=">
                                        <lx:Path OwnerQueryLevel="0">
                                            <lx:StepString name="MedicalRequestItem" />
                                            <lx:StepString name="MedicalRequestItemID" />
                                        </lx:Path>
                                        <lx:Path OwnerQueryLevel="1">
                                            <lx:StepString name="MedicalRequestItemID" />
                                        </lx:Path>
                                    </lx:Criterion>
                                </condition>
                                <lx:Path OwnerQueryLevel="0">
                                    <lx:StepString name="MedicalErrors" />
                                </lx:Path>
                                <lx:Count />
                            </lx:ChildCount>
                            <lx:ConstOperand type="Int32">0</lx:ConstOperand>
                        </lx:Criterion>
                        <lx:CriterionChildExists not="1">
                            <condition>
                                <lx:Criteria type="And">
                                    <lx:Criterion type="=">
                                        <lx:Path OwnerQueryLevel="0">
                                            <lx:StepString name="IsMainError" />
                                        </lx:Path>
                                        <lx:ConstOperand type="Boolean">True</lx:ConstOperand>
                                    </lx:Criterion>
                                    <lx:Criterion type="=">
                                        <lx:Path OwnerQueryLevel="0">
                                            <lx:StepString name="MedicalRequestItem" />
                                            <lx:StepString name="MedicalRequestItemID" />
                                        </lx:Path>
                                        <lx:Path OwnerQueryLevel="1">
                                            <lx:StepString name="MedicalRequestItemID" />
                                        </lx:Path>
                                    </lx:Criterion>
                                </lx:Criteria>
                            </condition>
                            <lx:Path OwnerQueryLevel="0">
                                <lx:StepString name="MedicalErrors" />
                            </lx:Path>
                        </lx:CriterionChildExists>
                    </lx:Criteria>
                </condition>
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="MedicalRequestItems" />
                </lx:Path>
            </lx:CriterionChildExists>
        </lx:Criteria>
    </When>
    <Then>
        <lx:ConstOperand type="String"><![CDATA[В одной из записей заявки имеется нарушения не отмеченные признаком удержания.]]></lx:ConstOperand>
    </Then>
    <Else>
        <lx:Null />
    </Else>
</lx:Case>'--Condition, varchar(max)
      ,'*'--Context, varchar(255)
      ,'Замена Опции клиентского модуля "Включить проверку вида экспертизы на отборе и поля Эксперт в заявке для ЛО"'--ValidationDescription, varchar(max)
      ,'1'--IsHierarchical, bit
      ,(select e.EntityID from VCLib.T_ENTITIES e where e.[Schema] + '.' + e.EntName = 'IES.MEDICAL_REQUEST')--DataformValidation, int
      ,'0'--ValidationType, int
      ,'1'--ServerSide, bit,
      where not exists
      (
      select *
      from VCLib.T_VALIDATION_CONDITION vc
      where vc.ValidationConditionID = '99e69efe-1f5a-435a-ab4b-2db390f3693c'
      );
      
end;

--удаление фичи
delete from VCLib.T_BASE_FEATURE_SETTINGS
where ClientModuleFeature in (
	select ClientModuleFeatureID from VCLib.T_CLIENT_MODULE_FEATURE cmf
	join VCLib.T_CLIENT_MODULES cm on cmf.ClientModules = cm.ClientModuleID
	where cmf.Name = @featureName and cm.ModuleType = moduleType
);

delete from VCLib.T_CLIENT_MODULE_FEATURE
where Name = @featureName and ClientModules in (
	select ClientModuleID from VCLib.T_CLIENT_MODULES
	where ModuleType = moduleType
);