<?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.snack.server.module.order.mapper.OrderItemMapper">

    <!-- 批量插入（foreach + VALUES 语法） -->
    <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO order_item
        (order_id, order_no, product_id, product_name, product_image,
         sku_id, sku_name, price, quantity, total_amount, create_time)
        VALUES
        <foreach collection="list" item="it" separator=",">
            (#{it.orderId}, #{it.orderNo}, #{it.productId}, #{it.productName}, #{it.productImage},
             #{it.skuId}, #{it.skuName}, #{it.price}, #{it.quantity}, #{it.totalAmount}, NOW())
        </foreach>
    </insert>

    <!-- 按订单 ID 批量查询 -->
    <select id="selectByOrderIds" resultType="com.snack.server.module.order.entity.OrderItem">
        SELECT id, order_id AS orderId, order_no AS orderNo, product_id AS productId,
               product_name AS productName, product_image AS productImage,
               sku_id AS skuId, sku_name AS skuName, price, quantity, total_amount AS totalAmount,
               create_time AS createTime
        FROM order_item
        WHERE order_id IN
        <foreach collection="orderIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        ORDER BY id ASC
    </select>

</mapper>
