<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.edu.ecnu.stu.bookstore.mapper.OrderMapper">
    <insert id="insertOrderBook">
        insert into t_order_book(order_id, book_id, count, single_price) values
        <foreach collection="books" item="map" separator=",">
            (#{orderId}, #{map.book.bookId}, #{map.count}, #{map.book.price})
        </foreach>
    </insert>
    
    <resultMap id="orderMap" type="order">
        <id property="orderId" column="order_id"/>
        <result property="buyerId" column="buyer_id"/>
        <result property="fromAddress" column="from_address"/>
        <result property="toAddress" column="to_address"/>
        <result property="price" column="price"/>
        <result property="storeId" column="store_id"/>
        <result property="createTime" column="create_time"/>
        <result property="status" column="status" typeHandler="cn.edu.ecnu.stu.bookstore.handler.OrderStatusTypeHandler"/>
    </resultMap>

    <select id="select" resultMap="orderMap">
        select order_id, buyer_id, from_address, to_address, price, status, store_id, create_time from t_order
        where buyer_id = #{buyerId} and status = #{status.value}
    </select>
    <select id="selectById" resultMap="orderMap">
        select order_id, buyer_id, from_address, to_address, price, status, store_id, create_time from t_order
        where order_id = #{orderId}
    </select>
</mapper>