<?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.home.HomeElderlyInfoMapper">

    <select id="selectPageWithAccountInfo" resultType="cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeElderlyInfoPageRespVO">
        select
            hei.id,
            hei.elder_name as elderName,
            hei.sex,
            case
                when hei.birthday is null then null
                else timestampdiff(year, hei.birthday, curdate())
            end as age,
            hei.phone,
            hei.telephone,
            hei.self_care_situation as selfCareSituation,
            hei.live_situation as liveSituation,
            concat('代金券：', ifnull(hea.coupon_amount, 0), '元，现金：', ifnull(hea.cash_amount, 0), '元') as accountBalance,
            hei.is_service as isService,
            hei.service_times_year as serviceTimesYear,
            hei.current_live_address as currentLiveAddress,
            hei.check_in as checkIn
        from home_elderly_info hei
        left join (
            select
                a.home_elder_id,
                sum(case when d.type = '1' then ifnull(d.amount, 0) else 0 end) as cash_amount,
                sum(case when d.type = '2' then ifnull(d.amount, 0) else 0 end) as coupon_amount
            from home_elderly_account a
            left join home_elderly_account_detail d on d.account_id = a.id
            group by a.home_elder_id
        ) hea on hea.home_elder_id = hei.id
        where 1 = 1
        <if test="params.elderName != null and params.elderName != ''">
            and hei.elder_name like concat('%', #{params.elderName}, '%')
        </if>
        <if test="params.idCard != null and params.idCard != ''">
            and hei.id_card like concat('%', #{params.idCard}, '%')
        </if>
        <if test="params.phone != null and params.phone != ''">
            and hei.phone like concat('%', #{params.phone}, '%')
        </if>
        <if test="params.tenantId != null">
            and hei.tenant_id = #{params.tenantId}
        </if>
        <if test="params.tenantIds != null and params.tenantIds.length > 0">
            and hei.tenant_id in
            <foreach collection="params.tenantIds" item="tenantId" open="(" separator="," close=")">
                #{tenantId}
            </foreach>
        </if>
        order by hei.id desc
    </select>

    <select id="selectElderlyMealPage" resultType="cn.iocoder.yudao.module.system.controller.admin.yk.vo.YkElderlyMealRespVO">
        select
            hei.id,
            hei.elder_name as elderName,
            hei.phone,
            hei.current_live_address as deliveryAddress,
            hde.delivery_date as deliveryDate,
            hde.meal_standard as mealStandard,
            hde.meal_type as mealType,
            hde.delivery_person_id as deliveryPersonId,
            hde.delivery_person_name as deliveryPersonName,
            hde.created_time as createdTime
        from home_elderly_info hei
        inner join yk_elderly_delivery hde on hde.yk_elder_id = hei.id
        where 1 = 1
        <if test="pageReqVO.elderName != null and pageReqVO.elderName != ''">
            and hei.elder_name like concat('%', #{pageReqVO.elderName}, '%')
        </if>
        <if test="pageReqVO.phone != null and pageReqVO.phone != ''">
            and hei.phone like concat('%', #{pageReqVO.phone}, '%')
        </if>
        <if test="pageReqVO.deliveryDate != null">
            and hde.delivery_date = #{pageReqVO.deliveryDate}
        </if>
        <if test="pageReqVO.tenantId != null">
            and hei.tenant_id = #{pageReqVO.tenantId}
        </if>
        order by hde.delivery_date desc, hde.id desc
    </select>

</mapper>

