<?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.dao.UpdateUserIdDao">
    <resultMap id="UpdateUserIdMap" type="com.zm.order.tool.server.model.UpdateUserId">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="before_user_id" jdbcType="BIGINT" property="beforeUserId"/>
        <result column="after_user_id" jdbcType="BIGINT" property="afterUserId"/>
        <result column="operate_at" jdbcType="TIMESTAMP" property="operateAt"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
        <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
        <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
        <result column="features" jdbcType="VARCHAR" property="features"/>
    </resultMap>


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

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        user_id, order_no, before_user_id, after_user_id , features, operate_at, status, phone, created_at, updated_at, is_deleted
    </sql>

    <sql id="values">
       #{id}, #{userId}, #{orderNo}, #{beforeUserId}, #{afterUserId}, #{features},
      now(), #{status}, #{phone}, now(), now(), 0
  </sql>


    <insert id="insert" parameterType="com.zm.order.tool.server.model.UpdateUserId" keyColumn="id" keyProperty="id"
            useGeneratedKeys="true">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        (<include refid="values"/>)
    </insert>


    <update id="updateStatus" parameterType="map">
        update
        <include refid="tb"/>
        set
        status = 2,
        updated_at =now()
        where
        id =#{id}
    </update>

    <update id="updatePhone" parameterType="map">
        update
        <include refid="tb"/>
        set
        phone = #{phone},
        updated_at =now()
        where
        id =#{id}
    </update>


    <update id="updateUserId" parameterType="map">
        update
        <include refid="tb"/>
        set
        after_user_id = #{userId},
        status = 1,
        updated_at =now()
        where
        id =#{id}
    </update>

    <select id="selectList" parameterType="map" resultMap="UpdateUserIdMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            id > #{lastId}
            and is_deleted =0
        </where>
        order by id asc
        limit 200
    </select>

    <select id="selectList1" parameterType="map" resultMap="UpdateUserIdMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            id > #{lastId}
            <if test="endId != null">
                <![CDATA[ and id <= #{endId} ]]>
            </if>
            and is_deleted =0
        </where>
        order by id asc
        limit 200
    </select>


    <select id="selectListByOrderNo" parameterType="list" resultMap="UpdateUserIdMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            order_no in
            <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                #{orderNo}
            </foreach>
            and is_deleted =0
        </where>
        limit 200
    </select>

    <insert id="inserts" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="updateUserIdList" item="updateUserId" index="index" separator=",">
            (
            #{updateUserId.id}, #{updateUserId.userId}, #{updateUserId.orderNo}, #{updateUserId.beforeUserId},
            #{updateUserId.afterUserId}, #{updateUserId.features},
            now(), #{updateUserId.status}, #{updateUserId.phone}, now(), now(), 0
            )
        </foreach>
    </insert>


</mapper>