<?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.xingxi.business.Order.mapper.OrderDetailMapper">
    <resultMap type="OrderDetail" id="OrderDetailResult">
        <result property="orderDetailId"    column="orderDetailId"    />
        <result property="orderId"    column="orderId"    />
        <result property="orderDetailStatus"    column="orderDetailStatus"    />
        <result property="prodId"    column="prodId"    />
        <result property="prodName"    column="prodName"    />
        <result property="prodAttrId"    column="prodAttrId"    />
        <result property="prodAttrName"    column="prodAttrName"    />
        <result property="vendorId"    column="vendorId"    />
        <result property="quantity"    column="quantity"    />
        <result property="afterSalesQty"    column="afterSalesQty"    />
        <result property="refundQty"    column="refundQty"    />
        <result property="price"    column="price"    />
        <result property="orderAmount"    column="orderAmount"    />
        <result property="payAmount"    column="payAmount"    />
        <result property="delFlag"    column="delFlag"    />
        <result property="createUser"    column="createUser"    />
        <result property="createTime"    column="createTime"    />
        <result property="updateUser"    column="updateUser"    />
        <result property="updateTime"    column="updateTime"    />
    </resultMap>

    <sql id="selectOrderDetail">
        select orderDetailId, orderId, orderDetailStatus, prodId, prodName, prodAttrId, prodAttrName, vendorId, quantity, afterSalesQty, refundQty, price, orderAmount, payAmount, delFlag, createUser, createTime, updateUser, updateTime from t_order_detail
    </sql>

    <sql id="selectOrderDetailExt">
        select
            ${tableAlias}.orderDetailId,
            ${tableAlias}.orderId,
            ${tableAlias}.orderDetailStatus,
            ${tableAlias}.prodId,
            ${tableAlias}.prodName,
            ${tableAlias}.prodAttrId,
            ${tableAlias}.prodAttrName,
            ${tableAlias}.vendorId,
            ${tableAlias}.quantity,
            ${tableAlias}.afterSalesQty,
            ${tableAlias}.refundQty,
            ${tableAlias}.price,
            ${tableAlias}.orderAmount,
            ${tableAlias}.payAmount,
            ${tableAlias}.delFlag,
            ${tableAlias}.createUser,
            ${tableAlias}.createTime,
            ${tableAlias}.updateUser,
            ${tableAlias}.updateTime
        from t_order_detail ${tableAlias}
    </sql>

    <select id="selectOrderDetailList" parameterType="OrderDetail" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail"/>
        <where>  
            <if test="orderId != null "> and orderId = #{orderId}</if>
            <if test="orderDetailStatus != null  and orderDetailStatus != ''"> and orderDetailStatus = #{orderDetailStatus}</if>
            <if test="prodId != null "> and prodId = #{prodId}</if>
            <if test="prodName != null  and prodName != ''"> and prodName like concat('%', #{prodName}, '%')</if>
            <if test="prodAttrId != null "> and prodAttrId = #{prodAttrId}</if>
            <if test="prodAttrName != null  and prodAttrName != ''"> and prodAttrName like concat('%', #{prodAttrName}, '%')</if>
            <if test="vendorId != null "> and vendorId = #{vendorId}</if>
            <if test="quantity != null "> and quantity = #{quantity}</if>
            <if test="afterSalesQty != null "> and afterSalesQty = #{afterSalesQty}</if>
            <if test="refundQty != null "> and refundQty = #{refundQty}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="orderAmount != null "> and orderAmount = #{orderAmount}</if>
            <if test="payAmount != null "> and payAmount = #{payAmount}</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </select>
    
    <select id="selectOrderDetailByOrderDetailId" parameterType="Long" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail"/>
        where orderDetailId = #{orderDetailId}
    </select>
        
    <insert id="insertOrderDetail" parameterType="OrderDetail" useGeneratedKeys="true" keyProperty="orderDetailId">
        insert into t_order_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">orderId,</if>
            <if test="orderDetailStatus != null">orderDetailStatus,</if>
            <if test="prodId != null">prodId,</if>
            <if test="prodName != null">prodName,</if>
            <if test="prodAttrId != null">prodAttrId,</if>
            <if test="prodAttrName != null">prodAttrName,</if>
            <if test="vendorId != null">vendorId,</if>
            <if test="quantity != null">quantity,</if>
            <if test="afterSalesQty != null">afterSalesQty,</if>
            <if test="refundQty != null">refundQty,</if>
            <if test="price != null">price,</if>
            <if test="orderAmount != null">orderAmount,</if>
            <if test="payAmount != null">payAmount,</if>
            <if test="delFlag != null">delFlag,</if>
            <if test="createUser != null">createUser,</if>
            <if test="createTime != null">createTime,</if>
            <if test="updateUser != null">updateUser,</if>
            <if test="updateTime != null">updateTime,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="orderDetailStatus != null">#{orderDetailStatus},</if>
            <if test="prodId != null">#{prodId},</if>
            <if test="prodName != null">#{prodName},</if>
            <if test="prodAttrId != null">#{prodAttrId},</if>
            <if test="prodAttrName != null">#{prodAttrName},</if>
            <if test="vendorId != null">#{vendorId},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="afterSalesQty != null">#{afterSalesQty},</if>
            <if test="refundQty != null">#{refundQty},</if>
            <if test="price != null">#{price},</if>
            <if test="orderAmount != null">#{orderAmount},</if>
            <if test="payAmount != null">#{payAmount},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateUser != null">#{updateUser},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertOrderDetail">
        insert into t_order_detail( orderId, orderDetailStatus, prodId, prodName, prodAttrId, prodAttrName, vendorId, quantity, afterSalesQty, refundQty, price, orderAmount, payAmount, delFlag, createUser, createTime, updateUser, updateTime) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.orderId}, #{item.orderDetailStatus}, #{item.prodId}, #{item.prodName}, #{item.prodAttrId}, #{item.prodAttrName}, #{item.vendorId}, #{item.quantity}, #{item.afterSalesQty}, #{item.refundQty}, #{item.price}, #{item.orderAmount}, #{item.payAmount}, #{item.delFlag}, #{item.createUser}, #{item.createTime}, #{item.updateUser}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="updateOrderDetail" parameterType="OrderDetail">
        update t_order_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">orderId = #{orderId},</if>
            <if test="orderDetailStatus != null">orderDetailStatus = #{orderDetailStatus},</if>
            <if test="prodId != null">prodId = #{prodId},</if>
            <if test="prodName != null">prodName = #{prodName},</if>
            <if test="prodAttrId != null">prodAttrId = #{prodAttrId},</if>
            <if test="prodAttrName != null">prodAttrName = #{prodAttrName},</if>
            <if test="vendorId != null">vendorId = #{vendorId},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="afterSalesQty != null">afterSalesQty = #{afterSalesQty},</if>
            <if test="refundQty != null">refundQty = #{refundQty},</if>
            <if test="price != null">price = #{price},</if>
            <if test="orderAmount != null">orderAmount = #{orderAmount},</if>
            <if test="payAmount != null">payAmount = #{payAmount},</if>
            <if test="delFlag != null">delFlag = #{delFlag},</if>
            <if test="createUser != null">createUser = #{createUser},</if>
            <if test="createTime != null">createTime = #{createTime},</if>
            <if test="updateUser != null">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where orderDetailId = #{orderDetailId}
    </update>

    <delete id="deleteOrderDetailByOrderDetailId" parameterType="Long">
        delete from t_order_detail where orderDetailId = #{orderDetailId}
    </delete>

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

    <update id="logicDeleteOrderDetailByOrderDetailId" parameterType="Long">
        update t_order_detail
            set delFlag = '1'
        where orderDetailId = #{orderDetailId}
    </update>

    <update id="logicDeleteOrderDetailByOrderDetailIds" parameterType="String">
        update t_order_detail
        set delFlag = '1'
        where orderDetailId in
        <foreach item="orderDetailId" collection="array" open="(" separator="," close=")">
            #{orderDetailId}
        </foreach>
    </update>

    <update id="logicDeleteOrderDetailByCondition" parameterType="OrderDetail">
        update t_order_detail
        set delFlag = '1'
        <where>
            <if test="orderId != null "> and orderId = #{orderId}</if>
            <if test="orderDetailStatus != null  and orderDetailStatus != ''"> and orderDetailStatus = #{orderDetailStatus}</if>
            <if test="prodId != null "> and prodId = #{prodId}</if>
            <if test="prodName != null  and prodName != ''"> and prodName like concat('%', #{prodName}, '%')</if>
            <if test="prodAttrId != null "> and prodAttrId = #{prodAttrId}</if>
            <if test="prodAttrName != null  and prodAttrName != ''"> and prodAttrName like concat('%', #{prodAttrName}, '%')</if>
            <if test="vendorId != null "> and vendorId = #{vendorId}</if>
            <if test="quantity != null "> and quantity = #{quantity}</if>
            <if test="afterSalesQty != null "> and afterSalesQty = #{afterSalesQty}</if>
            <if test="refundQty != null "> and refundQty = #{refundQty}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="orderAmount != null "> and orderAmount = #{orderAmount}</if>
            <if test="payAmount != null "> and payAmount = #{payAmount}</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </update>
</mapper>