<?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.info.collection.mapper.CcdiIntermediaryMapper">

    <!-- 中介综合库联合查询 -->
    <select id="selectIntermediaryList" resultType="com.ruoyi.info.collection.domain.vo.CcdiIntermediaryVO">
        SELECT * FROM (
            SELECT
                CAST('INTERMEDIARY' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS record_type,
                biz_id COLLATE utf8mb4_general_ci AS record_id,
                name COLLATE utf8mb4_general_ci AS name,
                person_id COLLATE utf8mb4_general_ci AS certificate_no,
                name COLLATE utf8mb4_general_ci AS related_intermediary_name,
                CAST('本人' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS relation_text,
                person_id COLLATE utf8mb4_general_ci AS related_intermediary_certificate_no,
                create_time
            FROM ccdi_biz_intermediary
            WHERE person_sub_type COLLATE utf8mb4_general_ci = '本人' COLLATE utf8mb4_general_ci

            UNION ALL

            SELECT
                CAST('RELATIVE' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS record_type,
                child.biz_id COLLATE utf8mb4_general_ci AS record_id,
                child.name COLLATE utf8mb4_general_ci AS name,
                child.person_id COLLATE utf8mb4_general_ci AS certificate_no,
                parent.name COLLATE utf8mb4_general_ci AS related_intermediary_name,
                child.person_sub_type COLLATE utf8mb4_general_ci AS relation_text,
                parent.person_id COLLATE utf8mb4_general_ci AS related_intermediary_certificate_no,
                child.create_time
            FROM ccdi_biz_intermediary child
            INNER JOIN ccdi_biz_intermediary parent
                ON child.related_num_id COLLATE utf8mb4_general_ci = parent.person_id COLLATE utf8mb4_general_ci
               AND parent.person_sub_type COLLATE utf8mb4_general_ci = '本人' COLLATE utf8mb4_general_ci
            WHERE child.person_sub_type IS NOT NULL
              AND child.person_sub_type COLLATE utf8mb4_general_ci != '本人' COLLATE utf8mb4_general_ci

            UNION ALL

            SELECT
                CAST('ENTERPRISE_RELATION' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS record_type,
                CAST(rel.id AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS record_id,
                COALESCE(
                    ent.enterprise_name COLLATE utf8mb4_general_ci,
                    rel.social_credit_code COLLATE utf8mb4_general_ci
                ) COLLATE utf8mb4_general_ci AS name,
                rel.social_credit_code COLLATE utf8mb4_general_ci AS certificate_no,
                parent.name COLLATE utf8mb4_general_ci AS related_intermediary_name,
                COALESCE(
                    NULLIF(rel.relation_person_post COLLATE utf8mb4_general_ci, ''),
                    CAST('实体' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci
                ) COLLATE utf8mb4_general_ci AS relation_text,
                parent.person_id COLLATE utf8mb4_general_ci AS related_intermediary_certificate_no,
                rel.create_time
            FROM ccdi_intermediary_enterprise_relation rel
            INNER JOIN ccdi_biz_intermediary parent
                ON rel.intermediary_biz_id COLLATE utf8mb4_general_ci = parent.biz_id COLLATE utf8mb4_general_ci
               AND parent.person_sub_type COLLATE utf8mb4_general_ci = '本人' COLLATE utf8mb4_general_ci
            LEFT JOIN ccdi_enterprise_base_info ent
                ON rel.social_credit_code COLLATE utf8mb4_general_ci = ent.social_credit_code COLLATE utf8mb4_general_ci
        ) AS combined_result
        <where>
            <if test="query.recordType != null and query.recordType != ''">
                AND record_type COLLATE utf8mb4_general_ci =
                    CONVERT(#{query.recordType} USING utf8mb4) COLLATE utf8mb4_general_ci
            </if>
            <if test="query.name != null and query.name != ''">
                AND name COLLATE utf8mb4_general_ci LIKE
                    CONCAT('%', CONVERT(#{query.name} USING utf8mb4), '%') COLLATE utf8mb4_general_ci
            </if>
            <if test="query.certificateNo != null and query.certificateNo != ''">
                AND certificate_no COLLATE utf8mb4_general_ci =
                    CONVERT(#{query.certificateNo} USING utf8mb4) COLLATE utf8mb4_general_ci
            </if>
            <if test="query.relatedIntermediaryKeyword != null and query.relatedIntermediaryKeyword != ''">
                AND (
                    related_intermediary_name COLLATE utf8mb4_general_ci LIKE
                        CONCAT('%', CONVERT(#{query.relatedIntermediaryKeyword} USING utf8mb4), '%') COLLATE utf8mb4_general_ci
                    OR related_intermediary_certificate_no COLLATE utf8mb4_general_ci LIKE
                        CONCAT('%', CONVERT(#{query.relatedIntermediaryKeyword} USING utf8mb4), '%') COLLATE utf8mb4_general_ci
                )
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

</mapper>
