<?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.comp.CompMapper">
    <resultMap id="BaseResultMap" type="com.engine.organization.entity.company.po.CompPO">
        <result column="id" property="id"/>
        <result column="subCompanyName" property="subCompanyName"/>
        <result column="subCompanyDesc" property="subCompanyDesc"/>
        <result column="companyId" property="companyId"/>
        <result column="supSubComId" property="supSubComId"/>
        <result column="url" property="url"/>
        <result column="canceled" property="canceled"/>
        <result column="subCompanyCode" property="subCompanyCode"/>
        <result column="outKey" property="outKey"/>
        <result column="limitUsers" property="limitUsers"/>
        <result column="uuid" property="uuid"/>
        <result column="showOrder" property="showOrder"/>
        <result column="showOrderOfTree" property="showOrderOfTree"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.subCompanyName
        , t.subCompanyDesc
        , t.companyId
        , t.supSubComId
        , t.url
        , t.canceled
        , t.subCompanyCode
        , t.outKey
        , t.limitUsers
        , t.uuid
        , t.showOrder
        , t.showOrderOfTree
    </sql>

    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrmsubcompany t
        order by ${orderSql}
    </select>

    <select id="listByFilter" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrmsubcompany t
        WHERE 1=1
        <include refid="likeSQL"/>
        <if test=" CompanyPO.supSubComId != null ">
            and t.supSubComId = #{CompanyPO.supSubComId}
        </if>
        <if test=" CompanyPO.canceled != null ">
            <include refid="isCanceled"/>
        </if>
        order by ${orderSql}
    </select>

    <select id="listUsedId" resultType="java.lang.String">
        select SUBCOMPANYID1
        from hrmdepartment
        union
        select ec_company
        from JCL_ORG_JOB
        where delete_type = 0
        union
        select ec_company
        from JCL_ORG_STAFF
        where delete_type = 0
    </select>

    <select id="listUsedIds" resultType="java.lang.String">
        select ec_company
        from JCL_ORG_STAFFPLAN
        where delete_type = 0
        union
        select ec_rolelevel
        from jcl_org_detach
        where delete_type = 0
    </select>

    <select id="listChild" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrmsubcompany t
        WHERE supsubcomid IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="listById" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrmsubcompany t
        WHERE id = #{id}
    </select>

    <select id="getCompsByIds" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from hrmsubcompany t
        WHERE id IN
        <foreach collection="ids" index="index" open="(" item="id" separator="," close=")">
            <if test="(index % 999) == 998"> NULL) OR id IN(</if>#{id}
        </foreach>
    </select>

    <select id="listParent" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        hrmsubcompany t
        WHERE t.SUPSUBCOMID = 0
    </select>

    <select id="hasSubs" resultType="java.lang.String">
        select supsubcomid
        from hrmsubcompany
        where 1=1
        <include refid="subsWhere"/>
        union
        select subcompanyid1
        from hrmdepartment
        where 1=1
        <include refid="subsWhere"/>
    </select>

    <select id="hasDetachSubs" resultType="java.lang.String">
        select supsubcomid
        from hrmsubcompany
        where 1=1
        <include refid="subsWhere"/>
        and id in
        <foreach collection="companyIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
        union
        select subcompanyid1
        from hrmdepartment
        where 1=1
        <include refid="subsWhere"/>
        and subcompanyid1 in
        <foreach collection="companyIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="getIdByNameAndPid" resultType="java.lang.Integer">
        select id
        from hrmsubcompany
        where subcompanyname = #{subCompanyName}
          and supsubcomid = #{supSubComId}
    </select>

    <select id="getMaxShowOrder" resultType="java.lang.Integer">
        select max(showorder)
        from hrmsubcompany
    </select>
    <select id="getIdBySubCompanyCode" resultType="java.lang.Integer">
        select id
        from hrmsubcompany
        where subcompanycode = #{subCompanyCode}
    </select>


    <sql id="likeSQL">
        <if test=" CompanyPO.subCompanyCode != null and CompanyPO.subCompanyCode != '' ">
            and t.subCompanyCode like CONCAT('%',#{CompanyPO.subCompanyCode},'%')
        </if>
        <if test=" CompanyPO.subCompanyDesc != null and CompanyPO.subCompanyDesc != '' ">
            and t.subCompanyDesc like CONCAT('%',#{CompanyPO.subCompanyDesc},'%')
        </if>
        <if test=" CompanyPO.subCompanyName != null and CompanyPO.subCompanyName != '' ">
            and t.subCompanyName like CONCAT('%',#{CompanyPO.subCompanyName},'%')
        </if>
    </sql>

    <sql id="likeSQL" databaseId="oracle">
        <if test=" CompanyPO.subCompanyCode != null and CompanyPO.subCompanyCode != '' ">
            and t.subCompanyCode like '%'||#{CompanyPO.subCompanyCode}||'%'
        </if>
        <if test=" CompanyPO.subCompanyDesc != null and CompanyPO.subCompanyDesc != '' ">
            and t.subCompanyDesc like '%'||#{CompanyPO.subCompanyDesc}||'%'
        </if>
        <if test=" CompanyPO.subCompanyName != null and CompanyPO.subCompanyName != '' ">
            and t.subCompanyName like '%'||#{CompanyPO.subCompanyName}||'%'
        </if>
    </sql>
    <sql id="likeSQL" databaseId="postgresql">
        <if test=" CompanyPO.subCompanyCode != null and CompanyPO.subCompanyCode != '' ">
            and t.subCompanyCode like '%'||#{CompanyPO.subCompanyCode}||'%'
        </if>
        <if test=" CompanyPO.subCompanyDesc != null and CompanyPO.subCompanyDesc != '' ">
            and t.subCompanyDesc like '%'||#{CompanyPO.subCompanyDesc}||'%'
        </if>
        <if test=" CompanyPO.subCompanyName != null and CompanyPO.subCompanyName != '' ">
            and t.subCompanyName like '%'||#{CompanyPO.subCompanyName}||'%'
        </if>
    </sql>

    <sql id="likeSQL" databaseId="sqlserver">
        <if test=" CompanyPO.subCompanyCode != null and CompanyPO.subCompanyCode != '' ">
            and t.subCompanyCode like '%'+#{CompanyPO.subCompanyCode}+'%'
        </if>
        <if test=" CompanyPO.subCompanyDesc != null and CompanyPO.subCompanyDesc != '' ">
            and t.subCompanyDesc like '%'+#{CompanyPO.subCompanyDesc}+'%'
        </if>
        <if test=" CompanyPO.subCompanyName != null and CompanyPO.subCompanyName != '' ">
            and t.subCompanyName like '%'+#{CompanyPO.subCompanyName}+'%'
        </if>
    </sql>

    <sql id="subsWhere">
        and ifnull(canceled,0)=0
    </sql>
    <sql id="subsWhere" databaseId="oracle">
        and nvl(canceled,0)=0
    </sql>
    <sql id="subsWhere" databaseId="sqlserver">
        and isnull(canceled,0)=0
    </sql>
    <sql id="subsWhere" databaseId="postgresql">
        and COALESCE(canceled,'0')='0'
    </sql>

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

</mapper>