<?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.gic.haoban.manage.service.dao.mapper.ExternalUseridLogMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabExternalUseridLog">
        <result column="id" property="id"/>
        <result column="old_external_user_id" property="oldExternalUserId"/>
        <result column="new_external_user_id" property="newExternalUserId"/>
        <result column="wx_enterprise_id" property="wxEnterpriseId"/>
        <result column="status_flag" property="statusFlag"/>
        <result column="creator_id" property="creatorId"/>
        <result column="creator_name" property="creatorName"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        id,
                old_external_user_id,
                new_external_user_id,
                wx_enterprise_id,
                status_flag,
                creator_id,
                creator_name,
                create_time,
                update_time
    </sql>


    <insert id="insertBatch">
        insert into tab_external_userid_log (id,
        old_external_user_id,
        new_external_user_id,
        wx_enterprise_id,
        status_flag,
        creator_id,
        creator_name,
        create_time,
        update_time)
        values
        <foreach collection="list" separator="," item="item">
            (#{item.id}, #{item.oldExternalUserId},#{item.newExternalUserId}, #{item.wxEnterpriseId}, 1, #{item.creatorId},
            #{item.creatorName},now(),now())
        </foreach>
    </insert>


    <update id="update" parameterType="com.gic.haoban.manage.service.entity.TabExternalUseridLog">
        UPDATE tab_external_userid_log
        <set>
            <if test="null != oldExternalUserId and '' != oldExternalUserId">
                old_external_user_id = #{oldExternalUserId},
            </if>
            <if test="null != newExternalUserId and '' != newExternalUserId">
                new_external_user_id = #{newExternalUserId},
            </if>
            <if test="null != wxEnterpriseId and '' != wxEnterpriseId">
                wx_enterprise_id = #{wxEnterpriseId},
            </if>
            <if test="null != statusFlag ">
                status_flag = #{statusFlag},
            </if>
            <if test="null != creatorId and '' != creatorId">
                creator_id = #{creatorId},
            </if>
            <if test="null != creatorName and '' != creatorName">
                creator_name = #{creatorName},
            </if>
            <if test="null != createTime ">
                create_time = #{createTime},
            </if>
            <if test="null != updateTime ">
                update_time = #{updateTime}
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <select id="selectByWxEnterpriseId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_external_userid_log
        where wx_enterprise_id = #{wxEnterpriseId} and status_flag = 1
    </select>

    <update id="updateStatusFlagByWxEnterpriseId">
        update tab_external_userid_log
        set status_flag = #{statusFlag},
            update_time = now()
        where wx_enterprise_id = #{wxEnterpriseId}
    </update>

    <update id="updateOldExternalUserId">
        UPDATE tab_haoban_external_clerk_related a
            JOIN tab_external_userid_log b ON a.wx_enterprise_id = b.wx_enterprise_id
                AND a.external_user_id = b.new_external_user_id
        SET a.external_user_id = b.old_external_user_id,a.update_time = now()
        WHERE
            a.wx_enterprise_id = #{wxEnterpriseId}
          AND b.wx_enterprise_id = #{wxEnterpriseId}
          AND a.status_flag = 1 and b.status_flag = 1;
    </update>

</mapper>