<?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.lframework.xingyun.sc.mappers.SaleOrderDetailMapper">
    <resultMap id="SaleOrderDetail" type="com.lframework.xingyun.sc.entity.SaleOrderDetail">
        <id column="id" property="id"/>
        <result column="order_id" property="orderId"/>
        <result column="product_id" property="productId"/>
        <result column="order_num" property="orderNum"/>
        <result column="ori_price" property="oriPrice"/>
        <result column="tax_price" property="taxPrice"/>
        <result column="discount_rate" property="discountRate"/>
        <result column="is_gift" property="isGift"/>
        <result column="tax_rate" property="taxRate"/>
        <result column="description" property="description"/>
        <result column="order_no" property="orderNo"/>
        <result column="out_num" property="outNum"/>
        <result column="purchase_decimal" property="purchaseDecimal"/>
        <result column="total_purchase_price" property="totalPurchasePrice"/>
        <result column="sc_id" property="scId"/>
    </resultMap>

    <sql id="SaleOrderDetailDto_sql">
        SELECT
            d.id,
            d.order_id,
            d.product_id,
            d.order_num,
            d.ori_price,
            d.tax_price,
            d.discount_rate,
            d.is_gift,
            d.tax_rate,
            d.description,
            d.order_no,
            d.out_num,
            d.purchase_decimal,
            d.total_purchase_price,
            d.sc_id
        FROM tbl_sale_order_detail AS d
    </sql>
    <update id="addOutNum">
        UPDATE tbl_sale_order_detail
        SET out_num = out_num + #{num}
        WHERE id = #{id}
        AND (order_num - out_num) >= #{num}
    </update>
    <update id="subOutNum">
        UPDATE tbl_sale_order_detail
        SET out_num = out_num - #{num}
        WHERE id = #{id}
        AND out_num >= #{num}
    </update>

    <select id="getByOrderId" resultMap="SaleOrderDetail">
        <include refid="SaleOrderDetailDto_sql"/>
        WHERE d.sheet_id = #{sheetId}
        ORDER BY d.order_no
    </select>

    <select id="getBySaleOrderId" resultMap="SaleOrderDetail">
        <include refid="SaleOrderDetailDto_sql"/>
        WHERE d.order_id = #{orderId}
        ORDER BY d.order_no
    </select>

    <select id="selectByOrderIds" resultMap="SaleOrderDetail">
        <include refid="SaleOrderDetailDto_sql"/>
        WHERE order_id IN
        <foreach item="id" collection="orderIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>
