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

    <resultMap type="BaseWorkUnit" id="BaseWorkUnitResult">
            <result property="objId"    column="obj_id"    />
            <result property="workUnitCode"    column="work_unit_code"    />
            <result property="parentId"    column="parent_id"    />
            <result property="workUnitName"    column="work_unit_name"    />
            <result property="workUnitAddress"    column="work_unit_address"    />
            <result property="ancestors"    column="ancestors"    />
            <result property="workUnitSort"    column="work_unit_sort"    />
            <result property="productLineCode"    column="product_line_code"    />
            <result property="productLineName"    column="product_line_name"    />
            <result property="workUnitType"    column="work_unit_type"    />
            <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"    />
    </resultMap>

    <sql id="selectBaseWorkUnitVo">
        select obj_id, work_unit_code, parent_id, work_unit_name, work_unit_address, ancestors, work_unit_sort, product_line_code, work_unit_type, is_flag, created_by, created_time, updated_by, updated_time from base_work_unit
    </sql>

    <select id="selectBaseWorkUnitList" parameterType="BaseWorkUnit" resultMap="BaseWorkUnitResult">
        <include refid="selectBaseWorkUnitVo"/>
        <where>
                        <if test="objId != null "> and obj_id = #{objId}</if>
                        <if test="workUnitCode != null  and workUnitCode != ''"> and work_unit_code = #{workUnitCode}</if>
                        <if test="parentId != null  and parentId != ''"> and parent_id = #{parentId}</if>
                        <if test="workUnitName != null  and workUnitName != ''"> and work_unit_name like concat(concat('%', #{workUnitName}), '%')</if>
                        <if test="workUnitAddress != null  and workUnitAddress != ''"> and work_unit_address = #{workUnitAddress}</if>
                        <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
                        <if test="workUnitSort != null "> and work_unit_sort = #{workUnitSort}</if>
                        <if test="productLineCode != null  and productLineCode != ''"> and product_line_code = #{productLineCode}</if>
                        <if test="workUnitType != null "> and work_unit_type = #{workUnitType}</if>
                        <if test="isFlag != null "> and is_flag = #{isFlag}</if>
                        <if test="createdBy != null  and createdBy != ''"> and created_by = #{createdBy}</if>
                        <if test="createdTime != null "> and created_time = #{createdTime}</if>
                        <if test="updatedBy != null  and updatedBy != ''"> and updated_by = #{updatedBy}</if>
                        <if test="updatedTime != null "> and updated_time = #{updatedTime}</if>
        </where>
    </select>

    <select id="selectBaseWorkUnitByObjId" parameterType="Long" resultMap="BaseWorkUnitResult">
            <include refid="selectBaseWorkUnitVo"/>
            where obj_id = #{objId}
    </select>

    <insert id="insertBaseWorkUnit" parameterType="BaseWorkUnit">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_report_day_dnb.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into base_work_unit
        <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="objId != null">obj_id,</if>
                <if test="workUnitCode != null and workUnitCode != ''">work_unit_code,</if>
                <if test="parentId != null">parent_id,</if>
                <if test="workUnitName != null">work_unit_name,</if>
                <if test="workUnitAddress != null">work_unit_address,</if>
                <if test="ancestors != null">ancestors,</if>
                <if test="workUnitSort != null">work_unit_sort,</if>
                <if test="productLineCode != null">product_line_code,</if>
                <if test="workUnitType != null">work_unit_type,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="objId != null">#{objId},</if>
                <if test="workUnitCode != null and workUnitCode != ''">#{workUnitCode},</if>
                <if test="parentId != null">#{parentId},</if>
                <if test="workUnitName != null">#{workUnitName},</if>
                <if test="workUnitAddress != null">#{workUnitAddress},</if>
                <if test="ancestors != null">#{ancestors},</if>
                <if test="workUnitSort != null">#{workUnitSort},</if>
                <if test="productLineCode != null">#{productLineCode},</if>
                <if test="workUnitType != null">#{workUnitType},</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>
        </trim>
    </insert>

    <update id="updateBaseWorkUnit" parameterType="BaseWorkUnit">
        update base_work_unit
        <trim prefix="SET" suffixOverrides=",">
                    <if test="workUnitCode != null and workUnitCode != ''">work_unit_code = #{workUnitCode},</if>
                    <if test="parentId != null">parent_id = #{parentId},</if>
                    <if test="workUnitName != null">work_unit_name = #{workUnitName},</if>
                    <if test="workUnitAddress != null">work_unit_address = #{workUnitAddress},</if>
                    <if test="ancestors != null">ancestors = #{ancestors},</if>
                    <if test="workUnitSort != null">work_unit_sort = #{workUnitSort},</if>
                    <if test="productLineCode != null">product_line_code = #{productLineCode},</if>
                    <if test="workUnitType != null">work_unit_type = #{workUnitType},</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>
        </trim>
        where obj_id = #{objId}
    </update>

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

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