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

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectListPage" resultType="cn.iocoder.yudao.module.system.api.bpm.vo.ElderlyRetreatRecordRespVO">
        SELECT
        err.id,
        err.retreat_date,
        err.cost_deadline_date,
        ei.elder_name,
        DATE_FORMAT(ei.check_in_time, '%Y-%m-%d') AS "checkInTime",
        err.bed_name,
        bbf.`status`,
        bbf.process_instance_id,
        st.name as tenantName,
        st.id as tenantId
        FROM
        elderly_retreat_record err
        LEFT JOIN elderly_info ei ON err.elderly_id = ei.id AND err.tenant_id = ei.tenant_id
        LEFT JOIN bpm_business_form bbf ON err.id = bbf.business_id
        LEFT JOIN system_tenant st ON st.id = ei.tenant_id
        AND err.tenant_id = bbf.tenant_id
        AND bbf.deleted = 0
        WHERE err.deleted = 0 AND err.current_flag = 0 AND err.apply_type = 1
        <if test="pageVO.elderName != null and pageVO.elderName != '' ">
            AND ei.elder_name LIKE concat('%', #{pageVO.elderName}, '%')
        </if>
        <if test="pageVO.retreatDate != null">
            AND err.retreat_date between #{pageVO.retreatDate[0]} and #{pageVO.retreatDate[1]}
        </if>
        <if test="pageVO.status != null">
            and bbf.`status` = #{pageVO.status}
        </if>
        <if test="pageVO.type != null">
            and bbf.`type` = #{pageVO.type}
        </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>
        ORDER BY err.create_time DESC , err.update_time DESC
    </select>

    <select id="selectPrintById" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.RetreatPrintRespVO">
        SELECT
            err.id,
            err.retreat_date,
            err.cost_deadline_date,
            err.reason,
            err.description,
            err.creator,
            err.elderly_id,
            err.bed_name,
            ei.elder_name,
            ei.id_card,
            ei.check_in_time,
            DATE_FORMAT( ei.check_in_time, '%Y-%m-%d' ) AS "checkInTime",
            st.name AS "tenantName",
            bbf.start_tenant_id AS "tenantId"
        FROM
            elderly_retreat_record err
                LEFT JOIN elderly_info ei ON err.elderly_id = ei.id
                LEFT JOIN bpm_business_form bbf ON err.id = bbf.business_id AND err.tenant_id = bbf.tenant_id AND bbf.deleted = 0
                LEFT JOIN system_tenant st ON err.tenant_id = st.id
        WHERE err.id = #{id} AND err.deleted = 0 AND err.apply_type = 1
        ORDER BY err.create_time DESC
        LIMIT 1
    </select>

</mapper>