<?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.younike.system.mapper.OrderRecordMapper">

    <resultMap type="OrderRecord" id="OrderRecordResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="preState"    column="pre_state"    />
        <result property="nowState"    column="now_state"    />
        <result property="operateInfo"    column="operate_info"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectOrderRecordVo">
        select id, order_id, pre_state, now_state, operate_info, create_by, create_time from order_record
    </sql>

    <select id="selectOrderRecordList" parameterType="OrderRecord" resultMap="OrderRecordResult">
        <include refid="selectOrderRecordVo"/>
        <where>
            <if test="orderId != null "> and order_id = #{orderId}</if>
            <if test="preState != null  and preState != ''"> and pre_state = #{preState}</if>
            <if test="nowState != null  and nowState != ''"> and now_state = #{nowState}</if>
            <if test="operateInfo != null  and operateInfo != ''"> and operate_info = #{operateInfo}</if>
        </where>
    </select>

    <select id="selectOrderRecordById" parameterType="Long" resultMap="OrderRecordResult">
        <include refid="selectOrderRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertOrderRecord" parameterType="OrderRecord">
        insert into order_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="preState != null and preState != ''">pre_state,</if>
            <if test="nowState != null and nowState != ''">now_state,</if>
            <if test="operateInfo != null and operateInfo != ''">operate_info,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="preState != null and preState != ''">#{preState},</if>
            <if test="nowState != null and nowState != ''">#{nowState},</if>
            <if test="operateInfo != null and operateInfo != ''">#{operateInfo},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateOrderRecord" parameterType="OrderRecord">
        update order_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="preState != null and preState != ''">pre_state = #{preState},</if>
            <if test="nowState != null and nowState != ''">now_state = #{nowState},</if>
            <if test="operateInfo != null and operateInfo != ''">operate_info = #{operateInfo},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOrderRecordById" parameterType="Long">
        delete from order_record where id = #{id}
    </delete>

    <delete id="deleteOrderRecordByIds" parameterType="String">
        delete from order_record where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

<!--    根据订单编号获取最近一条订单记录-->
    <select id="selectRefunfd" resultMap="OrderRecordResult" parameterType="Long">
        <include refid="selectOrderRecordVo"/>
        WHERE order_id = #{orderId}
        ORDER BY create_time DESC
        LIMIT 1
    </select>
</mapper>
