<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyRiskDisclosureStatementMapper">

    <select id="selectPageWithElderInfo"
            resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.riskdisclosurestatement.ElderlyRiskDisclosureStatementRespVO">
        SELECT
            erds.*,
            ei.elder_name AS elderName,
            ei.elder_sex AS elderSex,
            ei.elder_age AS elderAge,
            COALESCE(obf.floor_name, ei.floor_name) AS floorName,
            COALESCE(CONCAT_WS('-', ob.build_name, obf.floor_name, obr.room_name, obb.bed_name), ei.bed_name) AS bedName,
            ec_latest.contract_number AS contractNumber,
            ei.nurse_level_name AS nurseLevelName
        FROM elderly_risk_disclosure_statement erds
        LEFT JOIN elderly_info ei ON ei.id = erds.elder_id
        LEFT JOIN (
            SELECT
                ec.*
            FROM elderly_contract ec
            INNER JOIN (
                SELECT
                    elder_id,
                    tenant_id,
                    MAX(created_time) AS max_created_time
                FROM elderly_contract
                WHERE status = 1
                GROUP BY elder_id, tenant_id
            ) t ON t.elder_id = ec.elder_id
                AND t.tenant_id = ec.tenant_id
                AND t.max_created_time = ec.created_time
        ) ec_latest ON ec_latest.elder_id = erds.elder_id AND ec_latest.tenant_id = erds.tenant_id
        LEFT JOIN org_build_bed obb ON obb.id = ei.bed_id AND obb.tenant_id = erds.tenant_id
        LEFT JOIN org_build_room obr ON obr.id = obb.room_id AND obr.tenant_id = obb.tenant_id
        LEFT JOIN org_build_floor obf ON obf.id = obr.floor_id AND obf.tenant_id = obr.tenant_id
        LEFT JOIN org_build ob ON ob.id = obf.build_id AND ob.tenant_id = obf.tenant_id
        WHERE 1 = 1
        <if test="reqVO.tenantIds != null">
            AND erds.tenant_id IN
            <foreach item="id" collection="reqVO.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="reqVO.elderName != null and reqVO.elderName != ''">
            AND ei.elder_name LIKE CONCAT('%', #{reqVO.elderName}, '%')
        </if>
        <if test="reqVO.floorId != null">
            AND obf.id = #{reqVO.floorId}
        </if>
        ORDER BY erds.create_time DESC, erds.id DESC
    </select>

    <select id="selectByIdWithElderInfo"
            resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.riskdisclosurestatement.ElderlyRiskDisclosureStatementRespVO">
        SELECT
            erds.*,
            ei.elder_name AS elderName,
            ei.elder_sex AS elderSex,
            ei.elder_age AS elderAge,
            COALESCE(obf.floor_name, ei.floor_name) AS floorName,
            COALESCE(CONCAT_WS('-', ob.build_name, obf.floor_name, obr.room_name, obb.bed_name), ei.bed_name) AS bedName,
            eabc.assess_score AS asphyxiationByChokingAssessScore,
            eabc.risk_level AS asphyxiationByChokingRiskLevel,
            eps.assess_score AS pressureSoresAssessScore,
            eps.risk_level AS pressureSoresRiskLevel,
            efd.assess_score AS fallDownAssessScore,
            efd.risk_level AS fallDownRiskLevel,
            efpm.assess_score AS fallPreventionMeasuresAssessScore,
            efpm.risk_level AS fallPreventionMeasuresRiskLevel,
            ee.assess_score AS empyrosisAssessScore,
            ee.risk_level AS empyrosisRiskLevel,
            ewa.assess_score AS wanderAwayAssessScore,
            ewa.risk_level AS wanderAwayRiskLevel,
            easr.assess_score AS suicideRiskAssessScore,
            easr.risk_level AS suicideRiskRiskLevel,
            eaarf.assess_score AS attackRiskAssessScore,
            eaarf.risk_level AS attackRiskRiskLevel,
            eae.assess_score AS antiEntertainmentAssessScore,
            eae.risk_level AS antiEntertainmentRiskLevel,
            easms.assess_score AS simpleMentalStateAssessScore,
            easms.risk_level AS simpleMentalStateRiskLevel,
            eapc.assess_score AS perceptionCommunicationAssessScore,
            eapc.risk_level AS perceptionCommunicationRiskLevel,
            eeq.assess_score AS equilibriumAssessScore,
            eeq.risk_level AS equilibriumRiskLevel,
            edl.assess_score AS dailyLifeAssessScore,
            edl.risk_level AS dailyLifeRiskLevel,
            enr.assess_score AS nutritionalRiskAssessScore,
            enr.risk_level AS nutritionalRiskRiskLevel
        FROM elderly_risk_disclosure_statement erds
        LEFT JOIN elderly_info ei ON ei.id = erds.elder_id
        LEFT JOIN org_build_bed obb ON obb.id = ei.bed_id AND obb.tenant_id = erds.tenant_id
        LEFT JOIN org_build_room obr ON obr.id = obb.room_id AND obr.tenant_id = obb.tenant_id
        LEFT JOIN org_build_floor obf ON obf.id = obr.floor_id AND obf.tenant_id = obr.tenant_id
        LEFT JOIN org_build ob ON ob.id = obf.build_id AND ob.tenant_id = obf.tenant_id
        LEFT JOIN elderly_asphyxiation_by_choking eabc ON eabc.id = erds.asphyxiation_by_choking_id AND eabc.tenant_id = erds.tenant_id
        LEFT JOIN elderly_pressure_sores eps ON eps.id = erds.pressure_sores_id AND eps.tenant_id = erds.tenant_id
        LEFT JOIN elderly_fall_down efd ON efd.id = erds.fall_down_id AND efd.tenant_id = erds.tenant_id
        LEFT JOIN elderly_fall_prevention_measures efpm ON efpm.id = erds.fall_prevention_measures_id AND efpm.tenant_id = erds.tenant_id
        LEFT JOIN elderly_empyrosis ee ON ee.id = erds.empyrosis_id AND ee.tenant_id = erds.tenant_id
        LEFT JOIN elderly_wander_away ewa ON ewa.id = erds.wander_away_id AND ewa.tenant_id = erds.tenant_id
        LEFT JOIN elderly_assess_suicide_risk easr ON easr.id = erds.suicide_risk_id AND easr.tenant_id = erds.tenant_id
        LEFT JOIN elderly_assess_attack_risk_factors eaarf ON eaarf.id = erds.attack_risk_id AND eaarf.tenant_id = erds.tenant_id
        LEFT JOIN elderly_anti_entertainment eae ON eae.id = erds.anti_entertainment_id AND eae.tenant_id = erds.tenant_id
        LEFT JOIN elderly_assess_simple_mental_state easms ON easms.id = erds.simple_mental_state_id AND easms.tenant_id = erds.tenant_id
        LEFT JOIN elderly_assess_perception_communication eapc ON eapc.id = erds.perception_communication_id AND eapc.tenant_id = erds.tenant_id
        LEFT JOIN elderly_equilibrium eeq ON eeq.id = erds.equilibrium_id AND eeq.tenant_id = erds.tenant_id
        LEFT JOIN elderly_daily_life edl ON edl.id = erds.daily_life_id AND edl.tenant_id = erds.tenant_id
        LEFT JOIN elderly_nutritional_risk enr ON enr.id = erds.nutritional_risk_id AND enr.tenant_id = erds.tenant_id
        WHERE erds.id = #{id}
    </select>

</mapper>
