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

    <!-- 信贷客户实体关联信息ResultMap -->
    <resultMap type="com.ruoyi.info.collection.domain.vo.CcdiCustEnterpriseRelationVO" id="CcdiCustEnterpriseRelationVOResult">
        <id property="id" column="id"/>
        <result property="personId" column="person_id"/>
        <result property="relationPersonPost" column="relation_person_post"/>
        <result property="socialCreditCode" column="social_credit_code"/>
        <result property="enterpriseName" column="enterprise_name"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <result property="dataSource" column="data_source"/>
        <result property="isEmployee" column="is_employee"/>
        <result property="isEmpFamily" column="is_emp_family"/>
        <result property="isCustomer" column="is_customer"/>
        <result property="isCustFamily" column="is_cust_family"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="createdBy" column="created_by"/>
        <result property="updatedBy" column="updated_by"/>
    </resultMap>

    <!-- 分页查询信贷客户实体关联列表 -->
    <select id="selectRelationPage" resultMap="CcdiCustEnterpriseRelationVOResult">
        SELECT
            id, person_id, relation_person_post,
            social_credit_code, enterprise_name, status, remark,
            data_source, is_employee, is_emp_family, is_customer,
            is_cust_family, created_by, create_time, updated_by,
            update_time
        FROM ccdi_cust_enterprise_relation
        <where>
            <if test="query.personId != null and query.personId != ''">
                AND person_id LIKE CONCAT('%', #{query.personId}, '%')
            </if>
            <if test="query.socialCreditCode != null and query.socialCreditCode != ''">
                AND social_credit_code LIKE CONCAT('%', #{query.socialCreditCode}, '%')
            </if>
            <if test="query.enterpriseName != null and query.enterpriseName != ''">
                AND enterprise_name LIKE CONCAT('%', #{query.enterpriseName}, '%')
            </if>
            <if test="query.status != null">
                AND status = #{query.status}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <!-- 查询信贷客户实体关联详情 -->
    <select id="selectRelationById" resultMap="CcdiCustEnterpriseRelationVOResult">
        SELECT
            id, person_id, relation_person_post,
            social_credit_code, enterprise_name, status, remark,
            data_source, is_employee, is_emp_family, is_customer,
            is_cust_family, created_by, create_time, updated_by,
            update_time
        FROM ccdi_cust_enterprise_relation
        WHERE id = #{id}
    </select>

    <!-- 判断身份证号和统一社会信用代码的组合是否已存在 -->
    <select id="existsByPersonIdAndSocialCreditCode" resultType="boolean">
        SELECT COUNT(1) > 0
        FROM ccdi_cust_enterprise_relation
        WHERE person_id = #{personId}
          AND social_credit_code = #{socialCreditCode}
    </select>

    <!-- 批量查询已存在的person_id + social_credit_code组合 -->
    <!-- 优化导入性能：一次性查询所有组合，避免N+1查询问题 -->
    <select id="batchExistsByCombinations" resultType="string">
        SELECT CONCAT(person_id, '|', social_credit_code) AS combination
        FROM ccdi_cust_enterprise_relation
        WHERE CONCAT(person_id, '|', social_credit_code) IN
        <foreach collection="combinations" item="combination" open="(" separator="," close=")">
            #{combination}
        </foreach>
    </select>

    <!-- 批量插入信贷客户实体关联数据 -->
    <insert id="insertBatch">
        INSERT INTO ccdi_cust_enterprise_relation
        (person_id, relation_person_post, social_credit_code, enterprise_name,
         status, remark, data_source, is_employee, is_emp_family, is_customer, is_cust_family,
         created_by, create_time, updated_by, update_time)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.personId}, #{item.relationPersonPost}, #{item.socialCreditCode}, #{item.enterpriseName},
            #{item.status}, #{item.remark}, #{item.dataSource}, #{item.isEmployee}, #{item.isEmpFamily}, #{item.isCustomer}, #{item.isCustFamily},
            #{item.createdBy}, NOW(), #{item.updatedBy}, NOW())
        </foreach>
    </insert>

</mapper>
