<?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.tmerclub.cloud.delivery.mapper.DeliveryOrderItemMapper">
    <resultMap id="deliveryOrderItemMap" type="com.tmerclub.cloud.delivery.model.DeliveryOrderItem">
        <id column="delivery_order_item_id" property="deliveryOrderItemId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="delivery_order_id" property="deliveryOrderId"/>
        <result column="img_url" property="imgUrl"/>
        <result column="spu_name" property="spuName"/>
        <result column="supplier_spu_name" property="supplierSpuName"/>
        <result column="supplier_img_url" property="supplierImgUrl"/>
        <result column="count" property="count"/>
    </resultMap>
    <sql id="Vo_Column_List">
        `delivery_order_item_id`,
        `create_time`,
        `update_time`,
        `delivery_order_id`,
        `img_url`,
        `spu_name`,
        `supplier_img_url`,
        `supplier_spu_name`,
        `count`
    </sql>
    <select id="getById" resultType="com.tmerclub.cloud.delivery.vo.DeliveryOrderItemVO">
        select
        <include refid="Vo_Column_List"/>
        from delivery_order_item
        where delivery_order_item_id = #{id}
    </select>
    <insert id="save">
        insert into delivery_order_item (`delivery_order_item_id`, `delivery_order_id`, `img_url`, `spu_name`, `count`)
        values (#{deliveryOrderItem.deliveryOrderItemId}, #{deliveryOrderItem.deliveryOrderId}, #{deliveryOrderItem.imgUrl}, #{deliveryOrderItem.spuName},
                #{deliveryOrderItem.count});
    </insert>
    <update id="update">
        update delivery_order_item
        <set>
            <if test="deliveryOrderItem.deliveryOrderId != null">
                `delivery_order_id` = #{deliveryOrderItem.deliveryOrderId},
            </if>
            <if test="deliveryOrderItem.imgUrl != null">
                `img_url` = #{deliveryOrderItem.imgUrl},
            </if>
            <if test="deliveryOrderItem.spuName != null">
                `spu_name` = #{deliveryOrderItem.spuName},
            </if>
            <if test="deliveryOrderItem.count != null">
                `count` = #{deliveryOrderItem.count},
            </if>
            <if test="deliveryOrderItem.supplierSpuName != null">
                `supplier_spu_name` = #{deliveryOrderItem.supplierSpuName},
            </if>
            <if test="deliveryOrderItem.supplierImgUrl != null">
                `supplier_img_url` = #{deliveryOrderItem.supplierImgUrl},
            </if>
        </set>
        where delivery_order_item_id = #{deliveryOrderItem.deliveryOrderItemId}
    </update>
    <delete id="deleteById">
        delete
        from delivery_order_item
        where delivery_order_item_id = #{id}
    </delete>

    <insert id="saveBatch">
        insert into delivery_order_item (`delivery_order_item_id`, `delivery_order_id`, `img_url`, `spu_name`, `count`, `supplier_img_url`, `supplier_spu_name`)
        values
        <foreach collection="deliveryOrderItems" item="deliveryOrderItem" separator=",">
            (#{deliveryOrderItem.deliveryOrderItemId}, #{deliveryOrderItem.deliveryOrderId}, #{deliveryOrderItem.imgUrl}, #{deliveryOrderItem.spuName}, #{deliveryOrderItem.count},
            #{deliveryOrderItem.supplierImgUrl}, #{deliveryOrderItem.supplierSpuName})
        </foreach>
    </insert>
    <select id="listDetailDelivery" resultType="com.tmerclub.cloud.delivery.vo.DeliveryOrderVO">
        SELECT dc.name delivery_name,de.* FROM delivery_order de
        LEFT JOIN delivery_company dc ON dc.delivery_company_id =de.delivery_company_id
        WHERE de.order_id = #{orderId} AND de.status = 1

    </select>

    <select id="listItemDelivery" resultMap="deliveryOrderItemMap">
        select
        <include refid="Vo_Column_List"/>
        from delivery_order_item
        where delivery_order_id = #{deliveryOrderId}
    </select>
</mapper>
