<?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.ElderlyInfoMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO" id="BaseResultMap">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="elder_image" jdbcType="VARCHAR" property="elderImage"/>
        <result column="elder_cover" jdbcType="VARCHAR" property="elderCover"/>
        <result column="elder_name" jdbcType="VARCHAR" property="elderName"/>
        <result column="elder_sex" jdbcType="INTEGER" property="elderSex"/>
        <result column="elder_age" jdbcType="INTEGER" property="elderAge"/>
        <result column="elder_height" jdbcType="DECIMAL" property="elderHeight"/>
        <result column="elder_weight" jdbcType="DECIMAL" property="elderWeight"/>
        <result column="elder_blood" jdbcType="VARCHAR" property="elderBlood"/>
        <result column="region" jdbcType="VARCHAR" property="region"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="sales_user_id" jdbcType="BIGINT" property="salesUserId"/>
        <result column="user_sign" jdbcType="VARCHAR" property="userSign"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="contract_number" jdbcType="VARCHAR" property="contractNumber"/>
        <result column="census_register" jdbcType="VARCHAR" property="censusRegister"/>
        <result column="id_card" jdbcType="VARCHAR" property="idCard"/>
        <result column="blood_type" jdbcType="VARCHAR" property="bloodType"/>
        <result column="nation_id" jdbcType="VARCHAR" property="nationId"/>
        <result column="political" jdbcType="VARCHAR" property="political"/>
        <result column="birthday" jdbcType="DATE" property="birthday"/>
        <result column="tel_phone" jdbcType="VARCHAR" property="telPhone"/>
        <result column="economy_source" jdbcType="VARCHAR" property="economySource"/>
        <result column="religious_belief" jdbcType="VARCHAR" property="religiousBelief"/>
        <result column="home_tel" jdbcType="VARCHAR" property="homeTel"/>
        <result column="media_channels" jdbcType="VARCHAR" property="mediaChannels"/>
        <result column="business_type" jdbcType="VARCHAR" property="businessType"/>
        <result column="medical_insurance" jdbcType="VARCHAR" property="medicalInsurance"/>
        <result column="degree_education" jdbcType="INTEGER" property="degreeEducation"/>
        <result column="marital_status" jdbcType="INTEGER" property="maritalStatus"/>
        <result column="elder_type" jdbcType="INTEGER" property="elderType"/>
        <result column="form_supply" jdbcType="INTEGER" property="formSupply"/>
        <result column="retirement_unit" jdbcType="VARCHAR" property="retirementUnit"/>
        <result column="payment_method" jdbcType="INTEGER" property="paymentMethod"/>
        <result column="referee_name" jdbcType="VARCHAR" property="refereeName"/>
        <result column="referee_id" jdbcType="BIGINT" property="refereeId"/>
        <result column="referee_pay" jdbcType="DECIMAL" property="refereePay"/>
        <result column="allergic_drug" jdbcType="VARCHAR" property="allergicDrug"/>
        <result column="check_in_time" jdbcType="TIMESTAMP" property="checkInTime"/>
        <result column="in_status" jdbcType="INTEGER" property="inStatus"/>
        <result column="nurse_level_id" jdbcType="BIGINT" property="nurseLevelId"/>
        <result column="nurse_level_name" jdbcType="VARCHAR" property="nurseLevelName"/>
        <result column="nurse_group_id" jdbcType="BIGINT" property="nurseGroupId"/>
        <result column="nurse_group_name" jdbcType="VARCHAR" property="nurseGroupName"/>
        <result column="build_id" jdbcType="BIGINT" property="buildId"/>
        <result column="build_name" jdbcType="VARCHAR" property="buildName"/>
        <result column="floor_id" jdbcType="BIGINT" property="floorId"/>
        <result column="floor_name" jdbcType="VARCHAR" property="floorName"/>
        <result column="room_id" jdbcType="BIGINT" property="roomId"/>
        <result column="room_name" jdbcType="VARCHAR" property="roomName"/>
        <result column="bed_id" jdbcType="BIGINT" property="bedId"/>
        <result column="bed_name" jdbcType="VARCHAR" property="bedName"/>
        <result column="in_deposit" jdbcType="DECIMAL" property="inDeposit"/>
        <result column="machine_ids" jdbcType="VARCHAR" property="machineIds"/>
        <result column="disability" jdbcType="VARCHAR" property="disability"/>
        <result column="drug_allergy" jdbcType="VARCHAR" property="drugAllergy"/>
        <result column="settle_date" jdbcType="TIMESTAMP" property="settleDate"/>
        <result column="ocac" jdbcType="VARCHAR" property="ocac"/>
        <result column="file_number" jdbcType="VARCHAR" property="fileNumber"/>
        <result column="source_type" jdbcType="INTEGER" property="sourceType"/>
        <result column="in_status_type" jdbcType="INTEGER" property="inStatusType"/>
    </resultMap>
    <select id="findPage" resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO">
        select * from elderly_info where 1 = 1 and deleted = 0
        <if test="pageVO.elderName != null and pageVO.elderName != '' ">
            and elder_name like concat('%', #{pageVO.elderName}, '%')
        </if>
        <if test="pageVO.elderSex != null and pageVO.elderSex != '' ">
            and elder_sex = #{pageVO.elderSex}
        </if>
        <if test="pageVO.inStatus != null">
            and in_status = #{pageVO.inStatus}
        </if>
        <if test="pageVO.buildId != null">
            and build_id = #{pageVO.buildId}
        </if>
        <if test="pageVO.floorId != null">
            and floor_id = #{pageVO.floorId}
        </if>
        <if test="pageVO.roomName != null and pageVO.roomName != '' ">
            and room_name like concat('%', #{pageVO.roomName}, '%')
        </if>
        <if test="pageVO.nurseLevelId != null">
            and nurse_level_id = #{pageVO.nurseLevelId}
        </if>
        <if test="pageVO.checkInTime != null">
            and create_time between #{pageVO.checkInTime[0]} and #{pageVO.checkInTime[1]}
        </if>
        <if test="pageVO.orgType != null">
            and org_type = #{pageVO.orgType}
        </if>
        <if test="pageVO.tenantIds != null">
            and tenant_id in
            <foreach item="id" collection="pageVO.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        order by update_time desc
    </select>

    <select id="findBpmPage" resultType="cn.iocoder.yudao.module.system.api.bpm.vo.ElderlyCheckinInfoRespVO">
        SELECT
        ei.*,
        bbf.`status` AS "bpmStatus",
        bbf.process_instance_id,
        st.name as tenantName
        FROM
        elderly_info ei
        INNER JOIN elderly_retreat_record err ON ei.id = err.elderly_id AND apply_type = 0
        INNER JOIN bpm_business_form bbf ON bbf.business_id = err.id AND type = 1
        LEFT JOIN system_tenant st ON st.id = ei.tenant_id
        where err.current_flag = 0 AND err.deleted = 0
        <if test="pageVO.elderName != null and pageVO.elderName != '' ">
            and ei.elder_name like concat('%', #{pageVO.elderName}, '%')
        </if>
        <if test="pageVO.elderSex != null and pageVO.elderSex != '' ">
            and ei.elder_sex = #{pageVO.elderSex}
        </if>
        <if test="pageVO.inStatus != null">
            and ei.in_status = #{pageVO.inStatus}
        </if>
        <if test="pageVO.checkInTime != null">
            and ei.create_time between #{pageVO.checkInTime[0]} and #{pageVO.checkInTime[1]}
        </if>
        <if test="pageVO.orgType != null">
            and ei.org_type = #{pageVO.orgType}
        </if>
        <if test="pageVO.tenantIds != null">
            and ei.tenant_id in
            <foreach item="id" collection="pageVO.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="pageVO.orgType"></if>
        order by ei.update_time desc
    </select>

    <select id="elderlyCheckInRecordPage"
            resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyCheckInRecordDO">

        select t1.*,t2.elder_name,t2.elder_sex,t2.in_status_type,t2.nurse_level_name from elderly_check_in_record t1
        inner join elderly_info t2 on t1.elder_id = t2.id
        where 1 = 1
        <if test="params.elderName != null and params.elderName != '' ">
            and t2.elder_name like concat('%',#{params.elderName} ,'%')
        </if>
        <if test="params.status != null">
            and t1.status = #{params.status}
        </if>
        <if test="params.inStatusType != null">
            and t2.in_status_type = #{params.inStatusType}
        </if>
        <if test="params.checkInTime != null">
            and t1.check_in_time between #{params.checkInTime[0]} and #{params.checkInTime[1]}
        </if>
        <if test="params.orgType != null">
            and t2.org_type = #{params.orgType}
        </if>
        <if test="params.tenantIds != null">
            and t2.tenant_id in
            <foreach item="id" collection="params.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        order by t1.created_time desc,t1.status asc
    </select>
    <select id="getElderListByBuild" resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO">
        select * from elderly_info
        <where>
            1 = 1
            <if test="buildId != null">
                and build_id = #{buildId}
            </if>
            <if test="floorId != null">
                and floor_id = #{floorId}
            </if>
            <if test="roomId != null">
                and room_id = #{roomId}
            </if>
            <if test="tenantIds != null">
                and tenant_id in
                <foreach item="id" collection="tenantIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>
    <select id="getElderlyMedicationExecutionPage"
            resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyMedicationExecutionRespVO">
        select ei.id as elderId,ei.elder_name,ei.elder_sex,ebr.execute_status from elderly_info ei inner join elderly_byod_registration ebr on ebr.elder_id = ei.id
        <where>
            1 = 1
            <if test="params.elderName != null and params.elderName != ''">
                and ei.elder_name like concat('%',#{params.elderName} ,'%')
            </if>
            <if test="params.bedId != null">
                and ei.bed_id = #{params.bedId}
            </if>
            <if test="params.executeStatus != null">
                and ebr.execute_status = #{params.executeStatus}
            </if>
            <if test="params.orgType != null">
                and ei.org_type = #{params.orgType}
            </if>
            <if test="params.tenantId != null">
                and ei.tenant_id = #{params.tenantId}
            </if>
        </where>
        order by ei.id desc
    </select>

    <select id="getCheckInDetail" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.CheckInRecordDetailRespVO">
        SELECT
            ecir.*,
            ei.elder_name,
            ei.id AS elder_id,
            ei.elder_sex,
            ei.elder_age,
            ei.in_status_type,
            ei.nurse_level_name,
            ei.id_card,
            ei.bed_id,
            ei.tenant_id,
            bbf.status AS bpmStatus,
            bbf.process_instance_id,
            bbf.type,
            cia.contract_json,
            cia.monthly_expenses_json,
            cia.one_time_expenses_json,
            cia.stage_expenses_json,
            cia.total_amount,
            cia.remark,
            cia.check_in_time,
            cia.check_in_deadline_time,
            cia.nurse_level_id,
            cia.is_private_room,
            cia.special_care_notes
        FROM
            elderly_check_in_record ecir
        INNER JOIN elderly_info ei ON ecir.elder_id = ei.id
        INNER JOIN bpm_business_form bbf ON ecir.id = bbf.business_id AND bbf.type = 9
        INNER JOIN check_in_apply cia ON ecir.associate_id = cia.id
        WHERE ecir.id = #{id}
    </select>

    <select id="selectCountByParam" resultType="java.lang.Integer">
        SELECT  COUNT(1) FROM elderly_info
        <where>
            1 = 1
            <if test="inStatus != null">
                and in_status = #{inStatus}
            </if>
            <if test="checkInTime != null and checkOutFlag == 0">
                and  DATE_FORMAT(check_in_time, '%Y-%m') <![CDATA[ = ]]> #{checkInTime}
            </if>
            <if test="checkInTime != null and checkOutFlag == 1">
                and  DATE_FORMAT(check_in_time, '%Y-%m') <![CDATA[ <= ]]> #{checkInTime}
            </if>
            <if test="checkOutTime != null and checkOutFlag == 0">
                and  DATE_FORMAT(check_out_time, '%Y-%m') = #{checkOutTime}
            </if>
            <if test="checkOutTime != null and checkOutFlag == 1">
                and  (DATE_FORMAT(check_out_time, '%Y-%m') <![CDATA[ < ]]> #{checkOutTime} or check_out_time is NULL)
            </if>
            <if test="tenantId != null">
                and tenant_id = #{tenantId}
            </if>
        </where>
    </select>

    <select id="selectTryStayCount" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM
        elderly_info
        WHERE
        in_status_type = 2
        <if test="reqVO.currentDate != null">
            AND DATE_FORMAT(check_in_time, '%Y-%m') = DATE_FORMAT(#{reqVO.currentDate}, '%Y-%m')
        </if>
        <if test="reqVO.tenantIds != null">
            AND tenant_id IN
            <foreach item="id" collection="reqVO.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="selectGoOutCount" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM
        elderly_ask_leave
        WHERE
        status = 0
        <if test="reqVO.currentDate != null">
            AND DATE_FORMAT(out_date, '%Y-%m') = DATE_FORMAT(#{reqVO.currentDate}, '%Y-%m')
        </if>
        <if test="reqVO.tenantIds != null">
            AND tenant_id IN
            <foreach item="id" collection="reqVO.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="selectCurrentCheckInCount" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM
        elderly_info
        WHERE
        in_status = 1
        <if test="reqVO.currentDate != null">
            AND DATE_FORMAT(check_in_time, '%Y-%m') = DATE_FORMAT(#{reqVO.currentDate}, '%Y-%m')
        </if>
        <if test="reqVO.tenantIds != null">
            AND tenant_id IN
            <foreach item="id" collection="reqVO.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="selectVitalSignsList" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyVitalSignsStatisticsRespVO">
        SELECT
            ei.id AS elderId,
            ei.elder_name,
            ei.elder_age,
            ei.elder_sex,
            eei.item_name,
            ei.tenant_id
        FROM
            elderly_info ei
                INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
                AND ei.tenant_id = ee.tenant_id
                INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
                AND ei.tenant_id = eei.tenant_id
                INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id
                AND ei.tenant_id = scc.tenant_id
        WHERE
            ei.in_status = 1
          AND ee.created_time = ( SELECT MAX( ee2.created_time ) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id )
          AND scc.identification = 2
          AND  ei.tenant_id IN
          <foreach item="id" collection="pageReqVO.tenantIds" open="(" separator="," close=")">
              #{id}
          </foreach>
    </select>


    <select id="selectVitalSignsTodayList" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyVitalSignsStatisticsTodayRespVO">
        SELECT
            ei.id AS elderId,
            ei.elder_name,
            ei.elder_age,
            ei.elder_sex,
            ei.tenant_id,
            evs.measuring_time,
            evs.systolic_pressure,
            evs.diastolic_pressure
        FROM
            elderly_vital_signs evs
        INNER JOIN elderly_info ei ON evs.elder_id = ei.id AND evs.tenant_id = ei.tenant_id
        WHERE
        DATE_FORMAT(evs.measuring_time, '%Y-%m-%d') = DATE_FORMAT(#{pageReqVO.currentDate}, '%Y-%m-%d')
        <if test="pageReqVO.buildId">
            AND ei.build_id = #{pageReqVO.buildId}
        </if>
        <if test="pageReqVO.floorId">
            AND ei.floor_id = #{pageReqVO.floorId}
        </if>
            AND ei.tenant_id IN
        <foreach item="id" collection="pageReqVO.tenantIds" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND  (evs.systolic_pressure <![CDATA[ < ]]> #{pageReqVO.systolicPressureMin} OR evs.systolic_pressure <![CDATA[ > ]]> #{pageReqVO.systolicPressureMax})
        OR (evs.diastolic_pressure <![CDATA[ < ]]> #{pageReqVO.diastolicPressureMin} OR evs.diastolic_pressure <![CDATA[ > ]]> #{pageReqVO.diastolicPressureMax})
        ORDER BY evs.measuring_date,evs.measuring_time DESC

    </select>


</mapper>