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

    <resultMap type="OdPurchaseOrderDetail" id="OdPurchaseOrderDetailResult">
        <result property="id" column="id"/>
        <result property="orderId" column="order_id"/>
        <result property="itemId" column="item_id"/>
        <result property="itemNo" column="item_code"/>
        <result property="sapItemCode" column="sap_item_code"/>
        <result property="itemName" column="item_name"/>
        <result property="totalNum" column="total_num"/>
        <result property="lossNum" column="loss_num"/>
        <result property="num" column="num"/>
        <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"/>
        <result property="specJson" column="spec_json"/>
        <result property="productName" column="product_name"/>
        <result property="poProductName" column="po_product_name"/>
        <result property="poProductNo" column="po_product_no"/>
        <result property="poColor" column="po_color"/>
        <result property="poColorNo" column="po_color_no"/>
        <result property="region" column="region"/>
        <result property="sizeGroup" column="size_group"/>
        <result property="size" column="size"/>
        <result property="wholeYard" column="whole_yard"/>
        <result property="specialSize" column="special_size"/>
        <result property="featureNo" column="feature_no"/>
        <result property="cmdNo" column="cmd_no"/>
        <result property="orderNum" column="order_num"/>
        <result property="productSize" column="product_size"/>
        <result property="deliveryDate" column="delivery_date"/>
        <result property="poKg" column="po_kg"/>
        <result property="poOrderNo" column="po_order_no"/>
        <result property="customerUnit" column="customer_unit"/>
        <result property="signer" column="signer"/>
        <result property="salePath" column="sale_path"/>
        <result property="segment" column="segment"/>
        <result property="layout" column="layout"/>
        <result property="quarter" column="quarter"/>
        <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="shipToId" column="ship_to_id"/>
        <result property="remark" column="remark"/>
        <result property="barCode" column="bar_code"/>
        <result property="printingDirection" column="printing_direction"/>
    </resultMap>

    <sql id="selectOdPurchaseOrderDetailVo">
        select t1.id, t1.order_id, t1.item_id, t1.total_num, t1.loss_num, t1.num, t1.state, t1.create_by, t1.create_time,
               t1.update_by, t1.update_time, t1.spec_json, t1.product_name, t1.po_product_name, t1.po_product_no,
               t1.po_color, t1.po_color_no, t1.region, t1.size_group, t1.size, t1.whole_yard, t1.special_size,
               t1.feature_no, t1.cmd_no, t1.order_num, t1.product_size, t1.delivery_date, t1.po_kg, t1.po_order_no,
               t1.customer_unit, t1.signer, t1.sale_path, t1.segment, t1.layout, t1.quarter, t1.po_no, t1.wip_no,
               t1.packing_num, t1.cycle, t1.sk_no, t1.patent_no, t1.material_desc, t1.ship_to_id, t1.remark, t1.bar_code,
               t1.printing_direction, t2.item_name, t2.item_code, t2.sap_item_code
        from od_purchase_order_detail t1
        left join  md_item t2 on t1.item_id = t2.item_id
    </sql>

    <select id="selectOdPurchaseOrderDetailList" parameterType="OdPurchaseOrderDetail" resultMap="OdPurchaseOrderDetailResult">
        <include refid="selectOdPurchaseOrderDetailVo"/>
        <where>
            <if test="orderId != null  and orderId != ''"> and t1.order_id = #{orderId}</if>
            <if test="itemId != null  and itemId != ''"> and t1.item_id = #{itemId}</if>
        </where>
    </select>

    <select id="selectOdPurchaseOrderDetailById" parameterType="String" resultMap="OdPurchaseOrderDetailResult">
        <include refid="selectOdPurchaseOrderDetailVo"/>
        where id = #{id}
    </select>

    <insert id="insertOdPurchaseOrderDetail" parameterType="OdPurchaseOrderDetail" useGeneratedKeys="true" keyProperty="id">
        insert into od_purchase_order_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="itemId != null">item_id,</if>
            <if test="totalNum != null">total_num,</if>
            <if test="lossNum != null">loss_num,</if>
            <if test="num != null">num,</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>
            <if test="specJson != null">spec_json,</if>
            <if test="productName != null">product_name,</if>
            <if test="poProductName != null">po_product_name,</if>
            <if test="poProductNo != null">po_product_no,</if>
            <if test="poColor != null">po_color,</if>
            <if test="poColorNo != null">po_color_no,</if>
            <if test="region != null">region,</if>
            <if test="sizeGroup != null">size_group,</if>
            <if test="size != null">size,</if>
            <if test="wholeYard != null">whole_yard,</if>
            <if test="specialSize != null">special_size,</if>
            <if test="featureNo != null">feature_no,</if>
            <if test="cmdNo != null">cmd_no,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="productSize != null">product_size,</if>
            <if test="deliveryDate != null">delivery_date,</if>
            <if test="poKg != null">po_kg,</if>
            <if test="poOrderNo != null">po_order_no,</if>
            <if test="customerUnit != null">customer_unit,</if>
            <if test="signer != null">signer,</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="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="shipToId != null">ship_to_id,</if>
            <if test="remark != null">remark,</if>
            <if test="barCode != null">bar_code,</if>
            <if test="printingDirection != null">printing_direction,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="totalNum != null">#{totalNum},</if>
            <if test="lossNum != null">#{lossNum},</if>
            <if test="num != null">#{num},</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>
            <if test="specJson != null">#{specJson},</if>
            <if test="productName != null">#{productName},</if>
            <if test="poProductName != null">#{poProductName},</if>
            <if test="poProductNo != null">#{poProductNo},</if>
            <if test="poColor != null">#{poColor},</if>
            <if test="poColorNo != null">#{poColorNo},</if>
            <if test="region != null">#{region},</if>
            <if test="sizeGroup != null">#{sizeGroup},</if>
            <if test="size != null">#{size},</if>
            <if test="wholeYard != null">#{wholeYard},</if>
            <if test="specialSize != null">#{specialSize},</if>
            <if test="featureNo != null">#{featureNo},</if>
            <if test="cmdNo != null">#{cmdNo},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="productSize != null">#{productSize},</if>
            <if test="deliveryDate != null">#{deliveryDate},</if>
            <if test="poKg != null">#{poKg},</if>
            <if test="poOrderNo != null">#{poOrderNo},</if>
            <if test="customerUnit != null">#{customerUnit},</if>
            <if test="signer != null">#{signer},</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="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="shipToId != null">#{shipToId},</if>
            <if test="remark != null">#{remark},</if>
            <if test="barCode != null">#{barCode},</if>
            <if test="printingDirection != null">#{printingDirection},</if>
         </trim>
    </insert>

    <insert id="batchInsert">
        INSERT INTO od_purchase_order_detail
            (`id`, `order_id`, `item_id`, `state`, `create_by`, `create_time`, `update_by`,
             `update_time`, `spec_json`, `product_name`, `po_product_name`, `po_product_no`, `po_color`, `po_color_no`, `region`,
             `size_group`, `size`, `whole_yard`, `special_size`, `feature_no`, `cmd_no`, `order_num`, `product_size`,
             `delivery_date`, `po_kg`, `po_order_no`, `customer_unit`, `signer`, `sale_path`, `segment`, `layout`, `quarter`,
             `po_no`, `wip_no`, `packing_num`, `cycle`, `sk_no`, `patent_no`, `material_desc`, `ship_to_id`, `remark`, `bar_code`,
             `printing_direction`)
        VALUES <foreach collection="list" separator="," item="item">
        (null, #{item.orderId}, #{item.itemId}, #{item.state}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime},
        #{item.specJson}, #{item.productName}, #{item.poProductName}, #{item.poProductNo}, #{item.poColor},
        #{item.poColorNo}, #{item.region}, #{item.sizeGroup}, #{item.size}, #{item.wholeYard}, #{item.specialSize},
        #{item.featureNo}, #{item.cmdNo}, #{item.orderNum}, #{item.productSize}, #{item.deliveryDate}, #{item.poKg},
        #{item.poOrderNo}, #{item.customerUnit}, #{item.signer}, #{item.salePath}, #{item.segment}, #{item.layout},
        #{item.quarter}, #{item.poNo}, #{item.wipNo}, #{item.packingNum}, #{item.cycle}, #{item.skNo},
        #{item.patentNo}, #{item.materialDesc}, #{item.shipToId}, #{item.remark}, #{item.barCode}, #{item.printingDirection})
    </foreach>
    </insert>

    <update id="updateOdPurchaseOrderDetail" parameterType="OdPurchaseOrderDetail">
        update od_purchase_order_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="totalNum != null">total_num = #{totalNum},</if>
            <if test="lossNum != null">loss_num = #{lossNum},</if>
            <if test="num != null">num = #{num},</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>
            <if test="specJson != null">spec_json = #{specJson},</if>
            <if test="productName != null">product_name = #{productName},</if>
            <if test="poProductName != null">po_product_name = #{poProductName},</if>
            <if test="poProductNo != null">po_product_no = #{poProductNo},</if>
            <if test="poColor != null">po_color = #{poColor},</if>
            <if test="poColorNo != null">po_color_no = #{poColorNo},</if>
            <if test="region != null">region = #{region},</if>
            <if test="sizeGroup != null">size_group = #{sizeGroup},</if>
            <if test="size != null">size = #{size},</if>
            <if test="wholeYard != null">whole_yard = #{wholeYard},</if>
            <if test="specialSize != null">special_size = #{specialSize},</if>
            <if test="featureNo != null">feature_no = #{featureNo},</if>
            <if test="cmdNo != null">cmd_no = #{cmdNo},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="productSize != null">product_size = #{productSize},</if>
            <if test="deliveryDate != null">delivery_date = #{deliveryDate},</if>
            <if test="poKg != null">po_kg = #{poKg},</if>
            <if test="poOrderNo != null">po_order_no = #{poOrderNo},</if>
            <if test="customerUnit != null">customer_unit = #{customerUnit},</if>
            <if test="signer != null">signer = #{signer},</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="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="shipToId != null">ship_to_id = #{shipToId},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="barCode != null">bar_code = #{barCode},</if>
            <if test="printingDirection != null">printing_direction = #{printingDirection},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOdPurchaseOrderDetailById" parameterType="String">
        delete from od_purchase_order_detail where id = #{id}
    </delete>

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