<?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.bonus.canteen.core.order.mapper.OrderDetailMapper">
    
    <resultMap type="com.bonus.canteen.core.order.domain.OrderDetail" id="OrderDetailResult">
        <result property="detailId"    column="detail_id"    />
        <result property="orderId"    column="order_id"    />
        <result property="orderDate"    column="order_date"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="goodsName"    column="goods_name"    />
        <result property="goodsImgUrl"    column="goods_img_url"    />
        <result property="recipeDetailId" column="recipe_detail_id"    />
        <result property="salePrice"    column="sale_price"    />
        <result property="discountPrice"    column="discount_price"    />
        <result property="finalPrice"    column="final_price"    />
        <result property="quantity"    column="quantity"    />
        <result property="salesMode"    column="sales_mode"    />
        <result property="detailType"    column="detail_type"    />
        <result property="totalAmount"    column="total_amount"    />
        <result property="calcTotalAmount"    column="calc_total_amount"    />
        <result property="realAmount"    column="real_amount"    />
        <result property="refundAmount"    column="refund_amount"    />
        <result property="detailState"    column="detail_state"    />
        <result property="refundNum"    column="refund_num"    />
        <result property="deviceSn"    column="device_sn"    />
        <result property="serialNum"    column="serial_num"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectOrderDetailVo">
        select detail_id, order_id, order_date, goods_id, goods_name, goods_img_url, recipe_detail_id, sale_price, discount_price, final_price, quantity, sales_mode, detail_type, total_amount, calc_total_amount, real_amount, refund_amount, detail_state, refund_num, device_sn, serial_num, create_by, create_time, update_by, update_time from order_detail
    </sql>

    <select id="selectOrderDetailList" parameterType="com.bonus.canteen.core.order.domain.OrderDetail" resultMap="OrderDetailResult">
        <include refid="selectOrderDetailVo"/>
        <where>  
            <if test="orderId != null "> and order_id = #{orderId}</if>
            <if test="orderDate != null "> and order_date = #{orderDate}</if>
            <if test="goodsId != null "> and goods_id = #{goodsId}</if>
            <if test="goodsName != null  and goodsName != ''"> and goods_name like concat('%', #{goodsName}, '%')</if>
            <if test="goodsImgUrl != null  and goodsImgUrl != ''"> and goods_img_url = #{goodsImgUrl}</if>
            <if test="recipeDetailId != null">
                and recipe_detail_id = #{recipeDetailId}</if>
            <if test="salePrice != null "> and sale_price = #{salePrice}</if>
            <if test="discountPrice != null "> and discount_price = #{discountPrice}</if>
            <if test="finalPrice != null "> and final_price = #{finalPrice}</if>
            <if test="quantity != null "> and quantity = #{quantity}</if>
            <if test="salesMode != null "> and sales_mode = #{salesMode}</if>
            <if test="detailType != null "> and detail_type = #{detailType}</if>
            <if test="totalAmount != null "> and total_amount = #{totalAmount}</if>
            <if test="calcTotalAmount != null "> and calc_total_amount = #{calcTotalAmount}</if>
            <if test="realAmount != null "> and real_amount = #{realAmount}</if>
            <if test="refundAmount != null "> and refund_amount = #{refundAmount}</if>
            <if test="detailState != null "> and detail_state = #{detailState}</if>
            <if test="refundNum != null "> and refund_num = #{refundNum}</if>
            <if test="deviceSn != null  and deviceSn != ''"> and device_sn = #{deviceSn}</if>
            <if test="serialNum != null  and serialNum != ''"> and serial_num = #{serialNum}</if>
        </where>
    </select>
    
    <select id="selectOrderDetailByDetailId" parameterType="Long" resultMap="OrderDetailResult">
        <include refid="selectOrderDetailVo"/>
        where detail_id = #{detailId}
    </select>

    <insert id="insertOrderDetail" parameterType="com.bonus.canteen.core.order.domain.OrderDetail" useGeneratedKeys="true" keyProperty="detailId">
        insert into order_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="orderDate != null">order_date,</if>
            <if test="goodsId != null">goods_id,</if>
            <if test="goodsName != null and goodsName != ''">goods_name,</if>
            <if test="goodsImgUrl != null">goods_img_url,</if>
            <if test="recipeDetailId != null">recipe_detail_id,</if>
            <if test="salePrice != null">sale_price,</if>
            <if test="discountPrice != null">discount_price,</if>
            <if test="finalPrice != null">final_price,</if>
            <if test="quantity != null">quantity,</if>
            <if test="salesMode != null">sales_mode,</if>
            <if test="detailType != null">detail_type,</if>
            <if test="totalAmount != null">total_amount,</if>
            <if test="calcTotalAmount != null">calc_total_amount,</if>
            <if test="realAmount != null">real_amount,</if>
            <if test="refundAmount != null">refund_amount,</if>
            <if test="detailState != null">detail_state,</if>
            <if test="refundNum != null">refund_num,</if>
            <if test="deviceSn != null and deviceSn != ''">device_sn,</if>
            <if test="serialNum != null and serialNum != ''">serial_num,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="orderDate != null">#{orderDate},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="goodsName != null and goodsName != ''">#{goodsName},</if>
            <if test="goodsImgUrl != null">#{goodsImgUrl},</if>
            <if test="recipeDetailId != null">#{recipeDetailId},</if>
            <if test="salePrice != null">#{salePrice},</if>
            <if test="discountPrice != null">#{discountPrice},</if>
            <if test="finalPrice != null">#{finalPrice},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="salesMode != null">#{salesMode},</if>
            <if test="detailType != null">#{detailType},</if>
            <if test="totalAmount != null">#{totalAmount},</if>
            <if test="calcTotalAmount != null">#{calcTotalAmount},</if>
            <if test="realAmount != null">#{realAmount},</if>
            <if test="refundAmount != null">#{refundAmount},</if>
            <if test="detailState != null">#{detailState},</if>
            <if test="refundNum != null">#{refundNum},</if>
            <if test="deviceSn != null and deviceSn != ''">#{deviceSn},</if>
            <if test="serialNum != null and serialNum != ''">#{serialNum},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertOrderDetail" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="detailId">
        insert into order_detail (
        order_id,
        order_date,
        goods_id,
        goods_name,
        goods_img_url,
        recipe_detail_id,
        sale_price,
        discount_price,
        final_price,
        quantity,
        sales_mode,
        detail_type,
        total_amount,
        calc_total_amount,
        real_amount,
        refund_amount,
        detail_state,
        refund_num,
        device_sn,
        serial_num,
        create_by,
        update_by
        )
        values
        <foreach collection="list" item="orderDetail" separator=",">
            (
            #{orderDetail.orderId},
            #{orderDetail.orderDate},
            #{orderDetail.goodsId},
            #{orderDetail.goodsName},
            #{orderDetail.goodsImgUrl},
            #{orderDetail.recipeDetailId},
            #{orderDetail.salePrice},
            #{orderDetail.discountPrice},
            #{orderDetail.finalPrice},
            #{orderDetail.quantity},
            #{orderDetail.salesMode},
            #{orderDetail.detailType},
            #{orderDetail.totalAmount},
            #{orderDetail.calcTotalAmount},
            #{orderDetail.realAmount},
            #{orderDetail.refundAmount},
            #{orderDetail.detailState},
            #{orderDetail.refundNum},
            #{orderDetail.deviceSn},
            #{orderDetail.serialNum},
            #{orderDetail.createBy},
            #{orderDetail.createBy}
            )
        </foreach>
    </insert>


    <update id="updateOrderDetail" parameterType="com.bonus.canteen.core.order.domain.OrderDetail">
        update order_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="orderDate != null">order_date = #{orderDate},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="goodsName != null and goodsName != ''">goods_name = #{goodsName},</if>
            <if test="goodsImgUrl != null">goods_img_url = #{goodsImgUrl},</if>
            <if test="recipeDetailId != null">recipe_detail_id = #{recipeDetailId},</if>
            <if test="salePrice != null">sale_price = #{salePrice},</if>
            <if test="discountPrice != null">discount_price = #{discountPrice},</if>
            <if test="finalPrice != null">final_price = #{finalPrice},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="salesMode != null">sales_mode = #{salesMode},</if>
            <if test="detailType != null">detail_type = #{detailType},</if>
            <if test="totalAmount != null">total_amount = #{totalAmount},</if>
            <if test="calcTotalAmount != null">calc_total_amount = #{calcTotalAmount},</if>
            <if test="realAmount != null">real_amount = #{realAmount},</if>
            <if test="refundAmount != null">refund_amount = #{refundAmount},</if>
            <if test="detailState != null">detail_state = #{detailState},</if>
            <if test="refundNum != null">refund_num = #{refundNum},</if>
            <if test="deviceSn != null and deviceSn != ''">device_sn = #{deviceSn},</if>
            <if test="serialNum != null and serialNum != ''">serial_num = #{serialNum},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where detail_id = #{detailId}
    </update>

    <delete id="deleteOrderDetailByDetailId" parameterType="Long">
        delete from order_detail where detail_id = #{detailId}
    </delete>

    <delete id="deleteOrderDetailByDetailIds" parameterType="String">
        delete from order_detail where detail_id in 
        <foreach item="detailId" collection="detailIds" open="(" separator="," close=")">
            #{detailId}
        </foreach>
    </delete>

</mapper>