<?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="comp_no" property="compNo"/>
        <result column="comp_name" property="compName"/>
        <result column="comp_name_short" property="compNameShort"/>
        <result column="parent_company" property="parentCompany"/>
        <result column="ec_company" property="ecCompany"/>
        <result column="org_code" property="orgCode"/>
        <result column="industry" property="industry"/>
        <result column="comp_principal" property="compPrincipal"/>
        <result column="description" property="description"/>
        <result column="forbidden_tag" property="forbiddenTag"/>
        <result column="show_order" property="showOrder"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>

        <result column="uuid" property="uuid"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.comp_no
        , t.comp_name
        , t.comp_name_short
        , t.parent_company
        , t.ec_company
        , t.org_code
        , t.industry
        , t.comp_principal
        , t.description
        , t.forbidden_tag
        , t.show_order
        , t.creator
        , t.delete_type
        , t.create_time
        , t.update_time
        , t.uuid
    </sql>

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

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

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

    <sql id="nullParentCompany">
        and ifnull(parent_company,0) =
        #{parentCompany}
    </sql>

    <sql id="nullParentCompany" databaseId="sqlserver">
        and isnull(parent_company,0) =
        #{parentCompany}
    </sql>

    <sql id="nullParentCompany" databaseId="oracle">
        and NVL(parent_company,0) =
        #{parentCompany}
    </sql>

    <insert id="insertIgnoreNull" parameterType="com.engine.organization.entity.company.po.CompPO" keyProperty="id"
            keyColumn="id" useGeneratedKeys="true">
        INSERT INTO jcl_org_comp
        <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="compNo != null ">
                comp_no,
            </if>
            <if test="compName != null ">
                comp_name,
            </if>
            <if test="compNameShort != null ">
                comp_name_short,
            </if>
            <if test="parentCompany != null ">
                parent_company,
            </if>
            <if test="ecCompany != null ">
                ec_company,
            </if>
            <if test="orgCode != null ">
                org_code,
            </if>
            <if test="industry != null ">
                industry,
            </if>
            <if test="compPrincipal != null ">
                comp_principal,
            </if>
            <if test="description != null ">
                description,
            </if>
            forbidden_tag,
            <if test="showOrder != null ">
                show_order,
            </if>
        </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="compNo != null ">
                #{compNo},
            </if>
            <if test="compName != null ">
                #{compName},
            </if>
            <if test="compNameShort != null ">
                #{compNameShort},
            </if>
            <if test="parentCompany != null ">
                #{parentCompany},
            </if>
            <if test="ecCompany != null ">
                #{ecCompany},
            </if>
            <if test="orgCode != null ">
                #{orgCode},
            </if>
            <if test="industry != null ">
                #{industry},
            </if>
            <if test="compPrincipal != null ">
                #{compPrincipal},
            </if>
            <if test="description != null ">
                #{description},
            </if>
            0,
            <if test="showOrder != null ">
                #{showOrder},
            </if>
        </trim>
    </insert>

    <insert id="insertIgnoreNull" parameterType="com.engine.organization.entity.company.po.CompPO" databaseId="oracle">
        <selectKey keyProperty="id" resultType="long" order="AFTER">
            select JCL_ORG_COMP_ID.currval from dual
        </selectKey>
        INSERT INTO jcl_org_comp
        <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="compNo != null ">
                comp_no,
            </if>
            <if test="compName != null ">
                comp_name,
            </if>
            <if test="compNameShort != null ">
                comp_name_short,
            </if>
            <if test="parentCompany != null ">
                parent_company,
            </if>
            <if test="ecCompany != null ">
                ec_company,
            </if>
            <if test="orgCode != null ">
                org_code,
            </if>
            <if test="industry != null ">
                industry,
            </if>
            <if test="compPrincipal != null ">
                comp_principal,
            </if>
            <if test="description != null ">
                description,
            </if>
            forbidden_tag,
            <if test="showOrder != null ">
                show_order,
            </if>
        </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="compNo != null ">
                #{compNo},
            </if>
            <if test="compName != null ">
                #{compName},
            </if>
            <if test="compNameShort != null ">
                #{compNameShort},
            </if>
            <if test="parentCompany != null ">
                #{parentCompany},
            </if>
            <if test="ec_company != null ">
                #{ecCompany},
            </if>
            <if test="orgCode != null ">
                #{orgCode},
            </if>
            <if test="industry != null ">
                #{industry},
            </if>
            <if test="compPrincipal != null ">
                #{compPrincipal},
            </if>
            <if test="description != null ">
                #{description},
            </if>
            0,
            <if test="showOrder != null ">
                #{showOrder},
            </if>
        </trim>
    </insert>

    <select id="list" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        jcl_org_comp t
        WHERE t.delete_type = 0 order by ${orderSql}
    </select>

    <select id="listParent" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        jcl_org_comp t
        WHERE t.delete_type = 0
        <include refid="nullSql"/>
    </select>

    <select id="listChild" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        jcl_org_comp t
        WHERE t.delete_type = 0
        AND parent_company IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="listById" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        jcl_org_comp t
        WHERE t.delete_type = 0
        and id = #{id}
    </select>

    <select id="listByNo" parameterType="com.engine.organization.entity.company.po.CompPO" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_comp t where comp_no = #{compNo} AND delete_type = 0
    </select>
    <select id="listByFilter" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        jcl_org_comp t
        WHERE t.delete_type = 0
        <include refid="likeSQL"/>
        <if test=" compPO.parentCompany != null ">
            and t.parent_company = #{compPO.parentCompany}
        </if>
        <if test=" compPO.ecCompany != null ">
            and t.ec_company = #{compPO.ecCompany}
        </if>
        <if test=" compPO.industry != null ">
            and t.industry = #{compPO.industry}
        </if>
        <if test=" compPO.compPrincipal != null ">
            and t.comp_principal = #{compPO.compPrincipal}
        </if>
        <if test=" compPO.forbiddenTag != null ">
            and t.forbidden_tag = #{compPO.forbiddenTag}
        </if>
        order by ${orderSql}
    </select>

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

    <select id="listChildByPID" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        jcl_org_comp t
        WHERE t.delete_type = 0
        and parent_company = #{pid}
    </select>
    <select id="countChildByPID" resultType="java.lang.Integer">
        SELECT count(1)
        FROM jcl_org_comp t
        WHERE t.delete_type = 0
          and parent_company = #{pid}
    </select>
    <select id="getCompsByIds" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_comp t
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="listUsedId" resultType="java.lang.String">
        select parent_comp
        from JCL_ORG_DEPT
        where delete_type = 0
        union
        select parent_comp
        from JCL_ORG_JOB
        where delete_type = 0
        union
        select comp_id
        from JCL_ORG_STAFF
        where delete_type = 0
    </select>
    <select id="getMaxShowOrder" resultType="java.lang.Integer">
        select max(show_order)
        from jcl_org_comp
    </select>
    <select id="getIdByNameAndPid" resultType="java.lang.Long">
        select id
        from jcl_org_comp
        where delete_type = 0 and comp_name = #{companyName}
        <include refid="nullParentCompany"/>
    </select>
    <select id="countTopCompany" resultType="java.lang.Integer">
        select COUNT(id) from jcl_org_comp where 1=1
        <include refid="nullParentCompany"/>
    </select>
    <select id="listUsedIds" resultType="java.lang.String">
        select company_id
        from JCL_ORG_STAFFPLAN
        where delete_type = 0
        union
        select jcl_rolelevel
        from jcl_org_detach
        where delete_type = 0
    </select>
    <select id="getCompanyByUUID" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        jcl_org_comp t
        WHERE t.delete_type = 0
        and uuid = #{uuid}
    </select>
    <select id="checkRepeatNo" resultType="java.lang.Integer">
        select count(1)
        from jcl_org_comp t
        where t.delete_type = 0
        AND comp_no = #{companyNo}
        <if test=" id != null ">
            and t.id != #{id}
        </if>
    </select>
    <select id="getCompanyByNo" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from from jcl_org_comp t
        where t.delete_type = 0
        AND comp_no = #{companyNo}
    </select>
    <select id="hasSubs" resultType="java.lang.String">
        select parent_company
        from jcl_org_comp
        where forbidden_tag = 0
          and delete_type = 0
        union
        select parent_comp
        from jcl_org_dept
        where forbidden_tag = 0
          and delete_type = 0
    </select>
    <select id="getCompanyIdsByUuid" resultType="java.lang.Long">
        select id from jcl_org_comp
        where delete_type = 0 and uuid in
        <foreach collection="uuids" open="(" item="uuid" separator="," close=")">
            #{uuid}
        </foreach>
    </select>
    <select id="hasDetachSubs" resultType="java.lang.String">
        select parent_company
        from jcl_org_comp
        where forbidden_tag = 0
        and delete_type = 0
        and id in
        <foreach collection="companyIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
        union
        select parent_comp
        from jcl_org_dept
        where forbidden_tag = 0
        and delete_type = 0
        and parent_comp in
        <foreach collection="companyIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>

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

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

    <update id="updateBaseComp" parameterType="com.engine.organization.entity.company.po.CompPO">
        update jcl_org_comp
        <set>
            creator=#{creator},
            update_time=#{updateTime},
            comp_name=#{compName},
            comp_name_short=#{compNameShort},
            parent_company=#{parentCompany},
            ec_company=#{ecCompany},
            org_code=#{orgCode},
            industry=#{industry},
            comp_principal=#{compPrincipal},
            description=#{description},
            show_order=#{showOrder},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <sql id="likeSQL">
        <if test=" compPO.compNo != null and compPO.compNo != '' ">
            and t.comp_no like CONCAT('%',#{compPO.compNo},'%')
        </if>
        <if test=" compPO.compName != null and compPO.compName != '' ">
            and t.comp_name like CONCAT('%',#{compPO.compName},'%')
        </if>
        <if test=" compPO.compNameShort != null and compPO.compNameShort != '' ">
            and t.comp_name_short like CONCAT('%',#{compPO.compNameShort},'%')
        </if>
        <if test=" compPO.orgCode != null and compPO.orgCode != '' ">
            and t.org_code like CONCAT('%',#{compPO.orgCode},'%')
        </if>
    </sql>

    <sql id="likeSQL" databaseId="oracle">
        <if test=" compPO.compNo != null and compPO.compNo != '' ">
            and t.comp_no like '%'||#{compPO.compNo}||'%'
        </if>
        <if test=" compPO.compName != null and compPO.compName != '' ">
            and t.comp_name like '%'||#{compPO.compName}||'%'
        </if>
        <if test=" compPO.compNameShort != null and compPO.compNameShort != '' ">
            and t.comp_name_short like '%'||#{compPO.compNameShort}||'%'
        </if>
        <if test=" compPO.orgCode != null and compPO.orgCode != '' ">
            and t.org_code like '%'||#{compPO.orgCode}||'%'
        </if>
    </sql>

    <sql id="likeSQL" databaseId="sqlserver">
        <if test=" compPO.compNo != null and compPO.compNo != '' ">
            and t.comp_no like '%'+#{compPO.compNo}+'%'
        </if>
        <if test=" compPO.compName != null and compPO.compName != '' ">
            and t.comp_name like '%'+#{compPO.compName}+'%'
        </if>
        <if test=" compPO.compNameShort != null and compPO.compNameShort != '' ">
            and t.comp_name_short like '%'+#{compPO.compNameShort}+'%'
        </if>
        <if test=" compPO.orgCode != null and compPO.orgCode != '' ">
            and t.org_code like '%'+#{compPO.orgCode}+'%'
        </if>
    </sql>

</mapper>