<?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"/>
        <result property="processCode" column="processCode"/>
        <result property="stationType" column="station_type"/>
        <result property="workCenterCode" column="work_center_code"/>
        <result property="workCenterName" column="work_center_name"/>
        <result property="executionSort" column="execution_sort"/>
    </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.station_type,
               bpl.created_by,
               bpl.created_time,
               bpl.updated_by,
               bpl.updated_time,
               bpl.execution_sort,
               bpl.PARENT_ID,
               bpl.work_center_code,
               bpl.work_center_name,
               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"/>
        <if test="processCode != null  and processCode != ''">
             join
            (SELECT PROCESS_CODE,
            TRIM(REGEXP_SUBSTR(PARENT_ID, '[^,]+', 1, LEVEL)) AS station_code
            FROM BASE_PROCESS_STATION
            CONNECT BY PRIOR PROCESS_CODE = PROCESS_CODE
            AND PRIOR DBMS_RANDOM.VALUE IS NOT NULL
            AND LENGTH(REGEXP_REPLACE(PARENT_ID, '[^,]+')) + 1 >= LEVEL
            ORDER BY PROCESS_CODE, LEVEL) ST ON st.station_code = bpl.PRODUCT_LINE_CODE AND ST.PROCESS_CODE = #{processCode}
        </if>
        <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>
            <if test="stationType != null ">and bpl.station_type = #{stationType}</if>
        </where>
        order by
        <if test="executionSort != null and executionSort == 1 ">bpl.execution_sort,</if>
        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>
            <if test="stationType != null">station_type,</if>
            <if test="workCenterCode != null">work_center_code,</if>
            <if test="workCenterName != null">work_center_name,</if>
            <if test="executionSort != null">execution_sort,</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>
            <if test="stationType != null">#{stationType},</if>
            <if test="workCenterCode != null">#{workCenterCode},</if>
            <if test="workCenterName != null">#{workCenterName},</if>
            <if test="executionSort != null">#{executionSort},</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>
            <if test="stationType != null">station_type = #{stationType},</if>
            <if test="workCenterCode != null">work_center_code = #{workCenterCode},</if>
            <if test="workCenterName != null">work_center_name = #{workCenterName},</if>
            <if test="executionSort != null">execution_sort = #{executionSort},</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>

    <select id="selectReportQualityStationCode" parameterType="com.aucma.base.domain.BaseProductLine" resultType="com.aucma.base.domain.BaseProductLine">
        SELECT PRODUCT_LINE_CODE productLineCode, PRODUCT_LINE_NAME productLineName, EXECUTION_SORT executionSort
        FROM BASE_PRODUCTLINE
        WHERE PRODUCT_LINE_TYPE = 2
        AND STATION_TYPE = 2
        AND EXECUTION_SORT IS NOT NULL
        AND PARENT_ID = (SELECT PARENT_ID FROM BASE_PRODUCTLINE WHERE PRODUCT_LINE_CODE = #{productLineCode})
        AND PRODUCT_LINE_CODE NOT IN ('2015', '2009')
        ORDER BY EXECUTION_SORT
    </select>
</mapper>