<?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.core.order.common.mapper.OrderDetailMapper">

    <update id="batchUpdateDetailRefundQuantity">
        UPDATE order_detail
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="goods_refund_num = case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.quantity != null">
                        when detail_id = #{item.detailId} then #{item.goodsRefundNum}
                    </if>
                </foreach>
            </trim>
            <trim prefix="detail_state = case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.detailState != null">
                        when detail_id = #{item.detailId} then #{item.detailState}
                    </if>
                </foreach>
            </trim>
            <trim prefix="refund_amount = case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.refundAmount != null">
                        when detail_id = #{item.detailId} then #{item.refundAmount}
                    </if>
                </foreach>
            </trim>
        </trim>
        WHERE detail_id in
        <foreach collection="detailIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        AND order_date BETWEEN #{startDate} AND #{endDate}
    </update>

    <select id="getOrderDetailHistoryList" resultType="com.bonus.core.order.common.vo.OrderGoodsNumVO">
        SELECT
            od.goods_dishes_id as goodsDishesId,
            sum(od.quantity) as quantity,
            sum(od.goods_refund_num) as goodsRefundNum
        FROM order_detail od
            INNER JOIN order_info oi ON od.order_id = oi.order_id AND od.order_date = oi.order_date
        WHERE oi.order_date = #{orderDate}
          AND od.order_date = #{orderDate}
          AND oi.cust_id = #{custId}
          AND oi.order_state IN (1,2)
          <if test="mealtimeType != null">
              AND oi.mealtime_type = #{mealtimeType}
          </if>
          <if test="goodsIdList != null and goodsIdList.size() > 0">
              AND od.goods_dishes_id IN
              <foreach collection="goodsIdList" item="item" open="(" close=")" separator=",">
                  #{item}
              </foreach>
          </if>
        group by od.goods_dishes_id
    </select>


</mapper>
