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

    <select id="selectNursingPlanItemsWithFrequencyCategory"
            resultType="cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingPlanItemRespVO">
        select enpi.id,
               enpi.nursing_plan_id,
               enpi.nurse_item_id,
               enpi.nurse_item_name,
               ni.frequency as frequency,
               ni.frequency_type as frequencyType,
               enpi.operating_mode,
               enpi.is_delete,
               enpi.is_extra,
               enpi.begin_time,
               enpi.created_time,
               enpi.created_by,
               ni.frequency_category,
               ni.frequency_category_type
        from elderly_nursing_plan_item enpi
                 left join nurse_item ni on ni.id = enpi.nurse_item_id
        <where>
            enpi.nursing_plan_id = #{nursingPlanId}
            and enpi.is_delete = 0
            <if test="isExtra != null">
                and enpi.is_extra = #{isExtra}
            </if>
            <if test="isHighFrequencyCategoryType != null">
                and ni.frequency_category_type = #{isHighFrequencyCategoryType}
            </if>
        </where>
        order by enpi.id asc
    </select>

    <select id="selectHighFrequencyPlanItems"
            resultType="cn.iocoder.yudao.module.system.controller.admin.nursing.vo.HighFrequencyPlanItemVO">
        select enp.elder_id        as elderId,
               enp.id              as nursingPlanId,
               enpi.id             as nursingPlanItemId,
               enpi.nurse_item_id  as nurseItemId,
               ni.frequency        as frequency,
               ni.frequency_type   as frequencyType,
               enpi.operating_mode as operatingMode,
               enpi.begin_time     as beginTime,
               ni.timeout          as timeout
        from elderly_nursing_plan_item enpi
                 inner join elderly_nursing_plan enp on enp.id = enpi.nursing_plan_id
                 inner join elderly_info ei on ei.id = enp.elder_id
                 inner join nurse_item ni on ni.id = enpi.nurse_item_id
        <where>
            enpi.is_delete = 0
            and enp.status = 1
            and enp.effective_date &lt;= #{today}
            and (enp.expiration_date is null or enp.expiration_date &gt;= #{today})
            and ei.tenant_id = #{tenantId}
            and ni.tenant_id = #{tenantId}
            and ni.status = 1
            and ni.frequency_category_type = 1
        </where>
        order by enp.elder_id asc, enpi.id asc
    </select>

    <select id="selectPlanItemsWithElderByElderIdsAndFrequencyCategoryType"
            resultType="cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingPlanItemWithElderVO">
        select ei.id as elderId,
               ei.elder_name as elderName,
               enpi.id,
               enpi.nursing_plan_id,
               enpi.nurse_item_id,
               enpi.nurse_item_name,
               ni.frequency as frequency,
               ni.frequency_type as frequencyType,
               enpi.operating_mode,
               enpi.is_delete,
               enpi.is_extra,
               enpi.begin_time,
               enpi.created_time,
               enpi.created_by,
               ni.frequency_category,
               ni.frequency_category_type
        from elderly_info ei
                 inner join elderly_nursing_plan enp on enp.elder_id = ei.id
                 inner join elderly_nursing_plan_item enpi on enpi.nursing_plan_id = enp.id
                 inner join nurse_item ni on ni.id = enpi.nurse_item_id
        <where>
            ei.id in
            <foreach collection="elderIds" item="elderId" open="(" separator="," close=")">
                #{elderId}
            </foreach>
            and ei.in_status = 1
            and enp.status = 1
            and enp.effective_date &lt;= #{today}
            and (enp.expiration_date is null or enp.expiration_date &gt;= #{today})
            and enpi.is_delete = 0
            and ni.frequency_category_type = #{frequencyCategoryType}
            and ni.status = 1
            and ei.tenant_id = #{tenantId}
            and ni.tenant_id = #{tenantId}
        </where>
        order by ei.id asc, enpi.id asc
    </select>

</mapper>
