<?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="org.jeecg.modules.system.app.mapper.AppContractSignMapper">

    <select id="queryUserSignContractList" resultType="org.jeecg.modules.system.app.vo.ContractSignVO">
        SELECT DISTINCT
        a.id,
        o.product_name,
        c.full_name,
        c.phone,
        a.create_time,
        a.sign_time,
        a.is_sign,
        a.sign_flow_id
        FROM nm_contract_sign a
        LEFT JOIN nm_family_members c ON a.family_id = c.id
        LEFT JOIN nm_contract_info b ON a.org_code = b.org_code
        LEFT JOIN (
        SELECT order_id, MAX(product_name) AS product_name
        FROM nm_order_pro_info where type = 5
        GROUP BY order_id
        ) o ON a.order_id = o.order_id
        <where>
            1=1 AND a.org_code LIKE CONCAT(#{contractSignDTO.orgCode}, '%')
            <!-- 家庭成员姓名模糊查询 -->
            <if test="contractSignDTO.fullName != null and contractSignDTO.fullName != ''">
                AND c.full_name LIKE CONCAT('%', #{contractSignDTO.fullName}, '%')
            </if>
            <if test="contractSignDTO.productName != null and contractSignDTO.productName != ''">
                AND o.product_name LIKE CONCAT('%', #{contractSignDTO.productName}, '%')
            </if>

            <!-- 联系电话模糊查询 -->
            <if test="contractSignDTO.phone != null and contractSignDTO.phone != ''">
                AND c.phone LIKE CONCAT('%', #{contractSignDTO.phone}, '%')
            </if>

            <!-- 签署状态精确匹配 -->
            <if test="contractSignDTO.isSign != null">
                AND a.is_sign = #{contractSignDTO.isSign}
            </if>

            <!-- 签约完成时间范围查询 -->
            <if test="contractSignDTO.startTime != null">
                AND a.sign_time &gt;= #{contractSignDTO.startTime}
            </if>
            <if test="contractSignDTO.endTime != null">
                AND a.sign_time &lt;= #{contractSignDTO.endTime}
            </if>
        </where>
    </select>


</mapper>