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

    <resultMap type="BaseMaterialInfo" id="BaseMaterialInfoResult">
        <result property="objId"    column="obj_id"    />
        <result property="materialCode"    column="material_code"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialCategories"    column="material_categories"    />
        <result property="materialSubclass"    column="material_subclass"    />
        <result property="materialType"    column="material_type"    />
        <result property="materialUnit"    column="material_unit"    />
        <result property="materialMatkl"    column="material_matkl"    />
        <result property="materialSpecifications"    column="material_specifications"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="productLineCode"    column="productline_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="incrementDate"    column="increment_date"    />
        <result property="DISPO"    column="DISPO"    />
        <result property="productCode"    column="product_code"    />
        <result property="productName"    column="product_name"    />
    </resultMap>

    <sql id="selectBaseMaterialInfoVo">
        select ml.obj_id,
               ml.material_code,
               ml.material_name,
               ml.material_categories,
               ml.material_subclass,
               ml.material_type,
               ml.material_unit,
               ml.material_matkl,
               ml.DISPO,
               ml.material_specifications,
               ml.plant_code,
               bf.factory_name plantName,
               ml.productline_code,
               bpl.PRODUCT_LINE_NAME productLineName,
               ml.increment_date,
               ml.is_flag,
               ml.created_by,
               ml.created_time,
               ml.updated_by,
               ml.updated_time,
               ml.product_code,
               ml.product_name
        from base_materialinfo ml
                 left join base_factory bf on bf.factory_code = ml.plant_code
                 left join base_productline bpl on bpl.PRODUCT_LINE_CODE = ml.productline_code
    </sql>

    <select id="selectBaseMaterialInfoList" parameterType="BaseMaterialInfo" resultMap="BaseMaterialInfoResult">
        <include refid="selectBaseMaterialInfoVo"/>
        <where>
            <if test="materialCode != null  and materialCode != ''"> and ml.material_code = #{materialCode}</if>
            <if test="materialName != null  and materialName != ''"> and ml.material_name like concat(concat('%', #{materialName}), '%')</if>
            <if test="materialCategories != null  and materialCategories != ''"> and ml.material_categories = #{materialCategories}</if>
            <if test="materialSubclass != null  and materialSubclass != ''"> and ml.material_subclass = #{materialSubclass}</if>
            <if test="materialType != null  and materialType != ''"> and ml.material_type = #{materialType}</if>
            <if test="materialUnit != null  and materialUnit != ''"> and ml.material_unit = #{materialUnit}</if>
            <if test="materialMatkl != null  and materialMatkl != ''"> and ml.material_matkl = #{materialMatkl}</if>
            <if test="materialSpecifications != null "> and ml.material_specifications = #{materialSpecifications}</if>
            <if test="plantCode != null  and plantCode != ''"> and ml.plant_code = #{plantCode}</if>
            <if test="productLineCode != null  and productLineCode != ''"> and ml.productline_code = #{productLineCode}</if>
            <if test="isFlag != null "> and ml.is_flag = #{isFlag}</if>
            <if test="createdBy != null  and createdBy != ''"> and ml.created_by = #{createdBy}</if>
            <if test="createdTime != null "> and ml.created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''"> and ml.updated_by = #{updatedBy}</if>
            <if test="updatedTime != null "> and ml.updated_time = #{updatedTime}</if>
            <if test="productCode != null  and productCode != ''"> and ml.product_code = #{productCode}</if>
            <if test="productName != null  and productName != ''"> and ml.product_name = #{productName}</if>
        </where>
        order by ml.material_subclass, ml.increment_date desc
    </select>

    <select id="selectBaseMaterialInfoByObjId" parameterType="Long" resultMap="BaseMaterialInfoResult">
        <include refid="selectBaseMaterialInfoVo"/>
        where ml.obj_id = #{objId}
    </select>

    <insert id="insertBaseMaterialInfo" parameterType="BaseMaterialInfo">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_base_materialinfo.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into base_materialinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="materialCode != null">material_code,</if>
            <if test="materialName != null">material_name,</if>
            <if test="materialCategories != null">material_categories,</if>
            <if test="materialSubclass != null">material_subclass,</if>
            <if test="materialType != null">material_type,</if>
            <if test="materialUnit != null">material_unit,</if>
            <if test="materialMatkl != null">material_matkl,</if>
            <if test="materialSpecifications != null">material_specifications,</if>
            <if test="plantCode != null">plant_code,</if>
            <if test="productLineCode != null">productline_code,</if>
            <if test="incrementDate != null">increment_date,</if>
            <if test="isFlag != null">is_flag,</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="DISPO != null">DISPO,</if>
            <if test="productCode != null">product_code,</if>
            <if test="productName != null">product_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="materialCode != null">#{materialCode},</if>
            <if test="materialName != null">#{materialName,jdbcType=NVARCHAR},</if>
            <if test="materialCategories != null">#{materialCategories},</if>
            <if test="materialSubclass != null">#{materialSubclass},</if>
            <if test="materialType != null">#{materialType},</if>
            <if test="materialUnit != null">#{materialUnit},</if>
            <if test="materialMatkl != null">#{materialMatkl},</if>
            <if test="materialSpecifications != null">#{materialSpecifications},</if>
            <if test="plantCode != null">#{plantCode},</if>
            <if test="productLineCode != null">#{productLineCode},</if>
            <if test="incrementDate != null">#{incrementDate},</if>
            <if test="isFlag != null">#{isFlag},</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="DISPO != null">#{DISPO},</if>
            <if test="productCode != null">#{productCode},</if>
            <if test="productName != null">#{productName},</if>
        </trim>
    </insert>

    <update id="updateBaseMaterialInfo" parameterType="BaseMaterialInfo">
        update base_materialinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialName != null">material_name = #{materialName,jdbcType=NVARCHAR},</if>
            <if test="materialCategories != null">material_categories = #{materialCategories},</if>
            <if test="materialSubclass != null">material_subclass = #{materialSubclass},</if>
            <if test="materialType != null">material_type = #{materialType},</if>
            <if test="materialUnit != null">material_unit = #{materialUnit},</if>
            <if test="materialMatkl != null">material_matkl = #{materialMatkl},</if>
            <if test="materialSpecifications != null">material_specifications = #{materialSpecifications},</if>
            <if test="plantCode != null">plant_code = #{plantCode},</if>
            <if test="productLineCode != null">productline_code = #{productLineCode},</if>
            <if test="incrementDate != null">increment_date = #{incrementDate},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</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="DISPO != null">DISPO = #{DISPO},</if>
            <if test="productCode != null">product_code = #{productCode},</if>
            <if test="productName != null">product_name = #{productName},</if>
        </trim>
        where obj_id = #{objId}
    </update>

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

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