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

    <resultMap type="OrderBomInfo" id="OrderBomInfoResult">
        <result property="objId" column="obj_id"/>
        <result property="bomCode" column="bom_code"/>
        <result property="materialCode" column="material_code"/>
        <result property="materialName" column="material_name"/>
        <result property="materialType" column="material_type"/>
        <result property="standardAmount" column="standard_amount"/>
        <result property="parentId" column="parent_id"/>
        <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="factoryCode" column="factory_code"/>
        <result property="sort" column="sort"/>
        <result property="vbeln" column="vbeln"/>
        <result property="vbpos" column="vbpos"/>
        <result property="ancestors"    column="ancestors"    />
    </resultMap>

    <sql id="selectOrderBomInfoVo">
        select ob.obj_id,
               ob.bom_code,
               ob.material_code,
               bm.material_name,
               bm.material_type,
               ob.standard_amount,
               ob.parent_id,
               ob.is_flag,
               ob.created_by,
               ob.created_time,
               ob.updated_by,
               ob.updated_time,
               ob.factory_code,
               ob.sort,
               ob.ancestors,
               ob.vbeln,
               ob.vbpos
        from order_bominfo ob
                 left join BASE_MATERIALINFO bm ON bm.MATERIAL_CODE = ob.material_code
    </sql>

    <select id="selectOrderBomInfoList" parameterType="OrderBomInfo" resultMap="OrderBomInfoResult">
        <include refid="selectOrderBomInfoVo"/>
        <where>
            <if test="bomCode != null  and bomCode != ''">and ob.bom_code = #{bomCode}</if>
            <if test="materialCode != null  and materialCode != ''">and ob.material_code = #{materialCode}</if>
            <if test="materialName != null  and materialName != ''">and bm.material_name like concat(concat('%',
                #{materialName}), '%')
            </if>
            <if test="materialType != null  and materialType != ''">and ob.material_type = #{materialType}</if>
            <if test="standardAmount != null ">and ob.standard_amount = #{standardAmount}</if>
            <if test="parentId != null  and parentId != ''">and ob.parent_id = #{parentId}</if>
            <if test="isFlag != null ">and ob.is_flag = #{isFlag}</if>
            <if test="createdBy != null  and createdBy != ''">and ob.created_by = #{createdBy}</if>
            <if test="createdTime != null ">and ob.created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''">and ob.updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and ob.updated_time = #{updatedTime}</if>
            <if test="factoryCode != null  and factoryCode != ''">and ob.factory_code = #{factoryCode}</if>
            <if test="sort != null  and sort != ''">and ob.sort = #{sort}</if>
            <if test="vbeln != null  and vbeln != ''">and ob.vbeln = #{vbeln}</if>
            <if test="vbpos != null  and vbpos != ''">and ob.vbpos = #{vbpos}</if>
            <if test="ancestors != null  and ancestors != ''">and ob.ancestors like concat(concat('%', #{ancestors}), '%')</if>
        </where>
        order by ob.parent_id, ob.sort
    </select>

    <select id="selectOrderBomInfoByObjId" parameterType="Long" resultMap="OrderBomInfoResult">
        <include refid="selectOrderBomInfoVo"/>
        where ob.obj_id = #{objId}
    </select>

    <select id="selectOrderBomInfoByMaterialCode" resultMap="OrderBomInfoResult">
        <include refid="selectOrderBomInfoVo"/>
        where ob.material_code = #{materialCode}
    </select>

    <insert id="insertOrderBomInfo" parameterType="OrderBomInfo">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_order_bominfo.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into order_bominfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="bomCode != null">bom_code,</if>
            <if test="materialCode != null">material_code,</if>
            <if test="materialName != null">material_name,</if>
            <if test="materialType != null">material_type,</if>
            <if test="standardAmount != null">standard_amount,</if>
            <if test="parentId != null">parent_id,</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="factoryCode != null">factory_code,</if>
            <if test="sort != null">sort,</if>
            <if test="vbeln != null">vbeln,</if>
            <if test="vbpos != null">vbpos,</if>
            <if test="ancestors != null">ancestors,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="bomCode != null">#{bomCode},</if>
            <if test="materialCode != null">#{materialCode},</if>
            <if test="materialName != null">#{materialName},</if>
            <if test="materialType != null">#{materialType},</if>
            <if test="standardAmount != null">#{standardAmount},</if>
            <if test="parentId != null">#{parentId},</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="factoryCode != null">#{factoryCode},</if>
            <if test="sort != null">#{sort},</if>
            <if test="vbeln != null">#{vbeln},</if>
            <if test="vbpos != null">#{vbpos},</if>
            <if test="ancestors != null">#{ancestors},</if>
        </trim>
    </insert>

    <update id="updateOrderBomInfo" parameterType="OrderBomInfo">
        update order_bominfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="bomCode != null">bom_code = #{bomCode},</if>
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="materialType != null">material_type = #{materialType},</if>
            <if test="standardAmount != null">standard_amount = #{standardAmount},</if>
            <if test="parentId != null">parent_id = #{parentId},</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="factoryCode != null">factory_code = #{factoryCode},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="vbeln != null">vbeln = #{vbeln},</if>
            <if test="vbpos != null">vbpos = #{vbpos},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
        </trim>
        where obj_id = #{objId}
    </update>

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

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

    <select id="selectChildrenBomById" parameterType="String" resultMap="OrderBomInfoResult">
        select * from order_bominfo where FIND_IN_SET(#{materialCode}, ancestors) <![CDATA[ <> ]]> 0
    </select>

    <update id="updateBomChildren" parameterType="java.util.List">
        update order_bominfo set ancestors =
        <foreach collection="depts" item="item" index="index"
                 separator=" " open="case material_code" close="end">
            when #{item.materialCode} then #{item.ancestors}
        </foreach>
        where material_code in
        <foreach collection="depts" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.materialCode}
        </foreach>
    </update>
</mapper>