<?xml version="1.0" encoding="utf-8"?>
<Entity 
Schema="AKUZ" 
Name="REHAB_REGISTRY" 
Description="Регистр реабилитации" 
PrimaryKey="RehabRegistryID">
  <Fields>
    <Attribute Name="RehabRegistryID" Type="guid" Description="Идентификатор" />
    <Parent Name="Patient" Type="AKUZ.PATIENT" Description="Пациент" />
    <Attribute Name="DeregisterDate" Type="datetime" Description="Дата исключения из регистра" />
    <Attribute Name="TakeReason" Type="string" Description="Причина включения в регистр" CharacterMaximumLength="1024" />
    <Parent Name="RegisterPost" Type="AKUZ.POST" Description="Пользователь включивший в регистр" />
    <Attribute Name="RegisterDate" Type="datetime" Description="Дата включения в регистр" />
    <Parent Name="DeregisterPost" Type="AKUZ.POST" Description="Пользователь исключивший из регистра" />
    <Attribute Name="DeregisterReason" Type="string" Description="Причина исключения из регистра" CharacterMaximumLength="1024" />
    <Child Name="RehabCourses" Type="AKUZ.REHAB_COURSE" Description="Курсы реабилитации" ChildAttribute="RehabRegistry" />
    <Child Name="RehabFiles" Type="AKUZ.REHAB_FILE" Description="Прикрепленные документы для реабилитации" ChildAttribute="RehabRegistry" />
    <Attribute Name="Status" Type="string" Description="Статус" />
  </Fields>
  <DbMapping>
    <Table name="T_REHAB_REGISTRY">
      <Map Name="RehabRegistryID" Column="RehabRegistryID" />
      <Map Name="Patient" Column="Patient" />
      <Map Name="DeregisterDate" Column="DeregisterDate" />
      <Map Name="TakeReason" Column="TakeReason" />
      <Map Name="RegisterPost" Column="RegisterPost" />
      <Map Name="RegisterDate" Column="RegisterDate" />
      <Map Name="DeregisterPost" Column="DeregisterPost" />
      <Map Name="DeregisterReason" Column="DeregisterReason" />
    </Table>
    <Lexem>
      <Map Name="Status">
        <lx:Case xmlns:lx="lexem">
          <When>
            <lx:CriterionChildExists not="1">
              <condition />
              <lx:Path OwnerQueryLevel="0">
                <lx:StepString name="RehabCourses" />
              </lx:Path>
            </lx:CriterionChildExists>
          </When>
          <Then>
            <lx:ConstOperand type="String"><![CDATA[Нет курсов реабилитации]]></lx:ConstOperand>
          </Then>
          <Else>
            <lx:QueryValue entity="AKUZ.REHAB_COURSE" top="1" offset="0">
              <condition>
                <lx:Criterion type="=">
                  <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="RehabRegistry" />
                    <lx:StepString name="RehabRegistryID" />
                  </lx:Path>
                  <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="RehabRegistryID" />
                  </lx:Path>
                </lx:Criterion>
              </condition>
              <orderBy>
                <lx:Path OwnerQueryLevel="0">
                  <lx:StepString name="CourseOpeningDate" />
                </lx:Path>
                <desc>True</desc>
              </orderBy>
              <value>
                <lx:Case>
                  <When>
                    <lx:Criterion not="1" type="=">
                      <lx:Path OwnerQueryLevel="0">
                        <lx:StepString name="EventRehabilitationCard" />
                      </lx:Path>
                      <lx:BaseObjectOperand Entity="AKUZ.EVENT_REHABILITATION_CARD" isNull="1" />
                    </lx:Criterion>
                  </When>
                  <Then>
                    <lx:Case>
                      <When>
                        <lx:Criterion not="1" type="=">
                          <lx:Path OwnerQueryLevel="0">
                            <lx:StepString name="EventRehabilitationCard" />
                            <lx:StepString name="EventBase" />
                            <lx:StepString name="CloseDate" />
                          </lx:Path>
                          <lx:ConstOperand isNull="1" />
                        </lx:Criterion>
                      </When>
                      <Then>
                        <lx:ConstOperand type="String"><![CDATA[Проходит курс реабилитации]]></lx:ConstOperand>
                      </Then>
                      <Else>
                        <lx:ConstOperand type="String"><![CDATA[Завершил курс реабилитации]]></lx:ConstOperand>
                      </Else>
                    </lx:Case>
                  </Then>
                  <Else>
                    <lx:Case>
                      <When>
                        <lx:CriterionChildExists>
                          <condition>
                            <lx:Criterion type="=">
                              <lx:Path OwnerQueryLevel="0">
                                <lx:StepString name="isResult" />
                              </lx:Path>
                              <lx:ConstOperand type="Boolean">True</lx:ConstOperand>
                            </lx:Criterion>
                          </condition>
                          <lx:Path OwnerQueryLevel="0">
                            <lx:StepString name="RehabFiles" />
                          </lx:Path>
                        </lx:CriterionChildExists>
                      </When>
                      <Then>
                        <lx:ConstOperand type="String"><![CDATA[Завершил курс]]></lx:ConstOperand>
                      </Then>
                      <Else>
                        <lx:Case>
                          <When>
                            <lx:Criterion not="1" type="=">
                              <lx:Path OwnerQueryLevel="0">
                                <lx:StepString name="Refferal" />
                                <lx:StepAs as="AKUZ.REFERRAL" />
                                <lx:StepAs as="AKUZ.HOSPITALIZATION_REFERRAL" />
                                <lx:StepString name="EmergencyEvent" />
                              </lx:Path>
                              <lx:BaseObjectOperand Entity="AKUZ.EMERGENCY_EVENT" isNull="1" />
                            </lx:Criterion>
                          </When>
                          <Then>
                            <lx:ConstOperand type="String"><![CDATA[Госпитализирован]]></lx:ConstOperand>
                          </Then>
                          <Else>
                            <lx:Case>
                              <When>
                                <lx:Criterion not="1" type="=">
                                  <lx:Path OwnerQueryLevel="0">
                                    <lx:StepString name="Refferal" />
                                    <lx:StepString name="ConsiliumDecisionLink" />
                                    <lx:StepAs as="AKUZ.REHABILITATION_CONSILIUM_DECISION" />
                                  </lx:Path>
                                  <lx:BaseObjectOperand Entity="AKUZ.CONSILIUM_DECISION" isNull="1" />
                                </lx:Criterion>
                              </When>
                              <Then>
                                <lx:Case>
                                  <When>
                                    <lx:Criterion not="1" type="=">
                                      <lx:Path OwnerQueryLevel="0">
                                        <lx:StepString name="Refferal" />
                                        <lx:StepString name="ConsiliumDecisionLink" />
                                        <lx:StepAs as="AKUZ.REHABILITATION_CONSILIUM_DECISION" />
                                        <lx:StepString name="ConsiliumDecision" />
                                      </lx:Path>
                                      <lx:ChoiceOperand Entity="AKUZ.REHABILITATION_CONSILIUM_DECISION" AttributeName="ConsiliumDecision" isNull="1" />
                                    </lx:Criterion>
                                  </When>
                                  <Then>
                                    <lx:Case>
                                      <When>
                                        <lx:Criterion type="=">
                                          <lx:Path OwnerQueryLevel="0">
                                            <lx:StepString name="Refferal" />
                                            <lx:StepString name="ConsiliumDecisionLink" />
                                            <lx:StepAs as="AKUZ.REHABILITATION_CONSILIUM_DECISION" />
                                            <lx:StepString name="ConsiliumDecision" />
                                          </lx:Path>
                                          <lx:ChoiceOperand Entity="AKUZ.REHABILITATION_CONSILIUM_DECISION" AttributeName="ConsiliumDecision" type="Int32" PrimaryKey="0" />
                                        </lx:Criterion>
                                      </When>
                                      <Then>
                                        <lx:ConstOperand type="String"><![CDATA[Подтверждено консилиумом]]></lx:ConstOperand>
                                      </Then>
                                      <Else>
                                        <lx:ConstOperand type="String"><![CDATA[Ожидает отмены направления]]></lx:ConstOperand>
                                      </Else>
                                    </lx:Case>
                                  </Then>
                                  <Else>
                                    <lx:Case>
                                      <When>
                                        <lx:Criterion not="1" type="=">
                                          <lx:Path OwnerQueryLevel="0">
                                            <lx:StepString name="Refferal" />
                                            <lx:StepAs as="AKUZ.REFERRAL" />
                                            <lx:StepAs as="AKUZ.HOSPITALIZATION_REFERRAL" />
                                            <lx:StepString name="CancelHospitalizationReferral" />
                                          </lx:Path>
                                          <lx:BaseObjectOperand Entity="AKUZ.CANCEL_HOSPITALIZATION_REFERRAL" isNull="1" />
                                        </lx:Criterion>
                                      </When>
                                      <Then>
                                        <lx:ConstOperand type="String"><![CDATA[Отменено]]></lx:ConstOperand>
                                      </Then>
                                      <When>
                                        <lx:Criterion type="=">
                                          <lx:Path OwnerQueryLevel="0">
                                            <lx:StepString name="Refferal" />
                                            <lx:StepAs as="AKUZ.REFERRAL" />
                                            <lx:StepAs as="AKUZ.HOSPITALIZATION_REFERRAL" />
                                            <lx:StepString name="NeedConsiliumSign" />
                                          </lx:Path>
                                          <lx:ConstOperand type="Boolean">True</lx:ConstOperand>
                                        </lx:Criterion>
                                      </When>
                                      <Then>
                                        <lx:ConstOperand type="String"><![CDATA[Ожидает рассмотрения]]></lx:ConstOperand>
                                      </Then>
                                      <When>
                                        <lx:Criterion not="1" type="=">
                                          <lx:Path OwnerQueryLevel="0">
                                            <lx:StepString name="Refferal" />
                                            <lx:StepAs as="AKUZ.REFERRAL" />
                                            <lx:StepAs as="AKUZ.HOSPITALIZATION_REFERRAL" />
                                            <lx:StepString name="EmergencyEvent" />
                                          </lx:Path>
                                          <lx:BaseObjectOperand Entity="AKUZ.EMERGENCY_EVENT" isNull="1" />
                                        </lx:Criterion>
                                      </When>
                                      <Then>
                                        <lx:ConstOperand type="String"><![CDATA[Госпитализирован]]></lx:ConstOperand>
                                      </Then>
                                      <When>
                                        <lx:Criterion type="=">
                                          <lx:Path OwnerQueryLevel="0">
                                            <lx:StepString name="Refferal" />
                                            <lx:StepAs as="AKUZ.REFERRAL" />
                                            <lx:StepAs as="AKUZ.HOSPITALIZATION_REFERRAL" />
                                            <lx:StepString name="NeedConsiliumSign" />
                                          </lx:Path>
                                          <lx:ConstOperand type="Boolean">False</lx:ConstOperand>
                                        </lx:Criterion>
                                      </When>
                                      <Then>
                                        <lx:ConstOperand type="String"><![CDATA[Ожидает госпитализации]]></lx:ConstOperand>
                                      </Then>
                                      <Else>
                                        <lx:ConstOperand type="String"><![CDATA[---]]></lx:ConstOperand>
                                      </Else>
                                    </lx:Case>
                                  </Else>
                                </lx:Case>
                              </Then>
                              <Else>
                                <lx:ConstOperand type="String"><![CDATA[Ожидает рассмотрения]]></lx:ConstOperand>
                              </Else>
                            </lx:Case>
                          </Else>
                        </lx:Case>
                      </Else>
                    </lx:Case>
                  </Else>
                </lx:Case>
              </value>
            </lx:QueryValue>
          </Else>
        </lx:Case>
      </Map>
    </Lexem>
  </DbMapping>
</Entity>