<?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.OrderNoIndexDao">

    <resultMap id="OrderIndexNoMap" type="com.zm.order.tool.server.model.OrderNoIndex">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="biz_type" jdbcType="INTEGER" property="bizType"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="order_id" jdbcType="BIGINT" property="orderId"/>
        <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_index
    </sql>

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

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

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


    <select id="selectOne" parameterType="map" resultMap="OrderIndexNoMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no = #{orderNo}
        limit 1
    </select>

    <select id="selectList" parameterType="list" resultMap="OrderIndexNoMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where
        <if test="orderNoList != null and orderNoList.size > 0">
            order_no in
            <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                #{orderNo}
            </foreach>
        </if>
    </select>

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

    <insert id="inserts" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="orderNoIndexList" item="orderNoIndex" index="index" separator=",">
            (
            #{orderNoIndex.id}, #{orderNoIndex.orderNo}, #{orderNoIndex.bizType}, #{orderNoIndex.userId}, #{orderNoIndex.orderId}, now(), now(), 0
            )
        </foreach>
    </insert>

    <update id="update" parameterType="com.zm.order.tool.server.model.OrderNoIndex">
        update
        <include refid="tb"/>
        <set>
            <if test="userId != null">
                user_id = #{userId},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
        and order_no = #{orderNo}
    </update>

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

</mapper>