<?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.old.routing.OldHintOrderExtendsDao">

    <resultMap id="OrderExtendsMap" type="com.zm.order.tool.server.model.OrderExtends">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="third_features" jdbcType="VARCHAR" property="thirdFeatures"/>
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
        <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
        <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
    </resultMap>

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

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

    <sql id="cols_exclude_id">
         order_no, user_id, third_features, created_at, updated_at, is_deleted
    </sql>

    <sql id="values">
        #{id}, #{orderNo}, #{userId}, #{thirdFeatures}, now(), now(), 0
    </sql>

    <select id="scanId" parameterType="map" resultType="long">
        select
        id
        from
        <include refid="tb"/>
        <where>
            <if test="orderNo != null">
                and order_no = #{orderNo}
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at < #{updatedAtEnd} ]]>
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
        </where>
        order by id
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>


    <select id="listById" parameterType="map" resultMap="OrderExtendsMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where id in
        <foreach collection="list" item="param" index="index" open="(" close=")" separator=",">
            #{param}
        </foreach>
    </select>

    <select id="scanList" parameterType="map" resultMap="OrderExtendsMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="maxId != null">
                <![CDATA[ AND id < #{maxId} ]]>
            </if>
            <if test="orderNo != null">
                and order_no = #{orderNo}
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at < #{updatedAtEnd} ]]>
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
        </where>
        <choose>
            <when test="sortFlag == 1">
                order by updated_at, id
            </when>
            <otherwise>
                order by id
            </otherwise>
        </choose>
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>

    <update id="batchUpdateDate" parameterType="list">
        update
        <include refid="tb"/>
        <set>
            updated_at = now()
        </set>
        where
        id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

</mapper>