<?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.OdSalesOrderDetailMapper">

    <resultMap type="OdSalesOrderDetail" id="OdSalesOrderDetailResult">
        <result property="id" column="id"/>
        <result property="orderId" column="order_id"/>
        <result property="orderNo" column="order_no"/>
        <result property="itemId" column="item_id"/>
        <result property="itemNo" column="item_no"/>
        <result property="sapItemNo" column="sap_item_no"/>
        <result property="itemName" column="item_name"/>
        <result property="totalNum" column="total_num"/>
        <result property="num" column="num"/>
        <result property="lossNum" column="loss_num"/>
        <result property="price" column="price"/>
        <result property="shipmentPointNo" column="shipment_point_no"/>
        <result property="storeAddr" column="store_addr"/>
        <result property="batchNo" column="batch_no"/>
        <result property="actualDeliveryDate" column="actual_delivery_date"/>
        <result property="pricingVal" column="pricing_val"/>
        <result property="offerUnit" column="offer_unit"/>
        <result property="saleUnit" column="sale_unit"/>
        <result property="sizeUnit" column="size_unit"/>
        <result property="poOrderNo" column="po_order_no"/>
        <result property="clientItemNo" column="client_item_no"/>
        <result property="clientItemName" column="client_item_name"/>
        <result property="clientSizeUnit" column="client_size_unit"/>
        <result property="clientCmd" column="client_cmd"/>
        <result property="saleFactory" column="sale_factory"/>
        <result property="typeNo" column="type_no"/>
        <result property="colorNo" column="color_no"/>
        <result property="color" column="color"/>
        <result property="clientKg" column="client_kg"/>
        <result property="salePath" column="sale_path"/>
        <result property="segment" column="segment"/>
        <result property="layout" column="layout"/>
        <result property="quarter" column="quarter"/>
        <result property="wholeSize" column="whole_size"/>
        <result property="specSize" column="spec_size"/>
        <result property="poNo" column="po_no"/>
        <result property="wipNo" column="wip_no"/>
        <result property="packingNum" column="packing_num"/>
        <result property="cycle" column="cycle"/>
        <result property="skNo" column="sk_no"/>
        <result property="patentNo" column="patent_no"/>
        <result property="materialDesc" column="material_desc"/>
        <result property="signer" column="signer"/>
        <result property="shipToId" column="ship_to_id"/>
        <result property="deliveryDate" column="delivery_date"/>
        <result property="remark" column="remark"/>
        <result property="sizeGroup" column="size_group"/>
        <result property="size" column="size"/>
        <result property="orderNum" column="order_num"/>
        <result property="barCode" column="bar_code"/>
        <result property="printingDirection" column="printing_direction"/>
        <result property="prodSpec" column="prod_spec"/>
        <result property="plantCode" column="plant_code"/>
        <result property="packMode" column="pack_mode"/>
        <result property="state" column="state"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <collection property="itemList" ofType="OdSalesOrderDetailItem">
            <result column="detail_item_id" property="id"/>
            <result column="item_size" property="size"/>
            <result column="item_num" property="num"/>
            <result column="item_loss_num" property="lossNum"/>
            <result column="spec_json" property="specJson"/>
            <result column="item_remark" property="remark"/>
        </collection>
    </resultMap>

    <sql id="selectOdSalesOrderDetailVo">
        select t1.id, t1.order_id, t1.order_no, t1.item_id, t1.item_no, t1.sap_item_no, t1.item_name, t1.total_num, t1.num, t1.loss_num, t1.price, t1.shipment_point_no,
               t1.store_addr, t1.batch_no, t1.actual_delivery_date, t1.pricing_val, t1.offer_unit, t1.sale_unit, t1.size_unit, t1.po_order_no,
               t1.client_item_no, t1.client_item_name, t1.client_size_unit, t1.client_cmd, t1.sale_factory, t1.type_no, t1.color_no, t1.color,
               t1.client_kg, t1.sale_path, t1.segment, t1.layout, t1.quarter, t1.whole_size, t1.spec_size, t1.po_no, t1.wip_no, t1.packing_num,
               t1.cycle, t1.sk_no, t1.patent_no, t1.material_desc, t1.signer, t1.ship_to_id, t1.delivery_date, t1.remark, t1.size_group,
               t1.size, t1.order_num, t1.bar_code, t1.printing_direction, t1.prod_spec, t1.plant_code, t1.pack_mode, t1.state, t1.create_by,
               t1.create_time, t1.update_by, t1.update_time,
               t2.id detail_item_id, t2.size item_size, t2.num item_num, t2.loss_num item_loss_num, t2.spec_json, t2.remark item_remark,
               t3.item_code, t3.item_name
        from od_sales_order_detail t1
        left join od_sales_order_detail_item t2 on t1.id = t2.detail_id
        LEFT JOIN md_item t3 ON t1.item_id = t3.item_id
    </sql>

    <select id="selectOdSalesOrderDetailList" parameterType="OdSalesOrderDetail" resultMap="OdSalesOrderDetailResult">
        <include refid="selectOdSalesOrderDetailVo"/>
        <where>
            <if test="orderId != null "> and t1.order_id = #{orderId}</if>
            <if test="itemId != null "> and t1.item_id = #{itemId}</if>
            <if test="state != null "> and t1.state = #{state}</if>
        </where>
    </select>

    <select id="selectOdSalesOrderDetailById" parameterType="String" resultMap="OdSalesOrderDetailResult">
        <include refid="selectOdSalesOrderDetailVo"/>
        where t1.id = #{id}
    </select>
    <select id="getByOrderCodeAndproductId" resultType="com.ximai.mes.od.domain.OdSalesOrderDetail">
        select * from od_sales_order_detail where order_no = #{orderCode} and item_id = #{productId}
    </select>

    <insert id="insertOdSalesOrderDetail" parameterType="OdSalesOrderDetail" useGeneratedKeys="true" keyProperty="id">
        insert into od_sales_order_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="orderNo != null">order_no,</if>
            <if test="itemId != null">item_id,</if>
            <if test="itemNo != null">item_no,</if>
            <if test="sapItemNo != null">sap_item_no,</if>
            <if test="itemName != null">item_name,</if>
            <if test="totalNum != null">total_num,</if>
            <if test="num != null">num,</if>
            <if test="lossNum != null">loss_num,</if>
            <if test="price != null">price,</if>
            <if test="shipmentPointNo != null">shipment_point_no,</if>
            <if test="storeAddr != null">store_addr,</if>
            <if test="batchNo != null">batch_no,</if>
            <if test="actualDeliveryDate != null">actual_delivery_date,</if>
            <if test="pricingVal != null">pricing_val,</if>
            <if test="saleFactory != null">sale_factory,</if>
            <if test="offerUnit != null">offer_unit,</if>
            <if test="saleUnit != null">sale_unit,</if>
            <if test="sizeUnit != null">size_unit,</if>
            <if test="poOrderNo != null">po_order_no,</if>
            <if test="clientItemNo != null">client_item_no,</if>
            <if test="clientItemName != null">client_item_name,</if>
            <if test="clientSizeUnit != null">client_size_unit,</if>
            <if test="clientCmd != null">client_cmd,</if>
            <if test="typeNo != null">type_no,</if>
            <if test="colorNo != null">color_no,</if>
            <if test="color != null">color,</if>
            <if test="clientKg != null">client_kg,</if>
            <if test="salePath != null">sale_path,</if>
            <if test="segment != null">segment,</if>
            <if test="layout != null">layout,</if>
            <if test="quarter != null">quarter,</if>
            <if test="wholeSize != null">whole_size,</if>
            <if test="specSize != null">spec_size,</if>
            <if test="poNo != null">po_no,</if>
            <if test="wipNo != null">wip_no,</if>
            <if test="packingNum != null">packing_num,</if>
            <if test="cycle != null">cycle,</if>
            <if test="skNo != null">sk_no,</if>
            <if test="patentNo != null">patent_no,</if>
            <if test="materialDesc != null">material_desc,</if>
            <if test="signer != null">signer,</if>
            <if test="shipToId != null">ship_to_id,</if>
            <if test="deliveryDate != null">delivery_date,</if>
            <if test="remark != null">remark,</if>
            <if test="sizeGroup != null">size_group,</if>
            <if test="size != null">size,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="barCode != null">bar_code,</if>
            <if test="printingDirection != null">printing_direction,</if>
            <if test="prodSpec != null">prod_spec,</if>
            <if test="plantCode != null">plant_code,</if>
            <if test="packMode != null">pack_mode,</if>
            <if test="state != null">state,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="orderNo != null">#{orderNo},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="itemNo != null">#{itemNo},</if>
            <if test="sapItemNo != null">#{sapItemNo},</if>
            <if test="itemName != null">#{itemName},</if>
            <if test="totalNum != null">#{totalNum},</if>
            <if test="num != null">#{num},</if>
            <if test="lossNum != null">#{lossNum},</if>
            <if test="price != null">#{price},</if>
            <if test="shipmentPointNo != null">#{shipmentPointNo},</if>
            <if test="storeAddr != null">#{storeAddr},</if>
            <if test="batchNo != null">#{batchNo},</if>
            <if test="actualDeliveryDate != null">#{actualDeliveryDate},</if>
            <if test="pricingVal != null">#{pricingVal},</if>
            <if test="saleFactory != null">#{saleFactory},</if>
            <if test="offerUnit != null">#{offerUnit},</if>
            <if test="saleUnit != null">#{saleUnit},</if>
            <if test="sizeUnit != null">#{sizeUnit},</if>
            <if test="poOrderNo != null">#{poOrderNo},</if>
            <if test="clientItemNo != null">#{clientItemNo},</if>
            <if test="clientItemName != null">#{clientItemName},</if>
            <if test="clientSizeUnit != null">#{clientSizeUnit},</if>
            <if test="clientCmd != null">#{clientCmd},</if>
            <if test="typeNo != null">#{typeNo},</if>
            <if test="colorNo != null">#{colorNo},</if>
            <if test="color != null">#{color},</if>
            <if test="clientKg != null">#{clientKg},</if>
            <if test="salePath != null">#{salePath},</if>
            <if test="segment != null">#{segment},</if>
            <if test="layout != null">#{layout},</if>
            <if test="quarter != null">#{quarter},</if>
            <if test="wholeSize != null">#{wholeSize},</if>
            <if test="specSize != null">#{specSize},</if>
            <if test="poNo != null">#{poNo},</if>
            <if test="wipNo != null">#{wipNo},</if>
            <if test="packingNum != null">#{packingNum},</if>
            <if test="cycle != null">#{cycle},</if>
            <if test="skNo != null">#{skNo},</if>
            <if test="patentNo != null">#{patentNo},</if>
            <if test="materialDesc != null">#{materialDesc},</if>
            <if test="signer != null">#{signer},</if>
            <if test="shipToId != null">#{shipToId},</if>
            <if test="deliveryDate != null">#{deliveryDate},</if>
            <if test="remark != null">#{remark},</if>
            <if test="sizeGroup != null">#{sizeGroup},</if>
            <if test="size != null">#{size},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="barCode != null">#{barCode},</if>
            <if test="printingDirection != null">#{printingDirection},</if>
            <if test="prodSpec != null">#{prodSpec},</if>
            <if test="plantCode != null">#{plantCode},</if>
            <if test="packMode != null">#{packMode},</if>
            <if test="state != null">#{state},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateOdSalesOrderDetail" parameterType="OdSalesOrderDetail">
        update od_sales_order_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="itemNo != null">item_no = #{itemNo},</if>
            <if test="sapItemNo != null">sap_item_no = #{sapItemNo},</if>
            <if test="itemName != null">item_name = #{itemName},</if>
            <if test="totalNum != null">total_num = #{totalNum},</if>
            <if test="num != null">num = #{num},</if>
            <if test="lossNum != null">loss_num = #{lossNum},</if>
            <if test="price != null">price = #{price},</if>
            <if test="shipmentPointNo != null">shipment_point_no = #{shipmentPointNo},</if>
            <if test="storeAddr != null">store_addr = #{storeAddr},</if>
            <if test="batchNo != null">batch_no = #{batchNo},</if>
            <if test="actualDeliveryDate != null">actual_delivery_date = #{actualDeliveryDate},</if>
            <if test="pricingVal != null">pricing_val = #{pricingVal},</if>
            <if test="saleFactory != null">sale_factory = #{saleFactory},</if>
            <if test="offerUnit != null">offer_unit = #{offerUnit},</if>
            <if test="saleUnit != null">sale_unit = #{saleUnit},</if>
            <if test="sizeUnit != null">size_unit = #{sizeUnit},</if>
            <if test="poOrderNo != null">po_order_no = #{poOrderNo},</if>
            <if test="clientItemNo != null">client_item_no = #{clientItemNo},</if>
            <if test="clientItemName != null">client_item_name = #{clientItemName},</if>
            <if test="clientSizeUnit != null">client_size_unit = #{clientSizeUnit},</if>
            <if test="clientCmd != null">client_cmd = #{clientCmd},</if>
            <if test="typeNo != null">type_no = #{typeNo},</if>
            <if test="colorNo != null">color_no = #{colorNo},</if>
            <if test="color != null">color = #{color},</if>
            <if test="clientKg != null">client_kg = #{clientKg},</if>
            <if test="salePath != null">sale_path = #{salePath},</if>
            <if test="segment != null">segment = #{segment},</if>
            <if test="layout != null">layout = #{layout},</if>
            <if test="quarter != null">quarter = #{quarter},</if>
            <if test="wholeSize != null">whole_size = #{wholeSize},</if>
            <if test="specSize != null">spec_size = #{specSize},</if>
            <if test="poNo != null">po_no = #{poNo},</if>
            <if test="wipNo != null">wip_no = #{wipNo},</if>
            <if test="packingNum != null">packing_num = #{packingNum},</if>
            <if test="cycle != null">cycle = #{cycle},</if>
            <if test="skNo != null">sk_no = #{skNo},</if>
            <if test="patentNo != null">patent_no = #{patentNo},</if>
            <if test="materialDesc != null">material_desc = #{materialDesc},</if>
            <if test="signer != null">signer = #{signer},</if>
            <if test="shipToId != null">ship_to_id = #{shipToId},</if>
            <if test="deliveryDate != null">delivery_date = #{deliveryDate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="sizeGroup != null">size_group = #{sizeGroup},</if>
            <if test="size != null">size = #{size},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="barCode != null">bar_code = #{barCode},</if>
            <if test="printingDirection != null">printing_direction = #{printingDirection},</if>
            <if test="prodSpec != null">prod_spec = #{prodSpec},</if>
            <if test="plantCode != null">plant_code = #{plantCode},</if>
            <if test="packMode != null">pack_mode = #{packMode},</if>
            <if test="state != null">state = #{state},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <insert id="insertBatch">
        INSERT INTO od_sales_order_detail
            (`id`, `order_id`, `order_no`, `item_id`, `item_no`, `sap_item_no`, `item_name`, `total_num`, `num`, `loss_num`, `price`, `shipment_point_no`,
             `store_addr`, `batch_no`, `actual_delivery_date`, `pricing_val`, `offer_unit`, `sale_unit`, `size_unit`, `po_order_no`,
             `client_item_no`, `client_item_name`, `client_size_unit`, `client_cmd`, `sale_factory`, `type_no`, `color_no`, `color`,
             `client_kg`, `sale_path`, `segment`, `layout`,
             `quarter`, `whole_size`, `spec_size`, `po_no`, `wip_no`, `packing_num`, `cycle`, `sk_no`, `patent_no`, `material_desc`,
             `signer`, `ship_to_id`, `delivery_date`, `remark`, `size_group`, `size`, `order_num`, `bar_code`, `printing_direction`,
             `prod_spec`, `plant_code`, `pack_mode`, `state`, `create_by`, `create_time`, `update_by`, `update_time`)
        VALUES <foreach collection="list" separator="," item="item">
            (null, #{item.orderId}, #{item.orderNo}, #{item.itemId}, #{item.itemNo}, #{item.sapItemNo}, #{item.itemName}, #{item.totalNum}, #{item.num},
            #{item.lossNum}, #{item.price}, #{item.shipmentPointNo}, #{item.storeAddr}, #{item.batchNo},
            #{item.actualDeliveryDate}, #{item.pricingVal}, #{item.offerUnit}, #{item.saleUnit}, #{item.sizeUnit},
            #{item.poOrderNo}, #{item.clientItemNo}, #{item.clientItemName}, #{item.clientSizeUnit}, #{item.clientCmd},
            #{item.saleFactory}, #{item.typeNo}, #{item.colorNo}, #{item.color},
            #{item.clientKg}, #{item.salePath}, #{item.segment}, #{item.layout}, #{item.quarter}, #{item.wholeSize},
            #{item.specSize}, #{item.poNo}, #{item.wipNo}, #{item.packingNum}, #{item.cycle}, #{item.skNo},
            #{item.patentNo}, #{item.materialDesc}, #{item.signer}, #{item.shipToId}, #{item.deliveryDate}, #{item.remark},
            #{item.sizeGroup}, #{item.size}, #{item.orderNum}, #{item.barCode}, #{item.printingDirection}, #{item.prodSpec},
            #{item.plantCode}, #{item.packMode}, #{item.state}, #{item.createBy}, #{item.createTime}, #{item.updateBy},
            #{item.updateTime})
            </foreach>
    </insert>

    <delete id="deleteOdSalesOrderDetailById" parameterType="String">
        delete from od_sales_order_detail where id = #{id}
    </delete>

    <delete id="deleteOdSalesOrderDetailByIds" parameterType="String">
        delete from od_sales_order_detail where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteByOrderId">
        DELETE FROM od_sales_order_detail WHERE order_id = #{id};
    </delete>
    <delete id="deleteByOrderIds">
        DELETE FROM od_sales_order_detail WHERE order_id in
        <foreach collection="array" item="item" separator="," open="(" close=")">#{item}</foreach>
    </delete>
</mapper>
