<?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.fs.qw.mapper.QwExternalContactBlackLogMapper">

    <resultMap type="QwExternalContactBlackLog" id="QwExternalContactBlackLogResult">
        <result property="id"    column="id"    />
        <result property="externalUserId"    column="external_user_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="status"    column="status"    />
        <result property="blackStatus"    column="black_status"    />
    </resultMap>

    <sql id="selectQwExternalContactBlackLogVo">
        select id, external_user_id, company_user_id, create_time, status, black_status from qw_external_contact_black_log
    </sql>

    <select id="selectQwExternalContactBlackLogList" parameterType="QwExternalContactBlackLog" resultMap="QwExternalContactBlackLogResult">
        <include refid="selectQwExternalContactBlackLogVo"/>
        <where>
            <if test="id != null "> and id = #{id}</if>
            <if test="externalUserId != null  and externalUserId != ''"> and external_user_id = #{externalUserId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="blackStatus != null "> and black_status = #{blackStatus}</if>
        </where>
    </select>

    <select id="selectQwExternalContactBlackLogById" parameterType="Long" resultMap="QwExternalContactBlackLogResult">
        <include refid="selectQwExternalContactBlackLogVo"/>
        where id = #{id}
    </select>

    <insert id="insertQwExternalContactBlackLog" parameterType="QwExternalContactBlackLog" useGeneratedKeys="true" keyProperty="id">
        insert into qw_external_contact_black_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="externalUserId != null">external_user_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="status != null">status,</if>
            <if test="blackStatus != null">black_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="externalUserId != null">#{externalUserId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="status != null">#{status},</if>
            <if test="blackStatus != null">#{blackStatus},</if>
         </trim>
    </insert>

    <update id="updateQwExternalContactBlackLog" parameterType="QwExternalContactBlackLog">
        update qw_external_contact_black_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="externalUserId != null">external_user_id = #{externalUserId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="blackStatus != null">black_status = #{blackStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQwExternalContactBlackLogById" parameterType="Long">
        delete from qw_external_contact_black_log where id = #{id}
    </delete>

    <delete id="deleteQwExternalContactBlackLogByIds" parameterType="String">
        delete from qw_external_contact_black_log where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <insert id="insertLogBySelectExternalContact" parameterType="QwExternalContactBlackLog" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO qw_external_contact_black_log ( external_user_id, company_user_id, create_time, STATUS, black_status )
        SELECT
            id,company_user_id,
            NOW(),
            STATUS,
            7
        FROM
            qw_external_contact
        WHERE qw_external_contact.id in
        <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
             #{id}
        </foreach>
    </insert>

    <select id="selectContactLogByIds" resultType="QwExternalContact">
        SELECT
        qw_external_contact.id,
        qw_external_contact.STATUS
        from qw_external_contact_black_log
        where external_user_id in
        <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
            (#{id})
        </foreach>
    </select>

</mapper>
