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

    <select id="selectPageWithJoin" resultType="cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceOrderPageRespVO">
        select
            o.id,
            o.order_type as orderType,
            hei.elder_name as homeElderName,
            hei.sex as sex,
            TIMESTAMPDIFF(YEAR, hei.birthday, CURDATE()) AS age,
            hei.phone as phone,
            CONCAT_WS(' ', hei.current_live_area, hei.current_live_address) as address,
            o.service_member_id as serviceMemberId,
            m.name as serviceMemberName,
            o.order_time as orderTime,
            o.service_start_time as serviceStartTime,
            o.service_end_time as serviceEndTime,
            o.service_duration as serviceDuration,
            o.remark,
            o.project_amount as projectAmount,
            o.subsidy_amount as subsidyAmount,
            o.self_pay_amount as selfPayAmount,
            o.type,
            o.subsidy_arrival_date,
            o.status,
            o.tenant_id as tenantId,
            o.create_time as createTime,
            o.update_time as updateTime,
            o.creator,
            o.updater
        from home_service_order o
        left join home_service_member m on m.id = o.service_member_id
        left join home_elderly_info hei on hei.id = o.home_elder_id
        where 1 = 1
        <if test="params.elderName != null and params.elderName != ''">
            and hei.elder_name like concat('%', #{params.elderName}, '%')
        </if>
        <if test="params.serviceMemberName != null and params.serviceMemberName != ''">
            and m.name like concat('%', #{params.serviceMemberName}, '%')
        </if>
        <if test="params.orderType != null">
            and o.order_type = #{params.orderType}
        </if>
        <if test="params.serviceMemberId != null">
            and o.service_member_id = #{params.serviceMemberId}
        </if>
        <if test="params.type != null">
            and o.type = #{params.type}
        </if>
        <if test="params.tenantId != null">
            and o.tenant_id = #{params.tenantId}
        </if>
        <if test="params.tenantIds != null and params.tenantIds.length > 0">
            and o.tenant_id in
            <foreach collection="params.tenantIds" item="tenantId" open="(" separator="," close=")">
                #{tenantId}
            </foreach>
        </if>
        order by o.id desc
    </select>

</mapper>

