<?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="com.ruoyi.ccdi.mapper.CcdiIntermediaryMapper">

    <!--
        中介黑名单联合查询
        支持按中介类型筛选: 1=个人中介, 2=实体中介, null=全部
        使用MyBatis Plus分页插件自动处理分页
    -->
    <select id="selectIntermediaryList" resultType="com.ruoyi.ccdi.domain.vo.CcdiIntermediaryVO">
        SELECT * FROM (
            <!-- 查询个人中介 -->
            SELECT
                biz_id as id,
                name,
                person_id as certificate_no,
                '1' as intermediary_type,
                person_type,
                company,
                data_source,
                create_time,
                update_time
            FROM ccdi_biz_intermediary

            UNION ALL

            <!-- 查询实体中介 -->
            SELECT
                social_credit_code as id,
                enterprise_name as name,
                social_credit_code as certificate_no,
                '2' as intermediary_type,
                '实体' as person_type,
                enterprise_name as company,
                data_source,
                create_time,
                update_time
            FROM ccdi_enterprise_base_info
            WHERE risk_level = '1' AND ent_source = 'INTERMEDIARY'
        ) AS combined_result
        <where>
            <!-- 按中介类型筛选 -->
            <if test="query.intermediaryType != null and query.intermediaryType != ''">
                AND intermediary_type = #{query.intermediaryType}
            </if>
            <!-- 按姓名/机构名称模糊查询 -->
            <if test="query.name != null and query.name != ''">
                AND name LIKE CONCAT('%', #{query.name}, '%')
            </if>
            <!-- 按证件号/统一社会信用代码精确查询 -->
            <if test="query.certificateNo != null and query.certificateNo != ''">
                AND certificate_no = #{query.certificateNo}
            </if>
        </where>
        ORDER BY update_time DESC
    </select>

</mapper>
