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

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

    <select id="selectBedListPage" resultType="cn.iocoder.yudao.module.system.api.bpm.vo.BedChangeRecordRespVO">
        SELECT
            ecr.id,
            ei.id AS elderlyId,
            ei.elder_name AS elderlyName,
            ei.id_card,
            ei.bed_id,
            ei.bed_name,
            ecr.original_id,
            ecr.original_name,
            soc.charge_name AS originalItemName,
            st.name as tenantName,
            st.id as tenantId,
            ecr.expect_id,
            ecr.expect_name,
            soc2.charge_name AS expectItemName,

            ecr.change_date,
            ecr.is_private_room,
            ecr.amount,
            bbf.`status`,
            bbf.process_instance_id
        FROM elderly_change_record ecr
        INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
        INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
        INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
        INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id AND scc.identification = 1
        INNER JOIN sys_overhead_charge soc ON soc.id = ecr.expect_overhead_charge_id
        INNER JOIN sys_overhead_charge soc2 ON soc2.id = overhead_charge_id
        INNER JOIN bpm_business_form bbf ON ecr.id = bbf.business_id
        INNER JOIN system_tenant st ON st.id = ei.tenant_id
        WHERE ecr.current_flag = 0 AND ecr.change_type = 0
            AND ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)
            <if test="pageVO.bedName != null">
                AND ecr.expect_name LIKE CONCAT('%', #{pageVO.bedName}, '%')
            </if>
            <if test="pageVO.expectId != null">
                AND ecr.expect_id =  #{pageVO.expectId}
            </if>
            <if test="pageVO.originalBedName != null">
                AND ecr.original_name LIKE CONCAT('%', #{pageVO.originalBedName}, '%')
            </if>
            <if test="pageVO.elderName != null">
                AND ei.elder_name LIKE CONCAT('%', #{pageVO.elderName}, '%')
            </if>
            <if test="pageVO.changeDate != null">
                AND ecr.change_date between #{pageVO.changeDate[0]} and #{pageVO.changeDate[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 ecr.change_date DESC ,ecr.create_time DESC , ecr.update_time DESC
    </select>

    <select id="getBedDetail" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.BedChangeRecordRespVO">
        SELECT
            ecr.id,
            ei.id_card,
            ei.id AS elderlyId,
            ei.elder_name,
            ei.bed_id,
            ei.bed_name,
            ecr.original_id,
            ecr.original_name,
            ecr.overhead_charge_id,
            soc2.charge_name AS "originalItemName",
            eei.item_name AS originalItemName,
            eei.is_discount AS originalIsDiscount,
            ecr.original_amount AS originalAmount,

            ecr.expect_id,
            ecr.expect_name,
            ecr.expect_overhead_charge_id,
            soc.charge_name AS "expectItemName",
            ecr.change_date,
            ecr.is_private_room,
            ecr.is_discount,
            ecr.reason,
            ecr.elderly_id,
            ecr.change_files AS bedFiles,
            ecr.overhead_charge_id,
            ecr.amount,
            ecr.actual_amount,
            ecr.discount_amount,
            ecr.discount,
            ecr.category_name,
            bbf.start_tenant_id
        FROM elderly_change_record ecr
         INNER JOIN bpm_business_form bbf ON bbf.business_id = ecr.id AND bbf.type = 4
         INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
         INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
         INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
         INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id AND scc.identification = 1

         INNER JOIN sys_overhead_charge soc ON soc.id = ecr.expect_overhead_charge_id
         INNER JOIN sys_overhead_charge soc2 ON ecr.overhead_charge_id = soc2.id

        WHERE ecr.current_flag = #{currentFlag} AND ecr.id = #{id}
            AND ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)
    </select>

    <select id="createBedChangeGetDetail" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.BedChangeRecordRespVO">
        SELECT
        ecr.id,
        ei.id_card,
        ei.id AS elderlyId,
        ei.elder_name,
        ei.bed_id,
        ei.bed_name,
        ecr.original_id,
        ecr.original_name,
        ecr.overhead_charge_id,
        soc2.charge_name AS "originalItemName",
        eei.item_name AS originalItemName,
        eei.is_discount AS originalIsDiscount,
        eei.actual_amount AS originalAmount,

        ecr.expect_id,
        ecr.expect_name,
        ecr.expect_overhead_charge_id,
        soc.charge_name AS "expectItemName",
        ecr.change_date,
        ecr.is_private_room,
        ecr.is_discount,
        ecr.reason,
        ecr.elderly_id,
        ecr.change_files AS bedFiles,
        ecr.overhead_charge_id,
        ecr.amount,
        ecr.actual_amount,
        ecr.discount_amount,
        ecr.discount,
        ecr.category_name,
        bbf.start_tenant_id
        FROM elderly_change_record ecr
        INNER JOIN bpm_business_form bbf ON bbf.business_id = ecr.id AND bbf.type = 4
        INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
        INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
        INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
        INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id AND scc.identification = 1

        INNER JOIN sys_overhead_charge soc ON soc.id = ecr.expect_overhead_charge_id
        INNER JOIN sys_overhead_charge soc2 ON ecr.overhead_charge_id = soc2.id

        WHERE ecr.current_flag = #{currentFlag} AND ecr.id = #{id}
        AND ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)
    </select>

    <select id="selectElderlyPage" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.BedChangeElderlyRespVO">
        SELECT
            eei.id AS expenseItemId,
            eei.item_name,
            ei.id,
            ei.elder_name,
            ei.id_card,
            ei.bed_id,
            eei.item_id AS originalId,
            eei.item_id AS overheadChargeId,
            eei.actual_amount,
            eei.discount_amount,
            eei.is_discount,
            ei.tenant_id,
            st.name AS tenantName,
            ei.elder_sex
        FROM
            elderly_info ei
                INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
                INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
                INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id
                INNER JOIN system_tenant st ON st.id = ei.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)
            <if test="pageVO.elderName != null">
                AND ei.elder_name LIKE CONCAT('%', #{pageVO.elderName}, '%')
            </if>
            <if test="pageVO.identification != null">
                AND scc.identification = #{pageVO.identification}
            </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 ei.update_time desc
    </select>

    <select id="selectElderlyById" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.BedChangeElderlyRespVO">
        SELECT
            eei.item_name,
            ei.id,
            ei.elder_name,
            ei.id_card,
            ei.bed_id,
            eei.item_id AS originalId,
            eei.item_id AS overheadChargeId,
            eei.actual_amount,
            eei.discount_amount,
            eei.is_discount,
            ei.tenant_id,
            st.name AS tenantName,
            ei.elder_sex
        FROM
            elderly_info ei
                INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
                INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
                INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id
                INNER JOIN system_tenant st ON st.id = ei.tenant_id
        WHERE
            ei.in_status = 1
            AND ei.id = #{elderId}
            AND ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)
            <if test="pageVO.identification != null">
                AND scc.identification = #{pageVO.identification}
            </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 ei.update_time desc
    </select>

    <select id="selectCurrentChargeItemList" resultType="cn.iocoder.yudao.module.system.api.bpm.vo.ElderlyCurrentChargeItemRespVO">
        SELECT
            ei.id AS elderlyId,
            ei.elder_name AS elderlyName,
            ei.id_card AS idCard,
            ei.tenant_id AS tenantId,
            eei.id AS expenseItemId,
            eei.item_id AS overheadChargeId,
            eei.item_name AS itemName,
            eei.actual_amount AS actualAmount,
            eei.discount_amount AS discountAmount,
            eei.is_discount AS isDiscount,
            eei.discount AS discount
        FROM
            elderly_info ei
                INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
                INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
                INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_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)
            <if test="identification != null">
                AND scc.identification = #{identification}
            </if>
            <if test="tenantId != null">
                AND ei.tenant_id = #{tenantId}
            </if>
            <if test="orgType != null">
                AND ei.org_type = #{orgType}
            </if>
            <if test="overheadChargeIds != null and overheadChargeIds.size() > 0">
                AND eei.item_id in
                <foreach item="id" collection="overheadChargeIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        order by ei.update_time desc
    </select>

    <select id="selectNurseListPage" resultType="cn.iocoder.yudao.module.system.api.bpm.vo.NurseChangeRecordRespVO">
        SELECT
            ecr.id,
            ecr.original_name AS originalItemName,
            soc.charge_name,
            ei.id AS elderlyId,
            ei.elder_name AS elderlyName,
            ei.id_card,
            ecr.original_id,
            ecr.original_name,
            ecr.expect_id,
            ecr.expect_name,
            ecr.change_date,
            ecr.is_private_room,
            ecr.amount,
            st.name as tenantName,
            st.id as tenantId,
            bbf.`status`,
            bbf.process_instance_id
        FROM elderly_change_record ecr
        INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
        INNER JOIN sys_overhead_charge soc ON soc.id = ecr.overhead_charge_id
        INNER JOIN bpm_business_form bbf ON ecr.id = bbf.business_id
        INNER JOIN system_tenant st ON st.id = ei.tenant_id
        WHERE ecr.current_flag = 0
        <if test="pageVO.expectName != null">
            AND ecr.expect_name LIKE CONCAT('%', #{pageVO.expectName}, '%')
        </if>
        <if test="pageVO.originalName != null">
            AND ecr.original_name LIKE CONCAT('%', #{pageVO.originalName}, '%')
        </if>
        <if test="pageVO.elderName != null">
            AND ei.elder_name LIKE CONCAT('%', #{pageVO.elderName}, '%')
        </if>
        <if test="pageVO.changeDate != null">
            AND ecr.change_date between #{pageVO.changeDate[0]} and #{pageVO.changeDate[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.changeType != null">
            and ecr.change_type = #{pageVO.changeType}
        </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 ecr.create_time DESC , ecr.update_time DESC
    </select>

    <select id="selectCateringListPage" resultType="cn.iocoder.yudao.module.system.api.bpm.vo.CateringChangeRecordRespVO">
        SELECT
        ecr.id,
        eei.item_name AS originalItemName,
        soc.charge_name,
        ei.id AS elderlyId,
        ei.elder_name AS elderlyName,
        ei.id_card,
        ei.elder_sex,
        ecr.original_id,
        ecr.original_name,
        ecr.expect_id,
        ecr.expect_name,
        ecr.change_date,
        ecr.is_private_room,
        ecr.amount,
        ecr.overhead_charge_id,
        ecr.category_name,
        bbf.`status`,
        bbf.process_instance_id,
        st.name as tenantName,
        st.id as tenantId
        FROM elderly_change_record ecr
        INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
        INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
        INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
        INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id AND scc.identification = 3
        INNER JOIN sys_overhead_charge soc ON soc.id = ecr.overhead_charge_id
        INNER JOIN bpm_business_form bbf ON ecr.id = bbf.business_id
        INNER JOIN system_tenant st ON st.id = ei.tenant_id
        WHERE ecr.current_flag = 0
        AND ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)
        <if test="pageVO.expectName != null">
            AND ecr.expect_name LIKE CONCAT('%', #{pageVO.expectName}, '%')
        </if>
        <if test="pageVO.originalName != null">
            AND ecr.original_name LIKE CONCAT('%', #{pageVO.originalName}, '%')
        </if>
        <if test="pageVO.elderName != null">
            AND ei.elder_name LIKE CONCAT('%', #{pageVO.elderName}, '%')
        </if>
        <if test="pageVO.changeDate != null">
            AND ecr.change_date between #{pageVO.changeDate[0]} and #{pageVO.changeDate[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.changeType != null">
            and ecr.change_type = #{pageVO.changeType}
        </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 ecr.create_time DESC , ecr.update_time DESC
    </select>

    <select id="selectPriceListPage" resultType="cn.iocoder.yudao.module.system.api.bpm.vo.PriceChangeRecordRespVO">
        SELECT
            ecr.id,
            ei.id AS elderlyId,
            ei.elder_name AS elderlyName,
            ei.id_card,
            ecr.change_date,
            ecr.category_name,
            bbf.`status`,
            bbf.process_instance_id,
            st.name as tenantName,
            st.id as tenantId
        FROM elderly_change_record ecr
        INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
        INNER JOIN bpm_business_form bbf ON ecr.id = bbf.business_id
        INNER JOIN system_tenant st  ON st.id = ei.tenant_id
        WHERE ecr.current_flag = 0
        <if test="pageVO.expectName != null">
            AND ecr.expect_name LIKE CONCAT('%', #{pageVO.expectName}, '%')
        </if>
        <if test="pageVO.originalName != null">
            AND ecr.original_name LIKE CONCAT('%', #{pageVO.originalName}, '%')
        </if>
        <if test="pageVO.elderName != null">
            AND ei.elder_name LIKE CONCAT('%', #{pageVO.elderName}, '%')
        </if>
        <if test="pageVO.changeDate != null">
            AND ecr.change_date between #{pageVO.changeDate[0]} and #{pageVO.changeDate[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.changeType != null">
            and ecr.change_type = #{pageVO.changeType}
        </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 ecr.create_time DESC , ecr.update_time DESC
    </select>

    <select id="selectTransferListPage" resultType="cn.iocoder.yudao.module.system.api.bpm.vo.TransferChangeRecordRespVO">
        SELECT
            ecr.id,
            ei.id AS elderlyId,
            ei.elder_name AS elderlyName,
            ecr.change_date,
            bbf.`status`,
            bbf.process_instance_id,
            st.name as tenantName,
            st.id as tenantId
        FROM elderly_change_record ecr
        INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
        INNER JOIN bpm_business_form bbf ON ecr.id = bbf.business_id
        INNER JOIN system_tenant st ON st.id = ei.tenant_id
        WHERE ecr.current_flag = 0
        <if test="pageVO.expectName != null">
            AND ecr.expect_name LIKE CONCAT('%', #{pageVO.expectName}, '%')
        </if>
        <if test="pageVO.originalName != null">
            AND ecr.original_name LIKE CONCAT('%', #{pageVO.originalName}, '%')
        </if>
        <if test="pageVO.elderName != null">
            AND ei.elder_name LIKE CONCAT('%', #{pageVO.elderName}, '%')
        </if>
        <if test="pageVO.changeDate != null">
            AND ecr.change_date between #{pageVO.changeDate[0]} and #{pageVO.changeDate[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.changeType != null">
            and ecr.change_type = #{pageVO.changeType}
        </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 ecr.create_time DESC , ecr.update_time DESC
    </select>

    <select id="getEvaluationByElderId" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.NurseChangeyEvaluationRespVO">
        SELECT
            sep.id AS evaluationProcessId,
            sep.process_name,
            sep.type,
            esa.id AS syntheticAbilityId,
            esa.total_score
        FROM elderly_synthetic_ability esa
        INNER JOIN sys_evaluation_process sep ON esa.questionnaire_id = sep.id
        INNER JOIN elderly_info ei ON esa.elder_id = ei.id
        WHERE esa.status = 0 AND ei.id = #{elderId}
    </select>

    <select id="getNurseDetail" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.NurseChangeDetailRespVO">
        SELECT
            ecr.id,
            eei.item_name AS originalItemName,
            eei.is_discount AS originalIsDiscount,
            ecr.original_amount AS originalAmount,
            eei.discount_amount AS originalDiscountAmount,
            ecr.original_amount AS originalActualAmount,
            eei.discount AS originalDiscount,
            soc.charge_name,
            ei.elder_name,
            ei.id_card,
            ei.bed_id,
            ecr.original_id,
            ecr.original_name,
            ecr.expect_id,
            ecr.expect_name,
            ecr.change_date,
            ecr.is_private_room,
            ecr.is_discount,
            ecr.reason,
            ecr.elderly_id,
            ecr.change_files,
            ecr.overhead_charge_id,
            ecr.amount,
            ecr.actual_amount,
            ecr.discount_amount,
            ecr.discount,
            esa.id AS syntheticAbilityId,
            esa.questionnaire_name AS processName,
            sep.id AS evaluationProcessId,
            esa.total_score,
            bbf.start_tenant_id
        FROM elderly_change_record ecr
                 INNER JOIN bpm_business_form bbf ON bbf.business_id = ecr.id AND bbf.type = 5
                 INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
                 INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
                 INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id AND eei.item_id = ecr.overhead_charge_id
                 INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id AND scc.identification = 2
                 INNER JOIN sys_overhead_charge soc ON soc.id = ecr.overhead_charge_id
                 LEFT JOIN sys_evaluation_process sep ON ecr.associate_id = sep.id
                 LEFT JOIN elderly_synthetic_ability esa ON sep.id = esa.questionnaire_id AND esa.elder_id = ei.id
        WHERE ecr.current_flag = 0 AND ecr.id = #{id}
            <!--tips:添加该过滤条件如果该流程之后存在新的变更或者续约了，那么会报错。不添加该过滤条件的问题？？？-->
<!--            AND ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)-->
        order by ee.created_time
    </select>

    <select id="createNurseChangeGetDetail" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.NurseChangeDetailRespVO">
        SELECT
        ecr.id,
        eei.item_name AS originalItemName,
        eei.is_discount AS originalIsDiscount,
        ecr.original_amount AS originalAmount,
        eei.discount_amount AS originalDiscountAmount,
        eei.actual_amount AS originalActualAmount,
        eei.discount AS originalDiscount,
        soc.charge_name,
        ei.elder_name,
        ei.id_card,
        ei.bed_id,
        ecr.original_id,
        ecr.original_name,
        ecr.expect_id,
        ecr.expect_name,
        ecr.change_date,
        ecr.is_private_room,
        ecr.is_discount,
        ecr.reason,
        ecr.elderly_id,
        ecr.change_files,
        ecr.overhead_charge_id,
        ecr.amount,
        ecr.actual_amount,
        ecr.discount_amount,
        ecr.discount,
        esa.id AS syntheticAbilityId,
        esa.questionnaire_name AS processName,
        sep.id AS evaluationProcessId,
        esa.total_score,
        bbf.start_tenant_id
        FROM elderly_change_record ecr
        INNER JOIN bpm_business_form bbf ON bbf.business_id = ecr.id AND bbf.type = 5
        INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
        INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
        INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
        INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id AND scc.identification = 2
        INNER JOIN sys_overhead_charge soc ON soc.id = ecr.overhead_charge_id
        LEFT JOIN sys_evaluation_process sep ON ecr.associate_id = sep.id
        LEFT JOIN elderly_synthetic_ability esa ON sep.id = esa.questionnaire_id AND esa.elder_id = ei.id
        WHERE ecr.current_flag = 0 AND ecr.id = #{id}
        AND ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)
    </select>

    <select id="createCateringChangeGetDetail" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.CateringChangeDetailRespVO">
        SELECT
            ecr.id,
            eei.item_name AS originalItemName,
            eei.is_discount AS originalIsDiscount,
            eei.actual_amount AS originalAmount,
            soc.charge_name,
            ei.elder_name,
            ei.id_card,
            ei.bed_id,
            ecr.original_id,
            ecr.original_name,
            ecr.expect_id,
            ecr.expect_name,
            ecr.change_date,
            ecr.is_private_room,
            ecr.is_discount,
            ecr.reason,
            ecr.elderly_id,
            ecr.change_files,
            ecr.overhead_charge_id,
            ecr.amount,
            ecr.actual_amount,
            ecr.discount_amount,
            ecr.discount,
            bbf.process_instance_id,
            bbf.status
        FROM elderly_change_record ecr
                 INNER JOIN bpm_business_form bbf ON bbf.business_id = ecr.id AND bbf.type = 6
                 INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
                 INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
                 INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
                 INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id AND scc.identification = 3
                 INNER JOIN sys_overhead_charge soc ON soc.id = ecr.overhead_charge_id
        WHERE ecr.current_flag = 0 AND ecr.id = #{id}
          AND ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)
    </select>

    <select id="getCateringDetail" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.CateringChangeDetailRespVO">
        SELECT
        ecr.id,
        eei.item_name AS originalItemName,
        eei.is_discount AS originalIsDiscount,
        soc.price AS originalAmount,
        soc.charge_name,
        ei.elder_name,
        ei.id_card,
        ei.bed_id,
        ecr.original_id,
        ecr.original_name,
        ecr.expect_id,
        ecr.expect_name,
        ecr.change_date,
        ecr.is_private_room,
        ecr.is_discount,
        ecr.reason,
        ecr.elderly_id,
        ecr.change_files,
        ecr.overhead_charge_id,
        ecr.amount,
        ecr.actual_amount,
        ecr.discount_amount,
        ecr.discount,
        bbf.process_instance_id,
        bbf.status
        FROM elderly_change_record ecr
        INNER JOIN bpm_business_form bbf ON bbf.business_id = ecr.id AND bbf.type = 6
        INNER JOIN elderly_info ei ON ecr.elderly_id = ei.id
        INNER JOIN elderly_expense ee ON ei.id = ee.elder_id
        INNER JOIN elderly_expense_item eei ON eei.expense_id = ee.id
        INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id AND scc.identification = 3
        INNER JOIN sys_overhead_charge soc ON soc.id = ecr.overhead_charge_id
        WHERE ecr.current_flag = 0 AND ecr.id = #{id}
        AND ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)
    </select>

    <select id="selectDetail" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.TransferChangeDetailRespVO">
        SELECT
            ecr.id,
            ecr.elderly_id,
            ecr.original_id,
            ecr.original_name,
            ecr.change_date,
            ecr.expect_id,
            ecr.expect_name,
            ecr.associate_id,
            ecr.category_name,
            ecr.overhead_charge_id,
            ecr.amount,
            ecr.actual_amount,
            ecr.discount_amount,
            ecr.discount,
            ecr.is_discount,
            ecr.is_private_room,
            ecr.change_files,
            ecr.reason,
            ecr.current_flag,
            ecr.change_type,
            ecr.create_time,
            ecr.update_time,
            ecr.creator,
            ecr.updater,
            ecr.deleted ,
            bbf.process_instance_id,
            bbf.`status`,
            ei.elder_name,
            ei.id_card
        FROM
            elderly_change_record ecr
                INNER JOIN bpm_business_form bbf ON ecr.id = bbf.business_id AND type = 8
                INNER JOIN elderly_info ei ON ei.id = bbf.business_id2
        WHERE
            ecr.id = #{id}
    </select>

    <select id="selectTransferElderlyPage" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.BedChangeElderlyRespVO">
        SELECT
            ei.id,
            ei.elder_name,
            ei.id_card,
            ei.bed_id,
            ei.tenant_id,
            st.name AS tenantName,
            ei.elder_sex
        FROM
            elderly_info ei
        INNER JOIN system_tenant st ON st.id = ei.tenant_id
       WHERE ei.in_status = 1
            <if test="pageVO.elderName != null">
                AND ei.elder_name LIKE CONCAT('%', #{pageVO.elderName}, '%')
            </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 ei.update_time desc
    </select>


    <select id="selectLastRecordByElderId" resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyChangeRecordDO">
        SELECT
            ecr.change_date
        FROM
            `elderly_change_record` ecr
                INNER JOIN bpm_business_form bbf ON ecr.id = bbf.business_id AND bbf.type = #{bpmType}

        WHERE ecr.change_type = #{changeType}
          AND ecr.elderly_id = #{elderlyId}
          AND bbf.`status` IN (2, 5)
          AND DATE_FORMAT(ecr.change_date, '%Y-%m') = DATE_FORMAT(#{changeDate}, '%Y-%m')
          AND ecr.id != #{id}
        ORDER BY change_date DESC LIMIT 1
    </select>


    <select id="checkBpmBusinessFormById" resultType="java.lang.Integer">
        SELECT count(1)
        FROM elderly_change_record ecr
                 INNER JOIN bpm_business_form bbf ON bbf.business_id = ecr.id AND bbf.type = 4
        WHERE bbf.business_id = #{id}
          AND bbf.type = 4
          AND ecr.current_flag = 0
    </select>


    <select id="getBedNurseChangeOriginById" resultType="cn.iocoder.yudao.module.system.api.order.VO.BedNurseChangeOriginVO">
        SELECT eei.id as expenseItemId,
        soc.id as originalItemId,
        soc.charge_name as originalItemName,
        soc.price as originalAmount,
        eei.change_start_date as changeStartDate
        FROM elderly_expense_item eei
                INNER JOIN elderly_change_record ecr ON ecr.expect_overhead_charge_id = eei.item_id
                INNER JOIN sys_overhead_charge soc ON soc.id = ecr.overhead_charge_id
        WHERE eei.id = #{expenseItemId}
        AND eei.change_start_date &gt;= #{begin}
        AND eei.change_start_date &lt; #{end}
        AND soc.price is not null
        ORDER BY ecr.update_time DESC
        LIMIT 1
    </select>
    <select id="selectLastNurseRecordByElderIds" resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyChangeRecordDO">
        SELECT t.*
        FROM elderly_change_record t
        INNER JOIN (
            SELECT elderly_id, MAX(change_date) AS max_change_date
            FROM elderly_change_record
            WHERE change_type = 1
              AND tenant_id = #{tenantId}
              AND change_date &lt;= #{monthEnd}
              AND elderly_id IN
              <foreach item="id" collection="elderIds" open="(" separator="," close=")">
                  #{id}
              </foreach>
            GROUP BY elderly_id
        ) x ON x.elderly_id = t.elderly_id AND x.max_change_date = t.change_date
        WHERE t.change_type = 1
          AND t.tenant_id = #{tenantId}
    </select>

    <select id="selectLastBedRecordByElderIds" resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyChangeRecordDO">
        SELECT t.*
        FROM elderly_change_record t
        INNER JOIN (
            SELECT elderly_id, MAX(change_date) AS max_change_date
            FROM elderly_change_record
            WHERE change_type = 0
              AND tenant_id = #{tenantId}
              AND change_date &lt;= #{monthEnd}
              AND elderly_id IN
              <foreach item="id" collection="elderIds" open="(" separator="," close=")">
                  #{id}
              </foreach>
            GROUP BY elderly_id
        ) x ON x.elderly_id = t.elderly_id AND x.max_change_date = t.change_date
        WHERE t.change_type = 0
          AND t.tenant_id = #{tenantId}
    </select>

</mapper>
