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

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

    <select id="getDailyExpensesPage"
            resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.DailyExpensesDO">
        select ede.*,ei.elder_name,ei.id_card
            from elderly_daily_expenses ede
            left join elderly_info ei on ei.id = ede.elder_id
                LEFT JOIN org_build_floor obf ON ei.floor_id = obf.id
        <where>
            1 = 1
            <if test="params.elderName != null and params.elderName != ''">
                and ei.elder_name like concat('%',#{params.elderName} ,'%')
            </if>
            <if test="params.bedId != null">
                and ei.bed_id = #{params.bedId}
            </if>
            <if test="params.status != null">
                and ede.status = #{params.status}
            </if>
            <if test="params.type != null">
                and ede.type = #{params.type}
            </if>
            <if test="params.createdBy != null and params.createdBy != ''">
                and ede.created_by like concat('%',#{params.createdBy} ,'%')
            </if>
            <if test="params.floorName != null and params.floorName != ''">
                and obf.floor_name like concat('%',#{params.floorName} ,'%')
            </if>
            <if test="params.itemName != null and params.itemName != ''">
                and ede.item_name like concat('%',#{params.itemName} ,'%')
            </if>
            <if test="params.billOrderNumber != null and params.billOrderNumber != ''">
                and ede.expense_order_number = #{params.billOrderNumber}
            </if>
            <if test="params.attributionBillTime != null and params.attributionBillTime != ''">
                and ede.attribution_bill_time = #{params.attributionBillTime}
            </if>
            <if test="params.createTime != null">
                and ede.created_time between #{params.createTime[0]} and #{params.createTime[1]}
            </if>
            <if test="params.orgType != null">
                and ei.org_type = #{params.orgType}
            </if>
            <if test="params.tenantIds != null">
                and ei.tenant_id in
                <foreach item="id" collection="params.tenantIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by ede.accounting_time desc
    </select>
    <select id="getMedicalDailyExpenses"
            resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.DailyExpensesDO">
        select ede.*,ei.elder_name,ei.id_card
        from elderly_daily_expenses ede
        left join elderly_info ei on ei.id = ede.elder_id
        LEFT JOIN org_build_floor obf ON ei.floor_id = obf.id
        <where>
            1 = 1
            <if test="params.elderName != null and params.elderName != ''">
                and ei.elder_name like concat('%',#{params.elderName} ,'%')
            </if>
            <if test="params.bedId != null">
                and ei.bed_id = #{params.bedId}
            </if>
            <if test="params.itemCategory != null and params.itemCategory!= ''">
                and ede.item_category like concat('%',#{params.itemCategory} ,'%')
            </if>
            <if test="params.status != null">
                and ede.status = #{params.status}
            </if>
            <if test="params.type != null">
                and ede.type = #{params.type}
            </if>
            <if test="params.createdBy != null and params.createdBy != ''">
                and ede.created_by like concat('%',#{params.createdBy} ,'%')
            </if>
            <if test="params.floorName != null and params.floorName != ''">
                and obf.floor_name like concat('%',#{params.floorName} ,'%')
            </if>
            <if test="params.itemName != null and params.itemName != ''">
                and ede.item_name like concat('%',#{params.itemName} ,'%')
            </if>
            <if test="params.billOrderNumber != null and params.billOrderNumber != ''">
                and ede.expense_order_number = #{params.billOrderNumber}
            </if>
            <if test="params.attributionBillTime != null and params.attributionBillTime != ''">
                and ede.attribution_bill_time = #{params.attributionBillTime}
            </if>
            <if test="params.createTime != null">
                and ede.created_time between #{params.createTime[0]} and #{params.createTime[1]}
            </if>
        </where>
        order by ede.accounting_time desc
    </select>
    <select id="selectServiceList"
            resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyHomeService">
        SELECT
            ede.item_name,
            COUNT(*) AS serviceCount
        FROM
            elderly_daily_expenses ede
                INNER JOIN sys_overhead_charge sov ON ede.item_id = sov.id
                INNER JOIN sys_charge_category scc ON scc.id = sov.superiors_id AND scc.parent_id = #{chargeCategoryId}
                INNER JOIN system_tenant st ON ede.tenant_id = st.id
        WHERE
            ede.tenant_id = #{tenantId}
        GROUP BY
            ede.item_name
        ORDER BY
            serviceCount DESC
        LIMIT 3
    </select>


</mapper>