<?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.ximai.mes.od.mapper.OdSalesOrderProductDetailMapper">

    <resultMap type="OdSalesOrderProductDetail" id="OdSalesOrderProductDetailResult">
        <result property="id" column="id"/>
        <result property="orderDetailId" column="order_detail_id"/>
        <result property="specId" column="spec_id"/>
        <result property="specKey" column="spec_key"/>
        <result property="specVal" column="spec_val"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectOdSalesOrderProductDetailVo">
        select id, order_detail_id, spec_id, `spec_key`, spec_val, create_time, update_time
        from od_sales_order_product_detail
    </sql>

    <select id="selectOdSalesOrderProductDetailList" parameterType="OdSalesOrderProductDetail"
            resultMap="OdSalesOrderProductDetailResult">
        <include refid="selectOdSalesOrderProductDetailVo"/>
        <where>
            <if test="orderDetailId != null ">and order_detail_id = #{orderDetailId}</if>
            <if test="specId != null">and `spec_id` = #{specId}</if>
            <if test="specKey != null  and specKey != ''">and `spec_key` = #{specKey}</if>
            <if test="specVal != null  and specVal != ''">and spec_val = #{specVal}</if>
        </where>
    </select>

    <select id="selectOdSalesOrderProductDetailById" parameterType="String" resultMap="OdSalesOrderProductDetailResult">
        <include refid="selectOdSalesOrderProductDetailVo"/>
        where id = #{id}
    </select>

    <insert id="insertOdSalesOrderProductDetail" parameterType="OdSalesOrderProductDetail" useGeneratedKeys="true"
            keyProperty="id">
        insert into od_sales_order_product_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderDetailId != null">order_detail_id,</if>
            <if test="specId != null">`spec_id`,</if>
            <if test="specKey != null">`spec_key`,</if>
            <if test="specVal != null">spec_val,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderDetailId != null">#{orderDetailId},</if>
            <if test="specId != null">#{specId},</if>
            <if test="specKey != null">#{specKey},</if>
            <if test="specVal != null">#{specVal},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateOdSalesOrderProductDetail" parameterType="OdSalesOrderProductDetail">
        update od_sales_order_product_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderDetailId != null">order_detail_id = #{orderDetailId},</if>
            <if test="specId != null">`spec_id` = #{specId},</if>
            <if test="specKey != null">`spec_key` = #{specKey},</if>
            <if test="specVal != null">spec_val = #{specVal},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOdSalesOrderProductDetailById" parameterType="String">
        delete
        from od_sales_order_product_detail
        where id = #{id}
    </delete>

    <delete id="deleteOdSalesOrderProductDetailByIds" parameterType="String">
        delete from od_sales_order_product_detail where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteByOrderId">
        DELETE FROM od_sales_order_product_detail
        WHERE order_detail_id IN (SELECT id FROM od_sales_order_detail WHERE order_id = #{id});
    </delete>
    <delete id="deleteByOrderIds">
        DELETE FROM od_sales_order_product_detail
        WHERE order_detail_id IN (SELECT id FROM od_sales_order_detail WHERE order_id in
        <foreach collection="array" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </delete>
</mapper>
