<?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.OrderPushLogsDao">
    <resultMap id="ResultMap" type="com.zm.order.tool.server.model.OrderPushLogs">
        <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="scene" jdbcType="INTEGER" property="scene"/>
        <result column="push_at" jdbcType="TIMESTAMP" property="pushAt"/>
        <result column="features" jdbcType="VARCHAR" property="features"/>
        <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_push_logs
    </sql>

    <sql id="cols_all">
        id,
		user_id,
		order_no,
		scene,
		push_at,
		features,
		created_at,
		updated_at,
		is_deleted
    </sql>

    <sql id="values">
    	#{id,jdbcType=BIGINT},
		#{userId,jdbcType=BIGINT},
		#{orderNo,jdbcType=VARCHAR},
		#{scene,jdbcType=INTEGER},
		#{pushAt,jdbcType=TIMESTAMP},
		#{features,jdbcType=VARCHAR},
		now(),
		now(),
		0
    </sql>

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

    <select id="selectByOrderNo" parameterType="map" resultMap="ResultMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no = #{orderNo} order by updated_at desc
    </select>

    <select id="selectList" resultMap="ResultMap">
        SELECT
        <include refid="cols_all"/>
        FROM
        <include refid="tb"/>
        <where>
            is_deleted = 0
        </where>
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
    </select>

    <select id="selectIdList" parameterType="map" resultMap="ResultMap">
        select
        id, order_no
        from
        <include refid="tb"/>
        <where>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at <= #{updatedAtEnd} ]]>
            </if>
        </where>
        limit ${size}
    </select>

    <update id="realDelete" parameterType="map">
        delete from
        <include refid="tb"/>
        where
        id = #{id} and order_no = #{orderNo}
    </update>

</mapper>