<?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.SysOverheadChargeMapper">
    <select id="queryPage" resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.OverheadChargeDO">
        select oc.* from sys_overhead_charge oc inner join sys_charge_category cc on cc.id = oc.superiors_id where 1 = 1
        <if test="params.chargeName != null and params.chargeName != ''">
            and oc.charge_name = #{params.chargeName}
        </if>
        <if test="params.status != null">
            and oc.status = #{params.status}
        </if>
        <if test="params.categoryId != null">
            and cc.id = #{params.categoryId}
        </if>
        <if test="params.tenantIds != null">
            and oc.tenant_id in
            <foreach item="id" collection="params.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        order by oc.create_date desc
    </select>

    <select id="getListByElderlyId" resultType="cn.iocoder.yudao.module.system.controller.admin.sys.vo.ElderOverheadChargeRespVO">
        SELECT DISTINCT
            scc.id AS chargeCategoryId,
            scc.name,
            soc.id AS overheadChargeId,
            soc.charge_name,
            CAST(
                    eei.actual_amount AS DECIMAL ( 10, 2 )) AS  amount
        FROM
            elderly_expense ee
                INNER JOIN elderly_expense_item eei ON ee.id = eei.expense_id
                INNER JOIN sys_overhead_charge soc ON soc.id = eei.item_id
                INNER JOIN sys_charge_category scc ON scc.id = eei.item_category_id
        WHERE soc.charge_type = 'month'
          AND ee.elder_id = #{elderlyId}
          AND ee.created_time = (SELECT MAX(ee2.created_time) FROM elderly_expense ee2 WHERE ee2.elder_id = #{elderlyId})
    </select>

    <select id="getListByChargeCategoryId" resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.OverheadChargeDO">
        SELECT
            id,
            charge_name,
            price
        FROM
            sys_overhead_charge
        WHERE
            superiors_id = #{chargeCategoryId}
          AND charge_type = 'month'
    </select>
    <select id="selectListByTenantIdsAndType"
            resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.OverheadChargeDO">
        SELECT
            soc.*
        FROM
            sys_overhead_charge soc
                INNER JOIN sys_charge_category scc ON soc.superiors_id = scc.id
                AND soc.tenant_id IN
                    <foreach item="tenantId" collection="array" open="(" separator="," close=")">
                        #{tenantId}
                    </foreach>
                AND scc.identification NOT IN ( 1, 2, 3, 7 )


    </select>


</mapper>
