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

    <resultMap id="CcdiCustFmyRelationVOResult" type="com.ruoyi.info.collection.domain.vo.CcdiCustFmyRelationVO">
        <id property="id" column="id"/>
        <result property="personId" column="person_id"/>
        <result property="relationType" column="relation_type"/>
        <result property="relationName" column="relation_name"/>
        <result property="gender" column="gender"/>
        <result property="birthDate" column="birth_date"/>
        <result property="relationCertType" column="relation_cert_type"/>
        <result property="relationCertNo" column="relation_cert_no"/>
        <result property="mobilePhone1" column="mobile_phone1"/>
        <result property="mobilePhone2" column="mobile_phone2"/>
        <result property="wechatNo1" column="wechat_no1"/>
        <result property="wechatNo2" column="wechat_no2"/>
        <result property="wechatNo3" column="wechat_no3"/>
        <result property="contactAddress" column="contact_address"/>
        <result property="relationDesc" column="relation_desc"/>
        <result property="effectiveDate" column="effective_date"/>
        <result property="invalidDate" column="invalid_date"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <result property="dataSource" column="data_source"/>
        <result property="isEmpFamily" column="is_emp_family"/>
        <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="CcdiCustFmyRelationVOResult">
        SELECT
            r.id, r.person_id, r.relation_type, r.relation_name,
            r.gender, r.birth_date, r.relation_cert_type, r.relation_cert_no,
            r.mobile_phone1, r.mobile_phone2, r.wechat_no1, r.wechat_no2, r.wechat_no3,
            r.contact_address, r.relation_desc, r.effective_date, r.invalid_date,
            r.status, r.remark, r.data_source, r.is_emp_family, r.is_cust_family,
            r.created_by, r.create_time, r.updated_by, r.update_time
        FROM ccdi_cust_fmy_relation r
        WHERE r.is_cust_family = 1
        <if test="query.personId != null and query.personId != ''">
            AND r.person_id LIKE CONCAT('%', #{query.personId}, '%')
        </if>
        <if test="query.relationType != null and query.relationType != ''">
            AND r.relation_type = #{query.relationType}
        </if>
        <if test="query.relationName != null and query.relationName != ''">
            AND r.relation_name LIKE CONCAT('%', #{query.relationName}, '%')
        </if>
        <if test="query.relationCertNo != null and query.relationCertNo != ''">
            AND r.relation_cert_no LIKE CONCAT('%', #{query.relationCertNo}, '%')
        </if>
        ORDER BY r.create_time DESC
    </select>

    <!-- 根据ID查询详情 -->
    <select id="selectRelationById" resultMap="CcdiCustFmyRelationVOResult">
        SELECT
            r.id, r.person_id, r.relation_type, r.relation_name,
            r.gender, r.birth_date, r.relation_cert_type, r.relation_cert_no,
            r.mobile_phone1, r.mobile_phone2, r.wechat_no1, r.wechat_no2, r.wechat_no3,
            r.contact_address, r.relation_desc, r.effective_date, r.invalid_date,
            r.status, r.remark, r.data_source, r.is_emp_family, r.is_cust_family,
            r.created_by, r.create_time, r.updated_by, r.update_time
        FROM ccdi_cust_fmy_relation r
        WHERE r.id = #{id} AND r.is_cust_family = 1 AND 1=1
    </select>

    <!-- 查询已存在的关系(用于导入校验) -->
    <select id="selectExistingRelations" resultType="com.ruoyi.info.collection.domain.CcdiCustFmyRelation">
        SELECT *
        FROM ccdi_cust_fmy_relation
        WHERE is_cust_family = 1
          AND person_id = #{personId}
          AND relation_type = #{relationType}
          AND relation_cert_no = #{relationCertNo}
          AND status = 1
        LIMIT 1
    </select>

    <!-- 批量插入 -->
    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO ccdi_cust_fmy_relation (
            person_id, relation_type, relation_name, gender, birth_date,
            relation_cert_type, relation_cert_no, mobile_phone1, mobile_phone2,
            wechat_no1, wechat_no2, wechat_no3, contact_address, relation_desc,
            status, effective_date, invalid_date, remark, data_source,
            is_emp_family, is_cust_family, created_by, create_time
        ) VALUES
        <foreach collection="relations" item="item" separator=",">
            (
                #{item.personId}, #{item.relationType}, #{item.relationName},
                #{item.gender}, #{item.birthDate}, #{item.relationCertType},
                #{item.relationCertNo}, #{item.mobilePhone1}, #{item.mobilePhone2},
                #{item.wechatNo1}, #{item.wechatNo2}, #{item.wechatNo3},
                #{item.contactAddress}, #{item.relationDesc}, #{item.status},
                #{item.effectiveDate}, #{item.invalidDate}, #{item.remark},
                #{item.dataSource}, #{item.isEmpFamily}, #{item.isCustFamily},
                #{item.createdBy}, #{item.createTime}
            )
        </foreach>
    </insert>

    <!-- 根据证件号码查询关系数量 -->
    <select id="countByCertNo" resultType="int">
        SELECT COUNT(1)
        FROM ccdi_cust_fmy_relation
        WHERE is_cust_family = 1
          AND relation_cert_no = #{relationCertNo}
          AND status = 1
    </select>

    <!-- 批量查询已存在的关系组合(性能优化) -->
    <select id="batchExistsByCombinations" resultType="string">
        SELECT CONCAT(person_id, '|', relation_type, '|', relation_cert_no)
        FROM ccdi_cust_fmy_relation
        WHERE is_cust_family = 1
          AND status = 1
          AND CONCAT(person_id, '|', relation_type, '|', relation_cert_no) IN
        <foreach collection="combinations" item="combo" open="(" separator="," close=")">
            #{combo}
        </foreach>
    </select>

</mapper>
