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

    <resultMap type="BaseProductLine" id="BaseProductLineResult">
            <result property="objId"    column="obj_id"    />
            <result property="productLineCode"    column="product_line_code"    />
            <result property="productLineName"    column="product_line_name"    />
            <result property="productLineType"    column="product_line_type"    />
            <result property="plantCode"    column="plant_code"    />
            <result property="plantName"    column="plantName"    />
            <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="parentId"    column="parent_id"    />
            <result property="parentName"    column="parentName"    />
    </resultMap>

    <sql id="selectBaseProductLineVo">
        select bpl.OBJ_ID,
               bpl.PRODUCT_LINE_CODE,
               bpl.PRODUCT_LINE_NAME,
               bpl.PRODUCT_LINE_TYPE,
               bpl.plant_code,
               bf.factory_name plantName,
               bpl.is_flag,
               bpl.created_by,
               bpl.created_time,
               bpl.updated_by,
               bpl.updated_time,
               bpl.PARENT_ID,
               b2.PRODUCT_LINE_NAME parentName
        from base_productline bpl
                 left join base_factory bf on bf.factory_code = bpl.plant_code
                 left join base_productline b2 on b2.PRODUCT_LINE_CODE = bpl.PARENT_ID
    </sql>

    <select id="selectBaseProductLineList" parameterType="BaseProductLine" resultMap="BaseProductLineResult">
        <include refid="selectBaseProductLineVo"/>
        <where>
                        <if test="productLineCode != null  and productLineCode != ''"> and bpl.product_line_code = #{productLineCode}</if>
                        <if test="productLineName != null  and productLineName != ''"> and bpl.product_line_name like concat(concat('%', #{productLineName}), '%')</if>
                        <if test="productLineType != null "> and bpl.product_line_type = #{productLineType}</if>
                        <if test="plantCode != null  and plantCode != ''"> and bpl.plant_code = #{plantCode}</if>
                        <if test="isFlag != null "> and bpl.is_flag = #{isFlag}</if>
                        <if test="createdBy != null  and createdBy != ''"> and bpl.created_by = #{createdBy}</if>
                        <if test="createdTime != null "> and bpl.created_time = #{createdTime}</if>
                        <if test="updatedBy != null  and updatedBy != ''"> and bpl.updated_by = #{updatedBy}</if>
                        <if test="updatedTime != null "> and bpl.updated_time = #{updatedTime}</if>
                        <if test="parentId != null "> and bpl.parent_id = #{parentId}</if>
        </where>
        order by bpl.PRODUCT_LINE_CODE
    </select>

    <select id="selectBaseProductLineByObjId" parameterType="Long" resultMap="BaseProductLineResult">
            <include refid="selectBaseProductLineVo"/>
            where bpl.obj_id = #{objId}
    </select>

    <insert id="insertBaseProductLine" parameterType="BaseProductLine">
            <selectKey keyProperty="objId" resultType="long" order="BEFORE">
                SELECT seq_base_productline.NEXTVAL as objId FROM DUAL
            </selectKey>
        insert into base_productline
        <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="objId != null">obj_id,</if>
                <if test="productLineCode != null">product_line_code,</if>
                <if test="productLineName != null">product_line_name,</if>
                <if test="productLineType != null">product_line_type,</if>
                <if test="plantCode != null">plant_code,</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="parentId != null">parent_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="objId != null">#{objId},</if>
                <if test="productLineCode != null">#{productLineCode},</if>
                <if test="productLineName != null">#{productLineName},</if>
                <if test="productLineType != null">#{productLineType},</if>
                <if test="plantCode != null">#{plantCode},</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="parentId != null">#{parentId},</if>
        </trim>
    </insert>

    <update id="updateBaseProductLine" parameterType="BaseProductLine">
        update base_productline
        <trim prefix="SET" suffixOverrides=",">
                    <if test="productLineCode != null">product_line_code = #{productLineCode},</if>
                    <if test="productLineName != null">product_line_name = #{productLineName},</if>
                    <if test="productLineType != null">product_line_type = #{productLineType},</if>
                    <if test="plantCode != null">plant_code = #{plantCode},</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="parentId != null">parent_id = #{parentId},</if>
        </trim>
        where obj_id = #{objId}
    </update>

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

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