<?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.elderly.ElderlyNursingLogMapper">

    <select id="selectLatestCreateTimeByPlanItemIds"
            resultType="cn.iocoder.yudao.module.system.dal.mysql.elderly.ElderlyNursingLogMapper$LatestCreateTimeDTO">
        SELECT
            enl.nursing_plan_item_id AS nursingPlanItemId,
            MAX(enl.create_time) AS createTime
        FROM elderly_nursing_log enl
        WHERE enl.tenant_id = #{tenantId}
          AND enl.nursing_plan_item_id IN
            <foreach collection="nursingPlanItemIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        GROUP BY enl.nursing_plan_item_id
    </select>

    <select id="getFinishList" resultType="cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyNursingLogRespVO">
        SELECT
            enl.id,
            enl.elder_id AS elderId,
            enp.elder_name AS elderName,
            enl.nurse,
            enl.nursing_plan_item_id AS nursingPlanItemId,
            npi.nurse_item_name AS nursingItemName,
            ni.frequency_category_type AS frequencyCategoryType,
            enl.remark,
            enl.finish_time AS finishTime,
            enl.create_time AS createTime
        FROM elderly_nursing_log enl
        LEFT JOIN elderly_nursing_plan_item npi ON npi.id = enl.nursing_plan_item_id
        LEFT JOIN nurse_item ni ON npi.nurse_item_id = ni.id
        LEFT JOIN elderly_nursing_plan enp ON enp.id = npi.nursing_plan_id
        WHERE enl.status = 1
            <if test="nurse != null and nurse != ''">
                AND enl.nurse LIKE CONCAT('%', #{nurse}, '%')
            </if>
            <if test="elderName != null and elderName != ''">
                AND enp.elder_name LIKE CONCAT('%', #{elderName}, '%')
            </if>
            <if test="tenantId != null">
                AND enl.tenant_id = #{tenantId}
            </if>
            <if test="frequencyCategoryType != null">
                AND ni.frequency_category_type = #{frequencyCategoryType}
            </if>
            <if test="startDate != null">
                AND enl.finish_time &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND enl.finish_time &lt;= #{endDate}
            </if>
        ORDER BY enl.finish_time DESC, enl.id DESC
    </select>

    <select id="selectPageByElderIdWithItemName" resultType="cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyNursingLogRespVO">
        SELECT
            enl.id,
            enl.elder_id AS elderId,
            enl.nurse,
            enl.nursing_plan_item_id AS nursingPlanItemId,
            npi.nurse_item_name AS nursingItemName,
            ni.frequency_category_type AS frequencyCategoryType,
            enl.remark,
            enl.status,
            enl.finish_time AS finishTime,
            enl.create_time AS createTime
        FROM elderly_nursing_log enl
        LEFT JOIN elderly_nursing_plan_item npi ON npi.id = enl.nursing_plan_item_id
        LEFT JOIN nurse_item ni ON npi.nurse_item_id = ni.id
        WHERE enl.elder_id = #{elderId} AND enl.status != 0
            <if test="startDate != null">
                AND enl.create_time &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND enl.create_time &lt;= #{endDate}
            </if>
        ORDER BY enl.create_time DESC, enl.id DESC
    </select>

    <select id="selectPlanLogByElderIdWithItemName"
            resultType="cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingPlanLogRespVO">
        SELECT
            enl.id,
            enl.nursing_plan_item_id AS nursingPlanItemId,
            npi.nurse_item_name AS nurseItemName,
            enl.elder_id AS elderId,
            enl.nurse,
            enl.remark,
            enl.nurse_date AS nurseDate,
            enl.tenant_id AS tenantId,
            enl.finish_time AS finishTime,
            enl.timeout,
            enl.status,
            enl.create_time AS createTime
        FROM elderly_nursing_log enl
        LEFT JOIN elderly_nursing_plan_item npi ON npi.id = enl.nursing_plan_item_id
        WHERE enl.elder_id = #{elderId}
          AND enl.status != 1
        ORDER BY enl.nurse_date DESC, enl.id DESC
    </select>

</mapper>
