<?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.engine.organization.mapper.department.DepartmentMapper">
    <resultMap id="BaseResultMap" type="com.engine.organization.entity.department.po.DepartmentPO">
        <result column="id" property="id"/>
        <result column="dept_no" property="deptNo"/>
        <result column="dept_name" property="deptName"/>
        <result column="dept_name_short" property="deptNameShort"/>
        <result column="parent_comp" property="parentComp"/>
        <result column="parent_dept" property="parentDept"/>
        <result column="dept_principal" property="deptPrincipal"/>
        <result column="show_order" property="showOrder"/>
        <result column="description" property="description"/>
        <result column="forbidden_tag" property="forbiddenTag"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        ,
        t.dept_no,
        t.dept_name,
        t.dept_name_short,
        t.parent_comp,
        t.parent_dept,
        t.dept_principal,
        t.show_order,
        t.description,
        t.forbidden_tag
    </sql>

    <select id="getDeptListByCompId" resultType="com.engine.organization.entity.department.po.DepartmentPO">
        select t.id, t.dept_name, t.parent_dept, t.dept_principal, t.parent_dept, t.parent_comp
        from jcl_org_dept t
        where delete_type = 0
          and parent_comp = #{parentComp}
    </select>

    <select id="getDeptListByPId" resultType="com.engine.organization.entity.department.po.DepartmentPO">
        select t.id, t.dept_name, t.parent_dept, t.dept_principal
        from jcl_org_dept t
        where delete_type = 0
          and parent_dept = #{PId}
    </select>

    <select id="getDeptNameById" resultType="string">
        select t.dept_name
        from jcl_org_dept t
        where id = #{id}
    </select>

    <select id="listByFilter" parameterType="com.engine.organization.entity.department.po.DepartmentPO"
            resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_dept t
        where delete_type = 0
        <include refid="likeSQL"/>
        <if test=" departmentPO.parentComp != null ">
            and t.parent_comp = #{departmentPO.parentComp}
        </if>
        <if test=" departmentPO.parentDept != null ">
            and t.parent_dept = #{departmentPO.parentDept}
        </if>
        <if test=" departmentPO.deptPrincipal != null ">
            and t.dept_principal = #{departmentPO.deptPrincipal}
        </if>
        <if test=" departmentPO.showOrder != null ">
            and t.show_order = #{departmentPO.showOrder}
        </if>
        <if test=" departmentPO.forbiddenTag != null ">
            and t.forbidden_tag = #{departmentPO.forbiddenTag}
        </if>
        order by ${orderSql}
    </select>

    <select id="getDeptById" resultType="com.engine.organization.entity.department.po.DepartmentPO">
        select
        <include refid="baseColumns"/>
        from jcl_org_dept t
        where delete_type = 0
        and id = #{id}
    </select>

    <select id="listDeptsByIds" resultType="java.util.Map">
        select
        id,
        dept_name as name
        from jcl_org_dept t
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="list" resultType="com.engine.organization.entity.department.po.DepartmentPO">
        SELECT
        <include refid="baseColumns"/>
        FROM jcl_org_dept t
        WHERE t.delete_type = 0 order by ${orderSql}
    </select>
    <select id="listByNo" resultType="com.engine.organization.entity.department.po.DepartmentPO">
        select
        <include refid="baseColumns"/>
        from jcl_org_dept t where dept_no = #{deptNo} AND delete_type = 0
    </select>
    <select id="countChildByPID" resultType="java.lang.Integer">
        select count(1)
        from jcl_org_dept t
        where delete_type = 0
          and parent_dept = #{pid}
    </select>
    <select id="listUsedId" resultType="java.lang.String">
        select parent_dept
        from JCL_ORG_JOB
        where delete_type = 0
        union
        select dept_id
        from JCL_ORG_STAFF
        where delete_type = 0
    </select>
    <select id="getDeptsByIds" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_dept t
        where delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="getMaxShowOrder" resultType="java.lang.Integer">
        select max(show_order)
        from jcl_org_dept
    </select>

    <insert id="insertIgnoreNull" parameterType="com.engine.organization.entity.department.po.DepartmentPO"
            keyProperty="id"
            keyColumn="id" useGeneratedKeys="true">
        INSERT INTO jcl_org_dept
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>

            <if test="deptNo != null ">
                dept_no,
            </if>
            <if test="deptName != null ">
                dept_name,
            </if>
            <if test="deptNameShort != null ">
                dept_name_short,
            </if>
            <if test="parentComp != null ">
                parent_comp,
            </if>
            <if test="parentDept != null ">
                parent_dept,
            </if>
            <if test="deptPrincipal != null ">
                dept_principal,
            </if>
            <if test="showOrder != null ">
                show_order,
            </if>
            <if test="description != null ">
                description,
            </if>
            forbidden_tag,
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="deptNo != null ">
                #{deptNo},
            </if>
            <if test="deptName != null ">
                #{deptName},
            </if>
            <if test="deptNameShort != null ">
                #{deptNameShort},
            </if>
            <if test="parentComp != null ">
                #{parentComp},
            </if>
            <if test="parentDept != null ">
                #{parentDept},
            </if>
            <if test="deptPrincipal != null ">
                #{deptPrincipal},
            </if>
            <if test="showOrder != null ">
                #{showOrder},
            </if>
            <if test="description != null ">
                #{description},
            </if>
            0,
        </trim>
    </insert>

    <update id="updateBaseDept" parameterType="com.engine.organization.entity.department.po.DepartmentPO">
        update jcl_org_dept
        <set>
            creator=#{creator},
            update_time=#{updateTime},
            dept_name=#{deptName},
            dept_name_short=#{deptNameShort},
            parent_comp=#{parentComp},
            parent_dept=#{parentDept},
            dept_principal=#{deptPrincipal},
            show_order=#{showOrder},
            description=#{description},
            <if test="forbiddenTag !=null">
                forbidden_tag=#{forbiddenTag},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <update id="updateForbiddenTagById" parameterType="com.engine.organization.entity.department.po.DepartmentPO">
        update jcl_org_dept
        <set>
            forbidden_tag=#{forbiddenTag},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <update id="deleteByIds">
        UPDATE jcl_org_dept
        SET delete_type = 1
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <sql id="likeSQL">
        <if test=" departmentPO.deptNo != null and departmentPO.deptNo != '' ">
            and t.dept_no like CONCAT('%',#{departmentPO.deptNo},'%')
        </if>
        <if test=" departmentPO.deptName != null and departmentPO.deptName != '' ">
            and t.dept_name like CONCAT('%',#{departmentPO.deptName},'%')
        </if>
        <if test=" departmentPO.deptNameShort != null and departmentPO.deptNameShort != '' ">
            and t.dept_name_short like CONCAT('%',#{departmentPO.deptNameShort},'%')
        </if>
    </sql>

    <sql id="likeSQL" databaseId="oracle">
        <if test=" departmentPO.deptNo != null and departmentPO.deptNo != '' ">
            and t.dept_no like '%'||#{departmentPO.deptNo}||'%'
        </if>
        <if test=" departmentPO.deptName != null and departmentPO.deptName != '' ">
            and t.dept_name like '%'||#{departmentPO.deptName}||'%'
        </if>
        <if test=" departmentPO.deptNameShort != null and departmentPO.deptNameShort != '' ">
            and t.dept_name_short like '%'||#{departmentPO.deptNameShort}||'%'
        </if>
    </sql>

    <sql id="likeSQL" databaseId="sqlserver">
        <if test=" departmentPO.deptNo != null and departmentPO.deptNo != '' ">
            and t.dept_no like '%'+#{departmentPO.deptNo}+'%'
        </if>
        <if test=" departmentPO.deptName != null and departmentPO.deptName != '' ">
            and t.dept_name like '%'+#{departmentPO.deptName}+'%'
        </if>
        <if test=" departmentPO.deptNameShort != null and departmentPO.deptNameShort != '' ">
            and t.dept_name_short like '%'+#{departmentPO.deptNameShort}+'%'
        </if>
    </sql>

    <sql id="nullSql">
        and ifnull(parent_dept,'0')='0'
    </sql>

    <sql id="nullSql" databaseId="sqlserver">
        and isnull(parent_dept,'0')='0'
    </sql>

    <sql id="nullSql" databaseId="oracle">
        and NVL(parent_dept,'0')='0'
    </sql>

</mapper>