<?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.aucma.base.mapper.BaseOrderInfoMapper">

    <resultMap type="BaseOrderInfo" id="BaseOrderInfoResult">
        <result property="objId" column="obj_id"/>
        <result property="orderCode" column="order_code"/>
        <result property="saleOrderCode" column="saleorder_code"/>
        <result property="saleOrderLineNumber" column="saleorder_linenumber"/>
        <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="orderType" column="order_type"/>
        <result property="orderStatus" column="order_status"/>
        <result property="beginDate" column="begin_date"/>
        <result property="endDate" column="end_date"/>
        <result property="factoryCode" column="factory_code"/>
        <result property="isFlag" column="is_flag"/>
        <result property="createdBy" column="created_by"/>
        <result property="createdTime" column="created_time"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updatedTime" column="updated_time"/>
        <result property="completeDate" column="complete_date"/>
        <result property="isRelease" column="is_release"/>
        <result property="workCenterCode" column="work_center_code"/>
        <result property="routingCode" column="routing_code"/>
    </resultMap>

    <sql id="selectBaseOrderInfoVo">
        select oi.obj_id,
               oi.order_code,
               oi.saleorder_code,
               oi.saleorder_linenumber,
               oi.material_code,
               oi.material_name,
               oi.matkl,
               oi.order_amount,
               oi.complete_amount,
               oi.order_type,
               oi.order_status,
               oi.begin_date,
               oi.end_date,
               oi.factory_code,
               oi.is_flag,
               oi.is_release,
               oi.work_center_code,
               oi.routing_code,
               oi.created_by,
               oi.created_time,
               oi.updated_by,
               oi.updated_time,
               oi.complete_date
        from base_orderinfo oi
    </sql>

    <select id="selectBaseOrderInfoList" parameterType="BaseOrderInfo" resultMap="BaseOrderInfoResult">
        <include refid="selectBaseOrderInfoVo"/>
        JOIN BASE_MATERIALINFO bm ON bm.MATERIAL_CODE = oi.material_code AND bm.DISPO IN ('100')
        <where>
            <if test="orderCode != null  and orderCode != ''">and oi.order_code = #{orderCode}</if>
            <if test="saleOrderCode != null  and saleOrderCode != ''">and oi.saleorder_code = #{saleOrderCode}</if>
            <if test="saleOrderLineNumber != null  and saleOrderLineNumber != ''">and oi.saleorder_linenumber =
                #{saleOrderLineNumber}
            </if>
            <if test="materialCode != null  and materialCode != ''">and oi.material_code = #{materialCode}</if>
            <if test="materialName != null  and materialName != ''">and oi.material_name like concat(concat('%',
                #{materialName}), '%')
            </if>
            <if test="matkl != null  and matkl != ''">and oi.matkl = #{matkl}</if>
            <if test="orderAmount != null ">and oi.order_amount = #{orderAmount}</if>
            <if test="completeAmount != null ">and oi.complete_amount = #{completeAmount}</if>
            <if test="orderType != null  and orderType != ''">and oi.order_type = #{orderType}</if>
            <if test="orderStatus != null  and orderStatus != ''">and oi.order_status = #{orderStatus}</if>
            <if test="beginDate != null ">and oi.begin_date = #{beginDate}</if>
            <if test="endDate != null ">and oi.end_date = #{endDate}</if>
            <if test="factoryCode != null  and factoryCode != ''">and oi.factory_code = #{factoryCode}</if>
            <if test="isFlag != null ">and is_flag = #{isFlag}</if>
            <if test="createdBy != null  and createdBy != ''">and oi.created_by = #{createdBy}</if>
            <if test="createdTime != null ">and oi.created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''">and oi.updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and oi.updated_time = #{updatedTime}</if>
            <if test="completeDate != null ">and oi.complete_date = #{completeDate}</if>
            <if test="isRelease != null ">and oi.is_release = #{isRelease}</if>
            <if test="workCenterCode != null ">and oi.work_center_code = #{workCenterCode}</if>
        </where>
        order by order_code desc
    </select>

    <select id="selectBaseOrderInfoByObjId" parameterType="Long" resultMap="BaseOrderInfoResult">
        <include refid="selectBaseOrderInfoVo"/>
        where obj_id = #{objId}
    </select>

    <insert id="insertBaseOrderInfo" parameterType="BaseOrderInfo">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_base_orderinfo.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into base_orderinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="orderCode != null">order_code,</if>
            <if test="saleOrderCode != null">saleorder_code,</if>
            <if test="saleOrderLineNumber != null">saleorder_linenumber,</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="orderType != null">order_type,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="beginDate != null">begin_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="factoryCode != null">factory_code,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="isRelease != null">is_release,</if>
            <if test="createdBy != null">created_by,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="updatedBy != null">updated_by,</if>
            <if test="updatedTime != null">updated_time,</if>
            <if test="completeDate != null">complete_date,</if>
            <if test="workCenterCode != null">work_center_code,</if>
            <if test="routingCode != null">routing_code,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="orderCode != null">#{orderCode},</if>
            <if test="saleOrderCode != null">#{saleOrderCode},</if>
            <if test="saleOrderLineNumber != null">#{saleOrderLineNumber},</if>
            <if test="materialCode != null">#{materialCode},</if>
            <if test="materialName != null">#{materialName,jdbcType=NVARCHAR},</if>
            <if test="matkl != null">#{matkl},</if>
            <if test="orderAmount != null">#{orderAmount},</if>
            <if test="completeAmount != null">#{completeAmount},</if>
            <if test="orderType != null">#{orderType},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="beginDate != null">#{beginDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="factoryCode != null">#{factoryCode},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="isRelease != null">#{isRelease},</if>
            <if test="createdBy != null">#{createdBy},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedBy != null">#{updatedBy},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
            <if test="completeDate != null">#{completeDate},</if>
            <if test="workCenterCode != null">#{workCenterCode},</if>
            <if test="routingCode != null">#{routingCode},</if>
        </trim>
    </insert>

    <update id="updateBaseOrderInfo" parameterType="BaseOrderInfo">
        update base_orderinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="saleOrderCode != null">saleorder_code = #{saleOrderCode},</if>
            <if test="saleOrderLineNumber != null">saleorder_linenumber = #{saleOrderLineNumber},</if>
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialName != null">material_name = #{materialName,jdbcType=NVARCHAR},</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="orderType != null">order_type = #{orderType},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="beginDate != null">begin_date = #{beginDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="factoryCode != null">factory_code = #{factoryCode},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="isRelease != null">is_release = #{isRelease},</if>
            <if test="createdBy != null">created_by = #{createdBy},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="updatedBy != null">updated_by = #{updatedBy},</if>
            <if test="updatedTime != null">updated_time = #{updatedTime},</if>
            <if test="completeDate != null">complete_date = #{completeDate},</if>
            <if test="workCenterCode != null">work_center_code = #{workCenterCode},</if>
            <if test="routingCode != null">routing_code = #{routingCode},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteBaseOrderInfoByObjId" parameterType="Long">
        delete
        from base_orderinfo
        where obj_id = #{objId}
    </delete>

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