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

    <resultMap type="MesSaleOrder" id="MesSaleOrderResult">
        <result property="saleOrderId" column="sale_order_id"/>
        <result property="erpId" column="erp_id"/>
        <result property="fentryId" column="fentry_id"/>
        <result property="saleorderCode" column="saleorder_code"/>
        <result property="saleorderLinenumber" column="saleorder_linenumber"/>
        <result property="documentStatus" column="document_status"/>
        <result property="factoryId" column="factory_id"/>
        <result property="prodlineId" column="prodline_id"/>
        <result property="materialId" column="material_id"/>
        <result property="materialCode" column="material_code"/>
        <result property="materialName" column="material_name"/>
        <result property="matkl" column="matkl"/>
        <result property="orderAmount" column="order_amount"/>
        <result property="completeAmount" column="complete_amount"/>
        <result property="releaseQty" column="release_qty"/>
        <result property="isRelease" column="is_release"/>
        <result property="approveDate" column="approve_date"/>
        <result property="erpModifyDate" column="erp_modify_date"/>
        <result property="planDeliveryDate" column="plan_delivery_date"/>
        <result property="beginDate" column="begin_date"/>
        <result property="endDate" column="end_date"/>
        <result property="completeDate" column="complete_date"/>
        <result property="isFlag" column="is_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"/>
    </resultMap>

    <sql id="selectMesSaleOrderVo">
        select sale_order_id,
               erp_id,
               fentry_id,
               saleorder_code,
               saleorder_linenumber,
               document_status,
               factory_id,
               prodline_id,
               material_id,
               material_code,
               material_name,
               matkl,
               order_amount,
               complete_amount,
               release_qty,
               is_release,
               approve_date,
               erp_modify_date,
               plan_delivery_date,
               begin_date,
               end_date,
               complete_date,
               is_flag,
               remark,
               create_by,
               create_time,
               update_by,
               update_time
        from mes_sale_order
    </sql>

    <select id="selectMesSaleOrderList" parameterType="MesSaleOrder" resultMap="MesSaleOrderResult">
        <include refid="selectMesSaleOrderVo"/>
        <where>
            <if test="erpId != null ">and erp_id = #{erpId}</if>
            <if test="fentryId != null ">and fentry_id = #{fentryId}</if>
            <if test="saleorderCode != null  and saleorderCode != ''">and saleorder_code = #{saleorderCode}</if>
            <if test="saleorderLinenumber != null  and saleorderLinenumber != ''">and saleorder_linenumber =
                #{saleorderLinenumber}
            </if>
            <if test="documentStatus != null  and documentStatus != ''">and document_status = #{documentStatus}</if>
            <if test="factoryId != null ">and factory_id = #{factoryId}</if>
            <if test="prodlineId != null ">and prodline_id = #{prodlineId}</if>
            <if test="materialId != null ">and material_id = #{materialId}</if>
            <if test="materialCode != null  and materialCode != ''">and material_code = #{materialCode}</if>
            <if test="materialName != null  and materialName != ''">and material_name like concat('%', #{materialName},
                '%')
            </if>
            <if test="matkl != null  and matkl != ''">and matkl = #{matkl}</if>
            <if test="orderAmount != null ">and order_amount = #{orderAmount}</if>
            <if test="completeAmount != null ">and complete_amount = #{completeAmount}</if>
            <if test="releaseQty != null ">and release_qty = #{releaseQty}</if>
            <if test="isRelease != null  and isRelease != ''">and is_release = #{isRelease}</if>
            <if test="approveDate != null ">and approve_date = #{approveDate}</if>
            <if test="erpModifyDate != null ">and erp_modify_date = #{erpModifyDate}</if>
            <if test="planDeliveryDate != null ">and plan_delivery_date = #{planDeliveryDate}</if>
            <if test="beginDate != null ">and begin_date = #{beginDate}</if>
            <if test="endDate != null ">and end_date = #{endDate}</if>
            <if test="completeDate != null ">and complete_date = #{completeDate}</if>
            <if test="isFlag != null  and isFlag != ''">and is_flag = #{isFlag}</if>
        </where>
    </select>

    <select id="selectMesSaleOrderBySaleOrderId" parameterType="Long" resultMap="MesSaleOrderResult">
        <include refid="selectMesSaleOrderVo"/>
        where sale_order_id = #{saleOrderId}
    </select>

    <insert id="insertMesSaleOrder" parameterType="MesSaleOrder" useGeneratedKeys="true" keyProperty="saleOrderId">
        insert into mes_sale_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="erpId != null">erp_id,</if>
            <if test="fentryId != null">fentry_id,</if>
            <if test="saleorderCode != null">saleorder_code,</if>
            <if test="saleorderLinenumber != null">saleorder_linenumber,</if>
            <if test="documentStatus != null">document_status,</if>
            <if test="factoryId != null">factory_id,</if>
            <if test="prodlineId != null">prodline_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="materialCode != null">material_code,</if>
            <if test="materialName != null">material_name,</if>
            <if test="matkl != null">matkl,</if>
            <if test="orderAmount != null">order_amount,</if>
            <if test="completeAmount != null">complete_amount,</if>
            <if test="releaseQty != null">release_qty,</if>
            <if test="isRelease != null">is_release,</if>
            <if test="approveDate != null">approve_date,</if>
            <if test="erpModifyDate != null">erp_modify_date,</if>
            <if test="planDeliveryDate != null">plan_delivery_date,</if>
            <if test="beginDate != null">begin_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="completeDate != null">complete_date,</if>
            <if test="isFlag != null and isFlag != ''">is_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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="erpId != null">#{erpId},</if>
            <if test="fentryId != null">#{fentryId},</if>
            <if test="saleorderCode != null">#{saleorderCode},</if>
            <if test="saleorderLinenumber != null">#{saleorderLinenumber},</if>
            <if test="documentStatus != null">#{documentStatus},</if>
            <if test="factoryId != null">#{factoryId},</if>
            <if test="prodlineId != null">#{prodlineId},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="materialCode != null">#{materialCode},</if>
            <if test="materialName != null">#{materialName},</if>
            <if test="matkl != null">#{matkl},</if>
            <if test="orderAmount != null">#{orderAmount},</if>
            <if test="completeAmount != null">#{completeAmount},</if>
            <if test="releaseQty != null">#{releaseQty},</if>
            <if test="isRelease != null">#{isRelease},</if>
            <if test="approveDate != null">#{approveDate},</if>
            <if test="erpModifyDate != null">#{erpModifyDate},</if>
            <if test="planDeliveryDate != null">#{planDeliveryDate},</if>
            <if test="beginDate != null">#{beginDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="completeDate != null">#{completeDate},</if>
            <if test="isFlag != null and isFlag != ''">#{isFlag},</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>
        </trim>
    </insert>

    <update id="updateMesSaleOrder" parameterType="MesSaleOrder">
        update mes_sale_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="erpId != null">erp_id = #{erpId},</if>
            <if test="fentryId != null">fentry_id = #{fentryId},</if>
            <if test="saleorderCode != null">saleorder_code = #{saleorderCode},</if>
            <if test="saleorderLinenumber != null">saleorder_linenumber = #{saleorderLinenumber},</if>
            <if test="documentStatus != null">document_status = #{documentStatus},</if>
            <if test="factoryId != null">factory_id = #{factoryId},</if>
            <if test="prodlineId != null">prodline_id = #{prodlineId},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="matkl != null">matkl = #{matkl},</if>
            <if test="orderAmount != null">order_amount = #{orderAmount},</if>
            <if test="completeAmount != null">complete_amount = #{completeAmount},</if>
            <if test="releaseQty != null">release_qty = #{releaseQty},</if>
            <if test="isRelease != null">is_release = #{isRelease},</if>
            <if test="approveDate != null">approve_date = #{approveDate},</if>
            <if test="erpModifyDate != null">erp_modify_date = #{erpModifyDate},</if>
            <if test="planDeliveryDate != null">plan_delivery_date = #{planDeliveryDate},</if>
            <if test="beginDate != null">begin_date = #{beginDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="completeDate != null">complete_date = #{completeDate},</if>
            <if test="isFlag != null and isFlag != ''">is_flag = #{isFlag},</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>
        </trim>
        where sale_order_id = #{saleOrderId}
    </update>

    <delete id="deleteMesSaleOrderBySaleOrderId" parameterType="Long">
        delete
        from mes_sale_order
        where sale_order_id = #{saleOrderId}
    </delete>

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