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

    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO ccdi_intermediary_enterprise_relation (
            intermediary_biz_id, social_credit_code, relation_person_post, remark,
            created_by, updated_by, create_time, update_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.intermediaryBizId}, #{item.socialCreditCode}, #{item.relationPersonPost}, #{item.remark},
                #{item.createdBy}, #{item.updatedBy}, NOW(), NOW()
            )
        </foreach>
    </insert>

    <resultMap id="CcdiIntermediaryEnterpriseRelationVOResult"
               type="com.ruoyi.info.collection.domain.vo.CcdiIntermediaryEnterpriseRelationVO">
        <id property="id" column="id"/>
        <result property="intermediaryBizId" column="intermediary_biz_id"/>
        <result property="intermediaryName" column="intermediary_name"/>
        <result property="intermediaryPersonId" column="intermediary_person_id"/>
        <result property="socialCreditCode" column="social_credit_code"/>
        <result property="enterpriseName" column="enterprise_name"/>
        <result property="relationPersonPost" column="relation_person_post"/>
        <result property="remark" column="remark"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <select id="selectByIntermediaryBizId" resultMap="CcdiIntermediaryEnterpriseRelationVOResult">
        SELECT
            rel.id,
            rel.intermediary_biz_id,
            parent.name AS intermediary_name,
            parent.person_id AS intermediary_person_id,
            rel.social_credit_code,
            ent.enterprise_name,
            rel.relation_person_post,
            rel.remark,
            rel.create_time
        FROM ccdi_intermediary_enterprise_relation rel
        INNER JOIN ccdi_biz_intermediary parent
            ON rel.intermediary_biz_id = parent.biz_id
        LEFT JOIN ccdi_enterprise_base_info ent
            ON rel.social_credit_code = ent.social_credit_code
        WHERE rel.intermediary_biz_id = #{bizId}
        ORDER BY rel.create_time DESC
    </select>

    <select id="selectDetailById" resultMap="CcdiIntermediaryEnterpriseRelationVOResult">
        SELECT
            rel.id,
            rel.intermediary_biz_id,
            parent.name AS intermediary_name,
            parent.person_id AS intermediary_person_id,
            rel.social_credit_code,
            ent.enterprise_name,
            rel.relation_person_post,
            rel.remark,
            rel.create_time
        FROM ccdi_intermediary_enterprise_relation rel
        INNER JOIN ccdi_biz_intermediary parent
            ON rel.intermediary_biz_id = parent.biz_id
        LEFT JOIN ccdi_enterprise_base_info ent
            ON rel.social_credit_code = ent.social_credit_code
        WHERE rel.id = #{id}
    </select>

    <select id="existsByIntermediaryBizIdAndSocialCreditCode" resultType="boolean">
        SELECT COUNT(1) > 0
        FROM ccdi_intermediary_enterprise_relation
        WHERE intermediary_biz_id = #{bizId}
          AND social_credit_code = #{socialCreditCode}
    </select>

    <select id="batchExistsByCombinations" resultType="string">
        SELECT CONCAT(intermediary_biz_id, '|', social_credit_code)
        FROM ccdi_intermediary_enterprise_relation
        WHERE CONCAT(intermediary_biz_id, '|', social_credit_code) IN
        <foreach collection="combinations" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

</mapper>
