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


    <select id="getChargeCategory"
            resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.BedChangeCategoryVO">
        SELECT
            soc.id AS overheadChargeId,
            eei.item_category_id AS itemCategoryId,
            soc.charge_name AS "itemName",
            eei.actual_amount,
            ee.id AS "expenseId"
        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 AND scc.identification = #{identification}
                INNER JOIN sys_overhead_charge soc ON soc.id = eei.item_id
        WHERE ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)
          AND ei.id = #{elderlyId}
        ORDER BY ee.id DESC
        LIMIT 1
    </select>

    <select id="getChargeCategoryList" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.BedChangeCategoryVO">
        SELECT distinct
            soc.id AS overheadChargeId,
            eei.item_category_id AS itemCategoryId,
            soc.charge_name AS "itemName",
            eei.actual_amount,
            ee.id AS "expenseId"
        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 sys_overhead_charge soc ON soc.id = eei.item_id
        WHERE ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = ei.id)
          AND ei.id = #{elderlyId}
        ORDER BY ee.id DESC
    </select>

</mapper>