<?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.cool.store.mapper.order.StoreOrderOptionSnapshotMapper">

    <resultMap id="BaseResultMap" type="com.cool.store.entity.order.StoreOrderOptionSnapshotDO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="order_id" property="orderId" jdbcType="BIGINT"/>
        <result column="shop_id" property="shopId" jdbcType="BIGINT"/>
        <result column="store_type_id" property="storeTypeId" jdbcType="BIGINT"/>
        <result column="option_id" property="optionId" jdbcType="BIGINT"/>
        <result column="category_code" property="categoryCode" jdbcType="VARCHAR"/>
        <result column="item_code" property="itemCode" jdbcType="VARCHAR"/>
        <result column="option_code" property="optionCode" jdbcType="VARCHAR"/>
        <result column="option_unit" property="optionUnit" jdbcType="VARCHAR"/>
        <result column="option_name" property="optionName" jdbcType="VARCHAR"/>
        <result column="option_price" property="optionPrice" jdbcType="DECIMAL"/>
        <result column="original_option_price" property="originalOptionPrice" jdbcType="DECIMAL"/>
        <result column="option_remark" property="optionRemark" jdbcType="VARCHAR"/>
        <result column="image_url" property="imageUrl" jdbcType="VARCHAR"/>
        <result column="video_url" property="videoUrl" jdbcType="VARCHAR"/>
        <result column="quantity" property="quantity" jdbcType="INTEGER"/>
        <result column="amount" property="amount" jdbcType="DECIMAL"/>
        <result column="original_amount" property="originalAmount" jdbcType="DECIMAL"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="expense_type_code" property="expenseTypeCode" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, order_id, shop_id, store_type_id, option_id, category_code, item_code,
        option_code, option_name, option_price, original_option_price, option_remark, image_url, video_url,
        quantity, amount, original_amount, create_time, option_unit, expense_type_code
    </sql>

    <insert id="insertBatch">
        INSERT INTO zxjp_store_order_option_snapshot
        (order_id, shop_id, store_type_id, option_id, category_code, item_code,
         option_code, option_name, option_price, original_option_price, option_remark, image_url, video_url,
         quantity, amount, original_amount, create_time, option_unit,expense_type_code)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.orderId}, #{item.shopId}, #{item.storeTypeId}, #{item.optionId},
             #{item.categoryCode}, #{item.itemCode}, #{item.optionCode}, #{item.optionName},
             #{item.optionPrice}, #{item.originalOptionPrice}, #{item.optionRemark}, #{item.imageUrl}, #{item.videoUrl},
             #{item.quantity}, #{item.amount}, #{item.originalAmount}, NOW(), #{item.optionUnit}, #{item.expenseTypeCode})
        </foreach>
    </insert>

    <select id="listByOrderId" resultMap="BaseResultMap">
        SELECT
            a.id,
            a.order_id,
            a.shop_id,
            a.store_type_id,
            a.option_id,
            a.category_code,
            a.item_code,
            a.option_code,
            a.option_name,
            a.option_price,
            a.original_option_price,
            a.option_remark,
            a.image_url,
            a.video_url,
            a.quantity,
            a.amount,
            a.original_amount,
            a.create_time,
            a.option_unit,
            a.expense_type_code
        FROM zxjp_store_order_option_snapshot a left join
             zxjp_cfg_item_category  b on a.category_code = b.category_code
        WHERE order_id = #{orderId}
        ORDER BY b.sort_no asc,a.id ASC
    </select>

    <delete id="deleteByOrderId">
        DELETE FROM zxjp_store_order_option_snapshot
        WHERE order_id = #{orderId}
    </delete>

    <update id="batchUpdateQuantityAndAmount" parameterType="java.util.List">
        UPDATE zxjp_store_order_option_snapshot
        SET
        quantity = CASE id
        <foreach collection="list" item="item" separator=" ">
            WHEN #{item.id} THEN #{item.quantity}
        </foreach>
        ELSE quantity
        END,
        amount = CASE id
        <foreach collection="list" item="item" separator=" ">
            WHEN #{item.id} THEN #{item.amount}
        </foreach>
        ELSE amount
        END,
        original_amount = CASE id
        <foreach collection="list" item="item" separator=" ">
            WHEN #{item.id} THEN #{item.originalAmount}
        </foreach>
        ELSE original_amount
        END
        WHERE id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <select id="getExpenseTypeAmountByOrderId" resultType="com.cool.store.dto.fees.ExpenseTypeAmountDTO">
        select sum(a.amount) as totalAmount ,
               a.expense_type_code as expenseType
        from zxjp_store_order_option_snapshot a
        <where>
            <if test="orderId!=null">
                and  a.order_id = #{orderId}
            </if>
        </where>
        group by a.expense_type_code
    </select>

</mapper>
