<?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="departmentMark" property="departmentMark"/>
        <result column="departmentName" property="departmentName"/>
        <result column="subCompanyId1" property="subCompanyId1"/>
        <result column="supDepId" property="supDepId"/>
        <result column="allSupDepId" property="allSupDepId"/>
        <result column="canceled" property="canceled"/>
        <result column="departmentCode" property="departmentCode"/>
        <result column="coadjutant" property="coadjutant"/>
        <result column="showOrder" property="showOrder"/>
        <result column="uuid" property="uuid"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        ,
        t.departmentMark,
        t.departmentName,
        t.subCompanyId1,
        t.supDepId,
        t.allSupDepId,
        t.canceled,
        t.departmentCode,
        t.coadjutant,
        t.showOrder,
        t.uuid
    </sql>

    <select id="getDeptListByPId" resultType="com.engine.organization.entity.department.po.DepartmentPO">
        select
        <include refid="baseColumns"/>
        from hrmdepartment t
        where supDepId = #{PId}
    </select>

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

    <select id="listByFilter" parameterType="com.engine.organization.entity.department.po.DepartmentPO"
            resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from hrmdepartment t
        where 1=1
        <include refid="likeSQL"/>
        <if test=" departmentPO.subCompanyId1 != null ">
            and t.subCompanyId1 = #{departmentPO.subCompanyId1}
        </if>
        <if test=" departmentPO.supDepId != null ">
            and t.supDepId = #{departmentPO.supDepId}
        </if>
        <if test=" departmentPO.departmentId != null ">
            and t.id = #{departmentPO.departmentId}
        </if>
        <include refid="isCanceled"/>
        order by ${orderSql}
    </select>

    <select id="getDeptById" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from hrmdepartment t
        where id = #{id}
    </select>

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

    <select id="listAll" resultType="com.engine.organization.entity.department.po.DepartmentPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrmdepartment t order by ${orderSql}
    </select>
    <select id="listByNo" resultType="com.engine.organization.entity.department.po.DepartmentPO">
        select
        <include refid="baseColumns"/>
        from hrmdepartment t where departmentCode = #{departmentCode}
    </select>
    <select id="listUsedId" resultType="java.lang.String">
        select ec_department
        from JCL_ORG_JOB
        where delete_type = 0
        union
        select dept_id
        from JCL_ORG_STAFF
        where delete_type = 0
        union
        select departmentid
        from hrmresource
    </select>
    <select id="getDeptsByIds" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from hrmdepartment t
        where id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="getMaxShowOrder" resultType="java.lang.Double">
        select max(showOrder)
        from hrmdepartment
    </select>
    <select id="getIdByNameAndPid" resultType="java.lang.Integer">
        select id
        from hrmdepartment
        where departmentName = #{departmentName}
          and subCompanyId1 = #{subCompanyId1}
          and supDepId = #{supDepId}
    </select>
    <select id="checkRepeatNo" resultType="java.lang.Integer">
        select count(1)
        from hrmdepartment t
        where departmentCode = #{departmentCode}
        <if test=" id != null ">
            and t.id != #{id}
        </if>
    </select>
    <select id="hasSubs" resultType="java.lang.String">
        select distinct supDepId
        from hrmdepartment
        where 1=1
        <include refid="com.engine.organization.mapper.comp.CompMapper.subsWhere"/>
    </select>
    <select id="countUsedInJob" resultType="java.lang.Integer">
        select count(1)
        from jcl_org_job
        where forbidden_tag = 0
          and delete_type = 0
          and parent_dept = #{supDepId}
    </select>
    <select id="getIdByDepartmentCode" resultType="java.lang.String">
        select id
        from hrmdepartment
        where departmentcode = #{departmentCode}
    </select>
    <select id="getDepartmentPrincipal" resultType="java.lang.String">
        select bmfzr
        from hrmdepartmentdefined
        where deptid = #{deptId}
    </select>

    <sql id="likeSQL">
        <if test=" departmentPO.departmentCode != null and departmentPO.departmentCode != '' ">
            and t.departmentCode like CONCAT('%',#{departmentPO.departmentCode},'%')
        </if>
        <if test=" departmentPO.departmentName != null and departmentPO.departmentName != '' ">
            and t.departmentName like CONCAT('%',#{departmentPO.departmentName},'%')
        </if>
    </sql>

    <sql id="likeSQL" databaseId="oracle">
        <if test=" departmentPO.departmentCode != null and departmentPO.departmentCode != '' ">
            and t.departmentCode like '%'||#{departmentPO.departmentCode}||'%'
        </if>
        <if test=" departmentPO.departmentName != null and departmentPO.departmentName != '' ">
            and t.departmentName like '%'||#{departmentPO.departmentName}||'%'
        </if>
    </sql>
    <sql id="likeSQL" databaseId="postgresql">
        <if test=" departmentPO.departmentCode != null and departmentPO.departmentCode != '' ">
            and t.departmentCode like '%'||#{departmentPO.departmentCode}||'%'
        </if>
        <if test=" departmentPO.departmentName != null and departmentPO.departmentName != '' ">
            and t.departmentName like '%'||#{departmentPO.departmentName}||'%'
        </if>
    </sql>

    <sql id="likeSQL" databaseId="sqlserver">
        <if test=" departmentPO.departmentCode != null and departmentPO.departmentCode != '' ">
            and t.departmentCode like '%'+#{departmentPO.departmentCode}+'%'
        </if>
        <if test=" departmentPO.departmentName != null and departmentPO.departmentName != '' ">
            and t.departmentName like '%'+#{departmentPO.departmentName}+'%'
        </if>
    </sql>

    <sql id="isCanceled">
        <if test=" departmentPO.canceled != null ">
            and ifnull(canceled,0)= #{departmentPO.canceled}
        </if>
    </sql>
    <sql id="isCanceled" databaseId="oracle">
        <if test=" departmentPO.canceled != null">
            and nvl(canceled,0)= #{departmentPO.canceled}
        </if>
    </sql>
    <sql id="isCanceled" databaseId="sqlserver">
        <if test=" departmentPO.canceled != null">
            and isnull(canceled,0)= #{departmentPO.canceled}
        </if>
    </sql>
    <sql id="isCanceled" databaseId="postgresql">
        <if test=" departmentPO.canceled != null">
            and COALESCE(canceled,'0')= #{departmentPO.canceled}
        </if>
    </sql>

</mapper>