<?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.zm.order.tool.server.tempdb.UserIdRoutingDao">

    <resultMap id="OrderItemMap" type="com.zm.order.tool.server.model.UserIdRouting">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="db_index" jdbcType="INTEGER" property="dbIndex"/>
        <result column="tb_index" jdbcType="INTEGER" property="tbIndex"/>
    </resultMap>

    <sql id="tb">
        user_id_routing
    </sql>

    <sql id="cols_all">
        id,
        user_id,
        db_index,
        tb_index
    </sql>

    <select id="selectList" parameterType="map" resultMap="OrderItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="lastId != null">
                id > #{lastId}
            </if>
        </where>
        order by id asc limit 1000
    </select>

    <update id="update" parameterType="list">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="db_index = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.dbIndex}
                </foreach>
            </trim>
            <trim prefix="tb_index = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.tbIndex}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="order" separator="," open="(" close=")">
            #{order.id}
        </foreach>
    </update>
</mapper>