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

    <resultMap id="OrderExtendsMap" type="com.zm.order.tool.server.model.RoutingOrderExtends">
        <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"/>
        <result column="db_index" jdbcType="INTEGER" property="dbIndex"/>
        <result column="tb_index" jdbcType="INTEGER" property="tbIndex"/>
        <result column="sync_at" jdbcType="TIMESTAMP" property="syncAt"/>
    </resultMap>

    <sql id="tb">
        order_extends_0
    </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, db_index, tb_index, sync_at
    </sql>

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

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

    <insert id="insertList" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="list" item="order" index="index" separator=",">
            (
            #{order.id}, #{order.orderNo}, #{order.userId}, #{order.thirdFeatures}, #{order.createdAt},
            #{order.updatedAt}, #{order.isDeleted}
            )
        </foreach>
    </insert>

    <select id="selectByOrderNo" parameterType="map" resultMap="OrderExtendsMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no = #{orderNo}
        and user_id = #{userId}
        and is_deleted = 0
    </select>

    <update id="delete" parameterType="map">
        update
        <include refid="tb"/>
        set is_deleted = #{deleteType},
        updated_at = now()
        where id= #{id}
        and user_id = #{userId}
    </update>

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