<?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.hw.mes.mapper.MesProductOrderMapper">

    <resultMap type="MesProductOrder" id="MesProductOrderResult">
        <result property="productOrderId" column="product_order_id"/>
        <result property="orderCode" column="order_code"/>
        <result property="saleOrderId" column="sale_order_id"/>
        <result property="saleorderCode" column="saleorder_code"/>
        <result property="saleorderLinenumber" column="saleorder_linenumber"/>
        <result property="projectNo" column="project_no"/>
        <result property="materialId" column="material_id"/>
        <result property="materialBomId" column="material_bom_id"/>
        <result property="dispatchType" column="dispatch_type"/>
        <result property="dispatchId" column="dispatch_id"/>
        <result property="saleAmount" column="sale_amount"/>
        <result property="planAmount" column="plan_amount"/>
        <result property="dispatchAmount" column="dispatch_amount"/>
        <result property="completeAmount" column="complete_amount"/>
        <result property="releaseTime" column="release_time"/>
        <result property="planBeginTime" column="plan_begin_time"/>
        <result property="planEndTime" column="plan_end_time"/>
        <result property="realBeginTime" column="real_begin_time"/>
        <result property="realEndTime" column="real_end_time"/>
        <result property="orderStatus" column="order_status"/>
        <result property="stockLockFlag" column="stock_lock_flag"/>
        <result property="remark" column="remark"/>
        <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="saleOrderFlag" column="sale_order_flag"/>
        <result property="planDeliveryDate" column="plan_delivery_date"/>
        <result property="dispatchName" column="dispatchName"/>
        <result property="materialCode" column="material_code"/>
        <result property="materialName" column="material_name"/>
        <result property="materialBomDesc" column="material_bom_desc"/>
    </resultMap>

    <sql id="selectMesProductOrderVo">
        select mpo.product_order_id,
               mpo.order_code,
               mpo.sale_order_id,
               mpo.saleorder_code,
               mpo.saleorder_linenumber,
               mpo.project_no,
               mpo.material_id,
               mpo.material_bom_id,
               concat(mb.material_name, '-', mb.material_bom_desc) material_bom_desc,
               mpo.dispatch_type,
               mpo.dispatch_id,
               mbr.route_name                                      dispatchName,
               bmi.material_code,
               bmi.material_name,
               mpo.sale_amount,
               mpo.plan_amount,
               mpo.dispatch_amount,
               mpo.complete_amount,
               mpo.release_time,
               mpo.plan_begin_time,
               mpo.plan_end_time,
               mpo.real_begin_time,
               mpo.real_end_time,
               mpo.order_status,
               mpo.stock_lock_flag,
               mpo.sale_order_flag,
               mpo.plan_delivery_date,
               mpo.remark,
               mpo.create_by,
               mpo.create_time,
               mpo.update_by,
               mpo.update_time
        from mes_product_order mpo
                 left join mes_base_route mbr on mbr.route_id = mpo.dispatch_id
                 left join mes_base_material_info bmi on bmi.material_id = mpo.material_id
                 left join mes_material_bom mb on mb.material_bom_id = mpo.material_bom_id
    </sql>

    <select id="selectMesProductOrderList" parameterType="MesProductOrder" resultMap="MesProductOrderResult">
        <include refid="selectMesProductOrderVo"/>
        <where>
            mpo.order_status not in ('9')
            <if test="orderCode != null  and orderCode != ''">and mpo.order_code = #{orderCode}</if>
            <if test="saleOrderId != null ">and mpo.sale_order_id = #{saleOrderId}</if>
            <if test="saleorderCode != null  and saleorderCode != ''">and mpo.saleorder_code = #{saleorderCode}</if>
            <if test="saleorderLinenumber != null  and saleorderLinenumber != ''">and mpo.saleorder_linenumber =
                #{saleorderLinenumber}
            </if>
            <if test="projectNo != null  and projectNo != ''">and mpo.project_no = #{projectNo}</if>
            <if test="materialId != null ">and mpo.material_id = #{materialId}</if>
            <if test="materialBomId != null ">and mpo.material_bom_id = #{materialBomId}</if>
            <if test="dispatchType != null  and dispatchType != ''">and mpo.dispatch_type = #{dispatchType}</if>
            <if test="dispatchId != null ">and mpo.dispatch_id = #{dispatchId}</if>
            <if test="saleAmount != null ">and mpo.sale_amount = #{saleAmount}</if>
            <if test="planAmount != null ">and mpo.plan_amount = #{planAmount}</if>
            <if test="dispatchAmount != null ">and mpo.dispatch_amount = #{dispatchAmount}</if>
            <if test="completeAmount != null ">and mpo.complete_amount = #{completeAmount}</if>
            <if test="releaseTime != null ">and mpo.release_time = #{releaseTime}</if>
            <if test="planBeginTime != null ">and mpo.plan_begin_time = #{planBeginTime}</if>
            <if test="planEndTime != null ">and mpo.plan_end_time = #{planEndTime}</if>
            <if test="realBeginTime != null ">and mpo.real_begin_time = #{realBeginTime}</if>
            <if test="realEndTime != null ">and mpo.real_end_time = #{realEndTime}</if>
            <if test="orderStatus != null  and orderStatus != ''">and mpo.order_status = #{orderStatus}</if>
            <if test="stockLockFlag != null  and stockLockFlag != ''">and mpo.stock_lock_flag = #{stockLockFlag}</if>
            <if test="saleOrderFlag != null  and saleOrderFlag != ''">and mpo.sale_order_flag = #{saleOrderFlag}</if>
            <if test="createBy != null  and createBy != ''">and mpo.create_by = #{createBy}</if>
            <if test="createTime != null ">and mpo.create_time = #{createTime}</if>
            <if test="updateBy != null  and updateBy != ''">and mpo.update_by = #{updateBy}</if>
            <if test="updateTime != null ">and mpo.update_time = #{updateTime}</if>
        </where>
        order by mpo.sale_order_id desc, mpo.product_order_id desc
    </select>

    <select id="selectMesProductOrderByProductOrderId" parameterType="Long" resultMap="MesProductOrderResult">
        <include refid="selectMesProductOrderVo"/>
        where mpo.product_order_id = #{productOrderId}
    </select>

    <insert id="insertMesProductOrder" parameterType="MesProductOrder" useGeneratedKeys="true"
            keyProperty="productOrderId">
        insert into mes_product_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderCode != null and orderCode != ''">order_code,</if>
            <if test="saleOrderId != null">sale_order_id,</if>
            <if test="saleorderCode != null and saleorderCode != ''">saleorder_code,</if>
            <if test="saleorderLinenumber != null">saleorder_linenumber,</if>
            <if test="projectNo != null">project_no,</if>
            <if test="materialId != null">material_id,</if>
            <if test="materialBomId != null">material_bom_id,</if>
            <if test="dispatchType != null and dispatchType != ''">dispatch_type,</if>
            <if test="dispatchId != null">dispatch_id,</if>
            <if test="saleAmount != null">sale_amount,</if>
            <if test="planAmount != null">plan_amount,</if>
            <if test="dispatchAmount != null">dispatch_amount,</if>
            <if test="completeAmount != null">complete_amount,</if>
            <if test="releaseTime != null">release_time,</if>
            <if test="planBeginTime != null">plan_begin_time,</if>
            <if test="planEndTime != null">plan_end_time,</if>
            <if test="realBeginTime != null">real_begin_time,</if>
            <if test="realEndTime != null">real_end_time,</if>
            <if test="orderStatus != null and orderStatus != ''">order_status,</if>
            <if test="stockLockFlag != null and stockLockFlag != ''">stock_lock_flag,</if>
            <if test="saleOrderFlag != null and saleOrderFlag != ''">sale_order_flag,</if>
            <if test="remark != null">remark,</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="planDeliveryDate != null">plan_delivery_date,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderCode != null and orderCode != ''">#{orderCode},</if>
            <if test="saleOrderId != null">#{saleOrderId},</if>
            <if test="saleorderCode != null and saleorderCode != ''">#{saleorderCode},</if>
            <if test="saleorderLinenumber != null">#{saleorderLinenumber},</if>
            <if test="projectNo != null">#{projectNo},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="materialBomId != null">#{materialBomId},</if>
            <if test="dispatchType != null and dispatchType != ''">#{dispatchType},</if>
            <if test="dispatchId != null">#{dispatchId},</if>
            <if test="saleAmount != null">#{saleAmount},</if>
            <if test="planAmount != null">#{planAmount},</if>
            <if test="dispatchAmount != null">#{dispatchAmount},</if>
            <if test="completeAmount != null">#{completeAmount},</if>
            <if test="releaseTime != null">#{releaseTime},</if>
            <if test="planBeginTime != null">#{planBeginTime},</if>
            <if test="planEndTime != null">#{planEndTime},</if>
            <if test="realBeginTime != null">#{realBeginTime},</if>
            <if test="realEndTime != null">#{realEndTime},</if>
            <if test="orderStatus != null and orderStatus != ''">#{orderStatus},</if>
            <if test="stockLockFlag != null and stockLockFlag != ''">#{stockLockFlag},</if>
            <if test="saleOrderFlag != null and saleOrderFlag != ''">#{saleOrderFlag},</if>
            <if test="remark != null">#{remark},</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="planDeliveryDate != null">#{planDeliveryDate},</if>
        </trim>
    </insert>

    <update id="updateMesProductOrder" parameterType="MesProductOrder">
        update mes_product_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderCode != null and orderCode != ''">order_code = #{orderCode},</if>
            <if test="saleOrderId != null">sale_order_id = #{saleOrderId},</if>
            <if test="saleorderCode != null and saleorderCode != ''">saleorder_code = #{saleorderCode},</if>
            <if test="saleorderLinenumber != null">saleorder_linenumber = #{saleorderLinenumber},</if>
            <if test="projectNo != null">project_no = #{projectNo},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="materialBomId != null">material_bom_id = #{materialBomId},</if>
            <if test="dispatchType != null and dispatchType != ''">dispatch_type = #{dispatchType},</if>
            <if test="dispatchId != null">dispatch_id = #{dispatchId},</if>
            <if test="saleAmount != null">sale_amount = #{saleAmount},</if>
            <if test="planAmount != null">plan_amount = #{planAmount},</if>
            <if test="dispatchAmount != null">dispatch_amount = #{dispatchAmount},</if>
            <if test="completeAmount != null">complete_amount = #{completeAmount},</if>
            <if test="releaseTime != null">release_time = #{releaseTime},</if>
            <if test="planBeginTime != null">plan_begin_time = #{planBeginTime},</if>
            <if test="planEndTime != null">plan_end_time = #{planEndTime},</if>
            <if test="realBeginTime != null">real_begin_time = #{realBeginTime},</if>
            <if test="realEndTime != null">real_end_time = #{realEndTime},</if>
            <if test="orderStatus != null and orderStatus != ''">order_status = #{orderStatus},</if>
            <if test="stockLockFlag != null and stockLockFlag != ''">stock_lock_flag = #{stockLockFlag},</if>
            <if test="saleOrderFlag != null and saleOrderFlag != ''">sale_order_flag = #{saleOrderFlag},</if>
            <if test="remark != null">remark = #{remark},</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="planDeliveryDate != null">plan_delivery_date = #{planDeliveryDate},</if>
        </trim>
        where product_order_id = #{productOrderId}
    </update>

    <delete id="deleteMesProductOrderByProductOrderId" parameterType="Long">
        delete
        from mes_product_order
        where product_order_id = #{productOrderId}
    </delete>

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